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