OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/extensions/extension_preference_api.h" | |
6 | |
7 #include "base/stringprintf.h" | |
8 #include "base/values.h" | |
9 #include "chrome/browser/extensions/extension_prefs.h" | |
10 #include "chrome/browser/extensions/extension_service.h" | |
11 #include "chrome/browser/profiles/profile.h" | |
12 #include "chrome/common/pref_names.h" | |
13 | |
14 namespace { | |
15 | |
16 struct PrefMappingEntry { | |
17 const char* extension_pref; | |
18 const char* browser_pref; | |
19 const char* permission; | |
20 }; | |
21 | |
22 PrefMappingEntry pref_mapping[] = { | |
23 { "blockThirdPartyCookies", | |
24 prefs::kBlockThirdPartyCookies, | |
25 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
| |
26 }, | |
27 }; | |
28 | |
29 bool FindBrowserPrefForExtensionPref(const std::string& extension_pref, | |
30 std::string* browser_pref, | |
31 std::string* permission) { | |
32 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.
| |
33 if (extension_pref == pref_mapping[i].extension_pref) { | |
34 *browser_pref = pref_mapping[i].browser_pref; | |
35 *permission = pref_mapping[i].permission; | |
36 return true; | |
37 } | |
38 } | |
39 return false; | |
40 } | |
41 | |
42 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.
| |
43 const char kPermissionErrorMessage[] = | |
44 "You do not have permission to access the preference '%s'. " | |
45 "Be sure to declare in your manifest what permissions you need."; | |
46 | |
47 } // namespace | |
48 | |
49 GetPreferenceFunction::~GetPreferenceFunction() { } | |
50 | |
51 bool GetPreferenceFunction::RunImpl() { | |
52 std::string pref_key; | |
53 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); | |
54 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.
| |
55 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); | |
56 Value* incognito_value; | |
57 bool incognito = false; | |
58 if (details->Get("incognito", &incognito_value)) | |
59 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.
| |
60 | |
61 PrefService* prefs = incognito ? profile_->GetOffTheRecordPrefs() | |
62 : 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
| |
63 std::string browser_pref; | |
64 std::string permission; | |
65 EXTENSION_FUNCTION_VALIDATE( | |
66 FindBrowserPrefForExtensionPref(pref_key, &browser_pref, &permission)); | |
67 if (!GetExtension()->HasApiPermission(permission)) { | |
68 error_ = base::StringPrintf(kPermissionErrorMessage, pref_key.c_str()); | |
69 return false; | |
70 } | |
71 const PrefService::Preference* pref = | |
72 prefs->FindPreference(browser_pref.c_str()); | |
73 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.
| |
74 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.
| |
75 return true; | |
76 } | |
77 | |
78 SetPreferenceFunction::~SetPreferenceFunction() { } | |
79 | |
80 bool SetPreferenceFunction::RunImpl() { | |
81 std::string pref_key; | |
82 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); | |
83 DictionaryValue* details; | |
84 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); | |
85 | |
86 Value* value; | |
87 EXTENSION_FUNCTION_VALIDATE(details->Get("value", &value)); | |
88 | |
89 Value* incognito_value; | |
90 bool incognito = false; | |
91 if (details->Get("incognito", &incognito_value)) | |
92 EXTENSION_FUNCTION_VALIDATE(incognito_value->GetAsBoolean(&incognito)); | |
93 | |
94 std::string browser_pref; | |
95 std::string permission; | |
96 EXTENSION_FUNCTION_VALIDATE( | |
97 FindBrowserPrefForExtensionPref(pref_key, &browser_pref, &permission)); | |
98 if (!GetExtension()->HasApiPermission(permission)) { | |
99 error_ = base::StringPrintf(kPermissionErrorMessage, pref_key.c_str()); | |
100 return false; | |
101 } | |
102 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); | |
103 const PrefService::Preference* pref = | |
104 prefs->FindPreference(browser_pref.c_str()); | |
105 DCHECK(pref); | |
106 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.
| |
107 prefs->SetExtensionControlledPref(extension_id(), | |
108 browser_pref, | |
109 incognito, | |
110 value->DeepCopy()); | |
111 return true; | |
112 } | |
OLD | NEW |