[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/extensions/extension_util.h" |
| 6 | |
| 7 | #include "base/command_line.h" |
[email protected] | bb2feea | 2014-03-18 22:08:13 | [diff] [blame] | 8 | #include "base/logging.h" |
treib | 3202d59 | 2015-07-31 08:33:16 | [diff] [blame] | 9 | #include "base/metrics/field_trial.h" |
[email protected] | bb2feea | 2014-03-18 22:08:13 | [diff] [blame] | 10 | #include "base/values.h" |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 11 | #include "build/build_config.h" |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 12 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 13 | #include "chrome/browser/extensions/extension_sync_service.h" |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 14 | #include "chrome/browser/extensions/permissions_updater.h" |
rdevlin.cronin | cb9f86e | 2015-10-15 15:13:42 | [diff] [blame] | 15 | #include "chrome/browser/extensions/scripting_permissions_modifier.h" |
elijahtaylor | 0def443 | 2014-10-06 18:15:11 | [diff] [blame] | 16 | #include "chrome/browser/extensions/shared_module_service.h" |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 17 | #include "chrome/browser/profiles/profile.h" |
[email protected] | bb2feea | 2014-03-18 22:08:13 | [diff] [blame] | 18 | #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 19 | #include "chrome/common/chrome_switches.h" |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 20 | #include "chrome/common/extensions/sync_helper.h" |
treib | b6af28cd | 2015-12-01 11:19:46 | [diff] [blame] | 21 | #include "components/variations/variations_associated_data.h" |
[email protected] | 617342a4 | 2013-12-18 23:34:03 | [diff] [blame] | 22 | #include "content/public/browser/site_instance.h" |
[email protected] | 489db084 | 2014-01-22 18:20:03 | [diff] [blame] | 23 | #include "extensions/browser/extension_prefs.h" |
[email protected] | 59953980 | 2014-01-07 23:06:00 | [diff] [blame] | 24 | #include "extensions/browser/extension_registry.h" |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 25 | #include "extensions/browser/extension_system.h" |
[email protected] | 30e190f8 | 2014-05-26 16:44:39 | [diff] [blame] | 26 | #include "extensions/browser/extension_util.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 27 | #include "extensions/common/extension.h" |
[email protected] | 4b790884 | 2014-04-07 23:50:22 | [diff] [blame] | 28 | #include "extensions/common/extension_icon_set.h" |
[email protected] | 4d67e9d | 2014-08-18 22:03:54 | [diff] [blame] | 29 | #include "extensions/common/feature_switch.h" |
kalman | 64210328 | 2014-12-15 03:59:09 | [diff] [blame] | 30 | #include "extensions/common/features/behavior_feature.h" |
| 31 | #include "extensions/common/features/feature_provider.h" |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 32 | #include "extensions/common/manifest.h" |
tfarina | 0bcdf36 | 2015-06-29 22:19:26 | [diff] [blame] | 33 | #include "extensions/common/manifest_handlers/app_isolation_info.h" |
[email protected] | 1f7de25 | 2013-11-06 22:02:00 | [diff] [blame] | 34 | #include "extensions/common/manifest_handlers/incognito_info.h" |
[email protected] | 4d67e9d | 2014-08-18 22:03:54 | [diff] [blame] | 35 | #include "extensions/common/permissions/permissions_data.h" |
mukai | 4245dfe8 | 2014-09-05 17:40:51 | [diff] [blame] | 36 | #include "extensions/grit/extensions_browser_resources.h" |
[email protected] | 7eb20e3 | 2014-04-30 08:50:56 | [diff] [blame] | 37 | #include "ui/base/resource/resource_bundle.h" |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 38 | |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 39 | namespace extensions { |
| 40 | namespace util { |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 41 | |
[email protected] | b33c8c2 | 2014-05-29 19:51:08 | [diff] [blame] | 42 | namespace { |
thestig | 7b4bd93 | 2014-09-09 22:44:31 | [diff] [blame] | 43 | |
treib | 3202d59 | 2015-07-31 08:33:16 | [diff] [blame] | 44 | const char kSupervisedUserExtensionPermissionIncreaseFieldTrialName[] = |
| 45 | "SupervisedUserExtensionPermissionIncrease"; |
| 46 | |
[email protected] | b33c8c2 | 2014-05-29 19:51:08 | [diff] [blame] | 47 | // The entry into the ExtensionPrefs for allowing an extension to script on |
| 48 | // all urls without explicit permission. |
| 49 | const char kExtensionAllowedOnAllUrlsPrefName[] = |
| 50 | "extension_can_script_all_urls"; |
[email protected] | 277c414 | 2014-06-19 20:08:54 | [diff] [blame] | 51 | |
rdevlin.cronin | bb21358 | 2015-02-10 00:25:55 | [diff] [blame] | 52 | // The entry into the prefs for when a user has explicitly set the "extension |
| 53 | // allowed on all urls" pref. |
| 54 | const char kHasSetScriptOnAllUrlsPrefName[] = "has_set_script_all_urls"; |
| 55 | |
kalman | 64210328 | 2014-12-15 03:59:09 | [diff] [blame] | 56 | // Returns true if |extension| should always be enabled in incognito mode. |
| 57 | bool IsWhitelistedForIncognito(const Extension* extension) { |
kalman | 38ec488 | 2014-12-18 09:40:38 | [diff] [blame] | 58 | return FeatureProvider::GetBehaviorFeature( |
| 59 | BehaviorFeature::kWhitelistedForIncognito) |
kalman | 64210328 | 2014-12-15 03:59:09 | [diff] [blame] | 60 | ->IsAvailableToExtension(extension) |
| 61 | .is_available(); |
[email protected] | b33c8c2 | 2014-05-29 19:51:08 | [diff] [blame] | 62 | } |
thestig | 7b4bd93 | 2014-09-09 22:44:31 | [diff] [blame] | 63 | |
| 64 | // Returns |extension_id|. See note below. |
| 65 | std::string ReloadExtensionIfEnabled(const std::string& extension_id, |
| 66 | content::BrowserContext* context) { |
| 67 | ExtensionRegistry* registry = ExtensionRegistry::Get(context); |
| 68 | bool extension_is_enabled = |
| 69 | registry->enabled_extensions().Contains(extension_id); |
| 70 | |
| 71 | if (!extension_is_enabled) |
| 72 | return extension_id; |
| 73 | |
| 74 | // When we reload the extension the ID may be invalidated if we've passed it |
| 75 | // by const ref everywhere. Make a copy to be safe. http://crbug.com/103762 |
| 76 | std::string id = extension_id; |
| 77 | ExtensionService* service = |
| 78 | ExtensionSystem::Get(context)->extension_service(); |
| 79 | CHECK(service); |
| 80 | service->ReloadExtension(id); |
| 81 | return id; |
| 82 | } |
| 83 | |
rdevlin.cronin | b8dffe5 | 2015-02-07 00:58:01 | [diff] [blame] | 84 | // Sets the preference for scripting on all urls to |allowed|, optionally |
| 85 | // updating the extension's active permissions (based on |update_permissions|). |
| 86 | void SetAllowedScriptingOnAllUrlsHelper( |
| 87 | content::BrowserContext* context, |
| 88 | const std::string& extension_id, |
| 89 | bool allowed, |
| 90 | bool update_permissions) { |
| 91 | // TODO(devlin): Right now, we always need to have a value for this pref. |
| 92 | // Once the scripts-require-action feature launches, we can change the set |
| 93 | // to be null if false. |
| 94 | ExtensionPrefs::Get(context)->UpdateExtensionPref( |
| 95 | extension_id, |
| 96 | kExtensionAllowedOnAllUrlsPrefName, |
| 97 | new base::FundamentalValue(allowed)); |
| 98 | |
| 99 | if (update_permissions) { |
| 100 | const Extension* extension = |
| 101 | ExtensionRegistry::Get(context)->enabled_extensions().GetByID( |
| 102 | extension_id); |
| 103 | if (extension) { |
rdevlin.cronin | cb9f86e | 2015-10-15 15:13:42 | [diff] [blame] | 104 | ScriptingPermissionsModifier modifier(context, extension); |
rdevlin.cronin | b8dffe5 | 2015-02-07 00:58:01 | [diff] [blame] | 105 | if (allowed) |
rdevlin.cronin | cb9f86e | 2015-10-15 15:13:42 | [diff] [blame] | 106 | modifier.GrantWithheldImpliedAllHosts(); |
rdevlin.cronin | b8dffe5 | 2015-02-07 00:58:01 | [diff] [blame] | 107 | else |
rdevlin.cronin | cb9f86e | 2015-10-15 15:13:42 | [diff] [blame] | 108 | modifier.WithholdImpliedAllHosts(); |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 109 | |
| 110 | // If this was an update to permissions, we also need to sync the change. |
| 111 | ExtensionSyncService* sync_service = ExtensionSyncService::Get(context); |
| 112 | if (sync_service) // sync_service can be null in unittests. |
| 113 | sync_service->SyncExtensionChangeIfNeeded(*extension); |
rdevlin.cronin | b8dffe5 | 2015-02-07 00:58:01 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
[email protected] | 277c414 | 2014-06-19 20:08:54 | [diff] [blame] | 118 | } // namespace |
[email protected] | b33c8c2 | 2014-05-29 19:51:08 | [diff] [blame] | 119 | |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 120 | bool IsIncognitoEnabled(const std::string& extension_id, |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 121 | content::BrowserContext* context) { |
| 122 | const Extension* extension = ExtensionRegistry::Get(context)-> |
| 123 | GetExtensionById(extension_id, ExtensionRegistry::ENABLED); |
| 124 | if (extension) { |
kundaji | e548e744 | 2015-09-18 23:19:09 | [diff] [blame] | 125 | if (!util::CanBeIncognitoEnabled(extension)) |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 126 | return false; |
| 127 | // If this is an existing component extension we always allow it to |
| 128 | // work in incognito mode. |
| 129 | if (extension->location() == Manifest::COMPONENT) |
| 130 | return true; |
kalman | 64210328 | 2014-12-15 03:59:09 | [diff] [blame] | 131 | if (IsWhitelistedForIncognito(extension)) |
[email protected] | 277c414 | 2014-06-19 20:08:54 | [diff] [blame] | 132 | return true; |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 133 | } |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 134 | return ExtensionPrefs::Get(context)->IsIncognitoEnabled(extension_id); |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | void SetIsIncognitoEnabled(const std::string& extension_id, |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 138 | content::BrowserContext* context, |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 139 | bool enabled) { |
thestig | 7b4bd93 | 2014-09-09 22:44:31 | [diff] [blame] | 140 | ExtensionRegistry* registry = ExtensionRegistry::Get(context); |
| 141 | const Extension* extension = |
| 142 | registry->GetExtensionById(extension_id, ExtensionRegistry::EVERYTHING); |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 143 | |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 144 | if (extension) { |
kundaji | e548e744 | 2015-09-18 23:19:09 | [diff] [blame] | 145 | if (!util::CanBeIncognitoEnabled(extension)) |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 146 | return; |
| 147 | |
treib | c644a1c | 2015-07-13 08:37:04 | [diff] [blame] | 148 | // TODO(treib,kalman): Should this be Manifest::IsComponentLocation(..)? |
| 149 | // (which also checks for EXTERNAL_COMPONENT). |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 150 | if (extension->location() == Manifest::COMPONENT) { |
| 151 | // This shouldn't be called for component extensions unless it is called |
| 152 | // by sync, for syncable component extensions. |
| 153 | // See http://crbug.com/112290 and associated CLs for the sordid history. |
treib | c644a1c | 2015-07-13 08:37:04 | [diff] [blame] | 154 | DCHECK(sync_helper::IsSyncableComponentExtension(extension)); |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 155 | |
| 156 | // If we are here, make sure the we aren't trying to change the value. |
thestig | 7b4bd93 | 2014-09-09 22:44:31 | [diff] [blame] | 157 | DCHECK_EQ(enabled, IsIncognitoEnabled(extension_id, context)); |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 158 | return; |
| 159 | } |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 160 | } |
| 161 | |
thestig | 7b4bd93 | 2014-09-09 22:44:31 | [diff] [blame] | 162 | ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(context); |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 163 | // Broadcast unloaded and loaded events to update browser state. Only bother |
| 164 | // if the value changed and the extension is actually enabled, since there is |
| 165 | // no UI otherwise. |
| 166 | bool old_enabled = extension_prefs->IsIncognitoEnabled(extension_id); |
| 167 | if (enabled == old_enabled) |
| 168 | return; |
| 169 | |
| 170 | extension_prefs->SetIsIncognitoEnabled(extension_id, enabled); |
| 171 | |
thestig | 7b4bd93 | 2014-09-09 22:44:31 | [diff] [blame] | 172 | std::string id = ReloadExtensionIfEnabled(extension_id, context); |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 173 | |
| 174 | // Reloading the extension invalidates the |extension| pointer. |
thestig | 7b4bd93 | 2014-09-09 22:44:31 | [diff] [blame] | 175 | extension = registry->GetExtensionById(id, ExtensionRegistry::EVERYTHING); |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 176 | if (extension) { |
thestig | 7b4bd93 | 2014-09-09 22:44:31 | [diff] [blame] | 177 | Profile* profile = Profile::FromBrowserContext(context); |
| 178 | ExtensionSyncService::Get(profile)->SyncExtensionChangeIfNeeded(*extension); |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 179 | } |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | bool CanCrossIncognito(const Extension* extension, |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 183 | content::BrowserContext* context) { |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 184 | // We allow the extension to see events and data from another profile iff it |
| 185 | // uses "spanning" behavior and it has incognito access. "split" mode |
| 186 | // extensions only see events for a matching profile. |
| 187 | CHECK(extension); |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 188 | return IsIncognitoEnabled(extension->id(), context) && |
| 189 | !IncognitoInfo::IsSplitMode(extension); |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | bool CanLoadInIncognito(const Extension* extension, |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 193 | content::BrowserContext* context) { |
| 194 | CHECK(extension); |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 195 | if (extension->is_hosted_app()) |
| 196 | return true; |
| 197 | // Packaged apps and regular extensions need to be enabled specifically for |
| 198 | // incognito (and split mode should be set). |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 199 | return IncognitoInfo::IsSplitMode(extension) && |
| 200 | IsIncognitoEnabled(extension->id(), context); |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 201 | } |
| 202 | |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 203 | bool AllowFileAccess(const std::string& extension_id, |
| 204 | content::BrowserContext* context) { |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 205 | return base::CommandLine::ForCurrentProcess()->HasSwitch( |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 206 | switches::kDisableExtensionsFileAccessCheck) || |
| 207 | ExtensionPrefs::Get(context)->AllowFileAccess(extension_id); |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 208 | } |
| 209 | |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 210 | void SetAllowFileAccess(const std::string& extension_id, |
| 211 | content::BrowserContext* context, |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 212 | bool allow) { |
| 213 | // Reload to update browser state. Only bother if the value changed and the |
| 214 | // extension is actually enabled, since there is no UI otherwise. |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 215 | if (allow == AllowFileAccess(extension_id, context)) |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 216 | return; |
| 217 | |
[email protected] | 7c82539c | 2014-02-19 06:09:17 | [diff] [blame] | 218 | ExtensionPrefs::Get(context)->SetAllowFileAccess(extension_id, allow); |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 219 | |
thestig | 7b4bd93 | 2014-09-09 22:44:31 | [diff] [blame] | 220 | ReloadExtensionIfEnabled(extension_id, context); |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 221 | } |
| 222 | |
[email protected] | b33c8c2 | 2014-05-29 19:51:08 | [diff] [blame] | 223 | bool AllowedScriptingOnAllUrls(const std::string& extension_id, |
| 224 | content::BrowserContext* context) { |
| 225 | bool allowed = false; |
rdevlin.cronin | b8dffe5 | 2015-02-07 00:58:01 | [diff] [blame] | 226 | ExtensionPrefs* prefs = ExtensionPrefs::Get(context); |
| 227 | if (!prefs->ReadPrefAsBoolean(extension_id, |
| 228 | kExtensionAllowedOnAllUrlsPrefName, |
| 229 | &allowed)) { |
| 230 | // If there is no value present, we make one, defaulting it to the value of |
| 231 | // the 'scripts require action' flag. If the flag is on, then the extension |
| 232 | // does not have permission to script on all urls by default. |
| 233 | allowed = DefaultAllowedScriptingOnAllUrls(); |
| 234 | SetAllowedScriptingOnAllUrlsHelper(context, extension_id, allowed, false); |
| 235 | } |
| 236 | return allowed; |
[email protected] | b33c8c2 | 2014-05-29 19:51:08 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | void SetAllowedScriptingOnAllUrls(const std::string& extension_id, |
| 240 | content::BrowserContext* context, |
| 241 | bool allowed) { |
rdevlin.cronin | bb21358 | 2015-02-10 00:25:55 | [diff] [blame] | 242 | if (allowed != AllowedScriptingOnAllUrls(extension_id, context)) { |
rdevlin.cronin | bb21358 | 2015-02-10 00:25:55 | [diff] [blame] | 243 | ExtensionPrefs::Get(context)->UpdateExtensionPref( |
| 244 | extension_id, |
| 245 | kHasSetScriptOnAllUrlsPrefName, |
| 246 | new base::FundamentalValue(true)); |
rdevlin.cronin | 738501d | 2015-11-05 21:51:36 | [diff] [blame] | 247 | SetAllowedScriptingOnAllUrlsHelper(context, extension_id, allowed, true); |
rdevlin.cronin | bb21358 | 2015-02-10 00:25:55 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
| 251 | bool HasSetAllowedScriptingOnAllUrls(const std::string& extension_id, |
| 252 | content::BrowserContext* context) { |
| 253 | bool did_set = false; |
| 254 | return ExtensionPrefs::Get(context)->ReadPrefAsBoolean( |
| 255 | extension_id, |
| 256 | kHasSetScriptOnAllUrlsPrefName, |
| 257 | &did_set) && did_set; |
[email protected] | b33c8c2 | 2014-05-29 19:51:08 | [diff] [blame] | 258 | } |
| 259 | |
rdevlin.cronin | b8dffe5 | 2015-02-07 00:58:01 | [diff] [blame] | 260 | bool DefaultAllowedScriptingOnAllUrls() { |
| 261 | return !FeatureSwitch::scripts_require_action()->IsEnabled(); |
| 262 | } |
| 263 | |
[email protected] | f5ea096 | 2013-11-22 09:20:47 | [diff] [blame] | 264 | bool IsAppLaunchable(const std::string& extension_id, |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 265 | content::BrowserContext* context) { |
[email protected] | 47e1940 | 2014-06-27 09:01:14 | [diff] [blame] | 266 | int reason = ExtensionPrefs::Get(context)->GetDisableReasons(extension_id); |
| 267 | return !((reason & Extension::DISABLE_UNSUPPORTED_REQUIREMENT) || |
| 268 | (reason & Extension::DISABLE_CORRUPTED)); |
[email protected] | f5ea096 | 2013-11-22 09:20:47 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | bool IsAppLaunchableWithoutEnabling(const std::string& extension_id, |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 272 | content::BrowserContext* context) { |
| 273 | return ExtensionRegistry::Get(context)->GetExtensionById( |
| 274 | extension_id, ExtensionRegistry::ENABLED) != NULL; |
[email protected] | f5ea096 | 2013-11-22 09:20:47 | [diff] [blame] | 275 | } |
| 276 | |
treib | c644a1c | 2015-07-13 08:37:04 | [diff] [blame] | 277 | bool ShouldSync(const Extension* extension, |
| 278 | content::BrowserContext* context) { |
| 279 | return sync_helper::IsSyncable(extension) && |
[email protected] | 658eae5 | 2014-06-14 20:28:05 | [diff] [blame] | 280 | !ExtensionPrefs::Get(context)->DoNotSync(extension->id()); |
| 281 | } |
| 282 | |
[email protected] | 617342a4 | 2013-12-18 23:34:03 | [diff] [blame] | 283 | bool IsExtensionIdle(const std::string& extension_id, |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 284 | content::BrowserContext* context) { |
elijahtaylor | 0def443 | 2014-10-06 18:15:11 | [diff] [blame] | 285 | std::vector<std::string> ids_to_check; |
| 286 | ids_to_check.push_back(extension_id); |
[email protected] | 617342a4 | 2013-12-18 23:34:03 | [diff] [blame] | 287 | |
elijahtaylor | 0def443 | 2014-10-06 18:15:11 | [diff] [blame] | 288 | const Extension* extension = |
| 289 | ExtensionRegistry::Get(context) |
| 290 | ->GetExtensionById(extension_id, ExtensionRegistry::ENABLED); |
| 291 | if (extension && extension->is_shared_module()) { |
| 292 | // We have to check all the extensions that use this shared module for idle |
| 293 | // to tell whether it is really 'idle'. |
| 294 | SharedModuleService* service = ExtensionSystem::Get(context) |
| 295 | ->extension_service() |
| 296 | ->shared_module_service(); |
| 297 | scoped_ptr<ExtensionSet> dependents = |
| 298 | service->GetDependentExtensions(extension); |
| 299 | for (ExtensionSet::const_iterator i = dependents->begin(); |
| 300 | i != dependents->end(); |
| 301 | i++) { |
| 302 | ids_to_check.push_back((*i)->id()); |
| 303 | } |
| 304 | } |
| 305 | |
reillyg | 0ea3fa90 | 2014-10-28 15:30:23 | [diff] [blame] | 306 | ProcessManager* process_manager = ProcessManager::Get(context); |
elijahtaylor | 0def443 | 2014-10-06 18:15:11 | [diff] [blame] | 307 | for (std::vector<std::string>::const_iterator i = ids_to_check.begin(); |
| 308 | i != ids_to_check.end(); |
| 309 | i++) { |
| 310 | const std::string id = (*i); |
| 311 | ExtensionHost* host = process_manager->GetBackgroundHostForExtension(id); |
| 312 | if (host) |
| 313 | return false; |
| 314 | |
rdevlin.cronin | 3d426152 | 2015-02-10 00:48:15 | [diff] [blame] | 315 | scoped_refptr<content::SiteInstance> site_instance = |
elijahtaylor | 0def443 | 2014-10-06 18:15:11 | [diff] [blame] | 316 | process_manager->GetSiteInstanceForURL( |
| 317 | Extension::GetBaseURLFromExtensionId(id)); |
| 318 | if (site_instance && site_instance->HasProcess()) |
| 319 | return false; |
| 320 | |
rdevlin.cronin | 6ae04a01 | 2015-04-03 20:19:40 | [diff] [blame] | 321 | if (!process_manager->GetRenderFrameHostsForExtension(id).empty()) |
elijahtaylor | 0def443 | 2014-10-06 18:15:11 | [diff] [blame] | 322 | return false; |
| 323 | } |
| 324 | return true; |
[email protected] | 617342a4 | 2013-12-18 23:34:03 | [diff] [blame] | 325 | } |
| 326 | |
[email protected] | 3a746ec | 2014-03-15 05:30:56 | [diff] [blame] | 327 | GURL GetSiteForExtensionId(const std::string& extension_id, |
| 328 | content::BrowserContext* context) { |
| 329 | return content::SiteInstance::GetSiteForURL( |
| 330 | context, Extension::GetBaseURLFromExtensionId(extension_id)); |
| 331 | } |
| 332 | |
[email protected] | bb2feea | 2014-03-18 22:08:13 | [diff] [blame] | 333 | scoped_ptr<base::DictionaryValue> GetExtensionInfo(const Extension* extension) { |
| 334 | DCHECK(extension); |
| 335 | scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); |
| 336 | |
| 337 | dict->SetString("id", extension->id()); |
| 338 | dict->SetString("name", extension->name()); |
| 339 | |
| 340 | GURL icon = extensions::ExtensionIconSource::GetIconURL( |
| 341 | extension, |
| 342 | extension_misc::EXTENSION_ICON_SMALLISH, |
| 343 | ExtensionIconSet::MATCH_BIGGER, |
| 344 | false, // Not grayscale. |
| 345 | NULL); // Don't set bool if exists. |
| 346 | dict->SetString("icon", icon.spec()); |
| 347 | |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame^] | 348 | return dict; |
[email protected] | bb2feea | 2014-03-18 22:08:13 | [diff] [blame] | 349 | } |
| 350 | |
[email protected] | 7eb20e3 | 2014-04-30 08:50:56 | [diff] [blame] | 351 | const gfx::ImageSkia& GetDefaultAppIcon() { |
| 352 | return *ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
| 353 | IDR_APP_DEFAULT_ICON); |
| 354 | } |
| 355 | |
| 356 | const gfx::ImageSkia& GetDefaultExtensionIcon() { |
| 357 | return *ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
| 358 | IDR_EXTENSION_DEFAULT_ICON); |
| 359 | } |
| 360 | |
benwells | c431c0ae | 2015-01-27 22:04:06 | [diff] [blame] | 361 | bool IsNewBookmarkAppsEnabled() { |
benwells | cb7bea0e | 2015-04-30 23:21:13 | [diff] [blame] | 362 | return !base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 363 | switches::kDisableNewBookmarkApps); |
benwells | 39f23ae | 2014-08-27 08:01:52 | [diff] [blame] | 364 | } |
| 365 | |
dominickn | 2b10cbd | 2015-08-20 02:09:18 | [diff] [blame] | 366 | bool CanHostedAppsOpenInWindows() { |
| 367 | #if defined(OS_MACOSX) |
| 368 | return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 369 | switches::kEnableHostedAppsInWindows); |
| 370 | #else |
| 371 | return true; |
| 372 | #endif |
| 373 | } |
| 374 | |
treib | b6af28cd | 2015-12-01 11:19:46 | [diff] [blame] | 375 | bool IsExtensionSupervised(const Extension* extension, const Profile* profile) { |
treib | bb9a196 | 2015-02-25 13:40:59 | [diff] [blame] | 376 | return extension->was_installed_by_custodian() && profile->IsSupervised(); |
| 377 | } |
| 378 | |
treib | b6af28cd | 2015-12-01 11:19:46 | [diff] [blame] | 379 | bool NeedCustodianApprovalForPermissionIncrease(const Profile* profile) { |
| 380 | if (!profile->IsSupervised()) |
| 381 | return false; |
| 382 | // Query the trial group name first, to make sure it's properly initialized. |
| 383 | base::FieldTrialList::FindFullName( |
treib | 3202d59 | 2015-07-31 08:33:16 | [diff] [blame] | 384 | kSupervisedUserExtensionPermissionIncreaseFieldTrialName); |
treib | b6af28cd | 2015-12-01 11:19:46 | [diff] [blame] | 385 | std::string value = variations::GetVariationParamValue( |
| 386 | kSupervisedUserExtensionPermissionIncreaseFieldTrialName, |
| 387 | profile->IsChild() ? "child_account" : "legacy_supervised_user"); |
| 388 | return value == "true"; |
treib | 3202d59 | 2015-07-31 08:33:16 | [diff] [blame] | 389 | } |
| 390 | |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 391 | } // namespace util |
| 392 | } // namespace extensions |