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

Side by Side 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: Created 8 years, 4 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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_number_conversions.h" 7 #include "base/string_number_conversions.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "base/utf_string_conversions.h" 9 #include "base/utf_string_conversions.h"
10 #include "chrome/browser/extensions/admin_policy.h" 10 #include "chrome/browser/extensions/admin_policy.h"
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 // background page. 171 // background page.
172 const char kRegisteredEvents[] = "events"; 172 const char kRegisteredEvents[] = "events";
173 173
174 // A dictionary of event names to lists of filters that this extension has 174 // A dictionary of event names to lists of filters that this extension has
175 // registered from its lazy background page. 175 // registered from its lazy background page.
176 const char kFilteredEvents[] = "filtered_events"; 176 const char kFilteredEvents[] = "filtered_events";
177 177
178 // Persisted value for omnibox.setDefaultSuggestion. 178 // Persisted value for omnibox.setDefaultSuggestion.
179 const char kOmniboxDefaultSuggestion[] = "omnibox_default_suggestion"; 179 const char kOmniboxDefaultSuggestion[] = "omnibox_default_suggestion";
180 180
181 // List of media gallery permissions.
182 const char kMediaGalleriesPermissions[] = "media_galleries_permissions";
183
184 // Key for Media Gallery ID.
185 const char kMediaGalleryIdKey[] = "id";
186
187 // Key for Media Gallery Permission Value.
188 const char kMediaGalleryHasPermissionKey[] = "has_permission";
189
181 // Provider of write access to a dictionary storing extension prefs. 190 // Provider of write access to a dictionary storing extension prefs.
182 class ScopedExtensionPrefUpdate : public DictionaryPrefUpdate { 191 class ScopedExtensionPrefUpdate : public DictionaryPrefUpdate {
183 public: 192 public:
184 ScopedExtensionPrefUpdate(PrefService* service, 193 ScopedExtensionPrefUpdate(PrefService* service,
185 const std::string& extension_id) : 194 const std::string& extension_id) :
186 DictionaryPrefUpdate(service, ExtensionPrefs::kExtensionsPref), 195 DictionaryPrefUpdate(service, ExtensionPrefs::kExtensionsPref),
187 extension_id_(extension_id) {} 196 extension_id_(extension_id) {}
188 197
189 virtual ~ScopedExtensionPrefUpdate() { 198 virtual ~ScopedExtensionPrefUpdate() {
190 } 199 }
(...skipping 952 matching lines...) Expand 10 before | Expand all | Expand 10 after
1143 1152
1144 return result; 1153 return result;
1145 } 1154 }
1146 1155
1147 void ExtensionPrefs::SetLaunchType(const std::string& extension_id, 1156 void ExtensionPrefs::SetLaunchType(const std::string& extension_id,
1148 LaunchType launch_type) { 1157 LaunchType launch_type) {
1149 UpdateExtensionPref(extension_id, kPrefLaunchType, 1158 UpdateExtensionPref(extension_id, kPrefLaunchType,
1150 Value::CreateIntegerValue(static_cast<int>(launch_type))); 1159 Value::CreateIntegerValue(static_cast<int>(launch_type)));
1151 } 1160 }
1152 1161
1162 namespace {
1163
1164 bool GetMediaGalleryPermissionFromDictionary(
1165 const DictionaryValue* dict,
1166 MediaGalleryPermission* out_permission) {
1167 std::string string_id;
1168 if (dict->GetString(kMediaGalleryIdKey, &string_id) &&
1169 base::StringToUint64(string_id, &out_permission->pref_id) &&
1170 dict->GetBoolean(kMediaGalleryHasPermissionKey,
1171 &out_permission->has_permission)) {
1172 return true;
1173 }
1174 NOTREACHED();
1175 return false;
1176 }
1177
1178 void RemoveMediaGalleryPermissionsFromExtension(PrefService * prefs,
1179 const std::string& extension_id,
1180 MediaGalleryPrefId gallery_id) {
1181 ScopedExtensionPrefUpdate update(prefs, extension_id);
1182 DictionaryValue* extension_dict = update.Get();
1183 ListValue* permissions = NULL;
1184 if (!extension_dict->GetList(kMediaGalleriesPermissions, &permissions))
1185 return;
1186
1187 for (ListValue::iterator it = permissions->begin();
1188 it != permissions->end();
1189 it++) {
1190 const DictionaryValue* dict = NULL;
1191 if (!(*it)->GetAsDictionary(&dict))
1192 continue;
1193 MediaGalleryPermission perm;
1194 if (!GetMediaGalleryPermissionFromDictionary(dict, &perm))
1195 continue;
1196 if (perm.pref_id == gallery_id) {
1197 permissions->Erase(it, NULL);
1198 return;
1199 }
1200 }
1201 }
1202
1203 } // namespace
1204
1205 void ExtensionPrefs::SetMediaGalleryPermission(const std::string& extension_id,
1206 MediaGalleryPrefId gallery,
1207 bool has_access) {
1208 ScopedExtensionPrefUpdate update(prefs_, extension_id);
1209 DictionaryValue* extension_dict = update.Get();
1210 ListValue* permissions = NULL;
1211 if (!extension_dict->GetList(kMediaGalleriesPermissions, &permissions)) {
1212 permissions = new ListValue;
1213 extension_dict->Set(kMediaGalleriesPermissions, permissions);
1214 } else {
1215 // If the gallery is already in the list, update the permission.
1216 for (ListValue::const_iterator it = permissions->begin();
1217 it != permissions->end();
1218 it++) {
1219 DictionaryValue* dict = NULL;
1220 if (!(*it)->GetAsDictionary(&dict))
1221 continue;
1222 MediaGalleryPermission perm;
1223 if (!GetMediaGalleryPermissionFromDictionary(dict, &perm))
1224 continue;
1225 if (perm.pref_id == gallery) {
1226 dict->SetBoolean(kMediaGalleryHasPermissionKey, has_access);
1227 return;
1228 }
1229 }
1230 }
1231
1232 DictionaryValue* dict = new DictionaryValue;
1233 dict->SetString(kMediaGalleryIdKey, base::Uint64ToString(gallery));
1234 dict->SetBoolean(kMediaGalleryHasPermissionKey, has_access);
1235 permissions->Append(dict);
1236 }
1237
1238 std::vector<MediaGalleryPermission> ExtensionPrefs::GetMediaGalleryPermissions(
1239 const std::string& extension_id) {
1240 std::vector<MediaGalleryPermission> result;
1241 const ListValue* permissions = NULL;
1242 if (ReadExtensionPrefList(extension_id, kMediaGalleriesPermissions,
1243 &permissions)) {
1244 for (ListValue::const_iterator it = permissions->begin();
1245 it != permissions->end();
1246 it++) {
1247 DictionaryValue* dict = NULL;
1248 if (!(*it)->GetAsDictionary(&dict))
1249 continue;
1250 MediaGalleryPermission perm;
1251 if (!GetMediaGalleryPermissionFromDictionary(dict, &perm))
1252 continue;
1253 result.push_back(perm);
1254 }
1255 }
1256 return result;
1257 }
1258
1259 void ExtensionPrefs::RemoveMediaGalleryPermissions(
1260 MediaGalleryPrefId gallery_id) {
1261 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref);
1262 if (!extensions)
1263 return;
1264
1265 for (DictionaryValue::key_iterator iter = extensions->begin_keys();
1266 iter != extensions->end_keys(); ++iter) {
1267 const std::string& id(*iter);
1268 if (!Extension::IdIsValid(id)) {
1269 NOTREACHED();
1270 continue;
1271 }
1272 RemoveMediaGalleryPermissionsFromExtension(prefs_, id, gallery_id);
1273 }
1274 }
1275
1153 bool ExtensionPrefs::DoesExtensionHaveState( 1276 bool ExtensionPrefs::DoesExtensionHaveState(
1154 const std::string& id, Extension::State check_state) const { 1277 const std::string& id, Extension::State check_state) const {
1155 const DictionaryValue* extension = GetExtensionPref(id); 1278 const DictionaryValue* extension = GetExtensionPref(id);
1156 int state = -1; 1279 int state = -1;
1157 if (!extension || !extension->GetInteger(kPrefState, &state)) 1280 if (!extension || !extension->GetInteger(kPrefState, &state))
1158 return false; 1281 return false;
1159 1282
1160 if (state < 0 || state >= Extension::NUM_STATES) { 1283 if (state < 0 || state >= Extension::NUM_STATES) {
1161 LOG(ERROR) << "Bad pref 'state' for extension '" << id << "'"; 1284 LOG(ERROR) << "Bad pref 'state' for extension '" << id << "'";
1162 return false; 1285 return false;
(...skipping 771 matching lines...) Expand 10 before | Expand all | Expand 10 after
1934 0, // default value 2057 0, // default value
1935 PrefService::UNSYNCABLE_PREF); 2058 PrefService::UNSYNCABLE_PREF);
1936 prefs->RegisterInt64Pref(prefs::kNextExtensionsUpdateCheck, 2059 prefs->RegisterInt64Pref(prefs::kNextExtensionsUpdateCheck,
1937 0, // default value 2060 0, // default value
1938 PrefService::UNSYNCABLE_PREF); 2061 PrefService::UNSYNCABLE_PREF);
1939 prefs->RegisterListPref(prefs::kExtensionAllowedInstallSites, 2062 prefs->RegisterListPref(prefs::kExtensionAllowedInstallSites,
1940 PrefService::UNSYNCABLE_PREF); 2063 PrefService::UNSYNCABLE_PREF);
1941 } 2064 }
1942 2065
1943 } // namespace extensions 2066 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698