Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1775)

Unified Diff: chrome/browser/extensions/extension_prefs.cc

Issue 10824116: Store Media gallery permissions in extension prefs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments and rebase Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/extensions/extension_prefs.h ('k') | chrome/browser/extensions/extension_prefs_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..3437643a4d4ab5b8b1688ad08ba7fd30a844d527 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,121 @@ 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)) {
+ return true;
+ }
+ NOTREACHED();
+ return false;
+}
+
+void RemoveMediaGalleryPermissionsFromExtension(PrefService* prefs,
+ 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) {
+ 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 it = extensions->begin_keys();
+ it != extensions->end_keys();
+ ++it) {
+ const std::string& id(*it);
+ 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);
« no previous file with comments | « chrome/browser/extensions/extension_prefs.h ('k') | chrome/browser/extensions/extension_prefs_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698