Index: chrome/browser/extensions/extension_preference_api.cc |
diff --git a/chrome/browser/extensions/extension_preference_api.cc b/chrome/browser/extensions/extension_preference_api.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b5a571c3b4233d16e4d446345daaf1c112d4327a |
--- /dev/null |
+++ b/chrome/browser/extensions/extension_preference_api.cc |
@@ -0,0 +1,112 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/extensions/extension_preference_api.h" |
+ |
+#include "base/stringprintf.h" |
+#include "base/values.h" |
+#include "chrome/browser/extensions/extension_prefs.h" |
+#include "chrome/browser/extensions/extension_service.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "chrome/common/pref_names.h" |
+ |
+namespace { |
+ |
+struct PrefMappingEntry { |
+ const char* extension_pref; |
+ const char* browser_pref; |
+ const char* permission; |
+}; |
+ |
+PrefMappingEntry pref_mapping[] = { |
+ { "blockThirdPartyCookies", |
+ prefs::kBlockThirdPartyCookies, |
+ Extension::kContentSettingsPermission |
Aaron Boodman
2011/02/11 18:33:19
Can you give an example of a pref that would have
Bernhard Bauer
2011/02/13 18:19:12
Proxy settings, for example. We have to do the per
Aaron Boodman
2011/02/14 02:54:02
I'm sorry I don't understand. Permission checking
|
+ }, |
+}; |
+ |
+bool FindBrowserPrefForExtensionPref(const std::string& extension_pref, |
+ std::string* browser_pref, |
+ std::string* permission) { |
+ for (size_t i = 0; i < arraysize(pref_mapping); ++i) { |
Aaron Boodman
2011/02/11 18:33:19
Suggest starting out with this in a map. Even thou
Bernhard Bauer
2011/02/13 18:19:12
Okay, done.
|
+ if (extension_pref == pref_mapping[i].extension_pref) { |
+ *browser_pref = pref_mapping[i].browser_pref; |
+ *permission = pref_mapping[i].permission; |
+ return true; |
+ } |
+ } |
+ return false; |
+} |
+ |
+const char kInvalidPreferenceErrorMessage[] = "Invalid preference '%s'."; |
Aaron Boodman
2011/02/11 18:33:19
Not used?
Bernhard Bauer
2011/02/13 18:19:12
Right, leftover from a previous version.
|
+const char kPermissionErrorMessage[] = |
+ "You do not have permission to access the preference '%s'. " |
+ "Be sure to declare in your manifest what permissions you need."; |
+ |
+} // namespace |
+ |
+GetPreferenceFunction::~GetPreferenceFunction() { } |
+ |
+bool GetPreferenceFunction::RunImpl() { |
+ std::string pref_key; |
+ EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
+ DictionaryValue* details; |
Aaron Boodman
2011/02/11 18:33:19
Nit: initialize primitives. Multiple places in thi
Bernhard Bauer
2011/02/13 18:19:12
Done.
|
+ EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); |
+ Value* incognito_value; |
+ bool incognito = false; |
+ if (details->Get("incognito", &incognito_value)) |
+ EXTENSION_FUNCTION_VALIDATE(incognito_value->GetAsBoolean(&incognito)); |
Aaron Boodman
2011/02/11 18:33:19
Nit: I think this is more clear with:
if (details
Bernhard Bauer
2011/02/13 18:19:12
Done.
|
+ |
+ PrefService* prefs = incognito ? profile_->GetOffTheRecordPrefs() |
+ : profile_->GetPrefs(); |
Aaron Boodman
2011/02/11 18:33:19
This seems a bit weird. How do these prefs work in
Bernhard Bauer
2011/02/13 18:19:12
We already have a separate thread going with mpcom
Aaron Boodman
2011/02/14 02:54:02
Should we only allow this if the extension is enab
|
+ std::string browser_pref; |
+ std::string permission; |
+ EXTENSION_FUNCTION_VALIDATE( |
+ FindBrowserPrefForExtensionPref(pref_key, &browser_pref, &permission)); |
+ if (!GetExtension()->HasApiPermission(permission)) { |
+ error_ = base::StringPrintf(kPermissionErrorMessage, pref_key.c_str()); |
+ return false; |
+ } |
+ const PrefService::Preference* pref = |
+ prefs->FindPreference(browser_pref.c_str()); |
+ DCHECK(pref); |
Aaron Boodman
2011/02/11 18:33:19
Don't be afraid. If this really can't happen, use
Bernhard Bauer
2011/02/13 18:19:12
I thought if it really can't happen, we should use
Aaron Boodman
2011/02/14 02:54:02
This is my personal preference, not Chrome policy.
|
+ result_.reset(pref->GetValue()->DeepCopy()); |
Aaron Boodman
2011/02/11 18:33:19
Is GetValue() always a fundamental Value? Maybe yo
Bernhard Bauer
2011/02/13 18:19:12
Does it have to be? In theory, we could return an
Aaron Boodman
2011/02/14 02:54:02
I suppose. Ok, n/m.
|
+ return true; |
+} |
+ |
+SetPreferenceFunction::~SetPreferenceFunction() { } |
+ |
+bool SetPreferenceFunction::RunImpl() { |
+ std::string pref_key; |
+ EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
+ DictionaryValue* details; |
+ EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); |
+ |
+ Value* value; |
+ EXTENSION_FUNCTION_VALIDATE(details->Get("value", &value)); |
+ |
+ Value* incognito_value; |
+ bool incognito = false; |
+ if (details->Get("incognito", &incognito_value)) |
+ EXTENSION_FUNCTION_VALIDATE(incognito_value->GetAsBoolean(&incognito)); |
+ |
+ std::string browser_pref; |
+ std::string permission; |
+ EXTENSION_FUNCTION_VALIDATE( |
+ FindBrowserPrefForExtensionPref(pref_key, &browser_pref, &permission)); |
+ if (!GetExtension()->HasApiPermission(permission)) { |
+ error_ = base::StringPrintf(kPermissionErrorMessage, pref_key.c_str()); |
+ return false; |
+ } |
+ ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); |
+ const PrefService::Preference* pref = |
+ prefs->FindPreference(browser_pref.c_str()); |
+ DCHECK(pref); |
+ EXTENSION_FUNCTION_VALIDATE(value->GetType == pref->GetType()); |
Aaron Boodman
2011/02/11 18:33:19
Syntax error? GetType() ?
Bernhard Bauer
2011/02/13 18:19:12
Woah. Fixed.
|
+ prefs->SetExtensionControlledPref(extension_id(), |
+ browser_pref, |
+ incognito, |
+ value->DeepCopy()); |
+ return true; |
+} |