Chromium Code Reviews| 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..f12555fdcd35cd9159b2aa9b19384371c1ca60b2 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/extension_preference_api.cc |
| @@ -0,0 +1,87 @@ |
| +// 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/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; |
| +}; |
| + |
| +PrefMappingEntry pref_mapping[] = { |
| + {"blockThirdPartyCookies", prefs::kBlockThirdPartyCookies}, |
| +}; |
| + |
| +const char* BrowserPrefForExtensionPref(const std::string& extension_pref) { |
| + for (size_t i = 0; i < arraysize(pref_mapping); ++i) { |
| + if (extension_pref == pref_mapping[i].extension_pref) |
| + return pref_mapping[i].browser_pref; |
| + } |
| + return NULL; |
| +} |
| + |
| +} // namespace |
| + |
| +GetPreferenceFunction::~GetPreferenceFunction() { } |
| + |
| +bool GetPreferenceFunction::RunImpl() { |
| + std::string pref_key; |
| + EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
| + DictionaryValue* details; |
| + 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)); |
|
battre
2011/02/11 13:49:02
maybe
if (details->HasKey("incognito"))
EXTENSIO
|
| + |
| + PrefService* prefs = incognito ? profile_->GetOffTheRecordPrefs() |
| + : profile_->GetPrefs(); |
| + const char* browser_pref = BrowserPrefForExtensionPref(pref_key); |
| + if (!browser_pref) { |
| + NOTREACHED(); |
| + return false; |
| + } |
| + const PrefService::Preference* pref = prefs->FindPreference(browser_pref); |
| + if (!pref) { |
| + NOTREACHED(); |
| + return false; |
| + } |
| + result_.reset(pref->GetValue()->DeepCopy()); |
| + 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)); |
|
battre
2011/02/11 13:49:02
make value a full parameter of the function? That
Bernhard Bauer
2011/02/11 15:27:15
Hm. I tried to keep as close to the original spec
|
| + |
| + Value* incognito_value; |
| + bool incognito = false; |
| + if (details->Get("incognito", &incognito_value)) |
| + EXTENSION_FUNCTION_VALIDATE(incognito_value->GetAsBoolean(&incognito)); |
|
battre
2011/02/11 13:49:02
see above
|
| + |
| + ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); |
| + const char* browser_pref = BrowserPrefForExtensionPref(pref_key); |
| + if (!browser_pref) |
|
battre
2011/02/11 13:49:02
insert NOTREACHED(); as above?
Bernhard Bauer
2011/02/11 15:27:15
I've rewritten the checking code quite a bit. It s
|
| + return false; |
| + prefs->SetExtensionControlledPref(extension_id(), |
| + browser_pref, |
| + incognito, |
| + value->DeepCopy()); |
| + return true; |
| +} |