blob: a1ad7e239ee5f6acd846acbfee09f857fbf12cb0 [file] [log] [blame]
[email protected]098fa7a2013-03-08 22:11:171// Copyright (c) 2013 The Chromium Authors. All rights reserved.
[email protected]6014d672008-12-05 00:38:252// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]eaa7dd182010-12-14 11:09:005#include "chrome/browser/extensions/extension_service.h"
[email protected]6014d672008-12-05 00:38:256
[email protected]654512b2010-09-01 02:09:427#include <algorithm>
[email protected]695b5712012-12-06 23:55:288#include <iterator>
[email protected]97d2f1d2011-01-15 00:41:089#include <set>
[email protected]654512b2010-09-01 02:09:4210
[email protected]24b538a2010-02-27 01:22:4411#include "base/basictypes.h"
[email protected]05aad2da2011-10-28 10:12:3712#include "base/bind.h"
[email protected]e5af875f2011-10-10 21:09:1413#include "base/callback.h"
[email protected]62433d32011-10-12 22:33:1214#include "base/command_line.h"
[email protected]6014d672008-12-05 00:38:2515#include "base/file_util.h"
[email protected]14908b72011-04-20 06:54:3616#include "base/logging.h"
[email protected]835d7c82010-10-14 04:38:3817#include "base/metrics/histogram.h"
[email protected]1e127022011-11-29 21:11:4718#include "base/path_service.h"
[email protected]3853a4c2013-02-11 17:15:5719#include "base/prefs/pref_service.h"
[email protected]7286e3fc2011-07-19 22:13:2420#include "base/stl_util.h"
[email protected]6014d672008-12-05 00:38:2521#include "base/string_util.h"
[email protected]18d4b6c2010-09-21 03:21:0422#include "base/stringprintf.h"
[email protected]3ea1b182013-02-08 22:38:4123#include "base/strings/string_number_conversions.h"
[email protected]7f8f24f2012-11-15 19:40:1424#include "base/threading/sequenced_worker_pool.h"
[email protected]34b99632011-01-01 01:01:0625#include "base/threading/thread_restrictions.h"
[email protected]cc2c3432009-11-06 17:24:3626#include "base/time.h"
[email protected]ce7f62e32010-08-10 23:43:5927#include "base/utf_string_conversions.h"
[email protected]cc655912009-01-29 23:19:1928#include "base/values.h"
[email protected]aa142702010-03-26 01:26:3329#include "base/version.h"
[email protected]211a3f32013-05-28 21:48:1830#include "chrome/browser/app_mode/app_mode_utils.h"
[email protected]15730c42009-09-03 00:03:2031#include "chrome/browser/browser_process.h"
[email protected]70019152012-12-19 11:44:1932#include "chrome/browser/devtools/devtools_window.h"
[email protected]32b36c62012-10-24 05:37:4133#include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h"
[email protected]895a1e52012-05-15 02:50:1234#include "chrome/browser/extensions/api/declarative/rules_registry_service.h"
[email protected]ad445762013-01-23 00:47:4435#include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
[email protected]c39df1a2013-01-08 18:22:0736#include "chrome/browser/extensions/api/profile_keyed_api_factory.h"
[email protected]e9f541a2012-11-19 21:52:3137#include "chrome/browser/extensions/api/runtime/runtime_api.h"
[email protected]4636c832013-01-11 02:10:1138#include "chrome/browser/extensions/api/storage/settings_frontend.h"
[email protected]5db9ada2012-04-11 13:48:2039#include "chrome/browser/extensions/app_sync_data.h"
[email protected]5a38dfd2012-07-23 23:22:1040#include "chrome/browser/extensions/browser_event_router.h"
[email protected]d8c8f25f2011-11-02 18:18:0141#include "chrome/browser/extensions/component_loader.h"
[email protected]e0785902011-05-19 23:34:1742#include "chrome/browser/extensions/crx_installer.h"
[email protected]d9ede582012-08-14 19:21:3843#include "chrome/browser/extensions/data_deleter.h"
[email protected]62f051c2012-03-29 17:04:4444#include "chrome/browser/extensions/extension_disabled_ui.h"
[email protected]14a000d2010-04-29 21:44:2445#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]89226982012-07-16 20:09:1846#include "chrome/browser/extensions/extension_error_ui.h"
[email protected]b1748b1d82009-11-30 20:32:5647#include "chrome/browser/extensions/extension_host.h"
[email protected]00b38242012-07-18 18:43:2248#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]4814b512009-11-07 00:12:2949#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]3569b502012-01-12 20:08:2350#include "chrome/browser/extensions/extension_sorting.h"
[email protected]19eb80152011-02-26 00:28:4351#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]90310d92011-04-17 07:35:0452#include "chrome/browser/extensions/extension_sync_data.h"
[email protected]31d8f5f22012-04-02 15:22:0853#include "chrome/browser/extensions/extension_system.h"
[email protected]612a1cb12012-10-17 13:18:0354#include "chrome/browser/extensions/external_install_ui.h"
[email protected]5df038b2012-07-16 19:03:2755#include "chrome/browser/extensions/external_provider_impl.h"
56#include "chrome/browser/extensions/external_provider_interface.h"
[email protected]d8c8f25f2011-11-02 18:18:0157#include "chrome/browser/extensions/installed_loader.h"
[email protected]6cafe35a2012-08-27 22:40:5058#include "chrome/browser/extensions/lazy_background_task_queue.h"
[email protected]fdd679b2012-11-15 20:49:3959#include "chrome/browser/extensions/management_policy.h"
[email protected]b2907fd2011-03-25 16:43:3760#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]c333e792012-01-06 16:57:3961#include "chrome/browser/extensions/permissions_updater.h"
[email protected]6cafe35a2012-08-27 22:40:5062#include "chrome/browser/extensions/platform_app_launcher.h"
[email protected]6cafe35a2012-08-27 22:40:5063#include "chrome/browser/extensions/shell_window_registry.h"
[email protected]d8c8f25f2011-11-02 18:18:0164#include "chrome/browser/extensions/unpacked_installer.h"
[email protected]75bdcb872013-03-13 00:41:4565#include "chrome/browser/extensions/update_observer.h"
[email protected]42a08162012-03-16 18:09:1166#include "chrome/browser/extensions/updater/extension_updater.h"
[email protected]c2295f72013-01-03 22:18:5667#include "chrome/browser/plugins/chrome_plugin_service_filter.h"
[email protected]8ecad5e2010-12-02 21:18:3368#include "chrome/browser/profiles/profile.h"
[email protected]cf593af2011-12-30 05:44:3969#include "chrome/browser/profiles/profile_manager.h"
[email protected]d5949312012-12-03 22:13:3070#include "chrome/browser/themes/theme_service.h"
71#include "chrome/browser/themes/theme_service_factory.h"
[email protected]c8d407e2011-04-28 21:27:1772#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]5eddc3e2011-10-26 04:33:3173#include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
[email protected]b07e606e2012-09-15 20:16:1574#include "chrome/browser/ui/webui/theme_source.h"
[email protected]aab98a52009-12-02 03:22:3575#include "chrome/common/child_process_logging.h"
[email protected]432115822011-07-10 15:52:2776#include "chrome/common/chrome_notification_types.h"
[email protected]1e127022011-11-29 21:11:4777#include "chrome/common/chrome_paths.h"
[email protected]e2eb43112009-05-29 21:19:5478#include "chrome/common/chrome_switches.h"
[email protected]15397b42012-05-16 23:56:0679#include "chrome/common/chrome_version_info.h"
[email protected]098fa7a2013-03-08 22:11:1780#include "chrome/common/extensions/api/plugins/plugins_handler.h"
[email protected]9367eabc2013-03-01 01:29:2981#include "chrome/common/extensions/background_info.h"
[email protected]5b1a0e22009-05-26 19:00:5882#include "chrome/common/extensions/extension.h"
[email protected]7c927b62010-02-24 09:54:1383#include "chrome/common/extensions/extension_file_util.h"
[email protected]895a1e52012-05-15 02:50:1284#include "chrome/common/extensions/extension_manifest_constants.h"
[email protected]77a6970c2011-04-23 16:58:5685#include "chrome/common/extensions/extension_messages.h"
[email protected]612a1cb12012-10-17 13:18:0386#include "chrome/common/extensions/feature_switch.h"
[email protected]e9f541a2012-11-19 21:52:3187#include "chrome/common/extensions/features/feature.h"
[email protected]06492ed2013-03-24 22:13:1488#include "chrome/common/extensions/incognito_handler.h"
[email protected]ac875372013-02-28 04:36:0989#include "chrome/common/extensions/manifest.h"
[email protected]561e33d52013-04-03 06:58:4390#include "chrome/common/extensions/manifest_handlers/app_isolation_info.h"
[email protected]65348062013-01-15 07:27:2291#include "chrome/common/extensions/manifest_url_handler.h"
[email protected]2cb5e302013-05-09 05:00:0692#include "chrome/common/extensions/permissions/permissions_data.h"
[email protected]25b34332009-06-05 21:53:1993#include "chrome/common/pref_names.h"
[email protected]21f2e2d2013-01-23 22:59:5294#include "chrome/common/startup_metric_utils.h"
[email protected]a57209872009-05-04 22:53:1495#include "chrome/common/url_constants.h"
[email protected]c38831a12011-10-28 12:44:4996#include "content/public/browser/browser_thread.h"
[email protected]98f66112012-12-25 12:59:3697#include "content/public/browser/devtools_agent_host.h"
[email protected]05aad2da2011-10-28 10:12:3798#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1699#include "content/public/browser/notification_types.h"
[email protected]3a5180ae2011-12-21 02:39:38100#include "content/public/browser/plugin_service.h"
[email protected]f3b1a082011-11-18 00:34:30101#include "content/public/browser/render_process_host.h"
[email protected]399583b2012-12-11 09:33:42102#include "content/public/browser/site_instance.h"
103#include "content/public/browser/storage_partition.h"
[email protected]24ea7a12013-01-27 23:54:53104#include "content/public/browser/url_data_source.h"
[email protected]1e127022011-11-29 21:11:47105#include "content/public/common/pepper_plugin_info.h"
[email protected]836e2982013-05-16 08:07:42106#include "extensions/common/constants.h"
[email protected]e9f541a2012-11-19 21:52:31107#include "extensions/common/error_utils.h"
[email protected]c10da4b02010-03-25 14:38:32108#include "googleurl/src/gurl.h"
[email protected]65187152012-06-02 13:14:14109#include "grit/generated_resources.h"
[email protected]be28b5f42012-07-20 11:31:25110#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
[email protected]895a1e52012-05-15 02:50:12111#include "sync/api/sync_change.h"
112#include "sync/api/sync_error_factory.h"
[email protected]08b1f75f2013-05-22 22:02:38113#include "webkit/browser/database/database_tracker.h"
114#include "webkit/browser/database/database_util.h"
[email protected]79a60642012-10-20 21:03:18115
[email protected]eed367e2011-04-12 03:43:31116#if defined(OS_CHROMEOS)
[email protected]88e8ec9152013-01-17 04:05:18117#include "chrome/browser/chromeos/extensions/install_limiter.h"
[email protected]c6f9203a2013-05-28 02:08:07118#include "webkit/browser/fileapi/file_system_context.h"
[email protected]c4298d02013-05-20 05:42:52119#include "webkit/browser/fileapi/file_system_mount_point_provider.h"
[email protected]eed367e2011-04-12 03:43:31120#endif
121
[email protected]55eb70e762012-02-20 17:38:39122using content::BrowserContext;
[email protected]631bb742011-11-02 11:29:39123using content::BrowserThread;
[email protected]98f66112012-12-25 12:59:36124using content::DevToolsAgentHost;
[email protected]3a5180ae2011-12-21 02:39:38125using content::PluginService;
[email protected]bf3d9df2012-07-24 23:20:27126using extensions::CrxInstaller;
[email protected]1c321ee52012-05-21 03:02:34127using extensions::Extension;
128using extensions::ExtensionIdSet;
129using extensions::ExtensionInfo;
[email protected]215a7be2012-10-22 19:53:42130using extensions::FeatureSwitch;
[email protected]1d5e58b2013-01-31 08:41:40131using extensions::Manifest;
[email protected]c2e66e12012-06-27 06:27:06132using extensions::PermissionMessage;
133using extensions::PermissionMessages;
134using extensions::PermissionSet;
[email protected]e410b5f2012-12-14 14:02:24135using extensions::UnloadedExtensionInfo;
[email protected]5ef47ec2010-01-28 05:58:05136
[email protected]c6d474f82009-12-16 21:11:06137namespace errors = extension_manifest_errors;
138
[email protected]b6ab96d2009-08-20 18:58:19139namespace {
140
[email protected]d96eb512012-11-01 23:44:08141// Histogram values for logging events related to externally installed
142// extensions.
143enum ExternalExtensionEvent {
144 EXTERNAL_EXTENSION_INSTALLED = 0,
145 EXTERNAL_EXTENSION_IGNORED,
146 EXTERNAL_EXTENSION_REENABLED,
147 EXTERNAL_EXTENSION_UNINSTALLED,
148 EXTERNAL_EXTENSION_BUCKET_BOUNDARY,
149};
150
[email protected]612a1cb12012-10-17 13:18:03151// Prompt the user this many times before considering an extension acknowledged.
152static const int kMaxExtensionAcknowledgePromptCount = 3;
153
[email protected]0db124b02012-11-07 04:55:05154// Wait this many seconds after an extensions becomes idle before updating it.
155static const int kUpdateIdleDelay = 5;
156
[email protected]9bd9a6862012-11-29 09:24:22157// Wait this many seconds before trying to garbage collect extensions again.
158static const int kGarbageCollectRetryDelay = 30;
159
[email protected]300c0ea2011-07-15 23:04:33160const char* kNaClPluginMimeType = "application/x-nacl";
161
[email protected]3bdba0d2011-08-23 07:17:30162static bool IsSyncableExtension(const Extension& extension) {
163 return extension.GetSyncType() == Extension::SYNC_TYPE_EXTENSION;
164}
165
166static bool IsSyncableApp(const Extension& extension) {
167 return extension.GetSyncType() == Extension::SYNC_TYPE_APP;
168}
169
[email protected]c6d474f82009-12-16 21:11:06170} // namespace
[email protected]b6ab96d2009-08-20 18:58:19171
[email protected]eaa7dd182010-12-14 11:09:00172ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49173 : background_page_ready(false),
[email protected]f8bf5992011-10-24 22:38:57174 being_upgraded(false),
175 has_used_webrequest(false) {
[email protected]d7e9a862010-11-03 21:57:49176}
177
[email protected]eaa7dd182010-12-14 11:09:00178ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49179}
180
[email protected]cebc3dc2011-04-18 17:15:00181ExtensionService::NaClModuleInfo::NaClModuleInfo() {
182}
183
184ExtensionService::NaClModuleInfo::~NaClModuleInfo() {
185}
186
[email protected]eaa7dd182010-12-14 11:09:00187// ExtensionService.
[email protected]6014d672008-12-05 00:38:25188
[email protected]31bb5ee62012-09-12 22:58:40189const char ExtensionService::kLocalAppSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24190 "Local App Settings";
[email protected]31bb5ee62012-09-12 22:58:40191const char ExtensionService::kLocalExtensionSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24192 "Local Extension Settings";
[email protected]31bb5ee62012-09-12 22:58:40193const char ExtensionService::kSyncAppSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24194 "Sync App Settings";
[email protected]31bb5ee62012-09-12 22:58:40195const char ExtensionService::kSyncExtensionSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24196 "Sync Extension Settings";
[email protected]675bb01012012-12-14 22:03:29197const char ExtensionService::kManagedSettingsDirectoryName[] =
198 "Managed Extension Settings";
[email protected]31bb5ee62012-09-12 22:58:40199const char ExtensionService::kStateStoreName[] = "Extension State";
[email protected]a690e292012-12-19 19:22:49200const char ExtensionService::kRulesStoreName[] = "Extension Rules";
[email protected]494c06e2009-07-25 01:06:42201
[email protected]8e4560b62011-01-14 10:09:14202void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12203 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20204
[email protected]7a4c6852010-09-16 03:44:22205 // Check if the providers know about this extension.
[email protected]5df038b2012-07-16 19:03:27206 extensions::ProviderCollection::const_iterator i;
[email protected]0a60a2e2010-10-25 16:15:21207 for (i = external_extension_providers_.begin();
208 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14209 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21210 if (i->get()->HasExtension(id))
211 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22212 }
213
[email protected]0f48fca2011-05-19 18:46:35214 // We get the list of external extensions to check from preferences.
215 // It is possible that an extension has preferences but is not loaded.
216 // For example, an extension that requires experimental permissions
217 // will not be loaded if the experimental command line flag is not used.
218 // In this case, do not uninstall.
[email protected]31bb5ee62012-09-12 22:58:40219 if (!GetInstalledExtension(id)) {
[email protected]0f48fca2011-05-19 18:46:35220 // We can't call UninstallExtension with an unloaded/invalid
221 // extension ID.
222 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
223 << "with id: " << id;
224 return;
225 }
[email protected]d6ebc9792011-04-07 18:18:33226 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22227}
228
[email protected]7f8f24f2012-11-15 19:40:14229void ExtensionService::SetFileTaskRunnerForTesting(
230 base::SequencedTaskRunner* task_runner) {
231 file_task_runner_ = task_runner;
232}
233
[email protected]8e4560b62011-01-14 10:09:14234void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22235 external_extension_providers_.clear();
236}
237
[email protected]8e4560b62011-01-14 10:09:14238void ExtensionService::AddProviderForTesting(
[email protected]5df038b2012-07-16 19:03:27239 extensions::ExternalProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12240 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21241 external_extension_providers_.push_back(
[email protected]5df038b2012-07-16 19:03:27242 linked_ptr<extensions::ExternalProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22243}
244
[email protected]9060d8b02012-01-13 02:14:30245bool ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22246 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42247 const GURL& update_url,
[email protected]1d5e58b2013-01-31 08:41:40248 Manifest::Location location) {
[email protected]ab22ba42011-01-14 16:36:38249 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
250 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20251
[email protected]8a87a5332011-08-11 17:54:59252 const Extension* extension = GetExtensionById(id, true);
253 if (extension) {
254 // Already installed. Skip this install if the current location has
255 // higher priority than |location|.
[email protected]1d5e58b2013-01-31 08:41:40256 Manifest::Location current = extension->location();
257 if (current == Manifest::GetHigherPriorityLocation(current, location))
[email protected]9060d8b02012-01-13 02:14:30258 return false;
[email protected]8a87a5332011-08-11 17:54:59259 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22260 }
[email protected]9060d8b02012-01-13 02:14:30261
262 // Add |id| to the set of pending extensions. If it can not be added,
263 // then there is already a pending record from a higher-priority install
264 // source. In this case, signal that this extension will not be
265 // installed by returning false.
266 if (!pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]31bb5ee62012-09-12 22:58:40267 id, update_url, location)) {
[email protected]9060d8b02012-01-13 02:14:30268 return false;
[email protected]31bb5ee62012-09-12 22:58:40269 }
[email protected]9060d8b02012-01-13 02:14:30270
[email protected]94fde232012-04-27 10:22:30271 update_once_all_providers_are_ready_ = true;
[email protected]9060d8b02012-01-13 02:14:30272 return true;
[email protected]7a4c6852010-09-16 03:44:22273}
274
[email protected]dc9a74f72012-08-17 18:07:21275const Extension* ExtensionService::GetInstalledApp(const GURL& url) const {
[email protected]615d88f2011-12-13 01:47:44276 const Extension* extension = extensions_.GetExtensionOrAppByURL(
277 ExtensionURLInfo(url));
[email protected]dc9a74f72012-08-17 18:07:21278 return (extension && extension->is_app()) ? extension : NULL;
[email protected]d9696672011-03-15 22:45:09279}
280
[email protected]dc9a74f72012-08-17 18:07:21281bool ExtensionService::IsInstalledApp(const GURL& url) const {
[email protected]d9696672011-03-15 22:45:09282 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27283}
284
[email protected]404fb002012-10-02 21:06:17285const Extension* ExtensionService::GetIsolatedAppForRenderer(
[email protected]dc9a74f72012-08-17 18:07:21286 int renderer_child_id) const {
[email protected]404fb002012-10-02 21:06:17287 std::set<std::string> extension_ids =
288 process_map_.GetExtensionsInProcess(renderer_child_id);
289 // All apps in one process share the same partition.
290 // It is only possible for the app to have isolated storage
291 // if there is only 1 app in the process.
292 if (extension_ids.size() != 1)
293 return NULL;
294
295 const extensions::Extension* extension =
296 extensions_.GetByID(*(extension_ids.begin()));
[email protected]561e33d52013-04-03 06:58:43297 // We still need to check if the extension has isolated storage,
[email protected]404fb002012-10-02 21:06:17298 // because it's common for there to be one extension in a process
[email protected]561e33d52013-04-03 06:58:43299 // without isolated storage.
300 if (extension &&
301 extensions::AppIsolationInfo::HasIsolatedStorage(extension))
[email protected]404fb002012-10-02 21:06:17302 return extension;
303
304 return NULL;
[email protected]da5683db2011-04-23 17:12:21305}
306
[email protected]6aeac8342010-10-01 20:21:18307// static
[email protected]d6ebc9792011-04-07 18:18:33308// This function is used to implement the command-line switch
[email protected]3bdba0d2011-08-23 07:17:30309// --uninstall-extension, and to uninstall an extension via sync. The LOG
310// statements within this function are used to inform the user if the uninstall
311// cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00312bool ExtensionService::UninstallExtensionHelper(
313 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18314 const std::string& extension_id) {
[email protected]d6ebc9792011-04-07 18:18:33315 // We can't call UninstallExtension with an invalid extension ID.
[email protected]31bb5ee62012-09-12 22:58:40316 if (!extensions_service->GetInstalledExtension(extension_id)) {
[email protected]6aeac8342010-10-01 20:21:18317 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33318 << "id: " << extension_id;
319 return false;
[email protected]6aeac8342010-10-01 20:21:18320 }
321
[email protected]d6ebc9792011-04-07 18:18:33322 // The following call to UninstallExtension will not allow an uninstall of a
323 // policy-controlled extension.
[email protected]65187152012-06-02 13:14:14324 string16 error;
[email protected]d6ebc9792011-04-07 18:18:33325 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
326 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
327 << ": " << error;
328 return false;
329 }
[email protected]95da88c42011-03-31 10:07:33330
[email protected]6aeac8342010-10-01 20:21:18331 return true;
332}
333
[email protected]eaa7dd182010-12-14 11:09:00334ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24335 const CommandLine* command_line,
[email protected]650b2d52013-02-10 03:41:45336 const base::FilePath& install_directory,
[email protected]45759612012-07-10 17:21:23337 extensions::ExtensionPrefs* extension_prefs,
[email protected]fdd679b2012-11-15 20:49:39338 extensions::Blacklist* blacklist,
[email protected]0436b102011-04-15 18:30:03339 bool autoupdate_enabled,
[email protected]4a10006a2013-05-17 23:18:35340 bool extensions_enabled,
341 extensions::OneShotEvent* ready)
[email protected]fdd679b2012-11-15 20:49:39342 : extensions::Blacklist::Observer(blacklist),
343 profile_(profile),
[email protected]bd306722012-07-11 20:43:59344 system_(extensions::ExtensionSystem::Get(profile)),
[email protected]73c47932010-12-06 18:13:43345 extension_prefs_(extension_prefs),
[email protected]695b5712012-12-06 23:55:28346 blacklist_(blacklist),
[email protected]cccdf0aa2011-11-11 03:43:38347 settings_frontend_(extensions::SettingsFrontend::Create(profile)),
[email protected]9c009092013-05-01 03:14:09348 pending_extension_manager_(*this),
[email protected]a9b00ac2009-06-25 21:03:23349 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03350 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13351 show_extensions_prompts_(true),
[email protected]fc332ae2012-11-14 20:17:33352 install_updates_when_idle_(true),
[email protected]4a10006a2013-05-17 23:18:35353 ready_(ready),
[email protected]9c009092013-05-01 03:14:09354 toolbar_model_(this),
[email protected]8b280302011-10-13 22:22:23355 menu_manager_(profile),
[email protected]7c1490da2011-10-11 18:53:25356 event_routers_initialized_(false),
[email protected]dff1e042012-04-27 10:59:18357 update_once_all_providers_are_ready_(false),
[email protected]3c4abc82012-10-22 22:25:54358 browser_terminating_(false),
[email protected]399583b2012-12-11 09:33:42359 installs_delayed_(false),
[email protected]460c6712013-04-24 07:20:01360 is_first_run_(false),
[email protected]9c009092013-05-01 03:14:09361 app_sync_bundle_(this),
362 extension_sync_bundle_(this) {
[email protected]a29a517a2011-01-21 21:11:12363 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20364
[email protected]36a784c2009-06-23 06:21:08365 // Figure out if extension installation should be enabled.
[email protected]31bb5ee62012-09-12 22:58:40366 if (command_line->HasSwitch(switches::kDisableExtensions) ||
367 profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
[email protected]6d60703b2009-08-29 01:29:23368 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18369 }
[email protected]36a784c2009-06-23 06:21:08370
[email protected]3c4abc82012-10-22 22:25:54371 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
372 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27373 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07374 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27375 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
[email protected]ad50def52011-10-19 23:17:07376 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27377 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07378 content::NotificationService::AllBrowserContextsAndSources());
[email protected]0db124b02012-11-07 04:55:05379 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
380 content::NotificationService::AllBrowserContextsAndSources());
[email protected]92dd8d92013-02-26 00:41:08381 registrar_.Add(this, chrome::NOTIFICATION_UPGRADE_RECOMMENDED,
382 content::NotificationService::AllBrowserContextsAndSources());
[email protected]2fb7dc982010-09-29 12:24:28383 pref_change_registrar_.Init(profile->GetPrefs());
[email protected]90bb38d2012-11-14 18:36:03384 base::Closure callback =
385 base::Bind(&ExtensionService::OnExtensionInstallPrefChanged,
386 base::Unretained(this));
387 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, callback);
388 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, callback);
[email protected]e410b5f2012-12-14 14:02:24389 pref_change_registrar_.Add(prefs::kExtensionAllowedTypes, callback);
[email protected]4814b512009-11-07 00:12:29390
[email protected]93fd78f42009-07-10 16:43:17391 // Set up the ExtensionUpdater
392 if (autoupdate_enabled) {
393 int update_frequency = kDefaultUpdateFrequencySeconds;
394 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25395 base::StringToInt(command_line->GetSwitchValueASCII(
396 switches::kExtensionsUpdateFrequency),
397 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17398 }
[email protected]42a08162012-03-16 18:09:11399 updater_.reset(new extensions::ExtensionUpdater(this,
400 extension_prefs,
401 profile->GetPrefs(),
402 profile,
[email protected]fdd679b2012-11-15 20:49:39403 blacklist,
[email protected]42a08162012-03-16 18:09:11404 update_frequency));
[email protected]93fd78f42009-07-10 16:43:17405 }
406
[email protected]25ae0152011-11-18 14:40:02407 component_loader_.reset(
408 new extensions::ComponentLoader(this,
409 profile->GetPrefs(),
410 g_browser_process->local_state()));
[email protected]8e4560b62011-01-14 10:09:14411
[email protected]0436b102011-04-15 18:30:03412 if (extensions_enabled_) {
[email protected]ae4c37e2012-12-21 01:16:25413 extensions::ExternalProviderImpl::CreateExternalProviders(
414 this, profile_, &external_extension_providers_);
[email protected]873531342011-03-09 12:16:05415 }
[email protected]b671760b2010-07-15 21:13:47416
[email protected]fb82dcd2012-03-21 14:15:46417 // Set this as the ExtensionService for extension sorting to ensure it
418 // cause syncs if required.
419 extension_prefs_->extension_sorting()->SetExtensionService(this);
420
[email protected]b3aa7182013-04-25 04:45:23421 is_first_run_ = !extension_prefs_->SetAlertSystemFirstRun();
422
[email protected]9ce11d22012-08-08 23:20:13423#if defined(ENABLE_EXTENSIONS)
[email protected]c77f2352012-08-08 22:07:58424 extension_action_storage_manager_.reset(
425 new extensions::ExtensionActionStorageManager(profile_));
[email protected]9ce11d22012-08-08 23:20:13426#endif
[email protected]c77f2352012-08-08 22:07:58427
[email protected]cb0e50312011-05-09 15:03:07428 // How long is the path to the Extensions directory?
429 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
430 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25431}
432
[email protected]84df8332011-12-06 18:22:46433const ExtensionSet* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45434 return &extensions_;
435}
436
[email protected]84df8332011-12-06 18:22:46437const ExtensionSet* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45438 return &disabled_extensions_;
439}
440
[email protected]84df8332011-12-06 18:22:46441const ExtensionSet* ExtensionService::terminated_extensions() const {
[email protected]bb7f40952011-01-13 00:21:20442 return &terminated_extensions_;
443}
444
[email protected]695b5712012-12-06 23:55:28445const ExtensionSet* ExtensionService::blacklisted_extensions() const {
446 return &blacklisted_extensions_;
447}
448
449scoped_ptr<const ExtensionSet>
450 ExtensionService::GenerateInstalledExtensionsSet() const {
451 scoped_ptr<ExtensionSet> installed_extensions(new ExtensionSet());
[email protected]7f4308d2012-01-18 07:43:01452 installed_extensions->InsertAll(extensions_);
453 installed_extensions->InsertAll(disabled_extensions_);
454 installed_extensions->InsertAll(terminated_extensions_);
[email protected]695b5712012-12-06 23:55:28455 installed_extensions->InsertAll(blacklisted_extensions_);
456 return installed_extensions.PassAs<const ExtensionSet>();
[email protected]7f4308d2012-01-18 07:43:01457}
458
[email protected]3f213ad2012-07-26 23:39:41459extensions::PendingExtensionManager*
460 ExtensionService::pending_extension_manager() {
[email protected]b2907fd2011-03-25 16:43:37461 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45462}
463
[email protected]eaa7dd182010-12-14 11:09:00464ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17465 // No need to unload extensions here because they are profile-scoped, and the
466 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17467
[email protected]5df038b2012-07-16 19:03:27468 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:14469 for (i = external_extension_providers_.begin();
470 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:27471 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:14472 provider->ServiceShutdown();
473 }
[email protected]6014d672008-12-05 00:38:25474}
475
[email protected]d91268022011-08-26 13:17:37476void ExtensionService::InitEventRoutersAfterImport() {
[email protected]cf593af2011-12-30 05:44:39477 RegisterForImportFinished();
478}
479
480void ExtensionService::RegisterForImportFinished() {
481 if (!registrar_.IsRegistered(this, chrome::NOTIFICATION_IMPORT_FINISHED,
482 content::Source<Profile>(profile_))) {
483 registrar_.Add(this, chrome::NOTIFICATION_IMPORT_FINISHED,
484 content::Source<Profile>(profile_));
485 }
486}
487
488void ExtensionService::InitAfterImport() {
[email protected]21f2e2d2013-01-23 22:59:52489 startup_metric_utils::ScopedSlowStartupUMA
490 scoped_timer("Startup.SlowStartupExtensionServiceInitAfterImport");
[email protected]eac88332012-12-26 17:57:45491 component_loader_->LoadAll();
[email protected]ae4c37e2012-12-21 01:16:25492
[email protected]cf593af2011-12-30 05:44:39493 CheckForExternalUpdates();
494
495 GarbageCollectExtensions();
496
497 // Idempotent, so although there is a possible race if the import
498 // process finished sometime in the middle of ProfileImpl::InitExtensions,
499 // it cannot happen twice.
500 InitEventRouters();
[email protected]d91268022011-08-26 13:17:37501}
502
[email protected]eaa7dd182010-12-14 11:09:00503void ExtensionService::InitEventRouters() {
[email protected]b3d62312b12010-10-14 21:10:18504 if (event_routers_initialized_)
505 return;
506
[email protected]6a3cd37e2012-04-17 17:13:34507#if defined(ENABLE_EXTENSIONS)
[email protected]5a38dfd2012-07-23 23:22:10508 browser_event_router_.reset(new extensions::BrowserEventRouter(profile_));
[email protected]6a3cd37e2012-04-17 17:13:34509#endif // defined(ENABLE_EXTENSIONS)
[email protected]b3d62312b12010-10-14 21:10:18510 event_routers_initialized_ = true;
[email protected]c5ae74ab2010-04-15 18:14:37511}
512
[email protected]037228a2012-10-05 01:36:16513void ExtensionService::Shutdown() {
514 // Do nothing for now.
515}
516
[email protected]b2907fd2011-03-25 16:43:37517const Extension* ExtensionService::GetExtensionById(
518 const std::string& id, bool include_disabled) const {
[email protected]81f0d162012-08-15 04:21:47519 int include_mask = INCLUDE_ENABLED;
[email protected]695b5712012-12-06 23:55:28520 if (include_disabled) {
521 // Include blacklisted extensions here because there are hundreds of
522 // callers of this function, and many might assume that this includes those
523 // that have been disabled due to blacklisting.
524 include_mask |= INCLUDE_DISABLED | INCLUDE_BLACKLISTED;
525 }
526 return GetExtensionById(id, include_mask);
527}
528
[email protected]399583b2012-12-11 09:33:42529GURL ExtensionService::GetSiteForExtensionId(const std::string& extension_id) {
530 return content::SiteInstance::GetSiteForURL(
531 profile_,
532 Extension::GetBaseURLFromExtensionId(extension_id));
533}
534
[email protected]695b5712012-12-06 23:55:28535const Extension* ExtensionService::GetExtensionById(
536 const std::string& id, int include_mask) const {
537 std::string lowercase_id = StringToLowerASCII(id);
538 if (include_mask & INCLUDE_ENABLED) {
539 const Extension* extension = extensions_.GetByID(lowercase_id);
540 if (extension)
541 return extension;
542 }
543 if (include_mask & INCLUDE_DISABLED) {
544 const Extension* extension = disabled_extensions_.GetByID(lowercase_id);
545 if (extension)
546 return extension;
547 }
548 if (include_mask & INCLUDE_TERMINATED) {
549 const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
550 if (extension)
551 return extension;
552 }
553 if (include_mask & INCLUDE_BLACKLISTED) {
554 const Extension* extension = blacklisted_extensions_.GetByID(lowercase_id);
555 if (extension)
556 return extension;
557 }
558 return NULL;
[email protected]78994ab02010-12-08 18:06:44559}
560
[email protected]eaa7dd182010-12-14 11:09:00561void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12562 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20563
[email protected]4a10006a2013-05-17 23:18:35564 DCHECK(!is_ready()); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17565 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32566
[email protected]820d9bd2013-04-03 03:46:03567 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
[email protected]820d9bd2013-04-03 03:46:03568 if (cmd_line->HasSwitch(switches::kInstallFromWebstore) ||
569 cmd_line->HasSwitch(switches::kLimitedInstallFromWebstore)) {
570 // The sole purpose of this launch is to install a new extension from CWS
571 // and immediately terminate: loading already installed extensions is
572 // unnecessary and may interfere with the inline install dialog (e.g. if an
573 // extension listens to onStartup and opens a window).
574 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25575 } else {
[email protected]820d9bd2013-04-03 03:46:03576 // TODO(mek): It might be cleaner to do the FinishDelayedInstallInfo stuff
577 // here instead of in installedloader.
578 if (g_browser_process->profile_manager() &&
579 g_browser_process->profile_manager()->will_import()) {
580 // Do not load any component extensions, since they may conflict with the
581 // import process.
[email protected]ae4c37e2012-12-21 01:16:25582
[email protected]820d9bd2013-04-03 03:46:03583 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]4a10006a2013-05-17 23:18:35584 SetReadyAndNotifyListeners();
[email protected]820d9bd2013-04-03 03:46:03585 RegisterForImportFinished();
586 } else {
[email protected]4a10006a2013-05-17 23:18:35587 // In this case, LoadAllExtensions() calls OnLoadedInstalledExtensions().
[email protected]820d9bd2013-04-03 03:46:03588 component_loader_->LoadAll();
589 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]4a10006a2013-05-17 23:18:35590 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25591
[email protected]820d9bd2013-04-03 03:46:03592 // TODO(erikkay) this should probably be deferred to a future point
593 // rather than running immediately at startup.
594 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57595
[email protected]820d9bd2013-04-03 03:46:03596 // TODO(erikkay) this should probably be deferred as well.
597 GarbageCollectExtensions();
598 }
[email protected]215a7be2012-10-22 19:53:42599
[email protected]820d9bd2013-04-03 03:46:03600 if (extension_prefs_->NeedsStorageGarbageCollection()) {
601 GarbageCollectIsolatedStorage();
602 extension_prefs_->SetNeedsStorageGarbageCollection(false);
603 }
[email protected]399583b2012-12-11 09:33:42604 }
[email protected]6014d672008-12-05 00:38:25605}
606
[email protected]31bb5ee62012-09-12 22:58:40607bool ExtensionService::UpdateExtension(const std::string& id,
[email protected]650b2d52013-02-10 03:41:45608 const base::FilePath& extension_path,
[email protected]31bb5ee62012-09-12 22:58:40609 const GURL& download_url,
610 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12611 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3c4abc82012-10-22 22:25:54612 if (browser_terminating_) {
613 LOG(WARNING) << "Skipping UpdateExtension due to browser shutdown";
614 // Leak the temp file at extension_path. We don't want to add to the disk
615 // I/O burden at shutdown, we can't rely on the I/O completing anyway, and
616 // the file is in the OS temp directory which should be cleaned up for us.
617 return false;
618 }
[email protected]a8af9fdb2010-10-28 21:52:20619
[email protected]3f213ad2012-07-26 23:39:41620 const extensions::PendingExtensionInfo* pending_extension_info =
[email protected]51a3bf8b2012-06-08 22:53:06621 pending_extension_manager()->GetById(id);
[email protected]8ef78fd2010-08-19 17:14:32622
[email protected]695b5712012-12-06 23:55:28623 const Extension* extension = GetInstalledExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06624 if (!pending_extension_info && !extension) {
[email protected]aa142702010-03-26 01:26:33625 LOG(WARNING) << "Will not update extension " << id
626 << " because it is not installed or pending";
627 // Delete extension_path since we're not creating a CrxInstaller
628 // that would do it for us.
[email protected]7f8f24f2012-11-15 19:40:14629 if (!GetFileTaskRunner()->PostTask(
630 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36631 base::Bind(
[email protected]7296f2762011-11-21 19:23:44632 &extension_file_util::DeleteFile, extension_path, false)))
[email protected]14908b72011-04-20 06:54:36633 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03634
635 return false;
[email protected]e957fe52009-06-23 16:51:05636 }
637
[email protected]aa142702010-03-26 01:26:33638 // We want a silent install only for non-pending extensions and
639 // pending extensions that have install_silently set.
[email protected]91e51d612012-10-21 23:03:05640 ExtensionInstallPrompt* client = NULL;
641 if (pending_extension_info && !pending_extension_info->install_silently())
642 client = ExtensionInstallUI::CreateInstallPromptWithProfile(profile_);
[email protected]aa142702010-03-26 01:26:33643
[email protected]d8c8f25f2011-11-02 18:18:01644 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(this, client));
[email protected]6dfbbf82010-03-12 23:09:16645 installer->set_expected_id(id);
[email protected]51a3bf8b2012-06-08 22:53:06646 if (pending_extension_info) {
647 installer->set_install_source(pending_extension_info->install_source());
648 if (pending_extension_info->install_silently())
649 installer->set_allow_silent_install(true);
650 } else if (extension) {
[email protected]5eb375e92010-11-26 07:50:41651 installer->set_install_source(extension->location());
[email protected]51a3bf8b2012-06-08 22:53:06652 }
[email protected]7d8b7072012-04-07 01:07:46653 // If the extension was installed from or has migrated to the webstore, or
[email protected]fcb2c2c2012-10-17 21:08:45654 // its auto-update URL is from the webstore, treat it as a webstore install.
655 // Note that we ignore some older extensions with blank auto-update URLs
656 // because we are mostly concerned with restrictions on NaCl extensions,
657 // which are newer.
[email protected]a12ce8b22012-01-17 18:40:53658 int creation_flags = Extension::NO_FLAGS;
[email protected]75764512011-12-19 19:54:28659 if ((extension && extension->from_webstore()) ||
[email protected]7d8b7072012-04-07 01:07:46660 (extension && extension->UpdatesFromGallery()) ||
[email protected]963d13c2012-09-29 17:13:35661 (!extension && extension_urls::IsWebstoreUpdateUrl(
[email protected]51a3bf8b2012-06-08 22:53:06662 pending_extension_info->update_url()))) {
[email protected]a12ce8b22012-01-17 18:40:53663 creation_flags |= Extension::FROM_WEBSTORE;
664 }
665
666 // Bookmark apps being updated is kind of a contradiction, but that's because
667 // we mark the default apps as bookmark apps, and they're hosted in the web
668 // store, thus they can get updated. See http://crbug.com/101605 for more
669 // details.
670 if (extension && extension->from_bookmark())
671 creation_flags |= Extension::FROM_BOOKMARK;
672
[email protected]e33bbc22012-08-27 22:05:46673 if (extension && extension->was_installed_by_default())
674 creation_flags |= Extension::WAS_INSTALLED_BY_DEFAULT;
675
[email protected]a12ce8b22012-01-17 18:40:53676 installer->set_creation_flags(creation_flags);
677
[email protected]6dfbbf82010-03-12 23:09:16678 installer->set_delete_source(true);
[email protected]655b2b1a2011-10-13 17:13:06679 installer->set_download_url(download_url);
[email protected]cb0e50312011-05-09 15:03:07680 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16681 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03682
683 if (out_crx_installer)
684 *out_crx_installer = installer;
685
686 return true;
[email protected]e957fe52009-06-23 16:51:05687}
688
[email protected]eaa7dd182010-12-14 11:09:00689void ExtensionService::ReloadExtension(const std::string& extension_id) {
[email protected]32b36c62012-10-24 05:37:41690 int events = HasShellWindows(extension_id) ? EVENT_LAUNCHED : EVENT_NONE;
691 ReloadExtensionWithEvents(extension_id, events);
692}
693
694void ExtensionService::RestartExtension(const std::string& extension_id) {
[email protected]2380c6b2013-01-09 02:33:13695 ReloadExtensionWithEvents(extension_id, EVENT_RESTARTED);
[email protected]32b36c62012-10-24 05:37:41696}
697
698void ExtensionService::ReloadExtensionWithEvents(
699 const std::string& extension_id,
700 int events) {
[email protected]a29a517a2011-01-21 21:11:12701 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]2a947202013-03-06 04:58:05702
703 // If the extension is already reloading, don't reload again.
704 if (extension_prefs_->GetDisableReasons(extension_id) &
705 Extension::DISABLE_RELOAD) {
706 return;
707 }
708
[email protected]650b2d52013-02-10 03:41:45709 base::FilePath path;
[email protected]9adb9692010-10-29 23:14:02710 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40711
[email protected]f17dbd42010-08-16 23:21:10712 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06713 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29714 // If the extension has an inspector open for its background page, detach
715 // the inspector and hang onto a cookie for it, so that we can reattach
716 // later.
[email protected]31d8f5f22012-04-02 15:22:08717 // TODO(yoz): this is not incognito-safe!
718 ExtensionProcessManager* manager = system_->process_manager();
[email protected]3a1dc572012-07-31 22:25:13719 extensions::ExtensionHost* host =
720 manager->GetBackgroundHostForExtension(extension_id);
[email protected]98f66112012-12-25 12:59:36721 if (host && DevToolsAgentHost::HasFor(host->render_view_host())) {
[email protected]4814b512009-11-07 00:12:29722 // Look for an open inspector for the background page.
[email protected]ead056352013-03-12 08:47:54723 std::string devtools_cookie = DevToolsAgentHost::DisconnectRenderViewHost(
[email protected]562e20c2012-12-24 18:27:20724 host->render_view_host());
[email protected]ead056352013-03-12 08:47:54725 if (devtools_cookie != std::string())
[email protected]4814b512009-11-07 00:12:29726 orphaned_dev_tools_[extension_id] = devtools_cookie;
727 }
728
[email protected]b65272f2009-08-31 15:47:06729 path = current_extension->path();
[email protected]44d62b62012-04-11 00:06:03730 DisableExtension(extension_id, Extension::DISABLE_RELOAD);
[email protected]b914e2952013-04-26 07:10:03731 reloading_extensions_.insert(extension_id);
[email protected]1eb175082010-02-10 09:26:16732 } else {
733 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06734 }
735
[email protected]8b6d9642013-02-01 03:37:40736 on_load_events_[extension_id] = events;
[email protected]8b6d9642013-02-01 03:37:40737
[email protected]399583b2012-12-11 09:33:42738 if (delayed_updates_for_idle_.Contains(extension_id)) {
[email protected]6f6101832012-11-27 22:10:48739 FinishDelayedInstallation(extension_id);
[email protected]0db124b02012-11-07 04:55:05740 return;
741 }
742
[email protected]43ceb002012-02-10 23:19:15743 // If we're reloading a component extension, use the component extension
744 // loader's reloader.
745 if (component_loader_->Exists(extension_id)) {
746 component_loader_->Reload(extension_id);
747 return;
748 }
749
[email protected]e6090e42010-03-23 22:44:08750 // Check the installed extensions to see if what we're reloading was already
751 // installed.
752 scoped_ptr<ExtensionInfo> installed_extension(
753 extension_prefs_->GetInstalledExtensionInfo(extension_id));
754 if (installed_extension.get() &&
755 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01756 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08757 } else {
[email protected]d8c8f25f2011-11-02 18:18:01758 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08759 // We should always be able to remember the extension's path. If it's not in
760 // the map, someone failed to update |unloaded_extension_paths_|.
761 CHECK(!path.empty());
[email protected]d8c8f25f2011-11-02 18:18:01762 extensions::UnpackedInstaller::Create(this)->Load(path);
[email protected]e6090e42010-03-23 22:44:08763 }
[email protected]9cddd4702009-07-27 22:09:40764}
765
[email protected]fa2416f2011-05-03 08:41:20766bool ExtensionService::UninstallExtension(
[email protected]a12c706e2011-12-01 00:58:08767 std::string extension_id,
[email protected]fa2416f2011-05-03 08:41:20768 bool external_uninstall,
[email protected]65187152012-06-02 13:14:14769 string16* error) {
[email protected]a29a517a2011-01-21 21:11:12770 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20771
[email protected]0d6ec3a72011-09-02 02:09:43772 scoped_refptr<const Extension> extension(GetInstalledExtension(extension_id));
[email protected]631cf822009-05-15 07:01:25773
[email protected]e7afe2452010-08-22 16:19:13774 // Callers should not send us nonexistent extensions.
[email protected]aebe23a32010-12-10 22:15:48775 CHECK(extension);
[email protected]9f1087e2009-06-15 17:29:32776
[email protected]95da88c42011-03-31 10:07:33777 // Policy change which triggers an uninstall will always set
778 // |external_uninstall| to true so this is the only way to uninstall
779 // managed extensions.
[email protected]65187152012-06-02 13:14:14780 if (!external_uninstall &&
781 !system_->management_policy()->UserMayModifySettings(
782 extension.get(), error)) {
[email protected]ad50def52011-10-19 23:17:07783 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27784 chrome::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53785 content::Source<Profile>(profile_),
786 content::Details<const Extension>(extension));
[email protected]d6ebc9792011-04-07 18:18:33787 return false;
788 }
[email protected]95da88c42011-03-31 10:07:33789
[email protected]3bdba0d2011-08-23 07:17:30790 // Extract the data we need for sync now, but don't actually sync until we've
791 // completed the uninstallation.
[email protected]65f173552012-06-28 22:43:58792 syncer::SyncChange sync_change;
[email protected]5db9ada2012-04-11 13:48:20793 if (app_sync_bundle_.HandlesApp(*extension)) {
794 sync_change = app_sync_bundle_.CreateSyncChangeToDelete(extension);
795 } else if (extension_sync_bundle_.HandlesExtension(*extension)) {
796 sync_change = extension_sync_bundle_.CreateSyncChangeToDelete(extension);
[email protected]3bdba0d2011-08-23 07:17:30797 }
798
[email protected]d96eb512012-11-01 23:44:08799 if (IsUnacknowledgedExternalExtension(extension)) {
800 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
801 EXTERNAL_EXTENSION_UNINSTALLED,
802 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
803 }
[email protected]9b217652010-10-08 22:04:23804 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08805 extension->GetType(), 100);
[email protected]fe2dd7742011-04-19 22:52:49806 RecordPermissionMessagesHistogram(
807 extension, "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23808
[email protected]831aa212010-03-26 13:55:19809 // Unload before doing more cleanup to ensure that nothing is hanging on to
810 // any of these resources.
[email protected]814a7bf0f2011-08-13 05:30:59811 UnloadExtension(extension_id, extension_misc::UNLOAD_REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19812
[email protected]0d6ec3a72011-09-02 02:09:43813 extension_prefs_->OnExtensionUninstalled(extension_id, extension->location(),
[email protected]831aa212010-03-26 13:55:19814 external_uninstall);
[email protected]9f1087e2009-06-15 17:29:32815
816 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]12075d12013-02-27 05:38:05817 if (!Manifest::IsUnpackedLocation(extension->location())) {
[email protected]7f8f24f2012-11-15 19:40:14818 if (!GetFileTaskRunner()->PostTask(
819 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36820 base::Bind(
[email protected]14908b72011-04-20 06:54:36821 &extension_file_util::UninstallExtension,
822 install_directory_,
[email protected]fa2416f2011-05-03 08:41:20823 extension_id)))
[email protected]14908b72011-04-20 06:54:36824 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32825 }
826
[email protected]0d6ec3a72011-09-02 02:09:43827 GURL launch_web_url_origin(extension->launch_web_url());
828 launch_web_url_origin = launch_web_url_origin.GetOrigin();
[email protected]561e33d52013-04-03 06:58:43829 bool is_storage_isolated =
830 extensions::AppIsolationInfo::HasIsolatedStorage(extension);
[email protected]0d6ec3a72011-09-02 02:09:43831
[email protected]399583b2012-12-11 09:33:42832 if (is_storage_isolated) {
833 BrowserContext::AsyncObliterateStoragePartition(
834 profile_,
835 GetSiteForExtensionId(extension_id),
836 base::Bind(&ExtensionService::OnNeedsToGarbageCollectIsolatedStorage,
837 AsWeakPtr()));
838 } else {
839 if (extension->is_hosted_app() &&
840 !profile_->GetExtensionSpecialStoragePolicy()->
841 IsStorageProtected(launch_web_url_origin)) {
842 extensions::DataDeleter::StartDeleting(
843 profile_, extension_id, launch_web_url_origin);
844 }
845 extensions::DataDeleter::StartDeleting(profile_, extension_id,
846 extension->url());
[email protected]0d6ec3a72011-09-02 02:09:43847 }
[email protected]0d6ec3a72011-09-02 02:09:43848
[email protected]0dfe05c2011-02-23 23:03:36849 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06850
851 // Notify interested parties that we've uninstalled this extension.
[email protected]ad50def52011-10-19 23:17:07852 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27853 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]6c2381d2011-10-19 02:52:53854 content::Source<Profile>(profile_),
[email protected]d67d9412012-07-17 17:19:42855 content::Details<const Extension>(extension));
[email protected]d6ebc9792011-04-07 18:18:33856
[email protected]5db9ada2012-04-11 13:48:20857 if (app_sync_bundle_.HasExtensionId(extension_id) &&
[email protected]a4a147652012-07-03 23:41:32858 sync_change.sync_data().GetDataType() == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:20859 app_sync_bundle_.ProcessDeletion(extension_id, sync_change);
860 } else if (extension_sync_bundle_.HasExtensionId(extension_id) &&
[email protected]a4a147652012-07-03 23:41:32861 sync_change.sync_data().GetDataType() == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:20862 extension_sync_bundle_.ProcessDeletion(extension_id, sync_change);
[email protected]3bdba0d2011-08-23 07:17:30863 }
864
[email protected]399583b2012-12-11 09:33:42865 delayed_updates_for_idle_.Remove(extension_id);
866 delayed_installs_.Remove(extension_id);
[email protected]0db124b02012-11-07 04:55:05867
[email protected]333b1de2011-09-12 18:28:50868 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18869 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
870
[email protected]d6ebc9792011-04-07 18:18:33871 return true;
[email protected]c10da4b02010-03-25 14:38:32872}
873
[email protected]c3cfb012011-04-06 22:07:35874bool ExtensionService::IsExtensionEnabled(
875 const std::string& extension_id) const {
[email protected]84df8332011-12-06 18:22:46876 if (extensions_.Contains(extension_id) ||
[email protected]dc9a74f72012-08-17 18:07:21877 terminated_extensions_.Contains(extension_id)) {
[email protected]36429da2011-07-11 20:25:18878 return true;
[email protected]dc9a74f72012-08-17 18:07:21879 }
[email protected]36429da2011-07-11 20:25:18880
[email protected]695b5712012-12-06 23:55:28881 if (disabled_extensions_.Contains(extension_id) ||
882 blacklisted_extensions_.Contains(extension_id)) {
[email protected]ad83ca242011-07-29 01:32:25883 return false;
[email protected]695b5712012-12-06 23:55:28884 }
[email protected]ad83ca242011-07-29 01:32:25885
886 // If the extension hasn't been loaded yet, check the prefs for it. Assume
887 // enabled unless otherwise noted.
888 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
[email protected]f574c402012-12-04 23:20:31889 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35890}
891
892bool ExtensionService::IsExternalExtensionUninstalled(
893 const std::string& extension_id) const {
894 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
895}
896
[email protected]e2377032013-01-18 17:53:22897bool ExtensionService::IsExtensionEnabledForLauncher(
898 const std::string& extension_id) const {
899 return IsExtensionEnabled(extension_id) &&
900 !GetTerminatedExtension(extension_id);
901}
902
[email protected]eaa7dd182010-12-14 11:09:00903void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12904 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20905
[email protected]06f92562011-04-29 19:27:31906 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39907 return;
[email protected]0c6da502009-08-14 22:32:39908
[email protected]b3317ad2011-04-28 23:46:00909 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]eb5e4f92012-08-15 23:33:28910 extension_prefs_->ClearDisableReasons(extension_id);
[email protected]1784e83a2009-09-08 21:01:52911
[email protected]695b5712012-12-06 23:55:28912 const Extension* extension = disabled_extensions_.GetByID(extension_id);
[email protected]06f92562011-04-29 19:27:31913 // This can happen if sync enables an extension that is not
914 // installed yet.
915 if (!extension)
916 return;
917
[email protected]d96eb512012-11-01 23:44:08918 if (IsUnacknowledgedExternalExtension(extension)) {
919 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
920 EXTERNAL_EXTENSION_REENABLED,
921 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]612a1cb12012-10-17 13:18:03922 AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:08923 }
[email protected]612a1cb12012-10-17 13:18:03924
[email protected]0c6da502009-08-14 22:32:39925 // Move it over to the enabled list.
[email protected]84df8332011-12-06 18:22:46926 extensions_.Insert(make_scoped_refptr(extension));
927 disabled_extensions_.Remove(extension->id());
[email protected]0c6da502009-08-14 22:32:39928
[email protected]62d30f42009-10-01 22:36:06929 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30930
[email protected]f74d7f32012-06-19 19:22:51931 // Notify listeners that the extension was enabled.
932 content::NotificationService::current()->Notify(
933 chrome::NOTIFICATION_EXTENSION_ENABLED,
934 content::Source<Profile>(profile_),
935 content::Details<const Extension>(extension));
936
[email protected]3bdba0d2011-08-23 07:17:30937 SyncExtensionChangeIfNeeded(*extension);
[email protected]0c6da502009-08-14 22:32:39938}
939
[email protected]44d62b62012-04-11 00:06:03940void ExtensionService::DisableExtension(
941 const std::string& extension_id,
942 Extension::DisableReason disable_reason) {
[email protected]a29a517a2011-01-21 21:11:12943 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20944
[email protected]b2ba9962009-12-10 20:10:15945 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:31946 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:52947 return;
[email protected]1784e83a2009-09-08 21:01:52948
[email protected]06f92562011-04-29 19:27:31949 const Extension* extension = GetInstalledExtension(extension_id);
950 // |extension| can be NULL if sync disables an extension that is not
951 // installed yet.
[email protected]65187152012-06-02 13:14:14952 if (extension &&
953 !system_->management_policy()->UserMayModifySettings(extension, NULL)) {
[email protected]95da88c42011-03-31 10:07:33954 return;
[email protected]65187152012-06-02 13:14:14955 }
[email protected]95da88c42011-03-31 10:07:33956
[email protected]b3317ad2011-04-28 23:46:00957 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]eb5e4f92012-08-15 23:33:28958 extension_prefs_->AddDisableReason(extension_id, disable_reason);
[email protected]1784e83a2009-09-08 21:01:52959
[email protected]695b5712012-12-06 23:55:28960 int include_mask = INCLUDE_EVERYTHING & ~INCLUDE_DISABLED;
961 extension = GetExtensionById(extension_id, include_mask);
[email protected]06f92562011-04-29 19:27:31962 if (!extension)
963 return;
964
[email protected]64742de32013-04-22 08:44:34965 // Reset the background_page_ready flag
966 if (extensions::BackgroundInfo::HasBackgroundPage(extension))
967 extension_runtime_data_[extension->id()].background_page_ready = false;
968
[email protected]5c094792012-09-07 19:44:53969 // Move it over to the disabled list. Don't send a second unload notification
970 // for terminated extensions being disabled.
[email protected]84df8332011-12-06 18:22:46971 disabled_extensions_.Insert(make_scoped_refptr(extension));
[email protected]5c094792012-09-07 19:44:53972 if (extensions_.Contains(extension->id())) {
[email protected]84df8332011-12-06 18:22:46973 extensions_.Remove(extension->id());
[email protected]5c094792012-09-07 19:44:53974 NotifyExtensionUnloaded(extension, extension_misc::UNLOAD_REASON_DISABLE);
975 } else {
[email protected]84df8332011-12-06 18:22:46976 terminated_extensions_.Remove(extension->id());
[email protected]5c094792012-09-07 19:44:53977 }
[email protected]3bdba0d2011-08-23 07:17:30978
979 SyncExtensionChangeIfNeeded(*extension);
[email protected]1784e83a2009-09-08 21:01:52980}
981
[email protected]eaa7dd182010-12-14 11:09:00982void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]009633c2013-03-07 22:08:28983 const Extension* extension) {
984 GrantPermissions(extension);
[email protected]fe2dd7742011-04-19 22:52:49985 RecordPermissionMessagesHistogram(
986 extension, "Extensions.Permissions_ReEnable");
[email protected]8d888c12010-11-30 00:00:25987 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
988 EnableExtension(extension->id());
989}
990
[email protected]009633c2013-03-07 22:08:28991void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]be083862012-09-01 03:53:45992 CHECK(extension);
993 extensions::PermissionsUpdater perms_updater(profile());
[email protected]009633c2013-03-07 22:08:28994 perms_updater.GrantActivePermissions(extension);
[email protected]be083862012-09-01 03:53:45995}
996
[email protected]fe2dd7742011-04-19 22:52:49997// static
998void ExtensionService::RecordPermissionMessagesHistogram(
[email protected]13c68b62013-05-17 11:29:05999 const Extension* extension, const char* histogram) {
[email protected]de415552013-01-23 04:12:171000 // Since this is called from multiple sources, and since the histogram macros
1001 // use statics, we need to manually lookup the histogram ourselves.
1002 base::HistogramBase* counter = base::LinearHistogram::FactoryGet(
[email protected]fe2dd7742011-04-19 22:52:491003 histogram,
1004 1,
[email protected]c2e66e12012-06-27 06:27:061005 PermissionMessage::kEnumBoundary,
1006 PermissionMessage::kEnumBoundary + 1,
[email protected]de415552013-01-23 04:12:171007 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]fe2dd7742011-04-19 22:52:491008
[email protected]13c68b62013-05-17 11:29:051009 PermissionMessages permissions =
1010 extensions::PermissionsData::GetPermissionMessages(extension);
[email protected]fe2dd7742011-04-19 22:52:491011 if (permissions.empty()) {
[email protected]c2e66e12012-06-27 06:27:061012 counter->Add(PermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:491013 } else {
[email protected]c2e66e12012-06-27 06:27:061014 for (PermissionMessages::iterator it = permissions.begin();
[email protected]0d3e4a22011-06-23 19:02:521015 it != permissions.end(); ++it)
1016 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:491017 }
1018}
1019
[email protected]eaa7dd182010-12-14 11:09:001020void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:421021 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061022 // was loaded, otherwise a race can arise where a renderer that is created
1023 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421024 // that the request context doesn't yet know about. The profile is responsible
1025 // for ensuring its URLRequestContexts appropriately discover the loaded
1026 // extension.
[email protected]31d8f5f22012-04-02 15:22:081027 system_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:061028
[email protected]d5949312012-12-03 22:13:301029 // Tell renderers about the new extension, unless it's a theme (renderers
1030 // don't need to know about themes).
[email protected]a58f599c2012-12-01 03:08:191031 if (!extension->is_theme()) {
[email protected]19647262011-12-16 09:57:491032 for (content::RenderProcessHost::iterator i(
1033 content::RenderProcessHost::AllHostsIterator());
1034 !i.IsAtEnd(); i.Advance()) {
1035 content::RenderProcessHost* host = i.GetCurrentValue();
1036 Profile* host_profile =
1037 Profile::FromBrowserContext(host->GetBrowserContext());
1038 if (host_profile->GetOriginalProfile() ==
1039 profile_->GetOriginalProfile()) {
1040 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
1041 1, ExtensionMsg_Loaded_Params(extension));
1042 host->Send(
1043 new ExtensionMsg_Loaded(loaded_extensions));
1044 }
[email protected]c8d407e2011-04-28 21:27:171045 }
[email protected]77a6970c2011-04-23 16:58:561046 }
1047
[email protected]3442a662012-01-12 08:06:171048 // Tell subsystems that use the EXTENSION_LOADED notification about the new
1049 // extension.
1050 //
1051 // NOTE: It is important that this happen after notifying the renderers about
1052 // the new extensions so that if we navigate to an extension URL in
1053 // NOTIFICATION_EXTENSION_LOADED, the renderer is guaranteed to know about it.
1054 content::NotificationService::current()->Notify(
1055 chrome::NOTIFICATION_EXTENSION_LOADED,
1056 content::Source<Profile>(profile_),
1057 content::Details<const Extension>(extension));
1058
[email protected]c8d407e2011-04-28 21:27:171059 // Tell a random-ass collection of other subsystems about the new extension.
1060 // TODO(aa): What should we do with all this goop? Can it move into the
1061 // relevant objects via EXTENSION_LOADED?
1062
1063 profile_->GetExtensionSpecialStoragePolicy()->
1064 GrantRightsForExtension(extension);
1065
1066 UpdateActiveExtensionsInCrashReporter();
1067
[email protected]c8d407e2011-04-28 21:27:171068 // If the extension has permission to load chrome://favicon/ resources we need
1069 // to make sure that the FaviconSource is registered with the
1070 // ChromeURLDataManager.
[email protected]13c68b62013-05-17 11:29:051071 if (extensions::PermissionsData::HasHostPermission(
1072 extension, GURL(chrome::kChromeUIFaviconURL))) {
[email protected]c8d407e2011-04-28 21:27:171073 FaviconSource* favicon_source = new FaviconSource(profile_,
1074 FaviconSource::FAVICON);
[email protected]24ea7a12013-01-27 23:54:531075 content::URLDataSource::Add(profile_, favicon_source);
[email protected]c8d407e2011-04-28 21:27:171076 }
[email protected]b07e606e2012-09-15 20:16:151077
1078#if !defined(OS_ANDROID)
1079 // Same for chrome://theme/ resources.
[email protected]13c68b62013-05-17 11:29:051080 if (extensions::PermissionsData::HasHostPermission(
1081 extension, GURL(chrome::kChromeUIThemeURL))) {
[email protected]b07e606e2012-09-15 20:16:151082 ThemeSource* theme_source = new ThemeSource(profile_);
[email protected]24ea7a12013-01-27 23:54:531083 content::URLDataSource::Add(profile_, theme_source);
[email protected]b07e606e2012-09-15 20:16:151084 }
1085#endif
1086
[email protected]5eddc3e2011-10-26 04:33:311087 // Same for chrome://thumb/ resources.
[email protected]13c68b62013-05-17 11:29:051088 if (extensions::PermissionsData::HasHostPermission(
1089 extension, GURL(chrome::kChromeUIThumbnailURL))) {
[email protected]5eddc3e2011-10-26 04:33:311090 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_);
[email protected]24ea7a12013-01-27 23:54:531091 content::URLDataSource::Add(profile_, thumbnail_source);
[email protected]5eddc3e2011-10-26 04:33:311092 }
[email protected]c8d407e2011-04-28 21:27:171093
[email protected]ebd71962012-12-20 02:56:551094#if defined(ENABLE_PLUGINS)
[email protected]c8d407e2011-04-28 21:27:171095 // TODO(mpcomplete): This ends up affecting all profiles. See crbug.com/80757.
[email protected]2de307592011-04-05 21:16:581096 bool plugins_changed = false;
[email protected]098fa7a2013-03-08 22:11:171097 if (extensions::PluginInfo::HasPlugins(extension)) {
1098 const extensions::PluginInfo::PluginVector* plugins =
1099 extensions::PluginInfo::GetPlugins(extension);
1100 CHECK(plugins);
[email protected]2de307592011-04-05 21:16:581101 plugins_changed = true;
[email protected]098fa7a2013-03-08 22:11:171102 for (extensions::PluginInfo::PluginVector::const_iterator plugin =
1103 plugins->begin();
1104 plugin != plugins->end(); ++plugin) {
1105 PluginService::GetInstance()->RefreshPlugins();
1106 PluginService::GetInstance()->AddExtraPluginPath(plugin->path);
1107 ChromePluginServiceFilter* filter =
1108 ChromePluginServiceFilter::GetInstance();
1109 if (plugin->is_public) {
1110 filter->RestrictPluginToProfileAndOrigin(
1111 plugin->path, profile_, GURL());
1112 } else {
1113 filter->RestrictPluginToProfileAndOrigin(
1114 plugin->path, profile_, extension->url());
1115 }
[email protected]2de307592011-04-05 21:16:581116 }
1117 }
[email protected]84396dbc2011-04-14 06:33:421118
1119 bool nacl_modules_changed = false;
1120 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1121 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001122 RegisterNaClModule(module.url, module.mime_type);
[email protected]84396dbc2011-04-14 06:33:421123 nacl_modules_changed = true;
1124 }
1125
[email protected]ed0ba002011-05-26 16:55:131126 if (nacl_modules_changed)
1127 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421128
1129 if (plugins_changed || nacl_modules_changed)
[email protected]45a22e62011-10-12 09:48:021130 PluginService::GetInstance()->PurgePluginListCache(profile_, false);
[email protected]ebd71962012-12-20 02:56:551131#endif // defined(ENABLE_PLUGINS)
[email protected]62d30f42009-10-01 22:36:061132}
1133
[email protected]a9f39a312010-12-23 22:14:271134void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591135 const Extension* extension,
1136 extension_misc::UnloadedExtensionReason reason) {
[email protected]a9f39a312010-12-23 22:14:271137 UnloadedExtensionInfo details(extension, reason);
[email protected]ad50def52011-10-19 23:17:071138 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271139 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531140 content::Source<Profile>(profile_),
1141 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061142
[email protected]903d70a2012-12-04 13:00:171143#if defined(ENABLE_THEMES)
1144 // If the current theme is being unloaded, tell ThemeService to revert back
1145 // to the default theme.
1146 if (reason != extension_misc::UNLOAD_REASON_UPDATE && extension->is_theme()) {
1147 ThemeService* theme_service = ThemeServiceFactory::GetForProfile(profile_);
1148 if (extension->id() == theme_service->GetThemeID())
1149 theme_service->UseDefaultTheme();
1150 }
1151#endif
1152
[email protected]f3b1a082011-11-18 00:34:301153 for (content::RenderProcessHost::iterator i(
1154 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:561155 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:301156 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081157 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301158 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:081159 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171160 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561161 }
1162
[email protected]31d8f5f22012-04-02 15:22:081163 system_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]c8d407e2011-04-28 21:27:171164 profile_->GetExtensionSpecialStoragePolicy()->
1165 RevokeRightsForExtension(extension);
1166
[email protected]b777b332011-04-16 04:01:081167#if defined(OS_CHROMEOS)
[email protected]ac66e452013-01-11 09:21:231168 // Revoke external file access for the extension from its file system context.
1169 // It is safe to access the extension's storage partition at this point. The
1170 // storage partition may get destroyed only after the extension gets unloaded.
1171 GURL site = extensions::ExtensionSystem::Get(profile_)->extension_service()->
1172 GetSiteForExtensionId(extension->id());
[email protected]10eb28162012-09-18 03:04:091173 fileapi::FileSystemContext* filesystem_context =
[email protected]ac66e452013-01-11 09:21:231174 BrowserContext::GetStoragePartitionForSite(profile_, site)->
[email protected]10eb28162012-09-18 03:04:091175 GetFileSystemContext();
1176 if (filesystem_context && filesystem_context->external_provider()) {
1177 filesystem_context->external_provider()->
[email protected]c8d407e2011-04-28 21:27:171178 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061179 }
[email protected]c8d407e2011-04-28 21:27:171180#endif
1181
1182 UpdateActiveExtensionsInCrashReporter();
[email protected]2de307592011-04-05 21:16:581183
[email protected]ebd71962012-12-20 02:56:551184#if defined(ENABLE_PLUGINS)
[email protected]2de307592011-04-05 21:16:581185 bool plugins_changed = false;
[email protected]098fa7a2013-03-08 22:11:171186 if (extensions::PluginInfo::HasPlugins(extension)) {
1187 const extensions::PluginInfo::PluginVector* plugins =
1188 extensions::PluginInfo::GetPlugins(extension);
[email protected]2de307592011-04-05 21:16:581189 plugins_changed = true;
[email protected]098fa7a2013-03-08 22:11:171190 for (extensions::PluginInfo::PluginVector::const_iterator plugin =
1191 plugins->begin();
1192 plugin != plugins->end(); ++plugin) {
1193 PluginService::GetInstance()->ForcePluginShutdown(plugin->path);
1194 PluginService::GetInstance()->RefreshPlugins();
1195 PluginService::GetInstance()->RemoveExtraPluginPath(plugin->path);
1196 ChromePluginServiceFilter::GetInstance()->UnrestrictPlugin(plugin->path);
1197 }
[email protected]2de307592011-04-05 21:16:581198 }
[email protected]84396dbc2011-04-14 06:33:421199
1200 bool nacl_modules_changed = false;
1201 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1202 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001203 UnregisterNaClModule(module.url);
[email protected]84396dbc2011-04-14 06:33:421204 nacl_modules_changed = true;
1205 }
1206
[email protected]ed0ba002011-05-26 16:55:131207 if (nacl_modules_changed)
1208 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421209
1210 if (plugins_changed || nacl_modules_changed)
[email protected]45a22e62011-10-12 09:48:021211 PluginService::GetInstance()->PurgePluginListCache(profile_, false);
[email protected]ebd71962012-12-20 02:56:551212#endif // defined(ENABLE_PLUGINS)
[email protected]62d30f42009-10-01 22:36:061213}
1214
[email protected]0a071a32011-02-08 00:18:241215Profile* ExtensionService::profile() {
1216 return profile_;
1217}
1218
[email protected]45759612012-07-10 17:21:231219extensions::ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451220 return extension_prefs_;
1221}
1222
[email protected]0d9a2202011-11-09 13:48:411223extensions::SettingsFrontend* ExtensionService::settings_frontend() {
[email protected]cccdf0aa2011-11-11 03:43:381224 return settings_frontend_.get();
[email protected]b7f853e282011-08-10 09:24:201225}
1226
[email protected]e2d720aa2012-05-17 00:11:351227extensions::ContentSettingsStore* ExtensionService::GetContentSettingsStore() {
[email protected]b790b072011-05-20 09:46:441228 return extension_prefs()->content_settings_store();
1229}
1230
[email protected]25ae0152011-11-18 14:40:021231bool ExtensionService::is_ready() {
[email protected]4a10006a2013-05-17 23:18:351232 return ready_->is_signaled();
[email protected]25ae0152011-11-18 14:40:021233}
1234
[email protected]7f8f24f2012-11-15 19:40:141235base::SequencedTaskRunner* ExtensionService::GetFileTaskRunner() {
1236 if (file_task_runner_)
1237 return file_task_runner_;
1238
1239 // We should be able to interrupt any part of extension install process during
1240 // shutdown. SKIP_ON_SHUTDOWN ensures that not started extension install tasks
1241 // will be ignored/deleted while we will block on started tasks.
1242 std::string token("ext_install-");
1243 token.append(profile_->GetPath().AsUTF8Unsafe());
1244 file_task_runner_ = BrowserThread::GetBlockingPool()->
1245 GetSequencedTaskRunnerWithShutdownBehavior(
1246 BrowserThread::GetBlockingPool()->GetNamedSequenceToken(token),
1247 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
1248 return file_task_runner_;
1249}
1250
[email protected]42a08162012-03-16 18:09:111251extensions::ExtensionUpdater* ExtensionService::updater() {
[email protected]2859946f2011-04-04 18:18:061252 return updater_.get();
1253}
1254
[email protected]4ee07c62012-08-21 12:40:421255void ExtensionService::CheckManagementPolicy() {
[email protected]aa96d3a2010-08-21 08:45:251256 std::vector<std::string> to_be_removed;
[email protected]695b5712012-12-06 23:55:281257
[email protected]aa96d3a2010-08-21 08:45:251258 // Loop through extensions list, unload installed extensions.
[email protected]84df8332011-12-06 18:22:461259 for (ExtensionSet::const_iterator iter = extensions_.begin();
[email protected]aa96d3a2010-08-21 08:45:251260 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021261 const Extension* extension = (*iter);
[email protected]4ee07c62012-08-21 12:40:421262 if (!system_->management_policy()->UserMayLoad(extension, NULL))
[email protected]aa96d3a2010-08-21 08:45:251263 to_be_removed.push_back(extension->id());
1264 }
1265
1266 // UnloadExtension will change the extensions_ list. So, we should
1267 // call it outside the iterator loop.
[email protected]31bb5ee62012-09-12 22:58:401268 for (size_t i = 0; i < to_be_removed.size(); ++i)
[email protected]814a7bf0f2011-08-13 05:30:591269 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]aa96d3a2010-08-21 08:45:251270}
1271
[email protected]31206602011-04-13 23:07:321272void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351273 if (updater()) {
[email protected]94fde232012-04-27 10:22:301274 if (AreAllExternalProvidersReady()) {
1275 updater()->CheckSoon();
1276 } else {
1277 // Sync can start updating before all the external providers are ready
1278 // during startup. Start the update as soon as those providers are ready,
1279 // but not before.
1280 update_once_all_providers_are_ready_ = true;
1281 }
[email protected]c3cfb012011-04-06 22:07:351282 } else {
[email protected]31206602011-04-13 23:07:321283 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351284 }
1285}
1286
[email protected]3f98fd92012-11-17 15:54:291287syncer::SyncMergeResult ExtensionService::MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:321288 syncer::ModelType type,
[email protected]65f173552012-06-28 22:43:581289 const syncer::SyncDataList& initial_sync_data,
1290 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
1291 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) {
[email protected]5db9ada2012-04-11 13:48:201292 CHECK(sync_processor.get());
[email protected]5d36e052012-04-20 20:12:281293 CHECK(sync_error_factory.get());
[email protected]3bdba0d2011-08-23 07:17:301294
1295 switch (type) {
[email protected]a4a147652012-07-03 23:41:321296 case syncer::EXTENSIONS:
[email protected]5db9ada2012-04-11 13:48:201297 extension_sync_bundle_.SetupSync(sync_processor.release(),
[email protected]5d36e052012-04-20 20:12:281298 sync_error_factory.release(),
[email protected]5db9ada2012-04-11 13:48:201299 initial_sync_data);
[email protected]3bdba0d2011-08-23 07:17:301300 break;
1301
[email protected]a4a147652012-07-03 23:41:321302 case syncer::APPS:
[email protected]5d36e052012-04-20 20:12:281303 app_sync_bundle_.SetupSync(sync_processor.release(),
1304 sync_error_factory.release(),
1305 initial_sync_data);
[email protected]3bdba0d2011-08-23 07:17:301306 break;
1307
1308 default:
1309 LOG(FATAL) << "Got " << type << " ModelType";
1310 }
[email protected]3bdba0d2011-08-23 07:17:301311
[email protected]b1417ab2011-12-15 17:09:111312 // Process local extensions.
1313 // TODO(yoz): Determine whether pending extensions should be considered too.
1314 // See crbug.com/104399.
[email protected]65f173552012-06-28 22:43:581315 syncer::SyncDataList sync_data_list = GetAllSyncData(type);
1316 syncer::SyncChangeList sync_change_list;
1317 for (syncer::SyncDataList::const_iterator i = sync_data_list.begin();
[email protected]3bdba0d2011-08-23 07:17:301318 i != sync_data_list.end();
1319 ++i) {
[email protected]5db9ada2012-04-11 13:48:201320 switch (type) {
[email protected]a4a147652012-07-03 23:41:321321 case syncer::EXTENSIONS:
[email protected]5db9ada2012-04-11 13:48:201322 sync_change_list.push_back(
1323 extension_sync_bundle_.CreateSyncChange(*i));
1324 break;
[email protected]a4a147652012-07-03 23:41:321325 case syncer::APPS:
[email protected]5db9ada2012-04-11 13:48:201326 sync_change_list.push_back(app_sync_bundle_.CreateSyncChange(*i));
1327 break;
1328 default:
1329 LOG(FATAL) << "Got " << type << " ModelType";
[email protected]31bb5ee62012-09-12 22:58:401330 }
[email protected]3bdba0d2011-08-23 07:17:301331 }
[email protected]3bdba0d2011-08-23 07:17:301332
[email protected]5db9ada2012-04-11 13:48:201333
[email protected]a4a147652012-07-03 23:41:321334 if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201335 extension_sync_bundle_.ProcessSyncChangeList(sync_change_list);
[email protected]a4a147652012-07-03 23:41:321336 } else if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201337 app_sync_bundle_.ProcessSyncChangeList(sync_change_list);
1338 }
[email protected]0ffd9e52012-01-26 21:44:341339
[email protected]3f98fd92012-11-17 15:54:291340 return syncer::SyncMergeResult(type);
[email protected]3bdba0d2011-08-23 07:17:301341}
1342
[email protected]a4a147652012-07-03 23:41:321343void ExtensionService::StopSyncing(syncer::ModelType type) {
1344 if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201345 app_sync_bundle_.Reset();
[email protected]a4a147652012-07-03 23:41:321346 } else if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201347 extension_sync_bundle_.Reset();
1348 }
[email protected]3bdba0d2011-08-23 07:17:301349}
1350
[email protected]65f173552012-06-28 22:43:581351syncer::SyncDataList ExtensionService::GetAllSyncData(
[email protected]a4a147652012-07-03 23:41:321352 syncer::ModelType type) const {
[email protected]31bb5ee62012-09-12 22:58:401353 if (type == syncer::EXTENSIONS)
[email protected]5db9ada2012-04-11 13:48:201354 return extension_sync_bundle_.GetAllSyncData();
[email protected]31bb5ee62012-09-12 22:58:401355 if (type == syncer::APPS)
[email protected]5db9ada2012-04-11 13:48:201356 return app_sync_bundle_.GetAllSyncData();
[email protected]5db9ada2012-04-11 13:48:201357
1358 // We should only get sync data for extensions and apps.
1359 NOTREACHED();
1360
[email protected]65f173552012-06-28 22:43:581361 return syncer::SyncDataList();
[email protected]3bdba0d2011-08-23 07:17:301362}
1363
[email protected]65f173552012-06-28 22:43:581364syncer::SyncError ExtensionService::ProcessSyncChanges(
[email protected]3bdba0d2011-08-23 07:17:301365 const tracked_objects::Location& from_here,
[email protected]65f173552012-06-28 22:43:581366 const syncer::SyncChangeList& change_list) {
1367 for (syncer::SyncChangeList::const_iterator i = change_list.begin();
[email protected]3bdba0d2011-08-23 07:17:301368 i != change_list.end();
1369 ++i) {
[email protected]a4a147652012-07-03 23:41:321370 syncer::ModelType type = i->sync_data().GetDataType();
1371 if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201372 extension_sync_bundle_.ProcessSyncChange(
1373 extensions::ExtensionSyncData(*i));
[email protected]a4a147652012-07-03 23:41:321374 } else if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201375 app_sync_bundle_.ProcessSyncChange(extensions::AppSyncData(*i));
1376 }
[email protected]3bdba0d2011-08-23 07:17:301377 }
1378
[email protected]0ffd9e52012-01-26 21:44:341379 extension_prefs()->extension_sorting()->FixNTPOrdinalCollisions();
1380
[email protected]65f173552012-06-28 22:43:581381 return syncer::SyncError();
[email protected]b05fb9ff2011-04-23 00:07:561382}
1383
[email protected]5db9ada2012-04-11 13:48:201384extensions::ExtensionSyncData ExtensionService::GetExtensionSyncData(
1385 const Extension& extension) const {
1386 return extensions::ExtensionSyncData(extension,
1387 IsExtensionEnabled(extension.id()),
1388 IsIncognitoEnabled(extension.id()));
[email protected]b05fb9ff2011-04-23 00:07:561389}
1390
[email protected]5db9ada2012-04-11 13:48:201391extensions::AppSyncData ExtensionService::GetAppSyncData(
1392 const Extension& extension) const {
1393 return extensions::AppSyncData(
1394 extension,
1395 IsExtensionEnabled(extension.id()),
1396 IsIncognitoEnabled(extension.id()),
[email protected]5db9ada2012-04-11 13:48:201397 extension_prefs_->extension_sorting()->GetAppLaunchOrdinal(
1398 extension.id()),
1399 extension_prefs_->extension_sorting()->GetPageOrdinal(extension.id()));
1400}
[email protected]3bdba0d2011-08-23 07:17:301401
[email protected]5db9ada2012-04-11 13:48:201402std::vector<extensions::ExtensionSyncData>
1403 ExtensionService::GetExtensionSyncDataList() const {
1404 std::vector<extensions::ExtensionSyncData> extension_sync_list;
1405 extension_sync_bundle_.GetExtensionSyncDataListHelper(extensions_,
1406 &extension_sync_list);
1407 extension_sync_bundle_.GetExtensionSyncDataListHelper(disabled_extensions_,
1408 &extension_sync_list);
1409 extension_sync_bundle_.GetExtensionSyncDataListHelper(terminated_extensions_,
1410 &extension_sync_list);
1411
1412 std::vector<extensions::ExtensionSyncData> pending_extensions =
1413 extension_sync_bundle_.GetPendingData();
1414 extension_sync_list.insert(extension_sync_list.begin(),
1415 pending_extensions.begin(),
1416 pending_extensions.end());
[email protected]3bdba0d2011-08-23 07:17:301417
1418 return extension_sync_list;
[email protected]b05fb9ff2011-04-23 00:07:561419}
1420
[email protected]5db9ada2012-04-11 13:48:201421std::vector<extensions::AppSyncData> ExtensionService::GetAppSyncDataList()
1422 const {
1423 std::vector<extensions::AppSyncData> app_sync_list;
1424 app_sync_bundle_.GetAppSyncDataListHelper(extensions_, &app_sync_list);
1425 app_sync_bundle_.GetAppSyncDataListHelper(disabled_extensions_,
1426 &app_sync_list);
1427 app_sync_bundle_.GetAppSyncDataListHelper(terminated_extensions_,
1428 &app_sync_list);
1429
1430 std::vector<extensions::AppSyncData> pending_apps =
1431 app_sync_bundle_.GetPendingData();
1432 app_sync_list.insert(app_sync_list.begin(),
1433 pending_apps.begin(),
1434 pending_apps.end());
1435
1436 return app_sync_list;
1437}
1438
1439bool ExtensionService::ProcessExtensionSyncData(
1440 const extensions::ExtensionSyncData& extension_sync_data) {
1441 if (!ProcessExtensionSyncDataHelper(extension_sync_data,
[email protected]a4a147652012-07-03 23:41:321442 syncer::EXTENSIONS)) {
[email protected]5db9ada2012-04-11 13:48:201443 extension_sync_bundle_.AddPendingExtension(extension_sync_data.id(),
1444 extension_sync_data);
1445 CheckForUpdatesSoon();
1446 return false;
1447 }
1448
1449 return true;
1450}
1451
1452bool ExtensionService::ProcessAppSyncData(
1453 const extensions::AppSyncData& app_sync_data) {
1454 const std::string& id = app_sync_data.id();
[email protected]5db9ada2012-04-11 13:48:201455
1456 if (app_sync_data.app_launch_ordinal().IsValid() &&
1457 app_sync_data.page_ordinal().IsValid()) {
1458 extension_prefs_->extension_sorting()->SetAppLaunchOrdinal(
1459 id,
1460 app_sync_data.app_launch_ordinal());
1461 extension_prefs_->extension_sorting()->SetPageOrdinal(
1462 id,
1463 app_sync_data.page_ordinal());
1464 }
1465
[email protected]5db9ada2012-04-11 13:48:201466 if (!ProcessExtensionSyncDataHelper(app_sync_data.extension_sync_data(),
[email protected]a4a147652012-07-03 23:41:321467 syncer::APPS)) {
[email protected]5db9ada2012-04-11 13:48:201468 app_sync_bundle_.AddPendingApp(id, app_sync_data);
1469 CheckForUpdatesSoon();
1470 return false;
1471 }
1472
1473 return true;
1474}
1475
1476bool ExtensionService::IsCorrectSyncType(const Extension& extension,
[email protected]a4a147652012-07-03 23:41:321477 syncer::ModelType type) const {
1478 if (type == syncer::EXTENSIONS &&
[email protected]31bb5ee62012-09-12 22:58:401479 extension.GetSyncType() == Extension::SYNC_TYPE_EXTENSION) {
[email protected]5db9ada2012-04-11 13:48:201480 return true;
[email protected]31bb5ee62012-09-12 22:58:401481 }
[email protected]5db9ada2012-04-11 13:48:201482
[email protected]a4a147652012-07-03 23:41:321483 if (type == syncer::APPS &&
[email protected]31bb5ee62012-09-12 22:58:401484 extension.GetSyncType() == Extension::SYNC_TYPE_APP) {
[email protected]5db9ada2012-04-11 13:48:201485 return true;
[email protected]31bb5ee62012-09-12 22:58:401486 }
[email protected]5db9ada2012-04-11 13:48:201487
1488 return false;
1489}
1490
1491bool ExtensionService::ProcessExtensionSyncDataHelper(
1492 const extensions::ExtensionSyncData& extension_sync_data,
[email protected]a4a147652012-07-03 23:41:321493 syncer::ModelType type) {
[email protected]3bdba0d2011-08-23 07:17:301494 const std::string& id = extension_sync_data.id();
[email protected]96e989b2011-08-30 19:35:061495 const Extension* extension = GetInstalledExtension(id);
1496
1497 // TODO(bolms): we should really handle this better. The particularly bad
1498 // case is where an app becomes an extension or vice versa, and we end up with
1499 // a zombie extension that won't go away.
[email protected]5db9ada2012-04-11 13:48:201500 if (extension && !IsCorrectSyncType(*extension, type))
1501 return true;
[email protected]90310d92011-04-17 07:35:041502
1503 // Handle uninstalls first.
[email protected]3bdba0d2011-08-23 07:17:301504 if (extension_sync_data.uninstalled()) {
[email protected]90310d92011-04-17 07:35:041505 std::string error;
1506 if (!UninstallExtensionHelper(this, id)) {
1507 LOG(WARNING) << "Could not uninstall extension " << id
1508 << " for sync";
1509 }
[email protected]5db9ada2012-04-11 13:48:201510 return true;
[email protected]90310d92011-04-17 07:35:041511 }
1512
[email protected]caf2d442012-05-04 10:00:501513 // Extension from sync was uninstalled by the user as external extensions.
1514 // Honor user choice and skip installation/enabling.
1515 if (IsExternalExtensionUninstalled(id)) {
1516 LOG(WARNING) << "Extension with id " << id
1517 << " from sync was uninstalled as external extension";
1518 return true;
1519 }
1520
[email protected]06f92562011-04-29 19:27:311521 // Set user settings.
[email protected]7e9e2422012-12-13 19:54:191522 // If the extension has been disabled from sync, it may not have
1523 // been installed yet, so we don't know if the disable reason was a
[email protected]b914e2952013-04-26 07:10:031524 // permissions increase. That will be updated once CheckPermissionsIncrease
[email protected]7e9e2422012-12-13 19:54:191525 // is called for it.
[email protected]31bb5ee62012-09-12 22:58:401526 if (extension_sync_data.enabled())
[email protected]06f92562011-04-29 19:27:311527 EnableExtension(id);
[email protected]31bb5ee62012-09-12 22:58:401528 else
[email protected]7e9e2422012-12-13 19:54:191529 DisableExtension(id, Extension::DISABLE_UNKNOWN_FROM_SYNC);
[email protected]81b14cc2011-04-29 00:39:371530
[email protected]406b5a92011-11-08 11:58:261531 // We need to cache some version information here because setting the
1532 // incognito flag invalidates the |extension| pointer (it reloads the
1533 // extension).
[email protected]9060d8b02012-01-13 02:14:301534 bool extension_installed = (extension != NULL);
[email protected]406b5a92011-11-08 11:58:261535 int result = extension ?
1536 extension->version()->CompareTo(extension_sync_data.version()) : 0;
1537 SetIsIncognitoEnabled(id, extension_sync_data.incognito_enabled());
1538 extension = NULL; // No longer safe to use.
1539
1540 if (extension_installed) {
[email protected]06f92562011-04-29 19:27:311541 // If the extension is already installed, check if it's outdated.
[email protected]90310d92011-04-17 07:35:041542 if (result < 0) {
1543 // Extension is outdated.
[email protected]5db9ada2012-04-11 13:48:201544 return false;
[email protected]b020dc12011-12-06 04:35:421545 }
[email protected]06f92562011-04-29 19:27:311546 } else {
[email protected]06f92562011-04-29 19:27:311547 // TODO(akalin): Replace silent update with a list of enabled
1548 // permissions.
[email protected]6cc7dbae2011-04-29 21:18:331549 const bool kInstallSilently = true;
[email protected]5db9ada2012-04-11 13:48:201550
[email protected]a4a147652012-07-03 23:41:321551 CHECK(type == syncer::EXTENSIONS || type == syncer::APPS);
[email protected]5db9ada2012-04-11 13:48:201552 ExtensionFilter filter =
[email protected]a4a147652012-07-03 23:41:321553 (type == syncer::APPS) ? IsSyncableApp : IsSyncableExtension;
[email protected]5db9ada2012-04-11 13:48:201554
[email protected]06f92562011-04-29 19:27:311555 if (!pending_extension_manager()->AddFromSync(
1556 id,
[email protected]3bdba0d2011-08-23 07:17:301557 extension_sync_data.update_url(),
[email protected]5db9ada2012-04-11 13:48:201558 filter,
[email protected]6cc7dbae2011-04-29 21:18:331559 kInstallSilently)) {
[email protected]06f92562011-04-29 19:27:311560 LOG(WARNING) << "Could not add pending extension for " << id;
[email protected]3bdba0d2011-08-23 07:17:301561 // This means that the extension is already pending installation, with a
1562 // non-INTERNAL location. Add to pending_sync_data, even though it will
1563 // never be removed (we'll never install a syncable version of the
1564 // extension), so that GetAllSyncData() continues to send it.
[email protected]06f92562011-04-29 19:27:311565 }
[email protected]3bdba0d2011-08-23 07:17:301566 // Track pending extensions so that we can return them in GetAllSyncData().
[email protected]5db9ada2012-04-11 13:48:201567 return false;
[email protected]90310d92011-04-17 07:35:041568 }
[email protected]5db9ada2012-04-11 13:48:201569
1570 return true;
[email protected]90310d92011-04-17 07:35:041571}
1572
[email protected]c3cfb012011-04-06 22:07:351573bool ExtensionService::IsIncognitoEnabled(
1574 const std::string& extension_id) const {
[email protected]cdc7b1f42012-12-07 19:39:481575 const Extension* extension = GetInstalledExtension(extension_id);
1576 if (extension && !extension->can_be_incognito_enabled())
1577 return false;
[email protected]c3cfb012011-04-06 22:07:351578 // If this is an existing component extension we always allow it to
1579 // work in incognito mode.
[email protected]1d5e58b2013-01-31 08:41:401580 if (extension && extension->location() == Manifest::COMPONENT)
[email protected]cb0ce1e022010-03-10 19:54:411581 return true;
1582
1583 // Check the prefs.
[email protected]c3cfb012011-04-06 22:07:351584 return extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]db7331a2010-02-25 22:10:501585}
[email protected]55a35692010-02-11 23:25:211586
[email protected]c3cfb012011-04-06 22:07:351587void ExtensionService::SetIsIncognitoEnabled(
1588 const std::string& extension_id, bool enabled) {
[email protected]81b14cc2011-04-29 00:39:371589 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]cdc7b1f42012-12-07 19:39:481590 if (extension && !extension->can_be_incognito_enabled())
1591 return;
[email protected]1d5e58b2013-01-31 08:41:401592 if (extension && extension->location() == Manifest::COMPONENT) {
[email protected]b873cd92012-02-09 21:51:481593 // This shouldn't be called for component extensions unless they are
1594 // syncable.
1595 DCHECK(extension->IsSyncable());
1596
1597 // If we are here, make sure the we aren't trying to change the value.
1598 DCHECK_EQ(enabled, IsIncognitoEnabled(extension_id));
1599
[email protected]c3cfb012011-04-06 22:07:351600 return;
1601 }
1602
[email protected]8c6c8c22011-03-09 12:52:341603 // Broadcast unloaded and loaded events to update browser state. Only bother
1604 // if the value changed and the extension is actually enabled, since there is
1605 // no UI otherwise.
[email protected]c3cfb012011-04-06 22:07:351606 bool old_enabled = extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]8c6c8c22011-03-09 12:52:341607 if (enabled == old_enabled)
1608 return;
1609
[email protected]c3cfb012011-04-06 22:07:351610 extension_prefs_->SetIsIncognitoEnabled(extension_id, enabled);
[email protected]81b14cc2011-04-29 00:39:371611
[email protected]ffbf3f12011-12-09 22:10:331612 bool extension_is_enabled = extensions_.Contains(extension_id);
[email protected]bd9892eb2011-11-11 19:41:341613
1614 // When we reload the extension the ID may be invalidated if we've passed it
1615 // by const ref everywhere. Make a copy to be safe.
1616 std::string id = extension_id;
[email protected]406b5a92011-11-08 11:58:261617 if (extension_is_enabled)
[email protected]ffbf3f12011-12-09 22:10:331618 ReloadExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301619
[email protected]406b5a92011-11-08 11:58:261620 // Reloading the extension invalidates the |extension| pointer.
[email protected]bd9892eb2011-11-11 19:41:341621 extension = GetInstalledExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301622 if (extension)
1623 SyncExtensionChangeIfNeeded(*extension);
[email protected]55a35692010-02-11 23:25:211624}
1625
[email protected]dc9a74f72012-08-17 18:07:211626bool ExtensionService::CanCrossIncognito(const Extension* extension) const {
[email protected]2a8f24e2010-11-03 21:37:051627 // We allow the extension to see events and data from another profile iff it
1628 // uses "spanning" behavior and it has incognito access. "split" mode
1629 // extensions only see events for a matching profile.
[email protected]0c41298f2011-09-13 21:17:221630 CHECK(extension);
[email protected]c3cfb012011-04-06 22:07:351631 return IsIncognitoEnabled(extension->id()) &&
[email protected]06492ed2013-03-24 22:13:141632 !extensions::IncognitoInfo::IsSplitMode(extension);
[email protected]2a8f24e2010-11-03 21:37:051633}
1634
[email protected]5ef7b002011-05-12 23:16:201635bool ExtensionService::CanLoadInIncognito(const Extension* extension) const {
1636 if (extension->is_hosted_app())
1637 return true;
1638 // Packaged apps and regular extensions need to be enabled specifically for
1639 // incognito (and split mode should be set).
[email protected]06492ed2013-03-24 22:13:141640 return extensions::IncognitoInfo::IsSplitMode(extension) &&
[email protected]5ef7b002011-05-12 23:16:201641 IsIncognitoEnabled(extension->id());
1642}
1643
[email protected]168389f2011-12-20 17:12:481644void ExtensionService::OnExtensionMoved(
1645 const std::string& moved_extension_id,
1646 const std::string& predecessor_extension_id,
1647 const std::string& successor_extension_id) {
[email protected]3569b502012-01-12 20:08:231648 extension_prefs_->extension_sorting()->OnExtensionMoved(
1649 moved_extension_id,
1650 predecessor_extension_id,
1651 successor_extension_id);
[email protected]168389f2011-12-20 17:12:481652
1653 const Extension* extension = GetInstalledExtension(moved_extension_id);
1654 if (extension)
1655 SyncExtensionChangeIfNeeded(*extension);
1656}
1657
[email protected]dc9a74f72012-08-17 18:07:211658bool ExtensionService::AllowFileAccess(const Extension* extension) const {
[email protected]05c82182010-06-24 17:49:081659 return (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]334e04a2010-06-24 23:34:441660 switches::kDisableExtensionsFileAccessCheck) ||
[email protected]05c82182010-06-24 17:49:081661 extension_prefs_->AllowFileAccess(extension->id()));
1662}
1663
[email protected]eaa7dd182010-12-14 11:09:001664void ExtensionService::SetAllowFileAccess(const Extension* extension,
[email protected]3aff9ad2011-04-01 20:26:481665 bool allow) {
1666 // Reload to update browser state. Only bother if the value changed and the
1667 // extension is actually enabled, since there is no UI otherwise.
1668 bool old_allow = AllowFileAccess(extension);
1669 if (allow == old_allow)
1670 return;
1671
[email protected]05c82182010-06-24 17:49:081672 extension_prefs_->SetAllowFileAccess(extension->id(), allow);
[email protected]3aff9ad2011-04-01 20:26:481673
[email protected]84df8332011-12-06 18:22:461674 bool extension_is_enabled = extensions_.Contains(extension->id());
[email protected]3aff9ad2011-04-01 20:26:481675 if (extension_is_enabled)
1676 ReloadExtension(extension->id());
[email protected]05c82182010-06-24 17:49:081677}
1678
[email protected]8e4560b62011-01-14 10:09:141679// Some extensions will autoupdate themselves externally from Chrome. These
1680// are typically part of some larger client application package. To support
1681// these, the extension will register its location in the the preferences file
1682// (and also, on Windows, in the registry) and this code will periodically
1683// check that location for a .crx file, which it will then install locally if
1684// a new version is available.
1685// Errors are reported through ExtensionErrorReporter. Succcess is not
1686// reported.
[email protected]eaa7dd182010-12-14 11:09:001687void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121688 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141689
1690 // Note that this installation is intentionally silent (since it didn't
1691 // go through the front-end). Extensions that are registered in this
1692 // way are effectively considered 'pre-bundled', and so implicitly
1693 // trusted. In general, if something has HKLM or filesystem access,
1694 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121695
[email protected]8e4560b62011-01-14 10:09:141696 // Ask each external extension provider to give us a call back for each
1697 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
[email protected]5df038b2012-07-16 19:03:271698 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141699 for (i = external_extension_providers_.begin();
1700 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:271701 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:141702 provider->VisitRegisteredExtension();
1703 }
1704
[email protected]50067e52011-10-20 23:17:071705 // Do any required work that we would have done after completion of all
1706 // providers.
1707 if (external_extension_providers_.empty()) {
1708 OnAllExternalProvidersReady();
1709 }
[email protected]9f1087e2009-06-15 17:29:321710}
1711
[email protected]50067e52011-10-20 23:17:071712void ExtensionService::OnExternalProviderReady(
[email protected]5df038b2012-07-16 19:03:271713 const extensions::ExternalProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121714 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071715 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121716
1717 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141718 // if all of them are finished. So we check them first.
[email protected]94fde232012-04-27 10:22:301719 if (AreAllExternalProvidersReady())
1720 OnAllExternalProvidersReady();
1721}
1722
1723bool ExtensionService::AreAllExternalProvidersReady() const {
[email protected]5df038b2012-07-16 19:03:271724 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141725 for (i = external_extension_providers_.begin();
1726 i != external_extension_providers_.end(); ++i) {
[email protected]94fde232012-04-27 10:22:301727 if (!i->get()->IsReady())
1728 return false;
[email protected]8e4560b62011-01-14 10:09:141729 }
[email protected]94fde232012-04-27 10:22:301730 return true;
[email protected]50067e52011-10-20 23:17:071731}
1732
1733void ExtensionService::OnAllExternalProvidersReady() {
1734 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]94fde232012-04-27 10:22:301735 base::TimeDelta elapsed = base::Time::Now() - profile_->GetStartTime();
1736 UMA_HISTOGRAM_TIMES("Extension.ExternalProvidersReadyAfter", elapsed);
[email protected]50067e52011-10-20 23:17:071737
1738 // Install any pending extensions.
[email protected]94fde232012-04-27 10:22:301739 if (update_once_all_providers_are_ready_ && updater()) {
1740 update_once_all_providers_are_ready_ = false;
[email protected]42774e42012-11-14 21:01:061741 updater()->CheckNow(extensions::ExtensionUpdater::CheckParams());
[email protected]8e4560b62011-01-14 10:09:141742 }
1743
1744 // Uninstall all the unclaimed extensions.
[email protected]45759612012-07-10 17:21:231745 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]8e4560b62011-01-14 10:09:141746 extension_prefs_->GetInstalledExtensionsInfo());
1747 for (size_t i = 0; i < extensions_info->size(); ++i) {
1748 ExtensionInfo* info = extensions_info->at(i).get();
[email protected]1d5e58b2013-01-31 08:41:401749 if (Manifest::IsExternalLocation(info->extension_location))
[email protected]8e4560b62011-01-14 10:09:141750 CheckExternalUninstall(info->extension_id);
1751 }
[email protected]e5af875f2011-10-10 21:09:141752 IdentifyAlertableExtensions();
[email protected]e5af875f2011-10-10 21:09:141753}
1754
1755void ExtensionService::IdentifyAlertableExtensions() {
1756 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1757
[email protected]07c9f2f42012-02-29 18:45:221758 // Build up the lists of extensions that require acknowledgment. If this is
1759 // the first time, grandfather extensions that would have caused
1760 // notification.
[email protected]89226982012-07-16 20:09:181761 extension_error_ui_.reset(ExtensionErrorUI::Create(this));
[email protected]e5af875f2011-10-10 21:09:141762
[email protected]a9aa5932012-01-25 08:27:401763 bool did_show_alert = false;
[email protected]89226982012-07-16 20:09:181764 if (PopulateExtensionErrorUI(extension_error_ui_.get())) {
[email protected]460c6712013-04-24 07:20:011765 if (!is_first_run_) {
[email protected]a9aa5932012-01-25 08:27:401766 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]89226982012-07-16 20:09:181767 did_show_alert = extension_error_ui_->ShowErrorInBubbleView();
[email protected]e5af875f2011-10-10 21:09:141768 } else {
1769 // First run. Just acknowledge all the extensions, silently, by
1770 // shortcutting the display of the UI and going straight to the
1771 // callback for pressing the Accept button.
[email protected]a9aa5932012-01-25 08:27:401772 HandleExtensionAlertAccept();
[email protected]e5af875f2011-10-10 21:09:141773 }
1774 }
[email protected]a9aa5932012-01-25 08:27:401775
[email protected]612a1cb12012-10-17 13:18:031776 UpdateExternalExtensionAlert();
1777
[email protected]a9aa5932012-01-25 08:27:401778 if (!did_show_alert)
[email protected]89226982012-07-16 20:09:181779 extension_error_ui_.reset();
[email protected]e5af875f2011-10-10 21:09:141780}
1781
[email protected]89226982012-07-16 20:09:181782bool ExtensionService::PopulateExtensionErrorUI(
1783 ExtensionErrorUI* extension_error_ui) {
[email protected]07c9f2f42012-02-29 18:45:221784 bool needs_alert = false;
[email protected]695b5712012-12-06 23:55:281785
1786 // Extensions that are blacklisted.
1787 for (ExtensionSet::const_iterator it = blacklisted_extensions_.begin();
1788 it != blacklisted_extensions_.end(); ++it) {
1789 std::string id = (*it)->id();
1790 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(id)) {
1791 extension_error_ui->AddBlacklistedExtension(id);
1792 needs_alert = true;
1793 }
1794 }
1795
[email protected]07c9f2f42012-02-29 18:45:221796 for (ExtensionSet::const_iterator iter = extensions_.begin();
1797 iter != extensions_.end(); ++iter) {
1798 const Extension* e = *iter;
[email protected]695b5712012-12-06 23:55:281799
1800 // Extensions disabled by policy. Note: this no longer includes blacklisted
1801 // extensions, though we still show the same UI.
[email protected]fdd679b2012-11-15 20:49:391802 if (!system_->management_policy()->UserMayLoad(e, NULL)) {
[email protected]07c9f2f42012-02-29 18:45:221803 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(e->id())) {
[email protected]89226982012-07-16 20:09:181804 extension_error_ui->AddBlacklistedExtension(e->id());
[email protected]07c9f2f42012-02-29 18:45:221805 needs_alert = true;
1806 }
1807 }
[email protected]07c9f2f42012-02-29 18:45:221808 }
[email protected]695b5712012-12-06 23:55:281809
[email protected]07c9f2f42012-02-29 18:45:221810 return needs_alert;
1811}
1812
[email protected]a9aa5932012-01-25 08:27:401813void ExtensionService::HandleExtensionAlertClosed() {
[email protected]612a1cb12012-10-17 13:18:031814 const ExtensionIdSet* extension_ids =
1815 extension_error_ui_->get_blacklisted_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141816 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1817 iter != extension_ids->end(); ++iter) {
1818 extension_prefs_->AcknowledgeBlacklistedExtension(*iter);
1819 }
[email protected]879a6092013-03-06 04:12:501820 extension_error_ui_.reset();
1821}
1822
1823void ExtensionService::HandleExtensionAlertAccept() {
[email protected]73a85e012013-04-04 10:45:301824 extension_error_ui_->Close();
[email protected]e5af875f2011-10-10 21:09:141825}
1826
[email protected]47fc70c2011-12-06 07:29:511827void ExtensionService::AcknowledgeExternalExtension(const std::string& id) {
1828 extension_prefs_->AcknowledgeExternalExtension(id);
[email protected]612a1cb12012-10-17 13:18:031829 UpdateExternalExtensionAlert();
[email protected]47fc70c2011-12-06 07:29:511830}
1831
[email protected]d96eb512012-11-01 23:44:081832bool ExtensionService::IsUnacknowledgedExternalExtension(
1833 const Extension* extension) {
[email protected]b82d18da2012-11-09 00:11:321834 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
1835 return false;
1836
[email protected]1d5e58b2013-01-31 08:41:401837 return (Manifest::IsExternalLocation(extension->location()) &&
[email protected]d96eb512012-11-01 23:44:081838 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id()) &&
1839 !(extension_prefs_->GetDisableReasons(extension->id()) &
1840 Extension::DISABLE_SIDELOAD_WIPEOUT));
1841}
1842
[email protected]89226982012-07-16 20:09:181843void ExtensionService::HandleExtensionAlertDetails() {
1844 extension_error_ui_->ShowExtensions();
[email protected]73a85e012013-04-04 10:45:301845 // ShowExtensions may cause the error UI to close synchronously, e.g. if it
1846 // causes a navigation.
1847 if (extension_error_ui_)
1848 extension_error_ui_->Close();
[email protected]6c751e72010-11-23 10:11:101849}
1850
[email protected]612a1cb12012-10-17 13:18:031851void ExtensionService::UpdateExternalExtensionAlert() {
[email protected]41070e8d2012-10-24 01:34:361852 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
[email protected]612a1cb12012-10-17 13:18:031853 return;
1854
1855 const Extension* extension = NULL;
1856 for (ExtensionSet::const_iterator iter = disabled_extensions_.begin();
1857 iter != disabled_extensions_.end(); ++iter) {
1858 const Extension* e = *iter;
[email protected]d96eb512012-11-01 23:44:081859 if (IsUnacknowledgedExternalExtension(e)) {
1860 extension = e;
1861 break;
[email protected]612a1cb12012-10-17 13:18:031862 }
1863 }
1864
1865 if (extension) {
[email protected]d96eb512012-11-01 23:44:081866 if (!extensions::HasExternalInstallError(this)) {
1867 if (extension_prefs_->IncrementAcknowledgePromptCount(extension->id()) >
[email protected]612a1cb12012-10-17 13:18:031868 kMaxExtensionAcknowledgePromptCount) {
[email protected]d96eb512012-11-01 23:44:081869 // Stop prompting for this extension, and check if there's another
1870 // one that needs prompting.
[email protected]612a1cb12012-10-17 13:18:031871 extension_prefs_->AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:081872 UpdateExternalExtensionAlert();
1873 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
1874 EXTERNAL_EXTENSION_IGNORED,
1875 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
1876 return;
[email protected]612a1cb12012-10-17 13:18:031877 }
[email protected]b3aa7182013-04-25 04:45:231878 if (is_first_run_)
1879 extension_prefs_->SetExternalInstallFirstRun(extension->id());
1880 // first_run is true if the extension was installed during a first run
1881 // (even if it's post-first run now).
1882 bool first_run = extension_prefs_->IsExternalInstallFirstRun(
1883 extension->id());
1884 extensions::AddExternalInstallError(this, extension, first_run);
[email protected]612a1cb12012-10-17 13:18:031885 }
1886 } else {
1887 extensions::RemoveExternalInstallError(this);
1888 }
[email protected]612a1cb12012-10-17 13:18:031889}
1890
[email protected]a9f39a312010-12-23 22:14:271891void ExtensionService::UnloadExtension(
1892 const std::string& extension_id,
[email protected]814a7bf0f2011-08-13 05:30:591893 extension_misc::UnloadedExtensionReason reason) {
[email protected]27e469a2010-01-11 20:35:091894 // Make sure the extension gets deleted after we return from this function.
[email protected]695b5712012-12-06 23:55:281895 int include_mask = INCLUDE_EVERYTHING & ~INCLUDE_TERMINATED;
[email protected]9adb9692010-10-29 23:14:021896 scoped_refptr<const Extension> extension(
[email protected]695b5712012-12-06 23:55:281897 GetExtensionById(extension_id, include_mask));
[email protected]631cf822009-05-15 07:01:251898
[email protected]fa0c96732010-11-17 00:14:231899 // This method can be called via PostTask, so the extension may have been
1900 // unloaded by the time this runs.
[email protected]dd163fb02011-05-04 22:22:171901 if (!extension) {
1902 // In case the extension may have crashed/uninstalled. Allow the profile to
1903 // clean up its RequestContexts.
[email protected]31d8f5f22012-04-02 15:22:081904 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231905 return;
[email protected]dd163fb02011-05-04 22:22:171906 }
[email protected]0c6da502009-08-14 22:32:391907
[email protected]20c8b942013-05-26 02:03:401908 // If uninstalling let RuntimeEventRouter know.
1909 if (reason == extension_misc::UNLOAD_REASON_UNINSTALL)
1910 extensions::RuntimeEventRouter::OnExtensionUninstalled(
1911 profile_, extension_id);
1912
[email protected]1eb175082010-02-10 09:26:161913 // Keep information about the extension so that we can reload it later
1914 // even if it's not permanently installed.
1915 unloaded_extension_paths_[extension->id()] = extension->path();
1916
[email protected]f17dbd42010-08-16 23:21:101917 // Clean up if the extension is meant to be enabled after a reload.
[email protected]b914e2952013-04-26 07:10:031918 reloading_extensions_.erase(extension->id());
[email protected]f17dbd42010-08-16 23:21:101919
[email protected]d7e9a862010-11-03 21:57:491920 // Clean up runtime data.
1921 extension_runtime_data_.erase(extension_id);
1922
[email protected]060d4972012-07-19 17:22:391923 if (disabled_extensions_.Contains(extension->id())) {
[email protected]a9f39a312010-12-23 22:14:271924 UnloadedExtensionInfo details(extension, reason);
1925 details.already_disabled = true;
[email protected]84df8332011-12-06 18:22:461926 disabled_extensions_.Remove(extension->id());
[email protected]ad50def52011-10-19 23:17:071927 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271928 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531929 content::Source<Profile>(profile_),
1930 content::Details<UnloadedExtensionInfo>(&details));
[email protected]dd163fb02011-05-04 22:22:171931 // Make sure the profile cleans up its RequestContexts when an already
1932 // disabled extension is unloaded (since they are also tracking the disabled
1933 // extensions).
[email protected]31d8f5f22012-04-02 15:22:081934 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]0c6da502009-08-14 22:32:391935 return;
1936 }
1937
[email protected]060d4972012-07-19 17:22:391938 // Remove the extension from our list.
[email protected]84df8332011-12-06 18:22:461939 extensions_.Remove(extension->id());
[email protected]631cf822009-05-15 07:01:251940
[email protected]a9f39a312010-12-23 22:14:271941 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]631cf822009-05-15 07:01:251942}
1943
[email protected]eaa7dd182010-12-14 11:09:001944void ExtensionService::UnloadAllExtensions() {
[email protected]31bb5ee62012-09-12 22:58:401945 profile_->GetExtensionSpecialStoragePolicy()->RevokeRightsForAllExtensions();
[email protected]c8d407e2011-04-28 21:27:171946
[email protected]84df8332011-12-06 18:22:461947 extensions_.Clear();
1948 disabled_extensions_.Clear();
1949 terminated_extensions_.Clear();
[email protected]d7e9a862010-11-03 21:57:491950 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:321951
[email protected]c6e4a3412009-06-24 15:45:291952 // TODO(erikkay) should there be a notification for this? We can't use
1953 // EXTENSION_UNLOADED since that implies that the extension has been disabled
1954 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:321955}
1956
[email protected]eaa7dd182010-12-14 11:09:001957void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:321958 UnloadAllExtensions();
[email protected]eac88332012-12-26 17:57:451959 component_loader_->LoadAll();
[email protected]d8c8f25f2011-11-02 18:18:011960 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]4a10006a2013-05-17 23:18:351961 // Don't call SetReadyAndNotifyListeners() since tests call this multiple
1962 // times.
[email protected]9f1087e2009-06-15 17:29:321963}
1964
[email protected]eaa7dd182010-12-14 11:09:001965void ExtensionService::GarbageCollectExtensions() {
[email protected]2f8757c32012-06-19 19:17:471966 if (extension_prefs_->pref_service()->ReadOnly())
1967 return;
1968
[email protected]9bd9a6862012-11-29 09:24:221969 if (pending_extension_manager()->HasPendingExtensions()) {
1970 // Don't garbage collect while there are pending installations, which may
1971 // be using the temporary installation directory. Try to garbage collect
1972 // again later.
1973 MessageLoop::current()->PostDelayedTask(
1974 FROM_HERE,
1975 base::Bind(&ExtensionService::GarbageCollectExtensions, AsWeakPtr()),
1976 base::TimeDelta::FromSeconds(kGarbageCollectRetryDelay));
1977 return;
1978 }
1979
[email protected]45759612012-07-10 17:21:231980 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> info(
[email protected]2f8757c32012-06-19 19:17:471981 extension_prefs_->GetInstalledExtensionsInfo());
1982
[email protected]650b2d52013-02-10 03:41:451983 std::multimap<std::string, base::FilePath> extension_paths;
[email protected]2f8757c32012-06-19 19:17:471984 for (size_t i = 0; i < info->size(); ++i)
[email protected]0db124b02012-11-07 04:55:051985 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
1986 info->at(i)->extension_path));
1987
[email protected]399583b2012-12-11 09:33:421988 info = extension_prefs_->GetAllDelayedInstallInfo();
[email protected]0db124b02012-11-07 04:55:051989 for (size_t i = 0; i < info->size(); ++i)
1990 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
1991 info->at(i)->extension_path));
[email protected]2f8757c32012-06-19 19:17:471992
[email protected]7f8f24f2012-11-15 19:40:141993 if (!GetFileTaskRunner()->PostTask(
1994 FROM_HERE,
[email protected]2f8757c32012-06-19 19:17:471995 base::Bind(
1996 &extension_file_util::GarbageCollectExtensions,
1997 install_directory_,
[email protected]31bb5ee62012-09-12 22:58:401998 extension_paths))) {
[email protected]2f8757c32012-06-19 19:17:471999 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:402000 }
[email protected]f6ccd652010-11-17 00:48:342001
[email protected]f766a9d2012-07-11 01:57:442002#if defined(ENABLE_THEMES)
[email protected]f6ccd652010-11-17 00:48:342003 // Also garbage-collect themes. We check |profile_| to be
2004 // defensive; in the future, we may call GarbageCollectExtensions()
2005 // from somewhere other than Init() (e.g., in a timer).
2006 if (profile_) {
[email protected]d5949312012-12-03 22:13:302007 ThemeServiceFactory::GetForProfile(profile_)->RemoveUnusedThemes();
[email protected]f6ccd652010-11-17 00:48:342008 }
[email protected]f766a9d2012-07-11 01:57:442009#endif
[email protected]3cf4f0992009-02-03 23:00:302010}
2011
[email protected]fb82dcd2012-03-21 14:15:462012void ExtensionService::SyncExtensionChangeIfNeeded(const Extension& extension) {
[email protected]5db9ada2012-04-11 13:48:202013 if (app_sync_bundle_.HandlesApp(extension)) {
2014 app_sync_bundle_.SyncChangeIfNeeded(extension);
2015 } else if (extension_sync_bundle_.HandlesExtension(extension)) {
2016 extension_sync_bundle_.SyncChangeIfNeeded(extension);
[email protected]fb82dcd2012-03-21 14:15:462017 }
2018}
2019
[email protected]820d9bd2013-04-03 03:46:032020void ExtensionService::SetReadyAndNotifyListeners() {
[email protected]4a10006a2013-05-17 23:18:352021 ready_->Signal();
[email protected]820d9bd2013-04-03 03:46:032022 content::NotificationService::current()->Notify(
2023 chrome::NOTIFICATION_EXTENSIONS_READY,
2024 content::Source<Profile>(profile_),
2025 content::NotificationService::NoDetails());
2026}
2027
[email protected]eaa7dd182010-12-14 11:09:002028void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]3eeddd892013-04-17 17:00:112029 if (updater_)
[email protected]93fd78f42009-07-10 16:43:172030 updater_->Start();
[email protected]fa6a9102010-11-22 15:38:502031
[email protected]bc151cf92013-02-12 04:57:262032 OnBlacklistUpdated();
[email protected]e72e8eb82009-06-18 17:21:512033}
2034
[email protected]fcb58a862012-05-01 01:03:152035void ExtensionService::AddExtension(const Extension* extension) {
[email protected]c8d407e2011-04-28 21:27:172036 // TODO(jstritar): We may be able to get rid of this branch by overriding the
2037 // default extension state to DISABLED when the --disable-extensions flag
2038 // is set (http://crbug.com/29067).
2039 if (!extensions_enabled() &&
2040 !extension->is_theme() &&
[email protected]1d5e58b2013-01-31 08:41:402041 extension->location() != Manifest::COMPONENT &&
2042 !Manifest::IsExternalLocation(extension->location())) {
[email protected]fcb58a862012-05-01 01:03:152043 return;
[email protected]330840c2012-08-29 22:21:012044 }
[email protected]c8d407e2011-04-28 21:27:172045
[email protected]b914e2952013-04-26 07:10:032046 bool is_extension_upgrade = false;
2047 if (const Extension* old = GetInstalledExtension(extension->id())) {
2048 is_extension_upgrade = true;
2049 DCHECK_NE(extension, old);
2050 // Other than for unpacked extensions, CrxInstaller should have guaranteed
2051 // that we aren't downgrading.
2052 if (!Manifest::IsUnpackedLocation(extension->location()))
2053 CHECK_GE(extension->version()->CompareTo(*(old->version())), 0);
2054 }
2055 SetBeingUpgraded(extension, is_extension_upgrade);
[email protected]c8d407e2011-04-28 21:27:172056
[email protected]1eb175082010-02-10 09:26:162057 // The extension is now loaded, remove its data from unloaded extension map.
2058 unloaded_extension_paths_.erase(extension->id());
2059
[email protected]bb7f40952011-01-13 00:21:202060 // If a terminated extension is loaded, remove it from the terminated list.
2061 UntrackTerminatedExtension(extension->id());
2062
[email protected]f17dbd42010-08-16 23:21:102063 // If the extension was disabled for a reload, then enable it.
[email protected]ca2e82022013-04-29 16:48:412064 bool reloading = reloading_extensions_.erase(extension->id()) > 0;
[email protected]f17dbd42010-08-16 23:21:102065
[email protected]b914e2952013-04-26 07:10:032066 // Check if the extension's privileges have changed and mark the
2067 // extension disabled if necessary.
2068 CheckPermissionsIncrease(extension, is_extension_upgrade);
2069
[email protected]ca2e82022013-04-29 16:48:412070 if (is_extension_upgrade && !reloading) {
2071 // To upgrade an extension in place, unload the old one and then load the
2072 // new one. ReloadExtension disables the extension, which is sufficient.
[email protected]b914e2952013-04-26 07:10:032073 UnloadExtension(extension->id(), extension_misc::UNLOAD_REASON_UPDATE);
2074 }
[email protected]0c6da502009-08-14 22:32:392075
[email protected]695b5712012-12-06 23:55:282076 if (extension_prefs_->IsExtensionBlacklisted(extension->id())) {
[email protected]ac875372013-02-28 04:36:092077 // Only prefs is checked for the blacklist. We rely on callers to check the
2078 // blacklist before calling into here, e.g. CrxInstaller checks before
2079 // installation, we check when loading installed extensions.
[email protected]695b5712012-12-06 23:55:282080 blacklisted_extensions_.Insert(extension);
[email protected]ca2e82022013-04-29 16:48:412081 } else if (!reloading &&
2082 extension_prefs_->IsExtensionDisabled(extension->id())) {
[email protected]6f6101832012-11-27 22:10:482083 disabled_extensions_.Insert(extension);
[email protected]3bdba0d2011-08-23 07:17:302084 SyncExtensionChangeIfNeeded(*extension);
[email protected]44d62b62012-04-11 00:06:032085 content::NotificationService::current()->Notify(
2086 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
2087 content::Source<Profile>(profile_),
2088 content::Details<const Extension>(extension));
[email protected]62f051c2012-03-29 17:04:442089
[email protected]7e9e2422012-12-13 19:54:192090 // Show the extension disabled error if a permissions increase was the
2091 // only reason it was disabled.
2092 if (extension_prefs_->GetDisableReasons(extension->id()) ==
[email protected]44d62b62012-04-11 00:06:032093 Extension::DISABLE_PERMISSIONS_INCREASE) {
[email protected]62f051c2012-03-29 17:04:442094 extensions::AddExtensionDisabledError(this, extension);
2095 }
[email protected]ca2e82022013-04-29 16:48:412096 } else if (reloading) {
2097 // Replace the old extension with the new version.
2098 CHECK(!disabled_extensions_.Insert(extension));
2099 EnableExtension(extension->id());
2100 DoPostLoadTasks(extension);
[email protected]695b5712012-12-06 23:55:282101 } else {
2102 // All apps that are displayed in the launcher are ordered by their ordinals
2103 // so we must ensure they have valid ordinals.
2104 if (extension->RequiresSortOrdinal()) {
[email protected]bd36b652013-02-27 02:13:252105 if (!extension->ShouldDisplayInNewTabPage()) {
2106 extension_prefs_->extension_sorting()->MarkExtensionAsHidden(
2107 extension->id());
2108 }
[email protected]695b5712012-12-06 23:55:282109 extension_prefs_->extension_sorting()->EnsureValidOrdinals(
2110 extension->id(), syncer::StringOrdinal());
2111 }
2112
2113 extensions_.Insert(extension);
2114 SyncExtensionChangeIfNeeded(*extension);
2115 NotifyExtensionLoaded(extension);
2116 DoPostLoadTasks(extension);
[email protected]f574c402012-12-04 23:20:312117 }
[email protected]b914e2952013-04-26 07:10:032118 SetBeingUpgraded(extension, false);
[email protected]aab98a52009-12-02 03:22:352119}
2120
[email protected]8c484b742012-11-29 06:05:362121void ExtensionService::AddComponentExtension(const Extension* extension) {
2122 const std::string old_version_string(
2123 extension_prefs_->GetVersionString(extension->id()));
2124 const Version old_version(old_version_string);
2125
2126 if (!old_version.IsValid() || !old_version.Equals(*extension->version())) {
2127 VLOG(1) << "Component extension " << extension->name() << " ("
2128 << extension->id() << ") installing/upgrading from '"
2129 << old_version_string << "' to " << extension->version()->GetString();
2130
2131 AddNewOrUpdatedExtension(extension,
[email protected]399583b2012-12-11 09:33:422132 Extension::ENABLED_COMPONENT,
2133 syncer::StringOrdinal());
[email protected]8c484b742012-11-29 06:05:362134 return;
2135 }
2136
2137 AddExtension(extension);
2138}
2139
[email protected]b914e2952013-04-26 07:10:032140void ExtensionService::UpdateActivePermissions(const Extension* extension) {
[email protected]902fd7b2011-07-27 18:42:312141 // If the extension has used the optional permissions API, it will have a
2142 // custom set of active permissions defined in the extension prefs. Here,
2143 // we update the extension's active permissions based on the prefs.
[email protected]c2e66e12012-06-27 06:27:062144 scoped_refptr<PermissionSet> active_permissions =
[email protected]902fd7b2011-07-27 18:42:312145 extension_prefs()->GetActivePermissions(extension->id());
2146
[email protected]3eeddd892013-04-17 17:00:112147 if (active_permissions) {
[email protected]902fd7b2011-07-27 18:42:312148 // We restrict the active permissions to be within the bounds defined in the
2149 // extension's manifest.
2150 // a) active permissions must be a subset of optional + default permissions
2151 // b) active permissions must contains all default permissions
[email protected]c2e66e12012-06-27 06:27:062152 scoped_refptr<PermissionSet> total_permissions =
2153 PermissionSet::CreateUnion(
[email protected]2cb5e302013-05-09 05:00:062154 extensions::PermissionsData::GetRequiredPermissions(extension),
2155 extensions::PermissionsData::GetOptionalPermissions(extension));
[email protected]902fd7b2011-07-27 18:42:312156
2157 // Make sure the active permissions contain no more than optional + default.
[email protected]c2e66e12012-06-27 06:27:062158 scoped_refptr<PermissionSet> adjusted_active =
2159 PermissionSet::CreateIntersection(
[email protected]902fd7b2011-07-27 18:42:312160 total_permissions.get(), active_permissions.get());
2161
2162 // Make sure the active permissions contain the default permissions.
[email protected]c2e66e12012-06-27 06:27:062163 adjusted_active = PermissionSet::CreateUnion(
[email protected]2cb5e302013-05-09 05:00:062164 extensions::PermissionsData::GetRequiredPermissions(extension),
2165 adjusted_active.get());
[email protected]902fd7b2011-07-27 18:42:312166
[email protected]c333e792012-01-06 16:57:392167 extensions::PermissionsUpdater perms_updater(profile());
2168 perms_updater.UpdateActivePermissions(extension, adjusted_active);
[email protected]902fd7b2011-07-27 18:42:312169 }
[email protected]b914e2952013-04-26 07:10:032170}
2171
2172void ExtensionService::CheckPermissionsIncrease(const Extension* extension,
2173 bool is_extension_upgrade) {
2174 UpdateActivePermissions(extension);
[email protected]902fd7b2011-07-27 18:42:312175
[email protected]8d888c12010-11-30 00:00:252176 // We keep track of all permissions the user has granted each extension.
2177 // This allows extensions to gracefully support backwards compatibility
2178 // by including unknown permissions in their manifests. When the user
2179 // installs the extension, only the recognized permissions are recorded.
2180 // When the unknown permissions become recognized (e.g., through browser
2181 // upgrade), we can prompt the user to accept these new permissions.
2182 // Extensions can also silently upgrade to less permissions, and then
2183 // silently upgrade to a version that adds these permissions back.
2184 //
2185 // For example, pretend that Chrome 10 includes a permission "omnibox"
2186 // for an API that adds suggestions to the omnibox. An extension can
2187 // maintain backwards compatibility while still having "omnibox" in the
2188 // manifest. If a user installs the extension on Chrome 9, the browser
2189 // will record the permissions it recognized, not including "omnibox."
2190 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
2191 // will disable the extension and prompt the user to approve the increase
2192 // in privileges. The extension could then release a new version that
2193 // removes the "omnibox" permission. When the user upgrades, Chrome will
2194 // still remember that "omnibox" had been granted, so that if the
2195 // extension once again includes "omnibox" in an upgrade, the extension
2196 // can upgrade without requiring this user's approval.
[email protected]eb5e4f92012-08-15 23:33:282197 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
[email protected]8d888c12010-11-30 00:00:252198
[email protected]211a3f32013-05-28 21:48:182199 bool auto_grant_permission =
2200 (!is_extension_upgrade && extension->was_installed_by_default()) ||
2201 chrome::IsRunningInForcedAppMode();
[email protected]b914e2952013-04-26 07:10:032202 // Silently grant all active permissions to default apps only on install.
2203 // After install they should behave like other apps.
[email protected]211a3f32013-05-28 21:48:182204 // Silently grant all active permissions to apps install in kiosk mode on both
2205 // install and update.
2206 if (auto_grant_permission)
[email protected]b914e2952013-04-26 07:10:032207 GrantPermissions(extension);
2208
2209 bool is_privilege_increase = false;
2210 // We only need to compare the granted permissions to the current permissions
2211 // if the extension is not allowed to silently increase its permissions.
[email protected]13c68b62013-05-17 11:29:052212 if (!extensions::PermissionsData::CanSilentlyIncreasePermissions(extension) &&
[email protected]211a3f32013-05-28 21:48:182213 !auto_grant_permission) {
[email protected]8d888c12010-11-30 00:00:252214 // Add all the recognized permissions if the granted permissions list
2215 // hasn't been initialized yet.
[email protected]c2e66e12012-06-27 06:27:062216 scoped_refptr<PermissionSet> granted_permissions =
[email protected]902fd7b2011-07-27 18:42:312217 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:522218 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:252219
2220 // Here, we check if an extension's privileges have increased in a manner
2221 // that requires the user's approval. This could occur because the browser
2222 // upgraded and recognized additional privileges, or an extension upgrades
2223 // to a version that requires additional privileges.
[email protected]0d3e4a22011-06-23 19:02:522224 is_privilege_increase =
[email protected]902fd7b2011-07-27 18:42:312225 granted_permissions->HasLessPrivilegesThan(
2226 extension->GetActivePermissions());
[email protected]8d888c12010-11-30 00:00:252227 }
2228
2229 if (is_extension_upgrade) {
[email protected]44d62b62012-04-11 00:06:032230 // If the extension was already disabled, suppress any alerts for becoming
2231 // disabled on permissions increase.
[email protected]b914e2952013-04-26 07:10:032232 bool previously_disabled =
2233 extension_prefs_->IsExtensionDisabled(extension->id());
[email protected]7e9e2422012-12-13 19:54:192234 // Legacy disabled extensions do not have a disable reason. Infer that if
2235 // there was no permission increase, it was likely disabled by the user.
2236 if (previously_disabled && disable_reasons == Extension::DISABLE_NONE &&
[email protected]b914e2952013-04-26 07:10:032237 !extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
[email protected]7e9e2422012-12-13 19:54:192238 disable_reasons |= Extension::DISABLE_USER_ACTION;
[email protected]44d62b62012-04-11 00:06:032239 }
[email protected]7e9e2422012-12-13 19:54:192240 // Extensions that came to us disabled from sync need a similar inference,
2241 // except based on the new version's permissions.
2242 if (previously_disabled &&
2243 disable_reasons == Extension::DISABLE_UNKNOWN_FROM_SYNC) {
2244 // Remove the DISABLE_UNKNOWN_FROM_SYNC reason.
2245 extension_prefs_->ClearDisableReasons(extension->id());
2246 if (!is_privilege_increase)
2247 disable_reasons |= Extension::DISABLE_USER_ACTION;
2248 }
[email protected]b914e2952013-04-26 07:10:032249 disable_reasons &= ~Extension::DISABLE_UNKNOWN_FROM_SYNC;
[email protected]8d888c12010-11-30 00:00:252250 }
2251
2252 // Extension has changed permissions significantly. Disable it. A
2253 // notification should be sent by the caller.
2254 if (is_privilege_increase) {
[email protected]7e9e2422012-12-13 19:54:192255 disable_reasons |= Extension::DISABLE_PERMISSIONS_INCREASE;
[email protected]fe2dd7742011-04-19 22:52:492256 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
2257 RecordPermissionMessagesHistogram(
2258 extension, "Extensions.Permissions_AutoDisable");
2259 }
[email protected]b3317ad2011-04-28 23:46:002260 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:252261 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
[email protected]b914e2952013-04-26 07:10:032262 }
2263 if (disable_reasons != Extension::DISABLE_NONE) {
[email protected]eb5e4f92012-08-15 23:33:282264 extension_prefs_->AddDisableReason(
2265 extension->id(),
2266 static_cast<Extension::DisableReason>(disable_reasons));
[email protected]8d888c12010-11-30 00:00:252267 }
2268}
2269
[email protected]eaa7dd182010-12-14 11:09:002270void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:392271 std::set<std::string> extension_ids;
[email protected]84df8332011-12-06 18:22:462272 for (ExtensionSet::const_iterator iter = extensions_.begin();
2273 iter != extensions_.end(); ++iter) {
2274 const Extension* extension = *iter;
[email protected]1d5e58b2013-01-31 08:41:402275 if (!extension->is_theme() && extension->location() != Manifest::COMPONENT)
[email protected]84df8332011-12-06 18:22:462276 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:352277 }
2278
2279 child_process_logging::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:252280}
2281
[email protected]9d02fa12013-02-19 05:12:572282void ExtensionService::ScheduleLaunchOnLoad(const std::string& extension_id) {
2283 on_load_events_[extension_id] = EVENT_LAUNCHED;
2284}
2285
[email protected]8266d662011-07-12 21:53:262286void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:502287 const Extension* extension,
[email protected]98270432012-09-11 20:51:242288 const syncer::StringOrdinal& page_ordinal,
[email protected]0db124b02012-11-07 04:55:052289 bool has_requirement_errors,
2290 bool wait_for_idle) {
[email protected]a29a517a2011-01-21 21:11:122291 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:202292
[email protected]c3cfb012011-04-06 22:07:352293 const std::string& id = extension->id();
[email protected]612a1cb12012-10-17 13:18:032294 bool initial_enable = ShouldEnableOnInstall(extension);
[email protected]3f213ad2012-07-26 23:39:412295 const extensions::PendingExtensionInfo* pending_extension_info = NULL;
[email protected]51a3bf8b2012-06-08 22:53:062296 if ((pending_extension_info = pending_extension_manager()->GetById(id))) {
2297 if (!pending_extension_info->ShouldAllowInstall(*extension)) {
2298 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:322299
[email protected]31bb5ee62012-09-12 22:58:402300 LOG(WARNING) << "ShouldAllowInstall() returned false for "
2301 << id << " of type " << extension->GetType()
[email protected]65348062013-01-15 07:27:222302 << " and update URL "
2303 << extensions::ManifestURL::GetUpdateURL(extension).spec()
[email protected]31bb5ee62012-09-12 22:58:402304 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:542305
[email protected]ad50def52011-10-19 23:17:072306 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272307 chrome::NOTIFICATION_EXTENSION_INSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:532308 content::Source<Profile>(profile_),
2309 content::Details<const Extension>(extension));
[email protected]ae6cb3022011-04-04 20:11:542310
[email protected]4416c5a2010-06-26 01:28:572311 // Delete the extension directory since we're not going to
2312 // load it.
[email protected]7f8f24f2012-11-15 19:40:142313 if (!GetFileTaskRunner()->PostTask(
2314 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362315 base::Bind(&extension_file_util::DeleteFile,
[email protected]31bb5ee62012-09-12 22:58:402316 extension->path(), true))) {
[email protected]14908b72011-04-20 06:54:362317 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:402318 }
[email protected]4416c5a2010-06-26 01:28:572319 return;
2320 }
[email protected]51a3bf8b2012-06-08 22:53:062321
2322 pending_extension_manager()->Remove(id);
[email protected]4416c5a2010-06-26 01:28:572323 } else {
[email protected]c3cfb012011-04-06 22:07:352324 // We explicitly want to re-enable an uninstalled external
2325 // extension; if we're here, that means the user is manually
2326 // installing the extension.
[email protected]6cc7dbae2011-04-29 21:18:332327 if (IsExternalExtensionUninstalled(id)) {
2328 initial_enable = true;
2329 }
[email protected]aa142702010-03-26 01:26:332330 }
2331
[email protected]98270432012-09-11 20:51:242332 // Unsupported requirements overrides the management policy.
2333 if (has_requirement_errors) {
2334 initial_enable = false;
2335 extension_prefs_->AddDisableReason(
2336 id, Extension::DISABLE_UNSUPPORTED_REQUIREMENT);
2337 // If the extension was disabled because of unsupported requirements but
2338 // now supports all requirements after an update and there are not other
2339 // disable reasons, enable it.
2340 } else if (extension_prefs_->GetDisableReasons(id) ==
2341 Extension::DISABLE_UNSUPPORTED_REQUIREMENT) {
2342 initial_enable = true;
2343 extension_prefs_->ClearDisableReasons(id);
2344 }
2345
[email protected]695b5712012-12-06 23:55:282346 if (!GetInstalledExtension(extension->id())) {
[email protected]07533022011-06-27 20:42:552347 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
2348 extension->GetType(), 100);
[email protected]69084dc2012-11-17 07:39:302349 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallSource",
[email protected]1d5e58b2013-01-31 08:41:402350 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552351 RecordPermissionMessagesHistogram(
2352 extension, "Extensions.Permissions_Install");
[email protected]69084dc2012-11-17 07:39:302353 } else {
2354 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateType",
2355 extension->GetType(), 100);
2356 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateSource",
[email protected]1d5e58b2013-01-31 08:41:402357 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552358 }
2359
[email protected]a39921b42012-02-28 03:42:542360 // Certain extension locations are specific enough that we can
2361 // auto-acknowledge any extension that came from one of them.
[email protected]1d5e58b2013-01-31 08:41:402362 if (extension->location() == Manifest::EXTERNAL_POLICY_DOWNLOAD)
[email protected]a39921b42012-02-28 03:42:542363 AcknowledgeExternalExtension(extension->id());
[email protected]399583b2012-12-11 09:33:422364 const Extension::State initial_state =
2365 initial_enable ? Extension::ENABLED : Extension::DISABLED;
[email protected]e7aa7b7e2012-11-27 04:51:222366 if (ShouldDelayExtensionUpdate(id, wait_for_idle)) {
[email protected]399583b2012-12-11 09:33:422367 extension_prefs_->SetDelayedInstallInfo(extension, initial_state,
2368 page_ordinal);
[email protected]0db124b02012-11-07 04:55:052369
2370 // Transfer ownership of |extension|.
[email protected]399583b2012-12-11 09:33:422371 delayed_updates_for_idle_.Insert(extension);
[email protected]0db124b02012-11-07 04:55:052372
2373 // Notify extension of available update.
2374 extensions::RuntimeEventRouter::DispatchOnUpdateAvailableEvent(
2375 profile_, id, extension->manifest()->value());
[email protected]75bdcb872013-03-13 00:41:452376
2377 // Notify observers that app update is available.
2378 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2379 OnAppUpdateAvailable(extension->id()));
[email protected]0db124b02012-11-07 04:55:052380 return;
2381 }
2382
[email protected]399583b2012-12-11 09:33:422383 if (installs_delayed()) {
2384 extension_prefs_->SetDelayedInstallInfo(extension, initial_state,
2385 page_ordinal);
2386 delayed_installs_.Insert(extension);
2387 } else {
2388 AddNewOrUpdatedExtension(extension, initial_state, page_ordinal);
2389 }
[email protected]8c484b742012-11-29 06:05:362390}
2391
2392void ExtensionService::AddNewOrUpdatedExtension(
2393 const Extension* extension,
[email protected]399583b2012-12-11 09:33:422394 Extension::State initial_state,
2395 const syncer::StringOrdinal& page_ordinal) {
[email protected]8c484b742012-11-29 06:05:362396 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2397
[email protected]4416c5a2010-06-26 01:28:572398 extension_prefs_->OnExtensionInstalled(
[email protected]8266d662011-07-12 21:53:262399 extension,
[email protected]8c484b742012-11-29 06:05:362400 initial_state,
[email protected]36a5c4c2011-12-14 16:34:502401 page_ordinal);
[email protected]25b34332009-06-05 21:53:192402
[email protected]6f6101832012-11-27 22:10:482403 FinishInstallation(extension);
2404}
2405
2406void ExtensionService::MaybeFinishDelayedInstallation(
2407 const std::string& extension_id) {
2408 // Check if the extension already got updated.
[email protected]399583b2012-12-11 09:33:422409 if (!delayed_updates_for_idle_.Contains(extension_id))
[email protected]6f6101832012-11-27 22:10:482410 return;
2411 // Check if the extension is idle.
2412 if (!IsExtensionIdle(extension_id))
2413 return;
2414
2415 FinishDelayedInstallation(extension_id);
2416}
2417
2418void ExtensionService::FinishDelayedInstallation(
2419 const std::string& extension_id) {
2420 scoped_refptr<const Extension> extension(
2421 GetPendingExtensionUpdate(extension_id));
2422 CHECK(extension);
[email protected]399583b2012-12-11 09:33:422423 delayed_updates_for_idle_.Remove(extension_id);
[email protected]6f6101832012-11-27 22:10:482424
[email protected]399583b2012-12-11 09:33:422425 if (!extension_prefs_->FinishDelayedInstallInfo(extension_id))
[email protected]6f6101832012-11-27 22:10:482426 NOTREACHED();
2427
2428 FinishInstallation(extension);
2429}
2430
2431void ExtensionService::FinishInstallation(const Extension* extension) {
[email protected]41bb80bd2013-05-03 10:56:022432 bool is_update = GetInstalledExtension(extension->id()) != NULL;
2433 extensions::InstalledExtensionInfo details(extension, is_update);
[email protected]fcb58a862012-05-01 01:03:152434 content::NotificationService::current()->Notify(
2435 chrome::NOTIFICATION_EXTENSION_INSTALLED,
2436 content::Source<Profile>(profile_),
[email protected]41bb80bd2013-05-03 10:56:022437 content::Details<const extensions::InstalledExtensionInfo>(&details));
[email protected]fcb58a862012-05-01 01:03:152438
[email protected]d96eb512012-11-01 23:44:082439 bool unacknowledged_external = IsUnacknowledgedExternalExtension(extension);
[email protected]41070e8d2012-10-24 01:34:362440
[email protected]399583b2012-12-11 09:33:422441 // Unpacked extensions default to allowing file access, but if that has been
2442 // overridden, don't reset the value.
[email protected]1d5e58b2013-01-31 08:41:402443 if (Manifest::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]399583b2012-12-11 09:33:422444 !extension_prefs_->HasAllowFileAccessSetting(extension->id())) {
2445 extension_prefs_->SetAllowFileAccess(extension->id(), true);
2446 }
2447
[email protected]6f6101832012-11-27 22:10:482448 AddExtension(extension);
[email protected]612a1cb12012-10-17 13:18:032449
[email protected]a51c9e92012-12-09 09:19:202450#if defined(ENABLE_THEMES)
[email protected]bc151cf92013-02-12 04:57:262451 // We do this here since AddExtension() is always called on browser startup,
2452 // and we only really care about the last theme installed.
[email protected]a51c9e92012-12-09 09:19:202453 // If that ever changes and we have to move this code somewhere
2454 // else, it should be somewhere that's not in the startup path.
2455 if (extension->is_theme() && extensions_.GetByID(extension->id())) {
2456 DCHECK_EQ(extensions_.GetByID(extension->id()), extension);
2457 // Now that the theme extension is visible from outside the
2458 // ExtensionService, notify the ThemeService about the
2459 // newly-installed theme.
2460 ThemeServiceFactory::GetForProfile(profile_)->SetTheme(extension);
2461 }
2462#endif
2463
[email protected]612a1cb12012-10-17 13:18:032464 // If this is a new external extension that was disabled, alert the user
[email protected]d96eb512012-11-01 23:44:082465 // so he can reenable it. We do this last so that it has already been
2466 // added to our list of extensions.
2467 if (unacknowledged_external) {
[email protected]612a1cb12012-10-17 13:18:032468 UpdateExternalExtensionAlert();
[email protected]d96eb512012-11-01 23:44:082469 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
2470 EXTERNAL_EXTENSION_INSTALLED,
2471 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
2472 }
[email protected]4a190632009-05-09 01:07:422473}
2474
[email protected]76b65442012-11-17 14:11:482475const Extension* ExtensionService::GetPendingExtensionUpdate(
2476 const std::string& id) const {
[email protected]399583b2012-12-11 09:33:422477 return delayed_updates_for_idle_.GetByID(id);
[email protected]76b65442012-11-17 14:11:482478}
2479
[email protected]bb7f40952011-01-13 00:21:202480void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
[email protected]84df8332011-12-06 18:22:462481 if (!terminated_extensions_.Contains(extension->id()))
2482 terminated_extensions_.Insert(make_scoped_refptr(extension));
[email protected]fa2416f2011-05-03 08:41:202483
[email protected]b3f7fe22011-11-11 19:27:562484 UnloadExtension(extension->id(), extension_misc::UNLOAD_REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:202485}
2486
2487void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]fa2416f2011-05-03 08:41:202488 std::string lowercase_id = StringToLowerASCII(id);
[email protected]84df8332011-12-06 18:22:462489 terminated_extensions_.Remove(lowercase_id);
[email protected]bb7f40952011-01-13 00:21:202490}
2491
[email protected]0dfe05c2011-02-23 23:03:362492const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:472493 const std::string& id) const {
[email protected]695b5712012-12-06 23:55:282494 return GetExtensionById(id, INCLUDE_TERMINATED);
[email protected]8001df22011-04-28 19:59:472495}
2496
2497const Extension* ExtensionService::GetInstalledExtension(
2498 const std::string& id) const {
[email protected]695b5712012-12-06 23:55:282499 int include_mask = INCLUDE_ENABLED |
2500 INCLUDE_DISABLED |
2501 INCLUDE_TERMINATED |
2502 INCLUDE_BLACKLISTED;
2503 return GetExtensionById(id, include_mask);
[email protected]0dfe05c2011-02-23 23:03:362504}
2505
[email protected]eaa7dd182010-12-14 11:09:002506bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:442507 // Allow bindings for all packaged extensions and component hosted apps.
2508 const Extension* extension = extensions_.GetExtensionOrAppByURL(
2509 ExtensionURLInfo(url));
2510 return extension && (!extension->is_hosted_app() ||
[email protected]1d5e58b2013-01-31 08:41:402511 extension->location() == Manifest::COMPONENT);
[email protected]6d2e60bd2010-06-03 22:37:392512}
2513
[email protected]4d007b312012-10-17 03:00:482514bool ExtensionService::ShouldBlockUrlInBrowserTab(GURL* url) {
2515 const Extension* extension = extensions_.GetExtensionOrAppByURL(
2516 ExtensionURLInfo(*url));
2517 if (extension && extension->is_platform_app()) {
2518 *url = GURL(chrome::kExtensionInvalidRequestURL);
2519 return true;
2520 }
2521
2522 return false;
2523}
2524
[email protected]9060d8b02012-01-13 02:14:302525bool ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322526 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:142527 const Version* version,
[email protected]650b2d52013-02-10 03:41:452528 const base::FilePath& path,
[email protected]1d5e58b2013-01-31 08:41:402529 Manifest::Location location,
[email protected]47fc70c2011-12-06 07:29:512530 int creation_flags,
2531 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:382532 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2533 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012534 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]9060d8b02012-01-13 02:14:302535 return false;
[email protected]a8af9fdb2010-10-28 21:52:202536
[email protected]7577a5c52009-07-30 06:21:582537 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492538 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582539 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022540 const Extension* existing = GetExtensionById(id, true);
[email protected]910f72ce2012-08-24 01:38:352541
[email protected]7577a5c52009-07-30 06:21:582542 if (existing) {
[email protected]910f72ce2012-08-24 01:38:352543 // The default apps will have the location set as INTERNAL. Since older
2544 // default apps are installed as EXTERNAL, we override them. However, if the
2545 // app is already installed as internal, then do the version check.
[email protected]31bb5ee62012-09-12 22:58:402546 // TODO(grv) : Remove after Q1-2013.
[email protected]910f72ce2012-08-24 01:38:352547 bool is_default_apps_migration =
[email protected]1d5e58b2013-01-31 08:41:402548 (location == Manifest::INTERNAL &&
2549 Manifest::IsExternalLocation(existing->location()));
[email protected]910f72ce2012-08-24 01:38:352550
2551 if (!is_default_apps_migration) {
2552 DCHECK(version);
2553
2554 switch (existing->version()->CompareTo(*version)) {
2555 case -1: // existing version is older, we should upgrade
2556 break;
2557 case 0: // existing version is same, do nothing
2558 return false;
2559 case 1: // existing version is newer, uh-oh
2560 LOG(WARNING) << "Found external version of extension " << id
2561 << "that is older than current version. Current version "
2562 << "is: " << existing->VersionString() << ". New "
2563 << "version is: " << version->GetString()
2564 << ". Keeping current version.";
2565 return false;
2566 }
[email protected]7577a5c52009-07-30 06:21:582567 }
2568 }
2569
[email protected]9060d8b02012-01-13 02:14:302570 // If the extension is already pending, don't start an install.
[email protected]e3987852012-05-04 10:06:302571 if (!pending_extension_manager()->AddFromExternalFile(
2572 id, location, *version)) {
[email protected]9060d8b02012-01-13 02:14:302573 return false;
[email protected]e3987852012-05-04 10:06:302574 }
[email protected]9c635f22010-12-02 09:36:362575
[email protected]14908b72011-04-20 06:54:362576 // no client (silent install)
[email protected]d8c8f25f2011-11-02 18:18:012577 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(this, NULL));
[email protected]6dfbbf82010-03-12 23:09:162578 installer->set_install_source(location);
2579 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072580 installer->set_expected_version(*version);
2581 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]1bf73cc32011-10-26 22:38:312582 installer->set_creation_flags(creation_flags);
[email protected]88e8ec9152013-01-17 04:05:182583#if defined(OS_CHROMEOS)
2584 extensions::InstallLimiter::Get(profile_)->Add(installer, path);
2585#else
[email protected]6dfbbf82010-03-12 23:09:162586 installer->InstallCrx(path);
[email protected]88e8ec9152013-01-17 04:05:182587#endif
[email protected]47fc70c2011-12-06 07:29:512588
2589 // Depending on the source, a new external extension might not need a user
2590 // notification on installation. For such extensions, mark them acknowledged
2591 // now to suppress the notification.
2592 if (mark_acknowledged)
2593 AcknowledgeExternalExtension(id);
[email protected]9060d8b02012-01-13 02:14:302594
2595 return true;
[email protected]7577a5c52009-07-30 06:21:582596}
2597
[email protected]eaa7dd182010-12-14 11:09:002598void ExtensionService::ReportExtensionLoadError(
[email protected]650b2d52013-02-10 03:41:452599 const base::FilePath& extension_path,
[email protected]d11c8e92009-10-20 23:26:402600 const std::string &error,
[email protected]d11c8e92009-10-20 23:26:402601 bool be_noisy) {
[email protected]d8c8f25f2011-11-02 18:18:012602 content::NotificationService::current()->Notify(
2603 chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
2604 content::Source<Profile>(profile_),
2605 content::Details<const std::string>(&error));
[email protected]d11c8e92009-10-20 23:26:402606
[email protected]8a205c02011-02-04 20:41:332607 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]438afb12012-07-05 03:18:212608 string16 message = UTF8ToUTF16(base::StringPrintf(
[email protected]18d4b6c2010-09-21 03:21:042609 "Could not load extension from '%s'. %s",
[email protected]fc670822011-12-17 09:33:492610 path_str.c_str(), error.c_str()));
[email protected]d11c8e92009-10-20 23:26:402611 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2612}
2613
[email protected]eaa7dd182010-12-14 11:09:002614void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]3a1dc572012-07-31 22:25:132615 extensions::ExtensionHost* host) {
[email protected]406027c02010-09-27 08:03:182616 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332617 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182618 if (iter == orphaned_dev_tools_.end())
2619 return;
2620
[email protected]98f66112012-12-25 12:59:362621 DevToolsAgentHost::ConnectRenderViewHost(iter->second,
2622 host->render_view_host());
[email protected]406027c02010-09-27 08:03:182623 orphaned_dev_tools_.erase(iter);
2624}
2625
[email protected]432115822011-07-10 15:52:272626void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532627 const content::NotificationSource& source,
2628 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272629 switch (type) {
[email protected]3c4abc82012-10-22 22:25:542630 case chrome::NOTIFICATION_APP_TERMINATING:
2631 // Shutdown has started. Don't start any more extension installs.
2632 // (We cannot use ExtensionService::Shutdown() for this because it
2633 // happens too late in browser teardown.)
2634 browser_terminating_ = true;
2635 break;
[email protected]432115822011-07-10 15:52:272636 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532637 if (profile_ !=
2638 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322639 break;
[email protected]6c2381d2011-10-19 02:52:532640 }
[email protected]a4ed6282009-12-14 20:51:162641
[email protected]3a1dc572012-07-31 22:25:132642 extensions::ExtensionHost* host =
2643 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262644
[email protected]fa2416f2011-05-03 08:41:202645 // Mark the extension as terminated and Unload it. We want it to
2646 // be in a consistent state: either fully working or not loaded
2647 // at all, but never half-crashed. We do it in a PostTask so
2648 // that other handlers of this notification will still have
2649 // access to the Extension and ExtensionHost.
[email protected]14908b72011-04-20 06:54:362650 MessageLoop::current()->PostTask(
2651 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362652 base::Bind(
[email protected]fa2416f2011-05-03 08:41:202653 &ExtensionService::TrackTerminatedExtension,
[email protected]d8c8f25f2011-11-02 18:18:012654 AsWeakPtr(),
[email protected]fa2416f2011-05-03 08:41:202655 host->extension()));
[email protected]31f77262009-12-02 20:48:532656 break;
2657 }
[email protected]432115822011-07-10 15:52:272658 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
[email protected]f3b1a082011-11-18 00:34:302659 content::RenderProcessHost* process =
2660 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192661 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302662 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192663 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2664 break;
2665
[email protected]15397b42012-05-16 23:56:062666 // Extensions need to know the channel for API restrictions.
2667 process->Send(new ExtensionMsg_SetChannel(
[email protected]5243df072012-07-11 21:41:012668 extensions::Feature::GetCurrentChannel()));
[email protected]15397b42012-05-16 23:56:062669
[email protected]77a6970c2011-04-23 16:58:562670 // Valid extension function names, used to setup bindings in renderer.
2671 std::vector<std::string> function_names;
2672 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names);
2673 process->Send(new ExtensionMsg_SetFunctionNames(function_names));
[email protected]31f77262009-12-02 20:48:532674
[email protected]77a6970c2011-04-23 16:58:562675 // Scripting whitelist. This is modified by tests and must be communicated
2676 // to renderers.
2677 process->Send(new ExtensionMsg_SetScriptingWhitelist(
2678 *Extension::GetScriptingWhitelist()));
2679
2680 // Loaded extensions.
[email protected]9776e82e2011-11-15 02:17:532681 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions;
[email protected]84df8332011-12-06 18:22:462682 for (ExtensionSet::const_iterator iter = extensions_.begin();
2683 iter != extensions_.end(); ++iter) {
[email protected]19647262011-12-16 09:57:492684 // Renderers don't need to know about themes.
2685 if (!(*iter)->is_theme())
2686 loaded_extensions.push_back(ExtensionMsg_Loaded_Params(*iter));
[email protected]77a6970c2011-04-23 16:58:562687 }
[email protected]9776e82e2011-11-15 02:17:532688 process->Send(new ExtensionMsg_Loaded(loaded_extensions));
[email protected]77a6970c2011-04-23 16:58:562689 break;
2690 }
[email protected]432115822011-07-10 15:52:272691 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302692 content::RenderProcessHost* process =
2693 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192694 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302695 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192696 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2697 break;
2698
[email protected]6bc04fd82011-12-04 02:29:352699 process_map_.RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:462700 BrowserThread::PostTask(
2701 BrowserThread::IO, FROM_HERE,
2702 base::Bind(&ExtensionInfoMap::UnregisterAllExtensionsInProcess,
[email protected]31d8f5f22012-04-02 15:22:082703 system_->info_map(),
[email protected]f3b1a082011-11-18 00:34:302704 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212705 break;
2706 }
[email protected]d91268022011-08-26 13:17:372707 case chrome::NOTIFICATION_IMPORT_FINISHED: {
[email protected]cf593af2011-12-30 05:44:392708 InitAfterImport();
[email protected]d91268022011-08-26 13:17:372709 break;
2710 }
[email protected]0db124b02012-11-07 04:55:052711 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: {
2712 extensions::ExtensionHost* host =
2713 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]e7aa7b7e2012-11-27 04:51:222714 std::string extension_id = host->extension_id();
[email protected]399583b2012-12-11 09:33:422715 if (delayed_updates_for_idle_.Contains(extension_id)) {
[email protected]e7aa7b7e2012-11-27 04:51:222716 // We were waiting for this extension to become idle, it now might have,
2717 // so maybe finish installation.
2718 MessageLoop::current()->PostDelayedTask(
[email protected]0db124b02012-11-07 04:55:052719 FROM_HERE,
[email protected]6f6101832012-11-27 22:10:482720 base::Bind(&ExtensionService::MaybeFinishDelayedInstallation,
[email protected]0db124b02012-11-07 04:55:052721 AsWeakPtr(), extension_id),
2722 base::TimeDelta::FromSeconds(kUpdateIdleDelay));
[email protected]0db124b02012-11-07 04:55:052723 }
2724 break;
2725 }
[email protected]92dd8d92013-02-26 00:41:082726 case chrome::NOTIFICATION_UPGRADE_RECOMMENDED: {
2727 // Notify extensions that chrome update is available.
2728 extensions::RuntimeEventRouter::DispatchOnBrowserUpdateAvailableEvent(
2729 profile_);
[email protected]75bdcb872013-03-13 00:41:452730
2731 // Notify observers that chrome update is available.
2732 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2733 OnChromeUpdateAvailable());
[email protected]92dd8d92013-02-26 00:41:082734 break;
2735 }
[email protected]aa96d3a2010-08-21 08:45:252736
[email protected]4814b512009-11-07 00:12:292737 default:
2738 NOTREACHED() << "Unexpected notification type.";
2739 }
2740}
2741
[email protected]90bb38d2012-11-14 18:36:032742void ExtensionService::OnExtensionInstallPrefChanged() {
[email protected]a6a7ced2012-11-01 17:24:182743 IdentifyAlertableExtensions();
2744 CheckManagementPolicy();
2745}
2746
[email protected]eaa7dd182010-12-14 11:09:002747bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182748 return !GetAppIds().empty();
2749}
[email protected]377011d2010-07-20 04:18:502750
[email protected]eaa7dd182010-12-14 11:09:002751ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182752 ExtensionIdSet result;
[email protected]84df8332011-12-06 18:22:462753 for (ExtensionSet::const_iterator it = extensions_.begin();
[email protected]377011d2010-07-20 04:18:502754 it != extensions_.end(); ++it) {
[email protected]1d5e58b2013-01-31 08:41:402755 if ((*it)->is_app() && (*it)->location() != Manifest::COMPONENT)
[email protected]ec5b50d2010-10-09 16:35:182756 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502757 }
2758
[email protected]ec5b50d2010-10-09 16:35:182759 return result;
[email protected]377011d2010-07-20 04:18:502760}
[email protected]d7e9a862010-11-03 21:57:492761
[email protected]dc9a74f72012-08-17 18:07:212762bool ExtensionService::IsBackgroundPageReady(const Extension* extension) const {
[email protected]9367eabc2013-03-01 01:29:292763 if (!extensions::BackgroundInfo::HasPersistentBackgroundPage(extension))
[email protected]dc9a74f72012-08-17 18:07:212764 return true;
2765 ExtensionRuntimeDataMap::const_iterator it =
2766 extension_runtime_data_.find(extension->id());
2767 return it == extension_runtime_data_.end() ? false :
2768 it->second.background_page_ready;
[email protected]d7e9a862010-11-03 21:57:492769}
2770
[email protected]eaa7dd182010-12-14 11:09:002771void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]9367eabc2013-03-01 01:29:292772 DCHECK(extensions::BackgroundInfo::HasBackgroundPage(extension));
[email protected]d7e9a862010-11-03 21:57:492773 extension_runtime_data_[extension->id()].background_page_ready = true;
[email protected]ad50def52011-10-19 23:17:072774 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272775 chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
[email protected]6c2381d2011-10-19 02:52:532776 content::Source<const Extension>(extension),
[email protected]ad50def52011-10-19 23:17:072777 content::NotificationService::NoDetails());
[email protected]d7e9a862010-11-03 21:57:492778}
2779
[email protected]65992d52012-09-19 23:05:312780void ExtensionService::InspectBackgroundPage(const Extension* extension) {
2781 DCHECK(extension);
2782
[email protected]fdd679b2012-11-15 20:49:392783 ExtensionProcessManager* pm = system_->process_manager();
[email protected]65992d52012-09-19 23:05:312784 extensions::LazyBackgroundTaskQueue* queue =
[email protected]fdd679b2012-11-15 20:49:392785 system_->lazy_background_task_queue();
[email protected]65992d52012-09-19 23:05:312786
2787 extensions::ExtensionHost* host =
2788 pm->GetBackgroundHostForExtension(extension->id());
2789 if (host) {
2790 InspectExtensionHost(host);
2791 } else {
2792 queue->AddPendingTask(
2793 profile_, extension->id(),
2794 base::Bind(&ExtensionService::InspectExtensionHost,
2795 base::Unretained(this)));
2796 }
2797}
2798
[email protected]dc9a74f72012-08-17 18:07:212799bool ExtensionService::IsBeingUpgraded(const Extension* extension) const {
2800 ExtensionRuntimeDataMap::const_iterator it =
2801 extension_runtime_data_.find(extension->id());
2802 return it == extension_runtime_data_.end() ? false :
2803 it->second.being_upgraded;
[email protected]d7e9a862010-11-03 21:57:492804}
2805
[email protected]eaa7dd182010-12-14 11:09:002806void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]b914e2952013-04-26 07:10:032807 bool value) {
[email protected]d7e9a862010-11-03 21:57:492808 extension_runtime_data_[extension->id()].being_upgraded = value;
2809}
[email protected]1bead0712010-11-27 17:41:532810
[email protected]dc9a74f72012-08-17 18:07:212811bool ExtensionService::HasUsedWebRequest(const Extension* extension) const {
2812 ExtensionRuntimeDataMap::const_iterator it =
2813 extension_runtime_data_.find(extension->id());
2814 return it == extension_runtime_data_.end() ? false :
2815 it->second.has_used_webrequest;
[email protected]39a5b532011-10-22 01:47:072816}
2817
2818void ExtensionService::SetHasUsedWebRequest(const Extension* extension,
2819 bool value) {
2820 extension_runtime_data_[extension->id()].has_used_webrequest = value;
2821}
2822
[email protected]cebc3dc2011-04-18 17:15:002823void ExtensionService::RegisterNaClModule(const GURL& url,
2824 const std::string& mime_type) {
2825 NaClModuleInfo info;
2826 info.url = url;
2827 info.mime_type = mime_type;
2828
2829 DCHECK(FindNaClModule(url) == nacl_module_list_.end());
2830 nacl_module_list_.push_front(info);
2831}
2832
2833void ExtensionService::UnregisterNaClModule(const GURL& url) {
2834 NaClModuleInfoList::iterator iter = FindNaClModule(url);
2835 DCHECK(iter != nacl_module_list_.end());
2836 nacl_module_list_.erase(iter);
2837}
2838
[email protected]ed0ba002011-05-26 16:55:132839void ExtensionService::UpdatePluginListWithNaClModules() {
[email protected]300c0ea2011-07-15 23:04:332840 // An extension has been added which has a nacl_module component, which means
2841 // there is a MIME type that module wants to handle, so we need to add that
2842 // MIME type to plugins which handle NaCl modules in order to allow the
2843 // individual modules to handle these types.
[email protected]650b2d52013-02-10 03:41:452844 base::FilePath path;
[email protected]1e127022011-11-29 21:11:472845 if (!PathService::Get(chrome::FILE_NACL_PLUGIN, &path))
2846 return;
2847 const content::PepperPluginInfo* pepper_info =
2848 PluginService::GetInstance()->GetRegisteredPpapiPluginInfo(path);
2849 if (!pepper_info)
2850 return;
[email protected]300c0ea2011-07-15 23:04:332851
[email protected]1e127022011-11-29 21:11:472852 std::vector<webkit::WebPluginMimeType>::const_iterator mime_iter;
2853 // Check each MIME type the plugins handle for the NaCl MIME type.
2854 for (mime_iter = pepper_info->mime_types.begin();
2855 mime_iter != pepper_info->mime_types.end(); ++mime_iter) {
2856 if (mime_iter->mime_type == kNaClPluginMimeType) {
2857 // This plugin handles "application/x-nacl".
[email protected]300c0ea2011-07-15 23:04:332858
[email protected]1e127022011-11-29 21:11:472859 PluginService::GetInstance()->
2860 UnregisterInternalPlugin(pepper_info->path);
[email protected]300c0ea2011-07-15 23:04:332861
[email protected]1e127022011-11-29 21:11:472862 webkit::WebPluginInfo info = pepper_info->ToWebPluginInfo();
[email protected]300c0ea2011-07-15 23:04:332863
[email protected]1e127022011-11-29 21:11:472864 for (ExtensionService::NaClModuleInfoList::const_iterator iter =
2865 nacl_module_list_.begin();
2866 iter != nacl_module_list_.end(); ++iter) {
2867 // Add the MIME type specified in the extension to this NaCl plugin,
2868 // With an extra "nacl" argument to specify the location of the NaCl
2869 // manifest file.
2870 webkit::WebPluginMimeType mime_type_info;
2871 mime_type_info.mime_type = iter->mime_type;
2872 mime_type_info.additional_param_names.push_back(UTF8ToUTF16("nacl"));
2873 mime_type_info.additional_param_values.push_back(
2874 UTF8ToUTF16(iter->url.spec()));
2875 info.mime_types.push_back(mime_type_info);
[email protected]300c0ea2011-07-15 23:04:332876 }
[email protected]1e127022011-11-29 21:11:472877
2878 PluginService::GetInstance()->RefreshPlugins();
[email protected]c6f3dea2012-01-14 02:23:112879 PluginService::GetInstance()->RegisterInternalPlugin(info, true);
[email protected]1e127022011-11-29 21:11:472880 // This plugin has been modified, no need to check the rest of its
2881 // types, but continue checking other plugins.
2882 break;
[email protected]ed0ba002011-05-26 16:55:132883 }
2884 }
[email protected]ed0ba002011-05-26 16:55:132885}
2886
[email protected]cebc3dc2011-04-18 17:15:002887ExtensionService::NaClModuleInfoList::iterator
2888 ExtensionService::FindNaClModule(const GURL& url) {
2889 for (NaClModuleInfoList::iterator iter = nacl_module_list_.begin();
2890 iter != nacl_module_list_.end(); ++iter) {
2891 if (iter->url == url)
2892 return iter;
2893 }
2894 return nacl_module_list_.end();
2895}
[email protected]6cafe35a2012-08-27 22:40:502896
[email protected]32b36c62012-10-24 05:37:412897void ExtensionService::DoPostLoadTasks(const Extension* extension) {
2898 std::map<std::string, int>::iterator it =
2899 on_load_events_.find(extension->id());
2900 if (it == on_load_events_.end())
2901 return;
2902
2903 int events_to_fire = it->second;
2904 extensions::LazyBackgroundTaskQueue* queue =
2905 system_->lazy_background_task_queue();
2906 if (queue->ShouldEnqueueTask(profile(), extension)) {
2907 if (events_to_fire & EVENT_LAUNCHED)
[email protected]6cafe35a2012-08-27 22:40:502908 queue->AddPendingTask(profile(), extension->id(),
2909 base::Bind(&ExtensionService::LaunchApplication));
[email protected]fc2a40f2013-03-13 13:14:572910 if (events_to_fire & EVENT_RESTARTED) {
[email protected]32b36c62012-10-24 05:37:412911 queue->AddPendingTask(profile(), extension->id(),
[email protected]961745f2013-05-25 14:09:242912 base::Bind(&ExtensionService::RestartApplication));
[email protected]fc2a40f2013-03-13 13:14:572913 }
[email protected]6cafe35a2012-08-27 22:40:502914 }
[email protected]32b36c62012-10-24 05:37:412915
2916 on_load_events_.erase(it);
[email protected]6cafe35a2012-08-27 22:40:502917}
2918
2919// static
2920void ExtensionService::LaunchApplication(
2921 extensions::ExtensionHost* extension_host) {
2922 if (!extension_host)
2923 return;
2924
2925#if !defined(OS_ANDROID)
2926 extensions::LaunchPlatformApp(extension_host->profile(),
2927 extension_host->extension(),
[email protected]650b2d52013-02-10 03:41:452928 NULL, base::FilePath());
[email protected]6cafe35a2012-08-27 22:40:502929#endif
2930}
[email protected]65992d52012-09-19 23:05:312931
[email protected]32b36c62012-10-24 05:37:412932// static
2933void ExtensionService::RestartApplication(
[email protected]32b36c62012-10-24 05:37:412934 extensions::ExtensionHost* extension_host) {
2935 if (!extension_host)
2936 return;
2937
2938#if !defined(OS_ANDROID)
[email protected]961745f2013-05-25 14:09:242939 extensions::RestartPlatformApp(
2940 extension_host->profile(), extension_host->extension());
[email protected]32b36c62012-10-24 05:37:412941#endif
2942}
2943
2944bool ExtensionService::HasShellWindows(const std::string& extension_id) {
2945 const Extension* current_extension = GetExtensionById(extension_id, false);
2946 return current_extension && current_extension->is_platform_app() &&
2947 !extensions::ShellWindowRegistry::Get(profile_)->
2948 GetShellWindowsForApp(extension_id).empty();
2949}
2950
[email protected]65992d52012-09-19 23:05:312951void ExtensionService::InspectExtensionHost(
2952 extensions::ExtensionHost* host) {
2953 if (host)
2954 DevToolsWindow::OpenDevToolsWindow(host->render_view_host());
2955}
[email protected]612a1cb12012-10-17 13:18:032956
2957bool ExtensionService::ShouldEnableOnInstall(const Extension* extension) {
2958 // Extensions installed by policy can't be disabled. So even if a previous
2959 // installation disabled the extension, make sure it is now enabled.
2960 if (system_->management_policy()->MustRemainEnabled(extension, NULL))
2961 return true;
2962
2963 if (extension_prefs_->IsExtensionDisabled(extension->id()))
2964 return false;
2965
[email protected]41070e8d2012-10-24 01:34:362966 if (FeatureSwitch::prompt_for_external_extensions()->IsEnabled()) {
[email protected]612a1cb12012-10-17 13:18:032967 // External extensions are initially disabled. We prompt the user before
[email protected]2c495c42013-01-04 21:49:542968 // enabling them. Hosted apps are excepted because they are not dangerous
2969 // (they need to be launched by the user anyway).
[email protected]1d5e58b2013-01-31 08:41:402970 if (extension->GetType() != Manifest::TYPE_HOSTED_APP &&
2971 Manifest::IsExternalLocation(extension->location()) &&
[email protected]612a1cb12012-10-17 13:18:032972 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id())) {
2973 return false;
2974 }
2975 }
[email protected]612a1cb12012-10-17 13:18:032976
2977 return true;
2978}
[email protected]0db124b02012-11-07 04:55:052979
2980bool ExtensionService::IsExtensionIdle(const std::string& extension_id) const {
[email protected]d9a61e12012-11-14 02:43:472981 ExtensionProcessManager* process_manager = system_->process_manager();
[email protected]fc332ae2012-11-14 20:17:332982 DCHECK(process_manager);
[email protected]0db124b02012-11-07 04:55:052983 extensions::ExtensionHost* host =
[email protected]d9a61e12012-11-14 02:43:472984 process_manager->GetBackgroundHostForExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:222985 if (host)
2986 return false;
2987 return process_manager->GetRenderViewHostsForExtension(extension_id).empty();
2988}
2989
2990bool ExtensionService::ShouldDelayExtensionUpdate(
2991 const std::string& extension_id,
2992 bool wait_for_idle) const {
2993 const char kOnUpdateAvailableEvent[] = "runtime.onUpdateAvailable";
2994
2995 // If delayed updates are globally disabled, or just for this extension,
2996 // don't delay.
2997 if (!install_updates_when_idle_ || !wait_for_idle)
2998 return false;
2999
[email protected]695b5712012-12-06 23:55:283000 const Extension* old = GetInstalledExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:223001 // If there is no old extension, this is not an update, so don't delay.
3002 if (!old)
3003 return false;
3004
[email protected]9367eabc2013-03-01 01:29:293005 if (extensions::BackgroundInfo::HasPersistentBackgroundPage(old)) {
[email protected]e7aa7b7e2012-11-27 04:51:223006 // Delay installation if the extension listens for the onUpdateAvailable
3007 // event.
3008 return system_->event_router()->ExtensionHasEventListener(
3009 extension_id, kOnUpdateAvailableEvent);
3010 } else {
3011 // Delay installation if the extension is not idle.
3012 return !IsExtensionIdle(extension_id);
3013 }
[email protected]0db124b02012-11-07 04:55:053014}
[email protected]fdd679b2012-11-15 20:49:393015
[email protected]399583b2012-12-11 09:33:423016void ExtensionService::GarbageCollectIsolatedStorage() {
[email protected]650b2d52013-02-10 03:41:453017 scoped_ptr<base::hash_set<base::FilePath> > active_paths(
3018 new base::hash_set<base::FilePath>());
[email protected]399583b2012-12-11 09:33:423019 for (ExtensionSet::const_iterator it = extensions_.begin();
3020 it != extensions_.end(); ++it) {
[email protected]561e33d52013-04-03 06:58:433021 if (extensions::AppIsolationInfo::HasIsolatedStorage(*it)) {
[email protected]399583b2012-12-11 09:33:423022 active_paths->insert(
3023 BrowserContext::GetStoragePartitionForSite(
3024 profile_,
3025 GetSiteForExtensionId((*it)->id()))->GetPath());
3026 }
3027 }
3028
3029 DCHECK(!installs_delayed());
3030 set_installs_delayed(true);
3031 BrowserContext::GarbageCollectStoragePartitions(
3032 profile_, active_paths.Pass(),
3033 base::Bind(&ExtensionService::OnGarbageCollectIsolatedStorageFinished,
3034 AsWeakPtr()));
3035}
3036
3037void ExtensionService::OnGarbageCollectIsolatedStorageFinished() {
3038 set_installs_delayed(false);
3039 for (ExtensionSet::const_iterator it = delayed_installs_.begin();
3040 it != delayed_installs_.end();
3041 ++it) {
3042 FinishDelayedInstallation((*it)->id());
3043 }
3044 for (ExtensionSet::const_iterator it = delayed_updates_for_idle_.begin();
3045 it != delayed_updates_for_idle_.end();
3046 ++it) {
3047 MaybeFinishDelayedInstallation((*it)->id());
3048 }
3049 delayed_installs_.Clear();
3050}
3051
3052void ExtensionService::OnNeedsToGarbageCollectIsolatedStorage() {
3053 extension_prefs_->SetNeedsStorageGarbageCollection(true);
3054}
3055
[email protected]fdd679b2012-11-15 20:49:393056void ExtensionService::OnBlacklistUpdated() {
[email protected]695b5712012-12-06 23:55:283057 blacklist_->GetBlacklistedIDs(
3058 GenerateInstalledExtensionsSet()->GetIDs(),
3059 base::Bind(&ExtensionService::ManageBlacklist,
3060 AsWeakPtr(),
3061 blacklisted_extensions_.GetIDs()));
3062}
3063
3064void ExtensionService::ManageBlacklist(
3065 const std::set<std::string>& old_blacklisted_ids,
3066 const std::set<std::string>& new_blacklisted_ids) {
3067 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
3068
3069 std::set<std::string> no_longer_blacklisted;
3070 std::set_difference(old_blacklisted_ids.begin(), old_blacklisted_ids.end(),
3071 new_blacklisted_ids.begin(), new_blacklisted_ids.end(),
3072 std::inserter(no_longer_blacklisted,
3073 no_longer_blacklisted.begin()));
3074 std::set<std::string> not_yet_blacklisted;
3075 std::set_difference(new_blacklisted_ids.begin(), new_blacklisted_ids.end(),
3076 old_blacklisted_ids.begin(), old_blacklisted_ids.end(),
3077 std::inserter(not_yet_blacklisted,
3078 not_yet_blacklisted.begin()));
3079
3080 for (std::set<std::string>::iterator it = no_longer_blacklisted.begin();
3081 it != no_longer_blacklisted.end(); ++it) {
3082 scoped_refptr<const Extension> extension =
3083 blacklisted_extensions_.GetByID(*it);
3084 DCHECK(extension);
3085 if (!extension)
3086 continue;
3087 blacklisted_extensions_.Remove(*it);
3088 AddExtension(extension);
[email protected]ac875372013-02-28 04:36:093089 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.UnblacklistInstalled",
3090 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:283091 }
3092
3093 for (std::set<std::string>::iterator it = not_yet_blacklisted.begin();
3094 it != not_yet_blacklisted.end(); ++it) {
3095 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
3096 DCHECK(extension);
3097 if (!extension)
3098 continue;
3099 blacklisted_extensions_.Insert(extension);
3100 UnloadExtension(*it, extension_misc::UNLOAD_REASON_BLACKLIST);
[email protected]ac875372013-02-28 04:36:093101 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.BlacklistInstalled",
3102 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:283103 }
3104
3105 IdentifyAlertableExtensions();
[email protected]fdd679b2012-11-15 20:49:393106}
[email protected]75bdcb872013-03-13 00:41:453107
3108void ExtensionService::AddUpdateObserver(extensions::UpdateObserver* observer) {
3109 update_observers_.AddObserver(observer);
3110}
3111
3112void ExtensionService::RemoveUpdateObserver(
3113 extensions::UpdateObserver* observer) {
3114 update_observers_.RemoveObserver(observer);
3115}