OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_preference_api.h" | 5 #include "chrome/browser/extensions/extension_preference_api.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
10 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
11 #include "base/stl_util-inl.h" | 11 #include "base/stl_util-inl.h" |
12 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "chrome/browser/extensions/extension_event_router.h" | 14 #include "chrome/browser/extensions/extension_event_router.h" |
15 #include "chrome/browser/extensions/extension_prefs.h" | 15 #include "chrome/browser/extensions/extension_prefs.h" |
16 #include "chrome/browser/extensions/extension_prefs_scope.h" | |
16 #include "chrome/browser/extensions/extension_proxy_api.h" | 17 #include "chrome/browser/extensions/extension_proxy_api.h" |
17 #include "chrome/browser/extensions/extension_service.h" | 18 #include "chrome/browser/extensions/extension_service.h" |
18 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
19 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
20 #include "content/common/notification_type.h" | 21 #include "content/common/notification_type.h" |
21 #include "content/common/notification_service.h" | 22 #include "content/common/notification_service.h" |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
25 struct PrefMappingEntry { | 26 struct PrefMappingEntry { |
26 const char* extension_pref; | 27 const char* extension_pref; |
27 const char* browser_pref; | 28 const char* browser_pref; |
28 const char* permission; | 29 const char* permission; |
29 }; | 30 }; |
30 | 31 |
31 const char kNotControllable[] = "NotControllable"; | 32 const char kNotControllable[] = "NotControllable"; |
32 const char kControlledByOtherExtensions[] = "ControlledByOtherExtensions"; | 33 const char kControlledByOtherExtensions[] = "ControlledByOtherExtensions"; |
33 const char kControllableByThisExtension[] = "ControllableByThisExtension"; | 34 const char kControllableByThisExtension[] = "ControllableByThisExtension"; |
34 const char kControlledByThisExtension[] = "ControlledByThisExtension"; | 35 const char kControlledByThisExtension[] = "ControlledByThisExtension"; |
35 | 36 |
36 const char kIncognito[] = "incognito"; | 37 const char kIncognito[] = "incognito"; |
37 const char kIncognitoSpecific[] = "incognitoSpecific"; | 38 const char kIncognitoSpecific[] = "incognitoSpecific"; |
39 const char kScope[] = "scope"; | |
38 const char kLevelOfControl[] = "levelOfControl"; | 40 const char kLevelOfControl[] = "levelOfControl"; |
41 const char kRegular[] = "regular"; | |
42 const char kIncognitoPersistent[] = "incognito_persistent"; | |
39 const char kValue[] = "value"; | 43 const char kValue[] = "value"; |
40 | 44 |
41 const char kOnPrefChangeFormat[] = | 45 const char kOnPrefChangeFormat[] = |
42 "preferences.%s.onChange"; | 46 "preferences.%s.onChange"; |
43 | 47 |
44 const char kIncognitoErrorMessage[] = | 48 const char kIncognitoErrorMessage[] = |
45 "You do not have permission to access incognito preferences."; | 49 "You do not have permission to access incognito preferences."; |
46 | 50 |
47 const char kPermissionErrorMessage[] = | 51 const char kPermissionErrorMessage[] = |
48 "You do not have permission to access the preference '%s'. " | 52 "You do not have permission to access the preference '%s'. " |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
102 | 106 |
103 if (ep->DoesExtensionControlPref(extension_id, browser_pref, incognito)) | 107 if (ep->DoesExtensionControlPref(extension_id, browser_pref, incognito)) |
104 return kControlledByThisExtension; | 108 return kControlledByThisExtension; |
105 | 109 |
106 if (ep->CanExtensionControlPref(extension_id, browser_pref, incognito)) | 110 if (ep->CanExtensionControlPref(extension_id, browser_pref, incognito)) |
107 return kControllableByThisExtension; | 111 return kControllableByThisExtension; |
108 | 112 |
109 return kControlledByOtherExtensions; | 113 return kControlledByOtherExtensions; |
110 } | 114 } |
111 | 115 |
116 bool StringToScope(const std::string& s, extension_prefs_scope::Scope* scope) { | |
117 if (s == kRegular) { | |
118 *scope = extension_prefs_scope::kRegular; | |
119 } else if (s == kIncognitoPersistent) { | |
120 *scope = extension_prefs_scope::kIncognitoPersistent; | |
121 } else { | |
122 NOTREACHED(); | |
Matt Perry
2011/05/25 21:13:08
remove the NOTREACHED. VALIDATE is enough
battre
2011/05/25 22:07:47
Done.
| |
123 return false; | |
124 } | |
125 return true; | |
126 } | |
127 | |
112 class PrefMapping { | 128 class PrefMapping { |
113 public: | 129 public: |
114 static PrefMapping* GetInstance() { | 130 static PrefMapping* GetInstance() { |
115 return Singleton<PrefMapping>::get(); | 131 return Singleton<PrefMapping>::get(); |
116 } | 132 } |
117 | 133 |
118 bool FindBrowserPrefForExtensionPref(const std::string& extension_pref, | 134 bool FindBrowserPrefForExtensionPref(const std::string& extension_pref, |
119 std::string* browser_pref, | 135 std::string* browser_pref, |
120 std::string* permission) { | 136 std::string* permission) { |
121 std::map<std::string, std::pair<std::string, std::string> >::iterator it = | 137 std::map<std::string, std::pair<std::string, std::string> >::iterator it = |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
339 | 355 |
340 bool SetPreferenceFunction::RunImpl() { | 356 bool SetPreferenceFunction::RunImpl() { |
341 std::string pref_key; | 357 std::string pref_key; |
342 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); | 358 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
343 DictionaryValue* details = NULL; | 359 DictionaryValue* details = NULL; |
344 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); | 360 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); |
345 | 361 |
346 Value* value = NULL; | 362 Value* value = NULL; |
347 EXTENSION_FUNCTION_VALIDATE(details->Get(kValue, &value)); | 363 EXTENSION_FUNCTION_VALIDATE(details->Get(kValue, &value)); |
348 | 364 |
349 bool incognito = false; | 365 std::string scope_str = kRegular; |
366 if (details->HasKey(kScope)) | |
367 EXTENSION_FUNCTION_VALIDATE(details->GetString(kScope, &scope_str)); | |
350 | 368 |
351 // TODO(battre): enable incognito preferences again. | 369 extension_prefs_scope::Scope scope; |
352 // if (details->HasKey(kIncognito)) | 370 EXTENSION_FUNCTION_VALIDATE(StringToScope(scope_str, &scope)); |
353 // EXTENSION_FUNCTION_VALIDATE(details->GetBoolean(kIncognito, &incognito)); | |
354 | 371 |
355 if (incognito && !include_incognito()) { | 372 // TODO(battre): add kIncognitoSessionOnly |
356 error_ = kIncognitoErrorMessage; | 373 bool incognito = (scope == extension_prefs_scope::kIncognitoPersistent); |
357 return false; | 374 if (incognito) { |
375 // regular profiles can't access incognito unless | |
376 // include_incognito is true. | |
377 if (!profile()->IsOffTheRecord() && !include_incognito()) { | |
378 error_ = kIncognitoErrorMessage; | |
379 return false; | |
380 } | |
381 } else { | |
382 // incognito profiles can't access regular mode ever. | |
383 // (they only exist in split mode). | |
384 if (profile()->IsOffTheRecord()) { | |
385 error_ = "Incognito extension can't access regular settings."; | |
Matt Perry
2011/05/25 21:13:08
Fix the comments (full sentences), make this error
battre
2011/05/25 22:07:47
Done.
| |
386 return false; | |
387 } | |
358 } | 388 } |
359 | 389 |
360 std::string browser_pref; | 390 std::string browser_pref; |
361 std::string permission; | 391 std::string permission; |
362 EXTENSION_FUNCTION_VALIDATE( | 392 EXTENSION_FUNCTION_VALIDATE( |
363 PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( | 393 PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( |
364 pref_key, &browser_pref, &permission)); | 394 pref_key, &browser_pref, &permission)); |
365 if (!GetExtension()->HasApiPermission(permission)) { | 395 if (!GetExtension()->HasApiPermission(permission)) { |
366 error_ = base::StringPrintf(kPermissionErrorMessage, pref_key.c_str()); | 396 error_ = base::StringPrintf(kPermissionErrorMessage, pref_key.c_str()); |
367 return false; | 397 return false; |
368 } | 398 } |
369 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); | 399 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); |
370 const PrefService::Preference* pref = | 400 const PrefService::Preference* pref = |
371 prefs->pref_service()->FindPreference(browser_pref.c_str()); | 401 prefs->pref_service()->FindPreference(browser_pref.c_str()); |
372 CHECK(pref); | 402 CHECK(pref); |
373 EXTENSION_FUNCTION_VALIDATE(value->GetType() == pref->GetType()); | 403 EXTENSION_FUNCTION_VALIDATE(value->GetType() == pref->GetType()); |
374 PrefTransformerInterface* transformer = | 404 PrefTransformerInterface* transformer = |
375 PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); | 405 PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); |
376 std::string error; | 406 std::string error; |
377 bool bad_message = false; | 407 bool bad_message = false; |
378 Value* browserPrefValue = | 408 Value* browserPrefValue = |
379 transformer->ExtensionToBrowserPref(value, &error, &bad_message); | 409 transformer->ExtensionToBrowserPref(value, &error, &bad_message); |
380 if (!browserPrefValue) { | 410 if (!browserPrefValue) { |
381 error_ = error; | 411 error_ = error; |
382 bad_message_ = bad_message; | 412 bad_message_ = bad_message; |
383 return false; | 413 return false; |
384 } | 414 } |
385 prefs->SetExtensionControlledPref(extension_id(), | 415 prefs->SetExtensionControlledPref(extension_id(), |
386 browser_pref, | 416 browser_pref, |
387 incognito, | 417 scope, |
388 browserPrefValue); | 418 browserPrefValue); |
389 return true; | 419 return true; |
390 } | 420 } |
391 | 421 |
392 ClearPreferenceFunction::~ClearPreferenceFunction() { } | 422 ClearPreferenceFunction::~ClearPreferenceFunction() { } |
393 | 423 |
394 bool ClearPreferenceFunction::RunImpl() { | 424 bool ClearPreferenceFunction::RunImpl() { |
395 std::string pref_key; | 425 std::string pref_key; |
396 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); | 426 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
397 DictionaryValue* details = NULL; | 427 DictionaryValue* details = NULL; |
398 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); | 428 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); |
399 | 429 |
400 bool incognito = false; | 430 std::string scope_str = kRegular; |
431 if (details->HasKey(kScope)) | |
432 EXTENSION_FUNCTION_VALIDATE(details->GetString(kScope, &scope_str)); | |
401 | 433 |
402 // TODO(battre): enable incognito preferences again. | 434 extension_prefs_scope::Scope scope; |
403 // if (details->HasKey(kIncognito)) | 435 EXTENSION_FUNCTION_VALIDATE(StringToScope(scope_str, &scope)); |
404 // EXTENSION_FUNCTION_VALIDATE(details->GetBoolean(kIncognito, &incognito)); | |
405 | 436 |
406 // We don't check incognito permissions here, as an extension should be always | 437 // TODO(battre): add kIncognitoSessionOnly |
407 // allowed to clear its own settings. | 438 bool incognito = (scope == extension_prefs_scope::kIncognitoPersistent); |
439 if (incognito) { | |
440 // We don't check incognito permissions here, as an extension should be alwa ys | |
Matt Perry
2011/05/25 21:13:08
line length
battre
2011/05/25 22:07:47
Done.
| |
441 // allowed to clear its own settings. | |
442 } else { | |
443 // incognito profiles can't access regular mode ever. | |
444 // (they only exist in split mode). | |
445 if (profile()->IsOffTheRecord()) { | |
446 error_ = "Incognito extension can't access regular settings."; | |
447 return false; | |
448 } | |
449 } | |
408 | 450 |
409 std::string browser_pref; | 451 std::string browser_pref; |
410 std::string permission; | 452 std::string permission; |
411 EXTENSION_FUNCTION_VALIDATE( | 453 EXTENSION_FUNCTION_VALIDATE( |
412 PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( | 454 PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( |
413 pref_key, &browser_pref, &permission)); | 455 pref_key, &browser_pref, &permission)); |
414 if (!GetExtension()->HasApiPermission(permission)) { | 456 if (!GetExtension()->HasApiPermission(permission)) { |
415 error_ = base::StringPrintf(kPermissionErrorMessage, pref_key.c_str()); | 457 error_ = base::StringPrintf(kPermissionErrorMessage, pref_key.c_str()); |
416 return false; | 458 return false; |
417 } | 459 } |
418 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); | 460 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); |
419 prefs->RemoveExtensionControlledPref(extension_id(), browser_pref, incognito); | 461 prefs->RemoveExtensionControlledPref(extension_id(), browser_pref, scope); |
420 return true; | 462 return true; |
421 } | 463 } |
OLD | NEW |