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 9676f74170c8c9cf48b6f36ed0e1bbfb06197aa8..5bcde7f81458e65a0aaf9d34677d1facffaa56d1 100644 |
| --- a/chrome/browser/extensions/extension_prefs.cc |
| +++ b/chrome/browser/extensions/extension_prefs.cc |
| @@ -178,6 +178,15 @@ const char kFilteredEvents[] = "filtered_events"; |
| // Persisted value for omnibox.setDefaultSuggestion. |
| const char kOmniboxDefaultSuggestion[] = "omnibox_default_suggestion"; |
| +// List of media gallery permissions. |
| +const char kMediaGalleriesPermissions[] = "media_galleries_permissions"; |
| + |
| +// Key for Media Gallery ID. |
| +const char kMediaGalleryIdKey[] = "id"; |
| + |
| +// Key for Media Gallery Permission Value. |
| +const char kMediaGalleryHasPermissionKey[] = "has_permission"; |
| + |
| // Provider of write access to a dictionary storing extension prefs. |
| class ScopedExtensionPrefUpdate : public DictionaryPrefUpdate { |
| public: |
| @@ -1150,6 +1159,120 @@ void ExtensionPrefs::SetLaunchType(const std::string& extension_id, |
| Value::CreateIntegerValue(static_cast<int>(launch_type))); |
| } |
| +namespace { |
| + |
| +bool GetMediaGalleryPermissionFromDictionary( |
| + const DictionaryValue* dict, |
| + MediaGalleryPermission* out_permission) { |
| + std::string string_id; |
| + if (dict->GetString(kMediaGalleryIdKey, &string_id) && |
| + base::StringToUint64(string_id, &out_permission->pref_id) && |
| + dict->GetBoolean(kMediaGalleryHasPermissionKey, |
| + &out_permission->has_permission)) { |
|
Evan Stade
2012/08/02 00:53:57
indent to be flush with kMediaGallery
|
| + return true; |
| + } |
| + NOTREACHED(); |
| + return false; |
| +} |
| + |
| +void RemoveMediaGalleryPermissionsFromExtension(PrefService * prefs, |
|
Evan Stade
2012/08/02 00:53:57
no space before *
|
| + const std::string& extension_id, |
| + MediaGalleryPrefId gallery_id) { |
| + ScopedExtensionPrefUpdate update(prefs, extension_id); |
| + DictionaryValue* extension_dict = update.Get(); |
| + ListValue* permissions = NULL; |
| + if (!extension_dict->GetList(kMediaGalleriesPermissions, &permissions)) |
| + return; |
| + |
| + for (ListValue::iterator it = permissions->begin(); |
| + it != permissions->end(); |
| + it++) { |
|
Evan Stade
2012/08/02 00:53:57
++it here and elsewhere
|
| + const DictionaryValue* dict = NULL; |
| + if (!(*it)->GetAsDictionary(&dict)) |
| + continue; |
| + MediaGalleryPermission perm; |
| + if (!GetMediaGalleryPermissionFromDictionary(dict, &perm)) |
| + continue; |
| + if (perm.pref_id == gallery_id) { |
| + permissions->Erase(it, NULL); |
| + return; |
| + } |
| + } |
| +} |
| + |
| +} // namespace |
| + |
| +void ExtensionPrefs::SetMediaGalleryPermission(const std::string& extension_id, |
| + MediaGalleryPrefId gallery, |
| + bool has_access) { |
| + ScopedExtensionPrefUpdate update(prefs_, extension_id); |
| + DictionaryValue* extension_dict = update.Get(); |
| + ListValue* permissions = NULL; |
| + if (!extension_dict->GetList(kMediaGalleriesPermissions, &permissions)) { |
| + permissions = new ListValue; |
| + extension_dict->Set(kMediaGalleriesPermissions, permissions); |
| + } else { |
| + // If the gallery is already in the list, update the permission. |
| + for (ListValue::const_iterator it = permissions->begin(); |
| + it != permissions->end(); |
| + it++) { |
| + DictionaryValue* dict = NULL; |
| + if (!(*it)->GetAsDictionary(&dict)) |
| + continue; |
| + MediaGalleryPermission perm; |
| + if (!GetMediaGalleryPermissionFromDictionary(dict, &perm)) |
| + continue; |
| + if (perm.pref_id == gallery) { |
| + dict->SetBoolean(kMediaGalleryHasPermissionKey, has_access); |
| + return; |
| + } |
| + } |
| + } |
| + |
| + DictionaryValue* dict = new DictionaryValue; |
| + dict->SetString(kMediaGalleryIdKey, base::Uint64ToString(gallery)); |
| + dict->SetBoolean(kMediaGalleryHasPermissionKey, has_access); |
| + permissions->Append(dict); |
| +} |
| + |
| +std::vector<MediaGalleryPermission> ExtensionPrefs::GetMediaGalleryPermissions( |
| + const std::string& extension_id) { |
| + std::vector<MediaGalleryPermission> result; |
| + const ListValue* permissions = NULL; |
| + if (ReadExtensionPrefList(extension_id, kMediaGalleriesPermissions, |
| + &permissions)) { |
| + for (ListValue::const_iterator it = permissions->begin(); |
| + it != permissions->end(); |
| + it++) { |
| + DictionaryValue* dict = NULL; |
| + if (!(*it)->GetAsDictionary(&dict)) |
| + continue; |
| + MediaGalleryPermission perm; |
| + if (!GetMediaGalleryPermissionFromDictionary(dict, &perm)) |
| + continue; |
| + result.push_back(perm); |
| + } |
| + } |
| + return result; |
| +} |
| + |
| +void ExtensionPrefs::RemoveMediaGalleryPermissions( |
| + MediaGalleryPrefId gallery_id) { |
| + const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); |
| + if (!extensions) |
| + return; |
| + |
| + for (DictionaryValue::key_iterator iter = extensions->begin_keys(); |
| + iter != extensions->end_keys(); ++iter) { |
| + const std::string& id(*iter); |
| + if (!Extension::IdIsValid(id)) { |
| + NOTREACHED(); |
| + continue; |
| + } |
| + RemoveMediaGalleryPermissionsFromExtension(prefs_, id, gallery_id); |
| + } |
| +} |
| + |
| bool ExtensionPrefs::DoesExtensionHaveState( |
| const std::string& id, Extension::State check_state) const { |
| const DictionaryValue* extension = GetExtensionPref(id); |