Chromium Code Reviews| Index: chrome/browser/extensions/extension_prefs.cc |
| diff --git a/chrome/browser/extensions/extension_prefs.cc b/chrome/browser/extensions/extension_prefs.cc |
| index bf80550c19dc12b8f73e0b45464852889ca3dc5d..95c2d49b296a477d4910bb7d15752bba0ce7a050 100644 |
| --- a/chrome/browser/extensions/extension_prefs.cc |
| +++ b/chrome/browser/extensions/extension_prefs.cc |
| @@ -75,6 +75,15 @@ const char kPrefLaunchType[] = "launchType"; |
| // A preference determining the order of which the apps appear on the NTP. |
| const char kPrefAppLaunchIndex[] = "app_launcher_index"; |
| +// Preferences that hold the permissions the user has granted the extension. |
| +// We explicitly keep track of these so that extensions can contain unknown |
| +// permissions, for backwards compatibility reasons, and we can still detect |
| +// when unknown permissions become known |
|
Aaron Boodman
2010/11/12 00:05:24
Missing period at end of sentence.
jstritar
2010/11/19 21:38:36
Done.
|
| +const char kPrefGrantedPermissionsAPI[] = "granted_permissions.api"; |
| +const char kPrefGrantedPermissionsHost[] = "granted_permissions.host"; |
| +const char kPrefGrantedPermissionsInitialized[] = |
| + "granted_permissions.initialized"; |
| + |
| } // namespace |
| //////////////////////////////////////////////////////////////////////////////// |
| @@ -211,24 +220,17 @@ DictionaryValue* ExtensionPrefs::CopyCurrentExtensions() { |
| bool ExtensionPrefs::ReadBooleanFromPref( |
| DictionaryValue* ext, const std::string& pref_key) { |
| - if (!ext->HasKey(pref_key)) return false; |
|
Aaron Boodman
2010/11/12 00:05:24
Why change this?
jstritar
2010/11/19 21:38:36
The HasKey call seemed redundant since the Get met
|
| bool bool_value = false; |
| - if (!ext->GetBoolean(pref_key, &bool_value)) { |
| - NOTREACHED() << "Failed to fetch " << pref_key << " flag."; |
| - // In case we could not fetch the flag, we treat it as false. |
| + if (!ext->GetBoolean(pref_key, &bool_value)) |
| return false; |
| - } |
| + |
| return bool_value; |
| } |
| bool ExtensionPrefs::ReadExtensionPrefBoolean( |
| const std::string& extension_id, const std::string& pref_key) { |
| - const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); |
| - if (!extensions) |
| - return false; |
| - |
| - DictionaryValue* ext = NULL; |
| - if (!extensions->GetDictionary(extension_id, &ext)) { |
| + DictionaryValue* ext = GetExtensionPref(extension_id); |
|
Aaron Boodman
2010/11/12 00:05:24
Nice little cleanup, thanks.
|
| + if (!ext) { |
| // No such extension yet. |
| return false; |
| } |
| @@ -237,29 +239,79 @@ bool ExtensionPrefs::ReadExtensionPrefBoolean( |
| bool ExtensionPrefs::ReadIntegerFromPref( |
| DictionaryValue* ext, const std::string& pref_key, int* out_value) { |
| - if (!ext->HasKey(pref_key)) return false; |
| - if (!ext->GetInteger(pref_key, out_value)) { |
| - NOTREACHED() << "Failed to fetch " << pref_key << " flag."; |
| - // In case we could not fetch the flag, we treat it as false. |
| + if (!ext->GetInteger(pref_key, out_value)) |
| return false; |
| - } |
| + |
| return out_value != NULL; |
| } |
| bool ExtensionPrefs::ReadExtensionPrefInteger( |
| const std::string& extension_id, const std::string& pref_key, |
| int* out_value) { |
| - const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); |
| - if (!extensions) |
| - return false; |
| - DictionaryValue* ext = NULL; |
| - if (!extensions->GetDictionary(extension_id, &ext)) { |
| + DictionaryValue* ext = GetExtensionPref(extension_id); |
| + if (!ext) { |
| // No such extension yet. |
| return false; |
| } |
| return ReadIntegerFromPref(ext, pref_key, out_value); |
| } |
| +bool ExtensionPrefs::ReadExtensionPrefList( |
| + const std::string& extension_id, const std::string& pref_key, |
| + ListValue** out_value) { |
| + DictionaryValue* ext = GetExtensionPref(extension_id); |
| + if (!ext || !ext->GetList(pref_key, out_value)) |
| + return false; |
| + |
| + return out_value != NULL; |
| +} |
| + |
| +bool ExtensionPrefs::ReadExtensionPrefStringSet( |
| + const std::string& extension_id, |
| + const std::string& pref_key, |
| + std::set<std::string>* result) { |
| + DCHECK(Extension::IdIsValid(extension_id)); |
| + DCHECK(result); |
| + |
| + ListValue* value = NULL; |
| + if (!ReadExtensionPrefList(extension_id, pref_key, &value)) |
| + return false; |
| + |
| + result->clear(); |
| + |
| + for (size_t i = 0; i < value->GetSize(); ++i) { |
| + std::string item; |
| + if (!value->GetString(i, &item)) |
| + return false; |
| + result->insert(item); |
| + } |
| + |
| + return true; |
| +} |
| + |
| +void ExtensionPrefs::AddToExtensionPrefStringSet( |
| + const std::string& extension_id, |
| + const std::string& pref_key, |
| + const std::set<std::string>& added_value) { |
| + DCHECK(Extension::IdIsValid(extension_id)); |
| + |
| + std::set<std::string> old_value; |
| + std::set<std::string> new_value; |
| + ReadExtensionPrefStringSet(extension_id, pref_key, &old_value); |
| + |
| + std::set_union(old_value.begin(), old_value.end(), |
| + added_value.begin(), added_value.end(), |
| + std::inserter(new_value, new_value.end())); |
| + |
| + ListValue* value = new ListValue(); |
| + for (std::set<std::string>::const_iterator iter = new_value.begin(); |
| + iter != new_value.end(); ++iter) |
| + value->Append(Value::CreateStringValue(*iter)); |
| + |
| + UpdateExtensionPref(extension_id, pref_key, value); |
| + prefs_->ScheduleSavePersistentPrefs(); |
| +} |
| + |
| void ExtensionPrefs::SavePrefsAndNotify() { |
| prefs_->ScheduleSavePersistentPrefs(); |
| prefs_->pref_notifier()->OnUserPreferenceSet(kExtensionsPref); |
| @@ -404,6 +456,44 @@ void ExtensionPrefs::SetLastPingDayImpl(const Time& time, |
| SavePrefsAndNotify(); |
| } |
| + |
| +bool ExtensionPrefs::GetGrantedPermissions( |
| + const std::string& extension_id, |
| + std::set<std::string>* api_permissions, |
| + std::set<std::string>* host_permissions) { |
| + DCHECK(Extension::IdIsValid(extension_id)); |
| + DCHECK(api_permissions); |
| + DCHECK(host_permissions); |
| + |
| + if (!ReadExtensionPrefBoolean(extension_id, |
| + kPrefGrantedPermissionsInitialized)) |
| + return false; |
| + |
| + ReadExtensionPrefStringSet( |
| + extension_id, kPrefGrantedPermissionsAPI, api_permissions); |
| + ReadExtensionPrefStringSet( |
| + extension_id, kPrefGrantedPermissionsHost, host_permissions); |
| + return true; |
| +} |
| + |
| +void ExtensionPrefs::GrantPermissions( |
| + const std::string& extension_id, |
| + const std::set<std::string>& api_permissions, |
| + const std::set<std::string>& host_permissions) { |
| + DCHECK(Extension::IdIsValid(extension_id)); |
| + UpdateExtensionPref(extension_id, kPrefGrantedPermissionsInitialized, |
| + Value::CreateBooleanValue(true)); |
| + if (!api_permissions.empty()) { |
| + AddToExtensionPrefStringSet( |
| + extension_id, kPrefGrantedPermissionsAPI, api_permissions); |
| + } |
| + if (!host_permissions.empty()) { |
| + AddToExtensionPrefStringSet( |
| + extension_id, kPrefGrantedPermissionsHost, host_permissions); |
| + } |
| + SavePrefsAndNotify(); |
| +} |
| + |
| Time ExtensionPrefs::LastPingDay(const std::string& extension_id) const { |
| DCHECK(Extension::IdIsValid(extension_id)); |
| return LastPingDayImpl(GetExtensionPref(extension_id)); |