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

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: incorporate feedback Created 10 years 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"
11 #include "chrome/common/extensions/url_pattern.h"
11 #include "chrome/common/notification_service.h" 12 #include "chrome/common/notification_service.h"
12 #include "chrome/common/pref_names.h" 13 #include "chrome/common/pref_names.h"
13 14
14 using base::Time; 15 using base::Time;
15 16
16 namespace { 17 namespace {
17 18
18 // Additional preferences keys 19 // Additional preferences keys
19 20
20 // Where an extension was installed from. (see Extension::Location) 21 // Where an extension was installed from. (see Extension::Location)
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 76
76 // A preference determining the order of which the apps appear on the NTP. 77 // A preference determining the order of which the apps appear on the NTP.
77 const char kPrefAppLaunchIndex[] = "app_launcher_index"; 78 const char kPrefAppLaunchIndex[] = "app_launcher_index";
78 79
79 // A preference for storing extra data sent in update checks for an extension. 80 // A preference for storing extra data sent in update checks for an extension.
80 const char kUpdateUrlData[] = "update_url_data"; 81 const char kUpdateUrlData[] = "update_url_data";
81 82
82 // Whether the browser action is visible in the toolbar. 83 // Whether the browser action is visible in the toolbar.
83 const char kBrowserActionVisible[] = "browser_action_visible"; 84 const char kBrowserActionVisible[] = "browser_action_visible";
84 85
86 // Preferences that hold the permissions the user has granted the extension.
87 // We explicitly keep track of these so that extensions can contain unknown
88 // permissions, for backwards compatibility reasons, and we can still prompt
89 // the user to accept them once recognized.
90 const char kPrefGrantedPermissionsAPI[] = "granted_permissions.api";
91 const char kPrefGrantedPermissionsHost[] = "granted_permissions.host";
92 const char kPrefGrantedPermissionsInitialized[] =
93 "granted_permissions.initialized";
94
85 } // namespace 95 } // namespace
86 96
87 //////////////////////////////////////////////////////////////////////////////// 97 ////////////////////////////////////////////////////////////////////////////////
88 98
89 namespace { 99 namespace {
90 100
91 // TODO(asargent) - This is cleanup code for a key that was introduced into 101 // TODO(asargent) - This is cleanup code for a key that was introduced into
92 // the extensions.settings sub-dictionary which wasn't a valid extension 102 // the extensions.settings sub-dictionary which wasn't a valid extension
93 // id. We can remove this in a couple of months. (See http://crbug.com/40017 103 // id. We can remove this in a couple of months. (See http://crbug.com/40017
94 // and http://crbug.com/39745 for more details). 104 // and http://crbug.com/39745 for more details).
(...skipping 11 matching lines...) Expand all
106 bool dirty = false; 116 bool dirty = false;
107 for (std::set<std::string>::iterator i = bad_keys.begin(); 117 for (std::set<std::string>::iterator i = bad_keys.begin();
108 i != bad_keys.end(); ++i) { 118 i != bad_keys.end(); ++i) {
109 dirty = true; 119 dirty = true;
110 dictionary->Remove(*i, NULL); 120 dictionary->Remove(*i, NULL);
111 } 121 }
112 if (dirty) 122 if (dirty)
113 prefs->ScheduleSavePersistentPrefs(); 123 prefs->ScheduleSavePersistentPrefs();
114 } 124 }
115 125
126 static void ExtentToStringSet(const ExtensionExtent& host_extent,
127 std::set<std::string>* result) {
128 ExtensionExtent::PatternList patterns = host_extent.patterns();
129 ExtensionExtent::PatternList::const_iterator i;
130
131 for (i = patterns.begin(); i != patterns.end(); ++i)
132 result->insert(i->GetAsString());
133 }
134
116 } // namespace 135 } // namespace
117 136
118 ExtensionPrefs::ExtensionPrefs(PrefService* prefs, const FilePath& root_dir) 137 ExtensionPrefs::ExtensionPrefs(PrefService* prefs, const FilePath& root_dir)
119 : prefs_(prefs), 138 : prefs_(prefs),
120 install_directory_(root_dir) { 139 install_directory_(root_dir) {
121 // TODO(asargent) - Remove this in a couple of months. (See comment above 140 // TODO(asargent) - Remove this in a couple of months. (See comment above
122 // CleanupBadExtensionKeys). 141 // CleanupBadExtensionKeys).
123 CleanupBadExtensionKeys(prefs); 142 CleanupBadExtensionKeys(prefs);
124 143
125 MakePathsRelative(); 144 MakePathsRelative();
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 DictionaryValue* copy = 230 DictionaryValue* copy =
212 static_cast<DictionaryValue*>(extensions->DeepCopy()); 231 static_cast<DictionaryValue*>(extensions->DeepCopy());
213 MakePathsAbsolute(copy); 232 MakePathsAbsolute(copy);
214 return copy; 233 return copy;
215 } 234 }
216 return new DictionaryValue; 235 return new DictionaryValue;
217 } 236 }
218 237
219 bool ExtensionPrefs::ReadBooleanFromPref( 238 bool ExtensionPrefs::ReadBooleanFromPref(
220 DictionaryValue* ext, const std::string& pref_key) { 239 DictionaryValue* ext, const std::string& pref_key) {
221 if (!ext->HasKey(pref_key)) return false;
222 bool bool_value = false; 240 bool bool_value = false;
223 if (!ext->GetBoolean(pref_key, &bool_value)) { 241 if (!ext->GetBoolean(pref_key, &bool_value))
224 NOTREACHED() << "Failed to fetch " << pref_key << " flag.";
225 // In case we could not fetch the flag, we treat it as false.
226 return false; 242 return false;
227 } 243
228 return bool_value; 244 return bool_value;
229 } 245 }
230 246
231 bool ExtensionPrefs::ReadExtensionPrefBoolean( 247 bool ExtensionPrefs::ReadExtensionPrefBoolean(
232 const std::string& extension_id, const std::string& pref_key) { 248 const std::string& extension_id, const std::string& pref_key) {
233 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); 249 DictionaryValue* ext = GetExtensionPref(extension_id);
234 if (!extensions) 250 if (!ext) {
235 return false;
236
237 DictionaryValue* ext = NULL;
238 if (!extensions->GetDictionary(extension_id, &ext)) {
239 // No such extension yet. 251 // No such extension yet.
240 return false; 252 return false;
241 } 253 }
242 return ReadBooleanFromPref(ext, pref_key); 254 return ReadBooleanFromPref(ext, pref_key);
243 } 255 }
244 256
245 bool ExtensionPrefs::ReadIntegerFromPref( 257 bool ExtensionPrefs::ReadIntegerFromPref(
246 DictionaryValue* ext, const std::string& pref_key, int* out_value) { 258 DictionaryValue* ext, const std::string& pref_key, int* out_value) {
247 if (!ext->HasKey(pref_key)) return false; 259 if (!ext->GetInteger(pref_key, out_value))
248 if (!ext->GetInteger(pref_key, out_value)) {
249 NOTREACHED() << "Failed to fetch " << pref_key << " flag.";
250 // In case we could not fetch the flag, we treat it as false.
251 return false; 260 return false;
252 } 261
253 return out_value != NULL; 262 return out_value != NULL;
254 } 263 }
255 264
256 bool ExtensionPrefs::ReadExtensionPrefInteger( 265 bool ExtensionPrefs::ReadExtensionPrefInteger(
257 const std::string& extension_id, const std::string& pref_key, 266 const std::string& extension_id, const std::string& pref_key,
258 int* out_value) { 267 int* out_value) {
259 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); 268 DictionaryValue* ext = GetExtensionPref(extension_id);
260 if (!extensions) 269 if (!ext) {
261 return false;
262 DictionaryValue* ext = NULL;
263 if (!extensions->GetDictionary(extension_id, &ext)) {
264 // No such extension yet. 270 // No such extension yet.
265 return false; 271 return false;
266 } 272 }
267 return ReadIntegerFromPref(ext, pref_key, out_value); 273 return ReadIntegerFromPref(ext, pref_key, out_value);
268 } 274 }
269 275
276 bool ExtensionPrefs::ReadExtensionPrefList(
277 const std::string& extension_id, const std::string& pref_key,
278 ListValue** out_value) {
279 DictionaryValue* ext = GetExtensionPref(extension_id);
280 if (!ext || !ext->GetList(pref_key, out_value))
281 return false;
282
283 return out_value != NULL;
284 }
285
286 bool ExtensionPrefs::ReadExtensionPrefStringSet(
287 const std::string& extension_id,
288 const std::string& pref_key,
289 std::set<std::string>* result) {
290 ListValue* value = NULL;
291 if (!ReadExtensionPrefList(extension_id, pref_key, &value))
292 return false;
293
294 result->clear();
295
296 for (size_t i = 0; i < value->GetSize(); ++i) {
297 std::string item;
298 if (!value->GetString(i, &item))
299 return false;
300 result->insert(item);
301 }
302
303 return true;
304 }
305
306 void ExtensionPrefs::AddToExtensionPrefStringSet(
307 const std::string& extension_id,
308 const std::string& pref_key,
309 const std::set<std::string>& added_value) {
310 std::set<std::string> old_value;
311 std::set<std::string> new_value;
312 ReadExtensionPrefStringSet(extension_id, pref_key, &old_value);
313
314 std::set_union(old_value.begin(), old_value.end(),
315 added_value.begin(), added_value.end(),
316 std::inserter(new_value, new_value.end()));
317
318 ListValue* value = new ListValue();
319 for (std::set<std::string>::const_iterator iter = new_value.begin();
320 iter != new_value.end(); ++iter)
321 value->Append(Value::CreateStringValue(*iter));
322
323 UpdateExtensionPref(extension_id, pref_key, value);
324 prefs_->ScheduleSavePersistentPrefs();
325 }
326
270 void ExtensionPrefs::SavePrefsAndNotify() { 327 void ExtensionPrefs::SavePrefsAndNotify() {
271 prefs_->ScheduleSavePersistentPrefs(); 328 prefs_->ScheduleSavePersistentPrefs();
272 prefs_->pref_notifier()->OnUserPreferenceSet(kExtensionsPref); 329 prefs_->pref_notifier()->OnUserPreferenceSet(kExtensionsPref);
273 } 330 }
274 331
275 bool ExtensionPrefs::IsBlacklistBitSet(DictionaryValue* ext) { 332 bool ExtensionPrefs::IsBlacklistBitSet(DictionaryValue* ext) {
276 return ReadBooleanFromPref(ext, kPrefBlacklist); 333 return ReadBooleanFromPref(ext, kPrefBlacklist);
277 } 334 }
278 335
279 bool ExtensionPrefs::IsExtensionBlacklisted(const std::string& extension_id) { 336 bool ExtensionPrefs::IsExtensionBlacklisted(const std::string& extension_id) {
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 DictionaryValue* dictionary) { 461 DictionaryValue* dictionary) {
405 if (!dictionary) { 462 if (!dictionary) {
406 NOTREACHED(); 463 NOTREACHED();
407 return; 464 return;
408 } 465 }
409 std::string value = base::Int64ToString(time.ToInternalValue()); 466 std::string value = base::Int64ToString(time.ToInternalValue());
410 dictionary->SetString(kLastPingDay, value); 467 dictionary->SetString(kLastPingDay, value);
411 SavePrefsAndNotify(); 468 SavePrefsAndNotify();
412 } 469 }
413 470
471
472 bool ExtensionPrefs::GetGrantedPermissions(
473 const std::string& extension_id,
474 std::set<std::string>* api_permissions,
475 ExtensionExtent* host_extent) {
476 CHECK(Extension::IdIsValid(extension_id));
477
478 if (!ReadExtensionPrefBoolean(extension_id,
479 kPrefGrantedPermissionsInitialized))
480 return false;
481
482 ReadExtensionPrefStringSet(
483 extension_id, kPrefGrantedPermissionsAPI, api_permissions);
484
485 std::set<std::string> host_permissions;
486 ReadExtensionPrefStringSet(
487 extension_id, kPrefGrantedPermissionsHost, &host_permissions);
488
489 for (std::set<std::string>::iterator i = host_permissions.begin();
490 i != host_permissions.end(); ++i)
491 host_extent->AddPattern(URLPattern(
492 Extension::kValidWebExtentSchemes | UserScript::kValidUserScriptSchemes,
493 *i));
494
495 return true;
496 }
497
498 void ExtensionPrefs::AddGrantedPermissions(
499 const std::string& extension_id,
500 const std::set<std::string>& api_permissions,
501 const ExtensionExtent& host_extent) {
502 CHECK(Extension::IdIsValid(extension_id));
503
504 UpdateExtensionPref(extension_id, kPrefGrantedPermissionsInitialized,
505 Value::CreateBooleanValue(true));
506
507 if (!api_permissions.empty()) {
508 AddToExtensionPrefStringSet(
509 extension_id, kPrefGrantedPermissionsAPI, api_permissions);
510 }
511
512 if (!host_extent.is_empty()) {
513 std::set<std::string> host_permissions;
514 ExtentToStringSet(host_extent, &host_permissions);
515
516 AddToExtensionPrefStringSet(
517 extension_id, kPrefGrantedPermissionsHost, host_permissions);
518 }
519
520 SavePrefsAndNotify();
521 }
522
414 Time ExtensionPrefs::LastPingDay(const std::string& extension_id) const { 523 Time ExtensionPrefs::LastPingDay(const std::string& extension_id) const {
415 DCHECK(Extension::IdIsValid(extension_id)); 524 DCHECK(Extension::IdIsValid(extension_id));
416 return LastPingDayImpl(GetExtensionPref(extension_id)); 525 return LastPingDayImpl(GetExtensionPref(extension_id));
417 } 526 }
418 527
419 Time ExtensionPrefs::BlacklistLastPingDay() const { 528 Time ExtensionPrefs::BlacklistLastPingDay() const {
420 return LastPingDayImpl(prefs_->GetDictionary(kExtensionsBlacklistUpdate)); 529 return LastPingDayImpl(prefs_->GetDictionary(kExtensionsBlacklistUpdate));
421 } 530 }
422 531
423 void ExtensionPrefs::SetLastPingDay(const std::string& extension_id, 532 void ExtensionPrefs::SetLastPingDay(const std::string& extension_id,
(...skipping 538 matching lines...) Expand 10 before | Expand all | Expand 10 after
962 void ExtensionPrefs::RegisterUserPrefs(PrefService* prefs) { 1071 void ExtensionPrefs::RegisterUserPrefs(PrefService* prefs) {
963 prefs->RegisterDictionaryPref(kExtensionsPref); 1072 prefs->RegisterDictionaryPref(kExtensionsPref);
964 prefs->RegisterListPref(kExtensionToolbar); 1073 prefs->RegisterListPref(kExtensionToolbar);
965 prefs->RegisterIntegerPref(prefs::kExtensionToolbarSize, -1); 1074 prefs->RegisterIntegerPref(prefs::kExtensionToolbarSize, -1);
966 prefs->RegisterDictionaryPref(kExtensionsBlacklistUpdate); 1075 prefs->RegisterDictionaryPref(kExtensionsBlacklistUpdate);
967 prefs->RegisterListPref(prefs::kExtensionInstallAllowList); 1076 prefs->RegisterListPref(prefs::kExtensionInstallAllowList);
968 prefs->RegisterListPref(prefs::kExtensionInstallDenyList); 1077 prefs->RegisterListPref(prefs::kExtensionInstallDenyList);
969 prefs->RegisterListPref(prefs::kExtensionInstallForceList); 1078 prefs->RegisterListPref(prefs::kExtensionInstallForceList);
970 prefs->RegisterStringPref(kWebStoreLogin, std::string() /* default_value */); 1079 prefs->RegisterStringPref(kWebStoreLogin, std::string() /* default_value */);
971 } 1080 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698