blob: 7f629e16b428c96ec482375e75626165c6b849f4 [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
Hans Wennborg1790e6b2020-04-24 19:10:339#include "base/check_op.h"
[email protected]a7ff4b72013-10-17 20:56:0210#include "base/command_line.h"
tapted70cdc812017-03-23 20:48:1911#include "base/feature_list.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"
Yuta Hijikata1290fee22020-11-25 09:46:2815#include "build/chromeos_buildflags.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"
Alan Cutter09965802018-03-27 07:25:2923#include "chrome/browser/ui/browser.h"
[email protected]bb2feea2014-03-18 22:08:1324#include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
Ben Wellse13d31692018-08-31 00:22:3325#include "chrome/browser/web_applications/extensions/bookmark_app_util.h"
[email protected]a7ff4b72013-10-17 20:56:0226#include "chrome/common/chrome_switches.h"
Alan Cutter70927ec92018-03-22 00:58:4927#include "chrome/common/extensions/api/url_handlers/url_handlers_parser.h"
[email protected]a7ff4b72013-10-17 20:56:0228#include "chrome/common/extensions/sync_helper.h"
treibb6af28cd2015-12-01 11:19:4629#include "components/variations/variations_associated_data.h"
Evan Stade89158192021-01-27 18:45:2330#include "components/webapps/browser/banners/app_banner_manager.h"
[email protected]617342a42013-12-18 23:34:0331#include "content/public/browser/site_instance.h"
Devlin Croninbffe949eb2018-01-12 03:03:4032#include "extensions/browser/disable_reason.h"
[email protected]489db0842014-01-22 18:20:0333#include "extensions/browser/extension_prefs.h"
[email protected]599539802014-01-07 23:06:0034#include "extensions/browser/extension_registry.h"
[email protected]59b0e602014-01-30 00:41:2435#include "extensions/browser/extension_system.h"
[email protected]30e190f82014-05-26 16:44:3936#include "extensions/browser/extension_util.h"
[email protected]e4452d32013-11-15 23:07:4137#include "extensions/common/extension.h"
[email protected]4b7908842014-04-07 23:50:2238#include "extensions/common/extension_icon_set.h"
Swapnilf6f2f1d2021-01-14 18:50:4339#include "extensions/common/extension_urls.h"
[email protected]a7ff4b72013-10-17 20:56:0240#include "extensions/common/manifest.h"
tfarina0bcdf362015-06-29 22:19:2641#include "extensions/common/manifest_handlers/app_isolation_info.h"
[email protected]1f7de252013-11-06 22:02:0042#include "extensions/common/manifest_handlers/incognito_info.h"
Danan S5f430882020-01-14 04:52:5543#include "extensions/common/manifest_handlers/permissions_parser.h"
[email protected]4d67e9d2014-08-18 22:03:5444#include "extensions/common/permissions/permissions_data.h"
mukai4245dfe82014-09-05 17:40:5145#include "extensions/grit/extensions_browser_resources.h"
[email protected]7eb20e32014-04-30 08:50:5646#include "ui/base/resource/resource_bundle.h"
Michael Giuffrida7efeed142017-06-07 06:29:2147#include "url/gurl.h"
[email protected]a7ff4b72013-10-17 20:56:0248
Yuta Hijikata1290fee22020-11-25 09:46:2849#if BUILDFLAG(IS_CHROMEOS_ASH)
treib13251192016-06-29 07:13:1550#include "chrome/browser/chromeos/file_manager/app_id.h"
Pavol Marko41c37b12019-08-07 10:56:3251#include "chrome/browser/chromeos/profiles/profile_helper.h"
treib13251192016-06-29 07:13:1552#endif
53
[email protected]1d5cf4142014-01-24 18:25:2254namespace extensions {
55namespace util {
[email protected]a7ff4b72013-10-17 20:56:0256
[email protected]b33c8c22014-05-29 19:51:0857namespace {
thestig7b4bd932014-09-09 22:44:3158// Returns |extension_id|. See note below.
59std::string ReloadExtensionIfEnabled(const std::string& extension_id,
60 content::BrowserContext* context) {
61 ExtensionRegistry* registry = ExtensionRegistry::Get(context);
62 bool extension_is_enabled =
63 registry->enabled_extensions().Contains(extension_id);
64
65 if (!extension_is_enabled)
66 return extension_id;
67
68 // When we reload the extension the ID may be invalidated if we've passed it
69 // by const ref everywhere. Make a copy to be safe. http://crbug.com/103762
70 std::string id = extension_id;
71 ExtensionService* service =
72 ExtensionSystem::Get(context)->extension_service();
73 CHECK(service);
74 service->ReloadExtension(id);
75 return id;
76}
77
[email protected]277c4142014-06-19 20:08:5478} // namespace
[email protected]b33c8c22014-05-29 19:51:0879
Aaron Colwell8c430642020-08-04 01:55:2080bool IsExtensionSiteWithIsolatedStorage(const GURL& site_url,
81 content::BrowserContext* context) {
82 if (!site_url.SchemeIs(extensions::kExtensionScheme))
83 return false;
84
85 // The host in an extension site URL is the extension_id.
86 DCHECK(site_url.has_host());
87 return HasIsolatedStorage(site_url.host(), context);
88}
89
90bool HasIsolatedStorage(const std::string& extension_id,
91 content::BrowserContext* context) {
92 const Extension* extension =
93 ExtensionRegistry::Get(context)->enabled_extensions().GetByID(
94 extension_id);
Pavol Marko41c37b12019-08-07 10:56:3295
Yuta Hijikata1290fee22020-11-25 09:46:2896#if BUILDFLAG(IS_CHROMEOS_ASH)
Pavol Marko41c37b12019-08-07 10:56:3297 const bool is_policy_extension =
98 extension && Manifest::IsPolicyLocation(extension->location());
99 Profile* profile = Profile::FromBrowserContext(context);
100 if (profile && chromeos::ProfileHelper::IsSigninProfile(profile) &&
101 is_policy_extension) {
102 return true;
103 }
104#endif
105
106 return extension && AppIsolationInfo::HasIsolatedStorage(extension);
107}
108
[email protected]a7ff4b72013-10-17 20:56:02109void SetIsIncognitoEnabled(const std::string& extension_id,
[email protected]1d5cf4142014-01-24 18:25:22110 content::BrowserContext* context,
[email protected]a7ff4b72013-10-17 20:56:02111 bool enabled) {
thestig7b4bd932014-09-09 22:44:31112 ExtensionRegistry* registry = ExtensionRegistry::Get(context);
113 const Extension* extension =
114 registry->GetExtensionById(extension_id, ExtensionRegistry::EVERYTHING);
[email protected]a7ff4b72013-10-17 20:56:02115
[email protected]1d5cf4142014-01-24 18:25:22116 if (extension) {
kundajie548e7442015-09-18 23:19:09117 if (!util::CanBeIncognitoEnabled(extension))
[email protected]1d5cf4142014-01-24 18:25:22118 return;
119
treibc644a1c2015-07-13 08:37:04120 // TODO(treib,kalman): Should this be Manifest::IsComponentLocation(..)?
121 // (which also checks for EXTERNAL_COMPONENT).
[email protected]1d5cf4142014-01-24 18:25:22122 if (extension->location() == Manifest::COMPONENT) {
123 // This shouldn't be called for component extensions unless it is called
124 // by sync, for syncable component extensions.
125 // See http://crbug.com/112290 and associated CLs for the sordid history.
treib13251192016-06-29 07:13:15126 bool syncable = sync_helper::IsSyncableComponentExtension(extension);
Yuta Hijikata1290fee22020-11-25 09:46:28127#if BUILDFLAG(IS_CHROMEOS_ASH)
treib13251192016-06-29 07:13:15128 // For some users, the file manager app somehow ended up being synced even
129 // though it's supposed to be unsyncable; see crbug.com/576964. If the bad
130 // data ever gets cleaned up, this hack should be removed.
131 syncable = syncable || extension->id() == file_manager::kFileManagerAppId;
132#endif
133 DCHECK(syncable);
[email protected]1d5cf4142014-01-24 18:25:22134
135 // If we are here, make sure the we aren't trying to change the value.
thestig7b4bd932014-09-09 22:44:31136 DCHECK_EQ(enabled, IsIncognitoEnabled(extension_id, context));
[email protected]1d5cf4142014-01-24 18:25:22137 return;
138 }
[email protected]a7ff4b72013-10-17 20:56:02139 }
140
thestig7b4bd932014-09-09 22:44:31141 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(context);
[email protected]a7ff4b72013-10-17 20:56:02142 // Broadcast unloaded and loaded events to update browser state. Only bother
143 // if the value changed and the extension is actually enabled, since there is
144 // no UI otherwise.
145 bool old_enabled = extension_prefs->IsIncognitoEnabled(extension_id);
146 if (enabled == old_enabled)
147 return;
148
149 extension_prefs->SetIsIncognitoEnabled(extension_id, enabled);
150
thestig7b4bd932014-09-09 22:44:31151 std::string id = ReloadExtensionIfEnabled(extension_id, context);
[email protected]a7ff4b72013-10-17 20:56:02152
153 // Reloading the extension invalidates the |extension| pointer.
thestig7b4bd932014-09-09 22:44:31154 extension = registry->GetExtensionById(id, ExtensionRegistry::EVERYTHING);
[email protected]f8aefb132013-10-30 09:29:52155 if (extension) {
thestig7b4bd932014-09-09 22:44:31156 Profile* profile = Profile::FromBrowserContext(context);
157 ExtensionSyncService::Get(profile)->SyncExtensionChangeIfNeeded(*extension);
[email protected]f8aefb132013-10-30 09:29:52158 }
[email protected]a7ff4b72013-10-17 20:56:02159}
160
[email protected]a7ff4b72013-10-17 20:56:02161bool CanLoadInIncognito(const Extension* extension,
[email protected]1d5cf4142014-01-24 18:25:22162 content::BrowserContext* context) {
163 CHECK(extension);
[email protected]a7ff4b72013-10-17 20:56:02164 if (extension->is_hosted_app())
165 return true;
166 // Packaged apps and regular extensions need to be enabled specifically for
167 // incognito (and split mode should be set).
[email protected]1d5cf4142014-01-24 18:25:22168 return IncognitoInfo::IsSplitMode(extension) &&
169 IsIncognitoEnabled(extension->id(), context);
[email protected]a7ff4b72013-10-17 20:56:02170}
171
[email protected]1d5cf4142014-01-24 18:25:22172bool AllowFileAccess(const std::string& extension_id,
173 content::BrowserContext* context) {
avi3ef9ec9e2014-12-22 22:50:17174 return base::CommandLine::ForCurrentProcess()->HasSwitch(
Oscar Johansson7f4c1b932018-06-12 06:11:58175 ::switches::kDisableExtensionsFileAccessCheck) ||
[email protected]1d5cf4142014-01-24 18:25:22176 ExtensionPrefs::Get(context)->AllowFileAccess(extension_id);
[email protected]a7ff4b72013-10-17 20:56:02177}
178
[email protected]1d5cf4142014-01-24 18:25:22179void SetAllowFileAccess(const std::string& extension_id,
180 content::BrowserContext* context,
[email protected]a7ff4b72013-10-17 20:56:02181 bool allow) {
182 // Reload to update browser state. Only bother if the value changed and the
183 // extension is actually enabled, since there is no UI otherwise.
[email protected]1d5cf4142014-01-24 18:25:22184 if (allow == AllowFileAccess(extension_id, context))
[email protected]a7ff4b72013-10-17 20:56:02185 return;
186
[email protected]7c82539c2014-02-19 06:09:17187 ExtensionPrefs::Get(context)->SetAllowFileAccess(extension_id, allow);
[email protected]a7ff4b72013-10-17 20:56:02188
thestig7b4bd932014-09-09 22:44:31189 ReloadExtensionIfEnabled(extension_id, context);
[email protected]a7ff4b72013-10-17 20:56:02190}
191
[email protected]f5ea0962013-11-22 09:20:47192bool IsAppLaunchable(const std::string& extension_id,
[email protected]1d5cf4142014-01-24 18:25:22193 content::BrowserContext* context) {
[email protected]47e19402014-06-27 09:01:14194 int reason = ExtensionPrefs::Get(context)->GetDisableReasons(extension_id);
Minh X. Nguyen45479012017-08-18 21:35:36195 return !((reason & disable_reason::DISABLE_UNSUPPORTED_REQUIREMENT) ||
196 (reason & disable_reason::DISABLE_CORRUPTED));
[email protected]f5ea0962013-11-22 09:20:47197}
198
199bool IsAppLaunchableWithoutEnabling(const std::string& extension_id,
[email protected]1d5cf4142014-01-24 18:25:22200 content::BrowserContext* context) {
201 return ExtensionRegistry::Get(context)->GetExtensionById(
202 extension_id, ExtensionRegistry::ENABLED) != NULL;
[email protected]f5ea0962013-11-22 09:20:47203}
204
treibc644a1c2015-07-13 08:37:04205bool ShouldSync(const Extension* extension,
206 content::BrowserContext* context) {
Swapnilf6f2f1d2021-01-14 18:50:43207 ExtensionManagement* extension_management =
208 ExtensionManagementFactory::GetForBrowserContext(context);
209 // Update URL is overridden only for non webstore extensions and offstore
210 // extensions should not be synced.
211 if (extension_management->IsUpdateUrlOverridden(extension->id())) {
212 const GURL update_url =
213 extension_management->GetEffectiveUpdateURL(*extension);
214 DCHECK(!extension_urls::IsWebstoreUpdateUrl(update_url))
215 << "Update URL cannot be overridden to be the webstore URL!";
216 return false;
217 }
treibc644a1c2015-07-13 08:37:04218 return sync_helper::IsSyncable(extension) &&
[email protected]658eae52014-06-14 20:28:05219 !ExtensionPrefs::Get(context)->DoNotSync(extension->id());
220}
221
[email protected]617342a42013-12-18 23:34:03222bool IsExtensionIdle(const std::string& extension_id,
[email protected]1d5cf4142014-01-24 18:25:22223 content::BrowserContext* context) {
elijahtaylor0def4432014-10-06 18:15:11224 std::vector<std::string> ids_to_check;
225 ids_to_check.push_back(extension_id);
[email protected]617342a42013-12-18 23:34:03226
elijahtaylor0def4432014-10-06 18:15:11227 const Extension* extension =
228 ExtensionRegistry::Get(context)
229 ->GetExtensionById(extension_id, ExtensionRegistry::ENABLED);
230 if (extension && extension->is_shared_module()) {
231 // We have to check all the extensions that use this shared module for idle
232 // to tell whether it is really 'idle'.
233 SharedModuleService* service = ExtensionSystem::Get(context)
234 ->extension_service()
235 ->shared_module_service();
dchengc963c7142016-04-08 03:55:22236 std::unique_ptr<ExtensionSet> dependents =
elijahtaylor0def4432014-10-06 18:15:11237 service->GetDependentExtensions(extension);
238 for (ExtensionSet::const_iterator i = dependents->begin();
239 i != dependents->end();
240 i++) {
241 ids_to_check.push_back((*i)->id());
242 }
243 }
244
reillyg0ea3fa902014-10-28 15:30:23245 ProcessManager* process_manager = ProcessManager::Get(context);
elijahtaylor0def4432014-10-06 18:15:11246 for (std::vector<std::string>::const_iterator i = ids_to_check.begin();
247 i != ids_to_check.end();
248 i++) {
249 const std::string id = (*i);
250 ExtensionHost* host = process_manager->GetBackgroundHostForExtension(id);
251 if (host)
252 return false;
253
rdevlin.cronin3d4261522015-02-10 00:48:15254 scoped_refptr<content::SiteInstance> site_instance =
elijahtaylor0def4432014-10-06 18:15:11255 process_manager->GetSiteInstanceForURL(
256 Extension::GetBaseURLFromExtensionId(id));
257 if (site_instance && site_instance->HasProcess())
258 return false;
259
rdevlin.cronin6ae04a012015-04-03 20:19:40260 if (!process_manager->GetRenderFrameHostsForExtension(id).empty())
elijahtaylor0def4432014-10-06 18:15:11261 return false;
262 }
263 return true;
[email protected]617342a42013-12-18 23:34:03264}
265
dchengc963c7142016-04-08 03:55:22266std::unique_ptr<base::DictionaryValue> GetExtensionInfo(
267 const Extension* extension) {
[email protected]bb2feea2014-03-18 22:08:13268 DCHECK(extension);
dchengc963c7142016-04-08 03:55:22269 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
[email protected]bb2feea2014-03-18 22:08:13270
271 dict->SetString("id", extension->id());
272 dict->SetString("name", extension->name());
273
274 GURL icon = extensions::ExtensionIconSource::GetIconURL(
estade32426e02016-12-18 01:26:17275 extension, extension_misc::EXTENSION_ICON_SMALLISH,
[email protected]bb2feea2014-03-18 22:08:13276 ExtensionIconSet::MATCH_BIGGER,
estade32426e02016-12-18 01:26:17277 false); // Not grayscale.
[email protected]bb2feea2014-03-18 22:08:13278 dict->SetString("icon", icon.spec());
279
dcheng1fc00f12015-12-26 22:18:03280 return dict;
[email protected]bb2feea2014-03-18 22:08:13281}
282
[email protected]7eb20e32014-04-30 08:50:56283const gfx::ImageSkia& GetDefaultAppIcon() {
Lei Zhang7640d542017-10-03 16:26:49284 return *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
[email protected]7eb20e32014-04-30 08:50:56285 IDR_APP_DEFAULT_ICON);
286}
287
288const gfx::ImageSkia& GetDefaultExtensionIcon() {
Lei Zhang7640d542017-10-03 16:26:49289 return *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
[email protected]7eb20e32014-04-30 08:50:56290 IDR_EXTENSION_DEFAULT_ICON);
291}
292
Danan S5f430882020-01-14 04:52:55293std::unique_ptr<const PermissionSet> GetInstallPromptPermissionSetForExtension(
294 const Extension* extension,
295 Profile* profile,
296 bool include_optional_permissions) {
297 // Initialize permissions if they have not already been set so that
298 // any transformations are correctly reflected in the install prompt.
299 PermissionsUpdater(profile, PermissionsUpdater::INIT_FLAG_TRANSIENT)
300 .InitializePermissions(extension);
301
302 std::unique_ptr<const PermissionSet> permissions_to_display =
303 extension->permissions_data()->active_permissions().Clone();
304
305 if (include_optional_permissions) {
306 const PermissionSet& optional_permissions =
307 PermissionsParser::GetOptionalPermissions(extension);
308 permissions_to_display = PermissionSet::CreateUnion(*permissions_to_display,
309 optional_permissions);
310 }
311 return permissions_to_display;
312}
313
[email protected]1d5cf4142014-01-24 18:25:22314} // namespace util
315} // namespace extensions