OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <memory> | 5 #include <memory> |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "base/auto_reset.h" | 8 #include "base/auto_reset.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
(...skipping 1232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1243 host_content_settings_map->AddObserver(&mock_observer); | 1243 host_content_settings_map->AddObserver(&mock_observer); |
1244 | 1244 |
1245 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 1245 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
1246 host_content_settings_map->GetContentSetting( | 1246 host_content_settings_map->GetContentSetting( |
1247 host, host, CONTENT_SETTINGS_TYPE_COOKIES, std::string())); | 1247 host, host, CONTENT_SETTINGS_TYPE_COOKIES, std::string())); |
1248 host_content_settings_map->SetContentSettingDefaultScope( | 1248 host_content_settings_map->SetContentSettingDefaultScope( |
1249 host, GURL(), CONTENT_SETTINGS_TYPE_COOKIES, std::string(), | 1249 host, GURL(), CONTENT_SETTINGS_TYPE_COOKIES, std::string(), |
1250 CONTENT_SETTING_DEFAULT); | 1250 CONTENT_SETTING_DEFAULT); |
1251 } | 1251 } |
1252 | 1252 |
| 1253 // Guest profiles do not exist on Android, so don't run these tests there. |
| 1254 #if !defined(OS_ANDROID) |
1253 TEST_F(HostContentSettingsMapTest, GuestProfile) { | 1255 TEST_F(HostContentSettingsMapTest, GuestProfile) { |
1254 TestingProfile profile; | 1256 TestingProfile profile; |
1255 profile.SetGuestSession(true); | 1257 profile.SetGuestSession(true); |
1256 HostContentSettingsMap* host_content_settings_map = | 1258 HostContentSettingsMap* host_content_settings_map = |
1257 HostContentSettingsMapFactory::GetForProfile(&profile); | 1259 HostContentSettingsMapFactory::GetForProfile(&profile); |
1258 | 1260 |
1259 GURL host("http://example.com/"); | 1261 GURL host("http://example.com/"); |
1260 ContentSettingsPattern pattern = | 1262 ContentSettingsPattern pattern = |
1261 ContentSettingsPattern::FromString("[*.]example.com"); | 1263 ContentSettingsPattern::FromString("[*.]example.com"); |
1262 | 1264 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1314 profile.GetPrefs()->Set(GetPrefName(CONTENT_SETTINGS_TYPE_COOKIES), *value); | 1316 profile.GetPrefs()->Set(GetPrefName(CONTENT_SETTINGS_TYPE_COOKIES), *value); |
1315 | 1317 |
1316 // Test that during construction all the prefs get cleared. | 1318 // Test that during construction all the prefs get cleared. |
1317 HostContentSettingsMapFactory::GetForProfile(&profile); | 1319 HostContentSettingsMapFactory::GetForProfile(&profile); |
1318 | 1320 |
1319 const base::DictionaryValue* all_settings_dictionary = | 1321 const base::DictionaryValue* all_settings_dictionary = |
1320 profile.GetPrefs()->GetDictionary( | 1322 profile.GetPrefs()->GetDictionary( |
1321 GetPrefName(CONTENT_SETTINGS_TYPE_COOKIES)); | 1323 GetPrefName(CONTENT_SETTINGS_TYPE_COOKIES)); |
1322 EXPECT_TRUE(all_settings_dictionary->empty()); | 1324 EXPECT_TRUE(all_settings_dictionary->empty()); |
1323 } | 1325 } |
| 1326 #endif // !defined(OS_ANDROID) |
1324 | 1327 |
1325 TEST_F(HostContentSettingsMapTest, MigrateDomainScopedSettings) { | 1328 TEST_F(HostContentSettingsMapTest, MigrateDomainScopedSettings) { |
1326 TestingProfile profile; | 1329 TestingProfile profile; |
1327 HostContentSettingsMap* host_content_settings_map = | 1330 HostContentSettingsMap* host_content_settings_map = |
1328 HostContentSettingsMapFactory::GetForProfile(&profile); | 1331 HostContentSettingsMapFactory::GetForProfile(&profile); |
1329 PrefService* prefs = profile.GetPrefs(); | 1332 PrefService* prefs = profile.GetPrefs(); |
1330 // Set the pref to its initial state so that migration can be done later in | 1333 // Set the pref to its initial state so that migration can be done later in |
1331 // the test (normally it is done on construction of HostContentSettingsMap). | 1334 // the test (normally it is done on construction of HostContentSettingsMap). |
1332 int default_value; | 1335 int default_value; |
1333 prefs->GetDefaultPrefValue(prefs::kDomainToOriginMigrationStatus) | 1336 prefs->GetDefaultPrefValue(prefs::kDomainToOriginMigrationStatus) |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1490 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 1493 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
1491 host_content_settings_map->GetContentSetting( | 1494 host_content_settings_map->GetContentSetting( |
1492 https_host_narrower, https_host_narrower, | 1495 https_host_narrower, https_host_narrower, |
1493 CONTENT_SETTINGS_TYPE_COOKIES, std::string())); | 1496 CONTENT_SETTINGS_TYPE_COOKIES, std::string())); |
1494 } | 1497 } |
1495 | 1498 |
1496 // Ensure that migration only happens once upon construction of the HCSM and | 1499 // Ensure that migration only happens once upon construction of the HCSM and |
1497 // once after syncing (even when these events occur multiple times). | 1500 // once after syncing (even when these events occur multiple times). |
1498 TEST_F(HostContentSettingsMapTest, DomainToOriginMigrationStatus) { | 1501 TEST_F(HostContentSettingsMapTest, DomainToOriginMigrationStatus) { |
1499 TestingProfile profile; | 1502 TestingProfile profile; |
| 1503 |
| 1504 // Construct the map now to make the various platforms equivalent. On Android |
| 1505 // the map is created with the profile (due to dependencies), while on other |
| 1506 // platforms it is created lazily. Note, migration should be run here. |
| 1507 HostContentSettingsMap* host_content_settings_map = |
| 1508 HostContentSettingsMapFactory::GetForProfile(&profile); |
| 1509 |
1500 PrefService* prefs = profile.GetPrefs(); | 1510 PrefService* prefs = profile.GetPrefs(); |
1501 | 1511 |
1502 GURL http_host("http://example.com/"); | 1512 GURL http_host("http://example.com/"); |
1503 GURL http_host_narrower("http://a.example.com/"); | 1513 GURL http_host_narrower("http://a.example.com/"); |
1504 std::string host_pattern_string = | 1514 std::string host_pattern_string = |
1505 ContentSettingsPattern::FromURL(http_host).ToString(); | 1515 ContentSettingsPattern::FromURL(http_host).ToString(); |
1506 | 1516 |
1507 { | 1517 { |
1508 DictionaryPrefUpdate update(prefs, | 1518 DictionaryPrefUpdate update(prefs, |
1509 GetPrefName(CONTENT_SETTINGS_TYPE_POPUPS)); | 1519 GetPrefName(CONTENT_SETTINGS_TYPE_POPUPS)); |
1510 base::DictionaryValue* all_settings_dictionary = update.Get(); | 1520 base::DictionaryValue* all_settings_dictionary = update.Get(); |
1511 ASSERT_TRUE(NULL != all_settings_dictionary); | 1521 ASSERT_TRUE(NULL != all_settings_dictionary); |
1512 | 1522 |
1513 base::DictionaryValue* domain_setting = new base::DictionaryValue; | 1523 base::DictionaryValue* domain_setting = new base::DictionaryValue; |
1514 domain_setting->SetInteger("setting", CONTENT_SETTING_ALLOW); | 1524 domain_setting->SetInteger("setting", CONTENT_SETTING_ALLOW); |
1515 all_settings_dictionary->SetWithoutPathExpansion(host_pattern_string + ",*", | 1525 all_settings_dictionary->SetWithoutPathExpansion(host_pattern_string + ",*", |
1516 domain_setting); | 1526 domain_setting); |
1517 } | 1527 } |
1518 | 1528 |
1519 const base::DictionaryValue* all_settings_dictionary = | 1529 const base::DictionaryValue* all_settings_dictionary = |
1520 prefs->GetDictionary(GetPrefName(CONTENT_SETTINGS_TYPE_POPUPS)); | 1530 prefs->GetDictionary(GetPrefName(CONTENT_SETTINGS_TYPE_POPUPS)); |
1521 const base::DictionaryValue* result = NULL; | 1531 const base::DictionaryValue* result = NULL; |
1522 EXPECT_TRUE(all_settings_dictionary->GetDictionaryWithoutPathExpansion( | 1532 EXPECT_TRUE(all_settings_dictionary->GetDictionaryWithoutPathExpansion( |
1523 "[*.]example.com,*", &result)); | 1533 "[*.]example.com,*", &result)); |
1524 | 1534 |
1525 // Migration is done on construction of HostContentSettingsMap. | 1535 // Migration is done on construction of HostContentSettingsMap. Try to run it |
1526 HostContentSettingsMap* host_content_settings_map = | 1536 // again. This should not do anything as it has already ran. |
1527 HostContentSettingsMapFactory::GetForProfile(&profile); | 1537 host_content_settings_map->MigrateDomainScopedSettings( |
| 1538 false /* after_sync */); |
1528 | 1539 |
1529 // Change default setting to BLOCK. | 1540 // Change default setting to BLOCK. |
1530 host_content_settings_map->SetDefaultContentSetting( | 1541 host_content_settings_map->SetDefaultContentSetting( |
1531 CONTENT_SETTINGS_TYPE_POPUPS, CONTENT_SETTING_BLOCK); | 1542 CONTENT_SETTINGS_TYPE_POPUPS, CONTENT_SETTING_BLOCK); |
| 1543 |
1532 EXPECT_EQ( | 1544 EXPECT_EQ( |
1533 CONTENT_SETTING_ALLOW, | 1545 CONTENT_SETTING_ALLOW, |
1534 host_content_settings_map->GetContentSetting( | 1546 host_content_settings_map->GetContentSetting( |
| 1547 http_host, http_host, CONTENT_SETTINGS_TYPE_POPUPS, std::string())); |
| 1548 |
| 1549 // The setting should still be allow as it hasn't been migrated. |
| 1550 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 1551 host_content_settings_map->GetContentSetting( |
| 1552 http_host_narrower, http_host_narrower, |
| 1553 CONTENT_SETTINGS_TYPE_POPUPS, std::string())); |
| 1554 |
| 1555 // Set the pref to its initial state so that migration can be triggered again, |
| 1556 // just for the sake of testing. |
| 1557 int default_value; |
| 1558 prefs->GetDefaultPrefValue(prefs::kDomainToOriginMigrationStatus) |
| 1559 ->GetAsInteger(&default_value); |
| 1560 prefs->SetInteger(prefs::kDomainToOriginMigrationStatus, default_value); |
| 1561 |
| 1562 // Now, do the migration. This should work as we've cleared the pref back to |
| 1563 // its default value. |
| 1564 host_content_settings_map->MigrateDomainScopedSettings(false); |
| 1565 |
| 1566 // Now the settings should be migrated. |
| 1567 EXPECT_EQ( |
| 1568 CONTENT_SETTING_ALLOW, |
| 1569 host_content_settings_map->GetContentSetting( |
1535 http_host, http_host, CONTENT_SETTINGS_TYPE_POPUPS, std::string())); | 1570 http_host, http_host, CONTENT_SETTINGS_TYPE_POPUPS, std::string())); |
1536 // Settings only apply to origins. Migration got executed. | 1571 // Settings only apply to origins. Migration got executed. |
1537 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 1572 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
1538 host_content_settings_map->GetContentSetting( | 1573 host_content_settings_map->GetContentSetting( |
1539 http_host_narrower, http_host_narrower, | 1574 http_host_narrower, http_host_narrower, |
1540 CONTENT_SETTINGS_TYPE_POPUPS, std::string())); | 1575 CONTENT_SETTINGS_TYPE_POPUPS, std::string())); |
1541 | 1576 |
1542 GURL https_host("https://example.com/"); | 1577 GURL https_host("https://example.com/"); |
1543 GURL https_host_narrower("https://a.example.com/"); | 1578 GURL https_host_narrower("https://a.example.com/"); |
1544 | 1579 |
1545 host_content_settings_map->SetContentSettingCustomScope( | 1580 host_content_settings_map->SetContentSettingCustomScope( |
1546 ContentSettingsPattern::FromURL(https_host), | 1581 ContentSettingsPattern::FromURL(https_host), |
1547 ContentSettingsPattern::Wildcard(), CONTENT_SETTINGS_TYPE_POPUPS, | 1582 ContentSettingsPattern::Wildcard(), CONTENT_SETTINGS_TYPE_POPUPS, |
1548 std::string(), CONTENT_SETTING_ALLOW); | 1583 std::string(), CONTENT_SETTING_ALLOW); |
| 1584 |
1549 EXPECT_EQ( | 1585 EXPECT_EQ( |
1550 CONTENT_SETTING_ALLOW, | 1586 CONTENT_SETTING_ALLOW, |
1551 host_content_settings_map->GetContentSetting( | 1587 host_content_settings_map->GetContentSetting( |
1552 https_host, https_host, CONTENT_SETTINGS_TYPE_POPUPS, std::string())); | 1588 https_host, https_host, CONTENT_SETTINGS_TYPE_POPUPS, std::string())); |
1553 // Settings apply to subdomains. | 1589 // Settings apply to subdomains. |
1554 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 1590 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
1555 host_content_settings_map->GetContentSetting( | 1591 host_content_settings_map->GetContentSetting( |
1556 https_host_narrower, https_host_narrower, | 1592 https_host_narrower, https_host_narrower, |
1557 CONTENT_SETTINGS_TYPE_POPUPS, std::string())); | 1593 CONTENT_SETTINGS_TYPE_POPUPS, std::string())); |
1558 | 1594 |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1714 EXPECT_TRUE(map->CanSetNarrowestContentSetting( | 1750 EXPECT_TRUE(map->CanSetNarrowestContentSetting( |
1715 valid_url, valid_url, | 1751 valid_url, valid_url, |
1716 CONTENT_SETTINGS_TYPE_POPUPS)); | 1752 CONTENT_SETTINGS_TYPE_POPUPS)); |
1717 | 1753 |
1718 GURL invalid_url("about:blank"); | 1754 GURL invalid_url("about:blank"); |
1719 EXPECT_FALSE(map->CanSetNarrowestContentSetting( | 1755 EXPECT_FALSE(map->CanSetNarrowestContentSetting( |
1720 invalid_url, invalid_url, | 1756 invalid_url, invalid_url, |
1721 CONTENT_SETTINGS_TYPE_POPUPS)); | 1757 CONTENT_SETTINGS_TYPE_POPUPS)); |
1722 } | 1758 } |
1723 | 1759 |
OLD | NEW |