blob: b427811ffde2ee32fccd653a504936fe53a60823 [file] [log] [blame]
[email protected]a7ff4b72013-10-17 20:56:021// 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
tbarzic858a8482016-12-07 01:04:507#include <vector>
8
[email protected]a7ff4b72013-10-17 20:56:029#include "base/command_line.h"
tapted70cdc812017-03-23 20:48:1910#include "base/feature_list.h"
[email protected]bb2feea2014-03-18 22:08:1311#include "base/logging.h"
treib3202d592015-07-31 08:33:1612#include "base/metrics/field_trial.h"
[email protected]bb2feea2014-03-18 22:08:1313#include "base/values.h"
avia2f4804a2015-12-24 23:11:1314#include "build/build_config.h"
Matt Giucad4870432017-11-24 01:04:3815#include "chrome/browser/banners/app_banner_manager.h"
[email protected]a7ff4b72013-10-17 20:56:0216#include "chrome/browser/extensions/extension_service.h"
[email protected]f8aefb132013-10-30 09:29:5217#include "chrome/browser/extensions/extension_sync_service.h"
Alan Cutter70927ec92018-03-22 00:58:4918#include "chrome/browser/extensions/launch_util.h"
[email protected]23a85362014-07-07 23:26:1919#include "chrome/browser/extensions/permissions_updater.h"
rdevlin.cronincb9f86e2015-10-15 15:13:4220#include "chrome/browser/extensions/scripting_permissions_modifier.h"
elijahtaylor0def4432014-10-06 18:15:1121#include "chrome/browser/extensions/shared_module_service.h"
[email protected]1d5cf4142014-01-24 18:25:2222#include "chrome/browser/profiles/profile.h"
[email protected]bb2feea2014-03-18 22:08:1323#include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
tapted70cdc812017-03-23 20:48:1924#include "chrome/common/chrome_features.h"
[email protected]a7ff4b72013-10-17 20:56:0225#include "chrome/common/chrome_switches.h"
Alan Cutter70927ec92018-03-22 00:58:4926#include "chrome/common/extensions/api/url_handlers/url_handlers_parser.h"
[email protected]a7ff4b72013-10-17 20:56:0227#include "chrome/common/extensions/sync_helper.h"
treibb6af28cd2015-12-01 11:19:4628#include "components/variations/variations_associated_data.h"
[email protected]617342a42013-12-18 23:34:0329#include "content/public/browser/site_instance.h"
Devlin Croninbffe949eb2018-01-12 03:03:4030#include "extensions/browser/disable_reason.h"
[email protected]489db0842014-01-22 18:20:0331#include "extensions/browser/extension_prefs.h"
[email protected]599539802014-01-07 23:06:0032#include "extensions/browser/extension_registry.h"
[email protected]59b0e602014-01-30 00:41:2433#include "extensions/browser/extension_system.h"
[email protected]30e190f82014-05-26 16:44:3934#include "extensions/browser/extension_util.h"
[email protected]e4452d32013-11-15 23:07:4135#include "extensions/common/extension.h"
[email protected]4b7908842014-04-07 23:50:2236#include "extensions/common/extension_icon_set.h"
[email protected]a7ff4b72013-10-17 20:56:0237#include "extensions/common/manifest.h"
tfarina0bcdf362015-06-29 22:19:2638#include "extensions/common/manifest_handlers/app_isolation_info.h"
[email protected]1f7de252013-11-06 22:02:0039#include "extensions/common/manifest_handlers/incognito_info.h"
[email protected]4d67e9d2014-08-18 22:03:5440#include "extensions/common/permissions/permissions_data.h"
mukai4245dfe82014-09-05 17:40:5141#include "extensions/grit/extensions_browser_resources.h"
[email protected]7eb20e32014-04-30 08:50:5642#include "ui/base/resource/resource_bundle.h"
Michael Giuffrida7efeed142017-06-07 06:29:2143#include "url/gurl.h"
[email protected]a7ff4b72013-10-17 20:56:0244
treib13251192016-06-29 07:13:1545#if defined(OS_CHROMEOS)
46#include "chrome/browser/chromeos/file_manager/app_id.h"
47#endif
48
[email protected]1d5cf4142014-01-24 18:25:2249namespace extensions {
50namespace util {
[email protected]a7ff4b72013-10-17 20:56:0251
[email protected]b33c8c22014-05-29 19:51:0852namespace {
mamir192d7882016-06-22 17:10:1653// The entry into the prefs used to flag an extension as installed by custodian.
54// It is relevant only for supervised users.
55const char kWasInstalledByCustodianPrefName[] = "was_installed_by_custodian";
56
thestig7b4bd932014-09-09 22:44:3157// Returns |extension_id|. See note below.
58std::string ReloadExtensionIfEnabled(const std::string& extension_id,
59 content::BrowserContext* context) {
60 ExtensionRegistry* registry = ExtensionRegistry::Get(context);
61 bool extension_is_enabled =
62 registry->enabled_extensions().Contains(extension_id);
63
64 if (!extension_is_enabled)
65 return extension_id;
66
67 // When we reload the extension the ID may be invalidated if we've passed it
68 // by const ref everywhere. Make a copy to be safe. http://crbug.com/103762
69 std::string id = extension_id;
70 ExtensionService* service =
71 ExtensionSystem::Get(context)->extension_service();
72 CHECK(service);
73 service->ReloadExtension(id);
74 return id;
75}
76
[email protected]277c4142014-06-19 20:08:5477} // namespace
[email protected]b33c8c22014-05-29 19:51:0878
[email protected]a7ff4b72013-10-17 20:56:0279void SetIsIncognitoEnabled(const std::string& extension_id,
[email protected]1d5cf4142014-01-24 18:25:2280 content::BrowserContext* context,
[email protected]a7ff4b72013-10-17 20:56:0281 bool enabled) {
thestig7b4bd932014-09-09 22:44:3182 ExtensionRegistry* registry = ExtensionRegistry::Get(context);
83 const Extension* extension =
84 registry->GetExtensionById(extension_id, ExtensionRegistry::EVERYTHING);
[email protected]a7ff4b72013-10-17 20:56:0285
[email protected]1d5cf4142014-01-24 18:25:2286 if (extension) {
kundajie548e7442015-09-18 23:19:0987 if (!util::CanBeIncognitoEnabled(extension))
[email protected]1d5cf4142014-01-24 18:25:2288 return;
89
treibc644a1c2015-07-13 08:37:0490 // TODO(treib,kalman): Should this be Manifest::IsComponentLocation(..)?
91 // (which also checks for EXTERNAL_COMPONENT).
[email protected]1d5cf4142014-01-24 18:25:2292 if (extension->location() == Manifest::COMPONENT) {
93 // This shouldn't be called for component extensions unless it is called
94 // by sync, for syncable component extensions.
95 // See http://crbug.com/112290 and associated CLs for the sordid history.
treib13251192016-06-29 07:13:1596 bool syncable = sync_helper::IsSyncableComponentExtension(extension);
97#if defined(OS_CHROMEOS)
98 // For some users, the file manager app somehow ended up being synced even
99 // though it's supposed to be unsyncable; see crbug.com/576964. If the bad
100 // data ever gets cleaned up, this hack should be removed.
101 syncable = syncable || extension->id() == file_manager::kFileManagerAppId;
102#endif
103 DCHECK(syncable);
[email protected]1d5cf4142014-01-24 18:25:22104
105 // If we are here, make sure the we aren't trying to change the value.
thestig7b4bd932014-09-09 22:44:31106 DCHECK_EQ(enabled, IsIncognitoEnabled(extension_id, context));
[email protected]1d5cf4142014-01-24 18:25:22107 return;
108 }
[email protected]a7ff4b72013-10-17 20:56:02109 }
110
thestig7b4bd932014-09-09 22:44:31111 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(context);
[email protected]a7ff4b72013-10-17 20:56:02112 // Broadcast unloaded and loaded events to update browser state. Only bother
113 // if the value changed and the extension is actually enabled, since there is
114 // no UI otherwise.
115 bool old_enabled = extension_prefs->IsIncognitoEnabled(extension_id);
116 if (enabled == old_enabled)
117 return;
118
119 extension_prefs->SetIsIncognitoEnabled(extension_id, enabled);
120
thestig7b4bd932014-09-09 22:44:31121 std::string id = ReloadExtensionIfEnabled(extension_id, context);
[email protected]a7ff4b72013-10-17 20:56:02122
123 // Reloading the extension invalidates the |extension| pointer.
thestig7b4bd932014-09-09 22:44:31124 extension = registry->GetExtensionById(id, ExtensionRegistry::EVERYTHING);
[email protected]f8aefb132013-10-30 09:29:52125 if (extension) {
thestig7b4bd932014-09-09 22:44:31126 Profile* profile = Profile::FromBrowserContext(context);
127 ExtensionSyncService::Get(profile)->SyncExtensionChangeIfNeeded(*extension);
[email protected]f8aefb132013-10-30 09:29:52128 }
[email protected]a7ff4b72013-10-17 20:56:02129}
130
131bool CanCrossIncognito(const Extension* extension,
[email protected]1d5cf4142014-01-24 18:25:22132 content::BrowserContext* context) {
[email protected]a7ff4b72013-10-17 20:56:02133 // We allow the extension to see events and data from another profile iff it
134 // uses "spanning" behavior and it has incognito access. "split" mode
135 // extensions only see events for a matching profile.
136 CHECK(extension);
[email protected]1d5cf4142014-01-24 18:25:22137 return IsIncognitoEnabled(extension->id(), context) &&
138 !IncognitoInfo::IsSplitMode(extension);
[email protected]a7ff4b72013-10-17 20:56:02139}
140
141bool CanLoadInIncognito(const Extension* extension,
[email protected]1d5cf4142014-01-24 18:25:22142 content::BrowserContext* context) {
143 CHECK(extension);
[email protected]a7ff4b72013-10-17 20:56:02144 if (extension->is_hosted_app())
145 return true;
146 // Packaged apps and regular extensions need to be enabled specifically for
147 // incognito (and split mode should be set).
[email protected]1d5cf4142014-01-24 18:25:22148 return IncognitoInfo::IsSplitMode(extension) &&
149 IsIncognitoEnabled(extension->id(), context);
[email protected]a7ff4b72013-10-17 20:56:02150}
151
[email protected]1d5cf4142014-01-24 18:25:22152bool AllowFileAccess(const std::string& extension_id,
153 content::BrowserContext* context) {
avi3ef9ec9e2014-12-22 22:50:17154 return base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]1d5cf4142014-01-24 18:25:22155 switches::kDisableExtensionsFileAccessCheck) ||
156 ExtensionPrefs::Get(context)->AllowFileAccess(extension_id);
[email protected]a7ff4b72013-10-17 20:56:02157}
158
[email protected]1d5cf4142014-01-24 18:25:22159void SetAllowFileAccess(const std::string& extension_id,
160 content::BrowserContext* context,
[email protected]a7ff4b72013-10-17 20:56:02161 bool allow) {
162 // Reload to update browser state. Only bother if the value changed and the
163 // extension is actually enabled, since there is no UI otherwise.
[email protected]1d5cf4142014-01-24 18:25:22164 if (allow == AllowFileAccess(extension_id, context))
[email protected]a7ff4b72013-10-17 20:56:02165 return;
166
[email protected]7c82539c2014-02-19 06:09:17167 ExtensionPrefs::Get(context)->SetAllowFileAccess(extension_id, allow);
[email protected]a7ff4b72013-10-17 20:56:02168
thestig7b4bd932014-09-09 22:44:31169 ReloadExtensionIfEnabled(extension_id, context);
[email protected]a7ff4b72013-10-17 20:56:02170}
171
mamir192d7882016-06-22 17:10:16172void SetWasInstalledByCustodian(const std::string& extension_id,
173 content::BrowserContext* context,
174 bool installed_by_custodian) {
175 if (installed_by_custodian == WasInstalledByCustodian(extension_id, context))
176 return;
177
Karan Bhatia2a117232017-08-23 00:24:56178 ExtensionPrefs* prefs = ExtensionPrefs::Get(context);
179
180 prefs->UpdateExtensionPref(
mamir192d7882016-06-22 17:10:16181 extension_id, kWasInstalledByCustodianPrefName,
Jinho Bangb5216cec2018-01-17 19:43:11182 installed_by_custodian ? std::make_unique<base::Value>(true) : nullptr);
mamir192d7882016-06-22 17:10:16183 ExtensionService* service =
184 ExtensionSystem::Get(context)->extension_service();
185
mamir192d7882016-06-22 17:10:16186 if (!installed_by_custodian) {
187 // If installed_by_custodian changes to false, the extension may need to
188 // be unloaded now.
189 service->ReloadExtension(extension_id);
190 return;
191 }
192
mamire9609642016-06-28 22:17:54193 ExtensionRegistry* registry = ExtensionRegistry::Get(context);
mamir192d7882016-06-22 17:10:16194 // If it is already enabled, do nothing.
195 if (registry->enabled_extensions().Contains(extension_id))
196 return;
197
Karan Bhatia2a117232017-08-23 00:24:56198 // If the extension was disabled due to management policy, try to re-enable
199 // it. Example is a pre-installed extension that was disabled when a
mamir192d7882016-06-22 17:10:16200 // supervised user flag has been received.
Karan Bhatia2a117232017-08-23 00:24:56201 // Note: EnableExtension will fail if the extension still needs to be disabled
202 // due to manangement policy.
203 if (registry->disabled_extensions().Contains(extension_id) &&
204 prefs->GetDisableReasons(extension_id) ==
205 disable_reason::DISABLE_BLOCKED_BY_POLICY) {
206 service->EnableExtension(extension_id);
mamir192d7882016-06-22 17:10:16207 }
208}
209
210bool WasInstalledByCustodian(const std::string& extension_id,
211 content::BrowserContext* context) {
212 bool installed_by_custodian = false;
213 ExtensionPrefs* prefs = ExtensionPrefs::Get(context);
214 prefs->ReadPrefAsBoolean(extension_id, kWasInstalledByCustodianPrefName,
215 &installed_by_custodian);
216 return installed_by_custodian;
217}
218
[email protected]f5ea0962013-11-22 09:20:47219bool IsAppLaunchable(const std::string& extension_id,
[email protected]1d5cf4142014-01-24 18:25:22220 content::BrowserContext* context) {
[email protected]47e19402014-06-27 09:01:14221 int reason = ExtensionPrefs::Get(context)->GetDisableReasons(extension_id);
Minh X. Nguyen45479012017-08-18 21:35:36222 return !((reason & disable_reason::DISABLE_UNSUPPORTED_REQUIREMENT) ||
223 (reason & disable_reason::DISABLE_CORRUPTED));
[email protected]f5ea0962013-11-22 09:20:47224}
225
226bool IsAppLaunchableWithoutEnabling(const std::string& extension_id,
[email protected]1d5cf4142014-01-24 18:25:22227 content::BrowserContext* context) {
228 return ExtensionRegistry::Get(context)->GetExtensionById(
229 extension_id, ExtensionRegistry::ENABLED) != NULL;
[email protected]f5ea0962013-11-22 09:20:47230}
231
treibc644a1c2015-07-13 08:37:04232bool ShouldSync(const Extension* extension,
233 content::BrowserContext* context) {
234 return sync_helper::IsSyncable(extension) &&
[email protected]658eae52014-06-14 20:28:05235 !ExtensionPrefs::Get(context)->DoNotSync(extension->id());
236}
237
[email protected]617342a42013-12-18 23:34:03238bool IsExtensionIdle(const std::string& extension_id,
[email protected]1d5cf4142014-01-24 18:25:22239 content::BrowserContext* context) {
elijahtaylor0def4432014-10-06 18:15:11240 std::vector<std::string> ids_to_check;
241 ids_to_check.push_back(extension_id);
[email protected]617342a42013-12-18 23:34:03242
elijahtaylor0def4432014-10-06 18:15:11243 const Extension* extension =
244 ExtensionRegistry::Get(context)
245 ->GetExtensionById(extension_id, ExtensionRegistry::ENABLED);
246 if (extension && extension->is_shared_module()) {
247 // We have to check all the extensions that use this shared module for idle
248 // to tell whether it is really 'idle'.
249 SharedModuleService* service = ExtensionSystem::Get(context)
250 ->extension_service()
251 ->shared_module_service();
dchengc963c7142016-04-08 03:55:22252 std::unique_ptr<ExtensionSet> dependents =
elijahtaylor0def4432014-10-06 18:15:11253 service->GetDependentExtensions(extension);
254 for (ExtensionSet::const_iterator i = dependents->begin();
255 i != dependents->end();
256 i++) {
257 ids_to_check.push_back((*i)->id());
258 }
259 }
260
reillyg0ea3fa902014-10-28 15:30:23261 ProcessManager* process_manager = ProcessManager::Get(context);
elijahtaylor0def4432014-10-06 18:15:11262 for (std::vector<std::string>::const_iterator i = ids_to_check.begin();
263 i != ids_to_check.end();
264 i++) {
265 const std::string id = (*i);
266 ExtensionHost* host = process_manager->GetBackgroundHostForExtension(id);
267 if (host)
268 return false;
269
rdevlin.cronin3d4261522015-02-10 00:48:15270 scoped_refptr<content::SiteInstance> site_instance =
elijahtaylor0def4432014-10-06 18:15:11271 process_manager->GetSiteInstanceForURL(
272 Extension::GetBaseURLFromExtensionId(id));
273 if (site_instance && site_instance->HasProcess())
274 return false;
275
rdevlin.cronin6ae04a012015-04-03 20:19:40276 if (!process_manager->GetRenderFrameHostsForExtension(id).empty())
elijahtaylor0def4432014-10-06 18:15:11277 return false;
278 }
279 return true;
[email protected]617342a42013-12-18 23:34:03280}
281
dchengc963c7142016-04-08 03:55:22282std::unique_ptr<base::DictionaryValue> GetExtensionInfo(
283 const Extension* extension) {
[email protected]bb2feea2014-03-18 22:08:13284 DCHECK(extension);
dchengc963c7142016-04-08 03:55:22285 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
[email protected]bb2feea2014-03-18 22:08:13286
287 dict->SetString("id", extension->id());
288 dict->SetString("name", extension->name());
289
290 GURL icon = extensions::ExtensionIconSource::GetIconURL(
estade32426e02016-12-18 01:26:17291 extension, extension_misc::EXTENSION_ICON_SMALLISH,
[email protected]bb2feea2014-03-18 22:08:13292 ExtensionIconSet::MATCH_BIGGER,
estade32426e02016-12-18 01:26:17293 false); // Not grayscale.
[email protected]bb2feea2014-03-18 22:08:13294 dict->SetString("icon", icon.spec());
295
dcheng1fc00f12015-12-26 22:18:03296 return dict;
[email protected]bb2feea2014-03-18 22:08:13297}
298
[email protected]7eb20e32014-04-30 08:50:56299const gfx::ImageSkia& GetDefaultAppIcon() {
Lei Zhang7640d542017-10-03 16:26:49300 return *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
[email protected]7eb20e32014-04-30 08:50:56301 IDR_APP_DEFAULT_ICON);
302}
303
304const gfx::ImageSkia& GetDefaultExtensionIcon() {
Lei Zhang7640d542017-10-03 16:26:49305 return *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
[email protected]7eb20e32014-04-30 08:50:56306 IDR_EXTENSION_DEFAULT_ICON);
307}
308
benwellsc431c0ae2015-01-27 22:04:06309bool IsNewBookmarkAppsEnabled() {
dominicknfa0e3df2016-01-14 05:38:32310#if defined(OS_MACOSX)
benwellsc421ccdb2017-06-28 05:10:32311 return base::FeatureList::IsEnabled(features::kBookmarkApps) ||
benwells0eee2292017-06-28 08:41:22312 base::FeatureList::IsEnabled(features::kAppBanners) ||
Matt Giucad4870432017-11-24 01:04:38313 banners::AppBannerManager::IsExperimentalAppBannersEnabled();
dominicknfa0e3df2016-01-14 05:38:32314#else
tapted70cdc812017-03-23 20:48:19315 return true;
dominicknfa0e3df2016-01-14 05:38:32316#endif
benwells39f23ae2014-08-27 08:01:52317}
318
dominickn2b10cbd2015-08-20 02:09:18319bool CanHostedAppsOpenInWindows() {
320#if defined(OS_MACOSX)
321 return base::CommandLine::ForCurrentProcess()->HasSwitch(
Giovanni Ortuño Urquidi45020e232017-07-12 06:10:17322 switches::kEnableHostedAppsInWindows) ||
323 base::FeatureList::IsEnabled(features::kDesktopPWAWindowing);
dominickn2b10cbd2015-08-20 02:09:18324#else
325 return true;
326#endif
327}
328
mamir192d7882016-06-22 17:10:16329bool IsExtensionSupervised(const Extension* extension, Profile* profile) {
330 return WasInstalledByCustodian(extension->id(), profile) &&
331 profile->IsSupervised();
treibbb9a1962015-02-25 13:40:59332}
333
Alan Cutter70927ec92018-03-22 00:58:49334const Extension* GetInstalledPwaForUrl(
335 content::BrowserContext* context,
336 const GURL& url,
337 base::Optional<LaunchContainer> launch_container_filter) {
338 const ExtensionPrefs* prefs = ExtensionPrefs::Get(context);
339 for (scoped_refptr<const Extension> app :
340 ExtensionRegistry::Get(context)->enabled_extensions()) {
341 if (!app->from_bookmark())
342 continue;
343 if (launch_container_filter &&
344 GetLaunchContainer(prefs, app.get()) != *launch_container_filter) {
345 continue;
346 }
347 if (UrlHandlers::FindMatchingUrlHandler(app.get(), url))
348 return app.get();
349 }
350 return nullptr;
351}
352
[email protected]1d5cf4142014-01-24 18:25:22353} // namespace util
354} // namespace extensions