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

Side by Side Diff: chrome/browser/extensions/extension_prefs.cc

Issue 4687005: Track permissions granted to extensions in prefs (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 10 years, 1 month 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) 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"
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 // purchased apps. 68 // purchased apps.
69 const char kWebStoreLogin[] = "extensions.webstore_login"; 69 const char kWebStoreLogin[] = "extensions.webstore_login";
70 70
71 // A preference set by the the NTP to persist the desired launch container type 71 // A preference set by the the NTP to persist the desired launch container type
72 // used for apps. 72 // used for apps.
73 const char kPrefLaunchType[] = "launchType"; 73 const char kPrefLaunchType[] = "launchType";
74 74
75 // A preference determining the order of which the apps appear on the NTP. 75 // A preference determining the order of which the apps appear on the NTP.
76 const char kPrefAppLaunchIndex[] = "app_launcher_index"; 76 const char kPrefAppLaunchIndex[] = "app_launcher_index";
77 77
78 // Preferences that hold the permissions the user has granted the extension.
79 // We explicitly keep track of these so that extensions can contain unknown
80 // permissions, for backwards compatibility reasons, and we can still detect
81 // 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.
82 const char kPrefGrantedPermissionsAPI[] = "granted_permissions.api";
83 const char kPrefGrantedPermissionsHost[] = "granted_permissions.host";
84 const char kPrefGrantedPermissionsInitialized[] =
85 "granted_permissions.initialized";
86
78 } // namespace 87 } // namespace
79 88
80 //////////////////////////////////////////////////////////////////////////////// 89 ////////////////////////////////////////////////////////////////////////////////
81 90
82 namespace { 91 namespace {
83 92
84 // TODO(asargent) - This is cleanup code for a key that was introduced into 93 // TODO(asargent) - This is cleanup code for a key that was introduced into
85 // the extensions.settings sub-dictionary which wasn't a valid extension 94 // the extensions.settings sub-dictionary which wasn't a valid extension
86 // id. We can remove this in a couple of months. (See http://crbug.com/40017 95 // id. We can remove this in a couple of months. (See http://crbug.com/40017
87 // and http://crbug.com/39745 for more details). 96 // and http://crbug.com/39745 for more details).
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 DictionaryValue* copy = 213 DictionaryValue* copy =
205 static_cast<DictionaryValue*>(extensions->DeepCopy()); 214 static_cast<DictionaryValue*>(extensions->DeepCopy());
206 MakePathsAbsolute(copy); 215 MakePathsAbsolute(copy);
207 return copy; 216 return copy;
208 } 217 }
209 return new DictionaryValue; 218 return new DictionaryValue;
210 } 219 }
211 220
212 bool ExtensionPrefs::ReadBooleanFromPref( 221 bool ExtensionPrefs::ReadBooleanFromPref(
213 DictionaryValue* ext, const std::string& pref_key) { 222 DictionaryValue* ext, const std::string& pref_key) {
214 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
215 bool bool_value = false; 223 bool bool_value = false;
216 if (!ext->GetBoolean(pref_key, &bool_value)) { 224 if (!ext->GetBoolean(pref_key, &bool_value))
217 NOTREACHED() << "Failed to fetch " << pref_key << " flag.";
218 // In case we could not fetch the flag, we treat it as false.
219 return false; 225 return false;
220 } 226
221 return bool_value; 227 return bool_value;
222 } 228 }
223 229
224 bool ExtensionPrefs::ReadExtensionPrefBoolean( 230 bool ExtensionPrefs::ReadExtensionPrefBoolean(
225 const std::string& extension_id, const std::string& pref_key) { 231 const std::string& extension_id, const std::string& pref_key) {
226 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); 232 DictionaryValue* ext = GetExtensionPref(extension_id);
Aaron Boodman 2010/11/12 00:05:24 Nice little cleanup, thanks.
227 if (!extensions) 233 if (!ext) {
228 return false;
229
230 DictionaryValue* ext = NULL;
231 if (!extensions->GetDictionary(extension_id, &ext)) {
232 // No such extension yet. 234 // No such extension yet.
233 return false; 235 return false;
234 } 236 }
235 return ReadBooleanFromPref(ext, pref_key); 237 return ReadBooleanFromPref(ext, pref_key);
236 } 238 }
237 239
238 bool ExtensionPrefs::ReadIntegerFromPref( 240 bool ExtensionPrefs::ReadIntegerFromPref(
239 DictionaryValue* ext, const std::string& pref_key, int* out_value) { 241 DictionaryValue* ext, const std::string& pref_key, int* out_value) {
240 if (!ext->HasKey(pref_key)) return false; 242 if (!ext->GetInteger(pref_key, out_value))
241 if (!ext->GetInteger(pref_key, out_value)) {
242 NOTREACHED() << "Failed to fetch " << pref_key << " flag.";
243 // In case we could not fetch the flag, we treat it as false.
244 return false; 243 return false;
245 } 244
246 return out_value != NULL; 245 return out_value != NULL;
247 } 246 }
248 247
249 bool ExtensionPrefs::ReadExtensionPrefInteger( 248 bool ExtensionPrefs::ReadExtensionPrefInteger(
250 const std::string& extension_id, const std::string& pref_key, 249 const std::string& extension_id, const std::string& pref_key,
251 int* out_value) { 250 int* out_value) {
252 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); 251 DictionaryValue* ext = GetExtensionPref(extension_id);
253 if (!extensions) 252 if (!ext) {
254 return false;
255 DictionaryValue* ext = NULL;
256 if (!extensions->GetDictionary(extension_id, &ext)) {
257 // No such extension yet. 253 // No such extension yet.
258 return false; 254 return false;
259 } 255 }
260 return ReadIntegerFromPref(ext, pref_key, out_value); 256 return ReadIntegerFromPref(ext, pref_key, out_value);
261 } 257 }
262 258
259 bool ExtensionPrefs::ReadExtensionPrefList(
260 const std::string& extension_id, const std::string& pref_key,
261 ListValue** out_value) {
262 DictionaryValue* ext = GetExtensionPref(extension_id);
263 if (!ext || !ext->GetList(pref_key, out_value))
264 return false;
265
266 return out_value != NULL;
267 }
268
269 bool ExtensionPrefs::ReadExtensionPrefStringSet(
270 const std::string& extension_id,
271 const std::string& pref_key,
272 std::set<std::string>* result) {
273 DCHECK(Extension::IdIsValid(extension_id));
274 DCHECK(result);
275
276 ListValue* value = NULL;
277 if (!ReadExtensionPrefList(extension_id, pref_key, &value))
278 return false;
279
280 result->clear();
281
282 for (size_t i = 0; i < value->GetSize(); ++i) {
283 std::string item;
284 if (!value->GetString(i, &item))
285 return false;
286 result->insert(item);
287 }
288
289 return true;
290 }
291
292 void ExtensionPrefs::AddToExtensionPrefStringSet(
293 const std::string& extension_id,
294 const std::string& pref_key,
295 const std::set<std::string>& added_value) {
296 DCHECK(Extension::IdIsValid(extension_id));
297
298 std::set<std::string> old_value;
299 std::set<std::string> new_value;
300 ReadExtensionPrefStringSet(extension_id, pref_key, &old_value);
301
302 std::set_union(old_value.begin(), old_value.end(),
303 added_value.begin(), added_value.end(),
304 std::inserter(new_value, new_value.end()));
305
306 ListValue* value = new ListValue();
307 for (std::set<std::string>::const_iterator iter = new_value.begin();
308 iter != new_value.end(); ++iter)
309 value->Append(Value::CreateStringValue(*iter));
310
311 UpdateExtensionPref(extension_id, pref_key, value);
312 prefs_->ScheduleSavePersistentPrefs();
313 }
314
263 void ExtensionPrefs::SavePrefsAndNotify() { 315 void ExtensionPrefs::SavePrefsAndNotify() {
264 prefs_->ScheduleSavePersistentPrefs(); 316 prefs_->ScheduleSavePersistentPrefs();
265 prefs_->pref_notifier()->OnUserPreferenceSet(kExtensionsPref); 317 prefs_->pref_notifier()->OnUserPreferenceSet(kExtensionsPref);
266 } 318 }
267 319
268 bool ExtensionPrefs::IsBlacklistBitSet(DictionaryValue* ext) { 320 bool ExtensionPrefs::IsBlacklistBitSet(DictionaryValue* ext) {
269 return ReadBooleanFromPref(ext, kPrefBlacklist); 321 return ReadBooleanFromPref(ext, kPrefBlacklist);
270 } 322 }
271 323
272 bool ExtensionPrefs::IsExtensionBlacklisted(const std::string& extension_id) { 324 bool ExtensionPrefs::IsExtensionBlacklisted(const std::string& extension_id) {
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
397 DictionaryValue* dictionary) { 449 DictionaryValue* dictionary) {
398 if (!dictionary) { 450 if (!dictionary) {
399 NOTREACHED(); 451 NOTREACHED();
400 return; 452 return;
401 } 453 }
402 std::string value = base::Int64ToString(time.ToInternalValue()); 454 std::string value = base::Int64ToString(time.ToInternalValue());
403 dictionary->SetString(kLastPingDay, value); 455 dictionary->SetString(kLastPingDay, value);
404 SavePrefsAndNotify(); 456 SavePrefsAndNotify();
405 } 457 }
406 458
459
460 bool ExtensionPrefs::GetGrantedPermissions(
461 const std::string& extension_id,
462 std::set<std::string>* api_permissions,
463 std::set<std::string>* host_permissions) {
464 DCHECK(Extension::IdIsValid(extension_id));
465 DCHECK(api_permissions);
466 DCHECK(host_permissions);
467
468 if (!ReadExtensionPrefBoolean(extension_id,
469 kPrefGrantedPermissionsInitialized))
470 return false;
471
472 ReadExtensionPrefStringSet(
473 extension_id, kPrefGrantedPermissionsAPI, api_permissions);
474 ReadExtensionPrefStringSet(
475 extension_id, kPrefGrantedPermissionsHost, host_permissions);
476 return true;
477 }
478
479 void ExtensionPrefs::GrantPermissions(
480 const std::string& extension_id,
481 const std::set<std::string>& api_permissions,
482 const std::set<std::string>& host_permissions) {
483 DCHECK(Extension::IdIsValid(extension_id));
484 UpdateExtensionPref(extension_id, kPrefGrantedPermissionsInitialized,
485 Value::CreateBooleanValue(true));
486 if (!api_permissions.empty()) {
487 AddToExtensionPrefStringSet(
488 extension_id, kPrefGrantedPermissionsAPI, api_permissions);
489 }
490 if (!host_permissions.empty()) {
491 AddToExtensionPrefStringSet(
492 extension_id, kPrefGrantedPermissionsHost, host_permissions);
493 }
494 SavePrefsAndNotify();
495 }
496
407 Time ExtensionPrefs::LastPingDay(const std::string& extension_id) const { 497 Time ExtensionPrefs::LastPingDay(const std::string& extension_id) const {
408 DCHECK(Extension::IdIsValid(extension_id)); 498 DCHECK(Extension::IdIsValid(extension_id));
409 return LastPingDayImpl(GetExtensionPref(extension_id)); 499 return LastPingDayImpl(GetExtensionPref(extension_id));
410 } 500 }
411 501
412 Time ExtensionPrefs::BlacklistLastPingDay() const { 502 Time ExtensionPrefs::BlacklistLastPingDay() const {
413 return LastPingDayImpl(prefs_->GetDictionary(kExtensionsBlacklistUpdate)); 503 return LastPingDayImpl(prefs_->GetDictionary(kExtensionsBlacklistUpdate));
414 } 504 }
415 505
416 void ExtensionPrefs::SetLastPingDay(const std::string& extension_id, 506 void ExtensionPrefs::SetLastPingDay(const std::string& extension_id,
(...skipping 482 matching lines...) Expand 10 before | Expand all | Expand 10 after
899 void ExtensionPrefs::RegisterUserPrefs(PrefService* prefs) { 989 void ExtensionPrefs::RegisterUserPrefs(PrefService* prefs) {
900 prefs->RegisterDictionaryPref(kExtensionsPref); 990 prefs->RegisterDictionaryPref(kExtensionsPref);
901 prefs->RegisterListPref(kExtensionToolbar); 991 prefs->RegisterListPref(kExtensionToolbar);
902 prefs->RegisterIntegerPref(prefs::kExtensionToolbarSize, -1); 992 prefs->RegisterIntegerPref(prefs::kExtensionToolbarSize, -1);
903 prefs->RegisterDictionaryPref(kExtensionsBlacklistUpdate); 993 prefs->RegisterDictionaryPref(kExtensionsBlacklistUpdate);
904 prefs->RegisterListPref(prefs::kExtensionInstallAllowList); 994 prefs->RegisterListPref(prefs::kExtensionInstallAllowList);
905 prefs->RegisterListPref(prefs::kExtensionInstallDenyList); 995 prefs->RegisterListPref(prefs::kExtensionInstallDenyList);
906 prefs->RegisterListPref(prefs::kExtensionInstallForceList); 996 prefs->RegisterListPref(prefs::kExtensionInstallForceList);
907 prefs->RegisterStringPref(kWebStoreLogin, std::string() /* default_value */); 997 prefs->RegisterStringPref(kWebStoreLogin, std::string() /* default_value */);
908 } 998 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698