diff --git a/components/content_settings/core/browser/brave_content_settings_pref_provider.h b/components/content_settings/core/browser/brave_content_settings_pref_provider.h index 7b826334fffa..4927138f6ff8 100644 --- a/components/content_settings/core/browser/brave_content_settings_pref_provider.h +++ b/components/content_settings/core/browser/brave_content_settings_pref_provider.h @@ -52,6 +52,8 @@ class BravePrefProvider : public PrefProvider, FRIEND_TEST_ALL_PREFIXES(BravePrefProviderTest, TestShieldsSettingsMigration); FRIEND_TEST_ALL_PREFIXES(BravePrefProviderTest, TestShieldsSettingsMigrationVersion); + FRIEND_TEST_ALL_PREFIXES(BravePrefProviderTest, + TestShieldsSettingsMigrationFromResourceIDs); void MigrateShieldsSettings(bool incognito); void MigrateShieldsSettingsFromResourceIds(); void MigrateShieldsSettingsFromResourceIdsForOneType( diff --git a/components/content_settings/core/browser/brave_content_settings_pref_provider_unittest.cc b/components/content_settings/core/browser/brave_content_settings_pref_provider_unittest.cc index d2a9de842a0f..536e44062bf5 100644 --- a/components/content_settings/core/browser/brave_content_settings_pref_provider_unittest.cc +++ b/components/content_settings/core/browser/brave_content_settings_pref_provider_unittest.cc @@ -8,9 +8,11 @@ #include "base/macros.h" #include "base/optional.h" +#include "base/values.h" #include "brave/common/pref_names.h" #include "brave/components/brave_shields/common/brave_shield_constants.h" #include "brave/components/content_settings/core/browser/brave_content_settings_pref_provider.h" +#include "brave/components/content_settings/core/browser/brave_content_settings_utils.h" #include "chrome/test/base/testing_profile.h" #include "components/content_settings/core/browser/content_settings_registry.h" #include "components/content_settings/core/common/content_settings.h" @@ -20,6 +22,8 @@ #include "components/content_settings/core/test/content_settings_test_utils.h" #include "components/prefs/pref_service.h" #include "content/public/test/browser_task_environment.h" +#include "services/preferences/public/cpp/dictionary_value_update.h" +#include "services/preferences/public/cpp/scoped_pref_update.h" #include "testing/gtest/include/gtest/gtest.h" #include "url/gurl.h" @@ -27,6 +31,15 @@ namespace content_settings { namespace { +constexpr char kUserProfilePluginsPath[] = + "profile.content_settings.exceptions.plugins"; + +const char kExpirationPath[] = "expiration"; +const char kLastModifiedPath[] = "last_modified"; +const char kSessionModelPath[] = "model"; +const char kSettingPath[] = "setting"; +const char kPerResourcePath[] = "per_resource"; + using GURLSourcePair = std::pair; ContentSettingsPattern SecondaryUrlToPattern(const GURL& gurl) { @@ -37,6 +50,71 @@ ContentSettingsPattern SecondaryUrlToPattern(const GURL& gurl) { return ContentSettingsPattern::FromString("https://firstParty/*"); } +void InitializeAllShieldSettingsInDictionary( + prefs::DictionaryValueUpdate* dictionary, + const base::Time& last_modified_time, + const int& value) { + const uint64_t last_modified_time_in_ms = + last_modified_time.ToDeltaSinceWindowsEpoch().InMicroseconds(); + + dictionary->SetInteger(kExpirationPath, 0); + dictionary->SetString( + kLastModifiedPath, base::NumberToString(last_modified_time_in_ms)); + dictionary->SetInteger(kSessionModelPath, + static_cast(content_settings::SessionModel::Durable)); + + std::unique_ptr per_resource_dict = + dictionary->SetDictionaryWithoutPathExpansion( + kPerResourcePath, std::make_unique()); + + per_resource_dict->SetInteger(brave_shields::kAds, value); + per_resource_dict->SetInteger(brave_shields::kCookies, value); + per_resource_dict->SetInteger(brave_shields::kCosmeticFiltering, value); + per_resource_dict->SetInteger(brave_shields::kFingerprintingV2, value); + per_resource_dict->SetInteger(brave_shields::kHTTPUpgradableResources, value); + per_resource_dict->SetInteger(brave_shields::kReferrers, value); + per_resource_dict->SetInteger(brave_shields::kTrackers, value); +} + +void InitializeBraveShieldsSettingInDictionary( + prefs::DictionaryValueUpdate* dictionary, + const base::Time& last_modified_time, + const int& value) { + const uint64_t last_modified_time_in_ms = + last_modified_time.ToDeltaSinceWindowsEpoch().InMicroseconds(); + + dictionary->SetInteger(kExpirationPath, 0); + dictionary->SetString( + kLastModifiedPath, base::NumberToString(last_modified_time_in_ms)); + dictionary->SetInteger(kSessionModelPath, + static_cast(content_settings::SessionModel::Durable)); + + std::unique_ptr brave_per_resource_dict = + dictionary->SetDictionaryWithoutPathExpansion( + kPerResourcePath, std::make_unique()); + + brave_per_resource_dict->SetInteger(brave_shields::kBraveShields, value); +} + +void CheckMigrationFromResourceIdentifierForDictionary( + const base::DictionaryValue* dictionary, + const std::string& patterns_string, + const base::Time& expected_last_modified, + const int& expected_setting_value) { + const base::DictionaryValue* settings_dict = nullptr; + dictionary->GetDictionaryWithoutPathExpansion(patterns_string, + &settings_dict); + EXPECT_NE(settings_dict, nullptr); + + int actual_value; + settings_dict->GetInteger(kSettingPath, &actual_value); + EXPECT_EQ(GetTimeStampFromDictionary(settings_dict, kLastModifiedPath), + expected_last_modified); + EXPECT_EQ(GetSessionModelFromDictionary(settings_dict, kSessionModelPath), + content_settings::SessionModel::Durable); + EXPECT_EQ(actual_value, expected_setting_value); +} + class ShieldsSetting { public: ShieldsSetting(BravePrefProvider* provider, @@ -302,7 +380,7 @@ TEST_F(BravePrefProviderTest, TestShieldsSettingsMigrationVersion) { true /* store_last_modified */, false /* restore_session */); - // Should have migrated when constrcuted (with profile). + // Should have migrated when constructed (with profile). EXPECT_EQ(2, prefs->GetInteger(kBraveShieldsSettingsVersion)); // Reset and check that migration runs. @@ -318,4 +396,78 @@ TEST_F(BravePrefProviderTest, TestShieldsSettingsMigrationVersion) { provider.ShutdownOnUIThread(); } +TEST_F(BravePrefProviderTest, TestShieldsSettingsMigrationFromResourceIDs) { + PrefService* pref_service = testing_profile()->GetPrefs(); + BravePrefProvider provider( + pref_service, false /* incognito */, true /* store_last_modified */, + false /* restore_session */); + + // Manually write settings under the PLUGINS type using the no longer existing + // ResourceIdentifier names, and then perform the migration. + prefs::ScopedDictionaryPrefUpdate plugins_pref_update( + pref_service, kUserProfilePluginsPath); + std::unique_ptr plugins_dictionary = + plugins_pref_update.Get(); + EXPECT_NE(plugins_dictionary, nullptr); + + base::Time expected_last_modified = base::Time::Now(); + + // Seed global shield settings with non-default values. + std::unique_ptr global_settings_dict = + plugins_dictionary->SetDictionaryWithoutPathExpansion( + "*,*", std::make_unique()); + + const int expected_global_settings_value = 1; + InitializeAllShieldSettingsInDictionary(global_settings_dict.get(), + expected_last_modified, + expected_global_settings_value); + + // Change all of those global settings for www.example.com. + std::unique_ptr example_settings_dict = + plugins_dictionary->SetDictionaryWithoutPathExpansion( + "www.example.com,*", std::make_unique()); + + const int expected_example_com_settings_value = 1; + InitializeAllShieldSettingsInDictionary(example_settings_dict.get(), + expected_last_modified, + expected_example_com_settings_value); + + // Disable Brave Shields for www.brave.com. + std::unique_ptr brave_settings_dict = + plugins_dictionary->SetDictionaryWithoutPathExpansion( + "www.brave.com,*", std::make_unique()); + + const int expected_brave_com_settings_value = 1; + InitializeBraveShieldsSettingInDictionary(brave_settings_dict.get(), + expected_last_modified, + expected_brave_com_settings_value); + + provider.MigrateShieldsSettingsFromResourceIds(); + + // Check migration for all the settings has been properly done. + for (auto content_type : GetShieldsContentSettingsTypes()) { + const base::DictionaryValue* brave_shields_dict = + pref_service->GetDictionary(GetShieldsSettingUserPrefsPath( + GetShieldsContentTypeName(content_type))); + EXPECT_NE(brave_shields_dict, nullptr); + + if (content_type == ContentSettingsType::BRAVE_SHIELDS) { + // We only changed the value of BRAVE_SHIELDS in www.brave.com. + CheckMigrationFromResourceIdentifierForDictionary( + brave_shields_dict, "www.brave.com,*", expected_last_modified, + expected_brave_com_settings_value); + } else { + // All the other settings we changed them globally and in www.example.com. + CheckMigrationFromResourceIdentifierForDictionary( + brave_shields_dict, "*,*", expected_last_modified, + expected_global_settings_value); + CheckMigrationFromResourceIdentifierForDictionary( + brave_shields_dict, "www.example.com,*", expected_last_modified, + expected_example_com_settings_value); + } + } + + provider.ShutdownOnUIThread(); +} + } // namespace content_settings