Chromium Code Reviews| 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 ExtensionPrefs::Get(context)->UpdateExtensionPref( | |
| 81 extension_id, | |
| 82 kExtensionAllowedOnAllUrlsPrefName, | |
| 83 new base::FundamentalValue(allowed)); | |
|
not at google - send to devlin
2015/02/06 00:26:55
So that the prefs delete the value if false, use (
Devlin
2015/02/06 18:58:43
As discussed offline, we actually need this pref t
| |
| 84 | |
| 85 if (update_permissions) { | |
| 86 const Extension* extension = | |
| 87 ExtensionRegistry::Get(context)->enabled_extensions().GetByID( | |
| 88 extension_id); | |
| 89 if (extension) { | |
| 90 PermissionsUpdater updater(context); | |
| 91 if (allowed) | |
| 92 updater.GrantWithheldImpliedAllHosts(extension); | |
| 93 else | |
| 94 updater.WithholdImpliedAllHosts(extension); | |
| 95 } | |
| 96 } | |
| 97 } | |
| 98 | |
| 73 } // namespace | 99 } // namespace |
| 74 | 100 |
| 75 bool IsIncognitoEnabled(const std::string& extension_id, | 101 bool IsIncognitoEnabled(const std::string& extension_id, |
| 76 content::BrowserContext* context) { | 102 content::BrowserContext* context) { |
| 77 const Extension* extension = ExtensionRegistry::Get(context)-> | 103 const Extension* extension = ExtensionRegistry::Get(context)-> |
| 78 GetExtensionById(extension_id, ExtensionRegistry::ENABLED); | 104 GetExtensionById(extension_id, ExtensionRegistry::ENABLED); |
| 79 if (extension) { | 105 if (extension) { |
| 80 if (!extension->can_be_incognito_enabled()) | 106 if (!extension->can_be_incognito_enabled()) |
| 81 return false; | 107 return false; |
| 82 // If this is an existing component extension we always allow it to | 108 // 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; | 195 return; |
| 170 | 196 |
| 171 ExtensionPrefs::Get(context)->SetAllowFileAccess(extension_id, allow); | 197 ExtensionPrefs::Get(context)->SetAllowFileAccess(extension_id, allow); |
| 172 | 198 |
| 173 ReloadExtensionIfEnabled(extension_id, context); | 199 ReloadExtensionIfEnabled(extension_id, context); |
| 174 } | 200 } |
| 175 | 201 |
| 176 bool AllowedScriptingOnAllUrls(const std::string& extension_id, | 202 bool AllowedScriptingOnAllUrls(const std::string& extension_id, |
| 177 content::BrowserContext* context) { | 203 content::BrowserContext* context) { |
| 178 bool allowed = false; | 204 bool allowed = false; |
| 179 return ExtensionPrefs::Get(context)->ReadPrefAsBoolean( | 205 ExtensionPrefs* prefs = ExtensionPrefs::Get(context); |
| 180 extension_id, | 206 if (!prefs->ReadPrefAsBoolean(extension_id, |
| 181 kExtensionAllowedOnAllUrlsPrefName, | 207 kExtensionAllowedOnAllUrlsPrefName, |
| 182 &allowed) && | 208 &allowed)) { |
| 183 allowed; | 209 // If there is no value present, we make one, defaulting it to the value of |
| 210 // the 'scripts require action' flag. If the flag is on, then the extension | |
| 211 // does not have permission to script on all urls by default. | |
| 212 bool default_value = DefaultAllowedScriptingOnAllUrls(); | |
|
not at google - send to devlin
2015/02/06 00:26:55
sorry I'm in a nit mood. Just assign to |allowed|.
Devlin
2015/02/06 18:58:43
Done.
| |
| 213 SetAllowedScriptingOnAllUrlsHelper( | |
| 214 context, extension_id, default_value, false); | |
| 215 allowed = default_value; | |
| 216 } | |
| 217 return allowed; | |
| 184 } | 218 } |
| 185 | 219 |
| 186 void SetAllowedScriptingOnAllUrls(const std::string& extension_id, | 220 void SetAllowedScriptingOnAllUrls(const std::string& extension_id, |
| 187 content::BrowserContext* context, | 221 content::BrowserContext* context, |
| 188 bool allowed) { | 222 bool allowed) { |
| 189 if (allowed == AllowedScriptingOnAllUrls(extension_id, context)) | 223 if (allowed == AllowedScriptingOnAllUrls(extension_id, context)) |
| 190 return; // Nothing to do here. | 224 return; // Nothing to do here. |
| 191 | 225 |
| 192 ExtensionPrefs::Get(context)->UpdateExtensionPref( | 226 SetAllowedScriptingOnAllUrlsHelper(context, extension_id, allowed, true); |
|
not at google - send to devlin
2015/02/06 00:26:55
now that you have this helper, it would be nice to
Devlin
2015/02/06 18:58:43
Thanks for the catch - I usually cringe at those u
| |
| 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 } | 227 } |
| 208 | 228 |
| 209 bool ScriptsMayRequireActionForExtension(const Extension* extension) { | 229 bool DefaultAllowedScriptingOnAllUrls() { |
| 210 // An extension requires user action to execute scripts iff the switch to do | 230 return !FeatureSwitch::scripts_require_action()->IsEnabled(); |
| 211 // so is enabled, the extension shows up in chrome:extensions (so the user can | 231 } |
| 212 // grant withheld permissions), the extension is not part of chrome or | 232 |
| 213 // corporate policy, and also not on the scripting whitelist. | 233 bool ScriptsMayRequireActionForExtension( |
| 214 return FeatureSwitch::scripts_require_action()->IsEnabled() && | 234 const Extension* extension, |
| 215 extension->ShouldDisplayInExtensionSettings() && | 235 const scoped_refptr<const PermissionSet>& permissions) { |
| 236 // An extension may require user action to execute scripts iff the extension | |
| 237 // shows up in chrome:extensions (so the user can grant withheld permissions), | |
| 238 // is not part of chrome or corporate policy, not on the scripting whitelist, | |
| 239 // and requires enough permissions that we should withhold them. | |
| 240 return extension->ShouldDisplayInExtensionSettings() && | |
| 216 !Manifest::IsPolicyLocation(extension->location()) && | 241 !Manifest::IsPolicyLocation(extension->location()) && |
| 217 !Manifest::IsComponentLocation(extension->location()) && | 242 !Manifest::IsComponentLocation(extension->location()) && |
| 218 !PermissionsData::CanExecuteScriptEverywhere(extension); | 243 !PermissionsData::CanExecuteScriptEverywhere(extension) && |
| 244 permissions->ShouldWarnAllHosts(); | |
| 219 } | 245 } |
| 220 | 246 |
| 221 bool IsAppLaunchable(const std::string& extension_id, | 247 bool IsAppLaunchable(const std::string& extension_id, |
| 222 content::BrowserContext* context) { | 248 content::BrowserContext* context) { |
| 223 int reason = ExtensionPrefs::Get(context)->GetDisableReasons(extension_id); | 249 int reason = ExtensionPrefs::Get(context)->GetDisableReasons(extension_id); |
| 224 return !((reason & Extension::DISABLE_UNSUPPORTED_REQUIREMENT) || | 250 return !((reason & Extension::DISABLE_UNSUPPORTED_REQUIREMENT) || |
| 225 (reason & Extension::DISABLE_CORRUPTED)); | 251 (reason & Extension::DISABLE_CORRUPTED)); |
| 226 } | 252 } |
| 227 | 253 |
| 228 bool IsAppLaunchableWithoutEnabling(const std::string& extension_id, | 254 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); | 375 IDR_EXTENSION_DEFAULT_ICON); |
| 350 } | 376 } |
| 351 | 377 |
| 352 bool IsNewBookmarkAppsEnabled() { | 378 bool IsNewBookmarkAppsEnabled() { |
| 353 return base::CommandLine::ForCurrentProcess()->HasSwitch( | 379 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 354 switches::kEnableNewBookmarkApps); | 380 switches::kEnableNewBookmarkApps); |
| 355 } | 381 } |
| 356 | 382 |
| 357 } // namespace util | 383 } // namespace util |
| 358 } // namespace extensions | 384 } // namespace extensions |
| OLD | NEW |