blob: 7dcaf6567c8d618842991df40b45f63843ec16b0 [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"
[email protected]23a85362014-07-07 23:26:1918#include "chrome/browser/extensions/permissions_updater.h"
rdevlin.cronincb9f86e2015-10-15 15:13:4219#include "chrome/browser/extensions/scripting_permissions_modifier.h"
elijahtaylor0def4432014-10-06 18:15:1120#include "chrome/browser/extensions/shared_module_service.h"
[email protected]1d5cf4142014-01-24 18:25:2221#include "chrome/browser/profiles/profile.h"
[email protected]bb2feea2014-03-18 22:08:1322#include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
tapted70cdc812017-03-23 20:48:1923#include "chrome/common/chrome_features.h"
[email protected]a7ff4b72013-10-17 20:56:0224#include "chrome/common/chrome_switches.h"
[email protected]a7ff4b72013-10-17 20:56:0225#include "chrome/common/extensions/sync_helper.h"
treibb6af28cd2015-12-01 11:19:4626#include "components/variations/variations_associated_data.h"
[email protected]617342a42013-12-18 23:34:0327#include "content/public/browser/site_instance.h"
Devlin Croninbffe949eb2018-01-12 03:03:4028#include "extensions/browser/disable_reason.h"
[email protected]489db0842014-01-22 18:20:0329#include "extensions/browser/extension_prefs.h"
[email protected]599539802014-01-07 23:06:0030#include "extensions/browser/extension_registry.h"
[email protected]59b0e602014-01-30 00:41:2431#include "extensions/browser/extension_system.h"
[email protected]30e190f82014-05-26 16:44:3932#include "extensions/browser/extension_util.h"
[email protected]e4452d32013-11-15 23:07:4133#include "extensions/common/extension.h"
[email protected]4b7908842014-04-07 23:50:2234#include "extensions/common/extension_icon_set.h"
[email protected]a7ff4b72013-10-17 20:56:0235#include "extensions/common/manifest.h"
tfarina0bcdf362015-06-29 22:19:2636#include "extensions/common/manifest_handlers/app_isolation_info.h"
[email protected]1f7de252013-11-06 22:02:0037#include "extensions/common/manifest_handlers/incognito_info.h"
[email protected]4d67e9d2014-08-18 22:03:5438#include "extensions/common/permissions/permissions_data.h"
mukai4245dfe82014-09-05 17:40:5139#include "extensions/grit/extensions_browser_resources.h"
[email protected]7eb20e32014-04-30 08:50:5640#include "ui/base/resource/resource_bundle.h"
Michael Giuffrida7efeed142017-06-07 06:29:2141#include "url/gurl.h"
[email protected]a7ff4b72013-10-17 20:56:0242
treib13251192016-06-29 07:13:1543#if defined(OS_CHROMEOS)
44#include "chrome/browser/chromeos/file_manager/app_id.h"
45#endif
46
[email protected]1d5cf4142014-01-24 18:25:2247namespace extensions {
48namespace util {
[email protected]a7ff4b72013-10-17 20:56:0249
[email protected]b33c8c22014-05-29 19:51:0850namespace {
mamir192d7882016-06-22 17:10:1651// The entry into the prefs used to flag an extension as installed by custodian.
52// It is relevant only for supervised users.
53const char kWasInstalledByCustodianPrefName[] = "was_installed_by_custodian";
54
thestig7b4bd932014-09-09 22:44:3155// Returns |extension_id|. See note below.
56std::string ReloadExtensionIfEnabled(const std::string& extension_id,
57 content::BrowserContext* context) {
58 ExtensionRegistry* registry = ExtensionRegistry::Get(context);
59 bool extension_is_enabled =
60 registry->enabled_extensions().Contains(extension_id);
61
62 if (!extension_is_enabled)
63 return extension_id;
64
65 // When we reload the extension the ID may be invalidated if we've passed it
66 // by const ref everywhere. Make a copy to be safe. http://crbug.com/103762
67 std::string id = extension_id;
68 ExtensionService* service =
69 ExtensionSystem::Get(context)->extension_service();
70 CHECK(service);
71 service->ReloadExtension(id);
72 return id;
73}
74
[email protected]277c4142014-06-19 20:08:5475} // namespace
[email protected]b33c8c22014-05-29 19:51:0876
[email protected]a7ff4b72013-10-17 20:56:0277void SetIsIncognitoEnabled(const std::string& extension_id,
[email protected]1d5cf4142014-01-24 18:25:2278 content::BrowserContext* context,
[email protected]a7ff4b72013-10-17 20:56:0279 bool enabled) {
thestig7b4bd932014-09-09 22:44:3180 ExtensionRegistry* registry = ExtensionRegistry::Get(context);
81 const Extension* extension =
82 registry->GetExtensionById(extension_id, ExtensionRegistry::EVERYTHING);
[email protected]a7ff4b72013-10-17 20:56:0283
[email protected]1d5cf4142014-01-24 18:25:2284 if (extension) {
kundajie548e7442015-09-18 23:19:0985 if (!util::CanBeIncognitoEnabled(extension))
[email protected]1d5cf4142014-01-24 18:25:2286 return;
87
treibc644a1c2015-07-13 08:37:0488 // TODO(treib,kalman): Should this be Manifest::IsComponentLocation(..)?
89 // (which also checks for EXTERNAL_COMPONENT).
[email protected]1d5cf4142014-01-24 18:25:2290 if (extension->location() == Manifest::COMPONENT) {
91 // This shouldn't be called for component extensions unless it is called
92 // by sync, for syncable component extensions.
93 // See http://crbug.com/112290 and associated CLs for the sordid history.
treib13251192016-06-29 07:13:1594 bool syncable = sync_helper::IsSyncableComponentExtension(extension);
95#if defined(OS_CHROMEOS)
96 // For some users, the file manager app somehow ended up being synced even
97 // though it's supposed to be unsyncable; see crbug.com/576964. If the bad
98 // data ever gets cleaned up, this hack should be removed.
99 syncable = syncable || extension->id() == file_manager::kFileManagerAppId;
100#endif
101 DCHECK(syncable);
[email protected]1d5cf4142014-01-24 18:25:22102
103 // If we are here, make sure the we aren't trying to change the value.
thestig7b4bd932014-09-09 22:44:31104 DCHECK_EQ(enabled, IsIncognitoEnabled(extension_id, context));
[email protected]1d5cf4142014-01-24 18:25:22105 return;
106 }
[email protected]a7ff4b72013-10-17 20:56:02107 }
108
thestig7b4bd932014-09-09 22:44:31109 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(context);
[email protected]a7ff4b72013-10-17 20:56:02110 // Broadcast unloaded and loaded events to update browser state. Only bother
111 // if the value changed and the extension is actually enabled, since there is
112 // no UI otherwise.
113 bool old_enabled = extension_prefs->IsIncognitoEnabled(extension_id);
114 if (enabled == old_enabled)
115 return;
116
117 extension_prefs->SetIsIncognitoEnabled(extension_id, enabled);
118
thestig7b4bd932014-09-09 22:44:31119 std::string id = ReloadExtensionIfEnabled(extension_id, context);
[email protected]a7ff4b72013-10-17 20:56:02120
121 // Reloading the extension invalidates the |extension| pointer.
thestig7b4bd932014-09-09 22:44:31122 extension = registry->GetExtensionById(id, ExtensionRegistry::EVERYTHING);
[email protected]f8aefb132013-10-30 09:29:52123 if (extension) {
thestig7b4bd932014-09-09 22:44:31124 Profile* profile = Profile::FromBrowserContext(context);
125 ExtensionSyncService::Get(profile)->SyncExtensionChangeIfNeeded(*extension);
[email protected]f8aefb132013-10-30 09:29:52126 }
[email protected]a7ff4b72013-10-17 20:56:02127}
128
129bool CanCrossIncognito(const Extension* extension,
[email protected]1d5cf4142014-01-24 18:25:22130 content::BrowserContext* context) {
[email protected]a7ff4b72013-10-17 20:56:02131 // We allow the extension to see events and data from another profile iff it
132 // uses "spanning" behavior and it has incognito access. "split" mode
133 // extensions only see events for a matching profile.
134 CHECK(extension);
[email protected]1d5cf4142014-01-24 18:25:22135 return IsIncognitoEnabled(extension->id(), context) &&
136 !IncognitoInfo::IsSplitMode(extension);
[email protected]a7ff4b72013-10-17 20:56:02137}
138
139bool CanLoadInIncognito(const Extension* extension,
[email protected]1d5cf4142014-01-24 18:25:22140 content::BrowserContext* context) {
141 CHECK(extension);
[email protected]a7ff4b72013-10-17 20:56:02142 if (extension->is_hosted_app())
143 return true;
144 // Packaged apps and regular extensions need to be enabled specifically for
145 // incognito (and split mode should be set).
[email protected]1d5cf4142014-01-24 18:25:22146 return IncognitoInfo::IsSplitMode(extension) &&
147 IsIncognitoEnabled(extension->id(), context);
[email protected]a7ff4b72013-10-17 20:56:02148}
149
[email protected]1d5cf4142014-01-24 18:25:22150bool AllowFileAccess(const std::string& extension_id,
151 content::BrowserContext* context) {
avi3ef9ec9e2014-12-22 22:50:17152 return base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]1d5cf4142014-01-24 18:25:22153 switches::kDisableExtensionsFileAccessCheck) ||
154 ExtensionPrefs::Get(context)->AllowFileAccess(extension_id);
[email protected]a7ff4b72013-10-17 20:56:02155}
156
[email protected]1d5cf4142014-01-24 18:25:22157void SetAllowFileAccess(const std::string& extension_id,
158 content::BrowserContext* context,
[email protected]a7ff4b72013-10-17 20:56:02159 bool allow) {
160 // Reload to update browser state. Only bother if the value changed and the
161 // extension is actually enabled, since there is no UI otherwise.
[email protected]1d5cf4142014-01-24 18:25:22162 if (allow == AllowFileAccess(extension_id, context))
[email protected]a7ff4b72013-10-17 20:56:02163 return;
164
[email protected]7c82539c2014-02-19 06:09:17165 ExtensionPrefs::Get(context)->SetAllowFileAccess(extension_id, allow);
[email protected]a7ff4b72013-10-17 20:56:02166
thestig7b4bd932014-09-09 22:44:31167 ReloadExtensionIfEnabled(extension_id, context);
[email protected]a7ff4b72013-10-17 20:56:02168}
169
mamir192d7882016-06-22 17:10:16170void SetWasInstalledByCustodian(const std::string& extension_id,
171 content::BrowserContext* context,
172 bool installed_by_custodian) {
173 if (installed_by_custodian == WasInstalledByCustodian(extension_id, context))
174 return;
175
Karan Bhatia2a117232017-08-23 00:24:56176 ExtensionPrefs* prefs = ExtensionPrefs::Get(context);
177
178 prefs->UpdateExtensionPref(
mamir192d7882016-06-22 17:10:16179 extension_id, kWasInstalledByCustodianPrefName,
vabrfb687dc2017-03-22 11:40:57180 installed_by_custodian ? base::MakeUnique<base::Value>(true) : nullptr);
mamir192d7882016-06-22 17:10:16181 ExtensionService* service =
182 ExtensionSystem::Get(context)->extension_service();
183
mamir192d7882016-06-22 17:10:16184 if (!installed_by_custodian) {
185 // If installed_by_custodian changes to false, the extension may need to
186 // be unloaded now.
187 service->ReloadExtension(extension_id);
188 return;
189 }
190
mamire9609642016-06-28 22:17:54191 ExtensionRegistry* registry = ExtensionRegistry::Get(context);
mamir192d7882016-06-22 17:10:16192 // If it is already enabled, do nothing.
193 if (registry->enabled_extensions().Contains(extension_id))
194 return;
195
Karan Bhatia2a117232017-08-23 00:24:56196 // If the extension was disabled due to management policy, try to re-enable
197 // it. Example is a pre-installed extension that was disabled when a
mamir192d7882016-06-22 17:10:16198 // supervised user flag has been received.
Karan Bhatia2a117232017-08-23 00:24:56199 // Note: EnableExtension will fail if the extension still needs to be disabled
200 // due to manangement policy.
201 if (registry->disabled_extensions().Contains(extension_id) &&
202 prefs->GetDisableReasons(extension_id) ==
203 disable_reason::DISABLE_BLOCKED_BY_POLICY) {
204 service->EnableExtension(extension_id);
mamir192d7882016-06-22 17:10:16205 }
206}
207
208bool WasInstalledByCustodian(const std::string& extension_id,
209 content::BrowserContext* context) {
210 bool installed_by_custodian = false;
211 ExtensionPrefs* prefs = ExtensionPrefs::Get(context);
212 prefs->ReadPrefAsBoolean(extension_id, kWasInstalledByCustodianPrefName,
213 &installed_by_custodian);
214 return installed_by_custodian;
215}
216
[email protected]f5ea0962013-11-22 09:20:47217bool IsAppLaunchable(const std::string& extension_id,
[email protected]1d5cf4142014-01-24 18:25:22218 content::BrowserContext* context) {
[email protected]47e19402014-06-27 09:01:14219 int reason = ExtensionPrefs::Get(context)->GetDisableReasons(extension_id);
Minh X. Nguyen45479012017-08-18 21:35:36220 return !((reason & disable_reason::DISABLE_UNSUPPORTED_REQUIREMENT) ||
221 (reason & disable_reason::DISABLE_CORRUPTED));
[email protected]f5ea0962013-11-22 09:20:47222}
223
224bool IsAppLaunchableWithoutEnabling(const std::string& extension_id,
[email protected]1d5cf4142014-01-24 18:25:22225 content::BrowserContext* context) {
226 return ExtensionRegistry::Get(context)->GetExtensionById(
227 extension_id, ExtensionRegistry::ENABLED) != NULL;
[email protected]f5ea0962013-11-22 09:20:47228}
229
treibc644a1c2015-07-13 08:37:04230bool ShouldSync(const Extension* extension,
231 content::BrowserContext* context) {
232 return sync_helper::IsSyncable(extension) &&
[email protected]658eae52014-06-14 20:28:05233 !ExtensionPrefs::Get(context)->DoNotSync(extension->id());
234}
235
[email protected]617342a42013-12-18 23:34:03236bool IsExtensionIdle(const std::string& extension_id,
[email protected]1d5cf4142014-01-24 18:25:22237 content::BrowserContext* context) {
elijahtaylor0def4432014-10-06 18:15:11238 std::vector<std::string> ids_to_check;
239 ids_to_check.push_back(extension_id);
[email protected]617342a42013-12-18 23:34:03240
elijahtaylor0def4432014-10-06 18:15:11241 const Extension* extension =
242 ExtensionRegistry::Get(context)
243 ->GetExtensionById(extension_id, ExtensionRegistry::ENABLED);
244 if (extension && extension->is_shared_module()) {
245 // We have to check all the extensions that use this shared module for idle
246 // to tell whether it is really 'idle'.
247 SharedModuleService* service = ExtensionSystem::Get(context)
248 ->extension_service()
249 ->shared_module_service();
dchengc963c7142016-04-08 03:55:22250 std::unique_ptr<ExtensionSet> dependents =
elijahtaylor0def4432014-10-06 18:15:11251 service->GetDependentExtensions(extension);
252 for (ExtensionSet::const_iterator i = dependents->begin();
253 i != dependents->end();
254 i++) {
255 ids_to_check.push_back((*i)->id());
256 }
257 }
258
reillyg0ea3fa902014-10-28 15:30:23259 ProcessManager* process_manager = ProcessManager::Get(context);
elijahtaylor0def4432014-10-06 18:15:11260 for (std::vector<std::string>::const_iterator i = ids_to_check.begin();
261 i != ids_to_check.end();
262 i++) {
263 const std::string id = (*i);
264 ExtensionHost* host = process_manager->GetBackgroundHostForExtension(id);
265 if (host)
266 return false;
267
rdevlin.cronin3d4261522015-02-10 00:48:15268 scoped_refptr<content::SiteInstance> site_instance =
elijahtaylor0def4432014-10-06 18:15:11269 process_manager->GetSiteInstanceForURL(
270 Extension::GetBaseURLFromExtensionId(id));
271 if (site_instance && site_instance->HasProcess())
272 return false;
273
rdevlin.cronin6ae04a012015-04-03 20:19:40274 if (!process_manager->GetRenderFrameHostsForExtension(id).empty())
elijahtaylor0def4432014-10-06 18:15:11275 return false;
276 }
277 return true;
[email protected]617342a42013-12-18 23:34:03278}
279
dchengc963c7142016-04-08 03:55:22280std::unique_ptr<base::DictionaryValue> GetExtensionInfo(
281 const Extension* extension) {
[email protected]bb2feea2014-03-18 22:08:13282 DCHECK(extension);
dchengc963c7142016-04-08 03:55:22283 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
[email protected]bb2feea2014-03-18 22:08:13284
285 dict->SetString("id", extension->id());
286 dict->SetString("name", extension->name());
287
288 GURL icon = extensions::ExtensionIconSource::GetIconURL(
estade32426e02016-12-18 01:26:17289 extension, extension_misc::EXTENSION_ICON_SMALLISH,
[email protected]bb2feea2014-03-18 22:08:13290 ExtensionIconSet::MATCH_BIGGER,
estade32426e02016-12-18 01:26:17291 false); // Not grayscale.
[email protected]bb2feea2014-03-18 22:08:13292 dict->SetString("icon", icon.spec());
293
dcheng1fc00f12015-12-26 22:18:03294 return dict;
[email protected]bb2feea2014-03-18 22:08:13295}
296
[email protected]7eb20e32014-04-30 08:50:56297const gfx::ImageSkia& GetDefaultAppIcon() {
Lei Zhang7640d542017-10-03 16:26:49298 return *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
[email protected]7eb20e32014-04-30 08:50:56299 IDR_APP_DEFAULT_ICON);
300}
301
302const gfx::ImageSkia& GetDefaultExtensionIcon() {
Lei Zhang7640d542017-10-03 16:26:49303 return *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
[email protected]7eb20e32014-04-30 08:50:56304 IDR_EXTENSION_DEFAULT_ICON);
305}
306
benwellsc431c0ae2015-01-27 22:04:06307bool IsNewBookmarkAppsEnabled() {
dominicknfa0e3df2016-01-14 05:38:32308#if defined(OS_MACOSX)
benwellsc421ccdb2017-06-28 05:10:32309 return base::FeatureList::IsEnabled(features::kBookmarkApps) ||
benwells0eee2292017-06-28 08:41:22310 base::FeatureList::IsEnabled(features::kAppBanners) ||
Matt Giucad4870432017-11-24 01:04:38311 banners::AppBannerManager::IsExperimentalAppBannersEnabled();
dominicknfa0e3df2016-01-14 05:38:32312#else
tapted70cdc812017-03-23 20:48:19313 return true;
dominicknfa0e3df2016-01-14 05:38:32314#endif
benwells39f23ae2014-08-27 08:01:52315}
316
dominickn2b10cbd2015-08-20 02:09:18317bool CanHostedAppsOpenInWindows() {
318#if defined(OS_MACOSX)
319 return base::CommandLine::ForCurrentProcess()->HasSwitch(
Giovanni Ortuño Urquidi45020e232017-07-12 06:10:17320 switches::kEnableHostedAppsInWindows) ||
321 base::FeatureList::IsEnabled(features::kDesktopPWAWindowing);
dominickn2b10cbd2015-08-20 02:09:18322#else
323 return true;
324#endif
325}
326
mamir192d7882016-06-22 17:10:16327bool IsExtensionSupervised(const Extension* extension, Profile* profile) {
328 return WasInstalledByCustodian(extension->id(), profile) &&
329 profile->IsSupervised();
treibbb9a1962015-02-25 13:40:59330}
331
[email protected]1d5cf4142014-01-24 18:25:22332} // namespace util
333} // namespace extensions