OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/extensions/extension_prefs.h" | 5 #include "chrome/browser/extensions/extension_prefs.h" |
6 | 6 |
7 #include "base/string_number_conversions.h" | 7 #include "base/string_number_conversions.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "chrome/browser/extensions/extension_pref_store.h" | 10 #include "chrome/browser/extensions/extension_pref_store.h" |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 const char kPrefGrantedPermissionsAPI[] = "granted_permissions.api"; | 106 const char kPrefGrantedPermissionsAPI[] = "granted_permissions.api"; |
107 const char kPrefGrantedPermissionsHost[] = "granted_permissions.host"; | 107 const char kPrefGrantedPermissionsHost[] = "granted_permissions.host"; |
108 const char kPrefGrantedPermissionsAll[] = "granted_permissions.full"; | 108 const char kPrefGrantedPermissionsAll[] = "granted_permissions.full"; |
109 | 109 |
110 // A preference that indicates when an extension was installed. | 110 // A preference that indicates when an extension was installed. |
111 const char kPrefInstallTime[] = "install_time"; | 111 const char kPrefInstallTime[] = "install_time"; |
112 | 112 |
113 // A preference that contains any extension-controlled preferences. | 113 // A preference that contains any extension-controlled preferences. |
114 const char kPrefPreferences[] = "preferences"; | 114 const char kPrefPreferences[] = "preferences"; |
115 | 115 |
| 116 // A preference that contains any extension-controlled incognito preferences. |
| 117 const char kPrefIncognitoPreferences[] = "incognito_preferences"; |
| 118 |
116 // A preference that contains extension-set content settings. | 119 // A preference that contains extension-set content settings. |
117 const char kPrefContentSettings[] = "content_settings"; | 120 const char kPrefContentSettings[] = "content_settings"; |
118 | 121 |
119 // Provider of write access to a dictionary storing extension prefs. | 122 // Provider of write access to a dictionary storing extension prefs. |
120 class ScopedExtensionPrefUpdate : public DictionaryPrefUpdate { | 123 class ScopedExtensionPrefUpdate : public DictionaryPrefUpdate { |
121 public: | 124 public: |
122 ScopedExtensionPrefUpdate(PrefService* service, | 125 ScopedExtensionPrefUpdate(PrefService* service, |
123 const std::string& extension_id) : | 126 const std::string& extension_id) : |
124 DictionaryPrefUpdate(service, ExtensionPrefs::kExtensionsPref), | 127 DictionaryPrefUpdate(service, ExtensionPrefs::kExtensionsPref), |
125 prefs_(service), | 128 prefs_(service), |
(...skipping 17 matching lines...) Expand all Loading... |
143 private: | 146 private: |
144 PrefService* prefs_; | 147 PrefService* prefs_; |
145 const std::string extension_id_; | 148 const std::string extension_id_; |
146 | 149 |
147 DISALLOW_COPY_AND_ASSIGN(ScopedExtensionPrefUpdate); | 150 DISALLOW_COPY_AND_ASSIGN(ScopedExtensionPrefUpdate); |
148 }; | 151 }; |
149 | 152 |
150 // Provider of write access to a dictionary storing extension controlled prefs. | 153 // Provider of write access to a dictionary storing extension controlled prefs. |
151 class ScopedExtensionControlledPrefUpdate : public DictionaryPrefUpdate { | 154 class ScopedExtensionControlledPrefUpdate : public DictionaryPrefUpdate { |
152 public: | 155 public: |
153 ScopedExtensionControlledPrefUpdate(PrefService* service, | 156 // |incognito_or_regular_path| indicates the sub-path where the |
154 const std::string& extension_id) : | 157 // extension controlled preferences are stored. This has to be either |
| 158 // kPrefPreferences or kPrefIncognitoPreferences. |
| 159 ScopedExtensionControlledPrefUpdate( |
| 160 PrefService* service, |
| 161 const std::string& extension_id, |
| 162 const std::string& incognito_or_regular_path) : |
155 DictionaryPrefUpdate(service, ExtensionPrefs::kExtensionsPref), | 163 DictionaryPrefUpdate(service, ExtensionPrefs::kExtensionsPref), |
156 prefs_(service), | 164 prefs_(service), |
157 extension_id_(extension_id) {} | 165 extension_id_(extension_id), |
| 166 incognito_or_regular_path_(incognito_or_regular_path) {} |
158 | 167 |
159 virtual ~ScopedExtensionControlledPrefUpdate() { | 168 virtual ~ScopedExtensionControlledPrefUpdate() { |
160 prefs_->ScheduleSavePersistentPrefs(); | 169 prefs_->ScheduleSavePersistentPrefs(); |
161 } | 170 } |
162 | 171 |
163 virtual DictionaryValue* Get() { | 172 virtual DictionaryValue* Get() { |
164 DictionaryValue* dict = DictionaryPrefUpdate::Get(); | 173 DictionaryValue* dict = DictionaryPrefUpdate::Get(); |
165 DictionaryValue* preferences = NULL; | 174 DictionaryValue* preferences = NULL; |
166 std::string key = extension_id_ + std::string(".") + kPrefPreferences; | 175 std::string key = |
| 176 extension_id_ + std::string(".") + incognito_or_regular_path_; |
167 if (!dict->GetDictionary(key, &preferences)) { | 177 if (!dict->GetDictionary(key, &preferences)) { |
168 preferences = new DictionaryValue; | 178 preferences = new DictionaryValue; |
169 dict->Set(key, preferences); | 179 dict->Set(key, preferences); |
170 } | 180 } |
171 return preferences; | 181 return preferences; |
172 } | 182 } |
173 | 183 |
174 private: | 184 private: |
175 PrefService* prefs_; | 185 PrefService* prefs_; |
176 const std::string extension_id_; | 186 const std::string extension_id_; |
| 187 const std::string incognito_or_regular_path_; |
177 | 188 |
178 DISALLOW_COPY_AND_ASSIGN(ScopedExtensionControlledPrefUpdate); | 189 DISALLOW_COPY_AND_ASSIGN(ScopedExtensionControlledPrefUpdate); |
179 }; | 190 }; |
180 | 191 |
181 // TODO(mihaip): This is cleanup code for keys for unpacked extensions (which | 192 // TODO(mihaip): This is cleanup code for keys for unpacked extensions (which |
182 // are derived from paths). As part of the wstring removal, we changed the way | 193 // are derived from paths). As part of the wstring removal, we changed the way |
183 // we hash paths, so we need to move prefs from their old synthesized IDs to | 194 // we hash paths, so we need to move prefs from their old synthesized IDs to |
184 // their new ones. We can remove this by July 2011. (See http://crbug.com/75945 | 195 // their new ones. We can remove this by July 2011. (See http://crbug.com/75945 |
185 // for more details). | 196 // for more details). |
186 static void CleanupBadExtensionKeys(const FilePath& root_dir, | 197 static void CleanupBadExtensionKeys(const FilePath& root_dir, |
(...skipping 688 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
875 ScopedExtensionPrefUpdate update(prefs_, id); | 886 ScopedExtensionPrefUpdate update(prefs_, id); |
876 DictionaryValue* extension_dict = update.Get(); | 887 DictionaryValue* extension_dict = update.Get(); |
877 const base::Time install_time = GetCurrentTime(); | 888 const base::Time install_time = GetCurrentTime(); |
878 extension_dict->Set(kPrefState, Value::CreateIntegerValue(initial_state)); | 889 extension_dict->Set(kPrefState, Value::CreateIntegerValue(initial_state)); |
879 extension_dict->Set(kPrefLocation, | 890 extension_dict->Set(kPrefLocation, |
880 Value::CreateIntegerValue(extension->location())); | 891 Value::CreateIntegerValue(extension->location())); |
881 extension_dict->Set(kPrefInstallTime, | 892 extension_dict->Set(kPrefInstallTime, |
882 Value::CreateStringValue( | 893 Value::CreateStringValue( |
883 base::Int64ToString(install_time.ToInternalValue()))); | 894 base::Int64ToString(install_time.ToInternalValue()))); |
884 extension_dict->Set(kPrefPreferences, new DictionaryValue()); | 895 extension_dict->Set(kPrefPreferences, new DictionaryValue()); |
| 896 extension_dict->Set(kPrefIncognitoPreferences, new DictionaryValue()); |
885 extension_dict->Set(kPrefContentSettings, new ListValue()); | 897 extension_dict->Set(kPrefContentSettings, new ListValue()); |
886 | 898 |
887 FilePath::StringType path = MakePathRelative(install_directory_, | 899 FilePath::StringType path = MakePathRelative(install_directory_, |
888 extension->path()); | 900 extension->path()); |
889 extension_dict->Set(kPrefPath, Value::CreateStringValue(path)); | 901 extension_dict->Set(kPrefPath, Value::CreateStringValue(path)); |
890 // We store prefs about LOAD extensions, but don't cache their manifest | 902 // We store prefs about LOAD extensions, but don't cache their manifest |
891 // since it may change on disk. | 903 // since it may change on disk. |
892 if (extension->location() != Extension::LOAD) { | 904 if (extension->location() != Extension::LOAD) { |
893 extension_dict->Set(kPrefManifest, | 905 extension_dict->Set(kPrefManifest, |
894 extension->manifest_value()->DeepCopy()); | 906 extension->manifest_value()->DeepCopy()); |
(...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1367 << kPrefInstallTime << " was introduced. Updating " | 1379 << kPrefInstallTime << " was introduced. Updating " |
1368 << kPrefInstallTime << " to the current time."; | 1380 << kPrefInstallTime << " to the current time."; |
1369 const base::Time install_time = GetCurrentTime(); | 1381 const base::Time install_time = GetCurrentTime(); |
1370 UpdateExtensionPref(*ext_id, kPrefInstallTime, Value::CreateStringValue( | 1382 UpdateExtensionPref(*ext_id, kPrefInstallTime, Value::CreateStringValue( |
1371 base::Int64ToString(install_time.ToInternalValue()))); | 1383 base::Int64ToString(install_time.ToInternalValue()))); |
1372 } | 1384 } |
1373 } | 1385 } |
1374 } | 1386 } |
1375 | 1387 |
1376 const DictionaryValue* ExtensionPrefs::GetExtensionControlledPrefs( | 1388 const DictionaryValue* ExtensionPrefs::GetExtensionControlledPrefs( |
1377 const std::string& extension_id) const { | 1389 const std::string& extension_id, |
1378 std::string key = extension_id + std::string(".") + kPrefPreferences; | 1390 bool incognito) const { |
| 1391 std::string key = extension_id + std::string(".") + |
| 1392 (incognito ? kPrefIncognitoPreferences : kPrefPreferences); |
1379 DictionaryValue* preferences = NULL; | 1393 DictionaryValue* preferences = NULL; |
1380 // First try the regular lookup. | 1394 // First try the regular lookup. |
1381 const DictionaryValue* source_dict = prefs_->GetDictionary(kExtensionsPref); | 1395 const DictionaryValue* source_dict = prefs_->GetDictionary(kExtensionsPref); |
1382 if (!source_dict->GetDictionary(key, &preferences)) { | 1396 if (!source_dict->GetDictionary(key, &preferences)) { |
1383 // And then create a dictionary if it did not exist before. | 1397 // And then create a dictionary if it did not exist before. |
1384 DictionaryPrefUpdate update(prefs_, kExtensionsPref); | 1398 DictionaryPrefUpdate update(prefs_, kExtensionsPref); |
1385 preferences = new DictionaryValue; | 1399 preferences = new DictionaryValue; |
1386 update->Set(key, preferences); | 1400 update->Set(key, preferences); |
1387 } | 1401 } |
1388 return preferences; | 1402 return preferences; |
(...skipping 21 matching lines...) Expand all Loading... |
1410 ext_id != extension_ids.end(); ++ext_id) { | 1424 ext_id != extension_ids.end(); ++ext_id) { |
1411 extension_pref_value_map_->RegisterExtension( | 1425 extension_pref_value_map_->RegisterExtension( |
1412 *ext_id, | 1426 *ext_id, |
1413 GetInstallTime(*ext_id), | 1427 GetInstallTime(*ext_id), |
1414 GetExtensionState(*ext_id) == Extension::ENABLED); | 1428 GetExtensionState(*ext_id) == Extension::ENABLED); |
1415 content_settings_store_->RegisterExtension( | 1429 content_settings_store_->RegisterExtension( |
1416 *ext_id, | 1430 *ext_id, |
1417 GetInstallTime(*ext_id), | 1431 GetInstallTime(*ext_id), |
1418 GetExtensionState(*ext_id) == Extension::ENABLED); | 1432 GetExtensionState(*ext_id) == Extension::ENABLED); |
1419 | 1433 |
1420 const DictionaryValue* prefs = GetExtensionControlledPrefs(*ext_id); | 1434 // Set regular extension controlled prefs. |
| 1435 const DictionaryValue* prefs = GetExtensionControlledPrefs(*ext_id, false); |
1421 for (DictionaryValue::key_iterator i = prefs->begin_keys(); | 1436 for (DictionaryValue::key_iterator i = prefs->begin_keys(); |
1422 i != prefs->end_keys(); ++i) { | 1437 i != prefs->end_keys(); ++i) { |
1423 Value* value; | 1438 Value* value; |
1424 if (!prefs->GetWithoutPathExpansion(*i, &value)) | 1439 if (!prefs->GetWithoutPathExpansion(*i, &value)) |
1425 continue; | 1440 continue; |
1426 extension_pref_value_map_->SetExtensionPref( | 1441 extension_pref_value_map_->SetExtensionPref( |
1427 *ext_id, *i, false, value->DeepCopy()); | 1442 *ext_id, *i, extension_prefs_scope::kRegular, value->DeepCopy()); |
| 1443 } |
| 1444 |
| 1445 // Set incognito extension controlled prefs. |
| 1446 prefs = GetExtensionControlledPrefs(*ext_id, true); |
| 1447 for (DictionaryValue::key_iterator i = prefs->begin_keys(); |
| 1448 i != prefs->end_keys(); ++i) { |
| 1449 Value* value; |
| 1450 if (!prefs->GetWithoutPathExpansion(*i, &value)) |
| 1451 continue; |
| 1452 extension_pref_value_map_->SetExtensionPref( |
| 1453 *ext_id, *i, extension_prefs_scope::kIncognitoPersistent, |
| 1454 value->DeepCopy()); |
1428 } | 1455 } |
1429 | 1456 |
1430 const DictionaryValue* extension_prefs = GetExtensionPref(*ext_id); | 1457 const DictionaryValue* extension_prefs = GetExtensionPref(*ext_id); |
1431 DCHECK(extension_prefs); | 1458 DCHECK(extension_prefs); |
1432 ListValue* content_settings = NULL; | 1459 ListValue* content_settings = NULL; |
1433 if (extension_prefs->GetList(kPrefContentSettings, &content_settings)) { | 1460 if (extension_prefs->GetList(kPrefContentSettings, &content_settings)) { |
1434 content_settings_store_->SetExtensionContentSettingsFromList( | 1461 content_settings_store_->SetExtensionContentSettingsFromList( |
1435 *ext_id, content_settings); | 1462 *ext_id, content_settings); |
1436 } | 1463 } |
1437 } | 1464 } |
1438 | 1465 |
1439 extension_pref_value_map_->NotifyInitializationCompleted(); | 1466 extension_pref_value_map_->NotifyInitializationCompleted(); |
1440 } | 1467 } |
1441 | 1468 |
1442 | 1469 |
1443 void ExtensionPrefs::SetExtensionControlledPref(const std::string& extension_id, | 1470 void ExtensionPrefs::SetExtensionControlledPref( |
1444 const std::string& pref_key, | 1471 const std::string& extension_id, |
1445 bool incognito, | 1472 const std::string& pref_key, |
1446 Value* value) { | 1473 extension_prefs_scope::Scope scope, |
| 1474 Value* value) { |
1447 #ifndef NDEBUG | 1475 #ifndef NDEBUG |
1448 const PrefService::Preference* pref = | 1476 const PrefService::Preference* pref = |
1449 pref_service()->FindPreference(pref_key.c_str()); | 1477 pref_service()->FindPreference(pref_key.c_str()); |
1450 DCHECK(pref) << "Extension controlled preference key " << pref_key | 1478 DCHECK(pref) << "Extension controlled preference key " << pref_key |
1451 << " not registered."; | 1479 << " not registered."; |
1452 DCHECK_EQ(pref->GetType(), value->GetType()) | 1480 DCHECK_EQ(pref->GetType(), value->GetType()) |
1453 << "Extension controlled preference " << pref_key << " has wrong type."; | 1481 << "Extension controlled preference " << pref_key << " has wrong type."; |
1454 #endif | 1482 #endif |
1455 | 1483 |
1456 if (!incognito) { | 1484 if (scope == extension_prefs_scope::kRegular) { |
1457 // Also store in persisted Preferences file to recover after a | 1485 // Also store in persisted Preferences file to recover after a |
1458 // browser restart. | 1486 // browser restart. |
1459 ScopedExtensionControlledPrefUpdate update(prefs_, extension_id); | 1487 ScopedExtensionControlledPrefUpdate update(prefs_, extension_id, |
| 1488 kPrefPreferences); |
| 1489 update->SetWithoutPathExpansion(pref_key, value->DeepCopy()); |
| 1490 } else if (scope == extension_prefs_scope::kIncognitoPersistent) { |
| 1491 ScopedExtensionControlledPrefUpdate update(prefs_, extension_id, |
| 1492 kPrefIncognitoPreferences); |
1460 update->SetWithoutPathExpansion(pref_key, value->DeepCopy()); | 1493 update->SetWithoutPathExpansion(pref_key, value->DeepCopy()); |
1461 } | 1494 } |
1462 | 1495 |
1463 extension_pref_value_map_->SetExtensionPref( | 1496 extension_pref_value_map_->SetExtensionPref( |
1464 extension_id, pref_key, incognito, value); | 1497 extension_id, pref_key, scope, value); |
1465 } | 1498 } |
1466 | 1499 |
1467 void ExtensionPrefs::RemoveExtensionControlledPref( | 1500 void ExtensionPrefs::RemoveExtensionControlledPref( |
1468 const std::string& extension_id, | 1501 const std::string& extension_id, |
1469 const std::string& pref_key, | 1502 const std::string& pref_key, |
1470 bool incognito) { | 1503 extension_prefs_scope::Scope scope) { |
1471 DCHECK(pref_service()->FindPreference(pref_key.c_str())) | 1504 DCHECK(pref_service()->FindPreference(pref_key.c_str())) |
1472 << "Extension controlled preference key " << pref_key | 1505 << "Extension controlled preference key " << pref_key |
1473 << " not registered."; | 1506 << " not registered."; |
1474 | 1507 |
1475 if (!incognito) { | 1508 if (scope == extension_prefs_scope::kRegular) { |
1476 // Also store in persisted Preferences file to recover after a | 1509 // Also store in persisted Preferences file to recover after a |
1477 // browser restart. | 1510 // browser restart. |
1478 ScopedExtensionControlledPrefUpdate update(prefs_, extension_id); | 1511 ScopedExtensionControlledPrefUpdate update(prefs_, extension_id, |
| 1512 kPrefPreferences); |
| 1513 update->RemoveWithoutPathExpansion(pref_key, NULL); |
| 1514 } else if (scope == extension_prefs_scope::kIncognitoPersistent) { |
| 1515 ScopedExtensionControlledPrefUpdate update(prefs_, extension_id, |
| 1516 kPrefIncognitoPreferences); |
1479 update->RemoveWithoutPathExpansion(pref_key, NULL); | 1517 update->RemoveWithoutPathExpansion(pref_key, NULL); |
1480 } | 1518 } |
1481 | 1519 |
1482 extension_pref_value_map_->RemoveExtensionPref( | 1520 extension_pref_value_map_->RemoveExtensionPref( |
1483 extension_id, pref_key, incognito); | 1521 extension_id, pref_key, scope); |
1484 } | 1522 } |
1485 | 1523 |
1486 bool ExtensionPrefs::CanExtensionControlPref(const std::string& extension_id, | 1524 bool ExtensionPrefs::CanExtensionControlPref(const std::string& extension_id, |
1487 const std::string& pref_key, | 1525 const std::string& pref_key, |
1488 bool incognito) { | 1526 bool incognito) { |
1489 DCHECK(pref_service()->FindPreference(pref_key.c_str())) | 1527 DCHECK(pref_service()->FindPreference(pref_key.c_str())) |
1490 << "Extension controlled preference key " << pref_key | 1528 << "Extension controlled preference key " << pref_key |
1491 << " not registered."; | 1529 << " not registered."; |
1492 | 1530 |
1493 return extension_pref_value_map_->CanExtensionControlPref(extension_id, | 1531 return extension_pref_value_map_->CanExtensionControlPref(extension_id, |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1527 prefs->RegisterListPref(prefs::kExtensionInstallAllowList, | 1565 prefs->RegisterListPref(prefs::kExtensionInstallAllowList, |
1528 PrefService::UNSYNCABLE_PREF); | 1566 PrefService::UNSYNCABLE_PREF); |
1529 prefs->RegisterListPref(prefs::kExtensionInstallDenyList, | 1567 prefs->RegisterListPref(prefs::kExtensionInstallDenyList, |
1530 PrefService::UNSYNCABLE_PREF); | 1568 PrefService::UNSYNCABLE_PREF); |
1531 prefs->RegisterListPref(prefs::kExtensionInstallForceList, | 1569 prefs->RegisterListPref(prefs::kExtensionInstallForceList, |
1532 PrefService::UNSYNCABLE_PREF); | 1570 PrefService::UNSYNCABLE_PREF); |
1533 prefs->RegisterStringPref(kWebStoreLogin, | 1571 prefs->RegisterStringPref(kWebStoreLogin, |
1534 std::string() /* default_value */, | 1572 std::string() /* default_value */, |
1535 PrefService::UNSYNCABLE_PREF); | 1573 PrefService::UNSYNCABLE_PREF); |
1536 } | 1574 } |
OLD | NEW |