[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" |
| 9 | #include "base/values.h" |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 10 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 11 | #include "chrome/browser/extensions/extension_sync_service.h" |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 12 | #include "chrome/browser/profiles/profile.h" |
[email protected] | bb2feea | 2014-03-18 22:08:13 | [diff] [blame] | 13 | #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 14 | #include "chrome/common/chrome_switches.h" |
[email protected] | ca033634 | 2014-03-21 12:58:34 | [diff] [blame] | 15 | #include "chrome/common/extensions/manifest_handlers/app_isolation_info.h" |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 16 | #include "chrome/common/extensions/sync_helper.h" |
[email protected] | 617342a4 | 2013-12-18 23:34:03 | [diff] [blame] | 17 | #include "content/public/browser/site_instance.h" |
[email protected] | 489db084 | 2014-01-22 18:20:03 | [diff] [blame] | 18 | #include "extensions/browser/extension_prefs.h" |
[email protected] | 59953980 | 2014-01-07 23:06:00 | [diff] [blame] | 19 | #include "extensions/browser/extension_registry.h" |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 20 | #include "extensions/browser/extension_system.h" |
[email protected] | 30e190f8 | 2014-05-26 16:44:39 | [diff] [blame] | 21 | #include "extensions/browser/extension_util.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 22 | #include "extensions/common/extension.h" |
[email protected] | 4b790884 | 2014-04-07 23:50:22 | [diff] [blame] | 23 | #include "extensions/common/extension_icon_set.h" |
[email protected] | 277c414 | 2014-06-19 20:08:54 | [diff] [blame] | 24 | #include "extensions/common/features/simple_feature.h" |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 25 | #include "extensions/common/manifest.h" |
[email protected] | 1f7de25 | 2013-11-06 22:02:00 | [diff] [blame] | 26 | #include "extensions/common/manifest_handlers/incognito_info.h" |
[email protected] | 7eb20e3 | 2014-04-30 08:50:56 | [diff] [blame] | 27 | #include "grit/theme_resources.h" |
| 28 | #include "ui/base/resource/resource_bundle.h" |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 29 | |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 30 | namespace extensions { |
| 31 | namespace util { |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 32 | |
[email protected] | b33c8c2 | 2014-05-29 19:51:08 | [diff] [blame] | 33 | namespace { |
| 34 | // The entry into the ExtensionPrefs for allowing an extension to script on |
| 35 | // all urls without explicit permission. |
| 36 | const char kExtensionAllowedOnAllUrlsPrefName[] = |
| 37 | "extension_can_script_all_urls"; |
[email protected] | 277c414 | 2014-06-19 20:08:54 | [diff] [blame] | 38 | |
| 39 | // Returns true if |extension_id| for an external component extension should |
| 40 | // always be enabled in incognito windows. |
| 41 | bool IsWhitelistedForIncognito(const std::string& extension_id) { |
| 42 | static const char* kExtensionWhitelist[] = { |
| 43 | "D5736E4B5CF695CB93A2FB57E4FDC6E5AFAB6FE2", // http://crbug.com/312900 |
| 44 | "D57DE394F36DC1C3220E7604C575D29C51A6C495", // http://crbug.com/319444 |
| 45 | "3F65507A3B39259B38C8173C6FFA3D12DF64CCE9" // http://crbug.com/371562 |
| 46 | }; |
| 47 | |
| 48 | return extensions::SimpleFeature::IsIdInList( |
| 49 | extension_id, |
| 50 | std::set<std::string>( |
| 51 | kExtensionWhitelist, |
| 52 | kExtensionWhitelist + arraysize(kExtensionWhitelist))); |
[email protected] | b33c8c2 | 2014-05-29 19:51:08 | [diff] [blame] | 53 | } |
[email protected] | 277c414 | 2014-06-19 20:08:54 | [diff] [blame] | 54 | } // namespace |
[email protected] | b33c8c2 | 2014-05-29 19:51:08 | [diff] [blame] | 55 | |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 56 | bool IsIncognitoEnabled(const std::string& extension_id, |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 57 | content::BrowserContext* context) { |
| 58 | const Extension* extension = ExtensionRegistry::Get(context)-> |
| 59 | GetExtensionById(extension_id, ExtensionRegistry::ENABLED); |
| 60 | if (extension) { |
| 61 | if (!extension->can_be_incognito_enabled()) |
| 62 | return false; |
| 63 | // If this is an existing component extension we always allow it to |
| 64 | // work in incognito mode. |
| 65 | if (extension->location() == Manifest::COMPONENT) |
| 66 | return true; |
[email protected] | 277c414 | 2014-06-19 20:08:54 | [diff] [blame] | 67 | if (extension->location() == Manifest::EXTERNAL_COMPONENT && |
| 68 | IsWhitelistedForIncognito(extension_id)) { |
| 69 | return true; |
| 70 | } |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 71 | } |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 72 | |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 73 | return ExtensionPrefs::Get(context)->IsIncognitoEnabled(extension_id); |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void SetIsIncognitoEnabled(const std::string& extension_id, |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 77 | content::BrowserContext* context, |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 78 | bool enabled) { |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 79 | ExtensionService* service = |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 80 | ExtensionSystem::Get(context)->extension_service(); |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 81 | CHECK(service); |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 82 | const Extension* extension = service->GetInstalledExtension(extension_id); |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 83 | |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 84 | if (extension) { |
| 85 | if (!extension->can_be_incognito_enabled()) |
| 86 | return; |
| 87 | |
| 88 | if (extension->location() == Manifest::COMPONENT) { |
| 89 | // This shouldn't be called for component extensions unless it is called |
| 90 | // by sync, for syncable component extensions. |
| 91 | // See http://crbug.com/112290 and associated CLs for the sordid history. |
| 92 | DCHECK(sync_helper::IsSyncable(extension)); |
| 93 | |
| 94 | // If we are here, make sure the we aren't trying to change the value. |
| 95 | DCHECK_EQ(enabled, IsIncognitoEnabled(extension_id, service->profile())); |
| 96 | return; |
| 97 | } |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 98 | } |
| 99 | |
[email protected] | 7c82539c | 2014-02-19 06:09:17 | [diff] [blame] | 100 | ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(service->profile()); |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 101 | // Broadcast unloaded and loaded events to update browser state. Only bother |
| 102 | // if the value changed and the extension is actually enabled, since there is |
| 103 | // no UI otherwise. |
| 104 | bool old_enabled = extension_prefs->IsIncognitoEnabled(extension_id); |
| 105 | if (enabled == old_enabled) |
| 106 | return; |
| 107 | |
| 108 | extension_prefs->SetIsIncognitoEnabled(extension_id, enabled); |
| 109 | |
| 110 | bool extension_is_enabled = service->extensions()->Contains(extension_id); |
| 111 | |
| 112 | // When we reload the extension the ID may be invalidated if we've passed it |
| 113 | // by const ref everywhere. Make a copy to be safe. |
| 114 | std::string id = extension_id; |
| 115 | if (extension_is_enabled) |
| 116 | service->ReloadExtension(id); |
| 117 | |
| 118 | // Reloading the extension invalidates the |extension| pointer. |
| 119 | extension = service->GetInstalledExtension(id); |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 120 | if (extension) { |
| 121 | ExtensionSyncService::Get(service->profile())-> |
| 122 | SyncExtensionChangeIfNeeded(*extension); |
| 123 | } |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | bool CanCrossIncognito(const Extension* extension, |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 127 | content::BrowserContext* context) { |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 128 | // We allow the extension to see events and data from another profile iff it |
| 129 | // uses "spanning" behavior and it has incognito access. "split" mode |
| 130 | // extensions only see events for a matching profile. |
| 131 | CHECK(extension); |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 132 | return IsIncognitoEnabled(extension->id(), context) && |
| 133 | !IncognitoInfo::IsSplitMode(extension); |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | bool CanLoadInIncognito(const Extension* extension, |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 137 | content::BrowserContext* context) { |
| 138 | CHECK(extension); |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 139 | if (extension->is_hosted_app()) |
| 140 | return true; |
| 141 | // Packaged apps and regular extensions need to be enabled specifically for |
| 142 | // incognito (and split mode should be set). |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 143 | return IncognitoInfo::IsSplitMode(extension) && |
| 144 | IsIncognitoEnabled(extension->id(), context); |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 145 | } |
| 146 | |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 147 | bool AllowFileAccess(const std::string& extension_id, |
| 148 | content::BrowserContext* context) { |
| 149 | return CommandLine::ForCurrentProcess()->HasSwitch( |
| 150 | switches::kDisableExtensionsFileAccessCheck) || |
| 151 | ExtensionPrefs::Get(context)->AllowFileAccess(extension_id); |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 152 | } |
| 153 | |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 154 | void SetAllowFileAccess(const std::string& extension_id, |
| 155 | content::BrowserContext* context, |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 156 | bool allow) { |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 157 | ExtensionService* service = |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 158 | ExtensionSystem::Get(context)->extension_service(); |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 159 | CHECK(service); |
| 160 | |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 161 | // Reload to update browser state. Only bother if the value changed and the |
| 162 | // extension is actually enabled, since there is no UI otherwise. |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 163 | if (allow == AllowFileAccess(extension_id, context)) |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 164 | return; |
| 165 | |
[email protected] | 7c82539c | 2014-02-19 06:09:17 | [diff] [blame] | 166 | ExtensionPrefs::Get(context)->SetAllowFileAccess(extension_id, allow); |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 167 | |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 168 | bool extension_is_enabled = service->extensions()->Contains(extension_id); |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 169 | if (extension_is_enabled) |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 170 | service->ReloadExtension(extension_id); |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 171 | } |
| 172 | |
[email protected] | b33c8c2 | 2014-05-29 19:51:08 | [diff] [blame] | 173 | bool AllowedScriptingOnAllUrls(const std::string& extension_id, |
| 174 | content::BrowserContext* context) { |
| 175 | bool allowed = false; |
| 176 | return ExtensionPrefs::Get(context)->ReadPrefAsBoolean( |
| 177 | extension_id, |
| 178 | kExtensionAllowedOnAllUrlsPrefName, |
| 179 | &allowed) && |
| 180 | allowed; |
| 181 | } |
| 182 | |
| 183 | void SetAllowedScriptingOnAllUrls(const std::string& extension_id, |
| 184 | content::BrowserContext* context, |
| 185 | bool allowed) { |
| 186 | ExtensionPrefs::Get(context)->UpdateExtensionPref( |
| 187 | extension_id, |
| 188 | kExtensionAllowedOnAllUrlsPrefName, |
| 189 | allowed ? new base::FundamentalValue(true) : NULL); |
| 190 | } |
| 191 | |
[email protected] | f5ea096 | 2013-11-22 09:20:47 | [diff] [blame] | 192 | bool IsAppLaunchable(const std::string& extension_id, |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 193 | content::BrowserContext* context) { |
[email protected] | 47e1940 | 2014-06-27 09:01:14 | [diff] [blame^] | 194 | int reason = ExtensionPrefs::Get(context)->GetDisableReasons(extension_id); |
| 195 | return !((reason & Extension::DISABLE_UNSUPPORTED_REQUIREMENT) || |
| 196 | (reason & Extension::DISABLE_CORRUPTED)); |
[email protected] | f5ea096 | 2013-11-22 09:20:47 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | bool IsAppLaunchableWithoutEnabling(const std::string& extension_id, |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 200 | content::BrowserContext* context) { |
| 201 | return ExtensionRegistry::Get(context)->GetExtensionById( |
| 202 | extension_id, ExtensionRegistry::ENABLED) != NULL; |
[email protected] | f5ea096 | 2013-11-22 09:20:47 | [diff] [blame] | 203 | } |
| 204 | |
[email protected] | 658eae5 | 2014-06-14 20:28:05 | [diff] [blame] | 205 | bool ShouldSyncExtension(const Extension* extension, |
| 206 | content::BrowserContext* context) { |
| 207 | return sync_helper::IsSyncableExtension(extension) && |
| 208 | !ExtensionPrefs::Get(context)->DoNotSync(extension->id()); |
| 209 | } |
| 210 | |
[email protected] | 30e190f8 | 2014-05-26 16:44:39 | [diff] [blame] | 211 | bool ShouldSyncApp(const Extension* app, content::BrowserContext* context) { |
| 212 | return sync_helper::IsSyncableApp(app) && |
[email protected] | 658eae5 | 2014-06-14 20:28:05 | [diff] [blame] | 213 | !util::IsEphemeralApp(app->id(), context) && |
| 214 | !ExtensionPrefs::Get(context)->DoNotSync(app->id()); |
[email protected] | 30e190f8 | 2014-05-26 16:44:39 | [diff] [blame] | 215 | } |
| 216 | |
[email protected] | 617342a4 | 2013-12-18 23:34:03 | [diff] [blame] | 217 | bool IsExtensionIdle(const std::string& extension_id, |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 218 | content::BrowserContext* context) { |
| 219 | ProcessManager* process_manager = |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 220 | ExtensionSystem::Get(context)->process_manager(); |
[email protected] | 617342a4 | 2013-12-18 23:34:03 | [diff] [blame] | 221 | DCHECK(process_manager); |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 222 | ExtensionHost* host = |
[email protected] | 617342a4 | 2013-12-18 23:34:03 | [diff] [blame] | 223 | process_manager->GetBackgroundHostForExtension(extension_id); |
| 224 | if (host) |
| 225 | return false; |
| 226 | |
| 227 | content::SiteInstance* site_instance = process_manager->GetSiteInstanceForURL( |
| 228 | Extension::GetBaseURLFromExtensionId(extension_id)); |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 229 | if (site_instance && site_instance->HasProcess()) |
[email protected] | 617342a4 | 2013-12-18 23:34:03 | [diff] [blame] | 230 | return false; |
[email protected] | 617342a4 | 2013-12-18 23:34:03 | [diff] [blame] | 231 | |
| 232 | return process_manager->GetRenderViewHostsForExtension(extension_id).empty(); |
| 233 | } |
| 234 | |
[email protected] | 3a746ec | 2014-03-15 05:30:56 | [diff] [blame] | 235 | GURL GetSiteForExtensionId(const std::string& extension_id, |
| 236 | content::BrowserContext* context) { |
| 237 | return content::SiteInstance::GetSiteForURL( |
| 238 | context, Extension::GetBaseURLFromExtensionId(extension_id)); |
| 239 | } |
| 240 | |
[email protected] | bb2feea | 2014-03-18 22:08:13 | [diff] [blame] | 241 | scoped_ptr<base::DictionaryValue> GetExtensionInfo(const Extension* extension) { |
| 242 | DCHECK(extension); |
| 243 | scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); |
| 244 | |
| 245 | dict->SetString("id", extension->id()); |
| 246 | dict->SetString("name", extension->name()); |
| 247 | |
| 248 | GURL icon = extensions::ExtensionIconSource::GetIconURL( |
| 249 | extension, |
| 250 | extension_misc::EXTENSION_ICON_SMALLISH, |
| 251 | ExtensionIconSet::MATCH_BIGGER, |
| 252 | false, // Not grayscale. |
| 253 | NULL); // Don't set bool if exists. |
| 254 | dict->SetString("icon", icon.spec()); |
| 255 | |
| 256 | return dict.Pass(); |
| 257 | } |
| 258 | |
[email protected] | ca033634 | 2014-03-21 12:58:34 | [diff] [blame] | 259 | bool HasIsolatedStorage(const ExtensionInfo& info) { |
| 260 | if (!info.extension_manifest.get()) |
| 261 | return false; |
| 262 | |
| 263 | std::string error; |
| 264 | scoped_refptr<const Extension> extension(Extension::Create( |
| 265 | info.extension_path, |
| 266 | info.extension_location, |
| 267 | *info.extension_manifest, |
| 268 | Extension::NO_FLAGS, |
| 269 | info.extension_id, |
| 270 | &error)); |
| 271 | if (!extension.get()) |
| 272 | return false; |
| 273 | |
| 274 | return AppIsolationInfo::HasIsolatedStorage(extension.get()); |
| 275 | } |
| 276 | |
| 277 | bool SiteHasIsolatedStorage(const GURL& extension_site_url, |
| 278 | content::BrowserContext* context) { |
| 279 | const Extension* extension = ExtensionRegistry::Get(context)-> |
| 280 | enabled_extensions().GetExtensionOrAppByURL(extension_site_url); |
[email protected] | 9a87b6c | 2014-06-18 07:32:10 | [diff] [blame] | 281 | if (!extension) |
| 282 | return false; |
[email protected] | ca033634 | 2014-03-21 12:58:34 | [diff] [blame] | 283 | |
[email protected] | 9a87b6c | 2014-06-18 07:32:10 | [diff] [blame] | 284 | return AppIsolationInfo::HasIsolatedStorage(extension); |
[email protected] | ca033634 | 2014-03-21 12:58:34 | [diff] [blame] | 285 | } |
| 286 | |
[email protected] | 7eb20e3 | 2014-04-30 08:50:56 | [diff] [blame] | 287 | const gfx::ImageSkia& GetDefaultAppIcon() { |
| 288 | return *ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
| 289 | IDR_APP_DEFAULT_ICON); |
| 290 | } |
| 291 | |
| 292 | const gfx::ImageSkia& GetDefaultExtensionIcon() { |
| 293 | return *ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
| 294 | IDR_EXTENSION_DEFAULT_ICON); |
| 295 | } |
| 296 | |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 297 | } // namespace util |
| 298 | } // namespace extensions |