Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(247)

Side by Side Diff: chrome/browser/extensions/extension_preference_api.cc

Issue 10675007: Move each permission classes to its own files in extensions/permissions (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Rebase again Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include <utility> 8 #include <utility>
9 9
10 #include "base/memory/singleton.h" 10 #include "base/memory/singleton.h"
11 #include "base/stl_util.h" 11 #include "base/stl_util.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/api/proxy/proxy_api.h" 14 #include "chrome/browser/extensions/api/proxy/proxy_api.h"
15 #include "chrome/browser/extensions/extension_event_router.h" 15 #include "chrome/browser/extensions/extension_event_router.h"
16 #include "chrome/browser/extensions/extension_preference_api_constants.h" 16 #include "chrome/browser/extensions/extension_preference_api_constants.h"
17 #include "chrome/browser/extensions/extension_preference_helpers.h" 17 #include "chrome/browser/extensions/extension_preference_helpers.h"
18 #include "chrome/browser/extensions/extension_prefs.h" 18 #include "chrome/browser/extensions/extension_prefs.h"
19 #include "chrome/browser/extensions/extension_prefs_scope.h" 19 #include "chrome/browser/extensions/extension_prefs_scope.h"
20 #include "chrome/browser/extensions/extension_service.h" 20 #include "chrome/browser/extensions/extension_service.h"
21 #include "chrome/browser/prefs/pref_service.h" 21 #include "chrome/browser/prefs/pref_service.h"
22 #include "chrome/browser/profiles/profile.h" 22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/common/chrome_notification_types.h" 23 #include "chrome/common/chrome_notification_types.h"
24 #include "chrome/common/extensions/extension_error_utils.h" 24 #include "chrome/common/extensions/extension_error_utils.h"
25 #include "chrome/common/extensions/extension_permission_set.h" 25 #include "chrome/common/extensions/permissions/api_permission.h"
26 #include "chrome/common/pref_names.h" 26 #include "chrome/common/pref_names.h"
27 #include "content/public/browser/notification_details.h" 27 #include "content/public/browser/notification_details.h"
28 #include "content/public/browser/notification_source.h" 28 #include "content/public/browser/notification_source.h"
29 29
30 namespace keys = extension_preference_api_constants; 30 namespace keys = extension_preference_api_constants;
31 namespace helpers = extension_preference_helpers; 31 namespace helpers = extension_preference_helpers;
32 32
33 using extensions::APIPermission;
34
33 namespace { 35 namespace {
34 36
35 struct PrefMappingEntry { 37 struct PrefMappingEntry {
36 // Name of the preference referenced by the extension API JSON. 38 // Name of the preference referenced by the extension API JSON.
37 const char* extension_pref; 39 const char* extension_pref;
38 40
39 // Name of the preference in the PrefStores. 41 // Name of the preference in the PrefStores.
40 const char* browser_pref; 42 const char* browser_pref;
41 43
42 // Permission required to access this preference. 44 // Permission required to access this preference.
43 // Use ExtensionAPIPermission::kInvalid for |permission| to express that no 45 // Use APIPermission::kInvalid for |permission| to express that no
44 // permission is necessary. 46 // permission is necessary.
45 ExtensionAPIPermission::ID permission; 47 APIPermission::ID permission;
46 }; 48 };
47 49
48 const char kOnPrefChangeFormat[] = "types.ChromeSetting.%s.onChange"; 50 const char kOnPrefChangeFormat[] = "types.ChromeSetting.%s.onChange";
49 51
50 PrefMappingEntry kPrefMapping[] = { 52 PrefMappingEntry kPrefMapping[] = {
51 #if defined(OS_CHROMEOS) 53 #if defined(OS_CHROMEOS)
52 { "protectedContentEnabled", 54 { "protectedContentEnabled",
53 prefs::kEnableCrosDRM, 55 prefs::kEnableCrosDRM,
54 ExtensionAPIPermission::kPrivacy 56 APIPermission::kPrivacy
55 }, 57 },
56 #endif // defined(OS_CHROMEOS) 58 #endif // defined(OS_CHROMEOS)
57 { "alternateErrorPagesEnabled", 59 { "alternateErrorPagesEnabled",
58 prefs::kAlternateErrorPagesEnabled, 60 prefs::kAlternateErrorPagesEnabled,
59 ExtensionAPIPermission::kPrivacy 61 APIPermission::kPrivacy
60 }, 62 },
61 { "autofillEnabled", 63 { "autofillEnabled",
62 prefs::kAutofillEnabled, 64 prefs::kAutofillEnabled,
63 ExtensionAPIPermission::kPrivacy 65 APIPermission::kPrivacy
64 }, 66 },
65 { "hyperlinkAuditingEnabled", 67 { "hyperlinkAuditingEnabled",
66 prefs::kEnableHyperlinkAuditing, 68 prefs::kEnableHyperlinkAuditing,
67 ExtensionAPIPermission::kPrivacy 69 APIPermission::kPrivacy
68 }, 70 },
69 { "instantEnabled", 71 { "instantEnabled",
70 prefs::kInstantEnabled, 72 prefs::kInstantEnabled,
71 ExtensionAPIPermission::kPrivacy 73 APIPermission::kPrivacy
72 }, 74 },
73 { "managedModeEnabled", 75 { "managedModeEnabled",
74 prefs::kInManagedMode, 76 prefs::kInManagedMode,
75 ExtensionAPIPermission::kManagedModePrivate 77 APIPermission::kManagedModePrivate
76 }, 78 },
77 { "networkPredictionEnabled", 79 { "networkPredictionEnabled",
78 prefs::kNetworkPredictionEnabled, 80 prefs::kNetworkPredictionEnabled,
79 ExtensionAPIPermission::kPrivacy 81 APIPermission::kPrivacy
80 }, 82 },
81 { "proxy", 83 { "proxy",
82 prefs::kProxy, 84 prefs::kProxy,
83 ExtensionAPIPermission::kProxy 85 APIPermission::kProxy
84 }, 86 },
85 { "referrersEnabled", 87 { "referrersEnabled",
86 prefs::kEnableReferrers, 88 prefs::kEnableReferrers,
87 ExtensionAPIPermission::kPrivacy 89 APIPermission::kPrivacy
88 }, 90 },
89 { "safeBrowsingEnabled", 91 { "safeBrowsingEnabled",
90 prefs::kSafeBrowsingEnabled, 92 prefs::kSafeBrowsingEnabled,
91 ExtensionAPIPermission::kPrivacy 93 APIPermission::kPrivacy
92 }, 94 },
93 { "searchSuggestEnabled", 95 { "searchSuggestEnabled",
94 prefs::kSearchSuggestEnabled, 96 prefs::kSearchSuggestEnabled,
95 ExtensionAPIPermission::kPrivacy 97 APIPermission::kPrivacy
96 }, 98 },
97 { "spellingServiceEnabled", 99 { "spellingServiceEnabled",
98 prefs::kSpellCheckUseSpellingService, 100 prefs::kSpellCheckUseSpellingService,
99 ExtensionAPIPermission::kPrivacy 101 APIPermission::kPrivacy
100 }, 102 },
101 { "thirdPartyCookiesAllowed", 103 { "thirdPartyCookiesAllowed",
102 prefs::kBlockThirdPartyCookies, 104 prefs::kBlockThirdPartyCookies,
103 ExtensionAPIPermission::kPrivacy 105 APIPermission::kPrivacy
104 }, 106 },
105 { "translationServiceEnabled", 107 { "translationServiceEnabled",
106 prefs::kEnableTranslate, 108 prefs::kEnableTranslate,
107 ExtensionAPIPermission::kPrivacy 109 APIPermission::kPrivacy
108 } 110 }
109 }; 111 };
110 112
111 class IdentityPrefTransformer : public PrefTransformerInterface { 113 class IdentityPrefTransformer : public PrefTransformerInterface {
112 public: 114 public:
113 virtual Value* ExtensionToBrowserPref(const Value* extension_pref, 115 virtual Value* ExtensionToBrowserPref(const Value* extension_pref,
114 std::string* error, 116 std::string* error,
115 bool* bad_message) { 117 bool* bad_message) {
116 return extension_pref->DeepCopy(); 118 return extension_pref->DeepCopy();
117 } 119 }
(...skipping 25 matching lines...) Expand all
143 }; 145 };
144 146
145 class PrefMapping { 147 class PrefMapping {
146 public: 148 public:
147 static PrefMapping* GetInstance() { 149 static PrefMapping* GetInstance() {
148 return Singleton<PrefMapping>::get(); 150 return Singleton<PrefMapping>::get();
149 } 151 }
150 152
151 bool FindBrowserPrefForExtensionPref(const std::string& extension_pref, 153 bool FindBrowserPrefForExtensionPref(const std::string& extension_pref,
152 std::string* browser_pref, 154 std::string* browser_pref,
153 ExtensionAPIPermission::ID* permission) { 155 APIPermission::ID* permission) {
154 PrefMap::iterator it = mapping_.find(extension_pref); 156 PrefMap::iterator it = mapping_.find(extension_pref);
155 if (it != mapping_.end()) { 157 if (it != mapping_.end()) {
156 *browser_pref = it->second.first; 158 *browser_pref = it->second.first;
157 *permission = it->second.second; 159 *permission = it->second.second;
158 return true; 160 return true;
159 } 161 }
160 return false; 162 return false;
161 } 163 }
162 164
163 bool FindEventForBrowserPref(const std::string& browser_pref, 165 bool FindEventForBrowserPref(const std::string& browser_pref,
164 std::string* event_name, 166 std::string* event_name,
165 ExtensionAPIPermission::ID* permission) { 167 APIPermission::ID* permission) {
166 PrefMap::iterator it = event_mapping_.find(browser_pref); 168 PrefMap::iterator it = event_mapping_.find(browser_pref);
167 if (it != event_mapping_.end()) { 169 if (it != event_mapping_.end()) {
168 *event_name = it->second.first; 170 *event_name = it->second.first;
169 *permission = it->second.second; 171 *permission = it->second.second;
170 return true; 172 return true;
171 } 173 }
172 return false; 174 return false;
173 } 175 }
174 176
175 PrefTransformerInterface* FindTransformerForBrowserPref( 177 PrefTransformerInterface* FindTransformerForBrowserPref(
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 } 213 }
212 214
213 void RegisterPrefTransformer(const std::string& browser_pref, 215 void RegisterPrefTransformer(const std::string& browser_pref,
214 PrefTransformerInterface* transformer) { 216 PrefTransformerInterface* transformer) {
215 DCHECK_EQ(0u, transformers_.count(browser_pref)) << 217 DCHECK_EQ(0u, transformers_.count(browser_pref)) <<
216 "Trying to register pref transformer for " << browser_pref << " twice"; 218 "Trying to register pref transformer for " << browser_pref << " twice";
217 transformers_[browser_pref] = transformer; 219 transformers_[browser_pref] = transformer;
218 } 220 }
219 221
220 typedef std::map<std::string, 222 typedef std::map<std::string,
221 std::pair<std::string, ExtensionAPIPermission::ID> > 223 std::pair<std::string, APIPermission::ID> >
222 PrefMap; 224 PrefMap;
223 225
224 // Mapping from extension pref keys to browser pref keys and permissions. 226 // Mapping from extension pref keys to browser pref keys and permissions.
225 PrefMap mapping_; 227 PrefMap mapping_;
226 228
227 // Mapping from browser pref keys to extension event names and permissions. 229 // Mapping from browser pref keys to extension event names and permissions.
228 PrefMap event_mapping_; 230 PrefMap event_mapping_;
229 231
230 // Mapping from browser pref keys to transformers. 232 // Mapping from browser pref keys to transformers.
231 std::map<std::string, PrefTransformerInterface*> transformers_; 233 std::map<std::string, PrefTransformerInterface*> transformers_;
(...skipping 29 matching lines...) Expand all
261 NOTREACHED(); 263 NOTREACHED();
262 } 264 }
263 } 265 }
264 266
265 void ExtensionPreferenceEventRouter::OnPrefChanged( 267 void ExtensionPreferenceEventRouter::OnPrefChanged(
266 PrefService* pref_service, 268 PrefService* pref_service,
267 const std::string& browser_pref) { 269 const std::string& browser_pref) {
268 bool incognito = (pref_service != profile_->GetPrefs()); 270 bool incognito = (pref_service != profile_->GetPrefs());
269 271
270 std::string event_name; 272 std::string event_name;
271 ExtensionAPIPermission::ID permission = ExtensionAPIPermission::kInvalid; 273 APIPermission::ID permission = APIPermission::kInvalid;
272 bool rv = PrefMapping::GetInstance()->FindEventForBrowserPref( 274 bool rv = PrefMapping::GetInstance()->FindEventForBrowserPref(
273 browser_pref, &event_name, &permission); 275 browser_pref, &event_name, &permission);
274 DCHECK(rv); 276 DCHECK(rv);
275 277
276 ListValue args; 278 ListValue args;
277 DictionaryValue* dict = new DictionaryValue(); 279 DictionaryValue* dict = new DictionaryValue();
278 args.Append(dict); 280 args.Append(dict);
279 const PrefService::Preference* pref = 281 const PrefService::Preference* pref =
280 pref_service->FindPreference(browser_pref.c_str()); 282 pref_service->FindPreference(browser_pref.c_str());
281 CHECK(pref); 283 CHECK(pref);
(...skipping 14 matching lines...) Expand all
296 permission, 298 permission,
297 incognito, 299 incognito,
298 browser_pref); 300 browser_pref);
299 } 301 }
300 302
301 PreferenceFunction::~PreferenceFunction() { } 303 PreferenceFunction::~PreferenceFunction() { }
302 304
303 bool PreferenceFunction::ValidateBrowserPref( 305 bool PreferenceFunction::ValidateBrowserPref(
304 const std::string& extension_pref_key, 306 const std::string& extension_pref_key,
305 std::string* browser_pref_key) { 307 std::string* browser_pref_key) {
306 ExtensionAPIPermission::ID permission = ExtensionAPIPermission::kInvalid; 308 APIPermission::ID permission = APIPermission::kInvalid;
307 EXTENSION_FUNCTION_VALIDATE( 309 EXTENSION_FUNCTION_VALIDATE(
308 PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( 310 PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref(
309 extension_pref_key, browser_pref_key, &permission)); 311 extension_pref_key, browser_pref_key, &permission));
310 if (!GetExtension()->HasAPIPermission(permission)) { 312 if (!GetExtension()->HasAPIPermission(permission)) {
311 error_ = ExtensionErrorUtils::FormatErrorMessage( 313 error_ = ExtensionErrorUtils::FormatErrorMessage(
312 keys::kPermissionErrorMessage, extension_pref_key); 314 keys::kPermissionErrorMessage, extension_pref_key);
313 return false; 315 return false;
314 } 316 }
315 return true; 317 return true;
316 } 318 }
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
476 } 478 }
477 479
478 std::string browser_pref; 480 std::string browser_pref;
479 if (!ValidateBrowserPref(pref_key, &browser_pref)) 481 if (!ValidateBrowserPref(pref_key, &browser_pref))
480 return false; 482 return false;
481 483
482 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); 484 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs();
483 prefs->RemoveExtensionControlledPref(extension_id(), browser_pref, scope); 485 prefs->RemoveExtensionControlledPref(extension_id(), browser_pref, scope);
484 return true; 486 return true;
485 } 487 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_management_api.cc ('k') | chrome/browser/extensions/extension_preference_helpers.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698