blob: 63c35b2746d77c7fdea197a2d5ab21741b9447c0 [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]15730c42009-09-03 00:03:2030#include "chrome/browser/browser_process.h"
[email protected]70019152012-12-19 11:44:1931#include "chrome/browser/devtools/devtools_window.h"
[email protected]32b36c62012-10-24 05:37:4132#include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h"
[email protected]895a1e52012-05-15 02:50:1233#include "chrome/browser/extensions/api/declarative/rules_registry_service.h"
[email protected]ad445762013-01-23 00:47:4434#include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
[email protected]fc2a40f2013-03-13 13:14:5735#include "chrome/browser/extensions/api/file_handlers/app_file_handler_util.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]65348062013-01-15 07:27:2290#include "chrome/common/extensions/manifest_url_handler.h"
[email protected]25b34332009-06-05 21:53:1991#include "chrome/common/pref_names.h"
[email protected]21f2e2d2013-01-23 22:59:5292#include "chrome/common/startup_metric_utils.h"
[email protected]a57209872009-05-04 22:53:1493#include "chrome/common/url_constants.h"
[email protected]c38831a12011-10-28 12:44:4994#include "content/public/browser/browser_thread.h"
[email protected]98f66112012-12-25 12:59:3695#include "content/public/browser/devtools_agent_host.h"
[email protected]05aad2da2011-10-28 10:12:3796#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1697#include "content/public/browser/notification_types.h"
[email protected]3a5180ae2011-12-21 02:39:3898#include "content/public/browser/plugin_service.h"
[email protected]f3b1a082011-11-18 00:34:3099#include "content/public/browser/render_process_host.h"
[email protected]399583b2012-12-11 09:33:42100#include "content/public/browser/site_instance.h"
101#include "content/public/browser/storage_partition.h"
[email protected]24ea7a12013-01-27 23:54:53102#include "content/public/browser/url_data_source.h"
[email protected]1e127022011-11-29 21:11:47103#include "content/public/common/pepper_plugin_info.h"
[email protected]e9f541a2012-11-19 21:52:31104#include "extensions/common/error_utils.h"
[email protected]c10da4b02010-03-25 14:38:32105#include "googleurl/src/gurl.h"
[email protected]65187152012-06-02 13:14:14106#include "grit/generated_resources.h"
[email protected]be28b5f42012-07-20 11:31:25107#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
[email protected]895a1e52012-05-15 02:50:12108#include "sync/api/sync_change.h"
109#include "sync/api/sync_error_factory.h"
[email protected]24b538a2010-02-27 01:22:44110#include "webkit/database/database_tracker.h"
111#include "webkit/database/database_util.h"
[email protected]79a60642012-10-20 21:03:18112
[email protected]eed367e2011-04-12 03:43:31113#if defined(OS_CHROMEOS)
[email protected]88e8ec9152013-01-17 04:05:18114#include "chrome/browser/chromeos/extensions/install_limiter.h"
[email protected]b777b332011-04-16 04:01:08115#include "webkit/fileapi/file_system_context.h"
116#include "webkit/fileapi/file_system_mount_point_provider.h"
[email protected]eed367e2011-04-12 03:43:31117#endif
118
[email protected]55eb70e762012-02-20 17:38:39119using content::BrowserContext;
[email protected]631bb742011-11-02 11:29:39120using content::BrowserThread;
[email protected]98f66112012-12-25 12:59:36121using content::DevToolsAgentHost;
[email protected]3a5180ae2011-12-21 02:39:38122using content::PluginService;
[email protected]bf3d9df2012-07-24 23:20:27123using extensions::CrxInstaller;
[email protected]1c321ee52012-05-21 03:02:34124using extensions::Extension;
125using extensions::ExtensionIdSet;
126using extensions::ExtensionInfo;
[email protected]215a7be2012-10-22 19:53:42127using extensions::FeatureSwitch;
[email protected]1d5e58b2013-01-31 08:41:40128using extensions::Manifest;
[email protected]c2e66e12012-06-27 06:27:06129using extensions::PermissionMessage;
130using extensions::PermissionMessages;
131using extensions::PermissionSet;
[email protected]e410b5f2012-12-14 14:02:24132using extensions::UnloadedExtensionInfo;
[email protected]5ef47ec2010-01-28 05:58:05133
[email protected]c6d474f82009-12-16 21:11:06134namespace errors = extension_manifest_errors;
135
[email protected]b6ab96d2009-08-20 18:58:19136namespace {
137
[email protected]d96eb512012-11-01 23:44:08138// Histogram values for logging events related to externally installed
139// extensions.
140enum ExternalExtensionEvent {
141 EXTERNAL_EXTENSION_INSTALLED = 0,
142 EXTERNAL_EXTENSION_IGNORED,
143 EXTERNAL_EXTENSION_REENABLED,
144 EXTERNAL_EXTENSION_UNINSTALLED,
145 EXTERNAL_EXTENSION_BUCKET_BOUNDARY,
146};
147
[email protected]612a1cb12012-10-17 13:18:03148// Prompt the user this many times before considering an extension acknowledged.
149static const int kMaxExtensionAcknowledgePromptCount = 3;
150
[email protected]0db124b02012-11-07 04:55:05151// Wait this many seconds after an extensions becomes idle before updating it.
152static const int kUpdateIdleDelay = 5;
153
[email protected]9bd9a6862012-11-29 09:24:22154// Wait this many seconds before trying to garbage collect extensions again.
155static const int kGarbageCollectRetryDelay = 30;
156
[email protected]300c0ea2011-07-15 23:04:33157const char* kNaClPluginMimeType = "application/x-nacl";
158
[email protected]3bdba0d2011-08-23 07:17:30159static bool IsSyncableExtension(const Extension& extension) {
160 return extension.GetSyncType() == Extension::SYNC_TYPE_EXTENSION;
161}
162
163static bool IsSyncableApp(const Extension& extension) {
164 return extension.GetSyncType() == Extension::SYNC_TYPE_APP;
165}
166
[email protected]c6d474f82009-12-16 21:11:06167} // namespace
[email protected]b6ab96d2009-08-20 18:58:19168
[email protected]eaa7dd182010-12-14 11:09:00169ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49170 : background_page_ready(false),
[email protected]f8bf5992011-10-24 22:38:57171 being_upgraded(false),
172 has_used_webrequest(false) {
[email protected]d7e9a862010-11-03 21:57:49173}
174
[email protected]eaa7dd182010-12-14 11:09:00175ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49176}
177
[email protected]cebc3dc2011-04-18 17:15:00178ExtensionService::NaClModuleInfo::NaClModuleInfo() {
179}
180
181ExtensionService::NaClModuleInfo::~NaClModuleInfo() {
182}
183
[email protected]eaa7dd182010-12-14 11:09:00184// ExtensionService.
[email protected]6014d672008-12-05 00:38:25185
[email protected]31bb5ee62012-09-12 22:58:40186const char ExtensionService::kInstallDirectoryName[] = "Extensions";
[email protected]15d5b472011-11-24 04:17:24187
[email protected]31bb5ee62012-09-12 22:58:40188const char ExtensionService::kLocalAppSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24189 "Local App Settings";
[email protected]31bb5ee62012-09-12 22:58:40190const char ExtensionService::kLocalExtensionSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24191 "Local Extension Settings";
[email protected]31bb5ee62012-09-12 22:58:40192const char ExtensionService::kSyncAppSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24193 "Sync App Settings";
[email protected]31bb5ee62012-09-12 22:58:40194const char ExtensionService::kSyncExtensionSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24195 "Sync Extension Settings";
[email protected]675bb01012012-12-14 22:03:29196const char ExtensionService::kManagedSettingsDirectoryName[] =
197 "Managed Extension Settings";
[email protected]31bb5ee62012-09-12 22:58:40198const char ExtensionService::kStateStoreName[] = "Extension State";
[email protected]a690e292012-12-19 19:22:49199const char ExtensionService::kRulesStoreName[] = "Extension Rules";
[email protected]494c06e2009-07-25 01:06:42200
[email protected]8e4560b62011-01-14 10:09:14201void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12202 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20203
[email protected]7a4c6852010-09-16 03:44:22204 // Check if the providers know about this extension.
[email protected]5df038b2012-07-16 19:03:27205 extensions::ProviderCollection::const_iterator i;
[email protected]0a60a2e2010-10-25 16:15:21206 for (i = external_extension_providers_.begin();
207 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14208 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21209 if (i->get()->HasExtension(id))
210 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22211 }
212
[email protected]0f48fca2011-05-19 18:46:35213 // We get the list of external extensions to check from preferences.
214 // It is possible that an extension has preferences but is not loaded.
215 // For example, an extension that requires experimental permissions
216 // will not be loaded if the experimental command line flag is not used.
217 // In this case, do not uninstall.
[email protected]31bb5ee62012-09-12 22:58:40218 if (!GetInstalledExtension(id)) {
[email protected]0f48fca2011-05-19 18:46:35219 // We can't call UninstallExtension with an unloaded/invalid
220 // extension ID.
221 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
222 << "with id: " << id;
223 return;
224 }
[email protected]d6ebc9792011-04-07 18:18:33225 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22226}
227
[email protected]7f8f24f2012-11-15 19:40:14228void ExtensionService::SetFileTaskRunnerForTesting(
229 base::SequencedTaskRunner* task_runner) {
230 file_task_runner_ = task_runner;
231}
232
[email protected]8e4560b62011-01-14 10:09:14233void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22234 external_extension_providers_.clear();
235}
236
[email protected]8e4560b62011-01-14 10:09:14237void ExtensionService::AddProviderForTesting(
[email protected]5df038b2012-07-16 19:03:27238 extensions::ExternalProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12239 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21240 external_extension_providers_.push_back(
[email protected]5df038b2012-07-16 19:03:27241 linked_ptr<extensions::ExternalProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22242}
243
[email protected]9060d8b02012-01-13 02:14:30244bool ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22245 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42246 const GURL& update_url,
[email protected]1d5e58b2013-01-31 08:41:40247 Manifest::Location location) {
[email protected]ab22ba42011-01-14 16:36:38248 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
249 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20250
[email protected]8a87a5332011-08-11 17:54:59251 const Extension* extension = GetExtensionById(id, true);
252 if (extension) {
253 // Already installed. Skip this install if the current location has
254 // higher priority than |location|.
[email protected]1d5e58b2013-01-31 08:41:40255 Manifest::Location current = extension->location();
256 if (current == Manifest::GetHigherPriorityLocation(current, location))
[email protected]9060d8b02012-01-13 02:14:30257 return false;
[email protected]8a87a5332011-08-11 17:54:59258 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22259 }
[email protected]9060d8b02012-01-13 02:14:30260
261 // Add |id| to the set of pending extensions. If it can not be added,
262 // then there is already a pending record from a higher-priority install
263 // source. In this case, signal that this extension will not be
264 // installed by returning false.
265 if (!pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]31bb5ee62012-09-12 22:58:40266 id, update_url, location)) {
[email protected]9060d8b02012-01-13 02:14:30267 return false;
[email protected]31bb5ee62012-09-12 22:58:40268 }
[email protected]9060d8b02012-01-13 02:14:30269
[email protected]94fde232012-04-27 10:22:30270 update_once_all_providers_are_ready_ = true;
[email protected]9060d8b02012-01-13 02:14:30271 return true;
[email protected]7a4c6852010-09-16 03:44:22272}
273
[email protected]dc9a74f72012-08-17 18:07:21274const Extension* ExtensionService::GetInstalledApp(const GURL& url) const {
[email protected]615d88f2011-12-13 01:47:44275 const Extension* extension = extensions_.GetExtensionOrAppByURL(
276 ExtensionURLInfo(url));
[email protected]dc9a74f72012-08-17 18:07:21277 return (extension && extension->is_app()) ? extension : NULL;
[email protected]d9696672011-03-15 22:45:09278}
279
[email protected]dc9a74f72012-08-17 18:07:21280bool ExtensionService::IsInstalledApp(const GURL& url) const {
[email protected]d9696672011-03-15 22:45:09281 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27282}
283
[email protected]404fb002012-10-02 21:06:17284const Extension* ExtensionService::GetIsolatedAppForRenderer(
[email protected]dc9a74f72012-08-17 18:07:21285 int renderer_child_id) const {
[email protected]404fb002012-10-02 21:06:17286 std::set<std::string> extension_ids =
287 process_map_.GetExtensionsInProcess(renderer_child_id);
288 // All apps in one process share the same partition.
289 // It is only possible for the app to have isolated storage
290 // if there is only 1 app in the process.
291 if (extension_ids.size() != 1)
292 return NULL;
293
294 const extensions::Extension* extension =
295 extensions_.GetByID(*(extension_ids.begin()));
[email protected]c86a8262013-03-31 07:58:57296 // We still need to check is_storage_isolated(),
[email protected]404fb002012-10-02 21:06:17297 // because it's common for there to be one extension in a process
[email protected]c86a8262013-03-31 07:58:57298 // with is_storage_isolated() == false.
299 if (extension && extension->is_storage_isolated())
[email protected]404fb002012-10-02 21:06:17300 return extension;
301
302 return NULL;
[email protected]da5683db2011-04-23 17:12:21303}
304
[email protected]6aeac8342010-10-01 20:21:18305// static
[email protected]d6ebc9792011-04-07 18:18:33306// This function is used to implement the command-line switch
[email protected]3bdba0d2011-08-23 07:17:30307// --uninstall-extension, and to uninstall an extension via sync. The LOG
308// statements within this function are used to inform the user if the uninstall
309// cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00310bool ExtensionService::UninstallExtensionHelper(
311 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18312 const std::string& extension_id) {
[email protected]d6ebc9792011-04-07 18:18:33313 // We can't call UninstallExtension with an invalid extension ID.
[email protected]31bb5ee62012-09-12 22:58:40314 if (!extensions_service->GetInstalledExtension(extension_id)) {
[email protected]6aeac8342010-10-01 20:21:18315 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33316 << "id: " << extension_id;
317 return false;
[email protected]6aeac8342010-10-01 20:21:18318 }
319
[email protected]d6ebc9792011-04-07 18:18:33320 // The following call to UninstallExtension will not allow an uninstall of a
321 // policy-controlled extension.
[email protected]65187152012-06-02 13:14:14322 string16 error;
[email protected]d6ebc9792011-04-07 18:18:33323 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
324 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
325 << ": " << error;
326 return false;
327 }
[email protected]95da88c42011-03-31 10:07:33328
[email protected]6aeac8342010-10-01 20:21:18329 return true;
330}
331
[email protected]eaa7dd182010-12-14 11:09:00332ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24333 const CommandLine* command_line,
[email protected]650b2d52013-02-10 03:41:45334 const base::FilePath& install_directory,
[email protected]45759612012-07-10 17:21:23335 extensions::ExtensionPrefs* extension_prefs,
[email protected]fdd679b2012-11-15 20:49:39336 extensions::Blacklist* blacklist,
[email protected]0436b102011-04-15 18:30:03337 bool autoupdate_enabled,
338 bool extensions_enabled)
[email protected]fdd679b2012-11-15 20:49:39339 : extensions::Blacklist::Observer(blacklist),
340 profile_(profile),
[email protected]bd306722012-07-11 20:43:59341 system_(extensions::ExtensionSystem::Get(profile)),
[email protected]73c47932010-12-06 18:13:43342 extension_prefs_(extension_prefs),
[email protected]695b5712012-12-06 23:55:28343 blacklist_(blacklist),
[email protected]cccdf0aa2011-11-11 03:43:38344 settings_frontend_(extensions::SettingsFrontend::Create(profile)),
[email protected]14908b72011-04-20 06:54:36345 pending_extension_manager_(*ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]a9b00ac2009-06-25 21:03:23346 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03347 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13348 show_extensions_prompts_(true),
[email protected]fc332ae2012-11-14 20:17:33349 install_updates_when_idle_(true),
[email protected]3ecda252010-11-18 19:50:55350 ready_(false),
[email protected]14908b72011-04-20 06:54:36351 toolbar_model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]8b280302011-10-13 22:22:23352 menu_manager_(profile),
[email protected]7c1490da2011-10-11 18:53:25353 event_routers_initialized_(false),
[email protected]dff1e042012-04-27 10:59:18354 update_once_all_providers_are_ready_(false),
[email protected]3c4abc82012-10-22 22:25:54355 browser_terminating_(false),
[email protected]399583b2012-12-11 09:33:42356 installs_delayed_(false),
[email protected]1ff4d282012-12-08 00:39:21357 wipeout_is_active_(false),
[email protected]3a2748c42013-01-15 10:13:23358 wipeout_count_(0u),
[email protected]5db9ada2012-04-11 13:48:20359 app_sync_bundle_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]69496e52013-03-07 08:37:42360 extension_sync_bundle_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
[email protected]a29a517a2011-01-21 21:11:12361 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20362
[email protected]36a784c2009-06-23 06:21:08363 // Figure out if extension installation should be enabled.
[email protected]31bb5ee62012-09-12 22:58:40364 if (command_line->HasSwitch(switches::kDisableExtensions) ||
365 profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
[email protected]6d60703b2009-08-29 01:29:23366 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18367 }
[email protected]36a784c2009-06-23 06:21:08368
[email protected]3c4abc82012-10-22 22:25:54369 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
370 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27371 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07372 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27373 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
[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_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07376 content::NotificationService::AllBrowserContextsAndSources());
[email protected]0db124b02012-11-07 04:55:05377 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
378 content::NotificationService::AllBrowserContextsAndSources());
[email protected]92dd8d92013-02-26 00:41:08379 registrar_.Add(this, chrome::NOTIFICATION_UPGRADE_RECOMMENDED,
380 content::NotificationService::AllBrowserContextsAndSources());
[email protected]2fb7dc982010-09-29 12:24:28381 pref_change_registrar_.Init(profile->GetPrefs());
[email protected]90bb38d2012-11-14 18:36:03382 base::Closure callback =
383 base::Bind(&ExtensionService::OnExtensionInstallPrefChanged,
384 base::Unretained(this));
385 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, callback);
386 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, callback);
[email protected]e410b5f2012-12-14 14:02:24387 pref_change_registrar_.Add(prefs::kExtensionAllowedTypes, callback);
[email protected]4814b512009-11-07 00:12:29388
[email protected]93fd78f42009-07-10 16:43:17389 // Set up the ExtensionUpdater
390 if (autoupdate_enabled) {
391 int update_frequency = kDefaultUpdateFrequencySeconds;
392 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25393 base::StringToInt(command_line->GetSwitchValueASCII(
394 switches::kExtensionsUpdateFrequency),
395 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17396 }
[email protected]42a08162012-03-16 18:09:11397 updater_.reset(new extensions::ExtensionUpdater(this,
398 extension_prefs,
399 profile->GetPrefs(),
400 profile,
[email protected]fdd679b2012-11-15 20:49:39401 blacklist,
[email protected]42a08162012-03-16 18:09:11402 update_frequency));
[email protected]93fd78f42009-07-10 16:43:17403 }
404
[email protected]25ae0152011-11-18 14:40:02405 component_loader_.reset(
406 new extensions::ComponentLoader(this,
407 profile->GetPrefs(),
408 g_browser_process->local_state()));
[email protected]8e4560b62011-01-14 10:09:14409
[email protected]0436b102011-04-15 18:30:03410 if (extensions_enabled_) {
[email protected]ae4c37e2012-12-21 01:16:25411 CHECK(!ProfileManager::IsImportProcess(*command_line));
412 extensions::ExternalProviderImpl::CreateExternalProviders(
413 this, profile_, &external_extension_providers_);
[email protected]873531342011-03-09 12:16:05414 }
[email protected]b671760b2010-07-15 21:13:47415
[email protected]fb82dcd2012-03-21 14:15:46416 // Set this as the ExtensionService for extension sorting to ensure it
417 // cause syncs if required.
418 extension_prefs_->extension_sorting()->SetExtensionService(this);
419
[email protected]9ce11d22012-08-08 23:20:13420#if defined(ENABLE_EXTENSIONS)
[email protected]c77f2352012-08-08 22:07:58421 extension_action_storage_manager_.reset(
422 new extensions::ExtensionActionStorageManager(profile_));
[email protected]9ce11d22012-08-08 23:20:13423#endif
[email protected]c77f2352012-08-08 22:07:58424
[email protected]1ff4d282012-12-08 00:39:21425 // Before loading any extensions, determine whether Sideload Wipeout is on.
426 wipeout_is_active_ = FeatureSwitch::sideload_wipeout()->IsEnabled() &&
427 !extension_prefs_->GetSideloadWipeoutDone();
428
[email protected]cb0e50312011-05-09 15:03:07429 // How long is the path to the Extensions directory?
430 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
431 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25432}
433
[email protected]84df8332011-12-06 18:22:46434const ExtensionSet* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45435 return &extensions_;
436}
437
[email protected]84df8332011-12-06 18:22:46438const ExtensionSet* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45439 return &disabled_extensions_;
440}
441
[email protected]84df8332011-12-06 18:22:46442const ExtensionSet* ExtensionService::terminated_extensions() const {
[email protected]bb7f40952011-01-13 00:21:20443 return &terminated_extensions_;
444}
445
[email protected]695b5712012-12-06 23:55:28446const ExtensionSet* ExtensionService::blacklisted_extensions() const {
447 return &blacklisted_extensions_;
448}
449
450scoped_ptr<const ExtensionSet>
451 ExtensionService::GenerateInstalledExtensionsSet() const {
452 scoped_ptr<ExtensionSet> installed_extensions(new ExtensionSet());
[email protected]7f4308d2012-01-18 07:43:01453 installed_extensions->InsertAll(extensions_);
454 installed_extensions->InsertAll(disabled_extensions_);
455 installed_extensions->InsertAll(terminated_extensions_);
[email protected]695b5712012-12-06 23:55:28456 installed_extensions->InsertAll(blacklisted_extensions_);
457 return installed_extensions.PassAs<const ExtensionSet>();
[email protected]7f4308d2012-01-18 07:43:01458}
459
[email protected]695b5712012-12-06 23:55:28460scoped_ptr<const ExtensionSet> ExtensionService::GetWipedOutExtensions() const {
461 scoped_ptr<ExtensionSet> extension_set(new ExtensionSet());
[email protected]215a7be2012-10-22 19:53:42462 for (ExtensionSet::const_iterator iter = disabled_extensions_.begin();
463 iter != disabled_extensions_.end(); ++iter) {
464 int disabled_reason = extension_prefs_->GetDisableReasons((*iter)->id());
465 if ((disabled_reason & Extension::DISABLE_SIDELOAD_WIPEOUT) != 0)
466 extension_set->Insert(*iter);
467 }
[email protected]695b5712012-12-06 23:55:28468 return extension_set.PassAs<const ExtensionSet>();
[email protected]215a7be2012-10-22 19:53:42469}
470
[email protected]3f213ad2012-07-26 23:39:41471extensions::PendingExtensionManager*
472 ExtensionService::pending_extension_manager() {
[email protected]b2907fd2011-03-25 16:43:37473 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45474}
475
[email protected]eaa7dd182010-12-14 11:09:00476ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17477 // No need to unload extensions here because they are profile-scoped, and the
478 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17479
[email protected]5df038b2012-07-16 19:03:27480 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:14481 for (i = external_extension_providers_.begin();
482 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:27483 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:14484 provider->ServiceShutdown();
485 }
[email protected]6014d672008-12-05 00:38:25486}
487
[email protected]d91268022011-08-26 13:17:37488void ExtensionService::InitEventRoutersAfterImport() {
[email protected]cf593af2011-12-30 05:44:39489 RegisterForImportFinished();
490}
491
492void ExtensionService::RegisterForImportFinished() {
493 if (!registrar_.IsRegistered(this, chrome::NOTIFICATION_IMPORT_FINISHED,
494 content::Source<Profile>(profile_))) {
495 registrar_.Add(this, chrome::NOTIFICATION_IMPORT_FINISHED,
496 content::Source<Profile>(profile_));
497 }
498}
499
500void ExtensionService::InitAfterImport() {
[email protected]21f2e2d2013-01-23 22:59:52501 startup_metric_utils::ScopedSlowStartupUMA
502 scoped_timer("Startup.SlowStartupExtensionServiceInitAfterImport");
[email protected]eac88332012-12-26 17:57:45503 component_loader_->LoadAll();
[email protected]ae4c37e2012-12-21 01:16:25504
[email protected]cf593af2011-12-30 05:44:39505 CheckForExternalUpdates();
506
507 GarbageCollectExtensions();
508
509 // Idempotent, so although there is a possible race if the import
510 // process finished sometime in the middle of ProfileImpl::InitExtensions,
511 // it cannot happen twice.
512 InitEventRouters();
[email protected]d91268022011-08-26 13:17:37513}
514
[email protected]eaa7dd182010-12-14 11:09:00515void ExtensionService::InitEventRouters() {
[email protected]b3d62312b12010-10-14 21:10:18516 if (event_routers_initialized_)
517 return;
518
[email protected]6a3cd37e2012-04-17 17:13:34519#if defined(ENABLE_EXTENSIONS)
[email protected]5a38dfd2012-07-23 23:22:10520 browser_event_router_.reset(new extensions::BrowserEventRouter(profile_));
[email protected]6a3cd37e2012-04-17 17:13:34521#endif // defined(ENABLE_EXTENSIONS)
[email protected]b3d62312b12010-10-14 21:10:18522 event_routers_initialized_ = true;
[email protected]c5ae74ab2010-04-15 18:14:37523}
524
[email protected]037228a2012-10-05 01:36:16525void ExtensionService::Shutdown() {
526 // Do nothing for now.
527}
528
[email protected]b2907fd2011-03-25 16:43:37529const Extension* ExtensionService::GetExtensionById(
530 const std::string& id, bool include_disabled) const {
[email protected]81f0d162012-08-15 04:21:47531 int include_mask = INCLUDE_ENABLED;
[email protected]695b5712012-12-06 23:55:28532 if (include_disabled) {
533 // Include blacklisted extensions here because there are hundreds of
534 // callers of this function, and many might assume that this includes those
535 // that have been disabled due to blacklisting.
536 include_mask |= INCLUDE_DISABLED | INCLUDE_BLACKLISTED;
537 }
538 return GetExtensionById(id, include_mask);
539}
540
[email protected]399583b2012-12-11 09:33:42541GURL ExtensionService::GetSiteForExtensionId(const std::string& extension_id) {
542 return content::SiteInstance::GetSiteForURL(
543 profile_,
544 Extension::GetBaseURLFromExtensionId(extension_id));
545}
546
[email protected]695b5712012-12-06 23:55:28547const Extension* ExtensionService::GetExtensionById(
548 const std::string& id, int include_mask) const {
549 std::string lowercase_id = StringToLowerASCII(id);
550 if (include_mask & INCLUDE_ENABLED) {
551 const Extension* extension = extensions_.GetByID(lowercase_id);
552 if (extension)
553 return extension;
554 }
555 if (include_mask & INCLUDE_DISABLED) {
556 const Extension* extension = disabled_extensions_.GetByID(lowercase_id);
557 if (extension)
558 return extension;
559 }
560 if (include_mask & INCLUDE_TERMINATED) {
561 const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
562 if (extension)
563 return extension;
564 }
565 if (include_mask & INCLUDE_BLACKLISTED) {
566 const Extension* extension = blacklisted_extensions_.GetByID(lowercase_id);
567 if (extension)
568 return extension;
569 }
570 return NULL;
[email protected]78994ab02010-12-08 18:06:44571}
572
[email protected]eaa7dd182010-12-14 11:09:00573void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12574 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20575
[email protected]fa6a9102010-11-22 15:38:50576 DCHECK(!ready_); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17577 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32578
[email protected]820d9bd2013-04-03 03:46:03579 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
[email protected]ae4c37e2012-12-21 01:16:25580
[email protected]820d9bd2013-04-03 03:46:03581 CHECK(!ProfileManager::IsImportProcess(*cmd_line));
[email protected]ae4c37e2012-12-21 01:16:25582
[email protected]820d9bd2013-04-03 03:46:03583 if (cmd_line->HasSwitch(switches::kInstallFromWebstore) ||
584 cmd_line->HasSwitch(switches::kLimitedInstallFromWebstore)) {
585 // The sole purpose of this launch is to install a new extension from CWS
586 // and immediately terminate: loading already installed extensions is
587 // unnecessary and may interfere with the inline install dialog (e.g. if an
588 // extension listens to onStartup and opens a window).
589 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25590 } else {
[email protected]820d9bd2013-04-03 03:46:03591 // TODO(mek): It might be cleaner to do the FinishDelayedInstallInfo stuff
592 // here instead of in installedloader.
593 if (g_browser_process->profile_manager() &&
594 g_browser_process->profile_manager()->will_import()) {
595 // Do not load any component extensions, since they may conflict with the
596 // import process.
[email protected]ae4c37e2012-12-21 01:16:25597
[email protected]820d9bd2013-04-03 03:46:03598 extensions::InstalledLoader(this).LoadAllExtensions();
599 RegisterForImportFinished();
600 } else {
601 // In this case, LoadAllExtensions() calls OnLoadedInstalledExtensions(),
602 // which calls SetReadyAndNotifyListeners().
603 component_loader_->LoadAll();
604 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]ae4c37e2012-12-21 01:16:25605
[email protected]820d9bd2013-04-03 03:46:03606 // TODO(erikkay) this should probably be deferred to a future point
607 // rather than running immediately at startup.
608 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57609
[email protected]820d9bd2013-04-03 03:46:03610 // TODO(erikkay) this should probably be deferred as well.
611 GarbageCollectExtensions();
612 }
[email protected]215a7be2012-10-22 19:53:42613
[email protected]820d9bd2013-04-03 03:46:03614 if (extension_prefs_->NeedsStorageGarbageCollection()) {
615 GarbageCollectIsolatedStorage();
616 extension_prefs_->SetNeedsStorageGarbageCollection(false);
617 }
[email protected]399583b2012-12-11 09:33:42618 }
[email protected]6014d672008-12-05 00:38:25619}
620
[email protected]31bb5ee62012-09-12 22:58:40621bool ExtensionService::UpdateExtension(const std::string& id,
[email protected]650b2d52013-02-10 03:41:45622 const base::FilePath& extension_path,
[email protected]31bb5ee62012-09-12 22:58:40623 const GURL& download_url,
624 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12625 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3c4abc82012-10-22 22:25:54626 if (browser_terminating_) {
627 LOG(WARNING) << "Skipping UpdateExtension due to browser shutdown";
628 // Leak the temp file at extension_path. We don't want to add to the disk
629 // I/O burden at shutdown, we can't rely on the I/O completing anyway, and
630 // the file is in the OS temp directory which should be cleaned up for us.
631 return false;
632 }
[email protected]a8af9fdb2010-10-28 21:52:20633
[email protected]3f213ad2012-07-26 23:39:41634 const extensions::PendingExtensionInfo* pending_extension_info =
[email protected]51a3bf8b2012-06-08 22:53:06635 pending_extension_manager()->GetById(id);
[email protected]8ef78fd2010-08-19 17:14:32636
[email protected]695b5712012-12-06 23:55:28637 const Extension* extension = GetInstalledExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06638 if (!pending_extension_info && !extension) {
[email protected]aa142702010-03-26 01:26:33639 LOG(WARNING) << "Will not update extension " << id
640 << " because it is not installed or pending";
641 // Delete extension_path since we're not creating a CrxInstaller
642 // that would do it for us.
[email protected]7f8f24f2012-11-15 19:40:14643 if (!GetFileTaskRunner()->PostTask(
644 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36645 base::Bind(
[email protected]7296f2762011-11-21 19:23:44646 &extension_file_util::DeleteFile, extension_path, false)))
[email protected]14908b72011-04-20 06:54:36647 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03648
649 return false;
[email protected]e957fe52009-06-23 16:51:05650 }
651
[email protected]aa142702010-03-26 01:26:33652 // We want a silent install only for non-pending extensions and
653 // pending extensions that have install_silently set.
[email protected]91e51d612012-10-21 23:03:05654 ExtensionInstallPrompt* client = NULL;
655 if (pending_extension_info && !pending_extension_info->install_silently())
656 client = ExtensionInstallUI::CreateInstallPromptWithProfile(profile_);
[email protected]aa142702010-03-26 01:26:33657
[email protected]d8c8f25f2011-11-02 18:18:01658 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(this, client));
[email protected]6dfbbf82010-03-12 23:09:16659 installer->set_expected_id(id);
[email protected]51a3bf8b2012-06-08 22:53:06660 if (pending_extension_info) {
661 installer->set_install_source(pending_extension_info->install_source());
662 if (pending_extension_info->install_silently())
663 installer->set_allow_silent_install(true);
664 } else if (extension) {
[email protected]5eb375e92010-11-26 07:50:41665 installer->set_install_source(extension->location());
[email protected]51a3bf8b2012-06-08 22:53:06666 }
[email protected]7d8b7072012-04-07 01:07:46667 // If the extension was installed from or has migrated to the webstore, or
[email protected]fcb2c2c2012-10-17 21:08:45668 // its auto-update URL is from the webstore, treat it as a webstore install.
669 // Note that we ignore some older extensions with blank auto-update URLs
670 // because we are mostly concerned with restrictions on NaCl extensions,
671 // which are newer.
[email protected]a12ce8b22012-01-17 18:40:53672 int creation_flags = Extension::NO_FLAGS;
[email protected]75764512011-12-19 19:54:28673 if ((extension && extension->from_webstore()) ||
[email protected]7d8b7072012-04-07 01:07:46674 (extension && extension->UpdatesFromGallery()) ||
[email protected]963d13c2012-09-29 17:13:35675 (!extension && extension_urls::IsWebstoreUpdateUrl(
[email protected]51a3bf8b2012-06-08 22:53:06676 pending_extension_info->update_url()))) {
[email protected]a12ce8b22012-01-17 18:40:53677 creation_flags |= Extension::FROM_WEBSTORE;
678 }
679
680 // Bookmark apps being updated is kind of a contradiction, but that's because
681 // we mark the default apps as bookmark apps, and they're hosted in the web
682 // store, thus they can get updated. See http://crbug.com/101605 for more
683 // details.
684 if (extension && extension->from_bookmark())
685 creation_flags |= Extension::FROM_BOOKMARK;
686
[email protected]e33bbc22012-08-27 22:05:46687 if (extension && extension->was_installed_by_default())
688 creation_flags |= Extension::WAS_INSTALLED_BY_DEFAULT;
689
[email protected]a12ce8b22012-01-17 18:40:53690 installer->set_creation_flags(creation_flags);
691
[email protected]6dfbbf82010-03-12 23:09:16692 installer->set_delete_source(true);
[email protected]655b2b1a2011-10-13 17:13:06693 installer->set_download_url(download_url);
[email protected]cb0e50312011-05-09 15:03:07694 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16695 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03696
697 if (out_crx_installer)
698 *out_crx_installer = installer;
699
700 return true;
[email protected]e957fe52009-06-23 16:51:05701}
702
[email protected]eaa7dd182010-12-14 11:09:00703void ExtensionService::ReloadExtension(const std::string& extension_id) {
[email protected]32b36c62012-10-24 05:37:41704 int events = HasShellWindows(extension_id) ? EVENT_LAUNCHED : EVENT_NONE;
705 ReloadExtensionWithEvents(extension_id, events);
706}
707
708void ExtensionService::RestartExtension(const std::string& extension_id) {
[email protected]2380c6b2013-01-09 02:33:13709 ReloadExtensionWithEvents(extension_id, EVENT_RESTARTED);
[email protected]32b36c62012-10-24 05:37:41710}
711
712void ExtensionService::ReloadExtensionWithEvents(
713 const std::string& extension_id,
714 int events) {
[email protected]a29a517a2011-01-21 21:11:12715 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]2a947202013-03-06 04:58:05716
717 // If the extension is already reloading, don't reload again.
718 if (extension_prefs_->GetDisableReasons(extension_id) &
719 Extension::DISABLE_RELOAD) {
720 return;
721 }
722
[email protected]650b2d52013-02-10 03:41:45723 base::FilePath path;
[email protected]9adb9692010-10-29 23:14:02724 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40725
[email protected]f17dbd42010-08-16 23:21:10726 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06727 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29728 // If the extension has an inspector open for its background page, detach
729 // the inspector and hang onto a cookie for it, so that we can reattach
730 // later.
[email protected]31d8f5f22012-04-02 15:22:08731 // TODO(yoz): this is not incognito-safe!
732 ExtensionProcessManager* manager = system_->process_manager();
[email protected]3a1dc572012-07-31 22:25:13733 extensions::ExtensionHost* host =
734 manager->GetBackgroundHostForExtension(extension_id);
[email protected]98f66112012-12-25 12:59:36735 if (host && DevToolsAgentHost::HasFor(host->render_view_host())) {
[email protected]4814b512009-11-07 00:12:29736 // Look for an open inspector for the background page.
[email protected]ead056352013-03-12 08:47:54737 std::string devtools_cookie = DevToolsAgentHost::DisconnectRenderViewHost(
[email protected]562e20c2012-12-24 18:27:20738 host->render_view_host());
[email protected]ead056352013-03-12 08:47:54739 if (devtools_cookie != std::string())
[email protected]4814b512009-11-07 00:12:29740 orphaned_dev_tools_[extension_id] = devtools_cookie;
741 }
742
[email protected]b65272f2009-08-31 15:47:06743 path = current_extension->path();
[email protected]44d62b62012-04-11 00:06:03744 DisableExtension(extension_id, Extension::DISABLE_RELOAD);
[email protected]f17dbd42010-08-16 23:21:10745 disabled_extension_paths_[extension_id] = path;
[email protected]1eb175082010-02-10 09:26:16746 } else {
747 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06748 }
749
[email protected]8b6d9642013-02-01 03:37:40750 on_load_events_[extension_id] = events;
[email protected]fc2a40f2013-03-13 13:14:57751 if (events & EVENT_RESTARTED) {
752 extension_prefs_->GetSavedFileEntries(
753 extension_id, &on_restart_file_entries_[extension_id]);
754 }
755
[email protected]8b6d9642013-02-01 03:37:40756
[email protected]399583b2012-12-11 09:33:42757 if (delayed_updates_for_idle_.Contains(extension_id)) {
[email protected]6f6101832012-11-27 22:10:48758 FinishDelayedInstallation(extension_id);
[email protected]0db124b02012-11-07 04:55:05759 return;
760 }
761
[email protected]43ceb002012-02-10 23:19:15762 // If we're reloading a component extension, use the component extension
763 // loader's reloader.
764 if (component_loader_->Exists(extension_id)) {
765 component_loader_->Reload(extension_id);
766 return;
767 }
768
[email protected]e6090e42010-03-23 22:44:08769 // Check the installed extensions to see if what we're reloading was already
770 // installed.
771 scoped_ptr<ExtensionInfo> installed_extension(
772 extension_prefs_->GetInstalledExtensionInfo(extension_id));
773 if (installed_extension.get() &&
774 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01775 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08776 } else {
[email protected]d8c8f25f2011-11-02 18:18:01777 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08778 // We should always be able to remember the extension's path. If it's not in
779 // the map, someone failed to update |unloaded_extension_paths_|.
780 CHECK(!path.empty());
[email protected]d8c8f25f2011-11-02 18:18:01781 extensions::UnpackedInstaller::Create(this)->Load(path);
[email protected]e6090e42010-03-23 22:44:08782 }
[email protected]9cddd4702009-07-27 22:09:40783}
784
[email protected]fa2416f2011-05-03 08:41:20785bool ExtensionService::UninstallExtension(
[email protected]a12c706e2011-12-01 00:58:08786 std::string extension_id,
[email protected]fa2416f2011-05-03 08:41:20787 bool external_uninstall,
[email protected]65187152012-06-02 13:14:14788 string16* error) {
[email protected]a29a517a2011-01-21 21:11:12789 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20790
[email protected]0d6ec3a72011-09-02 02:09:43791 scoped_refptr<const Extension> extension(GetInstalledExtension(extension_id));
[email protected]631cf822009-05-15 07:01:25792
[email protected]e7afe2452010-08-22 16:19:13793 // Callers should not send us nonexistent extensions.
[email protected]aebe23a32010-12-10 22:15:48794 CHECK(extension);
[email protected]9f1087e2009-06-15 17:29:32795
[email protected]95da88c42011-03-31 10:07:33796 // Policy change which triggers an uninstall will always set
797 // |external_uninstall| to true so this is the only way to uninstall
798 // managed extensions.
[email protected]65187152012-06-02 13:14:14799 if (!external_uninstall &&
800 !system_->management_policy()->UserMayModifySettings(
801 extension.get(), error)) {
[email protected]ad50def52011-10-19 23:17:07802 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27803 chrome::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53804 content::Source<Profile>(profile_),
805 content::Details<const Extension>(extension));
[email protected]d6ebc9792011-04-07 18:18:33806 return false;
807 }
[email protected]95da88c42011-03-31 10:07:33808
[email protected]3bdba0d2011-08-23 07:17:30809 // Extract the data we need for sync now, but don't actually sync until we've
810 // completed the uninstallation.
[email protected]65f173552012-06-28 22:43:58811 syncer::SyncChange sync_change;
[email protected]5db9ada2012-04-11 13:48:20812 if (app_sync_bundle_.HandlesApp(*extension)) {
813 sync_change = app_sync_bundle_.CreateSyncChangeToDelete(extension);
814 } else if (extension_sync_bundle_.HandlesExtension(*extension)) {
815 sync_change = extension_sync_bundle_.CreateSyncChangeToDelete(extension);
[email protected]3bdba0d2011-08-23 07:17:30816 }
817
[email protected]d96eb512012-11-01 23:44:08818 if (IsUnacknowledgedExternalExtension(extension)) {
819 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
820 EXTERNAL_EXTENSION_UNINSTALLED,
821 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
822 }
[email protected]9b217652010-10-08 22:04:23823 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08824 extension->GetType(), 100);
[email protected]fe2dd7742011-04-19 22:52:49825 RecordPermissionMessagesHistogram(
826 extension, "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23827
[email protected]831aa212010-03-26 13:55:19828 // Unload before doing more cleanup to ensure that nothing is hanging on to
829 // any of these resources.
[email protected]814a7bf0f2011-08-13 05:30:59830 UnloadExtension(extension_id, extension_misc::UNLOAD_REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19831
[email protected]0d6ec3a72011-09-02 02:09:43832 extension_prefs_->OnExtensionUninstalled(extension_id, extension->location(),
[email protected]831aa212010-03-26 13:55:19833 external_uninstall);
[email protected]9f1087e2009-06-15 17:29:32834
835 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]12075d12013-02-27 05:38:05836 if (!Manifest::IsUnpackedLocation(extension->location())) {
[email protected]7f8f24f2012-11-15 19:40:14837 if (!GetFileTaskRunner()->PostTask(
838 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36839 base::Bind(
[email protected]14908b72011-04-20 06:54:36840 &extension_file_util::UninstallExtension,
841 install_directory_,
[email protected]fa2416f2011-05-03 08:41:20842 extension_id)))
[email protected]14908b72011-04-20 06:54:36843 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32844 }
845
[email protected]0d6ec3a72011-09-02 02:09:43846 GURL launch_web_url_origin(extension->launch_web_url());
847 launch_web_url_origin = launch_web_url_origin.GetOrigin();
[email protected]c86a8262013-03-31 07:58:57848 bool is_storage_isolated = extension->is_storage_isolated();
[email protected]0d6ec3a72011-09-02 02:09:43849
[email protected]399583b2012-12-11 09:33:42850 if (is_storage_isolated) {
851 BrowserContext::AsyncObliterateStoragePartition(
852 profile_,
853 GetSiteForExtensionId(extension_id),
854 base::Bind(&ExtensionService::OnNeedsToGarbageCollectIsolatedStorage,
855 AsWeakPtr()));
856 } else {
857 if (extension->is_hosted_app() &&
858 !profile_->GetExtensionSpecialStoragePolicy()->
859 IsStorageProtected(launch_web_url_origin)) {
860 extensions::DataDeleter::StartDeleting(
861 profile_, extension_id, launch_web_url_origin);
862 }
863 extensions::DataDeleter::StartDeleting(profile_, extension_id,
864 extension->url());
[email protected]0d6ec3a72011-09-02 02:09:43865 }
[email protected]0d6ec3a72011-09-02 02:09:43866
[email protected]0dfe05c2011-02-23 23:03:36867 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06868
869 // Notify interested parties that we've uninstalled this extension.
[email protected]ad50def52011-10-19 23:17:07870 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27871 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]6c2381d2011-10-19 02:52:53872 content::Source<Profile>(profile_),
[email protected]d67d9412012-07-17 17:19:42873 content::Details<const Extension>(extension));
[email protected]d6ebc9792011-04-07 18:18:33874
[email protected]5db9ada2012-04-11 13:48:20875 if (app_sync_bundle_.HasExtensionId(extension_id) &&
[email protected]a4a147652012-07-03 23:41:32876 sync_change.sync_data().GetDataType() == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:20877 app_sync_bundle_.ProcessDeletion(extension_id, sync_change);
878 } else if (extension_sync_bundle_.HasExtensionId(extension_id) &&
[email protected]a4a147652012-07-03 23:41:32879 sync_change.sync_data().GetDataType() == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:20880 extension_sync_bundle_.ProcessDeletion(extension_id, sync_change);
[email protected]3bdba0d2011-08-23 07:17:30881 }
882
[email protected]399583b2012-12-11 09:33:42883 delayed_updates_for_idle_.Remove(extension_id);
884 delayed_installs_.Remove(extension_id);
[email protected]0db124b02012-11-07 04:55:05885
[email protected]333b1de2011-09-12 18:28:50886 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18887 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
888
[email protected]d6ebc9792011-04-07 18:18:33889 return true;
[email protected]c10da4b02010-03-25 14:38:32890}
891
[email protected]c3cfb012011-04-06 22:07:35892bool ExtensionService::IsExtensionEnabled(
893 const std::string& extension_id) const {
[email protected]84df8332011-12-06 18:22:46894 if (extensions_.Contains(extension_id) ||
[email protected]dc9a74f72012-08-17 18:07:21895 terminated_extensions_.Contains(extension_id)) {
[email protected]36429da2011-07-11 20:25:18896 return true;
[email protected]dc9a74f72012-08-17 18:07:21897 }
[email protected]36429da2011-07-11 20:25:18898
[email protected]695b5712012-12-06 23:55:28899 if (disabled_extensions_.Contains(extension_id) ||
900 blacklisted_extensions_.Contains(extension_id)) {
[email protected]ad83ca242011-07-29 01:32:25901 return false;
[email protected]695b5712012-12-06 23:55:28902 }
[email protected]ad83ca242011-07-29 01:32:25903
904 // If the extension hasn't been loaded yet, check the prefs for it. Assume
905 // enabled unless otherwise noted.
906 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
[email protected]f574c402012-12-04 23:20:31907 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35908}
909
910bool ExtensionService::IsExternalExtensionUninstalled(
911 const std::string& extension_id) const {
912 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
913}
914
[email protected]e2377032013-01-18 17:53:22915bool ExtensionService::IsExtensionEnabledForLauncher(
916 const std::string& extension_id) const {
917 return IsExtensionEnabled(extension_id) &&
918 !GetTerminatedExtension(extension_id);
919}
920
[email protected]eaa7dd182010-12-14 11:09:00921void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12922 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20923
[email protected]06f92562011-04-29 19:27:31924 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39925 return;
[email protected]0c6da502009-08-14 22:32:39926
[email protected]c0231d72012-11-26 22:37:57927 int disable_reasons = extension_prefs_->GetDisableReasons(extension_id);
[email protected]b3317ad2011-04-28 23:46:00928 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]eb5e4f92012-08-15 23:33:28929 extension_prefs_->ClearDisableReasons(extension_id);
[email protected]1784e83a2009-09-08 21:01:52930
[email protected]695b5712012-12-06 23:55:28931 const Extension* extension = disabled_extensions_.GetByID(extension_id);
[email protected]06f92562011-04-29 19:27:31932 // This can happen if sync enables an extension that is not
933 // installed yet.
934 if (!extension)
935 return;
936
[email protected]d96eb512012-11-01 23:44:08937 if (IsUnacknowledgedExternalExtension(extension)) {
938 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
939 EXTERNAL_EXTENSION_REENABLED,
940 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]612a1cb12012-10-17 13:18:03941 AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:08942 }
[email protected]612a1cb12012-10-17 13:18:03943
[email protected]c0231d72012-11-26 22:37:57944 if (disable_reasons & Extension::DISABLE_SIDELOAD_WIPEOUT)
[email protected]94e14592012-11-19 10:49:42945 UMA_HISTOGRAM_BOOLEAN("DisabledExtension.ExtensionWipedStatus", false);
946
[email protected]0c6da502009-08-14 22:32:39947 // Move it over to the enabled list.
[email protected]84df8332011-12-06 18:22:46948 extensions_.Insert(make_scoped_refptr(extension));
949 disabled_extensions_.Remove(extension->id());
[email protected]0c6da502009-08-14 22:32:39950
[email protected]62d30f42009-10-01 22:36:06951 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30952
[email protected]f74d7f32012-06-19 19:22:51953 // Notify listeners that the extension was enabled.
954 content::NotificationService::current()->Notify(
955 chrome::NOTIFICATION_EXTENSION_ENABLED,
956 content::Source<Profile>(profile_),
957 content::Details<const Extension>(extension));
958
[email protected]3bdba0d2011-08-23 07:17:30959 SyncExtensionChangeIfNeeded(*extension);
[email protected]0c6da502009-08-14 22:32:39960}
961
[email protected]44d62b62012-04-11 00:06:03962void ExtensionService::DisableExtension(
963 const std::string& extension_id,
964 Extension::DisableReason disable_reason) {
[email protected]a29a517a2011-01-21 21:11:12965 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20966
[email protected]b2ba9962009-12-10 20:10:15967 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:31968 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:52969 return;
[email protected]1784e83a2009-09-08 21:01:52970
[email protected]06f92562011-04-29 19:27:31971 const Extension* extension = GetInstalledExtension(extension_id);
972 // |extension| can be NULL if sync disables an extension that is not
973 // installed yet.
[email protected]65187152012-06-02 13:14:14974 if (extension &&
975 !system_->management_policy()->UserMayModifySettings(extension, NULL)) {
[email protected]95da88c42011-03-31 10:07:33976 return;
[email protected]65187152012-06-02 13:14:14977 }
[email protected]95da88c42011-03-31 10:07:33978
[email protected]b3317ad2011-04-28 23:46:00979 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]eb5e4f92012-08-15 23:33:28980 extension_prefs_->AddDisableReason(extension_id, disable_reason);
[email protected]1784e83a2009-09-08 21:01:52981
[email protected]695b5712012-12-06 23:55:28982 int include_mask = INCLUDE_EVERYTHING & ~INCLUDE_DISABLED;
983 extension = GetExtensionById(extension_id, include_mask);
[email protected]06f92562011-04-29 19:27:31984 if (!extension)
985 return;
986
[email protected]5c094792012-09-07 19:44:53987 // Move it over to the disabled list. Don't send a second unload notification
988 // for terminated extensions being disabled.
[email protected]84df8332011-12-06 18:22:46989 disabled_extensions_.Insert(make_scoped_refptr(extension));
[email protected]5c094792012-09-07 19:44:53990 if (extensions_.Contains(extension->id())) {
[email protected]84df8332011-12-06 18:22:46991 extensions_.Remove(extension->id());
[email protected]5c094792012-09-07 19:44:53992 NotifyExtensionUnloaded(extension, extension_misc::UNLOAD_REASON_DISABLE);
993 } else {
[email protected]84df8332011-12-06 18:22:46994 terminated_extensions_.Remove(extension->id());
[email protected]5c094792012-09-07 19:44:53995 }
[email protected]3bdba0d2011-08-23 07:17:30996
997 SyncExtensionChangeIfNeeded(*extension);
[email protected]1784e83a2009-09-08 21:01:52998}
999
[email protected]eaa7dd182010-12-14 11:09:001000void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]009633c2013-03-07 22:08:281001 const Extension* extension) {
1002 GrantPermissions(extension);
[email protected]fe2dd7742011-04-19 22:52:491003 RecordPermissionMessagesHistogram(
1004 extension, "Extensions.Permissions_ReEnable");
[email protected]8d888c12010-11-30 00:00:251005 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
1006 EnableExtension(extension->id());
1007}
1008
[email protected]009633c2013-03-07 22:08:281009void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]be083862012-09-01 03:53:451010 CHECK(extension);
1011 extensions::PermissionsUpdater perms_updater(profile());
[email protected]009633c2013-03-07 22:08:281012 perms_updater.GrantActivePermissions(extension);
[email protected]be083862012-09-01 03:53:451013}
1014
[email protected]fe2dd7742011-04-19 22:52:491015// static
1016void ExtensionService::RecordPermissionMessagesHistogram(
1017 const Extension* e, const char* histogram) {
[email protected]de415552013-01-23 04:12:171018 // Since this is called from multiple sources, and since the histogram macros
1019 // use statics, we need to manually lookup the histogram ourselves.
1020 base::HistogramBase* counter = base::LinearHistogram::FactoryGet(
[email protected]fe2dd7742011-04-19 22:52:491021 histogram,
1022 1,
[email protected]c2e66e12012-06-27 06:27:061023 PermissionMessage::kEnumBoundary,
1024 PermissionMessage::kEnumBoundary + 1,
[email protected]de415552013-01-23 04:12:171025 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]fe2dd7742011-04-19 22:52:491026
[email protected]c2e66e12012-06-27 06:27:061027 PermissionMessages permissions = e->GetPermissionMessages();
[email protected]fe2dd7742011-04-19 22:52:491028 if (permissions.empty()) {
[email protected]c2e66e12012-06-27 06:27:061029 counter->Add(PermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:491030 } else {
[email protected]c2e66e12012-06-27 06:27:061031 for (PermissionMessages::iterator it = permissions.begin();
[email protected]0d3e4a22011-06-23 19:02:521032 it != permissions.end(); ++it)
1033 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:491034 }
1035}
1036
[email protected]eaa7dd182010-12-14 11:09:001037void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:421038 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061039 // was loaded, otherwise a race can arise where a renderer that is created
1040 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421041 // that the request context doesn't yet know about. The profile is responsible
1042 // for ensuring its URLRequestContexts appropriately discover the loaded
1043 // extension.
[email protected]31d8f5f22012-04-02 15:22:081044 system_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:061045
[email protected]d5949312012-12-03 22:13:301046 // Tell renderers about the new extension, unless it's a theme (renderers
1047 // don't need to know about themes).
[email protected]a58f599c2012-12-01 03:08:191048 if (!extension->is_theme()) {
[email protected]19647262011-12-16 09:57:491049 for (content::RenderProcessHost::iterator i(
1050 content::RenderProcessHost::AllHostsIterator());
1051 !i.IsAtEnd(); i.Advance()) {
1052 content::RenderProcessHost* host = i.GetCurrentValue();
1053 Profile* host_profile =
1054 Profile::FromBrowserContext(host->GetBrowserContext());
1055 if (host_profile->GetOriginalProfile() ==
1056 profile_->GetOriginalProfile()) {
1057 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
1058 1, ExtensionMsg_Loaded_Params(extension));
1059 host->Send(
1060 new ExtensionMsg_Loaded(loaded_extensions));
1061 }
[email protected]c8d407e2011-04-28 21:27:171062 }
[email protected]77a6970c2011-04-23 16:58:561063 }
1064
[email protected]3442a662012-01-12 08:06:171065 // Tell subsystems that use the EXTENSION_LOADED notification about the new
1066 // extension.
1067 //
1068 // NOTE: It is important that this happen after notifying the renderers about
1069 // the new extensions so that if we navigate to an extension URL in
1070 // NOTIFICATION_EXTENSION_LOADED, the renderer is guaranteed to know about it.
1071 content::NotificationService::current()->Notify(
1072 chrome::NOTIFICATION_EXTENSION_LOADED,
1073 content::Source<Profile>(profile_),
1074 content::Details<const Extension>(extension));
1075
[email protected]c8d407e2011-04-28 21:27:171076 // Tell a random-ass collection of other subsystems about the new extension.
1077 // TODO(aa): What should we do with all this goop? Can it move into the
1078 // relevant objects via EXTENSION_LOADED?
1079
1080 profile_->GetExtensionSpecialStoragePolicy()->
1081 GrantRightsForExtension(extension);
1082
1083 UpdateActiveExtensionsInCrashReporter();
1084
[email protected]c8d407e2011-04-28 21:27:171085 // If the extension has permission to load chrome://favicon/ resources we need
1086 // to make sure that the FaviconSource is registered with the
1087 // ChromeURLDataManager.
1088 if (extension->HasHostPermission(GURL(chrome::kChromeUIFaviconURL))) {
1089 FaviconSource* favicon_source = new FaviconSource(profile_,
1090 FaviconSource::FAVICON);
[email protected]24ea7a12013-01-27 23:54:531091 content::URLDataSource::Add(profile_, favicon_source);
[email protected]c8d407e2011-04-28 21:27:171092 }
[email protected]b07e606e2012-09-15 20:16:151093
1094#if !defined(OS_ANDROID)
1095 // Same for chrome://theme/ resources.
1096 if (extension->HasHostPermission(GURL(chrome::kChromeUIThemeURL))) {
1097 ThemeSource* theme_source = new ThemeSource(profile_);
[email protected]24ea7a12013-01-27 23:54:531098 content::URLDataSource::Add(profile_, theme_source);
[email protected]b07e606e2012-09-15 20:16:151099 }
1100#endif
1101
[email protected]5eddc3e2011-10-26 04:33:311102 // Same for chrome://thumb/ resources.
1103 if (extension->HasHostPermission(GURL(chrome::kChromeUIThumbnailURL))) {
1104 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_);
[email protected]24ea7a12013-01-27 23:54:531105 content::URLDataSource::Add(profile_, thumbnail_source);
[email protected]5eddc3e2011-10-26 04:33:311106 }
[email protected]c8d407e2011-04-28 21:27:171107
[email protected]ebd71962012-12-20 02:56:551108#if defined(ENABLE_PLUGINS)
[email protected]c8d407e2011-04-28 21:27:171109 // TODO(mpcomplete): This ends up affecting all profiles. See crbug.com/80757.
[email protected]2de307592011-04-05 21:16:581110 bool plugins_changed = false;
[email protected]098fa7a2013-03-08 22:11:171111 if (extensions::PluginInfo::HasPlugins(extension)) {
1112 const extensions::PluginInfo::PluginVector* plugins =
1113 extensions::PluginInfo::GetPlugins(extension);
1114 CHECK(plugins);
[email protected]2de307592011-04-05 21:16:581115 plugins_changed = true;
[email protected]098fa7a2013-03-08 22:11:171116 for (extensions::PluginInfo::PluginVector::const_iterator plugin =
1117 plugins->begin();
1118 plugin != plugins->end(); ++plugin) {
1119 PluginService::GetInstance()->RefreshPlugins();
1120 PluginService::GetInstance()->AddExtraPluginPath(plugin->path);
1121 ChromePluginServiceFilter* filter =
1122 ChromePluginServiceFilter::GetInstance();
1123 if (plugin->is_public) {
1124 filter->RestrictPluginToProfileAndOrigin(
1125 plugin->path, profile_, GURL());
1126 } else {
1127 filter->RestrictPluginToProfileAndOrigin(
1128 plugin->path, profile_, extension->url());
1129 }
[email protected]2de307592011-04-05 21:16:581130 }
1131 }
[email protected]84396dbc2011-04-14 06:33:421132
1133 bool nacl_modules_changed = false;
1134 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1135 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001136 RegisterNaClModule(module.url, module.mime_type);
[email protected]84396dbc2011-04-14 06:33:421137 nacl_modules_changed = true;
1138 }
1139
[email protected]ed0ba002011-05-26 16:55:131140 if (nacl_modules_changed)
1141 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421142
1143 if (plugins_changed || nacl_modules_changed)
[email protected]45a22e62011-10-12 09:48:021144 PluginService::GetInstance()->PurgePluginListCache(profile_, false);
[email protected]ebd71962012-12-20 02:56:551145#endif // defined(ENABLE_PLUGINS)
[email protected]62d30f42009-10-01 22:36:061146}
1147
[email protected]a9f39a312010-12-23 22:14:271148void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591149 const Extension* extension,
1150 extension_misc::UnloadedExtensionReason reason) {
[email protected]a9f39a312010-12-23 22:14:271151 UnloadedExtensionInfo details(extension, reason);
[email protected]ad50def52011-10-19 23:17:071152 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271153 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531154 content::Source<Profile>(profile_),
1155 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061156
[email protected]903d70a2012-12-04 13:00:171157#if defined(ENABLE_THEMES)
1158 // If the current theme is being unloaded, tell ThemeService to revert back
1159 // to the default theme.
1160 if (reason != extension_misc::UNLOAD_REASON_UPDATE && extension->is_theme()) {
1161 ThemeService* theme_service = ThemeServiceFactory::GetForProfile(profile_);
1162 if (extension->id() == theme_service->GetThemeID())
1163 theme_service->UseDefaultTheme();
1164 }
1165#endif
1166
[email protected]f3b1a082011-11-18 00:34:301167 for (content::RenderProcessHost::iterator i(
1168 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:561169 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:301170 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081171 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301172 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:081173 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171174 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561175 }
1176
[email protected]31d8f5f22012-04-02 15:22:081177 system_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]c8d407e2011-04-28 21:27:171178 profile_->GetExtensionSpecialStoragePolicy()->
1179 RevokeRightsForExtension(extension);
1180
[email protected]b777b332011-04-16 04:01:081181#if defined(OS_CHROMEOS)
[email protected]ac66e452013-01-11 09:21:231182 // Revoke external file access for the extension from its file system context.
1183 // It is safe to access the extension's storage partition at this point. The
1184 // storage partition may get destroyed only after the extension gets unloaded.
1185 GURL site = extensions::ExtensionSystem::Get(profile_)->extension_service()->
1186 GetSiteForExtensionId(extension->id());
[email protected]10eb28162012-09-18 03:04:091187 fileapi::FileSystemContext* filesystem_context =
[email protected]ac66e452013-01-11 09:21:231188 BrowserContext::GetStoragePartitionForSite(profile_, site)->
[email protected]10eb28162012-09-18 03:04:091189 GetFileSystemContext();
1190 if (filesystem_context && filesystem_context->external_provider()) {
1191 filesystem_context->external_provider()->
[email protected]c8d407e2011-04-28 21:27:171192 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061193 }
[email protected]c8d407e2011-04-28 21:27:171194#endif
1195
1196 UpdateActiveExtensionsInCrashReporter();
[email protected]2de307592011-04-05 21:16:581197
[email protected]ebd71962012-12-20 02:56:551198#if defined(ENABLE_PLUGINS)
[email protected]2de307592011-04-05 21:16:581199 bool plugins_changed = false;
[email protected]098fa7a2013-03-08 22:11:171200 if (extensions::PluginInfo::HasPlugins(extension)) {
1201 const extensions::PluginInfo::PluginVector* plugins =
1202 extensions::PluginInfo::GetPlugins(extension);
[email protected]2de307592011-04-05 21:16:581203 plugins_changed = true;
[email protected]098fa7a2013-03-08 22:11:171204 for (extensions::PluginInfo::PluginVector::const_iterator plugin =
1205 plugins->begin();
1206 plugin != plugins->end(); ++plugin) {
1207 PluginService::GetInstance()->ForcePluginShutdown(plugin->path);
1208 PluginService::GetInstance()->RefreshPlugins();
1209 PluginService::GetInstance()->RemoveExtraPluginPath(plugin->path);
1210 ChromePluginServiceFilter::GetInstance()->UnrestrictPlugin(plugin->path);
1211 }
[email protected]2de307592011-04-05 21:16:581212 }
[email protected]84396dbc2011-04-14 06:33:421213
1214 bool nacl_modules_changed = false;
1215 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1216 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001217 UnregisterNaClModule(module.url);
[email protected]84396dbc2011-04-14 06:33:421218 nacl_modules_changed = true;
1219 }
1220
[email protected]ed0ba002011-05-26 16:55:131221 if (nacl_modules_changed)
1222 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421223
1224 if (plugins_changed || nacl_modules_changed)
[email protected]45a22e62011-10-12 09:48:021225 PluginService::GetInstance()->PurgePluginListCache(profile_, false);
[email protected]ebd71962012-12-20 02:56:551226#endif // defined(ENABLE_PLUGINS)
[email protected]62d30f42009-10-01 22:36:061227}
1228
[email protected]0a071a32011-02-08 00:18:241229Profile* ExtensionService::profile() {
1230 return profile_;
1231}
1232
[email protected]45759612012-07-10 17:21:231233extensions::ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451234 return extension_prefs_;
1235}
1236
[email protected]0d9a2202011-11-09 13:48:411237extensions::SettingsFrontend* ExtensionService::settings_frontend() {
[email protected]cccdf0aa2011-11-11 03:43:381238 return settings_frontend_.get();
[email protected]b7f853e282011-08-10 09:24:201239}
1240
[email protected]e2d720aa2012-05-17 00:11:351241extensions::ContentSettingsStore* ExtensionService::GetContentSettingsStore() {
[email protected]b790b072011-05-20 09:46:441242 return extension_prefs()->content_settings_store();
1243}
1244
[email protected]25ae0152011-11-18 14:40:021245bool ExtensionService::is_ready() {
1246 return ready_;
1247}
1248
[email protected]7f8f24f2012-11-15 19:40:141249base::SequencedTaskRunner* ExtensionService::GetFileTaskRunner() {
1250 if (file_task_runner_)
1251 return file_task_runner_;
1252
1253 // We should be able to interrupt any part of extension install process during
1254 // shutdown. SKIP_ON_SHUTDOWN ensures that not started extension install tasks
1255 // will be ignored/deleted while we will block on started tasks.
1256 std::string token("ext_install-");
1257 token.append(profile_->GetPath().AsUTF8Unsafe());
1258 file_task_runner_ = BrowserThread::GetBlockingPool()->
1259 GetSequencedTaskRunnerWithShutdownBehavior(
1260 BrowserThread::GetBlockingPool()->GetNamedSequenceToken(token),
1261 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
1262 return file_task_runner_;
1263}
1264
[email protected]42a08162012-03-16 18:09:111265extensions::ExtensionUpdater* ExtensionService::updater() {
[email protected]2859946f2011-04-04 18:18:061266 return updater_.get();
1267}
1268
[email protected]4ee07c62012-08-21 12:40:421269void ExtensionService::CheckManagementPolicy() {
[email protected]aa96d3a2010-08-21 08:45:251270 std::vector<std::string> to_be_removed;
[email protected]695b5712012-12-06 23:55:281271
[email protected]aa96d3a2010-08-21 08:45:251272 // Loop through extensions list, unload installed extensions.
[email protected]84df8332011-12-06 18:22:461273 for (ExtensionSet::const_iterator iter = extensions_.begin();
[email protected]aa96d3a2010-08-21 08:45:251274 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021275 const Extension* extension = (*iter);
[email protected]4ee07c62012-08-21 12:40:421276 if (!system_->management_policy()->UserMayLoad(extension, NULL))
[email protected]aa96d3a2010-08-21 08:45:251277 to_be_removed.push_back(extension->id());
1278 }
1279
1280 // UnloadExtension will change the extensions_ list. So, we should
1281 // call it outside the iterator loop.
[email protected]31bb5ee62012-09-12 22:58:401282 for (size_t i = 0; i < to_be_removed.size(); ++i)
[email protected]814a7bf0f2011-08-13 05:30:591283 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]aa96d3a2010-08-21 08:45:251284}
1285
[email protected]31206602011-04-13 23:07:321286void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351287 if (updater()) {
[email protected]94fde232012-04-27 10:22:301288 if (AreAllExternalProvidersReady()) {
1289 updater()->CheckSoon();
1290 } else {
1291 // Sync can start updating before all the external providers are ready
1292 // during startup. Start the update as soon as those providers are ready,
1293 // but not before.
1294 update_once_all_providers_are_ready_ = true;
1295 }
[email protected]c3cfb012011-04-06 22:07:351296 } else {
[email protected]31206602011-04-13 23:07:321297 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351298 }
1299}
1300
[email protected]3f98fd92012-11-17 15:54:291301syncer::SyncMergeResult ExtensionService::MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:321302 syncer::ModelType type,
[email protected]65f173552012-06-28 22:43:581303 const syncer::SyncDataList& initial_sync_data,
1304 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
1305 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) {
[email protected]5db9ada2012-04-11 13:48:201306 CHECK(sync_processor.get());
[email protected]5d36e052012-04-20 20:12:281307 CHECK(sync_error_factory.get());
[email protected]3bdba0d2011-08-23 07:17:301308
1309 switch (type) {
[email protected]a4a147652012-07-03 23:41:321310 case syncer::EXTENSIONS:
[email protected]5db9ada2012-04-11 13:48:201311 extension_sync_bundle_.SetupSync(sync_processor.release(),
[email protected]5d36e052012-04-20 20:12:281312 sync_error_factory.release(),
[email protected]5db9ada2012-04-11 13:48:201313 initial_sync_data);
[email protected]3bdba0d2011-08-23 07:17:301314 break;
1315
[email protected]a4a147652012-07-03 23:41:321316 case syncer::APPS:
[email protected]5d36e052012-04-20 20:12:281317 app_sync_bundle_.SetupSync(sync_processor.release(),
1318 sync_error_factory.release(),
1319 initial_sync_data);
[email protected]3bdba0d2011-08-23 07:17:301320 break;
1321
1322 default:
1323 LOG(FATAL) << "Got " << type << " ModelType";
1324 }
[email protected]3bdba0d2011-08-23 07:17:301325
[email protected]b1417ab2011-12-15 17:09:111326 // Process local extensions.
1327 // TODO(yoz): Determine whether pending extensions should be considered too.
1328 // See crbug.com/104399.
[email protected]65f173552012-06-28 22:43:581329 syncer::SyncDataList sync_data_list = GetAllSyncData(type);
1330 syncer::SyncChangeList sync_change_list;
1331 for (syncer::SyncDataList::const_iterator i = sync_data_list.begin();
[email protected]3bdba0d2011-08-23 07:17:301332 i != sync_data_list.end();
1333 ++i) {
[email protected]5db9ada2012-04-11 13:48:201334 switch (type) {
[email protected]a4a147652012-07-03 23:41:321335 case syncer::EXTENSIONS:
[email protected]5db9ada2012-04-11 13:48:201336 sync_change_list.push_back(
1337 extension_sync_bundle_.CreateSyncChange(*i));
1338 break;
[email protected]a4a147652012-07-03 23:41:321339 case syncer::APPS:
[email protected]5db9ada2012-04-11 13:48:201340 sync_change_list.push_back(app_sync_bundle_.CreateSyncChange(*i));
1341 break;
1342 default:
1343 LOG(FATAL) << "Got " << type << " ModelType";
[email protected]31bb5ee62012-09-12 22:58:401344 }
[email protected]3bdba0d2011-08-23 07:17:301345 }
[email protected]3bdba0d2011-08-23 07:17:301346
[email protected]5db9ada2012-04-11 13:48:201347
[email protected]a4a147652012-07-03 23:41:321348 if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201349 extension_sync_bundle_.ProcessSyncChangeList(sync_change_list);
[email protected]a4a147652012-07-03 23:41:321350 } else if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201351 app_sync_bundle_.ProcessSyncChangeList(sync_change_list);
1352 }
[email protected]0ffd9e52012-01-26 21:44:341353
[email protected]3f98fd92012-11-17 15:54:291354 return syncer::SyncMergeResult(type);
[email protected]3bdba0d2011-08-23 07:17:301355}
1356
[email protected]a4a147652012-07-03 23:41:321357void ExtensionService::StopSyncing(syncer::ModelType type) {
1358 if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201359 app_sync_bundle_.Reset();
[email protected]a4a147652012-07-03 23:41:321360 } else if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201361 extension_sync_bundle_.Reset();
1362 }
[email protected]3bdba0d2011-08-23 07:17:301363}
1364
[email protected]65f173552012-06-28 22:43:581365syncer::SyncDataList ExtensionService::GetAllSyncData(
[email protected]a4a147652012-07-03 23:41:321366 syncer::ModelType type) const {
[email protected]31bb5ee62012-09-12 22:58:401367 if (type == syncer::EXTENSIONS)
[email protected]5db9ada2012-04-11 13:48:201368 return extension_sync_bundle_.GetAllSyncData();
[email protected]31bb5ee62012-09-12 22:58:401369 if (type == syncer::APPS)
[email protected]5db9ada2012-04-11 13:48:201370 return app_sync_bundle_.GetAllSyncData();
[email protected]5db9ada2012-04-11 13:48:201371
1372 // We should only get sync data for extensions and apps.
1373 NOTREACHED();
1374
[email protected]65f173552012-06-28 22:43:581375 return syncer::SyncDataList();
[email protected]3bdba0d2011-08-23 07:17:301376}
1377
[email protected]65f173552012-06-28 22:43:581378syncer::SyncError ExtensionService::ProcessSyncChanges(
[email protected]3bdba0d2011-08-23 07:17:301379 const tracked_objects::Location& from_here,
[email protected]65f173552012-06-28 22:43:581380 const syncer::SyncChangeList& change_list) {
1381 for (syncer::SyncChangeList::const_iterator i = change_list.begin();
[email protected]3bdba0d2011-08-23 07:17:301382 i != change_list.end();
1383 ++i) {
[email protected]a4a147652012-07-03 23:41:321384 syncer::ModelType type = i->sync_data().GetDataType();
1385 if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201386 extension_sync_bundle_.ProcessSyncChange(
1387 extensions::ExtensionSyncData(*i));
[email protected]a4a147652012-07-03 23:41:321388 } else if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201389 app_sync_bundle_.ProcessSyncChange(extensions::AppSyncData(*i));
1390 }
[email protected]3bdba0d2011-08-23 07:17:301391 }
1392
[email protected]0ffd9e52012-01-26 21:44:341393 extension_prefs()->extension_sorting()->FixNTPOrdinalCollisions();
1394
[email protected]65f173552012-06-28 22:43:581395 return syncer::SyncError();
[email protected]b05fb9ff2011-04-23 00:07:561396}
1397
[email protected]5db9ada2012-04-11 13:48:201398extensions::ExtensionSyncData ExtensionService::GetExtensionSyncData(
1399 const Extension& extension) const {
1400 return extensions::ExtensionSyncData(extension,
1401 IsExtensionEnabled(extension.id()),
1402 IsIncognitoEnabled(extension.id()));
[email protected]b05fb9ff2011-04-23 00:07:561403}
1404
[email protected]5db9ada2012-04-11 13:48:201405extensions::AppSyncData ExtensionService::GetAppSyncData(
1406 const Extension& extension) const {
1407 return extensions::AppSyncData(
1408 extension,
1409 IsExtensionEnabled(extension.id()),
1410 IsIncognitoEnabled(extension.id()),
[email protected]5db9ada2012-04-11 13:48:201411 extension_prefs_->extension_sorting()->GetAppLaunchOrdinal(
1412 extension.id()),
1413 extension_prefs_->extension_sorting()->GetPageOrdinal(extension.id()));
1414}
[email protected]3bdba0d2011-08-23 07:17:301415
[email protected]5db9ada2012-04-11 13:48:201416std::vector<extensions::ExtensionSyncData>
1417 ExtensionService::GetExtensionSyncDataList() const {
1418 std::vector<extensions::ExtensionSyncData> extension_sync_list;
1419 extension_sync_bundle_.GetExtensionSyncDataListHelper(extensions_,
1420 &extension_sync_list);
1421 extension_sync_bundle_.GetExtensionSyncDataListHelper(disabled_extensions_,
1422 &extension_sync_list);
1423 extension_sync_bundle_.GetExtensionSyncDataListHelper(terminated_extensions_,
1424 &extension_sync_list);
1425
1426 std::vector<extensions::ExtensionSyncData> pending_extensions =
1427 extension_sync_bundle_.GetPendingData();
1428 extension_sync_list.insert(extension_sync_list.begin(),
1429 pending_extensions.begin(),
1430 pending_extensions.end());
[email protected]3bdba0d2011-08-23 07:17:301431
1432 return extension_sync_list;
[email protected]b05fb9ff2011-04-23 00:07:561433}
1434
[email protected]5db9ada2012-04-11 13:48:201435std::vector<extensions::AppSyncData> ExtensionService::GetAppSyncDataList()
1436 const {
1437 std::vector<extensions::AppSyncData> app_sync_list;
1438 app_sync_bundle_.GetAppSyncDataListHelper(extensions_, &app_sync_list);
1439 app_sync_bundle_.GetAppSyncDataListHelper(disabled_extensions_,
1440 &app_sync_list);
1441 app_sync_bundle_.GetAppSyncDataListHelper(terminated_extensions_,
1442 &app_sync_list);
1443
1444 std::vector<extensions::AppSyncData> pending_apps =
1445 app_sync_bundle_.GetPendingData();
1446 app_sync_list.insert(app_sync_list.begin(),
1447 pending_apps.begin(),
1448 pending_apps.end());
1449
1450 return app_sync_list;
1451}
1452
1453bool ExtensionService::ProcessExtensionSyncData(
1454 const extensions::ExtensionSyncData& extension_sync_data) {
1455 if (!ProcessExtensionSyncDataHelper(extension_sync_data,
[email protected]a4a147652012-07-03 23:41:321456 syncer::EXTENSIONS)) {
[email protected]5db9ada2012-04-11 13:48:201457 extension_sync_bundle_.AddPendingExtension(extension_sync_data.id(),
1458 extension_sync_data);
1459 CheckForUpdatesSoon();
1460 return false;
1461 }
1462
1463 return true;
1464}
1465
1466bool ExtensionService::ProcessAppSyncData(
1467 const extensions::AppSyncData& app_sync_data) {
1468 const std::string& id = app_sync_data.id();
[email protected]5db9ada2012-04-11 13:48:201469
1470 if (app_sync_data.app_launch_ordinal().IsValid() &&
1471 app_sync_data.page_ordinal().IsValid()) {
1472 extension_prefs_->extension_sorting()->SetAppLaunchOrdinal(
1473 id,
1474 app_sync_data.app_launch_ordinal());
1475 extension_prefs_->extension_sorting()->SetPageOrdinal(
1476 id,
1477 app_sync_data.page_ordinal());
1478 }
1479
[email protected]5db9ada2012-04-11 13:48:201480 if (!ProcessExtensionSyncDataHelper(app_sync_data.extension_sync_data(),
[email protected]a4a147652012-07-03 23:41:321481 syncer::APPS)) {
[email protected]5db9ada2012-04-11 13:48:201482 app_sync_bundle_.AddPendingApp(id, app_sync_data);
1483 CheckForUpdatesSoon();
1484 return false;
1485 }
1486
1487 return true;
1488}
1489
1490bool ExtensionService::IsCorrectSyncType(const Extension& extension,
[email protected]a4a147652012-07-03 23:41:321491 syncer::ModelType type) const {
1492 if (type == syncer::EXTENSIONS &&
[email protected]31bb5ee62012-09-12 22:58:401493 extension.GetSyncType() == Extension::SYNC_TYPE_EXTENSION) {
[email protected]5db9ada2012-04-11 13:48:201494 return true;
[email protected]31bb5ee62012-09-12 22:58:401495 }
[email protected]5db9ada2012-04-11 13:48:201496
[email protected]a4a147652012-07-03 23:41:321497 if (type == syncer::APPS &&
[email protected]31bb5ee62012-09-12 22:58:401498 extension.GetSyncType() == Extension::SYNC_TYPE_APP) {
[email protected]5db9ada2012-04-11 13:48:201499 return true;
[email protected]31bb5ee62012-09-12 22:58:401500 }
[email protected]5db9ada2012-04-11 13:48:201501
1502 return false;
1503}
1504
1505bool ExtensionService::ProcessExtensionSyncDataHelper(
1506 const extensions::ExtensionSyncData& extension_sync_data,
[email protected]a4a147652012-07-03 23:41:321507 syncer::ModelType type) {
[email protected]3bdba0d2011-08-23 07:17:301508 const std::string& id = extension_sync_data.id();
[email protected]96e989b2011-08-30 19:35:061509 const Extension* extension = GetInstalledExtension(id);
1510
1511 // TODO(bolms): we should really handle this better. The particularly bad
1512 // case is where an app becomes an extension or vice versa, and we end up with
1513 // a zombie extension that won't go away.
[email protected]5db9ada2012-04-11 13:48:201514 if (extension && !IsCorrectSyncType(*extension, type))
1515 return true;
[email protected]90310d92011-04-17 07:35:041516
1517 // Handle uninstalls first.
[email protected]3bdba0d2011-08-23 07:17:301518 if (extension_sync_data.uninstalled()) {
[email protected]90310d92011-04-17 07:35:041519 std::string error;
1520 if (!UninstallExtensionHelper(this, id)) {
1521 LOG(WARNING) << "Could not uninstall extension " << id
1522 << " for sync";
1523 }
[email protected]5db9ada2012-04-11 13:48:201524 return true;
[email protected]90310d92011-04-17 07:35:041525 }
1526
[email protected]caf2d442012-05-04 10:00:501527 // Extension from sync was uninstalled by the user as external extensions.
1528 // Honor user choice and skip installation/enabling.
1529 if (IsExternalExtensionUninstalled(id)) {
1530 LOG(WARNING) << "Extension with id " << id
1531 << " from sync was uninstalled as external extension";
1532 return true;
1533 }
1534
[email protected]06f92562011-04-29 19:27:311535 // Set user settings.
[email protected]7e9e2422012-12-13 19:54:191536 // If the extension has been disabled from sync, it may not have
1537 // been installed yet, so we don't know if the disable reason was a
1538 // permissions increase. That will be updated once InitializePermissions
1539 // is called for it.
[email protected]31bb5ee62012-09-12 22:58:401540 if (extension_sync_data.enabled())
[email protected]06f92562011-04-29 19:27:311541 EnableExtension(id);
[email protected]31bb5ee62012-09-12 22:58:401542 else
[email protected]7e9e2422012-12-13 19:54:191543 DisableExtension(id, Extension::DISABLE_UNKNOWN_FROM_SYNC);
[email protected]81b14cc2011-04-29 00:39:371544
[email protected]406b5a92011-11-08 11:58:261545 // We need to cache some version information here because setting the
1546 // incognito flag invalidates the |extension| pointer (it reloads the
1547 // extension).
[email protected]9060d8b02012-01-13 02:14:301548 bool extension_installed = (extension != NULL);
[email protected]406b5a92011-11-08 11:58:261549 int result = extension ?
1550 extension->version()->CompareTo(extension_sync_data.version()) : 0;
1551 SetIsIncognitoEnabled(id, extension_sync_data.incognito_enabled());
1552 extension = NULL; // No longer safe to use.
1553
1554 if (extension_installed) {
[email protected]06f92562011-04-29 19:27:311555 // If the extension is already installed, check if it's outdated.
[email protected]90310d92011-04-17 07:35:041556 if (result < 0) {
1557 // Extension is outdated.
[email protected]5db9ada2012-04-11 13:48:201558 return false;
[email protected]b020dc12011-12-06 04:35:421559 }
[email protected]06f92562011-04-29 19:27:311560 } else {
[email protected]06f92562011-04-29 19:27:311561 // TODO(akalin): Replace silent update with a list of enabled
1562 // permissions.
[email protected]6cc7dbae2011-04-29 21:18:331563 const bool kInstallSilently = true;
[email protected]5db9ada2012-04-11 13:48:201564
[email protected]a4a147652012-07-03 23:41:321565 CHECK(type == syncer::EXTENSIONS || type == syncer::APPS);
[email protected]5db9ada2012-04-11 13:48:201566 ExtensionFilter filter =
[email protected]a4a147652012-07-03 23:41:321567 (type == syncer::APPS) ? IsSyncableApp : IsSyncableExtension;
[email protected]5db9ada2012-04-11 13:48:201568
[email protected]06f92562011-04-29 19:27:311569 if (!pending_extension_manager()->AddFromSync(
1570 id,
[email protected]3bdba0d2011-08-23 07:17:301571 extension_sync_data.update_url(),
[email protected]5db9ada2012-04-11 13:48:201572 filter,
[email protected]6cc7dbae2011-04-29 21:18:331573 kInstallSilently)) {
[email protected]06f92562011-04-29 19:27:311574 LOG(WARNING) << "Could not add pending extension for " << id;
[email protected]3bdba0d2011-08-23 07:17:301575 // This means that the extension is already pending installation, with a
1576 // non-INTERNAL location. Add to pending_sync_data, even though it will
1577 // never be removed (we'll never install a syncable version of the
1578 // extension), so that GetAllSyncData() continues to send it.
[email protected]06f92562011-04-29 19:27:311579 }
[email protected]3bdba0d2011-08-23 07:17:301580 // Track pending extensions so that we can return them in GetAllSyncData().
[email protected]5db9ada2012-04-11 13:48:201581 return false;
[email protected]90310d92011-04-17 07:35:041582 }
[email protected]5db9ada2012-04-11 13:48:201583
1584 return true;
[email protected]90310d92011-04-17 07:35:041585}
1586
[email protected]c3cfb012011-04-06 22:07:351587bool ExtensionService::IsIncognitoEnabled(
1588 const std::string& extension_id) const {
[email protected]cdc7b1f42012-12-07 19:39:481589 const Extension* extension = GetInstalledExtension(extension_id);
1590 if (extension && !extension->can_be_incognito_enabled())
1591 return false;
[email protected]c3cfb012011-04-06 22:07:351592 // If this is an existing component extension we always allow it to
1593 // work in incognito mode.
[email protected]1d5e58b2013-01-31 08:41:401594 if (extension && extension->location() == Manifest::COMPONENT)
[email protected]cb0ce1e022010-03-10 19:54:411595 return true;
1596
1597 // Check the prefs.
[email protected]c3cfb012011-04-06 22:07:351598 return extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]db7331a2010-02-25 22:10:501599}
[email protected]55a35692010-02-11 23:25:211600
[email protected]c3cfb012011-04-06 22:07:351601void ExtensionService::SetIsIncognitoEnabled(
1602 const std::string& extension_id, bool enabled) {
[email protected]81b14cc2011-04-29 00:39:371603 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]cdc7b1f42012-12-07 19:39:481604 if (extension && !extension->can_be_incognito_enabled())
1605 return;
[email protected]1d5e58b2013-01-31 08:41:401606 if (extension && extension->location() == Manifest::COMPONENT) {
[email protected]b873cd92012-02-09 21:51:481607 // This shouldn't be called for component extensions unless they are
1608 // syncable.
1609 DCHECK(extension->IsSyncable());
1610
1611 // If we are here, make sure the we aren't trying to change the value.
1612 DCHECK_EQ(enabled, IsIncognitoEnabled(extension_id));
1613
[email protected]c3cfb012011-04-06 22:07:351614 return;
1615 }
1616
[email protected]8c6c8c22011-03-09 12:52:341617 // Broadcast unloaded and loaded events to update browser state. Only bother
1618 // if the value changed and the extension is actually enabled, since there is
1619 // no UI otherwise.
[email protected]c3cfb012011-04-06 22:07:351620 bool old_enabled = extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]8c6c8c22011-03-09 12:52:341621 if (enabled == old_enabled)
1622 return;
1623
[email protected]c3cfb012011-04-06 22:07:351624 extension_prefs_->SetIsIncognitoEnabled(extension_id, enabled);
[email protected]81b14cc2011-04-29 00:39:371625
[email protected]ffbf3f12011-12-09 22:10:331626 bool extension_is_enabled = extensions_.Contains(extension_id);
[email protected]bd9892eb2011-11-11 19:41:341627
1628 // When we reload the extension the ID may be invalidated if we've passed it
1629 // by const ref everywhere. Make a copy to be safe.
1630 std::string id = extension_id;
[email protected]406b5a92011-11-08 11:58:261631 if (extension_is_enabled)
[email protected]ffbf3f12011-12-09 22:10:331632 ReloadExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301633
[email protected]406b5a92011-11-08 11:58:261634 // Reloading the extension invalidates the |extension| pointer.
[email protected]bd9892eb2011-11-11 19:41:341635 extension = GetInstalledExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301636 if (extension)
1637 SyncExtensionChangeIfNeeded(*extension);
[email protected]55a35692010-02-11 23:25:211638}
1639
[email protected]dc9a74f72012-08-17 18:07:211640bool ExtensionService::CanCrossIncognito(const Extension* extension) const {
[email protected]2a8f24e2010-11-03 21:37:051641 // We allow the extension to see events and data from another profile iff it
1642 // uses "spanning" behavior and it has incognito access. "split" mode
1643 // extensions only see events for a matching profile.
[email protected]0c41298f2011-09-13 21:17:221644 CHECK(extension);
[email protected]c3cfb012011-04-06 22:07:351645 return IsIncognitoEnabled(extension->id()) &&
[email protected]06492ed2013-03-24 22:13:141646 !extensions::IncognitoInfo::IsSplitMode(extension);
[email protected]2a8f24e2010-11-03 21:37:051647}
1648
[email protected]5ef7b002011-05-12 23:16:201649bool ExtensionService::CanLoadInIncognito(const Extension* extension) const {
1650 if (extension->is_hosted_app())
1651 return true;
1652 // Packaged apps and regular extensions need to be enabled specifically for
1653 // incognito (and split mode should be set).
[email protected]06492ed2013-03-24 22:13:141654 return extensions::IncognitoInfo::IsSplitMode(extension) &&
[email protected]5ef7b002011-05-12 23:16:201655 IsIncognitoEnabled(extension->id());
1656}
1657
[email protected]168389f2011-12-20 17:12:481658void ExtensionService::OnExtensionMoved(
1659 const std::string& moved_extension_id,
1660 const std::string& predecessor_extension_id,
1661 const std::string& successor_extension_id) {
[email protected]3569b502012-01-12 20:08:231662 extension_prefs_->extension_sorting()->OnExtensionMoved(
1663 moved_extension_id,
1664 predecessor_extension_id,
1665 successor_extension_id);
[email protected]168389f2011-12-20 17:12:481666
1667 const Extension* extension = GetInstalledExtension(moved_extension_id);
1668 if (extension)
1669 SyncExtensionChangeIfNeeded(*extension);
1670}
1671
[email protected]dc9a74f72012-08-17 18:07:211672bool ExtensionService::AllowFileAccess(const Extension* extension) const {
[email protected]05c82182010-06-24 17:49:081673 return (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]334e04a2010-06-24 23:34:441674 switches::kDisableExtensionsFileAccessCheck) ||
[email protected]05c82182010-06-24 17:49:081675 extension_prefs_->AllowFileAccess(extension->id()));
1676}
1677
[email protected]eaa7dd182010-12-14 11:09:001678void ExtensionService::SetAllowFileAccess(const Extension* extension,
[email protected]3aff9ad2011-04-01 20:26:481679 bool allow) {
1680 // Reload to update browser state. Only bother if the value changed and the
1681 // extension is actually enabled, since there is no UI otherwise.
1682 bool old_allow = AllowFileAccess(extension);
1683 if (allow == old_allow)
1684 return;
1685
[email protected]05c82182010-06-24 17:49:081686 extension_prefs_->SetAllowFileAccess(extension->id(), allow);
[email protected]3aff9ad2011-04-01 20:26:481687
[email protected]84df8332011-12-06 18:22:461688 bool extension_is_enabled = extensions_.Contains(extension->id());
[email protected]3aff9ad2011-04-01 20:26:481689 if (extension_is_enabled)
1690 ReloadExtension(extension->id());
[email protected]05c82182010-06-24 17:49:081691}
1692
[email protected]8e4560b62011-01-14 10:09:141693// Some extensions will autoupdate themselves externally from Chrome. These
1694// are typically part of some larger client application package. To support
1695// these, the extension will register its location in the the preferences file
1696// (and also, on Windows, in the registry) and this code will periodically
1697// check that location for a .crx file, which it will then install locally if
1698// a new version is available.
1699// Errors are reported through ExtensionErrorReporter. Succcess is not
1700// reported.
[email protected]eaa7dd182010-12-14 11:09:001701void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121702 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141703
1704 // Note that this installation is intentionally silent (since it didn't
1705 // go through the front-end). Extensions that are registered in this
1706 // way are effectively considered 'pre-bundled', and so implicitly
1707 // trusted. In general, if something has HKLM or filesystem access,
1708 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121709
[email protected]8e4560b62011-01-14 10:09:141710 // Ask each external extension provider to give us a call back for each
1711 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
[email protected]5df038b2012-07-16 19:03:271712 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141713 for (i = external_extension_providers_.begin();
1714 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:271715 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:141716 provider->VisitRegisteredExtension();
1717 }
1718
[email protected]50067e52011-10-20 23:17:071719 // Do any required work that we would have done after completion of all
1720 // providers.
1721 if (external_extension_providers_.empty()) {
1722 OnAllExternalProvidersReady();
1723 }
[email protected]9f1087e2009-06-15 17:29:321724}
1725
[email protected]50067e52011-10-20 23:17:071726void ExtensionService::OnExternalProviderReady(
[email protected]5df038b2012-07-16 19:03:271727 const extensions::ExternalProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121728 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071729 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121730
1731 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141732 // if all of them are finished. So we check them first.
[email protected]94fde232012-04-27 10:22:301733 if (AreAllExternalProvidersReady())
1734 OnAllExternalProvidersReady();
1735}
1736
1737bool ExtensionService::AreAllExternalProvidersReady() const {
[email protected]5df038b2012-07-16 19:03:271738 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141739 for (i = external_extension_providers_.begin();
1740 i != external_extension_providers_.end(); ++i) {
[email protected]94fde232012-04-27 10:22:301741 if (!i->get()->IsReady())
1742 return false;
[email protected]8e4560b62011-01-14 10:09:141743 }
[email protected]94fde232012-04-27 10:22:301744 return true;
[email protected]50067e52011-10-20 23:17:071745}
1746
1747void ExtensionService::OnAllExternalProvidersReady() {
1748 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]94fde232012-04-27 10:22:301749 base::TimeDelta elapsed = base::Time::Now() - profile_->GetStartTime();
1750 UMA_HISTOGRAM_TIMES("Extension.ExternalProvidersReadyAfter", elapsed);
[email protected]50067e52011-10-20 23:17:071751
1752 // Install any pending extensions.
[email protected]94fde232012-04-27 10:22:301753 if (update_once_all_providers_are_ready_ && updater()) {
1754 update_once_all_providers_are_ready_ = false;
[email protected]42774e42012-11-14 21:01:061755 updater()->CheckNow(extensions::ExtensionUpdater::CheckParams());
[email protected]8e4560b62011-01-14 10:09:141756 }
1757
1758 // Uninstall all the unclaimed extensions.
[email protected]45759612012-07-10 17:21:231759 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]8e4560b62011-01-14 10:09:141760 extension_prefs_->GetInstalledExtensionsInfo());
1761 for (size_t i = 0; i < extensions_info->size(); ++i) {
1762 ExtensionInfo* info = extensions_info->at(i).get();
[email protected]1d5e58b2013-01-31 08:41:401763 if (Manifest::IsExternalLocation(info->extension_location))
[email protected]8e4560b62011-01-14 10:09:141764 CheckExternalUninstall(info->extension_id);
1765 }
[email protected]e5af875f2011-10-10 21:09:141766 IdentifyAlertableExtensions();
[email protected]e5af875f2011-10-10 21:09:141767}
1768
1769void ExtensionService::IdentifyAlertableExtensions() {
1770 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1771
[email protected]07c9f2f42012-02-29 18:45:221772 // Build up the lists of extensions that require acknowledgment. If this is
1773 // the first time, grandfather extensions that would have caused
1774 // notification.
[email protected]89226982012-07-16 20:09:181775 extension_error_ui_.reset(ExtensionErrorUI::Create(this));
[email protected]e5af875f2011-10-10 21:09:141776
[email protected]a9aa5932012-01-25 08:27:401777 bool did_show_alert = false;
[email protected]89226982012-07-16 20:09:181778 if (PopulateExtensionErrorUI(extension_error_ui_.get())) {
[email protected]e5af875f2011-10-10 21:09:141779 if (extension_prefs_->SetAlertSystemFirstRun()) {
[email protected]a9aa5932012-01-25 08:27:401780 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]89226982012-07-16 20:09:181781 did_show_alert = extension_error_ui_->ShowErrorInBubbleView();
[email protected]e5af875f2011-10-10 21:09:141782 } else {
1783 // First run. Just acknowledge all the extensions, silently, by
1784 // shortcutting the display of the UI and going straight to the
1785 // callback for pressing the Accept button.
[email protected]a9aa5932012-01-25 08:27:401786 HandleExtensionAlertAccept();
[email protected]e5af875f2011-10-10 21:09:141787 }
1788 }
[email protected]a9aa5932012-01-25 08:27:401789
[email protected]612a1cb12012-10-17 13:18:031790 UpdateExternalExtensionAlert();
1791
[email protected]a9aa5932012-01-25 08:27:401792 if (!did_show_alert)
[email protected]89226982012-07-16 20:09:181793 extension_error_ui_.reset();
[email protected]e5af875f2011-10-10 21:09:141794}
1795
[email protected]89226982012-07-16 20:09:181796bool ExtensionService::PopulateExtensionErrorUI(
1797 ExtensionErrorUI* extension_error_ui) {
[email protected]07c9f2f42012-02-29 18:45:221798 bool needs_alert = false;
[email protected]695b5712012-12-06 23:55:281799
1800 // Extensions that are blacklisted.
1801 for (ExtensionSet::const_iterator it = blacklisted_extensions_.begin();
1802 it != blacklisted_extensions_.end(); ++it) {
1803 std::string id = (*it)->id();
1804 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(id)) {
1805 extension_error_ui->AddBlacklistedExtension(id);
1806 needs_alert = true;
1807 }
1808 }
1809
[email protected]07c9f2f42012-02-29 18:45:221810 for (ExtensionSet::const_iterator iter = extensions_.begin();
1811 iter != extensions_.end(); ++iter) {
1812 const Extension* e = *iter;
[email protected]695b5712012-12-06 23:55:281813
1814 // Extensions disabled by policy. Note: this no longer includes blacklisted
1815 // extensions, though we still show the same UI.
[email protected]fdd679b2012-11-15 20:49:391816 if (!system_->management_policy()->UserMayLoad(e, NULL)) {
[email protected]07c9f2f42012-02-29 18:45:221817 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(e->id())) {
[email protected]89226982012-07-16 20:09:181818 extension_error_ui->AddBlacklistedExtension(e->id());
[email protected]07c9f2f42012-02-29 18:45:221819 needs_alert = true;
1820 }
1821 }
[email protected]07c9f2f42012-02-29 18:45:221822 }
[email protected]695b5712012-12-06 23:55:281823
[email protected]07c9f2f42012-02-29 18:45:221824 return needs_alert;
1825}
1826
[email protected]a9aa5932012-01-25 08:27:401827void ExtensionService::HandleExtensionAlertClosed() {
[email protected]612a1cb12012-10-17 13:18:031828 const ExtensionIdSet* extension_ids =
1829 extension_error_ui_->get_blacklisted_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141830 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1831 iter != extension_ids->end(); ++iter) {
1832 extension_prefs_->AcknowledgeBlacklistedExtension(*iter);
1833 }
[email protected]879a6092013-03-06 04:12:501834 extension_error_ui_.reset();
1835}
1836
1837void ExtensionService::HandleExtensionAlertAccept() {
1838 HandleExtensionAlertClosed();
[email protected]e5af875f2011-10-10 21:09:141839}
1840
[email protected]47fc70c2011-12-06 07:29:511841void ExtensionService::AcknowledgeExternalExtension(const std::string& id) {
1842 extension_prefs_->AcknowledgeExternalExtension(id);
[email protected]612a1cb12012-10-17 13:18:031843 UpdateExternalExtensionAlert();
[email protected]47fc70c2011-12-06 07:29:511844}
1845
[email protected]d96eb512012-11-01 23:44:081846bool ExtensionService::IsUnacknowledgedExternalExtension(
1847 const Extension* extension) {
[email protected]b82d18da2012-11-09 00:11:321848 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
1849 return false;
1850
[email protected]1d5e58b2013-01-31 08:41:401851 return (Manifest::IsExternalLocation(extension->location()) &&
[email protected]d96eb512012-11-01 23:44:081852 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id()) &&
1853 !(extension_prefs_->GetDisableReasons(extension->id()) &
1854 Extension::DISABLE_SIDELOAD_WIPEOUT));
1855}
1856
[email protected]89226982012-07-16 20:09:181857void ExtensionService::HandleExtensionAlertDetails() {
1858 extension_error_ui_->ShowExtensions();
[email protected]879a6092013-03-06 04:12:501859 HandleExtensionAlertClosed();
[email protected]6c751e72010-11-23 10:11:101860}
1861
[email protected]612a1cb12012-10-17 13:18:031862void ExtensionService::UpdateExternalExtensionAlert() {
[email protected]41070e8d2012-10-24 01:34:361863 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
[email protected]612a1cb12012-10-17 13:18:031864 return;
1865
1866 const Extension* extension = NULL;
1867 for (ExtensionSet::const_iterator iter = disabled_extensions_.begin();
1868 iter != disabled_extensions_.end(); ++iter) {
1869 const Extension* e = *iter;
[email protected]d96eb512012-11-01 23:44:081870 if (IsUnacknowledgedExternalExtension(e)) {
1871 extension = e;
1872 break;
[email protected]612a1cb12012-10-17 13:18:031873 }
1874 }
1875
1876 if (extension) {
[email protected]d96eb512012-11-01 23:44:081877 if (!extensions::HasExternalInstallError(this)) {
1878 if (extension_prefs_->IncrementAcknowledgePromptCount(extension->id()) >
[email protected]612a1cb12012-10-17 13:18:031879 kMaxExtensionAcknowledgePromptCount) {
[email protected]d96eb512012-11-01 23:44:081880 // Stop prompting for this extension, and check if there's another
1881 // one that needs prompting.
[email protected]612a1cb12012-10-17 13:18:031882 extension_prefs_->AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:081883 UpdateExternalExtensionAlert();
1884 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
1885 EXTERNAL_EXTENSION_IGNORED,
1886 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
1887 return;
[email protected]612a1cb12012-10-17 13:18:031888 }
[email protected]d96eb512012-11-01 23:44:081889 extensions::AddExternalInstallError(this, extension);
[email protected]612a1cb12012-10-17 13:18:031890 }
1891 } else {
1892 extensions::RemoveExternalInstallError(this);
1893 }
[email protected]612a1cb12012-10-17 13:18:031894}
1895
[email protected]a9f39a312010-12-23 22:14:271896void ExtensionService::UnloadExtension(
1897 const std::string& extension_id,
[email protected]814a7bf0f2011-08-13 05:30:591898 extension_misc::UnloadedExtensionReason reason) {
[email protected]27e469a2010-01-11 20:35:091899 // Make sure the extension gets deleted after we return from this function.
[email protected]695b5712012-12-06 23:55:281900 int include_mask = INCLUDE_EVERYTHING & ~INCLUDE_TERMINATED;
[email protected]9adb9692010-10-29 23:14:021901 scoped_refptr<const Extension> extension(
[email protected]695b5712012-12-06 23:55:281902 GetExtensionById(extension_id, include_mask));
[email protected]631cf822009-05-15 07:01:251903
[email protected]fa0c96732010-11-17 00:14:231904 // This method can be called via PostTask, so the extension may have been
1905 // unloaded by the time this runs.
[email protected]dd163fb02011-05-04 22:22:171906 if (!extension) {
1907 // In case the extension may have crashed/uninstalled. Allow the profile to
1908 // clean up its RequestContexts.
[email protected]31d8f5f22012-04-02 15:22:081909 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231910 return;
[email protected]dd163fb02011-05-04 22:22:171911 }
[email protected]0c6da502009-08-14 22:32:391912
[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.
1918 disabled_extension_paths_.erase(extension->id());
1919
[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]9f1087e2009-06-15 17:29:321961}
1962
[email protected]eaa7dd182010-12-14 11:09:001963void ExtensionService::GarbageCollectExtensions() {
[email protected]2f8757c32012-06-19 19:17:471964 if (extension_prefs_->pref_service()->ReadOnly())
1965 return;
1966
[email protected]9bd9a6862012-11-29 09:24:221967 if (pending_extension_manager()->HasPendingExtensions()) {
1968 // Don't garbage collect while there are pending installations, which may
1969 // be using the temporary installation directory. Try to garbage collect
1970 // again later.
1971 MessageLoop::current()->PostDelayedTask(
1972 FROM_HERE,
1973 base::Bind(&ExtensionService::GarbageCollectExtensions, AsWeakPtr()),
1974 base::TimeDelta::FromSeconds(kGarbageCollectRetryDelay));
1975 return;
1976 }
1977
[email protected]45759612012-07-10 17:21:231978 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> info(
[email protected]2f8757c32012-06-19 19:17:471979 extension_prefs_->GetInstalledExtensionsInfo());
1980
[email protected]650b2d52013-02-10 03:41:451981 std::multimap<std::string, base::FilePath> extension_paths;
[email protected]2f8757c32012-06-19 19:17:471982 for (size_t i = 0; i < info->size(); ++i)
[email protected]0db124b02012-11-07 04:55:051983 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
1984 info->at(i)->extension_path));
1985
[email protected]399583b2012-12-11 09:33:421986 info = extension_prefs_->GetAllDelayedInstallInfo();
[email protected]0db124b02012-11-07 04:55:051987 for (size_t i = 0; i < info->size(); ++i)
1988 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
1989 info->at(i)->extension_path));
[email protected]2f8757c32012-06-19 19:17:471990
[email protected]7f8f24f2012-11-15 19:40:141991 if (!GetFileTaskRunner()->PostTask(
1992 FROM_HERE,
[email protected]2f8757c32012-06-19 19:17:471993 base::Bind(
1994 &extension_file_util::GarbageCollectExtensions,
1995 install_directory_,
[email protected]31bb5ee62012-09-12 22:58:401996 extension_paths))) {
[email protected]2f8757c32012-06-19 19:17:471997 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:401998 }
[email protected]f6ccd652010-11-17 00:48:341999
[email protected]f766a9d2012-07-11 01:57:442000#if defined(ENABLE_THEMES)
[email protected]f6ccd652010-11-17 00:48:342001 // Also garbage-collect themes. We check |profile_| to be
2002 // defensive; in the future, we may call GarbageCollectExtensions()
2003 // from somewhere other than Init() (e.g., in a timer).
2004 if (profile_) {
[email protected]d5949312012-12-03 22:13:302005 ThemeServiceFactory::GetForProfile(profile_)->RemoveUnusedThemes();
[email protected]f6ccd652010-11-17 00:48:342006 }
[email protected]f766a9d2012-07-11 01:57:442007#endif
[email protected]3cf4f0992009-02-03 23:00:302008}
2009
[email protected]fb82dcd2012-03-21 14:15:462010void ExtensionService::SyncExtensionChangeIfNeeded(const Extension& extension) {
[email protected]5db9ada2012-04-11 13:48:202011 if (app_sync_bundle_.HandlesApp(extension)) {
2012 app_sync_bundle_.SyncChangeIfNeeded(extension);
2013 } else if (extension_sync_bundle_.HandlesExtension(extension)) {
2014 extension_sync_bundle_.SyncChangeIfNeeded(extension);
[email protected]fb82dcd2012-03-21 14:15:462015 }
2016}
2017
[email protected]820d9bd2013-04-03 03:46:032018void ExtensionService::SetReadyAndNotifyListeners() {
2019 ready_ = true;
2020 content::NotificationService::current()->Notify(
2021 chrome::NOTIFICATION_EXTENSIONS_READY,
2022 content::Source<Profile>(profile_),
2023 content::NotificationService::NoDetails());
2024}
2025
[email protected]eaa7dd182010-12-14 11:09:002026void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]31bb5ee62012-09-12 22:58:402027 if (updater_.get())
[email protected]93fd78f42009-07-10 16:43:172028 updater_->Start();
[email protected]fa6a9102010-11-22 15:38:502029
[email protected]bc151cf92013-02-12 04:57:262030 OnBlacklistUpdated();
2031
[email protected]820d9bd2013-04-03 03:46:032032 // The Sideload Wipeout effort takes place during load (see above), so once
2033 // that is done the flag can be set so that we don't have to check again.
2034 if (wipeout_is_active_) {
2035 extension_prefs_->SetSideloadWipeoutDone();
2036 wipeout_is_active_ = false; // Wipeout is only on during load.
2037 UMA_HISTOGRAM_BOOLEAN("DisabledExtension.SideloadWipeoutNeeded",
2038 wipeout_count_ > 0u);
2039 }
2040
2041 SetReadyAndNotifyListeners();
[email protected]e72e8eb82009-06-18 17:21:512042}
2043
[email protected]fcb58a862012-05-01 01:03:152044void ExtensionService::AddExtension(const Extension* extension) {
[email protected]c8d407e2011-04-28 21:27:172045 // TODO(jstritar): We may be able to get rid of this branch by overriding the
2046 // default extension state to DISABLED when the --disable-extensions flag
2047 // is set (http://crbug.com/29067).
2048 if (!extensions_enabled() &&
2049 !extension->is_theme() &&
[email protected]1d5e58b2013-01-31 08:41:402050 extension->location() != Manifest::COMPONENT &&
2051 !Manifest::IsExternalLocation(extension->location())) {
[email protected]fcb58a862012-05-01 01:03:152052 return;
[email protected]330840c2012-08-29 22:21:012053 }
[email protected]c8d407e2011-04-28 21:27:172054
2055 SetBeingUpgraded(extension, false);
2056
[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.
2064 if (disabled_extension_paths_.erase(extension->id()) > 0)
2065 EnableExtension(extension->id());
2066
[email protected]d728e002010-12-08 04:46:232067 // Check if the extension's privileges have changed and disable the
2068 // extension if necessary.
[email protected]902fd7b2011-07-27 18:42:312069 InitializePermissions(extension);
[email protected]0c6da502009-08-14 22:32:392070
[email protected]215a7be2012-10-22 19:53:422071 // If this extension is a sideloaded extension and we've not performed a
2072 // wipeout before, we might disable this extension here.
2073 MaybeWipeout(extension);
2074
[email protected]695b5712012-12-06 23:55:282075 if (extension_prefs_->IsExtensionBlacklisted(extension->id())) {
[email protected]ac875372013-02-28 04:36:092076 // Only prefs is checked for the blacklist. We rely on callers to check the
2077 // blacklist before calling into here, e.g. CrxInstaller checks before
2078 // installation, we check when loading installed extensions.
[email protected]695b5712012-12-06 23:55:282079 blacklisted_extensions_.Insert(extension);
[email protected]695b5712012-12-06 23:55:282080 } else if (extension_prefs_->IsExtensionDisabled(extension->id())) {
[email protected]6f6101832012-11-27 22:10:482081 disabled_extensions_.Insert(extension);
[email protected]3bdba0d2011-08-23 07:17:302082 SyncExtensionChangeIfNeeded(*extension);
[email protected]44d62b62012-04-11 00:06:032083 content::NotificationService::current()->Notify(
2084 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
2085 content::Source<Profile>(profile_),
2086 content::Details<const Extension>(extension));
[email protected]62f051c2012-03-29 17:04:442087
[email protected]7e9e2422012-12-13 19:54:192088 // Show the extension disabled error if a permissions increase was the
2089 // only reason it was disabled.
2090 if (extension_prefs_->GetDisableReasons(extension->id()) ==
[email protected]44d62b62012-04-11 00:06:032091 Extension::DISABLE_PERMISSIONS_INCREASE) {
[email protected]62f051c2012-03-29 17:04:442092 extensions::AddExtensionDisabledError(this, extension);
2093 }
[email protected]695b5712012-12-06 23:55:282094 } else {
2095 // All apps that are displayed in the launcher are ordered by their ordinals
2096 // so we must ensure they have valid ordinals.
2097 if (extension->RequiresSortOrdinal()) {
[email protected]bd36b652013-02-27 02:13:252098 if (!extension->ShouldDisplayInNewTabPage()) {
2099 extension_prefs_->extension_sorting()->MarkExtensionAsHidden(
2100 extension->id());
2101 }
[email protected]695b5712012-12-06 23:55:282102 extension_prefs_->extension_sorting()->EnsureValidOrdinals(
2103 extension->id(), syncer::StringOrdinal());
2104 }
2105
2106 extensions_.Insert(extension);
2107 SyncExtensionChangeIfNeeded(*extension);
2108 NotifyExtensionLoaded(extension);
2109 DoPostLoadTasks(extension);
[email protected]f574c402012-12-04 23:20:312110 }
[email protected]aab98a52009-12-02 03:22:352111}
2112
[email protected]8c484b742012-11-29 06:05:362113void ExtensionService::AddComponentExtension(const Extension* extension) {
2114 const std::string old_version_string(
2115 extension_prefs_->GetVersionString(extension->id()));
2116 const Version old_version(old_version_string);
2117
2118 if (!old_version.IsValid() || !old_version.Equals(*extension->version())) {
2119 VLOG(1) << "Component extension " << extension->name() << " ("
2120 << extension->id() << ") installing/upgrading from '"
2121 << old_version_string << "' to " << extension->version()->GetString();
2122
2123 AddNewOrUpdatedExtension(extension,
[email protected]399583b2012-12-11 09:33:422124 Extension::ENABLED_COMPONENT,
2125 syncer::StringOrdinal());
[email protected]8c484b742012-11-29 06:05:362126 return;
2127 }
2128
2129 AddExtension(extension);
2130}
2131
[email protected]902fd7b2011-07-27 18:42:312132void ExtensionService::InitializePermissions(const Extension* extension) {
2133 // If the extension has used the optional permissions API, it will have a
2134 // custom set of active permissions defined in the extension prefs. Here,
2135 // we update the extension's active permissions based on the prefs.
[email protected]c2e66e12012-06-27 06:27:062136 scoped_refptr<PermissionSet> active_permissions =
[email protected]902fd7b2011-07-27 18:42:312137 extension_prefs()->GetActivePermissions(extension->id());
2138
2139 if (active_permissions.get()) {
2140 // We restrict the active permissions to be within the bounds defined in the
2141 // extension's manifest.
2142 // a) active permissions must be a subset of optional + default permissions
2143 // b) active permissions must contains all default permissions
[email protected]c2e66e12012-06-27 06:27:062144 scoped_refptr<PermissionSet> total_permissions =
2145 PermissionSet::CreateUnion(
[email protected]902fd7b2011-07-27 18:42:312146 extension->required_permission_set(),
2147 extension->optional_permission_set());
2148
2149 // Make sure the active permissions contain no more than optional + default.
[email protected]c2e66e12012-06-27 06:27:062150 scoped_refptr<PermissionSet> adjusted_active =
2151 PermissionSet::CreateIntersection(
[email protected]902fd7b2011-07-27 18:42:312152 total_permissions.get(), active_permissions.get());
2153
2154 // Make sure the active permissions contain the default permissions.
[email protected]c2e66e12012-06-27 06:27:062155 adjusted_active = PermissionSet::CreateUnion(
[email protected]902fd7b2011-07-27 18:42:312156 extension->required_permission_set(), adjusted_active.get());
2157
[email protected]c333e792012-01-06 16:57:392158 extensions::PermissionsUpdater perms_updater(profile());
2159 perms_updater.UpdateActivePermissions(extension, adjusted_active);
[email protected]902fd7b2011-07-27 18:42:312160 }
2161
[email protected]8d888c12010-11-30 00:00:252162 // We keep track of all permissions the user has granted each extension.
2163 // This allows extensions to gracefully support backwards compatibility
2164 // by including unknown permissions in their manifests. When the user
2165 // installs the extension, only the recognized permissions are recorded.
2166 // When the unknown permissions become recognized (e.g., through browser
2167 // upgrade), we can prompt the user to accept these new permissions.
2168 // Extensions can also silently upgrade to less permissions, and then
2169 // silently upgrade to a version that adds these permissions back.
2170 //
2171 // For example, pretend that Chrome 10 includes a permission "omnibox"
2172 // for an API that adds suggestions to the omnibox. An extension can
2173 // maintain backwards compatibility while still having "omnibox" in the
2174 // manifest. If a user installs the extension on Chrome 9, the browser
2175 // will record the permissions it recognized, not including "omnibox."
2176 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
2177 // will disable the extension and prompt the user to approve the increase
2178 // in privileges. The extension could then release a new version that
2179 // removes the "omnibox" permission. When the user upgrades, Chrome will
2180 // still remember that "omnibox" had been granted, so that if the
2181 // extension once again includes "omnibox" in an upgrade, the extension
2182 // can upgrade without requiring this user's approval.
[email protected]695b5712012-12-06 23:55:282183 const Extension* old = GetInstalledExtension(extension->id());
[email protected]8d888c12010-11-30 00:00:252184 bool is_extension_upgrade = old != NULL;
2185 bool is_privilege_increase = false;
[email protected]44d62b62012-04-11 00:06:032186 bool previously_disabled = false;
[email protected]eb5e4f92012-08-15 23:33:282187 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
[email protected]8d888c12010-11-30 00:00:252188
[email protected]902fd7b2011-07-27 18:42:312189 // We only need to compare the granted permissions to the current permissions
2190 // if the extension is not allowed to silently increase its permissions.
[email protected]be083862012-09-01 03:53:452191 bool is_default_app_install =
2192 (!is_extension_upgrade && extension->was_installed_by_default());
2193 if (!(extension->CanSilentlyIncreasePermissions()
2194 || is_default_app_install)) {
[email protected]8d888c12010-11-30 00:00:252195 // Add all the recognized permissions if the granted permissions list
2196 // hasn't been initialized yet.
[email protected]c2e66e12012-06-27 06:27:062197 scoped_refptr<PermissionSet> granted_permissions =
[email protected]902fd7b2011-07-27 18:42:312198 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:522199 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:252200
2201 // Here, we check if an extension's privileges have increased in a manner
2202 // that requires the user's approval. This could occur because the browser
2203 // upgraded and recognized additional privileges, or an extension upgrades
2204 // to a version that requires additional privileges.
[email protected]0d3e4a22011-06-23 19:02:522205 is_privilege_increase =
[email protected]902fd7b2011-07-27 18:42:312206 granted_permissions->HasLessPrivilegesThan(
2207 extension->GetActivePermissions());
[email protected]8d888c12010-11-30 00:00:252208 }
2209
[email protected]be083862012-09-01 03:53:452210 // Silently grant all active permissions to default apps only on install.
2211 // After install they should behave like other apps.
[email protected]c856e9762012-10-12 19:42:382212 if (is_default_app_install)
[email protected]009633c2013-03-07 22:08:282213 GrantPermissions(extension);
[email protected]be083862012-09-01 03:53:452214
[email protected]8d888c12010-11-30 00:00:252215 if (is_extension_upgrade) {
[email protected]66e26872010-12-03 20:07:252216 // Other than for unpacked extensions, CrxInstaller should have guaranteed
2217 // that we aren't downgrading.
[email protected]12075d12013-02-27 05:38:052218 if (!Manifest::IsUnpackedLocation(extension->location()))
[email protected]31bb5ee62012-09-12 22:58:402219 CHECK_GE(extension->version()->CompareTo(*(old->version())), 0);
[email protected]8d888c12010-11-30 00:00:252220
2221 // Extensions get upgraded if the privileges are allowed to increase or
2222 // the privileges haven't increased.
2223 if (!is_privilege_increase) {
2224 SetBeingUpgraded(old, true);
2225 SetBeingUpgraded(extension, true);
2226 }
2227
[email protected]44d62b62012-04-11 00:06:032228 // If the extension was already disabled, suppress any alerts for becoming
2229 // disabled on permissions increase.
2230 previously_disabled = extension_prefs_->IsExtensionDisabled(old->id());
[email protected]7e9e2422012-12-13 19:54:192231 // Legacy disabled extensions do not have a disable reason. Infer that if
2232 // there was no permission increase, it was likely disabled by the user.
2233 if (previously_disabled && disable_reasons == Extension::DISABLE_NONE &&
2234 !extension_prefs_->DidExtensionEscalatePermissions(old->id())) {
2235 disable_reasons |= Extension::DISABLE_USER_ACTION;
[email protected]44d62b62012-04-11 00:06:032236 }
[email protected]7e9e2422012-12-13 19:54:192237 // Extensions that came to us disabled from sync need a similar inference,
2238 // except based on the new version's permissions.
2239 if (previously_disabled &&
2240 disable_reasons == Extension::DISABLE_UNKNOWN_FROM_SYNC) {
2241 // Remove the DISABLE_UNKNOWN_FROM_SYNC reason.
2242 extension_prefs_->ClearDisableReasons(extension->id());
2243 if (!is_privilege_increase)
2244 disable_reasons |= Extension::DISABLE_USER_ACTION;
2245 }
2246 if (disable_reasons != Extension::DISABLE_NONE)
2247 disable_reasons &= ~Extension::DISABLE_UNKNOWN_FROM_SYNC;
[email protected]44d62b62012-04-11 00:06:032248
[email protected]8d888c12010-11-30 00:00:252249 // To upgrade an extension in place, unload the old one and
2250 // then load the new one.
[email protected]814a7bf0f2011-08-13 05:30:592251 UnloadExtension(old->id(), extension_misc::UNLOAD_REASON_UPDATE);
[email protected]8d888c12010-11-30 00:00:252252 old = NULL;
2253 }
2254
2255 // Extension has changed permissions significantly. Disable it. A
2256 // notification should be sent by the caller.
2257 if (is_privilege_increase) {
[email protected]7e9e2422012-12-13 19:54:192258 disable_reasons |= Extension::DISABLE_PERMISSIONS_INCREASE;
[email protected]fe2dd7742011-04-19 22:52:492259 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
2260 RecordPermissionMessagesHistogram(
2261 extension, "Extensions.Permissions_AutoDisable");
2262 }
[email protected]b3317ad2011-04-28 23:46:002263 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:252264 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
[email protected]eb5e4f92012-08-15 23:33:282265 extension_prefs_->AddDisableReason(
2266 extension->id(),
2267 static_cast<Extension::DisableReason>(disable_reasons));
[email protected]8d888c12010-11-30 00:00:252268 }
2269}
2270
[email protected]215a7be2012-10-22 19:53:422271void ExtensionService::MaybeWipeout(
2272 const extensions::Extension* extension) {
[email protected]1ff4d282012-12-08 00:39:212273 if (!wipeout_is_active_)
[email protected]215a7be2012-10-22 19:53:422274 return;
2275
[email protected]1d5e58b2013-01-31 08:41:402276 if (extension->GetType() != Manifest::TYPE_EXTENSION)
[email protected]215a7be2012-10-22 19:53:422277 return;
2278
[email protected]1d5e58b2013-01-31 08:41:402279 Manifest::Location location = extension->location();
2280 if (location != Manifest::EXTERNAL_REGISTRY)
[email protected]1ff4d282012-12-08 00:39:212281 return;
2282
2283 if (extension_prefs_->IsExternalExtensionExcludedFromWipeout(extension->id()))
2284 return;
2285
[email protected]215a7be2012-10-22 19:53:422286 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
[email protected]1ff4d282012-12-08 00:39:212287 if (disable_reasons == Extension::DISABLE_NONE) {
2288 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
2289 extension_prefs_->AddDisableReason(
2290 extension->id(),
2291 static_cast<Extension::DisableReason>(
2292 Extension::DISABLE_SIDELOAD_WIPEOUT));
2293 UMA_HISTOGRAM_BOOLEAN("DisabledExtension.ExtensionWipedStatus", true);
[email protected]3a2748c42013-01-15 10:13:232294 wipeout_count_++;
[email protected]215a7be2012-10-22 19:53:422295 }
2296}
2297
[email protected]eaa7dd182010-12-14 11:09:002298void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:392299 std::set<std::string> extension_ids;
[email protected]84df8332011-12-06 18:22:462300 for (ExtensionSet::const_iterator iter = extensions_.begin();
2301 iter != extensions_.end(); ++iter) {
2302 const Extension* extension = *iter;
[email protected]1d5e58b2013-01-31 08:41:402303 if (!extension->is_theme() && extension->location() != Manifest::COMPONENT)
[email protected]84df8332011-12-06 18:22:462304 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:352305 }
2306
2307 child_process_logging::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:252308}
2309
[email protected]9d02fa12013-02-19 05:12:572310void ExtensionService::ScheduleLaunchOnLoad(const std::string& extension_id) {
2311 on_load_events_[extension_id] = EVENT_LAUNCHED;
2312}
2313
[email protected]8266d662011-07-12 21:53:262314void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:502315 const Extension* extension,
[email protected]98270432012-09-11 20:51:242316 const syncer::StringOrdinal& page_ordinal,
[email protected]0db124b02012-11-07 04:55:052317 bool has_requirement_errors,
2318 bool wait_for_idle) {
[email protected]a29a517a2011-01-21 21:11:122319 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:202320
[email protected]c3cfb012011-04-06 22:07:352321 const std::string& id = extension->id();
[email protected]612a1cb12012-10-17 13:18:032322 bool initial_enable = ShouldEnableOnInstall(extension);
[email protected]3f213ad2012-07-26 23:39:412323 const extensions::PendingExtensionInfo* pending_extension_info = NULL;
[email protected]51a3bf8b2012-06-08 22:53:062324 if ((pending_extension_info = pending_extension_manager()->GetById(id))) {
2325 if (!pending_extension_info->ShouldAllowInstall(*extension)) {
2326 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:322327
[email protected]31bb5ee62012-09-12 22:58:402328 LOG(WARNING) << "ShouldAllowInstall() returned false for "
2329 << id << " of type " << extension->GetType()
[email protected]65348062013-01-15 07:27:222330 << " and update URL "
2331 << extensions::ManifestURL::GetUpdateURL(extension).spec()
[email protected]31bb5ee62012-09-12 22:58:402332 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:542333
[email protected]ad50def52011-10-19 23:17:072334 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272335 chrome::NOTIFICATION_EXTENSION_INSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:532336 content::Source<Profile>(profile_),
2337 content::Details<const Extension>(extension));
[email protected]ae6cb3022011-04-04 20:11:542338
[email protected]4416c5a2010-06-26 01:28:572339 // Delete the extension directory since we're not going to
2340 // load it.
[email protected]7f8f24f2012-11-15 19:40:142341 if (!GetFileTaskRunner()->PostTask(
2342 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362343 base::Bind(&extension_file_util::DeleteFile,
[email protected]31bb5ee62012-09-12 22:58:402344 extension->path(), true))) {
[email protected]14908b72011-04-20 06:54:362345 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:402346 }
[email protected]4416c5a2010-06-26 01:28:572347 return;
2348 }
[email protected]51a3bf8b2012-06-08 22:53:062349
2350 pending_extension_manager()->Remove(id);
[email protected]4416c5a2010-06-26 01:28:572351 } else {
[email protected]c3cfb012011-04-06 22:07:352352 // We explicitly want to re-enable an uninstalled external
2353 // extension; if we're here, that means the user is manually
2354 // installing the extension.
[email protected]6cc7dbae2011-04-29 21:18:332355 if (IsExternalExtensionUninstalled(id)) {
2356 initial_enable = true;
2357 }
[email protected]aa142702010-03-26 01:26:332358 }
2359
[email protected]98270432012-09-11 20:51:242360 // Unsupported requirements overrides the management policy.
2361 if (has_requirement_errors) {
2362 initial_enable = false;
2363 extension_prefs_->AddDisableReason(
2364 id, Extension::DISABLE_UNSUPPORTED_REQUIREMENT);
2365 // If the extension was disabled because of unsupported requirements but
2366 // now supports all requirements after an update and there are not other
2367 // disable reasons, enable it.
2368 } else if (extension_prefs_->GetDisableReasons(id) ==
2369 Extension::DISABLE_UNSUPPORTED_REQUIREMENT) {
2370 initial_enable = true;
2371 extension_prefs_->ClearDisableReasons(id);
2372 }
2373
[email protected]695b5712012-12-06 23:55:282374 if (!GetInstalledExtension(extension->id())) {
[email protected]07533022011-06-27 20:42:552375 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
2376 extension->GetType(), 100);
[email protected]69084dc2012-11-17 07:39:302377 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallSource",
[email protected]1d5e58b2013-01-31 08:41:402378 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552379 RecordPermissionMessagesHistogram(
2380 extension, "Extensions.Permissions_Install");
[email protected]69084dc2012-11-17 07:39:302381 } else {
2382 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateType",
2383 extension->GetType(), 100);
2384 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateSource",
[email protected]1d5e58b2013-01-31 08:41:402385 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552386 }
2387
[email protected]a39921b42012-02-28 03:42:542388 // Certain extension locations are specific enough that we can
2389 // auto-acknowledge any extension that came from one of them.
[email protected]1d5e58b2013-01-31 08:41:402390 if (extension->location() == Manifest::EXTERNAL_POLICY_DOWNLOAD)
[email protected]a39921b42012-02-28 03:42:542391 AcknowledgeExternalExtension(extension->id());
[email protected]399583b2012-12-11 09:33:422392 const Extension::State initial_state =
2393 initial_enable ? Extension::ENABLED : Extension::DISABLED;
[email protected]e7aa7b7e2012-11-27 04:51:222394 if (ShouldDelayExtensionUpdate(id, wait_for_idle)) {
[email protected]399583b2012-12-11 09:33:422395 extension_prefs_->SetDelayedInstallInfo(extension, initial_state,
2396 page_ordinal);
[email protected]0db124b02012-11-07 04:55:052397
2398 // Transfer ownership of |extension|.
[email protected]399583b2012-12-11 09:33:422399 delayed_updates_for_idle_.Insert(extension);
[email protected]0db124b02012-11-07 04:55:052400
2401 // Notify extension of available update.
2402 extensions::RuntimeEventRouter::DispatchOnUpdateAvailableEvent(
2403 profile_, id, extension->manifest()->value());
[email protected]75bdcb872013-03-13 00:41:452404
2405 // Notify observers that app update is available.
2406 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2407 OnAppUpdateAvailable(extension->id()));
[email protected]0db124b02012-11-07 04:55:052408 return;
2409 }
2410
[email protected]399583b2012-12-11 09:33:422411 if (installs_delayed()) {
2412 extension_prefs_->SetDelayedInstallInfo(extension, initial_state,
2413 page_ordinal);
2414 delayed_installs_.Insert(extension);
2415 } else {
2416 AddNewOrUpdatedExtension(extension, initial_state, page_ordinal);
2417 }
[email protected]8c484b742012-11-29 06:05:362418}
2419
2420void ExtensionService::AddNewOrUpdatedExtension(
2421 const Extension* extension,
[email protected]399583b2012-12-11 09:33:422422 Extension::State initial_state,
2423 const syncer::StringOrdinal& page_ordinal) {
[email protected]8c484b742012-11-29 06:05:362424 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2425
[email protected]4416c5a2010-06-26 01:28:572426 extension_prefs_->OnExtensionInstalled(
[email protected]8266d662011-07-12 21:53:262427 extension,
[email protected]8c484b742012-11-29 06:05:362428 initial_state,
[email protected]36a5c4c2011-12-14 16:34:502429 page_ordinal);
[email protected]25b34332009-06-05 21:53:192430
[email protected]6f6101832012-11-27 22:10:482431 FinishInstallation(extension);
2432}
2433
2434void ExtensionService::MaybeFinishDelayedInstallation(
2435 const std::string& extension_id) {
2436 // Check if the extension already got updated.
[email protected]399583b2012-12-11 09:33:422437 if (!delayed_updates_for_idle_.Contains(extension_id))
[email protected]6f6101832012-11-27 22:10:482438 return;
2439 // Check if the extension is idle.
2440 if (!IsExtensionIdle(extension_id))
2441 return;
2442
2443 FinishDelayedInstallation(extension_id);
2444}
2445
2446void ExtensionService::FinishDelayedInstallation(
2447 const std::string& extension_id) {
2448 scoped_refptr<const Extension> extension(
2449 GetPendingExtensionUpdate(extension_id));
2450 CHECK(extension);
[email protected]399583b2012-12-11 09:33:422451 delayed_updates_for_idle_.Remove(extension_id);
[email protected]6f6101832012-11-27 22:10:482452
[email protected]399583b2012-12-11 09:33:422453 if (!extension_prefs_->FinishDelayedInstallInfo(extension_id))
[email protected]6f6101832012-11-27 22:10:482454 NOTREACHED();
2455
2456 FinishInstallation(extension);
2457}
2458
2459void ExtensionService::FinishInstallation(const Extension* extension) {
[email protected]fcb58a862012-05-01 01:03:152460 content::NotificationService::current()->Notify(
2461 chrome::NOTIFICATION_EXTENSION_INSTALLED,
2462 content::Source<Profile>(profile_),
2463 content::Details<const Extension>(extension));
2464
[email protected]d96eb512012-11-01 23:44:082465 bool unacknowledged_external = IsUnacknowledgedExternalExtension(extension);
[email protected]41070e8d2012-10-24 01:34:362466
[email protected]399583b2012-12-11 09:33:422467 // Unpacked extensions default to allowing file access, but if that has been
2468 // overridden, don't reset the value.
[email protected]1d5e58b2013-01-31 08:41:402469 if (Manifest::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]399583b2012-12-11 09:33:422470 !extension_prefs_->HasAllowFileAccessSetting(extension->id())) {
2471 extension_prefs_->SetAllowFileAccess(extension->id(), true);
2472 }
2473
[email protected]6f6101832012-11-27 22:10:482474 AddExtension(extension);
[email protected]612a1cb12012-10-17 13:18:032475
[email protected]a51c9e92012-12-09 09:19:202476#if defined(ENABLE_THEMES)
[email protected]bc151cf92013-02-12 04:57:262477 // We do this here since AddExtension() is always called on browser startup,
2478 // and we only really care about the last theme installed.
[email protected]a51c9e92012-12-09 09:19:202479 // If that ever changes and we have to move this code somewhere
2480 // else, it should be somewhere that's not in the startup path.
2481 if (extension->is_theme() && extensions_.GetByID(extension->id())) {
2482 DCHECK_EQ(extensions_.GetByID(extension->id()), extension);
2483 // Now that the theme extension is visible from outside the
2484 // ExtensionService, notify the ThemeService about the
2485 // newly-installed theme.
2486 ThemeServiceFactory::GetForProfile(profile_)->SetTheme(extension);
2487 }
2488#endif
2489
[email protected]612a1cb12012-10-17 13:18:032490 // If this is a new external extension that was disabled, alert the user
[email protected]d96eb512012-11-01 23:44:082491 // so he can reenable it. We do this last so that it has already been
2492 // added to our list of extensions.
2493 if (unacknowledged_external) {
[email protected]612a1cb12012-10-17 13:18:032494 UpdateExternalExtensionAlert();
[email protected]d96eb512012-11-01 23:44:082495 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
2496 EXTERNAL_EXTENSION_INSTALLED,
2497 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
2498 }
[email protected]4a190632009-05-09 01:07:422499}
2500
[email protected]76b65442012-11-17 14:11:482501const Extension* ExtensionService::GetPendingExtensionUpdate(
2502 const std::string& id) const {
[email protected]399583b2012-12-11 09:33:422503 return delayed_updates_for_idle_.GetByID(id);
[email protected]76b65442012-11-17 14:11:482504}
2505
[email protected]bb7f40952011-01-13 00:21:202506void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
[email protected]84df8332011-12-06 18:22:462507 if (!terminated_extensions_.Contains(extension->id()))
2508 terminated_extensions_.Insert(make_scoped_refptr(extension));
[email protected]fa2416f2011-05-03 08:41:202509
[email protected]b3f7fe22011-11-11 19:27:562510 UnloadExtension(extension->id(), extension_misc::UNLOAD_REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:202511}
2512
2513void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]fa2416f2011-05-03 08:41:202514 std::string lowercase_id = StringToLowerASCII(id);
[email protected]84df8332011-12-06 18:22:462515 terminated_extensions_.Remove(lowercase_id);
[email protected]bb7f40952011-01-13 00:21:202516}
2517
[email protected]0dfe05c2011-02-23 23:03:362518const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:472519 const std::string& id) const {
[email protected]695b5712012-12-06 23:55:282520 return GetExtensionById(id, INCLUDE_TERMINATED);
[email protected]8001df22011-04-28 19:59:472521}
2522
2523const Extension* ExtensionService::GetInstalledExtension(
2524 const std::string& id) const {
[email protected]695b5712012-12-06 23:55:282525 int include_mask = INCLUDE_ENABLED |
2526 INCLUDE_DISABLED |
2527 INCLUDE_TERMINATED |
2528 INCLUDE_BLACKLISTED;
2529 return GetExtensionById(id, include_mask);
[email protected]0dfe05c2011-02-23 23:03:362530}
2531
[email protected]eaa7dd182010-12-14 11:09:002532bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:442533 // Allow bindings for all packaged extensions and component hosted apps.
2534 const Extension* extension = extensions_.GetExtensionOrAppByURL(
2535 ExtensionURLInfo(url));
2536 return extension && (!extension->is_hosted_app() ||
[email protected]1d5e58b2013-01-31 08:41:402537 extension->location() == Manifest::COMPONENT);
[email protected]6d2e60bd2010-06-03 22:37:392538}
2539
[email protected]4d007b312012-10-17 03:00:482540bool ExtensionService::ShouldBlockUrlInBrowserTab(GURL* url) {
2541 const Extension* extension = extensions_.GetExtensionOrAppByURL(
2542 ExtensionURLInfo(*url));
2543 if (extension && extension->is_platform_app()) {
2544 *url = GURL(chrome::kExtensionInvalidRequestURL);
2545 return true;
2546 }
2547
2548 return false;
2549}
2550
[email protected]9060d8b02012-01-13 02:14:302551bool ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322552 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:142553 const Version* version,
[email protected]650b2d52013-02-10 03:41:452554 const base::FilePath& path,
[email protected]1d5e58b2013-01-31 08:41:402555 Manifest::Location location,
[email protected]47fc70c2011-12-06 07:29:512556 int creation_flags,
2557 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:382558 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2559 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012560 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]9060d8b02012-01-13 02:14:302561 return false;
[email protected]a8af9fdb2010-10-28 21:52:202562
[email protected]7577a5c52009-07-30 06:21:582563 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492564 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582565 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022566 const Extension* existing = GetExtensionById(id, true);
[email protected]910f72ce2012-08-24 01:38:352567
[email protected]7577a5c52009-07-30 06:21:582568 if (existing) {
[email protected]910f72ce2012-08-24 01:38:352569 // The default apps will have the location set as INTERNAL. Since older
2570 // default apps are installed as EXTERNAL, we override them. However, if the
2571 // app is already installed as internal, then do the version check.
[email protected]31bb5ee62012-09-12 22:58:402572 // TODO(grv) : Remove after Q1-2013.
[email protected]910f72ce2012-08-24 01:38:352573 bool is_default_apps_migration =
[email protected]1d5e58b2013-01-31 08:41:402574 (location == Manifest::INTERNAL &&
2575 Manifest::IsExternalLocation(existing->location()));
[email protected]910f72ce2012-08-24 01:38:352576
2577 if (!is_default_apps_migration) {
2578 DCHECK(version);
2579
2580 switch (existing->version()->CompareTo(*version)) {
2581 case -1: // existing version is older, we should upgrade
2582 break;
2583 case 0: // existing version is same, do nothing
2584 return false;
2585 case 1: // existing version is newer, uh-oh
2586 LOG(WARNING) << "Found external version of extension " << id
2587 << "that is older than current version. Current version "
2588 << "is: " << existing->VersionString() << ". New "
2589 << "version is: " << version->GetString()
2590 << ". Keeping current version.";
2591 return false;
2592 }
[email protected]7577a5c52009-07-30 06:21:582593 }
2594 }
2595
[email protected]9060d8b02012-01-13 02:14:302596 // If the extension is already pending, don't start an install.
[email protected]e3987852012-05-04 10:06:302597 if (!pending_extension_manager()->AddFromExternalFile(
2598 id, location, *version)) {
[email protected]9060d8b02012-01-13 02:14:302599 return false;
[email protected]e3987852012-05-04 10:06:302600 }
[email protected]9c635f22010-12-02 09:36:362601
[email protected]14908b72011-04-20 06:54:362602 // no client (silent install)
[email protected]d8c8f25f2011-11-02 18:18:012603 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(this, NULL));
[email protected]6dfbbf82010-03-12 23:09:162604 installer->set_install_source(location);
2605 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072606 installer->set_expected_version(*version);
2607 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]1bf73cc32011-10-26 22:38:312608 installer->set_creation_flags(creation_flags);
[email protected]88e8ec9152013-01-17 04:05:182609#if defined(OS_CHROMEOS)
2610 extensions::InstallLimiter::Get(profile_)->Add(installer, path);
2611#else
[email protected]6dfbbf82010-03-12 23:09:162612 installer->InstallCrx(path);
[email protected]88e8ec9152013-01-17 04:05:182613#endif
[email protected]47fc70c2011-12-06 07:29:512614
2615 // Depending on the source, a new external extension might not need a user
2616 // notification on installation. For such extensions, mark them acknowledged
2617 // now to suppress the notification.
2618 if (mark_acknowledged)
2619 AcknowledgeExternalExtension(id);
[email protected]9060d8b02012-01-13 02:14:302620
2621 return true;
[email protected]7577a5c52009-07-30 06:21:582622}
2623
[email protected]eaa7dd182010-12-14 11:09:002624void ExtensionService::ReportExtensionLoadError(
[email protected]650b2d52013-02-10 03:41:452625 const base::FilePath& extension_path,
[email protected]d11c8e92009-10-20 23:26:402626 const std::string &error,
[email protected]d11c8e92009-10-20 23:26:402627 bool be_noisy) {
[email protected]d8c8f25f2011-11-02 18:18:012628 content::NotificationService::current()->Notify(
2629 chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
2630 content::Source<Profile>(profile_),
2631 content::Details<const std::string>(&error));
[email protected]d11c8e92009-10-20 23:26:402632
[email protected]8a205c02011-02-04 20:41:332633 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]438afb12012-07-05 03:18:212634 string16 message = UTF8ToUTF16(base::StringPrintf(
[email protected]18d4b6c2010-09-21 03:21:042635 "Could not load extension from '%s'. %s",
[email protected]fc670822011-12-17 09:33:492636 path_str.c_str(), error.c_str()));
[email protected]d11c8e92009-10-20 23:26:402637 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2638}
2639
[email protected]eaa7dd182010-12-14 11:09:002640void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]3a1dc572012-07-31 22:25:132641 extensions::ExtensionHost* host) {
[email protected]406027c02010-09-27 08:03:182642 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332643 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182644 if (iter == orphaned_dev_tools_.end())
2645 return;
2646
[email protected]98f66112012-12-25 12:59:362647 DevToolsAgentHost::ConnectRenderViewHost(iter->second,
2648 host->render_view_host());
[email protected]406027c02010-09-27 08:03:182649 orphaned_dev_tools_.erase(iter);
2650}
2651
[email protected]432115822011-07-10 15:52:272652void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532653 const content::NotificationSource& source,
2654 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272655 switch (type) {
[email protected]3c4abc82012-10-22 22:25:542656 case chrome::NOTIFICATION_APP_TERMINATING:
2657 // Shutdown has started. Don't start any more extension installs.
2658 // (We cannot use ExtensionService::Shutdown() for this because it
2659 // happens too late in browser teardown.)
2660 browser_terminating_ = true;
2661 break;
[email protected]432115822011-07-10 15:52:272662 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532663 if (profile_ !=
2664 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322665 break;
[email protected]6c2381d2011-10-19 02:52:532666 }
[email protected]a4ed6282009-12-14 20:51:162667
[email protected]3a1dc572012-07-31 22:25:132668 extensions::ExtensionHost* host =
2669 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262670
[email protected]fa2416f2011-05-03 08:41:202671 // Mark the extension as terminated and Unload it. We want it to
2672 // be in a consistent state: either fully working or not loaded
2673 // at all, but never half-crashed. We do it in a PostTask so
2674 // that other handlers of this notification will still have
2675 // access to the Extension and ExtensionHost.
[email protected]14908b72011-04-20 06:54:362676 MessageLoop::current()->PostTask(
2677 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362678 base::Bind(
[email protected]fa2416f2011-05-03 08:41:202679 &ExtensionService::TrackTerminatedExtension,
[email protected]d8c8f25f2011-11-02 18:18:012680 AsWeakPtr(),
[email protected]fa2416f2011-05-03 08:41:202681 host->extension()));
[email protected]31f77262009-12-02 20:48:532682 break;
2683 }
[email protected]432115822011-07-10 15:52:272684 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
[email protected]f3b1a082011-11-18 00:34:302685 content::RenderProcessHost* process =
2686 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192687 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302688 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192689 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2690 break;
2691
[email protected]15397b42012-05-16 23:56:062692 // Extensions need to know the channel for API restrictions.
2693 process->Send(new ExtensionMsg_SetChannel(
[email protected]5243df072012-07-11 21:41:012694 extensions::Feature::GetCurrentChannel()));
[email protected]15397b42012-05-16 23:56:062695
[email protected]77a6970c2011-04-23 16:58:562696 // Valid extension function names, used to setup bindings in renderer.
2697 std::vector<std::string> function_names;
2698 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names);
2699 process->Send(new ExtensionMsg_SetFunctionNames(function_names));
[email protected]31f77262009-12-02 20:48:532700
[email protected]77a6970c2011-04-23 16:58:562701 // Scripting whitelist. This is modified by tests and must be communicated
2702 // to renderers.
2703 process->Send(new ExtensionMsg_SetScriptingWhitelist(
2704 *Extension::GetScriptingWhitelist()));
2705
2706 // Loaded extensions.
[email protected]9776e82e2011-11-15 02:17:532707 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions;
[email protected]84df8332011-12-06 18:22:462708 for (ExtensionSet::const_iterator iter = extensions_.begin();
2709 iter != extensions_.end(); ++iter) {
[email protected]19647262011-12-16 09:57:492710 // Renderers don't need to know about themes.
2711 if (!(*iter)->is_theme())
2712 loaded_extensions.push_back(ExtensionMsg_Loaded_Params(*iter));
[email protected]77a6970c2011-04-23 16:58:562713 }
[email protected]9776e82e2011-11-15 02:17:532714 process->Send(new ExtensionMsg_Loaded(loaded_extensions));
[email protected]77a6970c2011-04-23 16:58:562715 break;
2716 }
[email protected]432115822011-07-10 15:52:272717 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302718 content::RenderProcessHost* process =
2719 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192720 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302721 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192722 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2723 break;
2724
[email protected]6bc04fd82011-12-04 02:29:352725 process_map_.RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:462726 BrowserThread::PostTask(
2727 BrowserThread::IO, FROM_HERE,
2728 base::Bind(&ExtensionInfoMap::UnregisterAllExtensionsInProcess,
[email protected]31d8f5f22012-04-02 15:22:082729 system_->info_map(),
[email protected]f3b1a082011-11-18 00:34:302730 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212731 break;
2732 }
[email protected]d91268022011-08-26 13:17:372733 case chrome::NOTIFICATION_IMPORT_FINISHED: {
[email protected]cf593af2011-12-30 05:44:392734 InitAfterImport();
[email protected]d91268022011-08-26 13:17:372735 break;
2736 }
[email protected]0db124b02012-11-07 04:55:052737 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: {
2738 extensions::ExtensionHost* host =
2739 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]e7aa7b7e2012-11-27 04:51:222740 std::string extension_id = host->extension_id();
[email protected]399583b2012-12-11 09:33:422741 if (delayed_updates_for_idle_.Contains(extension_id)) {
[email protected]e7aa7b7e2012-11-27 04:51:222742 // We were waiting for this extension to become idle, it now might have,
2743 // so maybe finish installation.
2744 MessageLoop::current()->PostDelayedTask(
[email protected]0db124b02012-11-07 04:55:052745 FROM_HERE,
[email protected]6f6101832012-11-27 22:10:482746 base::Bind(&ExtensionService::MaybeFinishDelayedInstallation,
[email protected]0db124b02012-11-07 04:55:052747 AsWeakPtr(), extension_id),
2748 base::TimeDelta::FromSeconds(kUpdateIdleDelay));
[email protected]0db124b02012-11-07 04:55:052749 }
2750 break;
2751 }
[email protected]92dd8d92013-02-26 00:41:082752 case chrome::NOTIFICATION_UPGRADE_RECOMMENDED: {
2753 // Notify extensions that chrome update is available.
2754 extensions::RuntimeEventRouter::DispatchOnBrowserUpdateAvailableEvent(
2755 profile_);
[email protected]75bdcb872013-03-13 00:41:452756
2757 // Notify observers that chrome update is available.
2758 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2759 OnChromeUpdateAvailable());
[email protected]92dd8d92013-02-26 00:41:082760 break;
2761 }
[email protected]aa96d3a2010-08-21 08:45:252762
[email protected]4814b512009-11-07 00:12:292763 default:
2764 NOTREACHED() << "Unexpected notification type.";
2765 }
2766}
2767
[email protected]90bb38d2012-11-14 18:36:032768void ExtensionService::OnExtensionInstallPrefChanged() {
[email protected]a6a7ced2012-11-01 17:24:182769 IdentifyAlertableExtensions();
2770 CheckManagementPolicy();
2771}
2772
[email protected]eaa7dd182010-12-14 11:09:002773bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182774 return !GetAppIds().empty();
2775}
[email protected]377011d2010-07-20 04:18:502776
[email protected]eaa7dd182010-12-14 11:09:002777ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182778 ExtensionIdSet result;
[email protected]84df8332011-12-06 18:22:462779 for (ExtensionSet::const_iterator it = extensions_.begin();
[email protected]377011d2010-07-20 04:18:502780 it != extensions_.end(); ++it) {
[email protected]1d5e58b2013-01-31 08:41:402781 if ((*it)->is_app() && (*it)->location() != Manifest::COMPONENT)
[email protected]ec5b50d2010-10-09 16:35:182782 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502783 }
2784
[email protected]ec5b50d2010-10-09 16:35:182785 return result;
[email protected]377011d2010-07-20 04:18:502786}
[email protected]d7e9a862010-11-03 21:57:492787
[email protected]dc9a74f72012-08-17 18:07:212788bool ExtensionService::IsBackgroundPageReady(const Extension* extension) const {
[email protected]9367eabc2013-03-01 01:29:292789 if (!extensions::BackgroundInfo::HasPersistentBackgroundPage(extension))
[email protected]dc9a74f72012-08-17 18:07:212790 return true;
2791 ExtensionRuntimeDataMap::const_iterator it =
2792 extension_runtime_data_.find(extension->id());
2793 return it == extension_runtime_data_.end() ? false :
2794 it->second.background_page_ready;
[email protected]d7e9a862010-11-03 21:57:492795}
2796
[email protected]eaa7dd182010-12-14 11:09:002797void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]9367eabc2013-03-01 01:29:292798 DCHECK(extensions::BackgroundInfo::HasBackgroundPage(extension));
[email protected]d7e9a862010-11-03 21:57:492799 extension_runtime_data_[extension->id()].background_page_ready = true;
[email protected]ad50def52011-10-19 23:17:072800 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272801 chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
[email protected]6c2381d2011-10-19 02:52:532802 content::Source<const Extension>(extension),
[email protected]ad50def52011-10-19 23:17:072803 content::NotificationService::NoDetails());
[email protected]d7e9a862010-11-03 21:57:492804}
2805
[email protected]65992d52012-09-19 23:05:312806void ExtensionService::InspectBackgroundPage(const Extension* extension) {
2807 DCHECK(extension);
2808
[email protected]fdd679b2012-11-15 20:49:392809 ExtensionProcessManager* pm = system_->process_manager();
[email protected]65992d52012-09-19 23:05:312810 extensions::LazyBackgroundTaskQueue* queue =
[email protected]fdd679b2012-11-15 20:49:392811 system_->lazy_background_task_queue();
[email protected]65992d52012-09-19 23:05:312812
2813 extensions::ExtensionHost* host =
2814 pm->GetBackgroundHostForExtension(extension->id());
2815 if (host) {
2816 InspectExtensionHost(host);
2817 } else {
2818 queue->AddPendingTask(
2819 profile_, extension->id(),
2820 base::Bind(&ExtensionService::InspectExtensionHost,
2821 base::Unretained(this)));
2822 }
2823}
2824
[email protected]dc9a74f72012-08-17 18:07:212825bool ExtensionService::IsBeingUpgraded(const Extension* extension) const {
2826 ExtensionRuntimeDataMap::const_iterator it =
2827 extension_runtime_data_.find(extension->id());
2828 return it == extension_runtime_data_.end() ? false :
2829 it->second.being_upgraded;
[email protected]d7e9a862010-11-03 21:57:492830}
2831
[email protected]eaa7dd182010-12-14 11:09:002832void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]d7e9a862010-11-03 21:57:492833 bool value) {
2834 extension_runtime_data_[extension->id()].being_upgraded = value;
2835}
[email protected]1bead0712010-11-27 17:41:532836
[email protected]dc9a74f72012-08-17 18:07:212837bool ExtensionService::HasUsedWebRequest(const Extension* extension) const {
2838 ExtensionRuntimeDataMap::const_iterator it =
2839 extension_runtime_data_.find(extension->id());
2840 return it == extension_runtime_data_.end() ? false :
2841 it->second.has_used_webrequest;
[email protected]39a5b532011-10-22 01:47:072842}
2843
2844void ExtensionService::SetHasUsedWebRequest(const Extension* extension,
2845 bool value) {
2846 extension_runtime_data_[extension->id()].has_used_webrequest = value;
2847}
2848
[email protected]cebc3dc2011-04-18 17:15:002849void ExtensionService::RegisterNaClModule(const GURL& url,
2850 const std::string& mime_type) {
2851 NaClModuleInfo info;
2852 info.url = url;
2853 info.mime_type = mime_type;
2854
2855 DCHECK(FindNaClModule(url) == nacl_module_list_.end());
2856 nacl_module_list_.push_front(info);
2857}
2858
2859void ExtensionService::UnregisterNaClModule(const GURL& url) {
2860 NaClModuleInfoList::iterator iter = FindNaClModule(url);
2861 DCHECK(iter != nacl_module_list_.end());
2862 nacl_module_list_.erase(iter);
2863}
2864
[email protected]ed0ba002011-05-26 16:55:132865void ExtensionService::UpdatePluginListWithNaClModules() {
[email protected]300c0ea2011-07-15 23:04:332866 // An extension has been added which has a nacl_module component, which means
2867 // there is a MIME type that module wants to handle, so we need to add that
2868 // MIME type to plugins which handle NaCl modules in order to allow the
2869 // individual modules to handle these types.
[email protected]650b2d52013-02-10 03:41:452870 base::FilePath path;
[email protected]1e127022011-11-29 21:11:472871 if (!PathService::Get(chrome::FILE_NACL_PLUGIN, &path))
2872 return;
2873 const content::PepperPluginInfo* pepper_info =
2874 PluginService::GetInstance()->GetRegisteredPpapiPluginInfo(path);
2875 if (!pepper_info)
2876 return;
[email protected]300c0ea2011-07-15 23:04:332877
[email protected]1e127022011-11-29 21:11:472878 std::vector<webkit::WebPluginMimeType>::const_iterator mime_iter;
2879 // Check each MIME type the plugins handle for the NaCl MIME type.
2880 for (mime_iter = pepper_info->mime_types.begin();
2881 mime_iter != pepper_info->mime_types.end(); ++mime_iter) {
2882 if (mime_iter->mime_type == kNaClPluginMimeType) {
2883 // This plugin handles "application/x-nacl".
[email protected]300c0ea2011-07-15 23:04:332884
[email protected]1e127022011-11-29 21:11:472885 PluginService::GetInstance()->
2886 UnregisterInternalPlugin(pepper_info->path);
[email protected]300c0ea2011-07-15 23:04:332887
[email protected]1e127022011-11-29 21:11:472888 webkit::WebPluginInfo info = pepper_info->ToWebPluginInfo();
[email protected]300c0ea2011-07-15 23:04:332889
[email protected]1e127022011-11-29 21:11:472890 for (ExtensionService::NaClModuleInfoList::const_iterator iter =
2891 nacl_module_list_.begin();
2892 iter != nacl_module_list_.end(); ++iter) {
2893 // Add the MIME type specified in the extension to this NaCl plugin,
2894 // With an extra "nacl" argument to specify the location of the NaCl
2895 // manifest file.
2896 webkit::WebPluginMimeType mime_type_info;
2897 mime_type_info.mime_type = iter->mime_type;
2898 mime_type_info.additional_param_names.push_back(UTF8ToUTF16("nacl"));
2899 mime_type_info.additional_param_values.push_back(
2900 UTF8ToUTF16(iter->url.spec()));
2901 info.mime_types.push_back(mime_type_info);
[email protected]300c0ea2011-07-15 23:04:332902 }
[email protected]1e127022011-11-29 21:11:472903
2904 PluginService::GetInstance()->RefreshPlugins();
[email protected]c6f3dea2012-01-14 02:23:112905 PluginService::GetInstance()->RegisterInternalPlugin(info, true);
[email protected]1e127022011-11-29 21:11:472906 // This plugin has been modified, no need to check the rest of its
2907 // types, but continue checking other plugins.
2908 break;
[email protected]ed0ba002011-05-26 16:55:132909 }
2910 }
[email protected]ed0ba002011-05-26 16:55:132911}
2912
[email protected]cebc3dc2011-04-18 17:15:002913ExtensionService::NaClModuleInfoList::iterator
2914 ExtensionService::FindNaClModule(const GURL& url) {
2915 for (NaClModuleInfoList::iterator iter = nacl_module_list_.begin();
2916 iter != nacl_module_list_.end(); ++iter) {
2917 if (iter->url == url)
2918 return iter;
2919 }
2920 return nacl_module_list_.end();
2921}
[email protected]6cafe35a2012-08-27 22:40:502922
[email protected]32b36c62012-10-24 05:37:412923void ExtensionService::DoPostLoadTasks(const Extension* extension) {
2924 std::map<std::string, int>::iterator it =
2925 on_load_events_.find(extension->id());
2926 if (it == on_load_events_.end())
2927 return;
2928
2929 int events_to_fire = it->second;
2930 extensions::LazyBackgroundTaskQueue* queue =
2931 system_->lazy_background_task_queue();
2932 if (queue->ShouldEnqueueTask(profile(), extension)) {
2933 if (events_to_fire & EVENT_LAUNCHED)
[email protected]6cafe35a2012-08-27 22:40:502934 queue->AddPendingTask(profile(), extension->id(),
2935 base::Bind(&ExtensionService::LaunchApplication));
[email protected]fc2a40f2013-03-13 13:14:572936 if (events_to_fire & EVENT_RESTARTED) {
2937 SavedFileEntryMap::iterator it =
2938 on_restart_file_entries_.find(extension->id());
2939 if (it == on_restart_file_entries_.end())
2940 NOTREACHED();
[email protected]32b36c62012-10-24 05:37:412941 queue->AddPendingTask(profile(), extension->id(),
[email protected]fc2a40f2013-03-13 13:14:572942 base::Bind(&ExtensionService::RestartApplication,
2943 it->second));
2944 on_restart_file_entries_.erase(it);
2945 }
[email protected]6cafe35a2012-08-27 22:40:502946 }
[email protected]32b36c62012-10-24 05:37:412947
2948 on_load_events_.erase(it);
[email protected]6cafe35a2012-08-27 22:40:502949}
2950
2951// static
2952void ExtensionService::LaunchApplication(
2953 extensions::ExtensionHost* extension_host) {
2954 if (!extension_host)
2955 return;
2956
2957#if !defined(OS_ANDROID)
2958 extensions::LaunchPlatformApp(extension_host->profile(),
2959 extension_host->extension(),
[email protected]650b2d52013-02-10 03:41:452960 NULL, base::FilePath());
[email protected]6cafe35a2012-08-27 22:40:502961#endif
2962}
[email protected]65992d52012-09-19 23:05:312963
[email protected]32b36c62012-10-24 05:37:412964// static
2965void ExtensionService::RestartApplication(
[email protected]fc2a40f2013-03-13 13:14:572966 std::vector<extensions::app_file_handler_util::SavedFileEntry> file_entries,
[email protected]32b36c62012-10-24 05:37:412967 extensions::ExtensionHost* extension_host) {
2968 if (!extension_host)
2969 return;
2970
2971#if !defined(OS_ANDROID)
[email protected]fc2a40f2013-03-13 13:14:572972 extensions::RestartPlatformAppWithFileEntries(
2973 extension_host->profile(), extension_host->extension(), file_entries);
[email protected]32b36c62012-10-24 05:37:412974#endif
2975}
2976
2977bool ExtensionService::HasShellWindows(const std::string& extension_id) {
2978 const Extension* current_extension = GetExtensionById(extension_id, false);
2979 return current_extension && current_extension->is_platform_app() &&
2980 !extensions::ShellWindowRegistry::Get(profile_)->
2981 GetShellWindowsForApp(extension_id).empty();
2982}
2983
[email protected]65992d52012-09-19 23:05:312984void ExtensionService::InspectExtensionHost(
2985 extensions::ExtensionHost* host) {
2986 if (host)
2987 DevToolsWindow::OpenDevToolsWindow(host->render_view_host());
2988}
[email protected]612a1cb12012-10-17 13:18:032989
2990bool ExtensionService::ShouldEnableOnInstall(const Extension* extension) {
2991 // Extensions installed by policy can't be disabled. So even if a previous
2992 // installation disabled the extension, make sure it is now enabled.
2993 if (system_->management_policy()->MustRemainEnabled(extension, NULL))
2994 return true;
2995
2996 if (extension_prefs_->IsExtensionDisabled(extension->id()))
2997 return false;
2998
[email protected]41070e8d2012-10-24 01:34:362999 if (FeatureSwitch::prompt_for_external_extensions()->IsEnabled()) {
[email protected]612a1cb12012-10-17 13:18:033000 // External extensions are initially disabled. We prompt the user before
[email protected]2c495c42013-01-04 21:49:543001 // enabling them. Hosted apps are excepted because they are not dangerous
3002 // (they need to be launched by the user anyway).
[email protected]1d5e58b2013-01-31 08:41:403003 if (extension->GetType() != Manifest::TYPE_HOSTED_APP &&
3004 Manifest::IsExternalLocation(extension->location()) &&
[email protected]612a1cb12012-10-17 13:18:033005 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id())) {
3006 return false;
3007 }
3008 }
[email protected]612a1cb12012-10-17 13:18:033009
3010 return true;
3011}
[email protected]0db124b02012-11-07 04:55:053012
3013bool ExtensionService::IsExtensionIdle(const std::string& extension_id) const {
[email protected]d9a61e12012-11-14 02:43:473014 ExtensionProcessManager* process_manager = system_->process_manager();
[email protected]fc332ae2012-11-14 20:17:333015 DCHECK(process_manager);
[email protected]0db124b02012-11-07 04:55:053016 extensions::ExtensionHost* host =
[email protected]d9a61e12012-11-14 02:43:473017 process_manager->GetBackgroundHostForExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:223018 if (host)
3019 return false;
3020 return process_manager->GetRenderViewHostsForExtension(extension_id).empty();
3021}
3022
3023bool ExtensionService::ShouldDelayExtensionUpdate(
3024 const std::string& extension_id,
3025 bool wait_for_idle) const {
3026 const char kOnUpdateAvailableEvent[] = "runtime.onUpdateAvailable";
3027
3028 // If delayed updates are globally disabled, or just for this extension,
3029 // don't delay.
3030 if (!install_updates_when_idle_ || !wait_for_idle)
3031 return false;
3032
[email protected]695b5712012-12-06 23:55:283033 const Extension* old = GetInstalledExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:223034 // If there is no old extension, this is not an update, so don't delay.
3035 if (!old)
3036 return false;
3037
[email protected]9367eabc2013-03-01 01:29:293038 if (extensions::BackgroundInfo::HasPersistentBackgroundPage(old)) {
[email protected]e7aa7b7e2012-11-27 04:51:223039 // Delay installation if the extension listens for the onUpdateAvailable
3040 // event.
3041 return system_->event_router()->ExtensionHasEventListener(
3042 extension_id, kOnUpdateAvailableEvent);
3043 } else {
3044 // Delay installation if the extension is not idle.
3045 return !IsExtensionIdle(extension_id);
3046 }
[email protected]0db124b02012-11-07 04:55:053047}
[email protected]fdd679b2012-11-15 20:49:393048
[email protected]399583b2012-12-11 09:33:423049void ExtensionService::GarbageCollectIsolatedStorage() {
[email protected]650b2d52013-02-10 03:41:453050 scoped_ptr<base::hash_set<base::FilePath> > active_paths(
3051 new base::hash_set<base::FilePath>());
[email protected]399583b2012-12-11 09:33:423052 for (ExtensionSet::const_iterator it = extensions_.begin();
3053 it != extensions_.end(); ++it) {
[email protected]c86a8262013-03-31 07:58:573054 if ((*it)->is_storage_isolated()) {
[email protected]399583b2012-12-11 09:33:423055 active_paths->insert(
3056 BrowserContext::GetStoragePartitionForSite(
3057 profile_,
3058 GetSiteForExtensionId((*it)->id()))->GetPath());
3059 }
3060 }
3061
3062 DCHECK(!installs_delayed());
3063 set_installs_delayed(true);
3064 BrowserContext::GarbageCollectStoragePartitions(
3065 profile_, active_paths.Pass(),
3066 base::Bind(&ExtensionService::OnGarbageCollectIsolatedStorageFinished,
3067 AsWeakPtr()));
3068}
3069
3070void ExtensionService::OnGarbageCollectIsolatedStorageFinished() {
3071 set_installs_delayed(false);
3072 for (ExtensionSet::const_iterator it = delayed_installs_.begin();
3073 it != delayed_installs_.end();
3074 ++it) {
3075 FinishDelayedInstallation((*it)->id());
3076 }
3077 for (ExtensionSet::const_iterator it = delayed_updates_for_idle_.begin();
3078 it != delayed_updates_for_idle_.end();
3079 ++it) {
3080 MaybeFinishDelayedInstallation((*it)->id());
3081 }
3082 delayed_installs_.Clear();
3083}
3084
3085void ExtensionService::OnNeedsToGarbageCollectIsolatedStorage() {
3086 extension_prefs_->SetNeedsStorageGarbageCollection(true);
3087}
3088
[email protected]fdd679b2012-11-15 20:49:393089void ExtensionService::OnBlacklistUpdated() {
[email protected]695b5712012-12-06 23:55:283090 blacklist_->GetBlacklistedIDs(
3091 GenerateInstalledExtensionsSet()->GetIDs(),
3092 base::Bind(&ExtensionService::ManageBlacklist,
3093 AsWeakPtr(),
3094 blacklisted_extensions_.GetIDs()));
3095}
3096
3097void ExtensionService::ManageBlacklist(
3098 const std::set<std::string>& old_blacklisted_ids,
3099 const std::set<std::string>& new_blacklisted_ids) {
3100 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
3101
3102 std::set<std::string> no_longer_blacklisted;
3103 std::set_difference(old_blacklisted_ids.begin(), old_blacklisted_ids.end(),
3104 new_blacklisted_ids.begin(), new_blacklisted_ids.end(),
3105 std::inserter(no_longer_blacklisted,
3106 no_longer_blacklisted.begin()));
3107 std::set<std::string> not_yet_blacklisted;
3108 std::set_difference(new_blacklisted_ids.begin(), new_blacklisted_ids.end(),
3109 old_blacklisted_ids.begin(), old_blacklisted_ids.end(),
3110 std::inserter(not_yet_blacklisted,
3111 not_yet_blacklisted.begin()));
3112
3113 for (std::set<std::string>::iterator it = no_longer_blacklisted.begin();
3114 it != no_longer_blacklisted.end(); ++it) {
3115 scoped_refptr<const Extension> extension =
3116 blacklisted_extensions_.GetByID(*it);
3117 DCHECK(extension);
3118 if (!extension)
3119 continue;
3120 blacklisted_extensions_.Remove(*it);
3121 AddExtension(extension);
[email protected]ac875372013-02-28 04:36:093122 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.UnblacklistInstalled",
3123 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:283124 }
3125
3126 for (std::set<std::string>::iterator it = not_yet_blacklisted.begin();
3127 it != not_yet_blacklisted.end(); ++it) {
3128 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
3129 DCHECK(extension);
3130 if (!extension)
3131 continue;
3132 blacklisted_extensions_.Insert(extension);
3133 UnloadExtension(*it, extension_misc::UNLOAD_REASON_BLACKLIST);
[email protected]ac875372013-02-28 04:36:093134 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.BlacklistInstalled",
3135 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:283136 }
3137
3138 IdentifyAlertableExtensions();
[email protected]fdd679b2012-11-15 20:49:393139}
[email protected]75bdcb872013-03-13 00:41:453140
3141void ExtensionService::AddUpdateObserver(extensions::UpdateObserver* observer) {
3142 update_observers_.AddObserver(observer);
3143}
3144
3145void ExtensionService::RemoveUpdateObserver(
3146 extensions::UpdateObserver* observer) {
3147 update_observers_.RemoveObserver(observer);
3148}