OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_util.h" | 5 #include "chrome/browser/extensions/extension_util.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "chrome/browser/extensions/extension_service.h" | 10 #include "chrome/browser/extensions/extension_service.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 // When we reload the extension the ID may be invalidated if we've passed it | 63 // When we reload the extension the ID may be invalidated if we've passed it |
64 // by const ref everywhere. Make a copy to be safe. http://crbug.com/103762 | 64 // by const ref everywhere. Make a copy to be safe. http://crbug.com/103762 |
65 std::string id = extension_id; | 65 std::string id = extension_id; |
66 ExtensionService* service = | 66 ExtensionService* service = |
67 ExtensionSystem::Get(context)->extension_service(); | 67 ExtensionSystem::Get(context)->extension_service(); |
68 CHECK(service); | 68 CHECK(service); |
69 service->ReloadExtension(id); | 69 service->ReloadExtension(id); |
70 return id; | 70 return id; |
71 } | 71 } |
72 | 72 |
| 73 // Sets the preference for scripting on all urls to |allowed|, optionally |
| 74 // updating the extension's active permissions (based on |update_permissions|). |
| 75 void SetAllowedScriptingOnAllUrlsHelper( |
| 76 content::BrowserContext* context, |
| 77 const std::string& extension_id, |
| 78 bool allowed, |
| 79 bool update_permissions) { |
| 80 // TODO(devlin): Right now, we always need to have a value for this pref. |
| 81 // Once the scripts-require-action feature launches, we can change the set |
| 82 // to be null if false. |
| 83 ExtensionPrefs::Get(context)->UpdateExtensionPref( |
| 84 extension_id, |
| 85 kExtensionAllowedOnAllUrlsPrefName, |
| 86 new base::FundamentalValue(allowed)); |
| 87 |
| 88 if (update_permissions) { |
| 89 const Extension* extension = |
| 90 ExtensionRegistry::Get(context)->enabled_extensions().GetByID( |
| 91 extension_id); |
| 92 if (extension) { |
| 93 PermissionsUpdater updater(context); |
| 94 if (allowed) |
| 95 updater.GrantWithheldImpliedAllHosts(extension); |
| 96 else |
| 97 updater.WithholdImpliedAllHosts(extension); |
| 98 } |
| 99 } |
| 100 } |
| 101 |
73 } // namespace | 102 } // namespace |
74 | 103 |
75 bool IsIncognitoEnabled(const std::string& extension_id, | 104 bool IsIncognitoEnabled(const std::string& extension_id, |
76 content::BrowserContext* context) { | 105 content::BrowserContext* context) { |
77 const Extension* extension = ExtensionRegistry::Get(context)-> | 106 const Extension* extension = ExtensionRegistry::Get(context)-> |
78 GetExtensionById(extension_id, ExtensionRegistry::ENABLED); | 107 GetExtensionById(extension_id, ExtensionRegistry::ENABLED); |
79 if (extension) { | 108 if (extension) { |
80 if (!extension->can_be_incognito_enabled()) | 109 if (!extension->can_be_incognito_enabled()) |
81 return false; | 110 return false; |
82 // If this is an existing component extension we always allow it to | 111 // If this is an existing component extension we always allow it to |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 return; | 198 return; |
170 | 199 |
171 ExtensionPrefs::Get(context)->SetAllowFileAccess(extension_id, allow); | 200 ExtensionPrefs::Get(context)->SetAllowFileAccess(extension_id, allow); |
172 | 201 |
173 ReloadExtensionIfEnabled(extension_id, context); | 202 ReloadExtensionIfEnabled(extension_id, context); |
174 } | 203 } |
175 | 204 |
176 bool AllowedScriptingOnAllUrls(const std::string& extension_id, | 205 bool AllowedScriptingOnAllUrls(const std::string& extension_id, |
177 content::BrowserContext* context) { | 206 content::BrowserContext* context) { |
178 bool allowed = false; | 207 bool allowed = false; |
179 return ExtensionPrefs::Get(context)->ReadPrefAsBoolean( | 208 ExtensionPrefs* prefs = ExtensionPrefs::Get(context); |
180 extension_id, | 209 if (!prefs->ReadPrefAsBoolean(extension_id, |
181 kExtensionAllowedOnAllUrlsPrefName, | 210 kExtensionAllowedOnAllUrlsPrefName, |
182 &allowed) && | 211 &allowed)) { |
183 allowed; | 212 // If there is no value present, we make one, defaulting it to the value of |
| 213 // the 'scripts require action' flag. If the flag is on, then the extension |
| 214 // does not have permission to script on all urls by default. |
| 215 allowed = DefaultAllowedScriptingOnAllUrls(); |
| 216 SetAllowedScriptingOnAllUrlsHelper(context, extension_id, allowed, false); |
| 217 } |
| 218 return allowed; |
184 } | 219 } |
185 | 220 |
186 void SetAllowedScriptingOnAllUrls(const std::string& extension_id, | 221 void SetAllowedScriptingOnAllUrls(const std::string& extension_id, |
187 content::BrowserContext* context, | 222 content::BrowserContext* context, |
188 bool allowed) { | 223 bool allowed) { |
189 if (allowed == AllowedScriptingOnAllUrls(extension_id, context)) | 224 if (allowed != AllowedScriptingOnAllUrls(extension_id, context)) |
190 return; // Nothing to do here. | 225 SetAllowedScriptingOnAllUrlsHelper(context, extension_id, allowed, true); |
191 | |
192 ExtensionPrefs::Get(context)->UpdateExtensionPref( | |
193 extension_id, | |
194 kExtensionAllowedOnAllUrlsPrefName, | |
195 allowed ? new base::FundamentalValue(true) : NULL); | |
196 | |
197 const Extension* extension = | |
198 ExtensionRegistry::Get(context)->enabled_extensions().GetByID( | |
199 extension_id); | |
200 if (extension) { | |
201 PermissionsUpdater updater(context); | |
202 if (allowed) | |
203 updater.GrantWithheldImpliedAllHosts(extension); | |
204 else | |
205 updater.WithholdImpliedAllHosts(extension); | |
206 } | |
207 } | 226 } |
208 | 227 |
209 bool ScriptsMayRequireActionForExtension(const Extension* extension) { | 228 bool DefaultAllowedScriptingOnAllUrls() { |
210 // An extension requires user action to execute scripts iff the switch to do | 229 return !FeatureSwitch::scripts_require_action()->IsEnabled(); |
211 // so is enabled, the extension shows up in chrome:extensions (so the user can | 230 } |
212 // grant withheld permissions), the extension is not part of chrome or | 231 |
213 // corporate policy, and also not on the scripting whitelist. | 232 bool ScriptsMayRequireActionForExtension( |
214 return FeatureSwitch::scripts_require_action()->IsEnabled() && | 233 const Extension* extension, |
215 extension->ShouldDisplayInExtensionSettings() && | 234 const PermissionSet* permissions) { |
| 235 // An extension may require user action to execute scripts iff the extension |
| 236 // shows up in chrome:extensions (so the user can grant withheld permissions), |
| 237 // is not part of chrome or corporate policy, not on the scripting whitelist, |
| 238 // and requires enough permissions that we should withhold them. |
| 239 return extension->ShouldDisplayInExtensionSettings() && |
216 !Manifest::IsPolicyLocation(extension->location()) && | 240 !Manifest::IsPolicyLocation(extension->location()) && |
217 !Manifest::IsComponentLocation(extension->location()) && | 241 !Manifest::IsComponentLocation(extension->location()) && |
218 !PermissionsData::CanExecuteScriptEverywhere(extension); | 242 !PermissionsData::CanExecuteScriptEverywhere(extension) && |
| 243 permissions->ShouldWarnAllHosts(); |
219 } | 244 } |
220 | 245 |
221 bool IsAppLaunchable(const std::string& extension_id, | 246 bool IsAppLaunchable(const std::string& extension_id, |
222 content::BrowserContext* context) { | 247 content::BrowserContext* context) { |
223 int reason = ExtensionPrefs::Get(context)->GetDisableReasons(extension_id); | 248 int reason = ExtensionPrefs::Get(context)->GetDisableReasons(extension_id); |
224 return !((reason & Extension::DISABLE_UNSUPPORTED_REQUIREMENT) || | 249 return !((reason & Extension::DISABLE_UNSUPPORTED_REQUIREMENT) || |
225 (reason & Extension::DISABLE_CORRUPTED)); | 250 (reason & Extension::DISABLE_CORRUPTED)); |
226 } | 251 } |
227 | 252 |
228 bool IsAppLaunchableWithoutEnabling(const std::string& extension_id, | 253 bool IsAppLaunchableWithoutEnabling(const std::string& extension_id, |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 IDR_EXTENSION_DEFAULT_ICON); | 374 IDR_EXTENSION_DEFAULT_ICON); |
350 } | 375 } |
351 | 376 |
352 bool IsNewBookmarkAppsEnabled() { | 377 bool IsNewBookmarkAppsEnabled() { |
353 return base::CommandLine::ForCurrentProcess()->HasSwitch( | 378 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
354 switches::kEnableNewBookmarkApps); | 379 switches::kEnableNewBookmarkApps); |
355 } | 380 } |
356 | 381 |
357 } // namespace util | 382 } // namespace util |
358 } // namespace extensions | 383 } // namespace extensions |
OLD | NEW |