1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
// 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 <map>
#include "base/json/json_writer.h"
#include "base/singleton.h"
#include "base/stl_util-inl.h"
#include "base/stringprintf.h"
#include "base/values.h"
#include "chrome/browser/extensions/extension_prefs.h"
#include "chrome/browser/extensions/extension_proxy_api.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;
};
const char kNotControllable[] = "NotControllable";
const char kControlledByOtherExtensions[] = "ControlledByOtherExtensions";
const char kControllableByThisExtension[] = "ControllableByThisExtension";
const char kControlledByThisExtension[] = "ControlledByThisExtension";
const char kIncognito[] = "incognito";
const char kIncognitoSpecific[] = "incognitoSpecific";
const char kLevelOfControl[] = "levelOfControl";
const char kValue[] = "value";
const char kIncognitoErrorMessage[] =
"You do not have permission to access incognito preferences.";
const char kPermissionErrorMessage[] =
"You do not have permission to access the preference '%s'. "
"Be sure to declare in your manifest what permissions you need.";
PrefMappingEntry kPrefMapping[] = {
{ "blockThirdPartyCookies",
prefs::kBlockThirdPartyCookies,
Extension::kContentSettingsPermission
},
{ "proxy",
prefs::kProxy,
Extension::kProxyPermission
},
};
class IdentityPrefTransformer : public PrefTransformerInterface {
public:
IdentityPrefTransformer() { }
virtual ~IdentityPrefTransformer() { }
virtual Value* ExtensionToBrowserPref(const Value* extension_pref,
std::string* error) {
return extension_pref->DeepCopy();
}
virtual Value* BrowserToExtensionPref(const Value* browser_pref) {
return browser_pref->DeepCopy();
}
};
class PrefMapping {
public:
static PrefMapping* GetInstance() {
return Singleton<PrefMapping>::get();
}
bool FindBrowserPrefForExtensionPref(const std::string& extension_pref,
std::string* browser_pref,
std::string* permission) {
std::map<std::string, std::pair<std::string, std::string> >::iterator it =
mapping_.find(extension_pref);
if (it != mapping_.end()) {
*browser_pref = it->second.first;
*permission = it->second.second;
return true;
}
return false;
}
PrefTransformerInterface* FindTransformerForBrowserPref(
const std::string& browser_pref) {
std::map<std::string, PrefTransformerInterface*>::iterator it =
transformers_.find(browser_pref);
if (it != transformers_.end())
return it->second;
else
return identity_transformer_.get();
}
private:
friend struct DefaultSingletonTraits<PrefMapping>;
PrefMapping() {
identity_transformer_.reset(new IdentityPrefTransformer());
for (size_t i = 0; i < arraysize(kPrefMapping); ++i) {
mapping_[kPrefMapping[i].extension_pref] =
std::make_pair(kPrefMapping[i].browser_pref,
kPrefMapping[i].permission);
}
DCHECK_EQ(arraysize(kPrefMapping), mapping_.size());
RegisterPrefTransformer(prefs::kProxy, new ProxyPrefTransformer());
}
~PrefMapping() {
STLDeleteContainerPairSecondPointers(transformers_.begin(),
transformers_.end());
}
void RegisterPrefTransformer(const std::string& browser_pref,
PrefTransformerInterface* transformer) {
DCHECK_EQ(0u, transformers_.count(browser_pref)) <<
"Trying to register pref transformer for " << browser_pref << " twice";
transformers_[browser_pref] = transformer;
}
// Mapping from extension pref keys to browser pref keys.
std::map<std::string, std::pair<std::string, std::string> > mapping_;
// Mapping from browser pref keys to transformers.
std::map<std::string, PrefTransformerInterface*> transformers_;
scoped_ptr<PrefTransformerInterface> identity_transformer_;
};
} // namespace
// TODO(battre): Factor out common parts once this is stable.
GetPreferenceFunction::~GetPreferenceFunction() { }
const char* GetPreferenceFunction::GetLevelOfControl(
const std::string& browser_pref,
bool incognito) const {
PrefService* prefs = incognito ? profile_->GetOffTheRecordPrefs()
: profile_->GetPrefs();
const PrefService::Preference* pref =
prefs->FindPreference(browser_pref.c_str());
CHECK(pref);
ExtensionPrefs* ep = profile_->GetExtensionService()->extension_prefs();
if (!pref->IsExtensionModifiable())
return kNotControllable;
if (ep->DoesExtensionControlPref(extension_id(), browser_pref, incognito))
return kControlledByThisExtension;
if (ep->CanExtensionControlPref(extension_id(), browser_pref, incognito))
return kControllableByThisExtension;
return kControlledByOtherExtensions;
}
bool GetPreferenceFunction::RunImpl() {
std::string pref_key;
EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key));
DictionaryValue* details = NULL;
EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details));
bool incognito = false;
if (details->HasKey(kIncognito))
EXTENSION_FUNCTION_VALIDATE(details->GetBoolean(kIncognito, &incognito));
if (incognito && !include_incognito()) {
error_ = kIncognitoErrorMessage;
return false;
}
PrefService* prefs = incognito ? profile_->GetOffTheRecordPrefs()
: profile_->GetPrefs();
std::string browser_pref;
std::string permission;
EXTENSION_FUNCTION_VALIDATE(
PrefMapping::GetInstance()->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());
CHECK(pref);
std::string level_of_control = GetLevelOfControl(browser_pref, incognito);
scoped_ptr<DictionaryValue> result(new DictionaryValue);
PrefTransformerInterface* transformer =
PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref);
result->Set(kValue, transformer->BrowserToExtensionPref(pref->GetValue()));
result->Set(kLevelOfControl, Value::CreateStringValue(level_of_control));
if (incognito) {
ExtensionPrefs* ep = profile_->GetExtensionService()->extension_prefs();
result->Set(
kIncognitoSpecific,
Value::CreateBooleanValue(ep->HasIncognitoPrefValue(browser_pref)));
}
result_.reset(result.release());
return true;
}
SetPreferenceFunction::~SetPreferenceFunction() { }
bool SetPreferenceFunction::RunImpl() {
std::string pref_key;
EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key));
DictionaryValue* details = NULL;
EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details));
Value* value = NULL;
EXTENSION_FUNCTION_VALIDATE(details->Get(kValue, &value));
bool incognito = false;
if (details->HasKey(kIncognito))
EXTENSION_FUNCTION_VALIDATE(details->GetBoolean(kIncognito, &incognito));
if (incognito && !include_incognito()) {
error_ = kIncognitoErrorMessage;
return false;
}
std::string browser_pref;
std::string permission;
EXTENSION_FUNCTION_VALIDATE(
PrefMapping::GetInstance()->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->pref_service()->FindPreference(browser_pref.c_str());
CHECK(pref);
EXTENSION_FUNCTION_VALIDATE(value->GetType() == pref->GetType());
PrefTransformerInterface* transformer =
PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref);
std::string error;
Value* browserPrefValue = transformer->ExtensionToBrowserPref(value, &error);
if (!browserPrefValue) {
error_ = error;
return false;
}
prefs->SetExtensionControlledPref(extension_id(),
browser_pref,
incognito,
browserPrefValue);
return true;
}
ClearPreferenceFunction::~ClearPreferenceFunction() { }
bool ClearPreferenceFunction::RunImpl() {
std::string pref_key;
EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key));
DictionaryValue* details = NULL;
EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details));
bool incognito = false;
if (details->HasKey(kIncognito))
EXTENSION_FUNCTION_VALIDATE(details->GetBoolean(kIncognito, &incognito));
// We don't check incognito permissions here, as an extension should be always
// allowed to clear its own settings.
std::string browser_pref;
std::string permission;
EXTENSION_FUNCTION_VALIDATE(
PrefMapping::GetInstance()->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();
prefs->RemoveExtensionControlledPref(extension_id(), browser_pref, incognito);
return true;
}
|