| 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 which 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 kPrefGrantedPermissionsAll[] = "granted_permissions.full"; |
| 93 |
| 85 } // namespace | 94 } // namespace |
| 86 | 95 |
| 87 //////////////////////////////////////////////////////////////////////////////// | 96 //////////////////////////////////////////////////////////////////////////////// |
| 88 | 97 |
| 89 namespace { | 98 namespace { |
| 90 | 99 |
| 91 // TODO(asargent) - This is cleanup code for a key that was introduced into | 100 // 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 | 101 // 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 | 102 // id. We can remove this in a couple of months. (See http://crbug.com/40017 |
| 94 // and http://crbug.com/39745 for more details). | 103 // and http://crbug.com/39745 for more details). |
| (...skipping 11 matching lines...) Expand all Loading... |
| 106 bool dirty = false; | 115 bool dirty = false; |
| 107 for (std::set<std::string>::iterator i = bad_keys.begin(); | 116 for (std::set<std::string>::iterator i = bad_keys.begin(); |
| 108 i != bad_keys.end(); ++i) { | 117 i != bad_keys.end(); ++i) { |
| 109 dirty = true; | 118 dirty = true; |
| 110 dictionary->Remove(*i, NULL); | 119 dictionary->Remove(*i, NULL); |
| 111 } | 120 } |
| 112 if (dirty) | 121 if (dirty) |
| 113 prefs->ScheduleSavePersistentPrefs(); | 122 prefs->ScheduleSavePersistentPrefs(); |
| 114 } | 123 } |
| 115 | 124 |
| 125 static void ExtentToStringSet(const ExtensionExtent& host_extent, |
| 126 std::set<std::string>* result) { |
| 127 ExtensionExtent::PatternList patterns = host_extent.patterns(); |
| 128 ExtensionExtent::PatternList::const_iterator i; |
| 129 |
| 130 for (i = patterns.begin(); i != patterns.end(); ++i) |
| 131 result->insert(i->GetAsString()); |
| 132 } |
| 133 |
| 116 } // namespace | 134 } // namespace |
| 117 | 135 |
| 118 ExtensionPrefs::ExtensionPrefs(PrefService* prefs, const FilePath& root_dir) | 136 ExtensionPrefs::ExtensionPrefs(PrefService* prefs, const FilePath& root_dir) |
| 119 : prefs_(prefs), | 137 : prefs_(prefs), |
| 120 install_directory_(root_dir) { | 138 install_directory_(root_dir) { |
| 121 // TODO(asargent) - Remove this in a couple of months. (See comment above | 139 // TODO(asargent) - Remove this in a couple of months. (See comment above |
| 122 // CleanupBadExtensionKeys). | 140 // CleanupBadExtensionKeys). |
| 123 CleanupBadExtensionKeys(prefs); | 141 CleanupBadExtensionKeys(prefs); |
| 124 | 142 |
| 125 MakePathsRelative(); | 143 MakePathsRelative(); |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 DictionaryValue* copy = | 229 DictionaryValue* copy = |
| 212 static_cast<DictionaryValue*>(extensions->DeepCopy()); | 230 static_cast<DictionaryValue*>(extensions->DeepCopy()); |
| 213 MakePathsAbsolute(copy); | 231 MakePathsAbsolute(copy); |
| 214 return copy; | 232 return copy; |
| 215 } | 233 } |
| 216 return new DictionaryValue; | 234 return new DictionaryValue; |
| 217 } | 235 } |
| 218 | 236 |
| 219 bool ExtensionPrefs::ReadBooleanFromPref( | 237 bool ExtensionPrefs::ReadBooleanFromPref( |
| 220 DictionaryValue* ext, const std::string& pref_key) { | 238 DictionaryValue* ext, const std::string& pref_key) { |
| 221 if (!ext->HasKey(pref_key)) return false; | |
| 222 bool bool_value = false; | 239 bool bool_value = false; |
| 223 if (!ext->GetBoolean(pref_key, &bool_value)) { | 240 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; | 241 return false; |
| 227 } | 242 |
| 228 return bool_value; | 243 return bool_value; |
| 229 } | 244 } |
| 230 | 245 |
| 231 bool ExtensionPrefs::ReadExtensionPrefBoolean( | 246 bool ExtensionPrefs::ReadExtensionPrefBoolean( |
| 232 const std::string& extension_id, const std::string& pref_key) { | 247 const std::string& extension_id, const std::string& pref_key) { |
| 233 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); | 248 DictionaryValue* ext = GetExtensionPref(extension_id); |
| 234 if (!extensions) | 249 if (!ext) { |
| 235 return false; | |
| 236 | |
| 237 DictionaryValue* ext = NULL; | |
| 238 if (!extensions->GetDictionary(extension_id, &ext)) { | |
| 239 // No such extension yet. | 250 // No such extension yet. |
| 240 return false; | 251 return false; |
| 241 } | 252 } |
| 242 return ReadBooleanFromPref(ext, pref_key); | 253 return ReadBooleanFromPref(ext, pref_key); |
| 243 } | 254 } |
| 244 | 255 |
| 245 bool ExtensionPrefs::ReadIntegerFromPref( | 256 bool ExtensionPrefs::ReadIntegerFromPref( |
| 246 DictionaryValue* ext, const std::string& pref_key, int* out_value) { | 257 DictionaryValue* ext, const std::string& pref_key, int* out_value) { |
| 247 if (!ext->HasKey(pref_key)) return false; | 258 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; | 259 return false; |
| 252 } | 260 |
| 253 return out_value != NULL; | 261 return out_value != NULL; |
| 254 } | 262 } |
| 255 | 263 |
| 256 bool ExtensionPrefs::ReadExtensionPrefInteger( | 264 bool ExtensionPrefs::ReadExtensionPrefInteger( |
| 257 const std::string& extension_id, const std::string& pref_key, | 265 const std::string& extension_id, const std::string& pref_key, |
| 258 int* out_value) { | 266 int* out_value) { |
| 259 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); | 267 DictionaryValue* ext = GetExtensionPref(extension_id); |
| 260 if (!extensions) | 268 if (!ext) { |
| 261 return false; | |
| 262 DictionaryValue* ext = NULL; | |
| 263 if (!extensions->GetDictionary(extension_id, &ext)) { | |
| 264 // No such extension yet. | 269 // No such extension yet. |
| 265 return false; | 270 return false; |
| 266 } | 271 } |
| 267 return ReadIntegerFromPref(ext, pref_key, out_value); | 272 return ReadIntegerFromPref(ext, pref_key, out_value); |
| 268 } | 273 } |
| 269 | 274 |
| 275 bool ExtensionPrefs::ReadExtensionPrefList( |
| 276 const std::string& extension_id, const std::string& pref_key, |
| 277 ListValue** out_value) { |
| 278 DictionaryValue* ext = GetExtensionPref(extension_id); |
| 279 if (!ext || !ext->GetList(pref_key, out_value)) |
| 280 return false; |
| 281 |
| 282 return out_value != NULL; |
| 283 } |
| 284 |
| 285 bool ExtensionPrefs::ReadExtensionPrefStringSet( |
| 286 const std::string& extension_id, |
| 287 const std::string& pref_key, |
| 288 std::set<std::string>* result) { |
| 289 ListValue* value = NULL; |
| 290 if (!ReadExtensionPrefList(extension_id, pref_key, &value)) |
| 291 return false; |
| 292 |
| 293 result->clear(); |
| 294 |
| 295 for (size_t i = 0; i < value->GetSize(); ++i) { |
| 296 std::string item; |
| 297 if (!value->GetString(i, &item)) |
| 298 return false; |
| 299 result->insert(item); |
| 300 } |
| 301 |
| 302 return true; |
| 303 } |
| 304 |
| 305 void ExtensionPrefs::AddToExtensionPrefStringSet( |
| 306 const std::string& extension_id, |
| 307 const std::string& pref_key, |
| 308 const std::set<std::string>& added_value) { |
| 309 std::set<std::string> old_value; |
| 310 std::set<std::string> new_value; |
| 311 ReadExtensionPrefStringSet(extension_id, pref_key, &old_value); |
| 312 |
| 313 std::set_union(old_value.begin(), old_value.end(), |
| 314 added_value.begin(), added_value.end(), |
| 315 std::inserter(new_value, new_value.end())); |
| 316 |
| 317 ListValue* value = new ListValue(); |
| 318 for (std::set<std::string>::const_iterator iter = new_value.begin(); |
| 319 iter != new_value.end(); ++iter) |
| 320 value->Append(Value::CreateStringValue(*iter)); |
| 321 |
| 322 UpdateExtensionPref(extension_id, pref_key, value); |
| 323 prefs_->ScheduleSavePersistentPrefs(); |
| 324 } |
| 325 |
| 270 void ExtensionPrefs::SavePrefsAndNotify() { | 326 void ExtensionPrefs::SavePrefsAndNotify() { |
| 271 prefs_->ScheduleSavePersistentPrefs(); | 327 prefs_->ScheduleSavePersistentPrefs(); |
| 272 prefs_->pref_notifier()->OnUserPreferenceSet(kExtensionsPref); | 328 prefs_->pref_notifier()->OnUserPreferenceSet(kExtensionsPref); |
| 273 } | 329 } |
| 274 | 330 |
| 275 bool ExtensionPrefs::IsBlacklistBitSet(DictionaryValue* ext) { | 331 bool ExtensionPrefs::IsBlacklistBitSet(DictionaryValue* ext) { |
| 276 return ReadBooleanFromPref(ext, kPrefBlacklist); | 332 return ReadBooleanFromPref(ext, kPrefBlacklist); |
| 277 } | 333 } |
| 278 | 334 |
| 279 bool ExtensionPrefs::IsExtensionBlacklisted(const std::string& extension_id) { | 335 bool ExtensionPrefs::IsExtensionBlacklisted(const std::string& extension_id) { |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 404 DictionaryValue* dictionary) { | 460 DictionaryValue* dictionary) { |
| 405 if (!dictionary) { | 461 if (!dictionary) { |
| 406 NOTREACHED(); | 462 NOTREACHED(); |
| 407 return; | 463 return; |
| 408 } | 464 } |
| 409 std::string value = base::Int64ToString(time.ToInternalValue()); | 465 std::string value = base::Int64ToString(time.ToInternalValue()); |
| 410 dictionary->SetString(kLastPingDay, value); | 466 dictionary->SetString(kLastPingDay, value); |
| 411 SavePrefsAndNotify(); | 467 SavePrefsAndNotify(); |
| 412 } | 468 } |
| 413 | 469 |
| 470 |
| 471 bool ExtensionPrefs::GetGrantedPermissions( |
| 472 const std::string& extension_id, |
| 473 bool* full_access, |
| 474 std::set<std::string>* api_permissions, |
| 475 ExtensionExtent* host_extent) { |
| 476 CHECK(Extension::IdIsValid(extension_id)); |
| 477 |
| 478 DictionaryValue* ext = GetExtensionPref(extension_id); |
| 479 if (!ext || !ext->GetBoolean(kPrefGrantedPermissionsAll, full_access)) |
| 480 return false; |
| 481 |
| 482 ReadExtensionPrefStringSet( |
| 483 extension_id, kPrefGrantedPermissionsAPI, api_permissions); |
| 484 |
| 485 std::set<std::string> host_permissions; |
| 486 ReadExtensionPrefStringSet( |
| 487 extension_id, kPrefGrantedPermissionsHost, &host_permissions); |
| 488 |
| 489 for (std::set<std::string>::iterator i = host_permissions.begin(); |
| 490 i != host_permissions.end(); ++i) |
| 491 host_extent->AddPattern(URLPattern( |
| 492 Extension::kValidWebExtentSchemes | UserScript::kValidUserScriptSchemes, |
| 493 *i)); |
| 494 |
| 495 return true; |
| 496 } |
| 497 |
| 498 void ExtensionPrefs::AddGrantedPermissions( |
| 499 const std::string& extension_id, |
| 500 const bool full_access, |
| 501 const std::set<std::string>& api_permissions, |
| 502 const ExtensionExtent& host_extent) { |
| 503 CHECK(Extension::IdIsValid(extension_id)); |
| 504 |
| 505 UpdateExtensionPref(extension_id, kPrefGrantedPermissionsAll, |
| 506 Value::CreateBooleanValue(full_access)); |
| 507 |
| 508 if (!api_permissions.empty()) { |
| 509 AddToExtensionPrefStringSet( |
| 510 extension_id, kPrefGrantedPermissionsAPI, api_permissions); |
| 511 } |
| 512 |
| 513 if (!host_extent.is_empty()) { |
| 514 std::set<std::string> host_permissions; |
| 515 ExtentToStringSet(host_extent, &host_permissions); |
| 516 |
| 517 AddToExtensionPrefStringSet( |
| 518 extension_id, kPrefGrantedPermissionsHost, host_permissions); |
| 519 } |
| 520 |
| 521 SavePrefsAndNotify(); |
| 522 } |
| 523 |
| 414 Time ExtensionPrefs::LastPingDay(const std::string& extension_id) const { | 524 Time ExtensionPrefs::LastPingDay(const std::string& extension_id) const { |
| 415 DCHECK(Extension::IdIsValid(extension_id)); | 525 DCHECK(Extension::IdIsValid(extension_id)); |
| 416 return LastPingDayImpl(GetExtensionPref(extension_id)); | 526 return LastPingDayImpl(GetExtensionPref(extension_id)); |
| 417 } | 527 } |
| 418 | 528 |
| 419 Time ExtensionPrefs::BlacklistLastPingDay() const { | 529 Time ExtensionPrefs::BlacklistLastPingDay() const { |
| 420 return LastPingDayImpl(prefs_->GetDictionary(kExtensionsBlacklistUpdate)); | 530 return LastPingDayImpl(prefs_->GetDictionary(kExtensionsBlacklistUpdate)); |
| 421 } | 531 } |
| 422 | 532 |
| 423 void ExtensionPrefs::SetLastPingDay(const std::string& extension_id, | 533 void ExtensionPrefs::SetLastPingDay(const std::string& extension_id, |
| (...skipping 538 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 962 void ExtensionPrefs::RegisterUserPrefs(PrefService* prefs) { | 1072 void ExtensionPrefs::RegisterUserPrefs(PrefService* prefs) { |
| 963 prefs->RegisterDictionaryPref(kExtensionsPref); | 1073 prefs->RegisterDictionaryPref(kExtensionsPref); |
| 964 prefs->RegisterListPref(kExtensionToolbar); | 1074 prefs->RegisterListPref(kExtensionToolbar); |
| 965 prefs->RegisterIntegerPref(prefs::kExtensionToolbarSize, -1); | 1075 prefs->RegisterIntegerPref(prefs::kExtensionToolbarSize, -1); |
| 966 prefs->RegisterDictionaryPref(kExtensionsBlacklistUpdate); | 1076 prefs->RegisterDictionaryPref(kExtensionsBlacklistUpdate); |
| 967 prefs->RegisterListPref(prefs::kExtensionInstallAllowList); | 1077 prefs->RegisterListPref(prefs::kExtensionInstallAllowList); |
| 968 prefs->RegisterListPref(prefs::kExtensionInstallDenyList); | 1078 prefs->RegisterListPref(prefs::kExtensionInstallDenyList); |
| 969 prefs->RegisterListPref(prefs::kExtensionInstallForceList); | 1079 prefs->RegisterListPref(prefs::kExtensionInstallForceList); |
| 970 prefs->RegisterStringPref(kWebStoreLogin, std::string() /* default_value */); | 1080 prefs->RegisterStringPref(kWebStoreLogin, std::string() /* default_value */); |
| 971 } | 1081 } |
| OLD | NEW |