Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_util.h" | 7 #include "base/string_util.h" |
| 8 #include "base/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/common/extensions/extension.h" | 10 #include "chrome/common/extensions/extension.h" |
| 11 #include "chrome/common/extensions/url_pattern.h" | |
| 11 #include "chrome/common/notification_service.h" | 12 #include "chrome/common/notification_service.h" |
| 12 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
| 13 | 14 |
| 14 using base::Time; | 15 using base::Time; |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 // Additional preferences keys | 19 // Additional preferences keys |
| 19 | 20 |
| 20 // Where an extension was installed from. (see Extension::Location) | 21 // Where an extension was installed from. (see Extension::Location) |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 75 | 76 |
| 76 // A preference determining the order of which the apps appear on the NTP. | 77 // A preference determining the order of which the apps appear on the NTP. |
| 77 const char kPrefAppLaunchIndex[] = "app_launcher_index"; | 78 const char kPrefAppLaunchIndex[] = "app_launcher_index"; |
| 78 | 79 |
| 79 // A preference for storing extra data sent in update checks for an extension. | 80 // A preference for storing extra data sent in update checks for an extension. |
| 80 const char kUpdateUrlData[] = "update_url_data"; | 81 const char kUpdateUrlData[] = "update_url_data"; |
| 81 | 82 |
| 82 // Whether the browser action is visible in the toolbar. | 83 // Whether the browser action is visible in the toolbar. |
| 83 const char kBrowserActionVisible[] = "browser_action_visible"; | 84 const char kBrowserActionVisible[] = "browser_action_visible"; |
| 84 | 85 |
| 86 // Preferences that hold the permissions the user has granted the extension. | |
| 87 // We explicitly keep track of these so that extensions can contain unknown | |
| 88 // permissions, for backwards compatibility reasons, and we can still prompt | |
| 89 // the user to accept them once recognized. | |
| 90 const char kPrefGrantedPermissionsAPI[] = "granted_permissions.api"; | |
| 91 const char kPrefGrantedPermissionsHost[] = "granted_permissions.host"; | |
| 92 const char kPrefGrantedPermissionsInitialized[] = | |
| 93 "granted_permissions.initialized"; | |
| 94 | |
| 85 } // namespace | 95 } // namespace |
| 86 | 96 |
| 87 //////////////////////////////////////////////////////////////////////////////// | 97 //////////////////////////////////////////////////////////////////////////////// |
| 88 | 98 |
| 89 namespace { | 99 namespace { |
| 90 | 100 |
| 91 // TODO(asargent) - This is cleanup code for a key that was introduced into | 101 // TODO(asargent) - This is cleanup code for a key that was introduced into |
| 92 // the extensions.settings sub-dictionary which wasn't a valid extension | 102 // the extensions.settings sub-dictionary which wasn't a valid extension |
| 93 // id. We can remove this in a couple of months. (See http://crbug.com/40017 | 103 // id. We can remove this in a couple of months. (See http://crbug.com/40017 |
| 94 // and http://crbug.com/39745 for more details). | 104 // and http://crbug.com/39745 for more details). |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 106 bool dirty = false; | 116 bool dirty = false; |
| 107 for (std::set<std::string>::iterator i = bad_keys.begin(); | 117 for (std::set<std::string>::iterator i = bad_keys.begin(); |
| 108 i != bad_keys.end(); ++i) { | 118 i != bad_keys.end(); ++i) { |
| 109 dirty = true; | 119 dirty = true; |
| 110 dictionary->Remove(*i, NULL); | 120 dictionary->Remove(*i, NULL); |
| 111 } | 121 } |
| 112 if (dirty) | 122 if (dirty) |
| 113 prefs->ScheduleSavePersistentPrefs(); | 123 prefs->ScheduleSavePersistentPrefs(); |
| 114 } | 124 } |
| 115 | 125 |
| 126 static void ExtentToStringSet(const ExtensionExtent& host_extent, | |
| 127 std::set<std::string>* result) { | |
| 128 ExtensionExtent::PatternList patterns = host_extent.patterns(); | |
| 129 ExtensionExtent::PatternList::const_iterator i; | |
| 130 | |
| 131 for (i = patterns.begin(); i != patterns.end(); ++i) | |
| 132 result->insert(i->GetAsString()); | |
| 133 } | |
| 134 | |
| 135 static void AddPattern(ExtensionExtent* extent, const std::string& pattern) { | |
| 136 int schemes = URLPattern::SCHEME_ALL; | |
| 137 extent->AddPattern(URLPattern(schemes, pattern)); | |
| 138 } | |
| 139 | |
| 116 } // namespace | 140 } // namespace |
| 117 | 141 |
| 118 ExtensionPrefs::ExtensionPrefs(PrefService* prefs, const FilePath& root_dir) | 142 ExtensionPrefs::ExtensionPrefs(PrefService* prefs, const FilePath& root_dir) |
| 119 : prefs_(prefs), | 143 : prefs_(prefs), |
| 120 install_directory_(root_dir) { | 144 install_directory_(root_dir) { |
| 121 // TODO(asargent) - Remove this in a couple of months. (See comment above | 145 // TODO(asargent) - Remove this in a couple of months. (See comment above |
| 122 // CleanupBadExtensionKeys). | 146 // CleanupBadExtensionKeys). |
| 123 CleanupBadExtensionKeys(prefs); | 147 CleanupBadExtensionKeys(prefs); |
| 124 | 148 |
| 125 MakePathsRelative(); | 149 MakePathsRelative(); |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 211 DictionaryValue* copy = | 235 DictionaryValue* copy = |
| 212 static_cast<DictionaryValue*>(extensions->DeepCopy()); | 236 static_cast<DictionaryValue*>(extensions->DeepCopy()); |
| 213 MakePathsAbsolute(copy); | 237 MakePathsAbsolute(copy); |
| 214 return copy; | 238 return copy; |
| 215 } | 239 } |
| 216 return new DictionaryValue; | 240 return new DictionaryValue; |
| 217 } | 241 } |
| 218 | 242 |
| 219 bool ExtensionPrefs::ReadBooleanFromPref( | 243 bool ExtensionPrefs::ReadBooleanFromPref( |
| 220 DictionaryValue* ext, const std::string& pref_key) { | 244 DictionaryValue* ext, const std::string& pref_key) { |
| 221 if (!ext->HasKey(pref_key)) return false; | |
| 222 bool bool_value = false; | 245 bool bool_value = false; |
| 223 if (!ext->GetBoolean(pref_key, &bool_value)) { | 246 if (!ext->GetBoolean(pref_key, &bool_value)) |
| 224 NOTREACHED() << "Failed to fetch " << pref_key << " flag."; | |
| 225 // In case we could not fetch the flag, we treat it as false. | |
| 226 return false; | 247 return false; |
| 227 } | 248 |
| 228 return bool_value; | 249 return bool_value; |
| 229 } | 250 } |
| 230 | 251 |
| 231 bool ExtensionPrefs::ReadExtensionPrefBoolean( | 252 bool ExtensionPrefs::ReadExtensionPrefBoolean( |
| 232 const std::string& extension_id, const std::string& pref_key) { | 253 const std::string& extension_id, const std::string& pref_key) { |
| 233 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); | 254 DictionaryValue* ext = GetExtensionPref(extension_id); |
| 234 if (!extensions) | 255 if (!ext) { |
| 235 return false; | |
| 236 | |
| 237 DictionaryValue* ext = NULL; | |
| 238 if (!extensions->GetDictionary(extension_id, &ext)) { | |
| 239 // No such extension yet. | 256 // No such extension yet. |
| 240 return false; | 257 return false; |
| 241 } | 258 } |
| 242 return ReadBooleanFromPref(ext, pref_key); | 259 return ReadBooleanFromPref(ext, pref_key); |
| 243 } | 260 } |
| 244 | 261 |
| 245 bool ExtensionPrefs::ReadIntegerFromPref( | 262 bool ExtensionPrefs::ReadIntegerFromPref( |
| 246 DictionaryValue* ext, const std::string& pref_key, int* out_value) { | 263 DictionaryValue* ext, const std::string& pref_key, int* out_value) { |
| 247 if (!ext->HasKey(pref_key)) return false; | 264 if (!ext->GetInteger(pref_key, out_value)) |
| 248 if (!ext->GetInteger(pref_key, out_value)) { | |
| 249 NOTREACHED() << "Failed to fetch " << pref_key << " flag."; | |
| 250 // In case we could not fetch the flag, we treat it as false. | |
| 251 return false; | 265 return false; |
| 252 } | 266 |
| 253 return out_value != NULL; | 267 return out_value != NULL; |
| 254 } | 268 } |
| 255 | 269 |
| 256 bool ExtensionPrefs::ReadExtensionPrefInteger( | 270 bool ExtensionPrefs::ReadExtensionPrefInteger( |
| 257 const std::string& extension_id, const std::string& pref_key, | 271 const std::string& extension_id, const std::string& pref_key, |
| 258 int* out_value) { | 272 int* out_value) { |
| 259 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); | 273 DictionaryValue* ext = GetExtensionPref(extension_id); |
| 260 if (!extensions) | 274 if (!ext) { |
| 261 return false; | |
| 262 DictionaryValue* ext = NULL; | |
| 263 if (!extensions->GetDictionary(extension_id, &ext)) { | |
| 264 // No such extension yet. | 275 // No such extension yet. |
| 265 return false; | 276 return false; |
| 266 } | 277 } |
| 267 return ReadIntegerFromPref(ext, pref_key, out_value); | 278 return ReadIntegerFromPref(ext, pref_key, out_value); |
| 268 } | 279 } |
| 269 | 280 |
| 281 bool ExtensionPrefs::ReadExtensionPrefList( | |
| 282 const std::string& extension_id, const std::string& pref_key, | |
| 283 ListValue** out_value) { | |
| 284 DictionaryValue* ext = GetExtensionPref(extension_id); | |
| 285 if (!ext || !ext->GetList(pref_key, out_value)) | |
| 286 return false; | |
| 287 | |
| 288 return out_value != NULL; | |
| 289 } | |
| 290 | |
| 291 bool ExtensionPrefs::ReadExtensionPrefStringSet( | |
| 292 const std::string& extension_id, | |
| 293 const std::string& pref_key, | |
| 294 std::set<std::string>* result) { | |
| 295 DCHECK(Extension::IdIsValid(extension_id)); | |
| 296 DCHECK(result); | |
| 297 | |
| 298 ListValue* value = NULL; | |
| 299 if (!ReadExtensionPrefList(extension_id, pref_key, &value)) | |
| 300 return false; | |
| 301 | |
| 302 result->clear(); | |
| 303 | |
| 304 for (size_t i = 0; i < value->GetSize(); ++i) { | |
| 305 std::string item; | |
| 306 if (!value->GetString(i, &item)) | |
| 307 return false; | |
| 308 result->insert(item); | |
| 309 } | |
| 310 | |
| 311 return true; | |
| 312 } | |
| 313 | |
| 314 void ExtensionPrefs::AddToExtensionPrefStringSet( | |
| 315 const std::string& extension_id, | |
| 316 const std::string& pref_key, | |
| 317 const std::set<std::string>& added_value) { | |
| 318 DCHECK(Extension::IdIsValid(extension_id)); | |
| 319 | |
| 320 std::set<std::string> old_value; | |
| 321 std::set<std::string> new_value; | |
| 322 ReadExtensionPrefStringSet(extension_id, pref_key, &old_value); | |
| 323 | |
| 324 std::set_union(old_value.begin(), old_value.end(), | |
| 325 added_value.begin(), added_value.end(), | |
| 326 std::inserter(new_value, new_value.end())); | |
| 327 | |
| 328 ListValue* value = new ListValue(); | |
| 329 for (std::set<std::string>::const_iterator iter = new_value.begin(); | |
| 330 iter != new_value.end(); ++iter) | |
| 331 value->Append(Value::CreateStringValue(*iter)); | |
| 332 | |
| 333 UpdateExtensionPref(extension_id, pref_key, value); | |
| 334 prefs_->ScheduleSavePersistentPrefs(); | |
| 335 } | |
| 336 | |
| 270 void ExtensionPrefs::SavePrefsAndNotify() { | 337 void ExtensionPrefs::SavePrefsAndNotify() { |
| 271 prefs_->ScheduleSavePersistentPrefs(); | 338 prefs_->ScheduleSavePersistentPrefs(); |
| 272 prefs_->pref_notifier()->OnUserPreferenceSet(kExtensionsPref); | 339 prefs_->pref_notifier()->OnUserPreferenceSet(kExtensionsPref); |
| 273 } | 340 } |
| 274 | 341 |
| 275 bool ExtensionPrefs::IsBlacklistBitSet(DictionaryValue* ext) { | 342 bool ExtensionPrefs::IsBlacklistBitSet(DictionaryValue* ext) { |
| 276 return ReadBooleanFromPref(ext, kPrefBlacklist); | 343 return ReadBooleanFromPref(ext, kPrefBlacklist); |
| 277 } | 344 } |
| 278 | 345 |
| 279 bool ExtensionPrefs::IsExtensionBlacklisted(const std::string& extension_id) { | 346 bool ExtensionPrefs::IsExtensionBlacklisted(const std::string& extension_id) { |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 404 DictionaryValue* dictionary) { | 471 DictionaryValue* dictionary) { |
| 405 if (!dictionary) { | 472 if (!dictionary) { |
| 406 NOTREACHED(); | 473 NOTREACHED(); |
| 407 return; | 474 return; |
| 408 } | 475 } |
| 409 std::string value = base::Int64ToString(time.ToInternalValue()); | 476 std::string value = base::Int64ToString(time.ToInternalValue()); |
| 410 dictionary->SetString(kLastPingDay, value); | 477 dictionary->SetString(kLastPingDay, value); |
| 411 SavePrefsAndNotify(); | 478 SavePrefsAndNotify(); |
| 412 } | 479 } |
| 413 | 480 |
| 481 | |
| 482 bool ExtensionPrefs::GetGrantedPermissions( | |
| 483 const std::string& extension_id, | |
| 484 std::set<std::string>* api_permissions, | |
| 485 ExtensionExtent* host_extent) { | |
| 486 DCHECK(Extension::IdIsValid(extension_id)); | |
|
Aaron Boodman
2010/11/22 07:57:53
You can leave these if you like, but it's a little
jstritar
2010/11/22 23:01:08
Got rid of all of them except the Extension::IdIsV
| |
| 487 DCHECK(api_permissions); | |
| 488 DCHECK(host_extent); | |
| 489 | |
| 490 if (!ReadExtensionPrefBoolean(extension_id, | |
| 491 kPrefGrantedPermissionsInitialized)) | |
| 492 return false; | |
| 493 | |
| 494 ReadExtensionPrefStringSet( | |
| 495 extension_id, kPrefGrantedPermissionsAPI, api_permissions); | |
| 496 | |
| 497 std::set<std::string> host_permissions; | |
| 498 ReadExtensionPrefStringSet( | |
| 499 extension_id, kPrefGrantedPermissionsHost, &host_permissions); | |
| 500 | |
| 501 for (std::set<std::string>::iterator i = host_permissions.begin(); | |
| 502 i != host_permissions.end(); ++i) | |
| 503 AddPattern(host_extent, *i); | |
|
Aaron Boodman
2010/11/22 07:57:53
No need for the helper function here since what it
jstritar
2010/11/22 23:01:08
Done.
| |
| 504 | |
| 505 return true; | |
| 506 } | |
| 507 | |
| 508 void ExtensionPrefs::AddGrantedPermissions( | |
| 509 const std::string& extension_id, | |
| 510 const std::set<std::string>& api_permissions, | |
| 511 const ExtensionExtent& host_extent) { | |
| 512 DCHECK(Extension::IdIsValid(extension_id)); | |
|
Aaron Boodman
2010/11/22 07:57:53
DCHECK->CHECK
jstritar
2010/11/22 23:01:08
Done.
| |
| 513 UpdateExtensionPref(extension_id, kPrefGrantedPermissionsInitialized, | |
|
Aaron Boodman
2010/11/22 07:57:53
Is it possible to remove this and just use the pre
jstritar
2010/11/22 23:01:08
I think we need this because empty lists and dicti
Aaron Boodman
2010/11/23 00:06:38
I see. It's a shame to bloat the preferences file
| |
| 514 Value::CreateBooleanValue(true)); | |
| 515 | |
| 516 if (!api_permissions.empty()) { | |
| 517 AddToExtensionPrefStringSet( | |
| 518 extension_id, kPrefGrantedPermissionsAPI, api_permissions); | |
| 519 } | |
| 520 | |
| 521 if (!host_extent.is_empty()) { | |
| 522 std::set<std::string> host_permissions; | |
| 523 ExtentToStringSet(host_extent, &host_permissions); | |
| 524 | |
| 525 AddToExtensionPrefStringSet( | |
| 526 extension_id, kPrefGrantedPermissionsHost, host_permissions); | |
| 527 } | |
| 528 | |
| 529 SavePrefsAndNotify(); | |
| 530 } | |
| 531 | |
| 414 Time ExtensionPrefs::LastPingDay(const std::string& extension_id) const { | 532 Time ExtensionPrefs::LastPingDay(const std::string& extension_id) const { |
| 415 DCHECK(Extension::IdIsValid(extension_id)); | 533 DCHECK(Extension::IdIsValid(extension_id)); |
| 416 return LastPingDayImpl(GetExtensionPref(extension_id)); | 534 return LastPingDayImpl(GetExtensionPref(extension_id)); |
| 417 } | 535 } |
| 418 | 536 |
| 419 Time ExtensionPrefs::BlacklistLastPingDay() const { | 537 Time ExtensionPrefs::BlacklistLastPingDay() const { |
| 420 return LastPingDayImpl(prefs_->GetDictionary(kExtensionsBlacklistUpdate)); | 538 return LastPingDayImpl(prefs_->GetDictionary(kExtensionsBlacklistUpdate)); |
| 421 } | 539 } |
| 422 | 540 |
| 423 void ExtensionPrefs::SetLastPingDay(const std::string& extension_id, | 541 void ExtensionPrefs::SetLastPingDay(const std::string& extension_id, |
| (...skipping 528 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 952 void ExtensionPrefs::RegisterUserPrefs(PrefService* prefs) { | 1070 void ExtensionPrefs::RegisterUserPrefs(PrefService* prefs) { |
| 953 prefs->RegisterDictionaryPref(kExtensionsPref); | 1071 prefs->RegisterDictionaryPref(kExtensionsPref); |
| 954 prefs->RegisterListPref(kExtensionToolbar); | 1072 prefs->RegisterListPref(kExtensionToolbar); |
| 955 prefs->RegisterIntegerPref(prefs::kExtensionToolbarSize, -1); | 1073 prefs->RegisterIntegerPref(prefs::kExtensionToolbarSize, -1); |
| 956 prefs->RegisterDictionaryPref(kExtensionsBlacklistUpdate); | 1074 prefs->RegisterDictionaryPref(kExtensionsBlacklistUpdate); |
| 957 prefs->RegisterListPref(prefs::kExtensionInstallAllowList); | 1075 prefs->RegisterListPref(prefs::kExtensionInstallAllowList); |
| 958 prefs->RegisterListPref(prefs::kExtensionInstallDenyList); | 1076 prefs->RegisterListPref(prefs::kExtensionInstallDenyList); |
| 959 prefs->RegisterListPref(prefs::kExtensionInstallForceList); | 1077 prefs->RegisterListPref(prefs::kExtensionInstallForceList); |
| 960 prefs->RegisterStringPref(kWebStoreLogin, std::string() /* default_value */); | 1078 prefs->RegisterStringPref(kWebStoreLogin, std::string() /* default_value */); |
| 961 } | 1079 } |
| OLD | NEW |