blob: 9a50210d9e017fc1d05ab1f231c42f092a6d0ffd [file] [log] [blame]
[email protected]16d900042012-01-04 21:24:321// Copyright (c) 2012 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]97d2f1d2011-01-15 00:41:088#include <set>
[email protected]654512b2010-09-01 02:09:429
[email protected]24b538a2010-02-27 01:22:4410#include "base/basictypes.h"
[email protected]05aad2da2011-10-28 10:12:3711#include "base/bind.h"
[email protected]e5af875f2011-10-10 21:09:1412#include "base/callback.h"
[email protected]62433d32011-10-12 22:33:1213#include "base/command_line.h"
[email protected]6014d672008-12-05 00:38:2514#include "base/file_util.h"
[email protected]14908b72011-04-20 06:54:3615#include "base/logging.h"
[email protected]49098f702011-10-13 03:47:1816#include "base/metrics/field_trial.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]7286e3fc2011-07-19 22:13:2419#include "base/stl_util.h"
[email protected]24b538a2010-02-27 01:22:4420#include "base/string16.h"
[email protected]e83326f2010-07-31 17:29:2521#include "base/string_number_conversions.h"
[email protected]6014d672008-12-05 00:38:2522#include "base/string_util.h"
[email protected]18d4b6c2010-09-21 03:21:0423#include "base/stringprintf.h"
[email protected]34b99632011-01-01 01:01:0624#include "base/threading/thread_restrictions.h"
[email protected]cc2c3432009-11-06 17:24:3625#include "base/time.h"
[email protected]ce7f62e32010-08-10 23:43:5926#include "base/utf_string_conversions.h"
[email protected]cc655912009-01-29 23:19:1927#include "base/values.h"
[email protected]aa142702010-03-26 01:26:3328#include "base/version.h"
[email protected]a692a952011-11-28 08:23:5429#include "chrome/browser/accessibility/accessibility_extension_api.h"
[email protected]23d5f172011-10-25 05:49:5330#include "chrome/browser/bookmarks/bookmark_extension_api.h"
[email protected]15730c42009-09-03 00:03:2031#include "chrome/browser/browser_process.h"
[email protected]dfba8762011-09-02 12:49:5432#include "chrome/browser/chrome_plugin_service_filter.h"
[email protected]cdcf60242011-11-04 15:53:4333#include "chrome/browser/download/download_extension_api.h"
[email protected]58edca52012-02-16 21:51:3834#include "chrome/browser/extensions/api/api_resource_controller.h"
[email protected]92ba7b012012-05-16 03:36:1735#include "chrome/browser/extensions/api/cookies/cookies_api.h"
[email protected]895a1e52012-05-15 02:50:1236#include "chrome/browser/extensions/api/declarative/rules_registry_service.h"
37#include "chrome/browser/extensions/api/web_navigation/web_navigation_api.h"
[email protected]af9db5f2011-10-05 05:13:1538#include "chrome/browser/extensions/app_notification_manager.h"
[email protected]5db9ada2012-04-11 13:48:2039#include "chrome/browser/extensions/app_sync_data.h"
[email protected]18cc5a52011-04-15 16:03:3840#include "chrome/browser/extensions/apps_promo.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]49098f702011-10-13 03:47:1843#include "chrome/browser/extensions/default_apps_trial.h"
[email protected]b68d5ed2009-04-16 02:41:2844#include "chrome/browser/extensions/extension_browser_event_router.h"
[email protected]c10da4b02010-03-25 14:38:3245#include "chrome/browser/extensions/extension_data_deleter.h"
[email protected]62f051c2012-03-29 17:04:4446#include "chrome/browser/extensions/extension_disabled_ui.h"
[email protected]14a000d2010-04-29 21:44:2447#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]bdd34452012-04-16 15:58:4048#include "chrome/browser/extensions/extension_font_settings_api.h"
[email protected]e5af875f2011-10-10 21:09:1449#include "chrome/browser/extensions/extension_global_error.h"
[email protected]b1748b1d82009-11-30 20:32:5650#include "chrome/browser/extensions/extension_host.h"
[email protected]8baf0dbb2011-11-27 22:47:0251#include "chrome/browser/extensions/extension_input_ime_api.h"
[email protected]8a4cf842012-05-16 13:35:3052#include "chrome/browser/extensions/extension_managed_mode_api.h"
[email protected]7596ce72010-08-30 05:10:4653#include "chrome/browser/extensions/extension_management_api.h"
[email protected]118de7892011-03-16 13:31:4054#include "chrome/browser/extensions/extension_preference_api.h"
[email protected]4814b512009-11-07 00:12:2955#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]8a661f82010-10-19 21:47:1156#include "chrome/browser/extensions/extension_processes_api.h"
[email protected]3569b502012-01-12 20:08:2357#include "chrome/browser/extensions/extension_sorting.h"
[email protected]19eb80152011-02-26 00:28:4358#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]90310d92011-04-17 07:35:0459#include "chrome/browser/extensions/extension_sync_data.h"
[email protected]31d8f5f22012-04-02 15:22:0860#include "chrome/browser/extensions/extension_system.h"
[email protected]8f9d4eb2011-02-05 01:39:1061#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]8e4560b62011-01-14 10:09:1462#include "chrome/browser/extensions/external_extension_provider_impl.h"
[email protected]42b795652011-02-22 23:32:3863#include "chrome/browser/extensions/external_extension_provider_interface.h"
[email protected]d8c8f25f2011-11-02 18:18:0164#include "chrome/browser/extensions/installed_loader.h"
[email protected]b2907fd2011-03-25 16:43:3765#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]c333e792012-01-06 16:57:3966#include "chrome/browser/extensions/permissions_updater.h"
[email protected]cccdf0aa2011-11-11 03:43:3867#include "chrome/browser/extensions/settings/settings_frontend.h"
[email protected]d8c8f25f2011-11-02 18:18:0168#include "chrome/browser/extensions/unpacked_installer.h"
[email protected]42a08162012-03-16 18:09:1169#include "chrome/browser/extensions/updater/extension_updater.h"
[email protected]d2e6bd62011-10-24 20:29:0770#include "chrome/browser/history/history_extension_api.h"
[email protected]56ad3792010-05-28 17:45:3371#include "chrome/browser/net/chrome_url_request_context.h"
[email protected]37858e52010-08-26 00:22:0272#include "chrome/browser/prefs/pref_service.h"
[email protected]8ecad5e2010-12-02 21:18:3373#include "chrome/browser/profiles/profile.h"
[email protected]cf593af2011-12-30 05:44:3974#include "chrome/browser/profiles/profile_manager.h"
[email protected]8e5c89a2011-06-07 18:13:3375#include "chrome/browser/search_engines/template_url_service.h"
76#include "chrome/browser/search_engines/template_url_service_factory.h"
[email protected]18280372011-03-22 18:05:2277#include "chrome/browser/themes/theme_service.h"
[email protected]a0ea76c2011-03-23 17:36:4278#include "chrome/browser/themes/theme_service_factory.h"
[email protected]e5af875f2011-10-10 21:09:1479#include "chrome/browser/ui/browser.h"
[email protected]d8748142012-05-16 21:13:4380#include "chrome/browser/ui/browser_finder.h"
[email protected]e5af875f2011-10-10 21:09:1481#include "chrome/browser/ui/global_error_service.h"
82#include "chrome/browser/ui/global_error_service_factory.h"
[email protected]c8d407e2011-04-28 21:27:1783#include "chrome/browser/ui/webui/chrome_url_data_manager.h"
84#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]5eddc3e2011-10-26 04:33:3185#include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
[email protected]aab98a52009-12-02 03:22:3586#include "chrome/common/child_process_logging.h"
[email protected]432115822011-07-10 15:52:2787#include "chrome/common/chrome_notification_types.h"
[email protected]1e127022011-11-29 21:11:4788#include "chrome/common/chrome_paths.h"
[email protected]e2eb43112009-05-29 21:19:5489#include "chrome/common/chrome_switches.h"
[email protected]15397b42012-05-16 23:56:0690#include "chrome/common/chrome_version_info.h"
[email protected]5b1a0e22009-05-26 19:00:5891#include "chrome/common/extensions/extension.h"
[email protected]05c82182010-06-24 17:49:0892#include "chrome/common/extensions/extension_error_utils.h"
[email protected]7c927b62010-02-24 09:54:1393#include "chrome/common/extensions/extension_file_util.h"
[email protected]895a1e52012-05-15 02:50:1294#include "chrome/common/extensions/extension_manifest_constants.h"
[email protected]77a6970c2011-04-23 16:58:5695#include "chrome/common/extensions/extension_messages.h"
[email protected]a315ba92010-11-16 14:12:2196#include "chrome/common/extensions/extension_resource.h"
[email protected]25b34332009-06-05 21:53:1997#include "chrome/common/pref_names.h"
[email protected]a57209872009-05-04 22:53:1498#include "chrome/common/url_constants.h"
[email protected]c38831a12011-10-28 12:44:4999#include "content/public/browser/browser_thread.h"
[email protected]0e12d7d2011-12-01 16:21:44100#include "content/public/browser/devtools_agent_host_registry.h"
101#include "content/public/browser/devtools_manager.h"
[email protected]05aad2da2011-10-28 10:12:37102#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:16103#include "content/public/browser/notification_types.h"
[email protected]3a5180ae2011-12-21 02:39:38104#include "content/public/browser/plugin_service.h"
[email protected]f3b1a082011-11-18 00:34:30105#include "content/public/browser/render_process_host.h"
[email protected]1e127022011-11-29 21:11:47106#include "content/public/common/pepper_plugin_info.h"
[email protected]c10da4b02010-03-25 14:38:32107#include "googleurl/src/gurl.h"
[email protected]1debbbb62010-10-06 17:23:44108#include "net/base/registry_controlled_domain.h"
[email protected]895a1e52012-05-15 02:50:12109#include "sync/api/sync_change.h"
110#include "sync/api/sync_error_factory.h"
[email protected]24b538a2010-02-27 01:22:44111#include "webkit/database/database_tracker.h"
112#include "webkit/database/database_util.h"
[email protected]c64631652009-04-29 22:24:31113
[email protected]eed367e2011-04-12 03:43:31114#if defined(OS_CHROMEOS)
[email protected]14da67b02011-08-02 05:49:13115#include "chrome/browser/chromeos/cros/cros_library.h"
[email protected]f463bc92012-03-22 16:38:23116#include "chrome/browser/chromeos/extensions/bluetooth_event_router.h"
[email protected]eed367e2011-04-12 03:43:31117#include "chrome/browser/chromeos/extensions/file_browser_event_router.h"
[email protected]be5bd5742011-07-14 07:21:01118#include "chrome/browser/chromeos/extensions/input_method_event_router.h"
[email protected]ce9802042011-05-27 10:11:40119#include "chrome/browser/chromeos/extensions/media_player_event_router.h"
[email protected]14da67b02011-08-02 05:49:13120#include "chrome/browser/chromeos/input_method/input_method_manager.h"
[email protected]614d18c22012-04-26 16:59:22121#include "chrome/browser/extensions/extension_input_ime_api.h"
[email protected]b777b332011-04-16 04:01:08122#include "webkit/fileapi/file_system_context.h"
123#include "webkit/fileapi/file_system_mount_point_provider.h"
[email protected]eed367e2011-04-12 03:43:31124#endif
125
[email protected]9a2aebe02011-12-02 21:54:14126#if defined(OS_CHROMEOS) && defined(USE_VIRTUAL_KEYBOARD)
[email protected]27072cad2011-05-09 19:46:40127#include "chrome/browser/extensions/extension_input_ui_api.h"
128#endif
129
[email protected]5ef47ec2010-01-28 05:58:05130using base::Time;
[email protected]55eb70e762012-02-20 17:38:39131using content::BrowserContext;
[email protected]631bb742011-11-02 11:29:39132using content::BrowserThread;
[email protected]0e12d7d2011-12-01 16:21:44133using content::DevToolsAgentHost;
134using content::DevToolsAgentHostRegistry;
[email protected]3a5180ae2011-12-21 02:39:38135using content::PluginService;
[email protected]1c321ee52012-05-21 03:02:34136using extensions::Extension;
137using extensions::ExtensionIdSet;
138using extensions::ExtensionInfo;
139using extensions::UnloadedExtensionInfo;
[email protected]5ef47ec2010-01-28 05:58:05140
[email protected]c6d474f82009-12-16 21:11:06141namespace errors = extension_manifest_errors;
142
[email protected]b6ab96d2009-08-20 18:58:19143namespace {
144
[email protected]29d0d4ac2010-09-08 21:10:31145#if defined(OS_LINUX)
146static const int kOmniboxIconPaddingLeft = 2;
147static const int kOmniboxIconPaddingRight = 2;
148#elif defined(OS_MACOSX)
149static const int kOmniboxIconPaddingLeft = 0;
150static const int kOmniboxIconPaddingRight = 2;
151#else
152static const int kOmniboxIconPaddingLeft = 0;
153static const int kOmniboxIconPaddingRight = 0;
154#endif
155
[email protected]300c0ea2011-07-15 23:04:33156const char* kNaClPluginMimeType = "application/x-nacl";
157
[email protected]3bdba0d2011-08-23 07:17:30158static bool IsSyncableExtension(const Extension& extension) {
159 return extension.GetSyncType() == Extension::SYNC_TYPE_EXTENSION;
160}
161
162static bool IsSyncableApp(const Extension& extension) {
163 return extension.GetSyncType() == Extension::SYNC_TYPE_APP;
164}
165
[email protected]c6d474f82009-12-16 21:11:06166} // namespace
[email protected]b6ab96d2009-08-20 18:58:19167
[email protected]eaa7dd182010-12-14 11:09:00168ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49169 : background_page_ready(false),
[email protected]f8bf5992011-10-24 22:38:57170 being_upgraded(false),
171 has_used_webrequest(false) {
[email protected]d7e9a862010-11-03 21:57:49172}
173
[email protected]eaa7dd182010-12-14 11:09:00174ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49175}
176
[email protected]cebc3dc2011-04-18 17:15:00177ExtensionService::NaClModuleInfo::NaClModuleInfo() {
178}
179
180ExtensionService::NaClModuleInfo::~NaClModuleInfo() {
181}
182
[email protected]eaa7dd182010-12-14 11:09:00183// ExtensionService.
[email protected]6014d672008-12-05 00:38:25184
[email protected]eaa7dd182010-12-14 11:09:00185const char* ExtensionService::kInstallDirectoryName = "Extensions";
[email protected]15d5b472011-11-24 04:17:24186
187const char* ExtensionService::kLocalAppSettingsDirectoryName =
188 "Local App Settings";
189const char* ExtensionService::kLocalExtensionSettingsDirectoryName =
190 "Local Extension Settings";
191const char* ExtensionService::kSyncAppSettingsDirectoryName =
192 "Sync App Settings";
193const char* ExtensionService::kSyncExtensionSettingsDirectoryName =
194 "Sync Extension Settings";
[email protected]494c06e2009-07-25 01:06:42195
[email protected]8e4560b62011-01-14 10:09:14196void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12197 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20198
[email protected]7a4c6852010-09-16 03:44:22199 // Check if the providers know about this extension.
[email protected]0a60a2e2010-10-25 16:15:21200 ProviderCollection::const_iterator i;
201 for (i = external_extension_providers_.begin();
202 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14203 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21204 if (i->get()->HasExtension(id))
205 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22206 }
207
[email protected]0f48fca2011-05-19 18:46:35208 // We get the list of external extensions to check from preferences.
209 // It is possible that an extension has preferences but is not loaded.
210 // For example, an extension that requires experimental permissions
211 // will not be loaded if the experimental command line flag is not used.
212 // In this case, do not uninstall.
213 const Extension* extension = GetInstalledExtension(id);
214 if (!extension) {
215 // We can't call UninstallExtension with an unloaded/invalid
216 // extension ID.
217 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
218 << "with id: " << id;
219 return;
220 }
[email protected]d6ebc9792011-04-07 18:18:33221 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22222}
223
[email protected]8e4560b62011-01-14 10:09:14224void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22225 external_extension_providers_.clear();
226}
227
[email protected]8e4560b62011-01-14 10:09:14228void ExtensionService::AddProviderForTesting(
229 ExternalExtensionProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12230 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21231 external_extension_providers_.push_back(
[email protected]8e4560b62011-01-14 10:09:14232 linked_ptr<ExternalExtensionProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22233}
234
[email protected]9060d8b02012-01-13 02:14:30235bool ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22236 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42237 const GURL& update_url,
238 Extension::Location location) {
[email protected]ab22ba42011-01-14 16:36:38239 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
240 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20241
[email protected]8a87a5332011-08-11 17:54:59242 const Extension* extension = GetExtensionById(id, true);
243 if (extension) {
244 // Already installed. Skip this install if the current location has
245 // higher priority than |location|.
246 Extension::Location current = extension->location();
247 if (current == Extension::GetHigherPriorityLocation(current, location))
[email protected]9060d8b02012-01-13 02:14:30248 return false;
[email protected]8a87a5332011-08-11 17:54:59249 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22250 }
[email protected]9060d8b02012-01-13 02:14:30251
252 // Add |id| to the set of pending extensions. If it can not be added,
253 // then there is already a pending record from a higher-priority install
254 // source. In this case, signal that this extension will not be
255 // installed by returning false.
256 if (!pending_extension_manager()->AddFromExternalUpdateUrl(
257 id, update_url, location))
258 return false;
259
[email protected]94fde232012-04-27 10:22:30260 update_once_all_providers_are_ready_ = true;
[email protected]9060d8b02012-01-13 02:14:30261 return true;
[email protected]7a4c6852010-09-16 03:44:22262}
263
[email protected]d9696672011-03-15 22:45:09264const Extension* ExtensionService::GetInstalledApp(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:44265 const Extension* extension = extensions_.GetExtensionOrAppByURL(
266 ExtensionURLInfo(url));
267 if (extension && extension->is_app())
268 return extension;
[email protected]d9696672011-03-15 22:45:09269
270 return NULL;
271}
272
273bool ExtensionService::IsInstalledApp(const GURL& url) {
274 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27275}
276
[email protected]da5683db2011-04-23 17:12:21277void ExtensionService::SetInstalledAppForRenderer(int renderer_child_id,
278 const Extension* app) {
279 installed_app_hosts_[renderer_child_id] = app;
280}
281
282const Extension* ExtensionService::GetInstalledAppForRenderer(
283 int renderer_child_id) {
284 InstalledAppMap::iterator i = installed_app_hosts_.find(renderer_child_id);
285 if (i == installed_app_hosts_.end())
286 return NULL;
287 return i->second;
288}
289
[email protected]6aeac8342010-10-01 20:21:18290// static
[email protected]d6ebc9792011-04-07 18:18:33291// This function is used to implement the command-line switch
[email protected]3bdba0d2011-08-23 07:17:30292// --uninstall-extension, and to uninstall an extension via sync. The LOG
293// statements within this function are used to inform the user if the uninstall
294// cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00295bool ExtensionService::UninstallExtensionHelper(
296 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18297 const std::string& extension_id) {
[email protected]6aeac8342010-10-01 20:21:18298
[email protected]95da88c42011-03-31 10:07:33299 const Extension* extension =
[email protected]8001df22011-04-28 19:59:47300 extensions_service->GetInstalledExtension(extension_id);
[email protected]95da88c42011-03-31 10:07:33301
[email protected]d6ebc9792011-04-07 18:18:33302 // We can't call UninstallExtension with an invalid extension ID.
[email protected]95da88c42011-03-31 10:07:33303 if (!extension) {
[email protected]6aeac8342010-10-01 20:21:18304 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33305 << "id: " << extension_id;
306 return false;
[email protected]6aeac8342010-10-01 20:21:18307 }
308
[email protected]d6ebc9792011-04-07 18:18:33309 // The following call to UninstallExtension will not allow an uninstall of a
310 // policy-controlled extension.
311 std::string error;
312 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
313 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
314 << ": " << error;
315 return false;
316 }
[email protected]95da88c42011-03-31 10:07:33317
[email protected]6aeac8342010-10-01 20:21:18318 return true;
319}
320
[email protected]eaa7dd182010-12-14 11:09:00321ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24322 const CommandLine* command_line,
323 const FilePath& install_directory,
324 ExtensionPrefs* extension_prefs,
[email protected]0436b102011-04-15 18:30:03325 bool autoupdate_enabled,
326 bool extensions_enabled)
[email protected]d8c8f25f2011-11-02 18:18:01327 : profile_(profile),
[email protected]749d59a2012-04-05 00:23:24328 system_(ExtensionSystem::Get(profile)),
[email protected]73c47932010-12-06 18:13:43329 extension_prefs_(extension_prefs),
[email protected]cccdf0aa2011-11-11 03:43:38330 settings_frontend_(extensions::SettingsFrontend::Create(profile)),
[email protected]14908b72011-04-20 06:54:36331 pending_extension_manager_(*ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]a9b00ac2009-06-25 21:03:23332 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03333 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13334 show_extensions_prompts_(true),
[email protected]3ecda252010-11-18 19:50:55335 ready_(false),
[email protected]14908b72011-04-20 06:54:36336 toolbar_model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]8b280302011-10-13 22:22:23337 menu_manager_(profile),
[email protected]af9db5f2011-10-05 05:13:15338 app_notification_manager_(new AppNotificationManager(profile)),
[email protected]18cc5a52011-04-15 16:03:38339 apps_promo_(profile->GetPrefs()),
[email protected]7c1490da2011-10-11 18:53:25340 event_routers_initialized_(false),
[email protected]dff1e042012-04-27 10:59:18341 update_once_all_providers_are_ready_(false),
[email protected]5db9ada2012-04-11 13:48:20342 app_sync_bundle_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
343 extension_sync_bundle_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]a1755bd2011-12-09 00:40:20344 extension_warnings_(profile),
[email protected]58edca52012-02-16 21:51:38345 api_resource_controller_(NULL),
[email protected]178f8512012-02-09 01:49:36346 app_shortcut_manager_(profile) {
[email protected]a29a517a2011-01-21 21:11:12347 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20348
[email protected]36a784c2009-06-23 06:21:08349 // Figure out if extension installation should be enabled.
[email protected]6d60703b2009-08-29 01:29:23350 if (command_line->HasSwitch(switches::kDisableExtensions)) {
351 extensions_enabled_ = false;
352 } else if (profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
353 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18354 }
[email protected]36a784c2009-06-23 06:21:08355
[email protected]432115822011-07-10 15:52:27356 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07357 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27358 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
[email protected]ad50def52011-10-19 23:17:07359 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27360 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07361 content::NotificationService::AllBrowserContextsAndSources());
[email protected]2fb7dc982010-09-29 12:24:28362 pref_change_registrar_.Init(profile->GetPrefs());
363 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, this);
364 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, this);
[email protected]4814b512009-11-07 00:12:29365
[email protected]93fd78f42009-07-10 16:43:17366 // Set up the ExtensionUpdater
367 if (autoupdate_enabled) {
368 int update_frequency = kDefaultUpdateFrequencySeconds;
369 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25370 base::StringToInt(command_line->GetSwitchValueASCII(
371 switches::kExtensionsUpdateFrequency),
372 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17373 }
[email protected]42a08162012-03-16 18:09:11374 updater_.reset(new extensions::ExtensionUpdater(this,
375 extension_prefs,
376 profile->GetPrefs(),
377 profile,
378 update_frequency));
[email protected]93fd78f42009-07-10 16:43:17379 }
380
[email protected]25ae0152011-11-18 14:40:02381 component_loader_.reset(
382 new extensions::ComponentLoader(this,
383 profile->GetPrefs(),
384 g_browser_process->local_state()));
[email protected]8e4560b62011-01-14 10:09:14385
[email protected]af9db5f2011-10-05 05:13:15386 app_notification_manager_->Init();
387
[email protected]0436b102011-04-15 18:30:03388 if (extensions_enabled_) {
[email protected]1f2f3002011-12-28 00:40:21389 if (!command_line->HasSwitch(switches::kImport) &&
390 !command_line->HasSwitch(switches::kImportFromFile)) {
391 ExternalExtensionProviderImpl::CreateExternalProviders(
392 this, profile_, &external_extension_providers_);
393 }
[email protected]873531342011-03-09 12:16:05394 }
[email protected]b671760b2010-07-15 21:13:47395
[email protected]aa96d3a2010-08-21 08:45:25396 // Use monochrome icons for Omnibox icons.
[email protected]29d0d4ac2010-09-08 21:10:31397 omnibox_popup_icon_manager_.set_monochrome(true);
[email protected]b671760b2010-07-15 21:13:47398 omnibox_icon_manager_.set_monochrome(true);
[email protected]29d0d4ac2010-09-08 21:10:31399 omnibox_icon_manager_.set_padding(gfx::Insets(0, kOmniboxIconPaddingLeft,
400 0, kOmniboxIconPaddingRight));
[email protected]cb0e50312011-05-09 15:03:07401
[email protected]fb82dcd2012-03-21 14:15:46402 // Set this as the ExtensionService for extension sorting to ensure it
403 // cause syncs if required.
404 extension_prefs_->extension_sorting()->SetExtensionService(this);
405
[email protected]cb0e50312011-05-09 15:03:07406 // How long is the path to the Extensions directory?
407 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
408 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25409}
410
[email protected]84df8332011-12-06 18:22:46411const ExtensionSet* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45412 return &extensions_;
413}
414
[email protected]84df8332011-12-06 18:22:46415const ExtensionSet* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45416 return &disabled_extensions_;
417}
418
[email protected]84df8332011-12-06 18:22:46419const ExtensionSet* ExtensionService::terminated_extensions() const {
[email protected]bb7f40952011-01-13 00:21:20420 return &terminated_extensions_;
421}
422
[email protected]7f4308d2012-01-18 07:43:01423const ExtensionSet* ExtensionService::GenerateInstalledExtensionsSet() const {
424 ExtensionSet* installed_extensions = new ExtensionSet();
425 installed_extensions->InsertAll(extensions_);
426 installed_extensions->InsertAll(disabled_extensions_);
427 installed_extensions->InsertAll(terminated_extensions_);
428 return installed_extensions;
429}
430
[email protected]b2907fd2011-03-25 16:43:37431PendingExtensionManager* ExtensionService::pending_extension_manager() {
432 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45433}
434
[email protected]eaa7dd182010-12-14 11:09:00435ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17436 // No need to unload extensions here because they are profile-scoped, and the
437 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17438
[email protected]8e4560b62011-01-14 10:09:14439 ProviderCollection::const_iterator i;
440 for (i = external_extension_providers_.begin();
441 i != external_extension_providers_.end(); ++i) {
442 ExternalExtensionProviderInterface* provider = i->get();
443 provider->ServiceShutdown();
444 }
[email protected]a1755bd2011-12-09 00:40:20445
[email protected]58edca52012-02-16 21:51:38446 if (api_resource_controller_) {
447 // If this check failed, then a unit test was using an APIResource but
448 // didn't provide the IO thread message loop needed for those resources to
449 // do their job (including destroying themselves at shutdown).
[email protected]e1fae1c2011-12-13 22:17:17450 DCHECK(BrowserThread::IsMessageLoopValid(BrowserThread::IO));
[email protected]58edca52012-02-16 21:51:38451 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE,
452 api_resource_controller_);
[email protected]e1fae1c2011-12-13 22:17:17453 }
[email protected]6014d672008-12-05 00:38:25454}
455
[email protected]d91268022011-08-26 13:17:37456void ExtensionService::InitEventRoutersAfterImport() {
[email protected]cf593af2011-12-30 05:44:39457 RegisterForImportFinished();
458}
459
460void ExtensionService::RegisterForImportFinished() {
461 if (!registrar_.IsRegistered(this, chrome::NOTIFICATION_IMPORT_FINISHED,
462 content::Source<Profile>(profile_))) {
463 registrar_.Add(this, chrome::NOTIFICATION_IMPORT_FINISHED,
464 content::Source<Profile>(profile_));
465 }
466}
467
468void ExtensionService::InitAfterImport() {
469 CheckForExternalUpdates();
470
471 GarbageCollectExtensions();
472
473 // Idempotent, so although there is a possible race if the import
474 // process finished sometime in the middle of ProfileImpl::InitExtensions,
475 // it cannot happen twice.
476 InitEventRouters();
[email protected]d91268022011-08-26 13:17:37477}
478
[email protected]eaa7dd182010-12-14 11:09:00479void ExtensionService::InitEventRouters() {
[email protected]b3d62312b12010-10-14 21:10:18480 if (event_routers_initialized_)
481 return;
482
[email protected]6a3cd37e2012-04-17 17:13:34483#if defined(ENABLE_EXTENSIONS)
[email protected]370e9502011-09-14 19:31:44484 downloads_event_router_.reset(new ExtensionDownloadsEventRouter(profile_));
[email protected]d2e6bd62011-10-24 20:29:07485 history_event_router_.reset(new HistoryExtensionEventRouter());
[email protected]f825df22011-06-28 17:36:10486 history_event_router_->ObserveProfile(profile_);
[email protected]97d2f1d2011-01-15 00:41:08487 browser_event_router_.reset(new ExtensionBrowserEventRouter(profile_));
[email protected]d97091502012-04-19 23:55:07488 browser_event_router_->Init(&toolbar_model_);
[email protected]118de7892011-03-16 13:31:40489 preference_event_router_.reset(new ExtensionPreferenceEventRouter(profile_));
[email protected]23d5f172011-10-25 05:49:53490 bookmark_event_router_.reset(new BookmarkExtensionEventRouter(
[email protected]f825df22011-06-28 17:36:10491 profile_->GetBookmarkModel()));
492 bookmark_event_router_->Init();
[email protected]92ba7b012012-05-16 03:36:17493 cookies_event_router_.reset(new
494 extensions::ExtensionCookiesEventRouter(profile_));
[email protected]f825df22011-06-28 17:36:10495 cookies_event_router_->Init();
496 management_event_router_.reset(new ExtensionManagementEventRouter(profile_));
497 management_event_router_->Init();
[email protected]7da9a8232011-05-27 21:07:11498 ExtensionProcessesEventRouter::GetInstance()->ObserveProfile(profile_);
[email protected]f825df22011-06-28 17:36:10499 web_navigation_event_router_.reset(
[email protected]017ecde92012-04-04 09:11:26500 new extensions::WebNavigationEventRouter(profile_));
[email protected]f825df22011-06-28 17:36:10501 web_navigation_event_router_->Init();
[email protected]bdd34452012-04-16 15:58:40502 font_settings_event_router_.reset(
503 new ExtensionFontSettingsEventRouter(profile_));
504 font_settings_event_router_->Init();
[email protected]8a4cf842012-05-16 13:35:30505 managed_mode_event_router_.reset(
506 new ExtensionManagedModeEventRouter(profile_));
507 managed_mode_event_router_->Init();
[email protected]27072cad2011-05-09 19:46:40508
[email protected]eed367e2011-04-12 03:43:31509#if defined(OS_CHROMEOS)
[email protected]2a72c9f2012-03-17 15:10:30510 FileBrowserEventRouterFactory::GetForProfile(
511 profile_)->ObserveFileSystemEvents();
[email protected]771a6182011-10-05 16:32:52512
[email protected]f463bc92012-03-22 16:38:23513 bluetooth_event_router_.reset(
514 new chromeos::ExtensionBluetoothEventRouter(profile_));
515
[email protected]771a6182011-10-05 16:32:52516 input_method_event_router_.reset(
517 new chromeos::ExtensionInputMethodEventRouter);
[email protected]be5bd5742011-07-14 07:21:01518
[email protected]c8bf09be2011-06-25 03:22:27519 ExtensionMediaPlayerEventRouter::GetInstance()->Init(profile_);
[email protected]8baf0dbb2011-11-27 22:47:02520 ExtensionInputImeEventRouter::GetInstance()->Init();
[email protected]eed367e2011-04-12 03:43:31521#endif
[email protected]27072cad2011-05-09 19:46:40522
[email protected]9a2aebe02011-12-02 21:54:14523#if defined(OS_CHROMEOS) && defined(USE_VIRTUAL_KEYBOARD)
[email protected]27072cad2011-05-09 19:46:40524 ExtensionInputUiEventRouter::GetInstance()->Init();
525#endif
526
[email protected]6a3cd37e2012-04-17 17:13:34527#endif // defined(ENABLE_EXTENSIONS)
[email protected]b3d62312b12010-10-14 21:10:18528 event_routers_initialized_ = true;
[email protected]c5ae74ab2010-04-15 18:14:37529}
530
[email protected]b2907fd2011-03-25 16:43:37531const Extension* ExtensionService::GetExtensionById(
532 const std::string& id, bool include_disabled) const {
[email protected]8001df22011-04-28 19:59:47533 return GetExtensionByIdInternal(id, true, include_disabled, false);
[email protected]78994ab02010-12-08 18:06:44534}
535
[email protected]eaa7dd182010-12-14 11:09:00536void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12537 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20538
[email protected]fa6a9102010-11-22 15:38:50539 DCHECK(!ready_); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17540 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32541
[email protected]d8c8f25f2011-11-02 18:18:01542 component_loader_->LoadAll();
543 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]894bb502009-05-21 22:39:57544
[email protected]7a161db2011-12-15 20:45:51545 // If we are running in the import process, don't bother initializing the
546 // extension service since this can interfere with the main browser process
547 // that is already running an extension service for this profile.
548 // TODO(aa): can we start up even less of ExtensionService?
549 // http://crbug.com/107636
[email protected]1f2f3002011-12-28 00:40:21550 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kImport) &&
551 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kImportFromFile)) {
[email protected]cf593af2011-12-30 05:44:39552 if (g_browser_process->profile_manager() &&
553 g_browser_process->profile_manager()->will_import()) {
554 RegisterForImportFinished();
555 } else {
556 // TODO(erikkay) this should probably be deferred to a future point
557 // rather than running immediately at startup.
558 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57559
[email protected]cf593af2011-12-30 05:44:39560 // TODO(erikkay) this should probably be deferred as well.
561 GarbageCollectExtensions();
562 }
[email protected]7a161db2011-12-15 20:45:51563 }
[email protected]6014d672008-12-05 00:38:25564}
565
[email protected]420a0ec2011-06-01 01:07:03566bool ExtensionService::UpdateExtension(
567 const std::string& id,
568 const FilePath& extension_path,
569 const GURL& download_url,
570 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12571 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20572
[email protected]b2907fd2011-03-25 16:43:37573 PendingExtensionInfo pending_extension_info;
574 bool is_pending_extension = pending_extension_manager_.GetById(
575 id, &pending_extension_info);
[email protected]8ef78fd2010-08-19 17:14:32576
[email protected]8001df22011-04-28 19:59:47577 const Extension* extension =
578 GetExtensionByIdInternal(id, true, true, false);
[email protected]5eb375e92010-11-26 07:50:41579 if (!is_pending_extension && !extension) {
[email protected]aa142702010-03-26 01:26:33580 LOG(WARNING) << "Will not update extension " << id
581 << " because it is not installed or pending";
582 // Delete extension_path since we're not creating a CrxInstaller
583 // that would do it for us.
[email protected]14908b72011-04-20 06:54:36584 if (!BrowserThread::PostTask(
585 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:36586 base::Bind(
[email protected]7296f2762011-11-21 19:23:44587 &extension_file_util::DeleteFile, extension_path, false)))
[email protected]14908b72011-04-20 06:54:36588 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03589
590 return false;
[email protected]e957fe52009-06-23 16:51:05591 }
592
[email protected]aa142702010-03-26 01:26:33593 // We want a silent install only for non-pending extensions and
594 // pending extensions that have install_silently set.
595 ExtensionInstallUI* client =
[email protected]b2907fd2011-03-25 16:43:37596 (!is_pending_extension || pending_extension_info.install_silently()) ?
[email protected]aa142702010-03-26 01:26:33597 NULL : new ExtensionInstallUI(profile_);
598
[email protected]d8c8f25f2011-11-02 18:18:01599 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(this, client));
[email protected]6dfbbf82010-03-12 23:09:16600 installer->set_expected_id(id);
[email protected]ec5b50d2010-10-09 16:35:18601 if (is_pending_extension)
[email protected]b2907fd2011-03-25 16:43:37602 installer->set_install_source(pending_extension_info.install_source());
[email protected]5eb375e92010-11-26 07:50:41603 else if (extension)
604 installer->set_install_source(extension->location());
[email protected]0d3e4a22011-06-23 19:02:52605 if (pending_extension_info.install_silently())
606 installer->set_allow_silent_install(true);
[email protected]7d8b7072012-04-07 01:07:46607 // If the extension was installed from or has migrated to the webstore, or
608 // if the extension came from sync and its auto-update URL is from the
[email protected]75764512011-12-19 19:54:28609 // webstore, treat it as a webstore install. Note that we ignore some older
610 // extensions with blank auto-update URLs because we are mostly concerned
611 // with restrictions on NaCl extensions, which are newer.
[email protected]a12ce8b22012-01-17 18:40:53612 int creation_flags = Extension::NO_FLAGS;
[email protected]75764512011-12-19 19:54:28613 if ((extension && extension->from_webstore()) ||
[email protected]7d8b7072012-04-07 01:07:46614 (extension && extension->UpdatesFromGallery()) ||
[email protected]75764512011-12-19 19:54:28615 (!extension && pending_extension_info.is_from_sync() &&
616 extension_urls::IsWebstoreUpdateUrl(
[email protected]a12ce8b22012-01-17 18:40:53617 pending_extension_info.update_url()))) {
618 creation_flags |= Extension::FROM_WEBSTORE;
619 }
620
621 // Bookmark apps being updated is kind of a contradiction, but that's because
622 // we mark the default apps as bookmark apps, and they're hosted in the web
623 // store, thus they can get updated. See http://crbug.com/101605 for more
624 // details.
625 if (extension && extension->from_bookmark())
626 creation_flags |= Extension::FROM_BOOKMARK;
627
[email protected]a12ce8b22012-01-17 18:40:53628 installer->set_creation_flags(creation_flags);
629
[email protected]6dfbbf82010-03-12 23:09:16630 installer->set_delete_source(true);
[email protected]655b2b1a2011-10-13 17:13:06631 installer->set_download_url(download_url);
[email protected]cb0e50312011-05-09 15:03:07632 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16633 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03634
635 if (out_crx_installer)
636 *out_crx_installer = installer;
637
638 return true;
[email protected]e957fe52009-06-23 16:51:05639}
640
[email protected]eaa7dd182010-12-14 11:09:00641void ExtensionService::ReloadExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12642 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b65272f2009-08-31 15:47:06643 FilePath path;
[email protected]9adb9692010-10-29 23:14:02644 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40645
[email protected]f17dbd42010-08-16 23:21:10646 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06647 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29648 // If the extension has an inspector open for its background page, detach
649 // the inspector and hang onto a cookie for it, so that we can reattach
650 // later.
[email protected]31d8f5f22012-04-02 15:22:08651 // TODO(yoz): this is not incognito-safe!
652 ExtensionProcessManager* manager = system_->process_manager();
[email protected]06024c62011-10-20 20:57:12653 ExtensionHost* host = manager->GetBackgroundHostForExtension(extension_id);
[email protected]0e12d7d2011-12-01 16:21:44654 if (host && DevToolsAgentHostRegistry::HasDevToolsAgentHost(
655 host->render_view_host())) {
[email protected]4814b512009-11-07 00:12:29656 // Look for an open inspector for the background page.
[email protected]0e12d7d2011-12-01 16:21:44657 DevToolsAgentHost* agent =
658 DevToolsAgentHostRegistry::GetDevToolsAgentHost(
659 host->render_view_host());
660 int devtools_cookie =
661 content::DevToolsManager::GetInstance()->DetachClientHost(agent);
[email protected]4814b512009-11-07 00:12:29662 if (devtools_cookie >= 0)
663 orphaned_dev_tools_[extension_id] = devtools_cookie;
664 }
665
[email protected]b65272f2009-08-31 15:47:06666 path = current_extension->path();
[email protected]44d62b62012-04-11 00:06:03667 DisableExtension(extension_id, Extension::DISABLE_RELOAD);
[email protected]f17dbd42010-08-16 23:21:10668 disabled_extension_paths_[extension_id] = path;
[email protected]1eb175082010-02-10 09:26:16669 } else {
670 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06671 }
672
[email protected]43ceb002012-02-10 23:19:15673 // If we're reloading a component extension, use the component extension
674 // loader's reloader.
675 if (component_loader_->Exists(extension_id)) {
676 component_loader_->Reload(extension_id);
677 return;
678 }
679
[email protected]e6090e42010-03-23 22:44:08680 // Check the installed extensions to see if what we're reloading was already
681 // installed.
682 scoped_ptr<ExtensionInfo> installed_extension(
683 extension_prefs_->GetInstalledExtensionInfo(extension_id));
684 if (installed_extension.get() &&
685 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01686 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08687 } else {
[email protected]d8c8f25f2011-11-02 18:18:01688 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08689 // We should always be able to remember the extension's path. If it's not in
690 // the map, someone failed to update |unloaded_extension_paths_|.
691 CHECK(!path.empty());
[email protected]d8c8f25f2011-11-02 18:18:01692 extensions::UnpackedInstaller::Create(this)->Load(path);
[email protected]e6090e42010-03-23 22:44:08693 }
[email protected]9cddd4702009-07-27 22:09:40694}
695
[email protected]fa2416f2011-05-03 08:41:20696bool ExtensionService::UninstallExtension(
[email protected]a12c706e2011-12-01 00:58:08697 std::string extension_id,
[email protected]fa2416f2011-05-03 08:41:20698 bool external_uninstall,
699 std::string* error) {
[email protected]a29a517a2011-01-21 21:11:12700 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20701
[email protected]0d6ec3a72011-09-02 02:09:43702 scoped_refptr<const Extension> extension(GetInstalledExtension(extension_id));
[email protected]631cf822009-05-15 07:01:25703
[email protected]e7afe2452010-08-22 16:19:13704 // Callers should not send us nonexistent extensions.
[email protected]aebe23a32010-12-10 22:15:48705 CHECK(extension);
[email protected]9f1087e2009-06-15 17:29:32706
[email protected]95da88c42011-03-31 10:07:33707 // Policy change which triggers an uninstall will always set
708 // |external_uninstall| to true so this is the only way to uninstall
709 // managed extensions.
[email protected]0d6ec3a72011-09-02 02:09:43710 if (!Extension::UserMayDisable(extension->location()) &&
711 !external_uninstall) {
[email protected]ad50def52011-10-19 23:17:07712 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27713 chrome::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53714 content::Source<Profile>(profile_),
715 content::Details<const Extension>(extension));
[email protected]d6ebc9792011-04-07 18:18:33716 if (error != NULL) {
717 *error = errors::kCannotUninstallManagedExtension;
718 }
719 return false;
720 }
[email protected]95da88c42011-03-31 10:07:33721
[email protected]3bdba0d2011-08-23 07:17:30722 // Extract the data we need for sync now, but don't actually sync until we've
723 // completed the uninstallation.
[email protected]3bdba0d2011-08-23 07:17:30724 SyncChange sync_change;
[email protected]5db9ada2012-04-11 13:48:20725 if (app_sync_bundle_.HandlesApp(*extension)) {
726 sync_change = app_sync_bundle_.CreateSyncChangeToDelete(extension);
727 } else if (extension_sync_bundle_.HandlesExtension(*extension)) {
728 sync_change = extension_sync_bundle_.CreateSyncChangeToDelete(extension);
[email protected]3bdba0d2011-08-23 07:17:30729 }
730
[email protected]9b217652010-10-08 22:04:23731 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08732 extension->GetType(), 100);
[email protected]fe2dd7742011-04-19 22:52:49733 RecordPermissionMessagesHistogram(
734 extension, "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23735
[email protected]8e5c89a2011-06-07 18:13:33736 TemplateURLService* url_service =
737 TemplateURLServiceFactory::GetForProfile(profile_);
738 if (url_service)
739 url_service->UnregisterExtensionKeyword(extension);
[email protected]56ad3792010-05-28 17:45:33740
[email protected]831aa212010-03-26 13:55:19741 // Unload before doing more cleanup to ensure that nothing is hanging on to
742 // any of these resources.
[email protected]814a7bf0f2011-08-13 05:30:59743 UnloadExtension(extension_id, extension_misc::UNLOAD_REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19744
[email protected]0d6ec3a72011-09-02 02:09:43745 extension_prefs_->OnExtensionUninstalled(extension_id, extension->location(),
[email protected]831aa212010-03-26 13:55:19746 external_uninstall);
[email protected]9f1087e2009-06-15 17:29:32747
748 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]0d6ec3a72011-09-02 02:09:43749 if (Extension::LOAD != extension->location()) {
[email protected]14908b72011-04-20 06:54:36750 if (!BrowserThread::PostTask(
751 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:36752 base::Bind(
[email protected]14908b72011-04-20 06:54:36753 &extension_file_util::UninstallExtension,
754 install_directory_,
[email protected]fa2416f2011-05-03 08:41:20755 extension_id)))
[email protected]14908b72011-04-20 06:54:36756 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32757 }
758
[email protected]0d6ec3a72011-09-02 02:09:43759 GURL launch_web_url_origin(extension->launch_web_url());
760 launch_web_url_origin = launch_web_url_origin.GetOrigin();
[email protected]605fb8102012-05-04 01:36:55761 bool is_storage_isolated = extension->is_storage_isolated();
[email protected]0d6ec3a72011-09-02 02:09:43762
763 if (extension->is_hosted_app() &&
764 !profile_->GetExtensionSpecialStoragePolicy()->
765 IsStorageProtected(launch_web_url_origin)) {
[email protected]dc0b5a12011-10-14 00:06:13766 ExtensionDataDeleter::StartDeleting(
767 profile_, extension_id, launch_web_url_origin, is_storage_isolated);
[email protected]0d6ec3a72011-09-02 02:09:43768 }
[email protected]dc0b5a12011-10-14 00:06:13769 ExtensionDataDeleter::StartDeleting(
770 profile_, extension_id, extension->url(), is_storage_isolated);
[email protected]0d6ec3a72011-09-02 02:09:43771
[email protected]0dfe05c2011-02-23 23:03:36772 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06773
774 // Notify interested parties that we've uninstalled this extension.
[email protected]ad50def52011-10-19 23:17:07775 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27776 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]6c2381d2011-10-19 02:52:53777 content::Source<Profile>(profile_),
778 content::Details<const std::string>(&extension_id));
[email protected]d6ebc9792011-04-07 18:18:33779
[email protected]5db9ada2012-04-11 13:48:20780 if (app_sync_bundle_.HasExtensionId(extension_id) &&
781 sync_change.sync_data().GetDataType() == syncable::APPS) {
782 app_sync_bundle_.ProcessDeletion(extension_id, sync_change);
783 } else if (extension_sync_bundle_.HasExtensionId(extension_id) &&
784 sync_change.sync_data().GetDataType() == syncable::EXTENSIONS) {
785 extension_sync_bundle_.ProcessDeletion(extension_id, sync_change);
[email protected]3bdba0d2011-08-23 07:17:30786 }
787
[email protected]333b1de2011-09-12 18:28:50788 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18789 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
790
791 static bool default_apps_trial_exists =
[email protected]568c3312011-12-06 15:36:04792 base::FieldTrialList::TrialExists(kDefaultAppsTrialName);
[email protected]49098f702011-10-13 03:47:18793 if (default_apps_trial_exists) {
794 UMA_HISTOGRAM_ENUMERATION(
795 base::FieldTrial::MakeName("Extensions.ExtensionUninstalled",
[email protected]568c3312011-12-06 15:36:04796 kDefaultAppsTrialName),
[email protected]49098f702011-10-13 03:47:18797 1, 2);
798 }
[email protected]333b1de2011-09-12 18:28:50799
[email protected]7c1490da2011-10-11 18:53:25800 // Uninstalling one extension might have solved the problems of others.
801 // Therefore, we clear warnings of this type for all extensions.
802 std::set<ExtensionWarningSet::WarningType> warnings;
803 extension_warnings_.GetWarningsAffectingExtension(extension_id, &warnings);
804 extension_warnings_.ClearWarnings(warnings);
805
[email protected]d6ebc9792011-04-07 18:18:33806 return true;
[email protected]c10da4b02010-03-25 14:38:32807}
808
[email protected]c3cfb012011-04-06 22:07:35809bool ExtensionService::IsExtensionEnabled(
810 const std::string& extension_id) const {
[email protected]84df8332011-12-06 18:22:46811 if (extensions_.Contains(extension_id) ||
812 terminated_extensions_.Contains(extension_id))
[email protected]36429da2011-07-11 20:25:18813 return true;
814
[email protected]84df8332011-12-06 18:22:46815 if (disabled_extensions_.Contains(extension_id))
[email protected]ad83ca242011-07-29 01:32:25816 return false;
817
818 // If the extension hasn't been loaded yet, check the prefs for it. Assume
819 // enabled unless otherwise noted.
820 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
821 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35822}
823
824bool ExtensionService::IsExternalExtensionUninstalled(
825 const std::string& extension_id) const {
826 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
827}
828
[email protected]eaa7dd182010-12-14 11:09:00829void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12830 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20831
[email protected]06f92562011-04-29 19:27:31832 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39833 return;
[email protected]0c6da502009-08-14 22:32:39834
[email protected]b3317ad2011-04-28 23:46:00835 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]44d62b62012-04-11 00:06:03836 extension_prefs_->RemoveDisableReason(extension_id);
[email protected]1784e83a2009-09-08 21:01:52837
[email protected]06f92562011-04-29 19:27:31838 const Extension* extension =
839 GetExtensionByIdInternal(extension_id, false, true, false);
840 // This can happen if sync enables an extension that is not
841 // installed yet.
842 if (!extension)
843 return;
844
[email protected]0c6da502009-08-14 22:32:39845 // Move it over to the enabled list.
[email protected]84df8332011-12-06 18:22:46846 extensions_.Insert(make_scoped_refptr(extension));
847 disabled_extensions_.Remove(extension->id());
[email protected]0c6da502009-08-14 22:32:39848
[email protected]f681c782010-11-19 11:19:39849 // Make sure any browser action contained within it is not hidden.
850 extension_prefs_->SetBrowserActionVisibility(extension, true);
851
[email protected]62d30f42009-10-01 22:36:06852 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30853
854 SyncExtensionChangeIfNeeded(*extension);
[email protected]0c6da502009-08-14 22:32:39855}
856
[email protected]44d62b62012-04-11 00:06:03857void ExtensionService::DisableExtension(
858 const std::string& extension_id,
859 Extension::DisableReason disable_reason) {
[email protected]a29a517a2011-01-21 21:11:12860 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20861
[email protected]b2ba9962009-12-10 20:10:15862 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:31863 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:52864 return;
[email protected]1784e83a2009-09-08 21:01:52865
[email protected]06f92562011-04-29 19:27:31866 const Extension* extension = GetInstalledExtension(extension_id);
867 // |extension| can be NULL if sync disables an extension that is not
868 // installed yet.
869 if (extension && !Extension::UserMayDisable(extension->location()))
[email protected]95da88c42011-03-31 10:07:33870 return;
871
[email protected]b3317ad2011-04-28 23:46:00872 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]44d62b62012-04-11 00:06:03873 extension_prefs_->SetDisableReason(extension_id, disable_reason);
[email protected]1784e83a2009-09-08 21:01:52874
[email protected]06f92562011-04-29 19:27:31875 extension = GetExtensionByIdInternal(extension_id, true, false, true);
876 if (!extension)
877 return;
878
[email protected]1784e83a2009-09-08 21:01:52879 // Move it over to the disabled list.
[email protected]84df8332011-12-06 18:22:46880 disabled_extensions_.Insert(make_scoped_refptr(extension));
881 if (extensions_.Contains(extension->id()))
882 extensions_.Remove(extension->id());
883 else
884 terminated_extensions_.Remove(extension->id());
[email protected]1784e83a2009-09-08 21:01:52885
[email protected]814a7bf0f2011-08-13 05:30:59886 NotifyExtensionUnloaded(extension, extension_misc::UNLOAD_REASON_DISABLE);
[email protected]3bdba0d2011-08-23 07:17:30887
888 SyncExtensionChangeIfNeeded(*extension);
[email protected]7c1490da2011-10-11 18:53:25889
890 // Deactivating one extension might have solved the problems of others.
891 // Therefore, we clear warnings of this type for all extensions.
892 std::set<ExtensionWarningSet::WarningType> warnings;
893 extension_warnings_.GetWarningsAffectingExtension(extension_id, &warnings);
894 extension_warnings_.ClearWarnings(warnings);
[email protected]1784e83a2009-09-08 21:01:52895}
896
[email protected]eaa7dd182010-12-14 11:09:00897void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]8d888c12010-11-30 00:00:25898 const Extension* extension) {
899 CHECK(extension);
[email protected]fe2dd7742011-04-19 22:52:49900 RecordPermissionMessagesHistogram(
901 extension, "Extensions.Permissions_ReEnable");
[email protected]c333e792012-01-06 16:57:39902 extensions::PermissionsUpdater perms_updater(profile());
903 perms_updater.GrantActivePermissions(extension);
[email protected]8d888c12010-11-30 00:00:25904 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
905 EnableExtension(extension->id());
906}
907
[email protected]fe2dd7742011-04-19 22:52:49908// static
909void ExtensionService::RecordPermissionMessagesHistogram(
910 const Extension* e, const char* histogram) {
911 // Since this is called from multiple sources, and since the Histogram macros
912 // use statics, we need to manually lookup the Histogram ourselves.
913 base::Histogram* counter = base::LinearHistogram::FactoryGet(
914 histogram,
915 1,
[email protected]0d3e4a22011-06-23 19:02:52916 ExtensionPermissionMessage::kEnumBoundary,
917 ExtensionPermissionMessage::kEnumBoundary + 1,
[email protected]fe2dd7742011-04-19 22:52:49918 base::Histogram::kUmaTargetedHistogramFlag);
919
[email protected]0d3e4a22011-06-23 19:02:52920 ExtensionPermissionMessages permissions = e->GetPermissionMessages();
[email protected]fe2dd7742011-04-19 22:52:49921 if (permissions.empty()) {
[email protected]0d3e4a22011-06-23 19:02:52922 counter->Add(ExtensionPermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:49923 } else {
[email protected]0d3e4a22011-06-23 19:02:52924 for (ExtensionPermissionMessages::iterator it = permissions.begin();
925 it != permissions.end(); ++it)
926 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:49927 }
928}
929
[email protected]eaa7dd182010-12-14 11:09:00930void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:42931 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:06932 // was loaded, otherwise a race can arise where a renderer that is created
933 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:42934 // that the request context doesn't yet know about. The profile is responsible
935 // for ensuring its URLRequestContexts appropriately discover the loaded
936 // extension.
[email protected]31d8f5f22012-04-02 15:22:08937 system_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:06938
[email protected]19647262011-12-16 09:57:49939 // Tell renderers about the new extension, unless it's a theme (renderers
940 // don't need to know about themes).
941 if (!extension->is_theme()) {
942 for (content::RenderProcessHost::iterator i(
943 content::RenderProcessHost::AllHostsIterator());
944 !i.IsAtEnd(); i.Advance()) {
945 content::RenderProcessHost* host = i.GetCurrentValue();
946 Profile* host_profile =
947 Profile::FromBrowserContext(host->GetBrowserContext());
948 if (host_profile->GetOriginalProfile() ==
949 profile_->GetOriginalProfile()) {
950 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
951 1, ExtensionMsg_Loaded_Params(extension));
952 host->Send(
953 new ExtensionMsg_Loaded(loaded_extensions));
954 }
[email protected]c8d407e2011-04-28 21:27:17955 }
[email protected]77a6970c2011-04-23 16:58:56956 }
957
[email protected]3442a662012-01-12 08:06:17958 // Tell subsystems that use the EXTENSION_LOADED notification about the new
959 // extension.
960 //
961 // NOTE: It is important that this happen after notifying the renderers about
962 // the new extensions so that if we navigate to an extension URL in
963 // NOTIFICATION_EXTENSION_LOADED, the renderer is guaranteed to know about it.
964 content::NotificationService::current()->Notify(
965 chrome::NOTIFICATION_EXTENSION_LOADED,
966 content::Source<Profile>(profile_),
967 content::Details<const Extension>(extension));
968
[email protected]c8d407e2011-04-28 21:27:17969 // Tell a random-ass collection of other subsystems about the new extension.
970 // TODO(aa): What should we do with all this goop? Can it move into the
971 // relevant objects via EXTENSION_LOADED?
972
973 profile_->GetExtensionSpecialStoragePolicy()->
974 GrantRightsForExtension(extension);
975
976 UpdateActiveExtensionsInCrashReporter();
977
978 ExtensionWebUI::RegisterChromeURLOverrides(
979 profile_, extension->GetChromeURLOverrides());
980
[email protected]8e5c89a2011-06-07 18:13:33981 TemplateURLService* url_service =
982 TemplateURLServiceFactory::GetForProfile(profile_);
983 if (url_service)
984 url_service->RegisterExtensionKeyword(extension);
[email protected]c8d407e2011-04-28 21:27:17985
986 // Load the icon for omnibox-enabled extensions so it will be ready to display
987 // in the URL bar.
988 if (!extension->omnibox_keyword().empty()) {
989 omnibox_popup_icon_manager_.LoadIcon(extension);
990 omnibox_icon_manager_.LoadIcon(extension);
991 }
992
993 // If the extension has permission to load chrome://favicon/ resources we need
994 // to make sure that the FaviconSource is registered with the
995 // ChromeURLDataManager.
996 if (extension->HasHostPermission(GURL(chrome::kChromeUIFaviconURL))) {
997 FaviconSource* favicon_source = new FaviconSource(profile_,
998 FaviconSource::FAVICON);
[email protected]ef92e172012-04-25 19:40:41999 ChromeURLDataManager::AddDataSource(profile_, favicon_source);
[email protected]c8d407e2011-04-28 21:27:171000 }
[email protected]5eddc3e2011-10-26 04:33:311001 // Same for chrome://thumb/ resources.
1002 if (extension->HasHostPermission(GURL(chrome::kChromeUIThumbnailURL))) {
1003 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_);
[email protected]ef92e172012-04-25 19:40:411004 ChromeURLDataManagerFactory::GetForProfile(profile_)->
1005 AddDataSource(thumbnail_source);
[email protected]5eddc3e2011-10-26 04:33:311006 }
[email protected]c8d407e2011-04-28 21:27:171007
1008 // TODO(mpcomplete): This ends up affecting all profiles. See crbug.com/80757.
[email protected]2de307592011-04-05 21:16:581009 bool plugins_changed = false;
1010 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1011 const Extension::PluginInfo& plugin = extension->plugins()[i];
[email protected]f520b5b2011-11-08 02:42:141012 PluginService::GetInstance()->RefreshPlugins();
1013 PluginService::GetInstance()->AddExtraPluginPath(plugin.path);
[email protected]2de307592011-04-05 21:16:581014 plugins_changed = true;
[email protected]476a27032011-09-13 13:40:491015 ChromePluginServiceFilter* filter =
1016 ChromePluginServiceFilter::GetInstance();
1017 if (plugin.is_public) {
1018 filter->RestrictPluginToProfileAndOrigin(
1019 plugin.path, profile_, GURL());
1020 } else {
1021 filter->RestrictPluginToProfileAndOrigin(
1022 plugin.path, profile_, extension->url());
[email protected]2de307592011-04-05 21:16:581023 }
1024 }
[email protected]84396dbc2011-04-14 06:33:421025
1026 bool nacl_modules_changed = false;
1027 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1028 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001029 RegisterNaClModule(module.url, module.mime_type);
[email protected]84396dbc2011-04-14 06:33:421030 nacl_modules_changed = true;
1031 }
1032
[email protected]ed0ba002011-05-26 16:55:131033 if (nacl_modules_changed)
1034 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421035
1036 if (plugins_changed || nacl_modules_changed)
[email protected]45a22e62011-10-12 09:48:021037 PluginService::GetInstance()->PurgePluginListCache(profile_, false);
[email protected]b4d9d122011-06-17 01:58:541038
[email protected]14da67b02011-08-02 05:49:131039#if defined(OS_CHROMEOS)
[email protected]b4d9d122011-06-17 01:58:541040 for (std::vector<Extension::InputComponentInfo>::const_iterator component =
1041 extension->input_components().begin();
1042 component != extension->input_components().end();
1043 ++component) {
[email protected]14da67b02011-08-02 05:49:131044 if (component->type == Extension::INPUT_COMPONENT_TYPE_IME) {
[email protected]8baf0dbb2011-11-27 22:47:021045 ExtensionInputImeEventRouter::GetInstance()->RegisterIme(
[email protected]14da67b02011-08-02 05:49:131046 profile_, extension->id(), *component);
1047 }
[email protected]9a2aebe02011-12-02 21:54:141048#if defined(USE_VIRTUAL_KEYBOARD)
[email protected]b4d9d122011-06-17 01:58:541049 if (component->type == Extension::INPUT_COMPONENT_TYPE_VIRTUAL_KEYBOARD &&
1050 !component->layouts.empty()) {
[email protected]a4215ac02011-12-04 04:11:561051 chromeos::input_method::InputMethodManager* input_method_manager =
1052 chromeos::input_method::InputMethodManager::GetInstance();
1053 const bool is_system_keyboard =
1054 extension->location() == Extension::COMPONENT;
[email protected]cbd4c042011-08-13 18:22:191055 input_method_manager->RegisterVirtualKeyboard(
1056 extension->url(),
1057 component->name, // human-readable name of the keyboard extension.
1058 component->layouts,
[email protected]a4215ac02011-12-04 04:11:561059 is_system_keyboard);
[email protected]b4d9d122011-06-17 01:58:541060 }
[email protected]14da67b02011-08-02 05:49:131061#endif
[email protected]b4d9d122011-06-17 01:58:541062 }
1063#endif
[email protected]62d30f42009-10-01 22:36:061064}
1065
[email protected]a9f39a312010-12-23 22:14:271066void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591067 const Extension* extension,
1068 extension_misc::UnloadedExtensionReason reason) {
[email protected]a9f39a312010-12-23 22:14:271069 UnloadedExtensionInfo details(extension, reason);
[email protected]ad50def52011-10-19 23:17:071070 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271071 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531072 content::Source<Profile>(profile_),
1073 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061074
[email protected]f3b1a082011-11-18 00:34:301075 for (content::RenderProcessHost::iterator i(
1076 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:561077 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:301078 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081079 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301080 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:081081 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171082 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561083 }
1084
[email protected]31d8f5f22012-04-02 15:22:081085 system_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]c8d407e2011-04-28 21:27:171086 profile_->GetExtensionSpecialStoragePolicy()->
1087 RevokeRightsForExtension(extension);
1088
1089 ExtensionWebUI::UnregisterChromeURLOverrides(
1090 profile_, extension->GetChromeURLOverrides());
1091
[email protected]b777b332011-04-16 04:01:081092#if defined(OS_CHROMEOS)
1093 // Revoke external file access to
[email protected]55eb70e762012-02-20 17:38:391094 if (BrowserContext::GetFileSystemContext(profile_) &&
1095 BrowserContext::GetFileSystemContext(profile_)->external_provider()) {
1096 BrowserContext::GetFileSystemContext(profile_)->external_provider()->
[email protected]c8d407e2011-04-28 21:27:171097 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061098 }
[email protected]54ccafa2011-12-06 23:36:321099
1100 if (extension->input_components().size() > 0) {
1101 ExtensionInputImeEventRouter::GetInstance()->UnregisterAllImes(
1102 profile_, extension->id());
1103 }
[email protected]c8d407e2011-04-28 21:27:171104#endif
1105
1106 UpdateActiveExtensionsInCrashReporter();
[email protected]2de307592011-04-05 21:16:581107
1108 bool plugins_changed = false;
1109 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1110 const Extension::PluginInfo& plugin = extension->plugins()[i];
[email protected]b6a2f8de2012-01-31 17:28:491111 PluginService::GetInstance()->ForcePluginShutdown(plugin.path);
[email protected]f520b5b2011-11-08 02:42:141112 PluginService::GetInstance()->RefreshPlugins();
1113 PluginService::GetInstance()->RemoveExtraPluginPath(plugin.path);
[email protected]2de307592011-04-05 21:16:581114 plugins_changed = true;
[email protected]476a27032011-09-13 13:40:491115 ChromePluginServiceFilter::GetInstance()->UnrestrictPlugin(plugin.path);
[email protected]2de307592011-04-05 21:16:581116 }
[email protected]84396dbc2011-04-14 06:33:421117
1118 bool nacl_modules_changed = false;
1119 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1120 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001121 UnregisterNaClModule(module.url);
[email protected]84396dbc2011-04-14 06:33:421122 nacl_modules_changed = true;
1123 }
1124
[email protected]ed0ba002011-05-26 16:55:131125 if (nacl_modules_changed)
1126 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421127
1128 if (plugins_changed || nacl_modules_changed)
[email protected]45a22e62011-10-12 09:48:021129 PluginService::GetInstance()->PurgePluginListCache(profile_, false);
[email protected]62d30f42009-10-01 22:36:061130}
1131
[email protected]eaa7dd182010-12-14 11:09:001132void ExtensionService::UpdateExtensionBlacklist(
[email protected]6b75ec32009-08-14 06:37:181133 const std::vector<std::string>& blacklist) {
1134 // Use this set to indicate if an extension in the blacklist has been used.
1135 std::set<std::string> blacklist_set;
1136 for (unsigned int i = 0; i < blacklist.size(); ++i) {
1137 if (Extension::IdIsValid(blacklist[i])) {
1138 blacklist_set.insert(blacklist[i]);
1139 }
1140 }
1141 extension_prefs_->UpdateBlacklist(blacklist_set);
1142 std::vector<std::string> to_be_removed;
1143 // Loop current extensions, unload installed extensions.
[email protected]84df8332011-12-06 18:22:461144 for (ExtensionSet::const_iterator iter = extensions_.begin();
[email protected]6b75ec32009-08-14 06:37:181145 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021146 const Extension* extension = (*iter);
[email protected]6b75ec32009-08-14 06:37:181147 if (blacklist_set.find(extension->id()) != blacklist_set.end()) {
1148 to_be_removed.push_back(extension->id());
1149 }
1150 }
1151
1152 // UnloadExtension will change the extensions_ list. So, we should
1153 // call it outside the iterator loop.
1154 for (unsigned int i = 0; i < to_be_removed.size(); ++i) {
[email protected]814a7bf0f2011-08-13 05:30:591155 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]6b75ec32009-08-14 06:37:181156 }
1157}
1158
[email protected]0a071a32011-02-08 00:18:241159Profile* ExtensionService::profile() {
1160 return profile_;
1161}
1162
[email protected]eaa7dd182010-12-14 11:09:001163ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451164 return extension_prefs_;
1165}
1166
[email protected]0d9a2202011-11-09 13:48:411167extensions::SettingsFrontend* ExtensionService::settings_frontend() {
[email protected]cccdf0aa2011-11-11 03:43:381168 return settings_frontend_.get();
[email protected]b7f853e282011-08-10 09:24:201169}
1170
[email protected]e2d720aa2012-05-17 00:11:351171extensions::ContentSettingsStore* ExtensionService::GetContentSettingsStore() {
[email protected]b790b072011-05-20 09:46:441172 return extension_prefs()->content_settings_store();
1173}
1174
[email protected]25ae0152011-11-18 14:40:021175bool ExtensionService::is_ready() {
1176 return ready_;
1177}
1178
[email protected]42a08162012-03-16 18:09:111179extensions::ExtensionUpdater* ExtensionService::updater() {
[email protected]2859946f2011-04-04 18:18:061180 return updater_.get();
1181}
1182
[email protected]eaa7dd182010-12-14 11:09:001183void ExtensionService::CheckAdminBlacklist() {
[email protected]aa96d3a2010-08-21 08:45:251184 std::vector<std::string> to_be_removed;
1185 // Loop through extensions list, unload installed extensions.
[email protected]84df8332011-12-06 18:22:461186 for (ExtensionSet::const_iterator iter = extensions_.begin();
[email protected]aa96d3a2010-08-21 08:45:251187 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021188 const Extension* extension = (*iter);
[email protected]05aad2da2011-10-28 10:12:371189 if (!extension_prefs_->IsExtensionAllowedByPolicy(extension->id(),
1190 extension->location())) {
[email protected]aa96d3a2010-08-21 08:45:251191 to_be_removed.push_back(extension->id());
[email protected]05aad2da2011-10-28 10:12:371192 }
[email protected]aa96d3a2010-08-21 08:45:251193 }
1194
1195 // UnloadExtension will change the extensions_ list. So, we should
1196 // call it outside the iterator loop.
1197 for (unsigned int i = 0; i < to_be_removed.size(); ++i)
[email protected]814a7bf0f2011-08-13 05:30:591198 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]aa96d3a2010-08-21 08:45:251199}
1200
[email protected]31206602011-04-13 23:07:321201void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351202 if (updater()) {
[email protected]94fde232012-04-27 10:22:301203 if (AreAllExternalProvidersReady()) {
1204 updater()->CheckSoon();
1205 } else {
1206 // Sync can start updating before all the external providers are ready
1207 // during startup. Start the update as soon as those providers are ready,
1208 // but not before.
1209 update_once_all_providers_are_ready_ = true;
1210 }
[email protected]c3cfb012011-04-06 22:07:351211 } else {
[email protected]31206602011-04-13 23:07:321212 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351213 }
1214}
1215
[email protected]3bdba0d2011-08-23 07:17:301216SyncError ExtensionService::MergeDataAndStartSyncing(
1217 syncable::ModelType type,
1218 const SyncDataList& initial_sync_data,
[email protected]5d36e052012-04-20 20:12:281219 scoped_ptr<SyncChangeProcessor> sync_processor,
1220 scoped_ptr<SyncErrorFactory> sync_error_factory) {
[email protected]5db9ada2012-04-11 13:48:201221 CHECK(sync_processor.get());
[email protected]5d36e052012-04-20 20:12:281222 CHECK(sync_error_factory.get());
[email protected]3bdba0d2011-08-23 07:17:301223
1224 switch (type) {
1225 case syncable::EXTENSIONS:
[email protected]5db9ada2012-04-11 13:48:201226 extension_sync_bundle_.SetupSync(sync_processor.release(),
[email protected]5d36e052012-04-20 20:12:281227 sync_error_factory.release(),
[email protected]5db9ada2012-04-11 13:48:201228 initial_sync_data);
[email protected]3bdba0d2011-08-23 07:17:301229 break;
1230
1231 case syncable::APPS:
[email protected]5d36e052012-04-20 20:12:281232 app_sync_bundle_.SetupSync(sync_processor.release(),
1233 sync_error_factory.release(),
1234 initial_sync_data);
[email protected]3bdba0d2011-08-23 07:17:301235 break;
1236
1237 default:
1238 LOG(FATAL) << "Got " << type << " ModelType";
1239 }
[email protected]3bdba0d2011-08-23 07:17:301240
[email protected]b1417ab2011-12-15 17:09:111241 // Process local extensions.
1242 // TODO(yoz): Determine whether pending extensions should be considered too.
1243 // See crbug.com/104399.
[email protected]3bdba0d2011-08-23 07:17:301244 SyncDataList sync_data_list = GetAllSyncData(type);
1245 SyncChangeList sync_change_list;
1246 for (SyncDataList::const_iterator i = sync_data_list.begin();
1247 i != sync_data_list.end();
1248 ++i) {
[email protected]5db9ada2012-04-11 13:48:201249 switch (type) {
1250 case syncable::EXTENSIONS:
1251 sync_change_list.push_back(
1252 extension_sync_bundle_.CreateSyncChange(*i));
1253 break;
1254 case syncable::APPS:
1255 sync_change_list.push_back(app_sync_bundle_.CreateSyncChange(*i));
1256 break;
1257 default:
1258 LOG(FATAL) << "Got " << type << " ModelType";
1259 }
[email protected]3bdba0d2011-08-23 07:17:301260 }
[email protected]3bdba0d2011-08-23 07:17:301261
[email protected]5db9ada2012-04-11 13:48:201262
1263 if (type == syncable::EXTENSIONS) {
1264 extension_sync_bundle_.ProcessSyncChangeList(sync_change_list);
1265 } else if (type == syncable::APPS) {
1266 app_sync_bundle_.ProcessSyncChangeList(sync_change_list);
1267 }
[email protected]0ffd9e52012-01-26 21:44:341268
[email protected]3bdba0d2011-08-23 07:17:301269 return SyncError();
1270}
1271
1272void ExtensionService::StopSyncing(syncable::ModelType type) {
[email protected]5db9ada2012-04-11 13:48:201273 if (type == syncable::APPS) {
1274 app_sync_bundle_.Reset();
1275 } else if (type == syncable::EXTENSIONS) {
1276 extension_sync_bundle_.Reset();
1277 }
[email protected]3bdba0d2011-08-23 07:17:301278}
1279
1280SyncDataList ExtensionService::GetAllSyncData(syncable::ModelType type) const {
[email protected]5db9ada2012-04-11 13:48:201281 if (type == syncable::EXTENSIONS) {
1282 return extension_sync_bundle_.GetAllSyncData();
1283
1284 } else if (type == syncable::APPS) {
1285 return app_sync_bundle_.GetAllSyncData();
[email protected]3bdba0d2011-08-23 07:17:301286 }
[email protected]5db9ada2012-04-11 13:48:201287
1288 // We should only get sync data for extensions and apps.
1289 NOTREACHED();
1290
1291 return SyncDataList();
[email protected]3bdba0d2011-08-23 07:17:301292}
1293
1294SyncError ExtensionService::ProcessSyncChanges(
1295 const tracked_objects::Location& from_here,
1296 const SyncChangeList& change_list) {
1297 for (SyncChangeList::const_iterator i = change_list.begin();
1298 i != change_list.end();
1299 ++i) {
[email protected]5db9ada2012-04-11 13:48:201300 syncable::ModelType type = i->sync_data().GetDataType();
1301 if (type == syncable::EXTENSIONS) {
1302 extension_sync_bundle_.ProcessSyncChange(
1303 extensions::ExtensionSyncData(*i));
1304 } else if (type == syncable::APPS) {
1305 app_sync_bundle_.ProcessSyncChange(extensions::AppSyncData(*i));
1306 }
[email protected]3bdba0d2011-08-23 07:17:301307 }
1308
[email protected]0ffd9e52012-01-26 21:44:341309 extension_prefs()->extension_sorting()->FixNTPOrdinalCollisions();
1310
[email protected]3bdba0d2011-08-23 07:17:301311 return SyncError();
[email protected]b05fb9ff2011-04-23 00:07:561312}
1313
[email protected]5db9ada2012-04-11 13:48:201314extensions::ExtensionSyncData ExtensionService::GetExtensionSyncData(
1315 const Extension& extension) const {
1316 return extensions::ExtensionSyncData(extension,
1317 IsExtensionEnabled(extension.id()),
1318 IsIncognitoEnabled(extension.id()));
[email protected]b05fb9ff2011-04-23 00:07:561319}
1320
[email protected]5db9ada2012-04-11 13:48:201321extensions::AppSyncData ExtensionService::GetAppSyncData(
1322 const Extension& extension) const {
1323 return extensions::AppSyncData(
1324 extension,
1325 IsExtensionEnabled(extension.id()),
1326 IsIncognitoEnabled(extension.id()),
1327 extension_prefs_->GetAppNotificationClientId(extension.id()),
1328 extension_prefs_->IsAppNotificationDisabled(extension.id()),
1329 extension_prefs_->extension_sorting()->GetAppLaunchOrdinal(
1330 extension.id()),
1331 extension_prefs_->extension_sorting()->GetPageOrdinal(extension.id()));
1332}
[email protected]3bdba0d2011-08-23 07:17:301333
[email protected]5db9ada2012-04-11 13:48:201334std::vector<extensions::ExtensionSyncData>
1335 ExtensionService::GetExtensionSyncDataList() const {
1336 std::vector<extensions::ExtensionSyncData> extension_sync_list;
1337 extension_sync_bundle_.GetExtensionSyncDataListHelper(extensions_,
1338 &extension_sync_list);
1339 extension_sync_bundle_.GetExtensionSyncDataListHelper(disabled_extensions_,
1340 &extension_sync_list);
1341 extension_sync_bundle_.GetExtensionSyncDataListHelper(terminated_extensions_,
1342 &extension_sync_list);
1343
1344 std::vector<extensions::ExtensionSyncData> pending_extensions =
1345 extension_sync_bundle_.GetPendingData();
1346 extension_sync_list.insert(extension_sync_list.begin(),
1347 pending_extensions.begin(),
1348 pending_extensions.end());
[email protected]3bdba0d2011-08-23 07:17:301349
1350 return extension_sync_list;
[email protected]b05fb9ff2011-04-23 00:07:561351}
1352
[email protected]5db9ada2012-04-11 13:48:201353std::vector<extensions::AppSyncData> ExtensionService::GetAppSyncDataList()
1354 const {
1355 std::vector<extensions::AppSyncData> app_sync_list;
1356 app_sync_bundle_.GetAppSyncDataListHelper(extensions_, &app_sync_list);
1357 app_sync_bundle_.GetAppSyncDataListHelper(disabled_extensions_,
1358 &app_sync_list);
1359 app_sync_bundle_.GetAppSyncDataListHelper(terminated_extensions_,
1360 &app_sync_list);
1361
1362 std::vector<extensions::AppSyncData> pending_apps =
1363 app_sync_bundle_.GetPendingData();
1364 app_sync_list.insert(app_sync_list.begin(),
1365 pending_apps.begin(),
1366 pending_apps.end());
1367
1368 return app_sync_list;
1369}
1370
1371bool ExtensionService::ProcessExtensionSyncData(
1372 const extensions::ExtensionSyncData& extension_sync_data) {
1373 if (!ProcessExtensionSyncDataHelper(extension_sync_data,
1374 syncable::EXTENSIONS)) {
1375 extension_sync_bundle_.AddPendingExtension(extension_sync_data.id(),
1376 extension_sync_data);
1377 CheckForUpdatesSoon();
1378 return false;
1379 }
1380
1381 return true;
1382}
1383
1384bool ExtensionService::ProcessAppSyncData(
1385 const extensions::AppSyncData& app_sync_data) {
1386 const std::string& id = app_sync_data.id();
1387 const Extension* extension = GetInstalledExtension(id);
1388 bool extension_installed = (extension != NULL);
1389
1390 if (app_sync_data.app_launch_ordinal().IsValid() &&
1391 app_sync_data.page_ordinal().IsValid()) {
1392 extension_prefs_->extension_sorting()->SetAppLaunchOrdinal(
1393 id,
1394 app_sync_data.app_launch_ordinal());
1395 extension_prefs_->extension_sorting()->SetPageOrdinal(
1396 id,
1397 app_sync_data.page_ordinal());
1398 }
1399
1400 if (extension_installed) {
1401 if (app_sync_data.notifications_disabled() !=
1402 extension_prefs_->IsAppNotificationDisabled(id)) {
1403 extension_prefs_->SetAppNotificationDisabled(
1404 id, app_sync_data.notifications_disabled());
1405 }
1406 }
1407
1408 if (!ProcessExtensionSyncDataHelper(app_sync_data.extension_sync_data(),
1409 syncable::APPS)) {
1410 app_sync_bundle_.AddPendingApp(id, app_sync_data);
1411 CheckForUpdatesSoon();
1412 return false;
1413 }
1414
1415 return true;
1416}
1417
1418bool ExtensionService::IsCorrectSyncType(const Extension& extension,
1419 syncable::ModelType type) const {
1420 if (type == syncable::EXTENSIONS &&
1421 extension.GetSyncType() == Extension::SYNC_TYPE_EXTENSION)
1422 return true;
1423
1424 if (type == syncable::APPS &&
1425 extension.GetSyncType() == Extension::SYNC_TYPE_APP)
1426 return true;
1427
1428 return false;
1429}
1430
1431bool ExtensionService::ProcessExtensionSyncDataHelper(
1432 const extensions::ExtensionSyncData& extension_sync_data,
1433 syncable::ModelType type) {
[email protected]3bdba0d2011-08-23 07:17:301434 const std::string& id = extension_sync_data.id();
[email protected]96e989b2011-08-30 19:35:061435 const Extension* extension = GetInstalledExtension(id);
1436
1437 // TODO(bolms): we should really handle this better. The particularly bad
1438 // case is where an app becomes an extension or vice versa, and we end up with
1439 // a zombie extension that won't go away.
[email protected]5db9ada2012-04-11 13:48:201440 if (extension && !IsCorrectSyncType(*extension, type))
1441 return true;
[email protected]90310d92011-04-17 07:35:041442
1443 // Handle uninstalls first.
[email protected]3bdba0d2011-08-23 07:17:301444 if (extension_sync_data.uninstalled()) {
[email protected]90310d92011-04-17 07:35:041445 std::string error;
1446 if (!UninstallExtensionHelper(this, id)) {
1447 LOG(WARNING) << "Could not uninstall extension " << id
1448 << " for sync";
1449 }
[email protected]5db9ada2012-04-11 13:48:201450 return true;
[email protected]90310d92011-04-17 07:35:041451 }
1452
[email protected]caf2d442012-05-04 10:00:501453 // Extension from sync was uninstalled by the user as external extensions.
1454 // Honor user choice and skip installation/enabling.
1455 if (IsExternalExtensionUninstalled(id)) {
1456 LOG(WARNING) << "Extension with id " << id
1457 << " from sync was uninstalled as external extension";
1458 return true;
1459 }
1460
[email protected]06f92562011-04-29 19:27:311461 // Set user settings.
[email protected]3bdba0d2011-08-23 07:17:301462 if (extension_sync_data.enabled()) {
[email protected]06f92562011-04-29 19:27:311463 EnableExtension(id);
1464 } else {
[email protected]44d62b62012-04-11 00:06:031465 DisableExtension(id, Extension::DISABLE_USER_ACTION);
[email protected]06f92562011-04-29 19:27:311466 }
[email protected]81b14cc2011-04-29 00:39:371467
[email protected]406b5a92011-11-08 11:58:261468 // We need to cache some version information here because setting the
1469 // incognito flag invalidates the |extension| pointer (it reloads the
1470 // extension).
[email protected]9060d8b02012-01-13 02:14:301471 bool extension_installed = (extension != NULL);
[email protected]406b5a92011-11-08 11:58:261472 int result = extension ?
1473 extension->version()->CompareTo(extension_sync_data.version()) : 0;
1474 SetIsIncognitoEnabled(id, extension_sync_data.incognito_enabled());
1475 extension = NULL; // No longer safe to use.
1476
1477 if (extension_installed) {
[email protected]06f92562011-04-29 19:27:311478 // If the extension is already installed, check if it's outdated.
[email protected]90310d92011-04-17 07:35:041479 if (result < 0) {
1480 // Extension is outdated.
[email protected]5db9ada2012-04-11 13:48:201481 return false;
[email protected]b020dc12011-12-06 04:35:421482 }
[email protected]06f92562011-04-29 19:27:311483 } else {
[email protected]06f92562011-04-29 19:27:311484 // TODO(akalin): Replace silent update with a list of enabled
1485 // permissions.
[email protected]6cc7dbae2011-04-29 21:18:331486 const bool kInstallSilently = true;
[email protected]5db9ada2012-04-11 13:48:201487
1488 CHECK(type == syncable::EXTENSIONS || type == syncable::APPS);
1489 ExtensionFilter filter =
1490 (type == syncable::APPS) ? IsSyncableApp : IsSyncableExtension;
1491
[email protected]06f92562011-04-29 19:27:311492 if (!pending_extension_manager()->AddFromSync(
1493 id,
[email protected]3bdba0d2011-08-23 07:17:301494 extension_sync_data.update_url(),
[email protected]5db9ada2012-04-11 13:48:201495 filter,
[email protected]6cc7dbae2011-04-29 21:18:331496 kInstallSilently)) {
[email protected]06f92562011-04-29 19:27:311497 LOG(WARNING) << "Could not add pending extension for " << id;
[email protected]3bdba0d2011-08-23 07:17:301498 // This means that the extension is already pending installation, with a
1499 // non-INTERNAL location. Add to pending_sync_data, even though it will
1500 // never be removed (we'll never install a syncable version of the
1501 // extension), so that GetAllSyncData() continues to send it.
[email protected]06f92562011-04-29 19:27:311502 }
[email protected]3bdba0d2011-08-23 07:17:301503 // Track pending extensions so that we can return them in GetAllSyncData().
[email protected]5db9ada2012-04-11 13:48:201504 return false;
[email protected]90310d92011-04-17 07:35:041505 }
[email protected]5db9ada2012-04-11 13:48:201506
1507 return true;
[email protected]90310d92011-04-17 07:35:041508}
1509
[email protected]c3cfb012011-04-06 22:07:351510bool ExtensionService::IsIncognitoEnabled(
1511 const std::string& extension_id) const {
1512 // If this is an existing component extension we always allow it to
1513 // work in incognito mode.
[email protected]81b14cc2011-04-29 00:39:371514 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]c3cfb012011-04-06 22:07:351515 if (extension && extension->location() == Extension::COMPONENT)
[email protected]cb0ce1e022010-03-10 19:54:411516 return true;
1517
1518 // Check the prefs.
[email protected]c3cfb012011-04-06 22:07:351519 return extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]db7331a2010-02-25 22:10:501520}
[email protected]55a35692010-02-11 23:25:211521
[email protected]c3cfb012011-04-06 22:07:351522void ExtensionService::SetIsIncognitoEnabled(
1523 const std::string& extension_id, bool enabled) {
[email protected]81b14cc2011-04-29 00:39:371524 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]c3cfb012011-04-06 22:07:351525 if (extension && extension->location() == Extension::COMPONENT) {
[email protected]b873cd92012-02-09 21:51:481526 // This shouldn't be called for component extensions unless they are
1527 // syncable.
1528 DCHECK(extension->IsSyncable());
1529
1530 // If we are here, make sure the we aren't trying to change the value.
1531 DCHECK_EQ(enabled, IsIncognitoEnabled(extension_id));
1532
[email protected]c3cfb012011-04-06 22:07:351533 return;
1534 }
1535
[email protected]8c6c8c22011-03-09 12:52:341536 // Broadcast unloaded and loaded events to update browser state. Only bother
1537 // if the value changed and the extension is actually enabled, since there is
1538 // no UI otherwise.
[email protected]c3cfb012011-04-06 22:07:351539 bool old_enabled = extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]8c6c8c22011-03-09 12:52:341540 if (enabled == old_enabled)
1541 return;
1542
[email protected]c3cfb012011-04-06 22:07:351543 extension_prefs_->SetIsIncognitoEnabled(extension_id, enabled);
[email protected]81b14cc2011-04-29 00:39:371544
[email protected]ffbf3f12011-12-09 22:10:331545 bool extension_is_enabled = extensions_.Contains(extension_id);
[email protected]bd9892eb2011-11-11 19:41:341546
1547 // When we reload the extension the ID may be invalidated if we've passed it
1548 // by const ref everywhere. Make a copy to be safe.
1549 std::string id = extension_id;
[email protected]406b5a92011-11-08 11:58:261550 if (extension_is_enabled)
[email protected]ffbf3f12011-12-09 22:10:331551 ReloadExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301552
[email protected]406b5a92011-11-08 11:58:261553 // Reloading the extension invalidates the |extension| pointer.
[email protected]bd9892eb2011-11-11 19:41:341554 extension = GetInstalledExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301555 if (extension)
1556 SyncExtensionChangeIfNeeded(*extension);
[email protected]55a35692010-02-11 23:25:211557}
1558
[email protected]aa7599d2011-10-28 07:24:321559void ExtensionService::SetAppNotificationSetupDone(
1560 const std::string& extension_id,
[email protected]b2689a902011-12-01 00:41:091561 const std::string& oauth_client_id) {
[email protected]aa7599d2011-10-28 07:24:321562 const Extension* extension = GetInstalledExtension(extension_id);
1563 // This method is called when the user sets up app notifications.
1564 // So it is not expected to be called until the extension is installed.
1565 if (!extension) {
1566 NOTREACHED();
1567 return;
1568 }
[email protected]b2689a902011-12-01 00:41:091569 extension_prefs_->SetAppNotificationClientId(extension_id, oauth_client_id);
[email protected]aa7599d2011-10-28 07:24:321570 SyncExtensionChangeIfNeeded(*extension);
1571}
1572
1573void ExtensionService::SetAppNotificationDisabled(
1574 const std::string& extension_id,
1575 bool value) {
1576 const Extension* extension = GetInstalledExtension(extension_id);
1577 // This method is called when the user enables/disables app notifications.
1578 // So it is not expected to be called until the extension is installed.
1579 if (!extension) {
1580 NOTREACHED();
1581 return;
1582 }
[email protected]97e856b12011-12-14 00:21:131583 if (value)
1584 UMA_HISTOGRAM_COUNTS("Apps.SetAppNotificationsDisabled", 1);
1585 else
1586 UMA_HISTOGRAM_COUNTS("Apps.SetAppNotificationsEnabled", 1);
[email protected]aa7599d2011-10-28 07:24:321587 extension_prefs_->SetAppNotificationDisabled(extension_id, value);
1588 SyncExtensionChangeIfNeeded(*extension);
1589}
1590
[email protected]eaa7dd182010-12-14 11:09:001591bool ExtensionService::CanCrossIncognito(const Extension* extension) {
[email protected]2a8f24e2010-11-03 21:37:051592 // We allow the extension to see events and data from another profile iff it
1593 // uses "spanning" behavior and it has incognito access. "split" mode
1594 // extensions only see events for a matching profile.
[email protected]0c41298f2011-09-13 21:17:221595 CHECK(extension);
[email protected]c3cfb012011-04-06 22:07:351596 return IsIncognitoEnabled(extension->id()) &&
1597 !extension->incognito_split_mode();
[email protected]2a8f24e2010-11-03 21:37:051598}
1599
[email protected]5ef7b002011-05-12 23:16:201600bool ExtensionService::CanLoadInIncognito(const Extension* extension) const {
1601 if (extension->is_hosted_app())
1602 return true;
1603 // Packaged apps and regular extensions need to be enabled specifically for
1604 // incognito (and split mode should be set).
1605 return extension->incognito_split_mode() &&
1606 IsIncognitoEnabled(extension->id());
1607}
1608
[email protected]168389f2011-12-20 17:12:481609void ExtensionService::OnExtensionMoved(
1610 const std::string& moved_extension_id,
1611 const std::string& predecessor_extension_id,
1612 const std::string& successor_extension_id) {
[email protected]3569b502012-01-12 20:08:231613 extension_prefs_->extension_sorting()->OnExtensionMoved(
1614 moved_extension_id,
1615 predecessor_extension_id,
1616 successor_extension_id);
[email protected]168389f2011-12-20 17:12:481617
1618 const Extension* extension = GetInstalledExtension(moved_extension_id);
1619 if (extension)
1620 SyncExtensionChangeIfNeeded(*extension);
1621}
1622
[email protected]eaa7dd182010-12-14 11:09:001623bool ExtensionService::AllowFileAccess(const Extension* extension) {
[email protected]05c82182010-06-24 17:49:081624 return (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]334e04a2010-06-24 23:34:441625 switches::kDisableExtensionsFileAccessCheck) ||
[email protected]05c82182010-06-24 17:49:081626 extension_prefs_->AllowFileAccess(extension->id()));
1627}
1628
[email protected]eaa7dd182010-12-14 11:09:001629void ExtensionService::SetAllowFileAccess(const Extension* extension,
[email protected]3aff9ad2011-04-01 20:26:481630 bool allow) {
1631 // Reload to update browser state. Only bother if the value changed and the
1632 // extension is actually enabled, since there is no UI otherwise.
1633 bool old_allow = AllowFileAccess(extension);
1634 if (allow == old_allow)
1635 return;
1636
[email protected]05c82182010-06-24 17:49:081637 extension_prefs_->SetAllowFileAccess(extension->id(), allow);
[email protected]3aff9ad2011-04-01 20:26:481638
[email protected]84df8332011-12-06 18:22:461639 bool extension_is_enabled = extensions_.Contains(extension->id());
[email protected]3aff9ad2011-04-01 20:26:481640 if (extension_is_enabled)
1641 ReloadExtension(extension->id());
[email protected]05c82182010-06-24 17:49:081642}
1643
[email protected]8e4560b62011-01-14 10:09:141644// Some extensions will autoupdate themselves externally from Chrome. These
1645// are typically part of some larger client application package. To support
1646// these, the extension will register its location in the the preferences file
1647// (and also, on Windows, in the registry) and this code will periodically
1648// check that location for a .crx file, which it will then install locally if
1649// a new version is available.
1650// Errors are reported through ExtensionErrorReporter. Succcess is not
1651// reported.
[email protected]eaa7dd182010-12-14 11:09:001652void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121653 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141654
1655 // Note that this installation is intentionally silent (since it didn't
1656 // go through the front-end). Extensions that are registered in this
1657 // way are effectively considered 'pre-bundled', and so implicitly
1658 // trusted. In general, if something has HKLM or filesystem access,
1659 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121660
[email protected]8e4560b62011-01-14 10:09:141661 // Ask each external extension provider to give us a call back for each
1662 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
1663 ProviderCollection::const_iterator i;
1664 for (i = external_extension_providers_.begin();
1665 i != external_extension_providers_.end(); ++i) {
1666 ExternalExtensionProviderInterface* provider = i->get();
1667 provider->VisitRegisteredExtension();
1668 }
1669
[email protected]50067e52011-10-20 23:17:071670 // Do any required work that we would have done after completion of all
1671 // providers.
1672 if (external_extension_providers_.empty()) {
1673 OnAllExternalProvidersReady();
1674 }
[email protected]9f1087e2009-06-15 17:29:321675}
1676
[email protected]50067e52011-10-20 23:17:071677void ExtensionService::OnExternalProviderReady(
1678 const ExternalExtensionProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121679 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071680 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121681
1682 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141683 // if all of them are finished. So we check them first.
[email protected]94fde232012-04-27 10:22:301684 if (AreAllExternalProvidersReady())
1685 OnAllExternalProvidersReady();
1686}
1687
1688bool ExtensionService::AreAllExternalProvidersReady() const {
[email protected]8e4560b62011-01-14 10:09:141689 ProviderCollection::const_iterator i;
1690 for (i = external_extension_providers_.begin();
1691 i != external_extension_providers_.end(); ++i) {
[email protected]94fde232012-04-27 10:22:301692 if (!i->get()->IsReady())
1693 return false;
[email protected]8e4560b62011-01-14 10:09:141694 }
[email protected]94fde232012-04-27 10:22:301695 return true;
[email protected]50067e52011-10-20 23:17:071696}
1697
1698void ExtensionService::OnAllExternalProvidersReady() {
1699 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]94fde232012-04-27 10:22:301700 base::TimeDelta elapsed = base::Time::Now() - profile_->GetStartTime();
1701 UMA_HISTOGRAM_TIMES("Extension.ExternalProvidersReadyAfter", elapsed);
[email protected]50067e52011-10-20 23:17:071702
1703 // Install any pending extensions.
[email protected]94fde232012-04-27 10:22:301704 if (update_once_all_providers_are_ready_ && updater()) {
1705 update_once_all_providers_are_ready_ = false;
[email protected]8e4560b62011-01-14 10:09:141706 updater()->CheckNow();
1707 }
1708
1709 // Uninstall all the unclaimed extensions.
1710 scoped_ptr<ExtensionPrefs::ExtensionsInfo> extensions_info(
1711 extension_prefs_->GetInstalledExtensionsInfo());
1712 for (size_t i = 0; i < extensions_info->size(); ++i) {
1713 ExtensionInfo* info = extensions_info->at(i).get();
1714 if (Extension::IsExternalLocation(info->extension_location))
1715 CheckExternalUninstall(info->extension_id);
1716 }
[email protected]e5af875f2011-10-10 21:09:141717 IdentifyAlertableExtensions();
[email protected]e5af875f2011-10-10 21:09:141718}
1719
1720void ExtensionService::IdentifyAlertableExtensions() {
1721 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1722
[email protected]07c9f2f42012-02-29 18:45:221723 // Build up the lists of extensions that require acknowledgment. If this is
1724 // the first time, grandfather extensions that would have caused
1725 // notification.
[email protected]a9aa5932012-01-25 08:27:401726 extension_global_error_.reset(new ExtensionGlobalError(this));
[email protected]e5af875f2011-10-10 21:09:141727
[email protected]a9aa5932012-01-25 08:27:401728 bool did_show_alert = false;
[email protected]07c9f2f42012-02-29 18:45:221729 if (PopulateExtensionGlobalError(extension_global_error_.get())) {
[email protected]e5af875f2011-10-10 21:09:141730 if (extension_prefs_->SetAlertSystemFirstRun()) {
[email protected]a9aa5932012-01-25 08:27:401731 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]d8748142012-05-16 21:13:431732 Browser* browser = browser::FindLastActiveWithProfile(profile_);
[email protected]a9aa5932012-01-25 08:27:401733 if (browser) {
1734 extension_global_error_->ShowBubbleView(browser);
1735 did_show_alert = true;
1736 }
[email protected]e5af875f2011-10-10 21:09:141737 } else {
1738 // First run. Just acknowledge all the extensions, silently, by
1739 // shortcutting the display of the UI and going straight to the
1740 // callback for pressing the Accept button.
[email protected]a9aa5932012-01-25 08:27:401741 HandleExtensionAlertAccept();
[email protected]e5af875f2011-10-10 21:09:141742 }
1743 }
[email protected]a9aa5932012-01-25 08:27:401744
1745 if (!did_show_alert)
1746 extension_global_error_.reset();
[email protected]e5af875f2011-10-10 21:09:141747}
1748
[email protected]07c9f2f42012-02-29 18:45:221749bool ExtensionService::PopulateExtensionGlobalError(
1750 ExtensionGlobalError* extension_global_error) {
1751 bool needs_alert = false;
1752 for (ExtensionSet::const_iterator iter = extensions_.begin();
1753 iter != extensions_.end(); ++iter) {
1754 const Extension* e = *iter;
1755 if (Extension::IsExternalLocation(e->location())) {
1756 if (!e->is_hosted_app()) {
1757 if (!extension_prefs_->IsExternalExtensionAcknowledged(e->id())) {
1758 extension_global_error->AddExternalExtension(e->id());
1759 needs_alert = true;
1760 }
1761 }
1762 }
1763 if (extension_prefs_->IsExtensionBlacklisted(e->id())) {
1764 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(e->id())) {
1765 extension_global_error->AddBlacklistedExtension(e->id());
1766 needs_alert = true;
1767 }
1768 }
1769 if (extension_prefs_->IsExtensionOrphaned(e->id())) {
1770 if (!extension_prefs_->IsOrphanedExtensionAcknowledged(e->id())) {
1771 extension_global_error->AddOrphanedExtension(e->id());
1772 needs_alert = true;
1773 }
1774 }
1775 }
1776 return needs_alert;
1777}
1778
[email protected]a9aa5932012-01-25 08:27:401779void ExtensionService::HandleExtensionAlertClosed() {
1780 extension_global_error_.reset();
[email protected]62433d32011-10-12 22:33:121781}
1782
[email protected]a9aa5932012-01-25 08:27:401783void ExtensionService::HandleExtensionAlertAccept() {
[email protected]e5af875f2011-10-10 21:09:141784 const ExtensionIdSet *extension_ids =
[email protected]a9aa5932012-01-25 08:27:401785 extension_global_error_->get_external_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141786 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1787 iter != extension_ids->end(); ++iter) {
[email protected]47fc70c2011-12-06 07:29:511788 AcknowledgeExternalExtension(*iter);
[email protected]e5af875f2011-10-10 21:09:141789 }
[email protected]a9aa5932012-01-25 08:27:401790 extension_ids = extension_global_error_->get_blacklisted_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141791 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1792 iter != extension_ids->end(); ++iter) {
1793 extension_prefs_->AcknowledgeBlacklistedExtension(*iter);
1794 }
[email protected]a9aa5932012-01-25 08:27:401795 extension_ids = extension_global_error_->get_orphaned_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141796 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1797 iter != extension_ids->end(); ++iter) {
1798 extension_prefs_->AcknowledgeOrphanedExtension(*iter);
1799 }
1800}
1801
[email protected]47fc70c2011-12-06 07:29:511802void ExtensionService::AcknowledgeExternalExtension(const std::string& id) {
1803 extension_prefs_->AcknowledgeExternalExtension(id);
1804}
1805
[email protected]a9aa5932012-01-25 08:27:401806void ExtensionService::HandleExtensionAlertDetails(Browser* browser) {
1807 DCHECK(browser);
1808 browser->ShowExtensionsTab();
[email protected]6c751e72010-11-23 10:11:101809}
1810
[email protected]a9f39a312010-12-23 22:14:271811void ExtensionService::UnloadExtension(
1812 const std::string& extension_id,
[email protected]814a7bf0f2011-08-13 05:30:591813 extension_misc::UnloadedExtensionReason reason) {
[email protected]27e469a2010-01-11 20:35:091814 // Make sure the extension gets deleted after we return from this function.
[email protected]9adb9692010-10-29 23:14:021815 scoped_refptr<const Extension> extension(
[email protected]8001df22011-04-28 19:59:471816 GetExtensionByIdInternal(extension_id, true, true, false));
[email protected]631cf822009-05-15 07:01:251817
[email protected]fa0c96732010-11-17 00:14:231818 // This method can be called via PostTask, so the extension may have been
1819 // unloaded by the time this runs.
[email protected]dd163fb02011-05-04 22:22:171820 if (!extension) {
1821 // In case the extension may have crashed/uninstalled. Allow the profile to
1822 // clean up its RequestContexts.
[email protected]31d8f5f22012-04-02 15:22:081823 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231824 return;
[email protected]dd163fb02011-05-04 22:22:171825 }
[email protected]0c6da502009-08-14 22:32:391826
[email protected]1eb175082010-02-10 09:26:161827 // Keep information about the extension so that we can reload it later
1828 // even if it's not permanently installed.
1829 unloaded_extension_paths_[extension->id()] = extension->path();
1830
[email protected]f17dbd42010-08-16 23:21:101831 // Clean up if the extension is meant to be enabled after a reload.
1832 disabled_extension_paths_.erase(extension->id());
1833
[email protected]d7e9a862010-11-03 21:57:491834 // Clean up runtime data.
1835 extension_runtime_data_.erase(extension_id);
1836
[email protected]614d18c22012-04-26 16:59:221837if (disabled_extensions_.Contains(extension->id())) {
[email protected]a9f39a312010-12-23 22:14:271838 UnloadedExtensionInfo details(extension, reason);
1839 details.already_disabled = true;
[email protected]84df8332011-12-06 18:22:461840 disabled_extensions_.Remove(extension->id());
[email protected]ad50def52011-10-19 23:17:071841 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271842 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531843 content::Source<Profile>(profile_),
1844 content::Details<UnloadedExtensionInfo>(&details));
[email protected]dd163fb02011-05-04 22:22:171845 // Make sure the profile cleans up its RequestContexts when an already
1846 // disabled extension is unloaded (since they are also tracking the disabled
1847 // extensions).
[email protected]31d8f5f22012-04-02 15:22:081848 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]0c6da502009-08-14 22:32:391849 return;
1850 }
1851
[email protected]84df8332011-12-06 18:22:461852// Remove the extension from our list.
1853 extensions_.Remove(extension->id());
[email protected]631cf822009-05-15 07:01:251854
[email protected]a9f39a312010-12-23 22:14:271855 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]631cf822009-05-15 07:01:251856}
1857
[email protected]eaa7dd182010-12-14 11:09:001858void ExtensionService::UnloadAllExtensions() {
[email protected]c8d407e2011-04-28 21:27:171859 profile_->GetExtensionSpecialStoragePolicy()->
1860 RevokeRightsForAllExtensions();
1861
[email protected]84df8332011-12-06 18:22:461862 extensions_.Clear();
1863 disabled_extensions_.Clear();
1864 terminated_extensions_.Clear();
[email protected]d7e9a862010-11-03 21:57:491865 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:321866
[email protected]c6e4a3412009-06-24 15:45:291867 // TODO(erikkay) should there be a notification for this? We can't use
1868 // EXTENSION_UNLOADED since that implies that the extension has been disabled
1869 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:321870}
1871
[email protected]eaa7dd182010-12-14 11:09:001872void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:321873 UnloadAllExtensions();
[email protected]d8c8f25f2011-11-02 18:18:011874 component_loader_->LoadAll();
1875 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]9f1087e2009-06-15 17:29:321876}
1877
[email protected]eaa7dd182010-12-14 11:09:001878void ExtensionService::GarbageCollectExtensions() {
[email protected]614d18c22012-04-26 16:59:221879 if (extension_prefs_->pref_service()->ReadOnly())
1880 return;
1881
1882 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(
1883 extension_prefs_->GetInstalledExtensionsInfo());
1884
1885 std::map<std::string, FilePath> extension_paths;
1886 for (size_t i = 0; i < info->size(); ++i)
1887 extension_paths[info->at(i)->extension_id] = info->at(i)->extension_path;
1888
1889 if (!BrowserThread::PostTask(
1890 BrowserThread::FILE, FROM_HERE,
1891 base::Bind(
1892 &extension_file_util::GarbageCollectExtensions,
1893 install_directory_,
1894 extension_paths)))
1895 NOTREACHED();
[email protected]f6ccd652010-11-17 00:48:341896
1897 // Also garbage-collect themes. We check |profile_| to be
1898 // defensive; in the future, we may call GarbageCollectExtensions()
1899 // from somewhere other than Init() (e.g., in a timer).
1900 if (profile_) {
[email protected]18280372011-03-22 18:05:221901 ThemeServiceFactory::GetForProfile(profile_)->RemoveUnusedThemes();
[email protected]f6ccd652010-11-17 00:48:341902 }
[email protected]3cf4f0992009-02-03 23:00:301903}
1904
[email protected]fb82dcd2012-03-21 14:15:461905void ExtensionService::SyncExtensionChangeIfNeeded(const Extension& extension) {
[email protected]5db9ada2012-04-11 13:48:201906 if (app_sync_bundle_.HandlesApp(extension)) {
1907 app_sync_bundle_.SyncChangeIfNeeded(extension);
1908 } else if (extension_sync_bundle_.HandlesExtension(extension)) {
1909 extension_sync_bundle_.SyncChangeIfNeeded(extension);
[email protected]fb82dcd2012-03-21 14:15:461910 }
1911}
1912
[email protected]eaa7dd182010-12-14 11:09:001913void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]93fd78f42009-07-10 16:43:171914 if (updater_.get()) {
1915 updater_->Start();
1916 }
[email protected]fa6a9102010-11-22 15:38:501917
1918 ready_ = true;
[email protected]ad50def52011-10-19 23:17:071919 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271920 chrome::NOTIFICATION_EXTENSIONS_READY,
[email protected]6c2381d2011-10-19 02:52:531921 content::Source<Profile>(profile_),
[email protected]ad50def52011-10-19 23:17:071922 content::NotificationService::NoDetails());
[email protected]e72e8eb82009-06-18 17:21:511923}
1924
[email protected]fcb58a862012-05-01 01:03:151925void ExtensionService::AddExtension(const Extension* extension) {
[email protected]ae09ca62009-08-21 19:46:461926 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:021927 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]9f1087e2009-06-15 17:29:321928
[email protected]c8d407e2011-04-28 21:27:171929 // TODO(jstritar): We may be able to get rid of this branch by overriding the
1930 // default extension state to DISABLED when the --disable-extensions flag
1931 // is set (http://crbug.com/29067).
1932 if (!extensions_enabled() &&
1933 !extension->is_theme() &&
1934 extension->location() != Extension::COMPONENT &&
1935 !Extension::IsExternalLocation(extension->location()))
[email protected]fcb58a862012-05-01 01:03:151936 return;
[email protected]c8d407e2011-04-28 21:27:171937
1938 SetBeingUpgraded(extension, false);
1939
[email protected]1eb175082010-02-10 09:26:161940 // The extension is now loaded, remove its data from unloaded extension map.
1941 unloaded_extension_paths_.erase(extension->id());
1942
[email protected]bb7f40952011-01-13 00:21:201943 // If a terminated extension is loaded, remove it from the terminated list.
1944 UntrackTerminatedExtension(extension->id());
1945
[email protected]f17dbd42010-08-16 23:21:101946 // If the extension was disabled for a reload, then enable it.
1947 if (disabled_extension_paths_.erase(extension->id()) > 0)
1948 EnableExtension(extension->id());
1949
[email protected]d728e002010-12-08 04:46:231950 // Check if the extension's privileges have changed and disable the
1951 // extension if necessary.
[email protected]902fd7b2011-07-27 18:42:311952 InitializePermissions(extension);
[email protected]0c6da502009-08-14 22:32:391953
[email protected]ad83ca242011-07-29 01:32:251954 bool disabled = extension_prefs_->IsExtensionDisabled(extension->id());
[email protected]36429da2011-07-11 20:25:181955 if (disabled) {
[email protected]84df8332011-12-06 18:22:461956 disabled_extensions_.Insert(scoped_extension);
[email protected]3bdba0d2011-08-23 07:17:301957 SyncExtensionChangeIfNeeded(*extension);
[email protected]44d62b62012-04-11 00:06:031958 content::NotificationService::current()->Notify(
1959 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
1960 content::Source<Profile>(profile_),
1961 content::Details<const Extension>(extension));
[email protected]62f051c2012-03-29 17:04:441962
[email protected]44d62b62012-04-11 00:06:031963 if (extension_prefs_->GetDisableReason(extension->id()) ==
1964 Extension::DISABLE_PERMISSIONS_INCREASE) {
[email protected]62f051c2012-03-29 17:04:441965 extensions::AddExtensionDisabledError(this, extension);
1966 }
[email protected]fcb58a862012-05-01 01:03:151967 return;
[email protected]e72e8eb82009-06-18 17:21:511968 }
[email protected]aab98a52009-12-02 03:22:351969
[email protected]b873cd92012-02-09 21:51:481970 // All apps that are displayed in the launcher are ordered by their ordinals
1971 // so we must ensure they have valid ordinals.
1972 if (extension->ShouldDisplayInLauncher())
1973 extension_prefs_->extension_sorting()->EnsureValidOrdinals(extension->id());
1974
[email protected]84df8332011-12-06 18:22:461975 extensions_.Insert(scoped_extension);
[email protected]3bdba0d2011-08-23 07:17:301976 SyncExtensionChangeIfNeeded(*extension);
[email protected]c8d407e2011-04-28 21:27:171977 NotifyExtensionLoaded(extension);
[email protected]aab98a52009-12-02 03:22:351978}
1979
[email protected]902fd7b2011-07-27 18:42:311980void ExtensionService::InitializePermissions(const Extension* extension) {
1981 // If the extension has used the optional permissions API, it will have a
1982 // custom set of active permissions defined in the extension prefs. Here,
1983 // we update the extension's active permissions based on the prefs.
1984 scoped_refptr<ExtensionPermissionSet> active_permissions =
1985 extension_prefs()->GetActivePermissions(extension->id());
1986
1987 if (active_permissions.get()) {
1988 // We restrict the active permissions to be within the bounds defined in the
1989 // extension's manifest.
1990 // a) active permissions must be a subset of optional + default permissions
1991 // b) active permissions must contains all default permissions
1992 scoped_refptr<ExtensionPermissionSet> total_permissions =
1993 ExtensionPermissionSet::CreateUnion(
1994 extension->required_permission_set(),
1995 extension->optional_permission_set());
1996
1997 // Make sure the active permissions contain no more than optional + default.
1998 scoped_refptr<ExtensionPermissionSet> adjusted_active =
1999 ExtensionPermissionSet::CreateIntersection(
2000 total_permissions.get(), active_permissions.get());
2001
2002 // Make sure the active permissions contain the default permissions.
2003 adjusted_active = ExtensionPermissionSet::CreateUnion(
2004 extension->required_permission_set(), adjusted_active.get());
2005
[email protected]c333e792012-01-06 16:57:392006 extensions::PermissionsUpdater perms_updater(profile());
2007 perms_updater.UpdateActivePermissions(extension, adjusted_active);
[email protected]902fd7b2011-07-27 18:42:312008 }
2009
[email protected]8d888c12010-11-30 00:00:252010 // We keep track of all permissions the user has granted each extension.
2011 // This allows extensions to gracefully support backwards compatibility
2012 // by including unknown permissions in their manifests. When the user
2013 // installs the extension, only the recognized permissions are recorded.
2014 // When the unknown permissions become recognized (e.g., through browser
2015 // upgrade), we can prompt the user to accept these new permissions.
2016 // Extensions can also silently upgrade to less permissions, and then
2017 // silently upgrade to a version that adds these permissions back.
2018 //
2019 // For example, pretend that Chrome 10 includes a permission "omnibox"
2020 // for an API that adds suggestions to the omnibox. An extension can
2021 // maintain backwards compatibility while still having "omnibox" in the
2022 // manifest. If a user installs the extension on Chrome 9, the browser
2023 // will record the permissions it recognized, not including "omnibox."
2024 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
2025 // will disable the extension and prompt the user to approve the increase
2026 // in privileges. The extension could then release a new version that
2027 // removes the "omnibox" permission. When the user upgrades, Chrome will
2028 // still remember that "omnibox" had been granted, so that if the
2029 // extension once again includes "omnibox" in an upgrade, the extension
2030 // can upgrade without requiring this user's approval.
2031 const Extension* old = GetExtensionByIdInternal(extension->id(),
[email protected]8001df22011-04-28 19:59:472032 true, true, false);
[email protected]8d888c12010-11-30 00:00:252033 bool is_extension_upgrade = old != NULL;
2034 bool is_privilege_increase = false;
[email protected]44d62b62012-04-11 00:06:032035 bool previously_disabled = false;
2036 Extension::DisableReason disable_reason =
2037 extension_prefs_->GetDisableReason(extension->id());
[email protected]8d888c12010-11-30 00:00:252038
[email protected]902fd7b2011-07-27 18:42:312039 // We only need to compare the granted permissions to the current permissions
2040 // if the extension is not allowed to silently increase its permissions.
2041 if (!extension->CanSilentlyIncreasePermissions()) {
[email protected]8d888c12010-11-30 00:00:252042 // Add all the recognized permissions if the granted permissions list
2043 // hasn't been initialized yet.
[email protected]902fd7b2011-07-27 18:42:312044 scoped_refptr<ExtensionPermissionSet> granted_permissions =
2045 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:522046 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:252047
2048 // Here, we check if an extension's privileges have increased in a manner
2049 // that requires the user's approval. This could occur because the browser
2050 // upgraded and recognized additional privileges, or an extension upgrades
2051 // to a version that requires additional privileges.
[email protected]0d3e4a22011-06-23 19:02:522052 is_privilege_increase =
[email protected]902fd7b2011-07-27 18:42:312053 granted_permissions->HasLessPrivilegesThan(
2054 extension->GetActivePermissions());
[email protected]8d888c12010-11-30 00:00:252055 }
2056
2057 if (is_extension_upgrade) {
[email protected]66e26872010-12-03 20:07:252058 // Other than for unpacked extensions, CrxInstaller should have guaranteed
2059 // that we aren't downgrading.
2060 if (extension->location() != Extension::LOAD)
[email protected]614d18c22012-04-26 16:59:222061 CHECK(extension->version()->CompareTo(*(old->version())) >= 0);
[email protected]8d888c12010-11-30 00:00:252062
2063 // Extensions get upgraded if the privileges are allowed to increase or
2064 // the privileges haven't increased.
2065 if (!is_privilege_increase) {
2066 SetBeingUpgraded(old, true);
2067 SetBeingUpgraded(extension, true);
2068 }
2069
[email protected]44d62b62012-04-11 00:06:032070 // If the extension was already disabled, suppress any alerts for becoming
2071 // disabled on permissions increase.
2072 previously_disabled = extension_prefs_->IsExtensionDisabled(old->id());
2073 if (previously_disabled) {
2074 Extension::DisableReason reason = extension_prefs_->GetDisableReason(
2075 old->id());
2076 if (reason == Extension::DISABLE_UNKNOWN) {
2077 // Initialize the reason for legacy disabled extensions from whether the
2078 // extension already exceeded granted permissions.
2079 if (extension_prefs_->DidExtensionEscalatePermissions(old->id()))
2080 disable_reason = Extension::DISABLE_PERMISSIONS_INCREASE;
2081 else
2082 disable_reason = Extension::DISABLE_USER_ACTION;
2083 }
2084 } else {
2085 disable_reason = Extension::DISABLE_PERMISSIONS_INCREASE;
2086 }
2087
[email protected]8d888c12010-11-30 00:00:252088 // To upgrade an extension in place, unload the old one and
2089 // then load the new one.
[email protected]814a7bf0f2011-08-13 05:30:592090 UnloadExtension(old->id(), extension_misc::UNLOAD_REASON_UPDATE);
[email protected]8d888c12010-11-30 00:00:252091 old = NULL;
2092 }
2093
2094 // Extension has changed permissions significantly. Disable it. A
2095 // notification should be sent by the caller.
2096 if (is_privilege_increase) {
[email protected]fe2dd7742011-04-19 22:52:492097 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
2098 RecordPermissionMessagesHistogram(
2099 extension, "Extensions.Permissions_AutoDisable");
2100 }
[email protected]b3317ad2011-04-28 23:46:002101 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:252102 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
[email protected]44d62b62012-04-11 00:06:032103 extension_prefs_->SetDisableReason(extension->id(), disable_reason);
[email protected]8d888c12010-11-30 00:00:252104 }
2105}
2106
[email protected]eaa7dd182010-12-14 11:09:002107void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:392108 std::set<std::string> extension_ids;
[email protected]84df8332011-12-06 18:22:462109 for (ExtensionSet::const_iterator iter = extensions_.begin();
2110 iter != extensions_.end(); ++iter) {
2111 const Extension* extension = *iter;
2112 if (!extension->is_theme() && extension->location() != Extension::COMPONENT)
2113 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:352114 }
2115
2116 child_process_logging::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:252117}
2118
[email protected]8266d662011-07-12 21:53:262119void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:502120 const Extension* extension,
2121 bool from_webstore,
2122 const StringOrdinal& page_ordinal) {
[email protected]a29a517a2011-01-21 21:11:122123 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:202124
[email protected]4416c5a2010-06-26 01:28:572125 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:022126 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]c3cfb012011-04-06 22:07:352127 const std::string& id = extension->id();
[email protected]8a87a5332011-08-11 17:54:592128 // Extensions installed by policy can't be disabled. So even if a previous
2129 // installation disabled the extension, make sure it is now enabled.
2130 bool initial_enable =
2131 !extension_prefs_->IsExtensionDisabled(id) ||
2132 !Extension::UserMayDisable(extension->location());
[email protected]b2907fd2011-03-25 16:43:372133 PendingExtensionInfo pending_extension_info;
[email protected]c3cfb012011-04-06 22:07:352134 if (pending_extension_manager()->GetById(id, &pending_extension_info)) {
2135 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:322136
[email protected]483d1ff2011-03-03 17:12:022137 if (!pending_extension_info.ShouldAllowInstall(*extension)) {
[email protected]4416c5a2010-06-26 01:28:572138 LOG(WARNING)
[email protected]ae6cb3022011-04-04 20:11:542139 << "ShouldAllowInstall() returned false for "
[email protected]c3cfb012011-04-06 22:07:352140 << id << " of type " << extension->GetType()
[email protected]7fa19f82010-12-21 19:40:082141 << " and update URL " << extension->update_url().spec()
2142 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:542143
[email protected]ad50def52011-10-19 23:17:072144 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272145 chrome::NOTIFICATION_EXTENSION_INSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:532146 content::Source<Profile>(profile_),
2147 content::Details<const Extension>(extension));
[email protected]ae6cb3022011-04-04 20:11:542148
[email protected]4416c5a2010-06-26 01:28:572149 // Delete the extension directory since we're not going to
2150 // load it.
[email protected]14908b72011-04-20 06:54:362151 if (!BrowserThread::PostTask(
2152 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:362153 base::Bind(&extension_file_util::DeleteFile,
2154 extension->path(), true)))
[email protected]14908b72011-04-20 06:54:362155 NOTREACHED();
[email protected]4416c5a2010-06-26 01:28:572156 return;
2157 }
[email protected]4416c5a2010-06-26 01:28:572158 } else {
[email protected]c3cfb012011-04-06 22:07:352159 // We explicitly want to re-enable an uninstalled external
2160 // extension; if we're here, that means the user is manually
2161 // installing the extension.
[email protected]6cc7dbae2011-04-29 21:18:332162 if (IsExternalExtensionUninstalled(id)) {
2163 initial_enable = true;
2164 }
[email protected]aa142702010-03-26 01:26:332165 }
2166
[email protected]07533022011-06-27 20:42:552167 // Do not record the install histograms for upgrades.
2168 if (!GetExtensionByIdInternal(extension->id(), true, true, false)) {
2169 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
2170 extension->GetType(), 100);
2171 RecordPermissionMessagesHistogram(
2172 extension, "Extensions.Permissions_Install");
2173 }
2174
[email protected]a39921b42012-02-28 03:42:542175 // Certain extension locations are specific enough that we can
2176 // auto-acknowledge any extension that came from one of them.
2177 if (extension->location() == Extension::EXTERNAL_POLICY_DOWNLOAD) {
2178 AcknowledgeExternalExtension(extension->id());
2179 }
2180
[email protected]4416c5a2010-06-26 01:28:572181 extension_prefs_->OnExtensionInstalled(
[email protected]8266d662011-07-12 21:53:262182 extension,
2183 initial_enable ? Extension::ENABLED : Extension::DISABLED,
[email protected]cc829cf2011-08-22 21:01:352184 from_webstore,
[email protected]36a5c4c2011-12-14 16:34:502185 page_ordinal);
[email protected]25b34332009-06-05 21:53:192186
[email protected]cdfca9702011-08-08 16:07:012187 // Unpacked extensions default to allowing file access, but if that has been
2188 // overridden, don't reset the value.
[email protected]b6e64fd2011-08-09 19:49:192189 if (Extension::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]cdfca9702011-08-08 16:07:012190 !extension_prefs_->HasAllowFileAccessSetting(id)) {
2191 extension_prefs_->SetAllowFileAccess(id, true);
2192 }
2193
[email protected]fcb58a862012-05-01 01:03:152194 content::NotificationService::current()->Notify(
2195 chrome::NOTIFICATION_EXTENSION_INSTALLED,
2196 content::Source<Profile>(profile_),
2197 content::Details<const Extension>(extension));
2198
[email protected]49cd1572011-02-08 21:38:452199 // Transfer ownership of |extension| to AddExtension.
[email protected]fcb58a862012-05-01 01:03:152200 AddExtension(scoped_extension);
[email protected]4a190632009-05-09 01:07:422201}
2202
[email protected]eaa7dd182010-12-14 11:09:002203const Extension* ExtensionService::GetExtensionByIdInternal(
[email protected]8001df22011-04-28 19:59:472204 const std::string& id, bool include_enabled, bool include_disabled,
2205 bool include_terminated) const {
[email protected]e957fe52009-06-23 16:51:052206 std::string lowercase_id = StringToLowerASCII(id);
[email protected]0c6da502009-08-14 22:32:392207 if (include_enabled) {
[email protected]84df8332011-12-06 18:22:462208 const Extension* extension = extensions_.GetByID(lowercase_id);
2209 if (extension)
2210 return extension;
[email protected]0c6da502009-08-14 22:32:392211 }
2212 if (include_disabled) {
[email protected]84df8332011-12-06 18:22:462213 const Extension* extension = disabled_extensions_.GetByID(lowercase_id);
2214 if (extension)
2215 return extension;
[email protected]ce5c4502009-05-06 16:46:112216 }
[email protected]8001df22011-04-28 19:59:472217 if (include_terminated) {
[email protected]84df8332011-12-06 18:22:462218 const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
2219 if (extension)
2220 return extension;
[email protected]8001df22011-04-28 19:59:472221 }
[email protected]ce5c4502009-05-06 16:46:112222 return NULL;
2223}
2224
[email protected]bb7f40952011-01-13 00:21:202225void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
[email protected]84df8332011-12-06 18:22:462226 if (!terminated_extensions_.Contains(extension->id()))
2227 terminated_extensions_.Insert(make_scoped_refptr(extension));
[email protected]fa2416f2011-05-03 08:41:202228
[email protected]b3f7fe22011-11-11 19:27:562229 UnloadExtension(extension->id(), extension_misc::UNLOAD_REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:202230}
2231
2232void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]fa2416f2011-05-03 08:41:202233 std::string lowercase_id = StringToLowerASCII(id);
[email protected]84df8332011-12-06 18:22:462234 terminated_extensions_.Remove(lowercase_id);
[email protected]bb7f40952011-01-13 00:21:202235}
2236
[email protected]0dfe05c2011-02-23 23:03:362237const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:472238 const std::string& id) const {
2239 return GetExtensionByIdInternal(id, false, false, true);
2240}
2241
2242const Extension* ExtensionService::GetInstalledExtension(
2243 const std::string& id) const {
2244 return GetExtensionByIdInternal(id, true, true, true);
[email protected]0dfe05c2011-02-23 23:03:362245}
2246
[email protected]eaa7dd182010-12-14 11:09:002247bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:442248 // Allow bindings for all packaged extensions and component hosted apps.
2249 const Extension* extension = extensions_.GetExtensionOrAppByURL(
2250 ExtensionURLInfo(url));
2251 return extension && (!extension->is_hosted_app() ||
2252 extension->location() == Extension::COMPONENT);
[email protected]6d2e60bd2010-06-03 22:37:392253}
2254
[email protected]eaa7dd182010-12-14 11:09:002255const SkBitmap& ExtensionService::GetOmniboxIcon(
[email protected]b671760b2010-07-15 21:13:472256 const std::string& extension_id) {
2257 return omnibox_icon_manager_.GetIcon(extension_id);
2258}
2259
[email protected]eaa7dd182010-12-14 11:09:002260const SkBitmap& ExtensionService::GetOmniboxPopupIcon(
[email protected]29d0d4ac2010-09-08 21:10:312261 const std::string& extension_id) {
2262 return omnibox_popup_icon_manager_.GetIcon(extension_id);
2263}
2264
[email protected]9060d8b02012-01-13 02:14:302265bool ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322266 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:142267 const Version* version,
[email protected]8ef78fd2010-08-19 17:14:322268 const FilePath& path,
[email protected]1bf73cc32011-10-26 22:38:312269 Extension::Location location,
[email protected]47fc70c2011-12-06 07:29:512270 int creation_flags,
2271 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:382272 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2273 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012274 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]9060d8b02012-01-13 02:14:302275 return false;
[email protected]a8af9fdb2010-10-28 21:52:202276
[email protected]8e4560b62011-01-14 10:09:142277 DCHECK(version);
2278
[email protected]7577a5c52009-07-30 06:21:582279 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492280 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582281 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022282 const Extension* existing = GetExtensionById(id, true);
[email protected]7577a5c52009-07-30 06:21:582283 if (existing) {
[email protected]8e4560b62011-01-14 10:09:142284 switch (existing->version()->CompareTo(*version)) {
[email protected]7577a5c52009-07-30 06:21:582285 case -1: // existing version is older, we should upgrade
2286 break;
2287 case 0: // existing version is same, do nothing
[email protected]9060d8b02012-01-13 02:14:302288 return false;
[email protected]7577a5c52009-07-30 06:21:582289 case 1: // existing version is newer, uh-oh
2290 LOG(WARNING) << "Found external version of extension " << id
2291 << "that is older than current version. Current version "
2292 << "is: " << existing->VersionString() << ". New version "
[email protected]16d900042012-01-04 21:24:322293 << "is: " << version->GetString()
2294 << ". Keeping current version.";
[email protected]9060d8b02012-01-13 02:14:302295 return false;
[email protected]7577a5c52009-07-30 06:21:582296 }
2297 }
2298
[email protected]9060d8b02012-01-13 02:14:302299 // If the extension is already pending, don't start an install.
[email protected]e3987852012-05-04 10:06:302300 if (!pending_extension_manager()->AddFromExternalFile(
2301 id, location, *version)) {
[email protected]9060d8b02012-01-13 02:14:302302 return false;
[email protected]e3987852012-05-04 10:06:302303 }
[email protected]9c635f22010-12-02 09:36:362304
[email protected]14908b72011-04-20 06:54:362305 // no client (silent install)
[email protected]d8c8f25f2011-11-02 18:18:012306 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(this, NULL));
[email protected]6dfbbf82010-03-12 23:09:162307 installer->set_install_source(location);
2308 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072309 installer->set_expected_version(*version);
2310 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]1bf73cc32011-10-26 22:38:312311 installer->set_creation_flags(creation_flags);
[email protected]6dfbbf82010-03-12 23:09:162312 installer->InstallCrx(path);
[email protected]47fc70c2011-12-06 07:29:512313
2314 // Depending on the source, a new external extension might not need a user
2315 // notification on installation. For such extensions, mark them acknowledged
2316 // now to suppress the notification.
2317 if (mark_acknowledged)
2318 AcknowledgeExternalExtension(id);
[email protected]9060d8b02012-01-13 02:14:302319
2320 return true;
[email protected]7577a5c52009-07-30 06:21:582321}
2322
[email protected]eaa7dd182010-12-14 11:09:002323void ExtensionService::ReportExtensionLoadError(
[email protected]d11c8e92009-10-20 23:26:402324 const FilePath& extension_path,
2325 const std::string &error,
[email protected]d11c8e92009-10-20 23:26:402326 bool be_noisy) {
[email protected]d8c8f25f2011-11-02 18:18:012327 content::NotificationService::current()->Notify(
2328 chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
2329 content::Source<Profile>(profile_),
2330 content::Details<const std::string>(&error));
[email protected]d11c8e92009-10-20 23:26:402331
[email protected]8a205c02011-02-04 20:41:332332 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]fc670822011-12-17 09:33:492333 string16 message = ASCIIToUTF16(base::StringPrintf(
[email protected]18d4b6c2010-09-21 03:21:042334 "Could not load extension from '%s'. %s",
[email protected]fc670822011-12-17 09:33:492335 path_str.c_str(), error.c_str()));
[email protected]d11c8e92009-10-20 23:26:402336 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2337}
2338
[email protected]eaa7dd182010-12-14 11:09:002339void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]406027c02010-09-27 08:03:182340 ExtensionHost* host) {
2341 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332342 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182343 if (iter == orphaned_dev_tools_.end())
2344 return;
2345
[email protected]0e12d7d2011-12-01 16:21:442346 DevToolsAgentHost* agent = DevToolsAgentHostRegistry::GetDevToolsAgentHost(
2347 host->render_view_host());
2348 content::DevToolsManager::GetInstance()->AttachClientHost(iter->second,
2349 agent);
[email protected]406027c02010-09-27 08:03:182350 orphaned_dev_tools_.erase(iter);
2351}
2352
[email protected]432115822011-07-10 15:52:272353void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532354 const content::NotificationSource& source,
2355 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272356 switch (type) {
2357 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532358 if (profile_ !=
2359 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322360 break;
[email protected]6c2381d2011-10-19 02:52:532361 }
[email protected]a4ed6282009-12-14 20:51:162362
[email protected]6c2381d2011-10-19 02:52:532363 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262364
[email protected]fa2416f2011-05-03 08:41:202365 // Mark the extension as terminated and Unload it. We want it to
2366 // be in a consistent state: either fully working or not loaded
2367 // at all, but never half-crashed. We do it in a PostTask so
2368 // that other handlers of this notification will still have
2369 // access to the Extension and ExtensionHost.
[email protected]14908b72011-04-20 06:54:362370 MessageLoop::current()->PostTask(
2371 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362372 base::Bind(
[email protected]fa2416f2011-05-03 08:41:202373 &ExtensionService::TrackTerminatedExtension,
[email protected]d8c8f25f2011-11-02 18:18:012374 AsWeakPtr(),
[email protected]fa2416f2011-05-03 08:41:202375 host->extension()));
[email protected]31f77262009-12-02 20:48:532376 break;
2377 }
[email protected]432115822011-07-10 15:52:272378 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
[email protected]f3b1a082011-11-18 00:34:302379 content::RenderProcessHost* process =
2380 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192381 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302382 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192383 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2384 break;
2385
[email protected]15397b42012-05-16 23:56:062386 // Extensions need to know the channel for API restrictions.
2387 process->Send(new ExtensionMsg_SetChannel(
2388 chrome::VersionInfo::GetChannel()));
2389
[email protected]77a6970c2011-04-23 16:58:562390 // Valid extension function names, used to setup bindings in renderer.
2391 std::vector<std::string> function_names;
2392 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names);
2393 process->Send(new ExtensionMsg_SetFunctionNames(function_names));
[email protected]31f77262009-12-02 20:48:532394
[email protected]77a6970c2011-04-23 16:58:562395 // Scripting whitelist. This is modified by tests and must be communicated
2396 // to renderers.
2397 process->Send(new ExtensionMsg_SetScriptingWhitelist(
2398 *Extension::GetScriptingWhitelist()));
2399
2400 // Loaded extensions.
[email protected]9776e82e2011-11-15 02:17:532401 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions;
[email protected]84df8332011-12-06 18:22:462402 for (ExtensionSet::const_iterator iter = extensions_.begin();
2403 iter != extensions_.end(); ++iter) {
[email protected]19647262011-12-16 09:57:492404 // Renderers don't need to know about themes.
2405 if (!(*iter)->is_theme())
2406 loaded_extensions.push_back(ExtensionMsg_Loaded_Params(*iter));
[email protected]77a6970c2011-04-23 16:58:562407 }
[email protected]9776e82e2011-11-15 02:17:532408 process->Send(new ExtensionMsg_Loaded(loaded_extensions));
[email protected]77a6970c2011-04-23 16:58:562409 break;
2410 }
[email protected]432115822011-07-10 15:52:272411 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302412 content::RenderProcessHost* process =
2413 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192414 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302415 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192416 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2417 break;
2418
[email protected]f3b1a082011-11-18 00:34:302419 installed_app_hosts_.erase(process->GetID());
[email protected]6f371442011-11-09 06:45:462420
[email protected]6bc04fd82011-12-04 02:29:352421 process_map_.RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:462422 BrowserThread::PostTask(
2423 BrowserThread::IO, FROM_HERE,
2424 base::Bind(&ExtensionInfoMap::UnregisterAllExtensionsInProcess,
[email protected]31d8f5f22012-04-02 15:22:082425 system_->info_map(),
[email protected]f3b1a082011-11-18 00:34:302426 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212427 break;
2428 }
[email protected]432115822011-07-10 15:52:272429 case chrome::NOTIFICATION_PREF_CHANGED: {
[email protected]6c2381d2011-10-19 02:52:532430 std::string* pref_name = content::Details<std::string>(details).ptr();
[email protected]6c751e72010-11-23 10:11:102431 if (*pref_name == prefs::kExtensionInstallAllowList ||
2432 *pref_name == prefs::kExtensionInstallDenyList) {
2433 CheckAdminBlacklist();
[email protected]6c751e72010-11-23 10:11:102434 } else {
2435 NOTREACHED() << "Unexpected preference name.";
2436 }
[email protected]aa96d3a2010-08-21 08:45:252437 break;
2438 }
[email protected]d91268022011-08-26 13:17:372439 case chrome::NOTIFICATION_IMPORT_FINISHED: {
[email protected]cf593af2011-12-30 05:44:392440 InitAfterImport();
[email protected]d91268022011-08-26 13:17:372441 break;
2442 }
[email protected]aa96d3a2010-08-21 08:45:252443
[email protected]4814b512009-11-07 00:12:292444 default:
2445 NOTREACHED() << "Unexpected notification type.";
2446 }
2447}
2448
[email protected]eaa7dd182010-12-14 11:09:002449bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182450 return !GetAppIds().empty();
2451}
[email protected]377011d2010-07-20 04:18:502452
[email protected]eaa7dd182010-12-14 11:09:002453ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182454 ExtensionIdSet result;
[email protected]84df8332011-12-06 18:22:462455 for (ExtensionSet::const_iterator it = extensions_.begin();
[email protected]377011d2010-07-20 04:18:502456 it != extensions_.end(); ++it) {
[email protected]ec5b50d2010-10-09 16:35:182457 if ((*it)->is_app() && (*it)->location() != Extension::COMPONENT)
2458 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502459 }
2460
[email protected]ec5b50d2010-10-09 16:35:182461 return result;
[email protected]377011d2010-07-20 04:18:502462}
[email protected]d7e9a862010-11-03 21:57:492463
[email protected]eaa7dd182010-12-14 11:09:002464bool ExtensionService::IsBackgroundPageReady(const Extension* extension) {
[email protected]4762e7902012-03-28 23:34:042465 return (!extension->has_persistent_background_page() ||
[email protected]d7e9a862010-11-03 21:57:492466 extension_runtime_data_[extension->id()].background_page_ready);
2467}
2468
[email protected]eaa7dd182010-12-14 11:09:002469void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]a03d4448f2012-01-10 23:25:282470 DCHECK(extension->has_background_page());
[email protected]d7e9a862010-11-03 21:57:492471 extension_runtime_data_[extension->id()].background_page_ready = true;
[email protected]ad50def52011-10-19 23:17:072472 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272473 chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
[email protected]6c2381d2011-10-19 02:52:532474 content::Source<const Extension>(extension),
[email protected]ad50def52011-10-19 23:17:072475 content::NotificationService::NoDetails());
[email protected]d7e9a862010-11-03 21:57:492476}
2477
[email protected]eaa7dd182010-12-14 11:09:002478bool ExtensionService::IsBeingUpgraded(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492479 return extension_runtime_data_[extension->id()].being_upgraded;
2480}
2481
[email protected]eaa7dd182010-12-14 11:09:002482void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]d7e9a862010-11-03 21:57:492483 bool value) {
2484 extension_runtime_data_[extension->id()].being_upgraded = value;
2485}
[email protected]1bead0712010-11-27 17:41:532486
[email protected]39a5b532011-10-22 01:47:072487bool ExtensionService::HasUsedWebRequest(const Extension* extension) {
2488 return extension_runtime_data_[extension->id()].has_used_webrequest;
2489}
2490
2491void ExtensionService::SetHasUsedWebRequest(const Extension* extension,
2492 bool value) {
2493 extension_runtime_data_[extension->id()].has_used_webrequest = value;
2494}
2495
[email protected]45644f62011-11-23 00:58:232496base::PropertyBag* ExtensionService::GetPropertyBag(
2497 const Extension* extension) {
[email protected]1bead0712010-11-27 17:41:532498 return &extension_runtime_data_[extension->id()].property_bag;
2499}
[email protected]cebc3dc2011-04-18 17:15:002500
2501void ExtensionService::RegisterNaClModule(const GURL& url,
2502 const std::string& mime_type) {
2503 NaClModuleInfo info;
2504 info.url = url;
2505 info.mime_type = mime_type;
2506
2507 DCHECK(FindNaClModule(url) == nacl_module_list_.end());
2508 nacl_module_list_.push_front(info);
2509}
2510
2511void ExtensionService::UnregisterNaClModule(const GURL& url) {
2512 NaClModuleInfoList::iterator iter = FindNaClModule(url);
2513 DCHECK(iter != nacl_module_list_.end());
2514 nacl_module_list_.erase(iter);
2515}
2516
[email protected]ed0ba002011-05-26 16:55:132517void ExtensionService::UpdatePluginListWithNaClModules() {
[email protected]300c0ea2011-07-15 23:04:332518 // An extension has been added which has a nacl_module component, which means
2519 // there is a MIME type that module wants to handle, so we need to add that
2520 // MIME type to plugins which handle NaCl modules in order to allow the
2521 // individual modules to handle these types.
[email protected]1e127022011-11-29 21:11:472522 FilePath path;
2523 if (!PathService::Get(chrome::FILE_NACL_PLUGIN, &path))
2524 return;
2525 const content::PepperPluginInfo* pepper_info =
2526 PluginService::GetInstance()->GetRegisteredPpapiPluginInfo(path);
2527 if (!pepper_info)
2528 return;
[email protected]300c0ea2011-07-15 23:04:332529
[email protected]1e127022011-11-29 21:11:472530 std::vector<webkit::WebPluginMimeType>::const_iterator mime_iter;
2531 // Check each MIME type the plugins handle for the NaCl MIME type.
2532 for (mime_iter = pepper_info->mime_types.begin();
2533 mime_iter != pepper_info->mime_types.end(); ++mime_iter) {
2534 if (mime_iter->mime_type == kNaClPluginMimeType) {
2535 // This plugin handles "application/x-nacl".
[email protected]300c0ea2011-07-15 23:04:332536
[email protected]1e127022011-11-29 21:11:472537 PluginService::GetInstance()->
2538 UnregisterInternalPlugin(pepper_info->path);
[email protected]300c0ea2011-07-15 23:04:332539
[email protected]1e127022011-11-29 21:11:472540 webkit::WebPluginInfo info = pepper_info->ToWebPluginInfo();
[email protected]300c0ea2011-07-15 23:04:332541
[email protected]1e127022011-11-29 21:11:472542 for (ExtensionService::NaClModuleInfoList::const_iterator iter =
2543 nacl_module_list_.begin();
2544 iter != nacl_module_list_.end(); ++iter) {
2545 // Add the MIME type specified in the extension to this NaCl plugin,
2546 // With an extra "nacl" argument to specify the location of the NaCl
2547 // manifest file.
2548 webkit::WebPluginMimeType mime_type_info;
2549 mime_type_info.mime_type = iter->mime_type;
2550 mime_type_info.additional_param_names.push_back(UTF8ToUTF16("nacl"));
2551 mime_type_info.additional_param_values.push_back(
2552 UTF8ToUTF16(iter->url.spec()));
2553 info.mime_types.push_back(mime_type_info);
[email protected]300c0ea2011-07-15 23:04:332554 }
[email protected]1e127022011-11-29 21:11:472555
2556 PluginService::GetInstance()->RefreshPlugins();
[email protected]c6f3dea2012-01-14 02:23:112557 PluginService::GetInstance()->RegisterInternalPlugin(info, true);
[email protected]1e127022011-11-29 21:11:472558 // This plugin has been modified, no need to check the rest of its
2559 // types, but continue checking other plugins.
2560 break;
[email protected]ed0ba002011-05-26 16:55:132561 }
2562 }
[email protected]ed0ba002011-05-26 16:55:132563}
2564
[email protected]cebc3dc2011-04-18 17:15:002565ExtensionService::NaClModuleInfoList::iterator
2566 ExtensionService::FindNaClModule(const GURL& url) {
2567 for (NaClModuleInfoList::iterator iter = nacl_module_list_.begin();
2568 iter != nacl_module_list_.end(); ++iter) {
2569 if (iter->url == url)
2570 return iter;
2571 }
2572 return nacl_module_list_.end();
2573}
[email protected]9de83282011-12-12 11:06:372574
[email protected]58edca52012-02-16 21:51:382575extensions::APIResourceController*
2576ExtensionService::api_resource_controller() {
2577 // TODO(miket): Find a better place for this thing to live. It needs to be
2578 // scoped such that it can be created and destroyed on the IO thread.
[email protected]e1fae1c2011-12-13 22:17:172579 //
2580 // To coexist with certain unit tests that don't have an IO thread message
2581 // loop available at ExtensionService shutdown, we lazy-initialize this
[email protected]58edca52012-02-16 21:51:382582 // object so that those cases neither create nor destroy an
2583 // APIResourceController.
[email protected]e1fae1c2011-12-13 22:17:172584 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]58edca52012-02-16 21:51:382585 if (!api_resource_controller_) {
2586 api_resource_controller_ = new extensions::APIResourceController();
[email protected]e1fae1c2011-12-13 22:17:172587 }
[email protected]58edca52012-02-16 21:51:382588 return api_resource_controller_;
[email protected]e1fae1c2011-12-13 22:17:172589}