blob: 6923e15d854cedb5979ac5ab97de410f9f1b257a [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]af9db5f2011-10-05 05:13:1535#include "chrome/browser/extensions/app_notification_manager.h"
[email protected]18cc5a52011-04-15 16:03:3836#include "chrome/browser/extensions/apps_promo.h"
[email protected]d8c8f25f2011-11-02 18:18:0137#include "chrome/browser/extensions/component_loader.h"
[email protected]e0785902011-05-19 23:34:1738#include "chrome/browser/extensions/crx_installer.h"
[email protected]e0a65f72012-02-10 01:45:4639#include "chrome/browser/extensions/api/declarative/rules_registry_service.h"
[email protected]49098f702011-10-13 03:47:1840#include "chrome/browser/extensions/default_apps_trial.h"
[email protected]b68d5ed2009-04-16 02:41:2841#include "chrome/browser/extensions/extension_browser_event_router.h"
[email protected]2c5e1e12010-06-10 13:14:4442#include "chrome/browser/extensions/extension_cookies_api.h"
[email protected]c10da4b02010-03-25 14:38:3243#include "chrome/browser/extensions/extension_data_deleter.h"
[email protected]62f051c2012-03-29 17:04:4444#include "chrome/browser/extensions/extension_disabled_ui.h"
[email protected]14a000d2010-04-29 21:44:2445#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]e5af875f2011-10-10 21:09:1446#include "chrome/browser/extensions/extension_global_error.h"
[email protected]b1748b1d82009-11-30 20:32:5647#include "chrome/browser/extensions/extension_host.h"
[email protected]8baf0dbb2011-11-27 22:47:0248#include "chrome/browser/extensions/extension_input_ime_api.h"
[email protected]7596ce72010-08-30 05:10:4649#include "chrome/browser/extensions/extension_management_api.h"
[email protected]118de7892011-03-16 13:31:4050#include "chrome/browser/extensions/extension_preference_api.h"
[email protected]4814b512009-11-07 00:12:2951#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]8a661f82010-10-19 21:47:1152#include "chrome/browser/extensions/extension_processes_api.h"
[email protected]3569b502012-01-12 20:08:2353#include "chrome/browser/extensions/extension_sorting.h"
[email protected]19eb80152011-02-26 00:28:4354#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]90310d92011-04-17 07:35:0455#include "chrome/browser/extensions/extension_sync_data.h"
[email protected]8f9d4eb2011-02-05 01:39:1056#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]784688a62010-09-13 07:06:5257#include "chrome/browser/extensions/extension_webnavigation_api.h"
[email protected]8e4560b62011-01-14 10:09:1458#include "chrome/browser/extensions/external_extension_provider_impl.h"
[email protected]42b795652011-02-22 23:32:3859#include "chrome/browser/extensions/external_extension_provider_interface.h"
[email protected]d8c8f25f2011-11-02 18:18:0160#include "chrome/browser/extensions/installed_loader.h"
[email protected]b2907fd2011-03-25 16:43:3761#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]c333e792012-01-06 16:57:3962#include "chrome/browser/extensions/permissions_updater.h"
[email protected]cccdf0aa2011-11-11 03:43:3863#include "chrome/browser/extensions/settings/settings_frontend.h"
[email protected]d8c8f25f2011-11-02 18:18:0164#include "chrome/browser/extensions/unpacked_installer.h"
[email protected]42a08162012-03-16 18:09:1165#include "chrome/browser/extensions/updater/extension_updater.h"
[email protected]d2e6bd62011-10-24 20:29:0766#include "chrome/browser/history/history_extension_api.h"
[email protected]56ad3792010-05-28 17:45:3367#include "chrome/browser/net/chrome_url_request_context.h"
[email protected]37858e52010-08-26 00:22:0268#include "chrome/browser/prefs/pref_service.h"
[email protected]8ecad5e2010-12-02 21:18:3369#include "chrome/browser/profiles/profile.h"
[email protected]cf593af2011-12-30 05:44:3970#include "chrome/browser/profiles/profile_manager.h"
[email protected]8e5c89a2011-06-07 18:13:3371#include "chrome/browser/search_engines/template_url_service.h"
72#include "chrome/browser/search_engines/template_url_service_factory.h"
[email protected]3bdba0d2011-08-23 07:17:3073#include "chrome/browser/sync/api/sync_change.h"
[email protected]18280372011-03-22 18:05:2274#include "chrome/browser/themes/theme_service.h"
[email protected]a0ea76c2011-03-23 17:36:4275#include "chrome/browser/themes/theme_service_factory.h"
[email protected]e5af875f2011-10-10 21:09:1476#include "chrome/browser/ui/browser.h"
[email protected]62433d32011-10-12 22:33:1277#include "chrome/browser/ui/browser_list.h"
[email protected]e5af875f2011-10-10 21:09:1478#include "chrome/browser/ui/global_error_service.h"
79#include "chrome/browser/ui/global_error_service_factory.h"
[email protected]c8d407e2011-04-28 21:27:1780#include "chrome/browser/ui/webui/chrome_url_data_manager.h"
81#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]5eddc3e2011-10-26 04:33:3182#include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
[email protected]aab98a52009-12-02 03:22:3583#include "chrome/common/child_process_logging.h"
[email protected]432115822011-07-10 15:52:2784#include "chrome/common/chrome_notification_types.h"
[email protected]1e127022011-11-29 21:11:4785#include "chrome/common/chrome_paths.h"
[email protected]e2eb43112009-05-29 21:19:5486#include "chrome/common/chrome_switches.h"
[email protected]5b1a0e22009-05-26 19:00:5887#include "chrome/common/extensions/extension.h"
[email protected]a52c0e92012-03-23 06:02:2488#include "chrome/common/extensions/extension_manifest_constants.h"
[email protected]05c82182010-06-24 17:49:0889#include "chrome/common/extensions/extension_error_utils.h"
[email protected]7c927b62010-02-24 09:54:1390#include "chrome/common/extensions/extension_file_util.h"
[email protected]77a6970c2011-04-23 16:58:5691#include "chrome/common/extensions/extension_messages.h"
[email protected]a315ba92010-11-16 14:12:2192#include "chrome/common/extensions/extension_resource.h"
[email protected]25b34332009-06-05 21:53:1993#include "chrome/common/pref_names.h"
[email protected]a57209872009-05-04 22:53:1494#include "chrome/common/url_constants.h"
[email protected]c38831a12011-10-28 12:44:4995#include "content/public/browser/browser_thread.h"
[email protected]0e12d7d2011-12-01 16:21:4496#include "content/public/browser/devtools_agent_host_registry.h"
97#include "content/public/browser/devtools_manager.h"
[email protected]05aad2da2011-10-28 10:12:3798#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1699#include "content/public/browser/notification_types.h"
[email protected]3a5180ae2011-12-21 02:39:38100#include "content/public/browser/plugin_service.h"
[email protected]f3b1a082011-11-18 00:34:30101#include "content/public/browser/render_process_host.h"
[email protected]1e127022011-11-29 21:11:47102#include "content/public/common/pepper_plugin_info.h"
[email protected]c10da4b02010-03-25 14:38:32103#include "googleurl/src/gurl.h"
[email protected]1debbbb62010-10-06 17:23:44104#include "net/base/registry_controlled_domain.h"
[email protected]24b538a2010-02-27 01:22:44105#include "webkit/database/database_tracker.h"
106#include "webkit/database/database_util.h"
[email protected]c64631652009-04-29 22:24:31107
[email protected]eed367e2011-04-12 03:43:31108#if defined(OS_CHROMEOS)
[email protected]14da67b02011-08-02 05:49:13109#include "chrome/browser/chromeos/cros/cros_library.h"
[email protected]f463bc92012-03-22 16:38:23110#include "chrome/browser/chromeos/extensions/bluetooth_event_router.h"
[email protected]eed367e2011-04-12 03:43:31111#include "chrome/browser/chromeos/extensions/file_browser_event_router.h"
[email protected]be5bd5742011-07-14 07:21:01112#include "chrome/browser/chromeos/extensions/input_method_event_router.h"
[email protected]ce9802042011-05-27 10:11:40113#include "chrome/browser/chromeos/extensions/media_player_event_router.h"
[email protected]14da67b02011-08-02 05:49:13114#include "chrome/browser/chromeos/input_method/input_method_manager.h"
[email protected]8baf0dbb2011-11-27 22:47:02115#include "chrome/browser/extensions/extension_input_ime_api.h"
[email protected]b777b332011-04-16 04:01:08116#include "webkit/fileapi/file_system_context.h"
117#include "webkit/fileapi/file_system_mount_point_provider.h"
[email protected]eed367e2011-04-12 03:43:31118#endif
119
[email protected]9a2aebe02011-12-02 21:54:14120#if defined(OS_CHROMEOS) && defined(USE_VIRTUAL_KEYBOARD)
[email protected]27072cad2011-05-09 19:46:40121#include "chrome/browser/extensions/extension_input_ui_api.h"
122#endif
123
[email protected]5ef47ec2010-01-28 05:58:05124using base::Time;
[email protected]55eb70e762012-02-20 17:38:39125using content::BrowserContext;
[email protected]631bb742011-11-02 11:29:39126using content::BrowserThread;
[email protected]0e12d7d2011-12-01 16:21:44127using content::DevToolsAgentHost;
128using content::DevToolsAgentHostRegistry;
[email protected]3a5180ae2011-12-21 02:39:38129using content::PluginService;
[email protected]5ef47ec2010-01-28 05:58:05130
[email protected]c6d474f82009-12-16 21:11:06131namespace errors = extension_manifest_errors;
132
[email protected]b6ab96d2009-08-20 18:58:19133namespace {
134
[email protected]29d0d4ac2010-09-08 21:10:31135#if defined(OS_LINUX)
136static const int kOmniboxIconPaddingLeft = 2;
137static const int kOmniboxIconPaddingRight = 2;
138#elif defined(OS_MACOSX)
139static const int kOmniboxIconPaddingLeft = 0;
140static const int kOmniboxIconPaddingRight = 2;
141#else
142static const int kOmniboxIconPaddingLeft = 0;
143static const int kOmniboxIconPaddingRight = 0;
144#endif
145
[email protected]300c0ea2011-07-15 23:04:33146const char* kNaClPluginMimeType = "application/x-nacl";
147
[email protected]3bdba0d2011-08-23 07:17:30148static bool IsSyncableExtension(const Extension& extension) {
149 return extension.GetSyncType() == Extension::SYNC_TYPE_EXTENSION;
150}
151
152static bool IsSyncableApp(const Extension& extension) {
153 return extension.GetSyncType() == Extension::SYNC_TYPE_APP;
154}
155
[email protected]c6d474f82009-12-16 21:11:06156} // namespace
[email protected]b6ab96d2009-08-20 18:58:19157
[email protected]eaa7dd182010-12-14 11:09:00158ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49159 : background_page_ready(false),
[email protected]f8bf5992011-10-24 22:38:57160 being_upgraded(false),
161 has_used_webrequest(false) {
[email protected]d7e9a862010-11-03 21:57:49162}
163
[email protected]eaa7dd182010-12-14 11:09:00164ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49165}
166
[email protected]cebc3dc2011-04-18 17:15:00167ExtensionService::NaClModuleInfo::NaClModuleInfo() {
168}
169
170ExtensionService::NaClModuleInfo::~NaClModuleInfo() {
171}
172
[email protected]eaa7dd182010-12-14 11:09:00173// ExtensionService.
[email protected]6014d672008-12-05 00:38:25174
[email protected]eaa7dd182010-12-14 11:09:00175const char* ExtensionService::kInstallDirectoryName = "Extensions";
[email protected]15d5b472011-11-24 04:17:24176
177const char* ExtensionService::kLocalAppSettingsDirectoryName =
178 "Local App Settings";
179const char* ExtensionService::kLocalExtensionSettingsDirectoryName =
180 "Local Extension Settings";
181const char* ExtensionService::kSyncAppSettingsDirectoryName =
182 "Sync App Settings";
183const char* ExtensionService::kSyncExtensionSettingsDirectoryName =
184 "Sync Extension Settings";
[email protected]494c06e2009-07-25 01:06:42185
[email protected]8e4560b62011-01-14 10:09:14186void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12187 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20188
[email protected]7a4c6852010-09-16 03:44:22189 // Check if the providers know about this extension.
[email protected]0a60a2e2010-10-25 16:15:21190 ProviderCollection::const_iterator i;
191 for (i = external_extension_providers_.begin();
192 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14193 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21194 if (i->get()->HasExtension(id))
195 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22196 }
197
[email protected]0f48fca2011-05-19 18:46:35198 // We get the list of external extensions to check from preferences.
199 // It is possible that an extension has preferences but is not loaded.
200 // For example, an extension that requires experimental permissions
201 // will not be loaded if the experimental command line flag is not used.
202 // In this case, do not uninstall.
203 const Extension* extension = GetInstalledExtension(id);
204 if (!extension) {
205 // We can't call UninstallExtension with an unloaded/invalid
206 // extension ID.
207 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
208 << "with id: " << id;
209 return;
210 }
[email protected]d6ebc9792011-04-07 18:18:33211 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22212}
213
[email protected]8e4560b62011-01-14 10:09:14214void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22215 external_extension_providers_.clear();
216}
217
[email protected]8e4560b62011-01-14 10:09:14218void ExtensionService::AddProviderForTesting(
219 ExternalExtensionProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12220 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21221 external_extension_providers_.push_back(
[email protected]8e4560b62011-01-14 10:09:14222 linked_ptr<ExternalExtensionProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22223}
224
[email protected]9060d8b02012-01-13 02:14:30225bool ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22226 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42227 const GURL& update_url,
228 Extension::Location location) {
[email protected]ab22ba42011-01-14 16:36:38229 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
230 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20231
[email protected]8a87a5332011-08-11 17:54:59232 const Extension* extension = GetExtensionById(id, true);
233 if (extension) {
234 // Already installed. Skip this install if the current location has
235 // higher priority than |location|.
236 Extension::Location current = extension->location();
237 if (current == Extension::GetHigherPriorityLocation(current, location))
[email protected]9060d8b02012-01-13 02:14:30238 return false;
[email protected]8a87a5332011-08-11 17:54:59239 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22240 }
[email protected]9060d8b02012-01-13 02:14:30241
242 // Add |id| to the set of pending extensions. If it can not be added,
243 // then there is already a pending record from a higher-priority install
244 // source. In this case, signal that this extension will not be
245 // installed by returning false.
246 if (!pending_extension_manager()->AddFromExternalUpdateUrl(
247 id, update_url, location))
248 return false;
249
[email protected]50067e52011-10-20 23:17:07250 external_extension_url_added_ = true;
[email protected]9060d8b02012-01-13 02:14:30251 return true;
[email protected]7a4c6852010-09-16 03:44:22252}
253
[email protected]454434f2011-08-24 04:00:00254// If a download url matches one of these patterns and has a referrer of the
255// webstore, then we're willing to treat that as a gallery download.
256static const char* kAllowedDownloadURLPatterns[] = {
257 "https://clients2.google.com/service/update2*",
258 "https://clients2.googleusercontent.com/crx/*"
259};
260
[email protected]eaa7dd182010-12-14 11:09:00261bool ExtensionService::IsDownloadFromGallery(const GURL& download_url,
[email protected]a29a517a2011-01-21 21:11:12262 const GURL& referrer_url) {
[email protected]615d88f2011-12-13 01:47:44263 const Extension* download_extension =
264 extensions_.GetHostedAppByURL(ExtensionURLInfo(download_url));
265 const Extension* referrer_extension =
266 extensions_.GetHostedAppByURL(ExtensionURLInfo(referrer_url));
[email protected]9adb9692010-10-29 23:14:02267 const Extension* webstore_app = GetWebStoreApp();
[email protected]d3071992010-10-08 15:24:07268
269 bool referrer_valid = (referrer_extension == webstore_app);
270 bool download_valid = (download_extension == webstore_app);
271
[email protected]454434f2011-08-24 04:00:00272 // We also allow the download to be from a small set of trusted paths.
273 if (!download_valid) {
274 for (size_t i = 0; i < arraysize(kAllowedDownloadURLPatterns); i++) {
275 URLPattern pattern(URLPattern::SCHEME_HTTPS,
276 kAllowedDownloadURLPatterns[i]);
277 if (pattern.MatchesURL(download_url)) {
278 download_valid = true;
279 break;
280 }
281 }
282 }
283
[email protected]d3071992010-10-08 15:24:07284 // If the command-line gallery URL is set, then be a bit more lenient.
285 GURL store_url =
286 GURL(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
287 switches::kAppsGalleryURL));
288 if (!store_url.is_empty()) {
289 std::string store_tld =
290 net::RegistryControlledDomainService::GetDomainAndRegistry(store_url);
291 if (!referrer_valid) {
292 std::string referrer_tld =
293 net::RegistryControlledDomainService::GetDomainAndRegistry(
294 referrer_url);
295 // The referrer gets stripped when transitioning from https to http,
296 // or when hitting an unknown test cert and that commonly happens in
297 // testing environments. Given this, we allow an empty referrer when
298 // the command-line flag is set.
299 // Otherwise, the TLD must match the TLD of the command-line url.
300 referrer_valid = referrer_url.is_empty() || (referrer_tld == store_tld);
301 }
302
303 if (!download_valid) {
304 std::string download_tld =
305 net::RegistryControlledDomainService::GetDomainAndRegistry(
[email protected]04866c42011-05-03 20:03:50306 download_url);
[email protected]d3071992010-10-08 15:24:07307
308 // Otherwise, the TLD must match the TLD of the command-line url.
309 download_valid = (download_tld == store_tld);
310 }
311 }
312
313 return (referrer_valid && download_valid);
[email protected]b7c2f252009-12-08 00:47:23314}
315
[email protected]d9696672011-03-15 22:45:09316const Extension* ExtensionService::GetInstalledApp(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:44317 const Extension* extension = extensions_.GetExtensionOrAppByURL(
318 ExtensionURLInfo(url));
319 if (extension && extension->is_app())
320 return extension;
[email protected]d9696672011-03-15 22:45:09321
322 return NULL;
323}
324
325bool ExtensionService::IsInstalledApp(const GURL& url) {
326 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27327}
328
[email protected]da5683db2011-04-23 17:12:21329void ExtensionService::SetInstalledAppForRenderer(int renderer_child_id,
330 const Extension* app) {
331 installed_app_hosts_[renderer_child_id] = app;
332}
333
334const Extension* ExtensionService::GetInstalledAppForRenderer(
335 int renderer_child_id) {
336 InstalledAppMap::iterator i = installed_app_hosts_.find(renderer_child_id);
337 if (i == installed_app_hosts_.end())
338 return NULL;
339 return i->second;
340}
341
[email protected]6aeac8342010-10-01 20:21:18342// static
[email protected]d6ebc9792011-04-07 18:18:33343// This function is used to implement the command-line switch
[email protected]3bdba0d2011-08-23 07:17:30344// --uninstall-extension, and to uninstall an extension via sync. The LOG
345// statements within this function are used to inform the user if the uninstall
346// cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00347bool ExtensionService::UninstallExtensionHelper(
348 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18349 const std::string& extension_id) {
[email protected]6aeac8342010-10-01 20:21:18350
[email protected]95da88c42011-03-31 10:07:33351 const Extension* extension =
[email protected]8001df22011-04-28 19:59:47352 extensions_service->GetInstalledExtension(extension_id);
[email protected]95da88c42011-03-31 10:07:33353
[email protected]d6ebc9792011-04-07 18:18:33354 // We can't call UninstallExtension with an invalid extension ID.
[email protected]95da88c42011-03-31 10:07:33355 if (!extension) {
[email protected]6aeac8342010-10-01 20:21:18356 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33357 << "id: " << extension_id;
358 return false;
[email protected]6aeac8342010-10-01 20:21:18359 }
360
[email protected]d6ebc9792011-04-07 18:18:33361 // The following call to UninstallExtension will not allow an uninstall of a
362 // policy-controlled extension.
363 std::string error;
364 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
365 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
366 << ": " << error;
367 return false;
368 }
[email protected]95da88c42011-03-31 10:07:33369
[email protected]6aeac8342010-10-01 20:21:18370 return true;
371}
372
[email protected]eaa7dd182010-12-14 11:09:00373ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24374 const CommandLine* command_line,
375 const FilePath& install_directory,
376 ExtensionPrefs* extension_prefs,
[email protected]0436b102011-04-15 18:30:03377 bool autoupdate_enabled,
378 bool extensions_enabled)
[email protected]d8c8f25f2011-11-02 18:18:01379 : profile_(profile),
[email protected]73c47932010-12-06 18:13:43380 extension_prefs_(extension_prefs),
[email protected]cccdf0aa2011-11-11 03:43:38381 settings_frontend_(extensions::SettingsFrontend::Create(profile)),
[email protected]14908b72011-04-20 06:54:36382 pending_extension_manager_(*ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]a9b00ac2009-06-25 21:03:23383 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03384 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13385 show_extensions_prompts_(true),
[email protected]3ecda252010-11-18 19:50:55386 ready_(false),
[email protected]14908b72011-04-20 06:54:36387 toolbar_model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]8b280302011-10-13 22:22:23388 menu_manager_(profile),
[email protected]af9db5f2011-10-05 05:13:15389 app_notification_manager_(new AppNotificationManager(profile)),
[email protected]18cc5a52011-04-15 16:03:38390 apps_promo_(profile->GetPrefs()),
[email protected]7c1490da2011-10-11 18:53:25391 event_routers_initialized_(false),
[email protected]a1755bd2011-12-09 00:40:20392 extension_warnings_(profile),
[email protected]58edca52012-02-16 21:51:38393 api_resource_controller_(NULL),
[email protected]178f8512012-02-09 01:49:36394 app_shortcut_manager_(profile) {
[email protected]a29a517a2011-01-21 21:11:12395 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20396
[email protected]36a784c2009-06-23 06:21:08397 // Figure out if extension installation should be enabled.
[email protected]6d60703b2009-08-29 01:29:23398 if (command_line->HasSwitch(switches::kDisableExtensions)) {
399 extensions_enabled_ = false;
400 } else if (profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
401 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18402 }
[email protected]36a784c2009-06-23 06:21:08403
[email protected]432115822011-07-10 15:52:27404 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07405 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27406 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
[email protected]ad50def52011-10-19 23:17:07407 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27408 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07409 content::NotificationService::AllBrowserContextsAndSources());
[email protected]2fb7dc982010-09-29 12:24:28410 pref_change_registrar_.Init(profile->GetPrefs());
411 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, this);
412 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, this);
[email protected]4814b512009-11-07 00:12:29413
[email protected]93fd78f42009-07-10 16:43:17414 // Set up the ExtensionUpdater
415 if (autoupdate_enabled) {
416 int update_frequency = kDefaultUpdateFrequencySeconds;
417 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25418 base::StringToInt(command_line->GetSwitchValueASCII(
419 switches::kExtensionsUpdateFrequency),
420 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17421 }
[email protected]42a08162012-03-16 18:09:11422 updater_.reset(new extensions::ExtensionUpdater(this,
423 extension_prefs,
424 profile->GetPrefs(),
425 profile,
426 update_frequency));
[email protected]93fd78f42009-07-10 16:43:17427 }
428
[email protected]25ae0152011-11-18 14:40:02429 component_loader_.reset(
430 new extensions::ComponentLoader(this,
431 profile->GetPrefs(),
432 g_browser_process->local_state()));
[email protected]8e4560b62011-01-14 10:09:14433
[email protected]af9db5f2011-10-05 05:13:15434 app_notification_manager_->Init();
435
[email protected]0436b102011-04-15 18:30:03436 if (extensions_enabled_) {
[email protected]1f2f3002011-12-28 00:40:21437 if (!command_line->HasSwitch(switches::kImport) &&
438 !command_line->HasSwitch(switches::kImportFromFile)) {
439 ExternalExtensionProviderImpl::CreateExternalProviders(
440 this, profile_, &external_extension_providers_);
441 }
[email protected]873531342011-03-09 12:16:05442 }
[email protected]b671760b2010-07-15 21:13:47443
[email protected]aa96d3a2010-08-21 08:45:25444 // Use monochrome icons for Omnibox icons.
[email protected]29d0d4ac2010-09-08 21:10:31445 omnibox_popup_icon_manager_.set_monochrome(true);
[email protected]b671760b2010-07-15 21:13:47446 omnibox_icon_manager_.set_monochrome(true);
[email protected]29d0d4ac2010-09-08 21:10:31447 omnibox_icon_manager_.set_padding(gfx::Insets(0, kOmniboxIconPaddingLeft,
448 0, kOmniboxIconPaddingRight));
[email protected]cb0e50312011-05-09 15:03:07449
[email protected]fb82dcd2012-03-21 14:15:46450 // Set this as the ExtensionService for extension sorting to ensure it
451 // cause syncs if required.
452 extension_prefs_->extension_sorting()->SetExtensionService(this);
453
[email protected]cb0e50312011-05-09 15:03:07454 // How long is the path to the Extensions directory?
455 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
456 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25457}
458
[email protected]84df8332011-12-06 18:22:46459const ExtensionSet* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45460 return &extensions_;
461}
462
[email protected]84df8332011-12-06 18:22:46463const ExtensionSet* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45464 return &disabled_extensions_;
465}
466
[email protected]84df8332011-12-06 18:22:46467const ExtensionSet* ExtensionService::terminated_extensions() const {
[email protected]bb7f40952011-01-13 00:21:20468 return &terminated_extensions_;
469}
470
[email protected]7f4308d2012-01-18 07:43:01471const ExtensionSet* ExtensionService::GenerateInstalledExtensionsSet() const {
472 ExtensionSet* installed_extensions = new ExtensionSet();
473 installed_extensions->InsertAll(extensions_);
474 installed_extensions->InsertAll(disabled_extensions_);
475 installed_extensions->InsertAll(terminated_extensions_);
476 return installed_extensions;
477}
478
[email protected]b2907fd2011-03-25 16:43:37479PendingExtensionManager* ExtensionService::pending_extension_manager() {
480 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45481}
482
[email protected]eaa7dd182010-12-14 11:09:00483ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17484 // No need to unload extensions here because they are profile-scoped, and the
485 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17486
[email protected]8e4560b62011-01-14 10:09:14487 ProviderCollection::const_iterator i;
488 for (i = external_extension_providers_.begin();
489 i != external_extension_providers_.end(); ++i) {
490 ExternalExtensionProviderInterface* provider = i->get();
491 provider->ServiceShutdown();
492 }
[email protected]a1755bd2011-12-09 00:40:20493
[email protected]58edca52012-02-16 21:51:38494 if (api_resource_controller_) {
495 // If this check failed, then a unit test was using an APIResource but
496 // didn't provide the IO thread message loop needed for those resources to
497 // do their job (including destroying themselves at shutdown).
[email protected]e1fae1c2011-12-13 22:17:17498 DCHECK(BrowserThread::IsMessageLoopValid(BrowserThread::IO));
[email protected]58edca52012-02-16 21:51:38499 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE,
500 api_resource_controller_);
[email protected]e1fae1c2011-12-13 22:17:17501 }
[email protected]6014d672008-12-05 00:38:25502}
503
[email protected]d91268022011-08-26 13:17:37504void ExtensionService::InitEventRoutersAfterImport() {
[email protected]cf593af2011-12-30 05:44:39505 RegisterForImportFinished();
506}
507
508void ExtensionService::RegisterForImportFinished() {
509 if (!registrar_.IsRegistered(this, chrome::NOTIFICATION_IMPORT_FINISHED,
510 content::Source<Profile>(profile_))) {
511 registrar_.Add(this, chrome::NOTIFICATION_IMPORT_FINISHED,
512 content::Source<Profile>(profile_));
513 }
514}
515
516void ExtensionService::InitAfterImport() {
517 CheckForExternalUpdates();
518
519 GarbageCollectExtensions();
520
521 // Idempotent, so although there is a possible race if the import
522 // process finished sometime in the middle of ProfileImpl::InitExtensions,
523 // it cannot happen twice.
524 InitEventRouters();
[email protected]d91268022011-08-26 13:17:37525}
526
[email protected]eaa7dd182010-12-14 11:09:00527void ExtensionService::InitEventRouters() {
[email protected]b3d62312b12010-10-14 21:10:18528 if (event_routers_initialized_)
529 return;
530
[email protected]370e9502011-09-14 19:31:44531 downloads_event_router_.reset(new ExtensionDownloadsEventRouter(profile_));
[email protected]d2e6bd62011-10-24 20:29:07532 history_event_router_.reset(new HistoryExtensionEventRouter());
[email protected]f825df22011-06-28 17:36:10533 history_event_router_->ObserveProfile(profile_);
[email protected]97d2f1d2011-01-15 00:41:08534 browser_event_router_.reset(new ExtensionBrowserEventRouter(profile_));
535 browser_event_router_->Init();
[email protected]118de7892011-03-16 13:31:40536 preference_event_router_.reset(new ExtensionPreferenceEventRouter(profile_));
[email protected]23d5f172011-10-25 05:49:53537 bookmark_event_router_.reset(new BookmarkExtensionEventRouter(
[email protected]f825df22011-06-28 17:36:10538 profile_->GetBookmarkModel()));
539 bookmark_event_router_->Init();
540 cookies_event_router_.reset(new ExtensionCookiesEventRouter(profile_));
541 cookies_event_router_->Init();
542 management_event_router_.reset(new ExtensionManagementEventRouter(profile_));
543 management_event_router_->Init();
[email protected]7da9a8232011-05-27 21:07:11544 ExtensionProcessesEventRouter::GetInstance()->ObserveProfile(profile_);
[email protected]f825df22011-06-28 17:36:10545 web_navigation_event_router_.reset(
546 new ExtensionWebNavigationEventRouter(profile_));
547 web_navigation_event_router_->Init();
[email protected]27072cad2011-05-09 19:46:40548
[email protected]eed367e2011-04-12 03:43:31549#if defined(OS_CHROMEOS)
[email protected]2a72c9f2012-03-17 15:10:30550 FileBrowserEventRouterFactory::GetForProfile(
551 profile_)->ObserveFileSystemEvents();
[email protected]771a6182011-10-05 16:32:52552
[email protected]f463bc92012-03-22 16:38:23553 bluetooth_event_router_.reset(
554 new chromeos::ExtensionBluetoothEventRouter(profile_));
555
[email protected]771a6182011-10-05 16:32:52556 input_method_event_router_.reset(
557 new chromeos::ExtensionInputMethodEventRouter);
[email protected]be5bd5742011-07-14 07:21:01558
[email protected]c8bf09be2011-06-25 03:22:27559 ExtensionMediaPlayerEventRouter::GetInstance()->Init(profile_);
[email protected]8baf0dbb2011-11-27 22:47:02560 ExtensionInputImeEventRouter::GetInstance()->Init();
[email protected]eed367e2011-04-12 03:43:31561#endif
[email protected]27072cad2011-05-09 19:46:40562
[email protected]9a2aebe02011-12-02 21:54:14563#if defined(OS_CHROMEOS) && defined(USE_VIRTUAL_KEYBOARD)
[email protected]27072cad2011-05-09 19:46:40564 ExtensionInputUiEventRouter::GetInstance()->Init();
565#endif
566
[email protected]b3d62312b12010-10-14 21:10:18567 event_routers_initialized_ = true;
[email protected]c5ae74ab2010-04-15 18:14:37568}
569
[email protected]b2907fd2011-03-25 16:43:37570const Extension* ExtensionService::GetExtensionById(
571 const std::string& id, bool include_disabled) const {
[email protected]8001df22011-04-28 19:59:47572 return GetExtensionByIdInternal(id, true, include_disabled, false);
[email protected]78994ab02010-12-08 18:06:44573}
574
[email protected]eaa7dd182010-12-14 11:09:00575void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12576 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20577
[email protected]fa6a9102010-11-22 15:38:50578 DCHECK(!ready_); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17579 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32580
[email protected]d8c8f25f2011-11-02 18:18:01581 component_loader_->LoadAll();
582 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]894bb502009-05-21 22:39:57583
[email protected]7a161db2011-12-15 20:45:51584 // If we are running in the import process, don't bother initializing the
585 // extension service since this can interfere with the main browser process
586 // that is already running an extension service for this profile.
587 // TODO(aa): can we start up even less of ExtensionService?
588 // http://crbug.com/107636
[email protected]1f2f3002011-12-28 00:40:21589 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kImport) &&
590 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kImportFromFile)) {
[email protected]cf593af2011-12-30 05:44:39591 if (g_browser_process->profile_manager() &&
592 g_browser_process->profile_manager()->will_import()) {
593 RegisterForImportFinished();
594 } else {
595 // TODO(erikkay) this should probably be deferred to a future point
596 // rather than running immediately at startup.
597 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57598
[email protected]cf593af2011-12-30 05:44:39599 // TODO(erikkay) this should probably be deferred as well.
600 GarbageCollectExtensions();
601 }
[email protected]7a161db2011-12-15 20:45:51602 }
[email protected]6014d672008-12-05 00:38:25603}
604
[email protected]420a0ec2011-06-01 01:07:03605bool ExtensionService::UpdateExtension(
606 const std::string& id,
607 const FilePath& extension_path,
608 const GURL& download_url,
609 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12610 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20611
[email protected]b2907fd2011-03-25 16:43:37612 PendingExtensionInfo pending_extension_info;
613 bool is_pending_extension = pending_extension_manager_.GetById(
614 id, &pending_extension_info);
[email protected]8ef78fd2010-08-19 17:14:32615
[email protected]8001df22011-04-28 19:59:47616 const Extension* extension =
617 GetExtensionByIdInternal(id, true, true, false);
[email protected]5eb375e92010-11-26 07:50:41618 if (!is_pending_extension && !extension) {
[email protected]aa142702010-03-26 01:26:33619 LOG(WARNING) << "Will not update extension " << id
620 << " because it is not installed or pending";
621 // Delete extension_path since we're not creating a CrxInstaller
622 // that would do it for us.
[email protected]14908b72011-04-20 06:54:36623 if (!BrowserThread::PostTask(
624 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:36625 base::Bind(
[email protected]7296f2762011-11-21 19:23:44626 &extension_file_util::DeleteFile, extension_path, false)))
[email protected]14908b72011-04-20 06:54:36627 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03628
629 return false;
[email protected]e957fe52009-06-23 16:51:05630 }
631
[email protected]aa142702010-03-26 01:26:33632 // We want a silent install only for non-pending extensions and
633 // pending extensions that have install_silently set.
634 ExtensionInstallUI* client =
[email protected]b2907fd2011-03-25 16:43:37635 (!is_pending_extension || pending_extension_info.install_silently()) ?
[email protected]aa142702010-03-26 01:26:33636 NULL : new ExtensionInstallUI(profile_);
637
[email protected]d8c8f25f2011-11-02 18:18:01638 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(this, client));
[email protected]6dfbbf82010-03-12 23:09:16639 installer->set_expected_id(id);
[email protected]ec5b50d2010-10-09 16:35:18640 if (is_pending_extension)
[email protected]b2907fd2011-03-25 16:43:37641 installer->set_install_source(pending_extension_info.install_source());
[email protected]5eb375e92010-11-26 07:50:41642 else if (extension)
643 installer->set_install_source(extension->location());
[email protected]0d3e4a22011-06-23 19:02:52644 if (pending_extension_info.install_silently())
645 installer->set_allow_silent_install(true);
[email protected]75764512011-12-19 19:54:28646 // If the extension came from sync and its auto-update URL is from the
647 // webstore, treat it as a webstore install. Note that we ignore some older
648 // extensions with blank auto-update URLs because we are mostly concerned
649 // with restrictions on NaCl extensions, which are newer.
[email protected]a12ce8b22012-01-17 18:40:53650 int creation_flags = Extension::NO_FLAGS;
[email protected]75764512011-12-19 19:54:28651 if ((extension && extension->from_webstore()) ||
652 (!extension && pending_extension_info.is_from_sync() &&
653 extension_urls::IsWebstoreUpdateUrl(
[email protected]a12ce8b22012-01-17 18:40:53654 pending_extension_info.update_url()))) {
655 creation_flags |= Extension::FROM_WEBSTORE;
656 }
657
658 // Bookmark apps being updated is kind of a contradiction, but that's because
659 // we mark the default apps as bookmark apps, and they're hosted in the web
660 // store, thus they can get updated. See http://crbug.com/101605 for more
661 // details.
662 if (extension && extension->from_bookmark())
663 creation_flags |= Extension::FROM_BOOKMARK;
664
665 if (extension) {
666 // Additionally, if the extension is an external extension, we preserve the
667 // creation flags (usually from_bookmark), even if the current pref values
668 // don't reflect them. This is to fix http://crbug.com/109791 for users that
669 // had default apps updated and lost the from_bookmark bit.
670 ProviderCollection::const_iterator i;
671 for (i = external_extension_providers_.begin();
672 i != external_extension_providers_.end(); ++i) {
673 ExternalExtensionProviderInterface* provider = i->get();
674 if (provider->HasExtension(extension->id())) {
675 creation_flags |= provider->GetCreationFlags();
676 break;
677 }
678 }
679 }
680 installer->set_creation_flags(creation_flags);
681
[email protected]6dfbbf82010-03-12 23:09:16682 installer->set_delete_source(true);
[email protected]655b2b1a2011-10-13 17:13:06683 installer->set_download_url(download_url);
[email protected]cb0e50312011-05-09 15:03:07684 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16685 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03686
687 if (out_crx_installer)
688 *out_crx_installer = installer;
689
690 return true;
[email protected]e957fe52009-06-23 16:51:05691}
692
[email protected]eaa7dd182010-12-14 11:09:00693void ExtensionService::ReloadExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12694 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b65272f2009-08-31 15:47:06695 FilePath path;
[email protected]9adb9692010-10-29 23:14:02696 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40697
[email protected]f17dbd42010-08-16 23:21:10698 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06699 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29700 // If the extension has an inspector open for its background page, detach
701 // the inspector and hang onto a cookie for it, so that we can reattach
702 // later.
[email protected]e15be2b2012-03-09 08:42:11703 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
[email protected]06024c62011-10-20 20:57:12704 ExtensionHost* host = manager->GetBackgroundHostForExtension(extension_id);
[email protected]0e12d7d2011-12-01 16:21:44705 if (host && DevToolsAgentHostRegistry::HasDevToolsAgentHost(
706 host->render_view_host())) {
[email protected]4814b512009-11-07 00:12:29707 // Look for an open inspector for the background page.
[email protected]0e12d7d2011-12-01 16:21:44708 DevToolsAgentHost* agent =
709 DevToolsAgentHostRegistry::GetDevToolsAgentHost(
710 host->render_view_host());
711 int devtools_cookie =
712 content::DevToolsManager::GetInstance()->DetachClientHost(agent);
[email protected]4814b512009-11-07 00:12:29713 if (devtools_cookie >= 0)
714 orphaned_dev_tools_[extension_id] = devtools_cookie;
715 }
716
[email protected]b65272f2009-08-31 15:47:06717 path = current_extension->path();
[email protected]f17dbd42010-08-16 23:21:10718 DisableExtension(extension_id);
719 disabled_extension_paths_[extension_id] = path;
[email protected]1eb175082010-02-10 09:26:16720 } else {
721 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06722 }
723
[email protected]43ceb002012-02-10 23:19:15724 // If we're reloading a component extension, use the component extension
725 // loader's reloader.
726 if (component_loader_->Exists(extension_id)) {
727 component_loader_->Reload(extension_id);
728 return;
729 }
730
[email protected]e6090e42010-03-23 22:44:08731 // Check the installed extensions to see if what we're reloading was already
732 // installed.
733 scoped_ptr<ExtensionInfo> installed_extension(
734 extension_prefs_->GetInstalledExtensionInfo(extension_id));
735 if (installed_extension.get() &&
736 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01737 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08738 } else {
[email protected]d8c8f25f2011-11-02 18:18:01739 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08740 // We should always be able to remember the extension's path. If it's not in
741 // the map, someone failed to update |unloaded_extension_paths_|.
742 CHECK(!path.empty());
[email protected]d8c8f25f2011-11-02 18:18:01743 extensions::UnpackedInstaller::Create(this)->Load(path);
[email protected]e6090e42010-03-23 22:44:08744 }
[email protected]9cddd4702009-07-27 22:09:40745}
746
[email protected]fa2416f2011-05-03 08:41:20747bool ExtensionService::UninstallExtension(
[email protected]a12c706e2011-12-01 00:58:08748 std::string extension_id,
[email protected]fa2416f2011-05-03 08:41:20749 bool external_uninstall,
750 std::string* error) {
[email protected]a29a517a2011-01-21 21:11:12751 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20752
[email protected]0d6ec3a72011-09-02 02:09:43753 scoped_refptr<const Extension> extension(GetInstalledExtension(extension_id));
[email protected]631cf822009-05-15 07:01:25754
[email protected]e7afe2452010-08-22 16:19:13755 // Callers should not send us nonexistent extensions.
[email protected]aebe23a32010-12-10 22:15:48756 CHECK(extension);
[email protected]9f1087e2009-06-15 17:29:32757
[email protected]95da88c42011-03-31 10:07:33758 // Policy change which triggers an uninstall will always set
759 // |external_uninstall| to true so this is the only way to uninstall
760 // managed extensions.
[email protected]0d6ec3a72011-09-02 02:09:43761 if (!Extension::UserMayDisable(extension->location()) &&
762 !external_uninstall) {
[email protected]ad50def52011-10-19 23:17:07763 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27764 chrome::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53765 content::Source<Profile>(profile_),
766 content::Details<const Extension>(extension));
[email protected]d6ebc9792011-04-07 18:18:33767 if (error != NULL) {
768 *error = errors::kCannotUninstallManagedExtension;
769 }
770 return false;
771 }
[email protected]95da88c42011-03-31 10:07:33772
[email protected]3bdba0d2011-08-23 07:17:30773 // Extract the data we need for sync now, but don't actually sync until we've
774 // completed the uninstallation.
775 SyncBundle* sync_bundle = GetSyncBundleForExtension(*extension);
776
777 SyncChange sync_change;
778 if (sync_bundle) {
[email protected]aa7599d2011-10-28 07:24:32779 ExtensionSyncData extension_sync_data(
780 *extension,
781 IsExtensionEnabled(extension_id),
782 IsIncognitoEnabled(extension_id),
[email protected]b2689a902011-12-01 00:41:09783 extension_prefs_->GetAppNotificationClientId(extension_id),
[email protected]168389f2011-12-20 17:12:48784 extension_prefs_->IsAppNotificationDisabled(extension_id),
[email protected]fb82dcd2012-03-21 14:15:46785 extension_prefs_->extension_sorting()->
786 GetAppLaunchOrdinal(extension_id),
787 extension_prefs_->extension_sorting()->GetPageOrdinal(extension_id));
[email protected]3bdba0d2011-08-23 07:17:30788 sync_change = extension_sync_data.GetSyncChange(SyncChange::ACTION_DELETE);
789 }
790
[email protected]9b217652010-10-08 22:04:23791 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08792 extension->GetType(), 100);
[email protected]fe2dd7742011-04-19 22:52:49793 RecordPermissionMessagesHistogram(
794 extension, "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23795
[email protected]8e5c89a2011-06-07 18:13:33796 TemplateURLService* url_service =
797 TemplateURLServiceFactory::GetForProfile(profile_);
798 if (url_service)
799 url_service->UnregisterExtensionKeyword(extension);
[email protected]56ad3792010-05-28 17:45:33800
[email protected]831aa212010-03-26 13:55:19801 // Unload before doing more cleanup to ensure that nothing is hanging on to
802 // any of these resources.
[email protected]814a7bf0f2011-08-13 05:30:59803 UnloadExtension(extension_id, extension_misc::UNLOAD_REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19804
[email protected]0d6ec3a72011-09-02 02:09:43805 extension_prefs_->OnExtensionUninstalled(extension_id, extension->location(),
[email protected]831aa212010-03-26 13:55:19806 external_uninstall);
[email protected]9f1087e2009-06-15 17:29:32807
808 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]0d6ec3a72011-09-02 02:09:43809 if (Extension::LOAD != extension->location()) {
[email protected]14908b72011-04-20 06:54:36810 if (!BrowserThread::PostTask(
811 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:36812 base::Bind(
[email protected]14908b72011-04-20 06:54:36813 &extension_file_util::UninstallExtension,
814 install_directory_,
[email protected]fa2416f2011-05-03 08:41:20815 extension_id)))
[email protected]14908b72011-04-20 06:54:36816 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32817 }
818
[email protected]0d6ec3a72011-09-02 02:09:43819 GURL launch_web_url_origin(extension->launch_web_url());
820 launch_web_url_origin = launch_web_url_origin.GetOrigin();
821 bool is_storage_isolated =
822 (extension->is_storage_isolated() &&
823 extension->HasAPIPermission(ExtensionAPIPermission::kExperimental));
824
825 if (extension->is_hosted_app() &&
826 !profile_->GetExtensionSpecialStoragePolicy()->
827 IsStorageProtected(launch_web_url_origin)) {
[email protected]dc0b5a12011-10-14 00:06:13828 ExtensionDataDeleter::StartDeleting(
829 profile_, extension_id, launch_web_url_origin, is_storage_isolated);
[email protected]0d6ec3a72011-09-02 02:09:43830 }
[email protected]dc0b5a12011-10-14 00:06:13831 ExtensionDataDeleter::StartDeleting(
832 profile_, extension_id, extension->url(), is_storage_isolated);
[email protected]0d6ec3a72011-09-02 02:09:43833
[email protected]0dfe05c2011-02-23 23:03:36834 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06835
836 // Notify interested parties that we've uninstalled this extension.
[email protected]ad50def52011-10-19 23:17:07837 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27838 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]6c2381d2011-10-19 02:52:53839 content::Source<Profile>(profile_),
840 content::Details<const std::string>(&extension_id));
[email protected]d6ebc9792011-04-07 18:18:33841
[email protected]3bdba0d2011-08-23 07:17:30842 if (sync_bundle && sync_bundle->HasExtensionId(extension_id)) {
843 sync_bundle->sync_processor->ProcessSyncChanges(
844 FROM_HERE, SyncChangeList(1, sync_change));
845 sync_bundle->synced_extensions.erase(extension_id);
846 }
847
[email protected]333b1de2011-09-12 18:28:50848 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18849 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
850
851 static bool default_apps_trial_exists =
[email protected]568c3312011-12-06 15:36:04852 base::FieldTrialList::TrialExists(kDefaultAppsTrialName);
[email protected]49098f702011-10-13 03:47:18853 if (default_apps_trial_exists) {
854 UMA_HISTOGRAM_ENUMERATION(
855 base::FieldTrial::MakeName("Extensions.ExtensionUninstalled",
[email protected]568c3312011-12-06 15:36:04856 kDefaultAppsTrialName),
[email protected]49098f702011-10-13 03:47:18857 1, 2);
858 }
[email protected]333b1de2011-09-12 18:28:50859
[email protected]7c1490da2011-10-11 18:53:25860 // Uninstalling one extension might have solved the problems of others.
861 // Therefore, we clear warnings of this type for all extensions.
862 std::set<ExtensionWarningSet::WarningType> warnings;
863 extension_warnings_.GetWarningsAffectingExtension(extension_id, &warnings);
864 extension_warnings_.ClearWarnings(warnings);
865
[email protected]d6ebc9792011-04-07 18:18:33866 return true;
[email protected]c10da4b02010-03-25 14:38:32867}
868
[email protected]c3cfb012011-04-06 22:07:35869bool ExtensionService::IsExtensionEnabled(
870 const std::string& extension_id) const {
[email protected]84df8332011-12-06 18:22:46871 if (extensions_.Contains(extension_id) ||
872 terminated_extensions_.Contains(extension_id))
[email protected]36429da2011-07-11 20:25:18873 return true;
874
[email protected]84df8332011-12-06 18:22:46875 if (disabled_extensions_.Contains(extension_id))
[email protected]ad83ca242011-07-29 01:32:25876 return false;
877
878 // If the extension hasn't been loaded yet, check the prefs for it. Assume
879 // enabled unless otherwise noted.
880 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
881 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35882}
883
884bool ExtensionService::IsExternalExtensionUninstalled(
885 const std::string& extension_id) const {
886 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
887}
888
[email protected]eaa7dd182010-12-14 11:09:00889void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12890 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20891
[email protected]06f92562011-04-29 19:27:31892 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39893 return;
[email protected]0c6da502009-08-14 22:32:39894
[email protected]b3317ad2011-04-28 23:46:00895 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]1784e83a2009-09-08 21:01:52896
[email protected]06f92562011-04-29 19:27:31897 const Extension* extension =
898 GetExtensionByIdInternal(extension_id, false, true, false);
899 // This can happen if sync enables an extension that is not
900 // installed yet.
901 if (!extension)
902 return;
903
[email protected]0c6da502009-08-14 22:32:39904 // Move it over to the enabled list.
[email protected]84df8332011-12-06 18:22:46905 extensions_.Insert(make_scoped_refptr(extension));
906 disabled_extensions_.Remove(extension->id());
[email protected]0c6da502009-08-14 22:32:39907
[email protected]f681c782010-11-19 11:19:39908 // Make sure any browser action contained within it is not hidden.
909 extension_prefs_->SetBrowserActionVisibility(extension, true);
910
[email protected]62d30f42009-10-01 22:36:06911 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30912
913 SyncExtensionChangeIfNeeded(*extension);
[email protected]0c6da502009-08-14 22:32:39914}
915
[email protected]eaa7dd182010-12-14 11:09:00916void ExtensionService::DisableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12917 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20918
[email protected]b2ba9962009-12-10 20:10:15919 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:31920 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:52921 return;
[email protected]1784e83a2009-09-08 21:01:52922
[email protected]06f92562011-04-29 19:27:31923 const Extension* extension = GetInstalledExtension(extension_id);
924 // |extension| can be NULL if sync disables an extension that is not
925 // installed yet.
926 if (extension && !Extension::UserMayDisable(extension->location()))
[email protected]95da88c42011-03-31 10:07:33927 return;
928
[email protected]b3317ad2011-04-28 23:46:00929 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]1784e83a2009-09-08 21:01:52930
[email protected]06f92562011-04-29 19:27:31931 extension = GetExtensionByIdInternal(extension_id, true, false, true);
932 if (!extension)
933 return;
934
[email protected]1784e83a2009-09-08 21:01:52935 // Move it over to the disabled list.
[email protected]84df8332011-12-06 18:22:46936 disabled_extensions_.Insert(make_scoped_refptr(extension));
937 if (extensions_.Contains(extension->id()))
938 extensions_.Remove(extension->id());
939 else
940 terminated_extensions_.Remove(extension->id());
[email protected]1784e83a2009-09-08 21:01:52941
[email protected]814a7bf0f2011-08-13 05:30:59942 NotifyExtensionUnloaded(extension, extension_misc::UNLOAD_REASON_DISABLE);
[email protected]3bdba0d2011-08-23 07:17:30943
944 SyncExtensionChangeIfNeeded(*extension);
[email protected]7c1490da2011-10-11 18:53:25945
946 // Deactivating one extension might have solved the problems of others.
947 // Therefore, we clear warnings of this type for all extensions.
948 std::set<ExtensionWarningSet::WarningType> warnings;
949 extension_warnings_.GetWarningsAffectingExtension(extension_id, &warnings);
950 extension_warnings_.ClearWarnings(warnings);
[email protected]1784e83a2009-09-08 21:01:52951}
952
[email protected]eaa7dd182010-12-14 11:09:00953void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]8d888c12010-11-30 00:00:25954 const Extension* extension) {
955 CHECK(extension);
[email protected]fe2dd7742011-04-19 22:52:49956 RecordPermissionMessagesHistogram(
957 extension, "Extensions.Permissions_ReEnable");
[email protected]c333e792012-01-06 16:57:39958 extensions::PermissionsUpdater perms_updater(profile());
959 perms_updater.GrantActivePermissions(extension);
[email protected]8d888c12010-11-30 00:00:25960 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
961 EnableExtension(extension->id());
962}
963
[email protected]fe2dd7742011-04-19 22:52:49964// static
965void ExtensionService::RecordPermissionMessagesHistogram(
966 const Extension* e, const char* histogram) {
967 // Since this is called from multiple sources, and since the Histogram macros
968 // use statics, we need to manually lookup the Histogram ourselves.
969 base::Histogram* counter = base::LinearHistogram::FactoryGet(
970 histogram,
971 1,
[email protected]0d3e4a22011-06-23 19:02:52972 ExtensionPermissionMessage::kEnumBoundary,
973 ExtensionPermissionMessage::kEnumBoundary + 1,
[email protected]fe2dd7742011-04-19 22:52:49974 base::Histogram::kUmaTargetedHistogramFlag);
975
[email protected]0d3e4a22011-06-23 19:02:52976 ExtensionPermissionMessages permissions = e->GetPermissionMessages();
[email protected]fe2dd7742011-04-19 22:52:49977 if (permissions.empty()) {
[email protected]0d3e4a22011-06-23 19:02:52978 counter->Add(ExtensionPermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:49979 } else {
[email protected]0d3e4a22011-06-23 19:02:52980 for (ExtensionPermissionMessages::iterator it = permissions.begin();
981 it != permissions.end(); ++it)
982 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:49983 }
984}
985
[email protected]eaa7dd182010-12-14 11:09:00986void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:42987 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:06988 // was loaded, otherwise a race can arise where a renderer that is created
989 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:42990 // that the request context doesn't yet know about. The profile is responsible
991 // for ensuring its URLRequestContexts appropriately discover the loaded
992 // extension.
[email protected]e15be2b2012-03-09 08:42:11993 profile_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:06994
[email protected]19647262011-12-16 09:57:49995 // Tell renderers about the new extension, unless it's a theme (renderers
996 // don't need to know about themes).
997 if (!extension->is_theme()) {
998 for (content::RenderProcessHost::iterator i(
999 content::RenderProcessHost::AllHostsIterator());
1000 !i.IsAtEnd(); i.Advance()) {
1001 content::RenderProcessHost* host = i.GetCurrentValue();
1002 Profile* host_profile =
1003 Profile::FromBrowserContext(host->GetBrowserContext());
1004 if (host_profile->GetOriginalProfile() ==
1005 profile_->GetOriginalProfile()) {
1006 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
1007 1, ExtensionMsg_Loaded_Params(extension));
1008 host->Send(
1009 new ExtensionMsg_Loaded(loaded_extensions));
1010 }
[email protected]c8d407e2011-04-28 21:27:171011 }
[email protected]77a6970c2011-04-23 16:58:561012 }
1013
[email protected]3442a662012-01-12 08:06:171014 // Tell subsystems that use the EXTENSION_LOADED notification about the new
1015 // extension.
1016 //
1017 // NOTE: It is important that this happen after notifying the renderers about
1018 // the new extensions so that if we navigate to an extension URL in
1019 // NOTIFICATION_EXTENSION_LOADED, the renderer is guaranteed to know about it.
1020 content::NotificationService::current()->Notify(
1021 chrome::NOTIFICATION_EXTENSION_LOADED,
1022 content::Source<Profile>(profile_),
1023 content::Details<const Extension>(extension));
1024
[email protected]c8d407e2011-04-28 21:27:171025 // Tell a random-ass collection of other subsystems about the new extension.
1026 // TODO(aa): What should we do with all this goop? Can it move into the
1027 // relevant objects via EXTENSION_LOADED?
1028
1029 profile_->GetExtensionSpecialStoragePolicy()->
1030 GrantRightsForExtension(extension);
1031
1032 UpdateActiveExtensionsInCrashReporter();
1033
1034 ExtensionWebUI::RegisterChromeURLOverrides(
1035 profile_, extension->GetChromeURLOverrides());
1036
[email protected]8e5c89a2011-06-07 18:13:331037 TemplateURLService* url_service =
1038 TemplateURLServiceFactory::GetForProfile(profile_);
1039 if (url_service)
1040 url_service->RegisterExtensionKeyword(extension);
[email protected]c8d407e2011-04-28 21:27:171041
1042 // Load the icon for omnibox-enabled extensions so it will be ready to display
1043 // in the URL bar.
1044 if (!extension->omnibox_keyword().empty()) {
1045 omnibox_popup_icon_manager_.LoadIcon(extension);
1046 omnibox_icon_manager_.LoadIcon(extension);
1047 }
1048
1049 // If the extension has permission to load chrome://favicon/ resources we need
1050 // to make sure that the FaviconSource is registered with the
1051 // ChromeURLDataManager.
1052 if (extension->HasHostPermission(GURL(chrome::kChromeUIFaviconURL))) {
1053 FaviconSource* favicon_source = new FaviconSource(profile_,
1054 FaviconSource::FAVICON);
1055 profile_->GetChromeURLDataManager()->AddDataSource(favicon_source);
1056 }
[email protected]5eddc3e2011-10-26 04:33:311057 // Same for chrome://thumb/ resources.
1058 if (extension->HasHostPermission(GURL(chrome::kChromeUIThumbnailURL))) {
1059 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_);
1060 profile_->GetChromeURLDataManager()->AddDataSource(thumbnail_source);
1061 }
[email protected]c8d407e2011-04-28 21:27:171062
1063 // TODO(mpcomplete): This ends up affecting all profiles. See crbug.com/80757.
[email protected]2de307592011-04-05 21:16:581064 bool plugins_changed = false;
1065 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1066 const Extension::PluginInfo& plugin = extension->plugins()[i];
[email protected]f520b5b2011-11-08 02:42:141067 PluginService::GetInstance()->RefreshPlugins();
1068 PluginService::GetInstance()->AddExtraPluginPath(plugin.path);
[email protected]2de307592011-04-05 21:16:581069 plugins_changed = true;
[email protected]476a27032011-09-13 13:40:491070 ChromePluginServiceFilter* filter =
1071 ChromePluginServiceFilter::GetInstance();
1072 if (plugin.is_public) {
1073 filter->RestrictPluginToProfileAndOrigin(
1074 plugin.path, profile_, GURL());
1075 } else {
1076 filter->RestrictPluginToProfileAndOrigin(
1077 plugin.path, profile_, extension->url());
[email protected]2de307592011-04-05 21:16:581078 }
1079 }
[email protected]84396dbc2011-04-14 06:33:421080
1081 bool nacl_modules_changed = false;
1082 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1083 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001084 RegisterNaClModule(module.url, module.mime_type);
[email protected]84396dbc2011-04-14 06:33:421085 nacl_modules_changed = true;
1086 }
1087
[email protected]ed0ba002011-05-26 16:55:131088 if (nacl_modules_changed)
1089 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421090
1091 if (plugins_changed || nacl_modules_changed)
[email protected]45a22e62011-10-12 09:48:021092 PluginService::GetInstance()->PurgePluginListCache(profile_, false);
[email protected]b4d9d122011-06-17 01:58:541093
[email protected]14da67b02011-08-02 05:49:131094#if defined(OS_CHROMEOS)
[email protected]b4d9d122011-06-17 01:58:541095 for (std::vector<Extension::InputComponentInfo>::const_iterator component =
1096 extension->input_components().begin();
1097 component != extension->input_components().end();
1098 ++component) {
[email protected]14da67b02011-08-02 05:49:131099 if (component->type == Extension::INPUT_COMPONENT_TYPE_IME) {
[email protected]8baf0dbb2011-11-27 22:47:021100 ExtensionInputImeEventRouter::GetInstance()->RegisterIme(
[email protected]14da67b02011-08-02 05:49:131101 profile_, extension->id(), *component);
1102 }
[email protected]9a2aebe02011-12-02 21:54:141103#if defined(USE_VIRTUAL_KEYBOARD)
[email protected]b4d9d122011-06-17 01:58:541104 if (component->type == Extension::INPUT_COMPONENT_TYPE_VIRTUAL_KEYBOARD &&
1105 !component->layouts.empty()) {
[email protected]a4215ac02011-12-04 04:11:561106 chromeos::input_method::InputMethodManager* input_method_manager =
1107 chromeos::input_method::InputMethodManager::GetInstance();
1108 const bool is_system_keyboard =
1109 extension->location() == Extension::COMPONENT;
[email protected]cbd4c042011-08-13 18:22:191110 input_method_manager->RegisterVirtualKeyboard(
1111 extension->url(),
1112 component->name, // human-readable name of the keyboard extension.
1113 component->layouts,
[email protected]a4215ac02011-12-04 04:11:561114 is_system_keyboard);
[email protected]b4d9d122011-06-17 01:58:541115 }
[email protected]14da67b02011-08-02 05:49:131116#endif
[email protected]b4d9d122011-06-17 01:58:541117 }
1118#endif
[email protected]62d30f42009-10-01 22:36:061119}
1120
[email protected]a9f39a312010-12-23 22:14:271121void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591122 const Extension* extension,
1123 extension_misc::UnloadedExtensionReason reason) {
[email protected]a9f39a312010-12-23 22:14:271124 UnloadedExtensionInfo details(extension, reason);
[email protected]ad50def52011-10-19 23:17:071125 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271126 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531127 content::Source<Profile>(profile_),
1128 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061129
[email protected]f3b1a082011-11-18 00:34:301130 for (content::RenderProcessHost::iterator i(
1131 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:561132 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:301133 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081134 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301135 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:081136 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171137 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561138 }
1139
[email protected]e15be2b2012-03-09 08:42:111140 profile_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]c8d407e2011-04-28 21:27:171141 profile_->GetExtensionSpecialStoragePolicy()->
1142 RevokeRightsForExtension(extension);
1143
1144 ExtensionWebUI::UnregisterChromeURLOverrides(
1145 profile_, extension->GetChromeURLOverrides());
1146
[email protected]b777b332011-04-16 04:01:081147#if defined(OS_CHROMEOS)
1148 // Revoke external file access to
[email protected]55eb70e762012-02-20 17:38:391149 if (BrowserContext::GetFileSystemContext(profile_) &&
1150 BrowserContext::GetFileSystemContext(profile_)->external_provider()) {
1151 BrowserContext::GetFileSystemContext(profile_)->external_provider()->
[email protected]c8d407e2011-04-28 21:27:171152 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061153 }
[email protected]54ccafa2011-12-06 23:36:321154
1155 if (extension->input_components().size() > 0) {
1156 ExtensionInputImeEventRouter::GetInstance()->UnregisterAllImes(
1157 profile_, extension->id());
1158 }
[email protected]c8d407e2011-04-28 21:27:171159#endif
1160
1161 UpdateActiveExtensionsInCrashReporter();
[email protected]2de307592011-04-05 21:16:581162
1163 bool plugins_changed = false;
1164 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1165 const Extension::PluginInfo& plugin = extension->plugins()[i];
[email protected]b6a2f8de2012-01-31 17:28:491166 PluginService::GetInstance()->ForcePluginShutdown(plugin.path);
[email protected]f520b5b2011-11-08 02:42:141167 PluginService::GetInstance()->RefreshPlugins();
1168 PluginService::GetInstance()->RemoveExtraPluginPath(plugin.path);
[email protected]2de307592011-04-05 21:16:581169 plugins_changed = true;
[email protected]476a27032011-09-13 13:40:491170 ChromePluginServiceFilter::GetInstance()->UnrestrictPlugin(plugin.path);
[email protected]2de307592011-04-05 21:16:581171 }
[email protected]84396dbc2011-04-14 06:33:421172
1173 bool nacl_modules_changed = false;
1174 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1175 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001176 UnregisterNaClModule(module.url);
[email protected]84396dbc2011-04-14 06:33:421177 nacl_modules_changed = true;
1178 }
1179
[email protected]ed0ba002011-05-26 16:55:131180 if (nacl_modules_changed)
1181 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421182
1183 if (plugins_changed || nacl_modules_changed)
[email protected]45a22e62011-10-12 09:48:021184 PluginService::GetInstance()->PurgePluginListCache(profile_, false);
[email protected]62d30f42009-10-01 22:36:061185}
1186
[email protected]eaa7dd182010-12-14 11:09:001187void ExtensionService::UpdateExtensionBlacklist(
[email protected]6b75ec32009-08-14 06:37:181188 const std::vector<std::string>& blacklist) {
1189 // Use this set to indicate if an extension in the blacklist has been used.
1190 std::set<std::string> blacklist_set;
1191 for (unsigned int i = 0; i < blacklist.size(); ++i) {
1192 if (Extension::IdIsValid(blacklist[i])) {
1193 blacklist_set.insert(blacklist[i]);
1194 }
1195 }
1196 extension_prefs_->UpdateBlacklist(blacklist_set);
1197 std::vector<std::string> to_be_removed;
1198 // Loop current extensions, unload installed extensions.
[email protected]84df8332011-12-06 18:22:461199 for (ExtensionSet::const_iterator iter = extensions_.begin();
[email protected]6b75ec32009-08-14 06:37:181200 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021201 const Extension* extension = (*iter);
[email protected]6b75ec32009-08-14 06:37:181202 if (blacklist_set.find(extension->id()) != blacklist_set.end()) {
1203 to_be_removed.push_back(extension->id());
1204 }
1205 }
1206
1207 // UnloadExtension will change the extensions_ list. So, we should
1208 // call it outside the iterator loop.
1209 for (unsigned int i = 0; i < to_be_removed.size(); ++i) {
[email protected]814a7bf0f2011-08-13 05:30:591210 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]6b75ec32009-08-14 06:37:181211 }
1212}
1213
[email protected]0a071a32011-02-08 00:18:241214Profile* ExtensionService::profile() {
1215 return profile_;
1216}
1217
[email protected]eaa7dd182010-12-14 11:09:001218ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451219 return extension_prefs_;
1220}
1221
[email protected]0d9a2202011-11-09 13:48:411222extensions::SettingsFrontend* ExtensionService::settings_frontend() {
[email protected]cccdf0aa2011-11-11 03:43:381223 return settings_frontend_.get();
[email protected]b7f853e282011-08-10 09:24:201224}
1225
[email protected]b790b072011-05-20 09:46:441226ExtensionContentSettingsStore*
1227 ExtensionService::GetExtensionContentSettingsStore() {
1228 return extension_prefs()->content_settings_store();
1229}
1230
[email protected]25ae0152011-11-18 14:40:021231bool ExtensionService::is_ready() {
1232 return ready_;
1233}
1234
[email protected]42a08162012-03-16 18:09:111235extensions::ExtensionUpdater* ExtensionService::updater() {
[email protected]2859946f2011-04-04 18:18:061236 return updater_.get();
1237}
1238
[email protected]eaa7dd182010-12-14 11:09:001239void ExtensionService::CheckAdminBlacklist() {
[email protected]aa96d3a2010-08-21 08:45:251240 std::vector<std::string> to_be_removed;
1241 // Loop through extensions list, unload installed extensions.
[email protected]84df8332011-12-06 18:22:461242 for (ExtensionSet::const_iterator iter = extensions_.begin();
[email protected]aa96d3a2010-08-21 08:45:251243 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021244 const Extension* extension = (*iter);
[email protected]05aad2da2011-10-28 10:12:371245 if (!extension_prefs_->IsExtensionAllowedByPolicy(extension->id(),
1246 extension->location())) {
[email protected]aa96d3a2010-08-21 08:45:251247 to_be_removed.push_back(extension->id());
[email protected]05aad2da2011-10-28 10:12:371248 }
[email protected]aa96d3a2010-08-21 08:45:251249 }
1250
1251 // UnloadExtension will change the extensions_ list. So, we should
1252 // call it outside the iterator loop.
1253 for (unsigned int i = 0; i < to_be_removed.size(); ++i)
[email protected]814a7bf0f2011-08-13 05:30:591254 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]aa96d3a2010-08-21 08:45:251255}
1256
[email protected]31206602011-04-13 23:07:321257void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351258 if (updater()) {
[email protected]31206602011-04-13 23:07:321259 updater()->CheckSoon();
[email protected]c3cfb012011-04-06 22:07:351260 } else {
[email protected]31206602011-04-13 23:07:321261 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351262 }
1263}
1264
[email protected]3bdba0d2011-08-23 07:17:301265namespace {
1266 bool IsSyncableNone(const Extension& extension) { return false; }
[email protected]406b5a92011-11-08 11:58:261267} // namespace
[email protected]3bdba0d2011-08-23 07:17:301268
1269ExtensionService::SyncBundle::SyncBundle()
[email protected]0677c6952012-03-23 21:59:001270 : filter(IsSyncableNone) {
[email protected]b05fb9ff2011-04-23 00:07:561271}
1272
[email protected]3bdba0d2011-08-23 07:17:301273ExtensionService::SyncBundle::~SyncBundle() {
1274}
1275
[email protected]0677c6952012-03-23 21:59:001276void ExtensionService::SyncBundle::Reset() {
1277 filter = IsSyncableNone;
1278 synced_extensions.clear();
1279 pending_sync_data.clear();
1280 sync_processor.reset();
1281}
1282
[email protected]3bdba0d2011-08-23 07:17:301283bool ExtensionService::SyncBundle::HasExtensionId(const std::string& id) const {
1284 return synced_extensions.find(id) != synced_extensions.end();
1285}
1286
1287bool ExtensionService::SyncBundle::HasPendingExtensionId(const std::string& id)
1288 const {
1289 return pending_sync_data.find(id) != pending_sync_data.end();
1290}
1291
[email protected]3bdba0d2011-08-23 07:17:301292ExtensionService::SyncBundle* ExtensionService::GetSyncBundleForExtension(
1293 const Extension& extension) {
1294 if (app_sync_bundle_.filter(extension))
1295 return &app_sync_bundle_;
1296 else if (extension_sync_bundle_.filter(extension))
1297 return &extension_sync_bundle_;
1298 else
1299 return NULL;
1300}
1301
1302ExtensionService::SyncBundle*
1303 ExtensionService::GetSyncBundleForExtensionSyncData(
1304 const ExtensionSyncData& extension_sync_data) {
1305 switch (extension_sync_data.type()) {
1306 case Extension::SYNC_TYPE_APP:
1307 return &app_sync_bundle_;
1308 case Extension::SYNC_TYPE_EXTENSION:
1309 return &extension_sync_bundle_;
1310 default:
1311 NOTREACHED();
1312 return NULL;
1313 }
1314}
1315
1316#define GET_SYNC_BUNDLE_FOR_MODEL_TYPE_BODY() \
1317 do { \
1318 switch (type) { \
1319 case syncable::APPS: \
1320 return &app_sync_bundle_; \
1321 case syncable::EXTENSIONS: \
1322 return &extension_sync_bundle_; \
1323 default: \
1324 NOTREACHED(); \
1325 return NULL; \
1326 } \
1327 } while (0)
1328
1329const ExtensionService::SyncBundle*
1330 ExtensionService::GetSyncBundleForModelTypeConst(
1331 syncable::ModelType type) const {
1332 GET_SYNC_BUNDLE_FOR_MODEL_TYPE_BODY();
1333}
1334
1335ExtensionService::SyncBundle* ExtensionService::GetSyncBundleForModelType(
1336 syncable::ModelType type) {
1337 GET_SYNC_BUNDLE_FOR_MODEL_TYPE_BODY();
1338}
1339
1340#undef GET_SYNC_BUNDLE_FOR_MODEL_TYPE_BODY
1341
1342SyncError ExtensionService::MergeDataAndStartSyncing(
1343 syncable::ModelType type,
1344 const SyncDataList& initial_sync_data,
[email protected]0677c6952012-03-23 21:59:001345 scoped_ptr<SyncChangeProcessor> sync_processor) {
[email protected]3bdba0d2011-08-23 07:17:301346 SyncBundle* bundle = NULL;
1347
1348 switch (type) {
1349 case syncable::EXTENSIONS:
1350 bundle = &extension_sync_bundle_;
1351 bundle->filter = IsSyncableExtension;
1352 break;
1353
1354 case syncable::APPS:
1355 bundle = &app_sync_bundle_;
1356 bundle->filter = IsSyncableApp;
1357 break;
1358
1359 default:
1360 LOG(FATAL) << "Got " << type << " ModelType";
1361 }
[email protected]0677c6952012-03-23 21:59:001362 DCHECK(!bundle->sync_processor.get());
1363 DCHECK(sync_processor.get());
1364 bundle->sync_processor = sync_processor.Pass();
[email protected]3bdba0d2011-08-23 07:17:301365
[email protected]b1417ab2011-12-15 17:09:111366 // Process extensions from sync.
[email protected]3bdba0d2011-08-23 07:17:301367 for (SyncDataList::const_iterator i = initial_sync_data.begin();
1368 i != initial_sync_data.end();
1369 ++i) {
1370 ExtensionSyncData extension_sync_data = ExtensionSyncData(*i);
1371 bundle->synced_extensions.insert(extension_sync_data.id());
1372 ProcessExtensionSyncData(extension_sync_data, *bundle);
1373 }
1374
[email protected]b1417ab2011-12-15 17:09:111375 // Process local extensions.
1376 // TODO(yoz): Determine whether pending extensions should be considered too.
1377 // See crbug.com/104399.
[email protected]3bdba0d2011-08-23 07:17:301378 SyncDataList sync_data_list = GetAllSyncData(type);
1379 SyncChangeList sync_change_list;
1380 for (SyncDataList::const_iterator i = sync_data_list.begin();
1381 i != sync_data_list.end();
1382 ++i) {
[email protected]b1417ab2011-12-15 17:09:111383 if (bundle->HasExtensionId(i->GetTag())) {
[email protected]3bdba0d2011-08-23 07:17:301384 sync_change_list.push_back(SyncChange(SyncChange::ACTION_UPDATE, *i));
[email protected]b1417ab2011-12-15 17:09:111385 } else {
1386 bundle->synced_extensions.insert(i->GetTag());
[email protected]3bdba0d2011-08-23 07:17:301387 sync_change_list.push_back(SyncChange(SyncChange::ACTION_ADD, *i));
[email protected]b1417ab2011-12-15 17:09:111388 }
[email protected]3bdba0d2011-08-23 07:17:301389 }
1390 bundle->sync_processor->ProcessSyncChanges(FROM_HERE, sync_change_list);
1391
[email protected]0ffd9e52012-01-26 21:44:341392 extension_prefs_->extension_sorting()->FixNTPOrdinalCollisions();
1393
[email protected]3bdba0d2011-08-23 07:17:301394 return SyncError();
1395}
1396
1397void ExtensionService::StopSyncing(syncable::ModelType type) {
1398 SyncBundle* bundle = GetSyncBundleForModelType(type);
1399 CHECK(bundle);
[email protected]0677c6952012-03-23 21:59:001400 bundle->Reset();
[email protected]3bdba0d2011-08-23 07:17:301401}
1402
1403SyncDataList ExtensionService::GetAllSyncData(syncable::ModelType type) const {
1404 const SyncBundle* bundle = GetSyncBundleForModelTypeConst(type);
1405 CHECK(bundle);
1406 std::vector<ExtensionSyncData> extension_sync_data = GetSyncDataList(*bundle);
1407 SyncDataList result(extension_sync_data.size());
1408 for (int i = 0; i < static_cast<int>(extension_sync_data.size()); ++i) {
1409 result[i] = extension_sync_data[i].GetSyncData();
1410 }
1411 return result;
1412}
1413
1414SyncError ExtensionService::ProcessSyncChanges(
1415 const tracked_objects::Location& from_here,
1416 const SyncChangeList& change_list) {
1417 for (SyncChangeList::const_iterator i = change_list.begin();
1418 i != change_list.end();
1419 ++i) {
1420 ExtensionSyncData extension_sync_data = ExtensionSyncData(*i);
1421 SyncBundle* bundle = GetSyncBundleForExtensionSyncData(extension_sync_data);
1422 CHECK(bundle);
1423
1424 if (extension_sync_data.uninstalled())
1425 bundle->synced_extensions.erase(extension_sync_data.id());
1426 else
1427 bundle->synced_extensions.insert(extension_sync_data.id());
1428 ProcessExtensionSyncData(extension_sync_data, *bundle);
1429 }
1430
[email protected]0ffd9e52012-01-26 21:44:341431 extension_prefs()->extension_sorting()->FixNTPOrdinalCollisions();
1432
[email protected]3bdba0d2011-08-23 07:17:301433 return SyncError();
[email protected]b05fb9ff2011-04-23 00:07:561434}
1435
1436void ExtensionService::GetSyncDataListHelper(
[email protected]84df8332011-12-06 18:22:461437 const ExtensionSet& extensions,
[email protected]3bdba0d2011-08-23 07:17:301438 const SyncBundle& bundle,
[email protected]b05fb9ff2011-04-23 00:07:561439 std::vector<ExtensionSyncData>* sync_data_list) const {
[email protected]84df8332011-12-06 18:22:461440 for (ExtensionSet::const_iterator it = extensions.begin();
[email protected]b05fb9ff2011-04-23 00:07:561441 it != extensions.end(); ++it) {
1442 const Extension& extension = **it;
[email protected]3bdba0d2011-08-23 07:17:301443 if (bundle.filter(extension) &&
1444 // If we have pending extension data for this extension, then this
1445 // version is out of date. We'll sync back the version we got from
1446 // sync.
1447 !bundle.HasPendingExtensionId(extension.id())) {
[email protected]aa7599d2011-10-28 07:24:321448 sync_data_list->push_back(ExtensionSyncData(
1449 extension,
1450 IsExtensionEnabled(extension.id()),
1451 IsIncognitoEnabled(extension.id()),
[email protected]b2689a902011-12-01 00:41:091452 extension_prefs_->GetAppNotificationClientId(extension.id()),
[email protected]168389f2011-12-20 17:12:481453 extension_prefs_->IsAppNotificationDisabled(extension.id()),
[email protected]fb82dcd2012-03-21 14:15:461454 extension_prefs_->extension_sorting()->
1455 GetAppLaunchOrdinal(extension.id()),
1456 extension_prefs_->extension_sorting()->
1457 GetPageOrdinal(extension.id())));
[email protected]b05fb9ff2011-04-23 00:07:561458 }
1459 }
1460}
1461
1462std::vector<ExtensionSyncData> ExtensionService::GetSyncDataList(
[email protected]3bdba0d2011-08-23 07:17:301463 const SyncBundle& bundle) const {
1464 std::vector<ExtensionSyncData> extension_sync_list;
1465 GetSyncDataListHelper(extensions_, bundle, &extension_sync_list);
1466 GetSyncDataListHelper(disabled_extensions_, bundle, &extension_sync_list);
1467 GetSyncDataListHelper(terminated_extensions_, bundle, &extension_sync_list);
1468
1469 for (std::map<std::string, ExtensionSyncData>::const_iterator i =
1470 bundle.pending_sync_data.begin();
1471 i != bundle.pending_sync_data.end();
1472 ++i) {
1473 extension_sync_list.push_back(i->second);
1474 }
1475
1476 return extension_sync_list;
[email protected]b05fb9ff2011-04-23 00:07:561477}
1478
[email protected]3bdba0d2011-08-23 07:17:301479void ExtensionService::ProcessExtensionSyncData(
[email protected]90310d92011-04-17 07:35:041480 const ExtensionSyncData& extension_sync_data,
[email protected]3bdba0d2011-08-23 07:17:301481 SyncBundle& bundle) {
1482 const std::string& id = extension_sync_data.id();
[email protected]96e989b2011-08-30 19:35:061483 const Extension* extension = GetInstalledExtension(id);
1484
1485 // TODO(bolms): we should really handle this better. The particularly bad
1486 // case is where an app becomes an extension or vice versa, and we end up with
1487 // a zombie extension that won't go away.
1488 if (extension && !bundle.filter(*extension))
1489 return;
[email protected]90310d92011-04-17 07:35:041490
1491 // Handle uninstalls first.
[email protected]3bdba0d2011-08-23 07:17:301492 if (extension_sync_data.uninstalled()) {
[email protected]90310d92011-04-17 07:35:041493 std::string error;
1494 if (!UninstallExtensionHelper(this, id)) {
1495 LOG(WARNING) << "Could not uninstall extension " << id
1496 << " for sync";
1497 }
1498 return;
1499 }
1500
[email protected]06f92562011-04-29 19:27:311501 // Set user settings.
[email protected]3bdba0d2011-08-23 07:17:301502 if (extension_sync_data.enabled()) {
[email protected]06f92562011-04-29 19:27:311503 EnableExtension(id);
1504 } else {
1505 DisableExtension(id);
1506 }
[email protected]81b14cc2011-04-29 00:39:371507
[email protected]406b5a92011-11-08 11:58:261508 // We need to cache some version information here because setting the
1509 // incognito flag invalidates the |extension| pointer (it reloads the
1510 // extension).
[email protected]9060d8b02012-01-13 02:14:301511 bool extension_installed = (extension != NULL);
[email protected]406b5a92011-11-08 11:58:261512 int result = extension ?
1513 extension->version()->CompareTo(extension_sync_data.version()) : 0;
1514 SetIsIncognitoEnabled(id, extension_sync_data.incognito_enabled());
1515 extension = NULL; // No longer safe to use.
1516
[email protected]0ffd9e52012-01-26 21:44:341517 if (extension_sync_data.app_launch_ordinal().IsValid() &&
1518 extension_sync_data.page_ordinal().IsValid()) {
[email protected]fb82dcd2012-03-21 14:15:461519 extension_prefs_->extension_sorting()->SetAppLaunchOrdinal(
1520 id,
1521 extension_sync_data.app_launch_ordinal());
1522 extension_prefs_->extension_sorting()->SetPageOrdinal(
1523 id,
1524 extension_sync_data.page_ordinal());
[email protected]0ffd9e52012-01-26 21:44:341525 }
1526
[email protected]406b5a92011-11-08 11:58:261527 if (extension_installed) {
[email protected]06f92562011-04-29 19:27:311528 // If the extension is already installed, check if it's outdated.
[email protected]90310d92011-04-17 07:35:041529 if (result < 0) {
1530 // Extension is outdated.
[email protected]3bdba0d2011-08-23 07:17:301531 bundle.pending_sync_data[extension_sync_data.id()] = extension_sync_data;
[email protected]90310d92011-04-17 07:35:041532 CheckForUpdatesSoon();
[email protected]90310d92011-04-17 07:35:041533 }
[email protected]b10a4abe2011-12-10 02:31:231534 if (extension_sync_data.type() == Extension::SYNC_TYPE_APP &&
1535 extension_sync_data.notifications_disabled() !=
[email protected]b020dc12011-12-06 04:35:421536 extension_prefs_->IsAppNotificationDisabled(id)) {
1537 extension_prefs_->SetAppNotificationDisabled(
1538 id, extension_sync_data.notifications_disabled());
1539 }
[email protected]06f92562011-04-29 19:27:311540 } else {
[email protected]06f92562011-04-29 19:27:311541 // TODO(akalin): Replace silent update with a list of enabled
1542 // permissions.
[email protected]6cc7dbae2011-04-29 21:18:331543 const bool kInstallSilently = true;
[email protected]06f92562011-04-29 19:27:311544 if (!pending_extension_manager()->AddFromSync(
1545 id,
[email protected]3bdba0d2011-08-23 07:17:301546 extension_sync_data.update_url(),
1547 bundle.filter,
[email protected]6cc7dbae2011-04-29 21:18:331548 kInstallSilently)) {
[email protected]06f92562011-04-29 19:27:311549 LOG(WARNING) << "Could not add pending extension for " << id;
[email protected]3bdba0d2011-08-23 07:17:301550 // This means that the extension is already pending installation, with a
1551 // non-INTERNAL location. Add to pending_sync_data, even though it will
1552 // never be removed (we'll never install a syncable version of the
1553 // extension), so that GetAllSyncData() continues to send it.
[email protected]06f92562011-04-29 19:27:311554 }
[email protected]3bdba0d2011-08-23 07:17:301555 // Track pending extensions so that we can return them in GetAllSyncData().
1556 bundle.pending_sync_data[extension_sync_data.id()] = extension_sync_data;
[email protected]06f92562011-04-29 19:27:311557 CheckForUpdatesSoon();
[email protected]90310d92011-04-17 07:35:041558 }
[email protected]90310d92011-04-17 07:35:041559}
1560
[email protected]c3cfb012011-04-06 22:07:351561bool ExtensionService::IsIncognitoEnabled(
1562 const std::string& extension_id) const {
1563 // If this is an existing component extension we always allow it to
1564 // work in incognito mode.
[email protected]81b14cc2011-04-29 00:39:371565 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]c3cfb012011-04-06 22:07:351566 if (extension && extension->location() == Extension::COMPONENT)
[email protected]cb0ce1e022010-03-10 19:54:411567 return true;
1568
1569 // Check the prefs.
[email protected]c3cfb012011-04-06 22:07:351570 return extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]db7331a2010-02-25 22:10:501571}
[email protected]55a35692010-02-11 23:25:211572
[email protected]c3cfb012011-04-06 22:07:351573void ExtensionService::SetIsIncognitoEnabled(
1574 const std::string& extension_id, bool enabled) {
[email protected]81b14cc2011-04-29 00:39:371575 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]c3cfb012011-04-06 22:07:351576 if (extension && extension->location() == Extension::COMPONENT) {
[email protected]b873cd92012-02-09 21:51:481577 // This shouldn't be called for component extensions unless they are
1578 // syncable.
1579 DCHECK(extension->IsSyncable());
1580
1581 // If we are here, make sure the we aren't trying to change the value.
1582 DCHECK_EQ(enabled, IsIncognitoEnabled(extension_id));
1583
[email protected]c3cfb012011-04-06 22:07:351584 return;
1585 }
1586
[email protected]8c6c8c22011-03-09 12:52:341587 // Broadcast unloaded and loaded events to update browser state. Only bother
1588 // if the value changed and the extension is actually enabled, since there is
1589 // no UI otherwise.
[email protected]c3cfb012011-04-06 22:07:351590 bool old_enabled = extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]8c6c8c22011-03-09 12:52:341591 if (enabled == old_enabled)
1592 return;
1593
[email protected]c3cfb012011-04-06 22:07:351594 extension_prefs_->SetIsIncognitoEnabled(extension_id, enabled);
[email protected]81b14cc2011-04-29 00:39:371595
[email protected]ffbf3f12011-12-09 22:10:331596 bool extension_is_enabled = extensions_.Contains(extension_id);
[email protected]bd9892eb2011-11-11 19:41:341597
1598 // When we reload the extension the ID may be invalidated if we've passed it
1599 // by const ref everywhere. Make a copy to be safe.
1600 std::string id = extension_id;
[email protected]406b5a92011-11-08 11:58:261601 if (extension_is_enabled)
[email protected]ffbf3f12011-12-09 22:10:331602 ReloadExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301603
[email protected]406b5a92011-11-08 11:58:261604 // Reloading the extension invalidates the |extension| pointer.
[email protected]bd9892eb2011-11-11 19:41:341605 extension = GetInstalledExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301606 if (extension)
1607 SyncExtensionChangeIfNeeded(*extension);
[email protected]55a35692010-02-11 23:25:211608}
1609
[email protected]aa7599d2011-10-28 07:24:321610void ExtensionService::SetAppNotificationSetupDone(
1611 const std::string& extension_id,
[email protected]b2689a902011-12-01 00:41:091612 const std::string& oauth_client_id) {
[email protected]aa7599d2011-10-28 07:24:321613 const Extension* extension = GetInstalledExtension(extension_id);
1614 // This method is called when the user sets up app notifications.
1615 // So it is not expected to be called until the extension is installed.
1616 if (!extension) {
1617 NOTREACHED();
1618 return;
1619 }
[email protected]b2689a902011-12-01 00:41:091620 extension_prefs_->SetAppNotificationClientId(extension_id, oauth_client_id);
[email protected]aa7599d2011-10-28 07:24:321621 SyncExtensionChangeIfNeeded(*extension);
1622}
1623
1624void ExtensionService::SetAppNotificationDisabled(
1625 const std::string& extension_id,
1626 bool value) {
1627 const Extension* extension = GetInstalledExtension(extension_id);
1628 // This method is called when the user enables/disables app notifications.
1629 // So it is not expected to be called until the extension is installed.
1630 if (!extension) {
1631 NOTREACHED();
1632 return;
1633 }
[email protected]97e856b12011-12-14 00:21:131634 if (value)
1635 UMA_HISTOGRAM_COUNTS("Apps.SetAppNotificationsDisabled", 1);
1636 else
1637 UMA_HISTOGRAM_COUNTS("Apps.SetAppNotificationsEnabled", 1);
[email protected]aa7599d2011-10-28 07:24:321638 extension_prefs_->SetAppNotificationDisabled(extension_id, value);
1639 SyncExtensionChangeIfNeeded(*extension);
1640}
1641
[email protected]eaa7dd182010-12-14 11:09:001642bool ExtensionService::CanCrossIncognito(const Extension* extension) {
[email protected]2a8f24e2010-11-03 21:37:051643 // We allow the extension to see events and data from another profile iff it
1644 // uses "spanning" behavior and it has incognito access. "split" mode
1645 // extensions only see events for a matching profile.
[email protected]0c41298f2011-09-13 21:17:221646 CHECK(extension);
[email protected]c3cfb012011-04-06 22:07:351647 return IsIncognitoEnabled(extension->id()) &&
1648 !extension->incognito_split_mode();
[email protected]2a8f24e2010-11-03 21:37:051649}
1650
[email protected]5ef7b002011-05-12 23:16:201651bool ExtensionService::CanLoadInIncognito(const Extension* extension) const {
1652 if (extension->is_hosted_app())
1653 return true;
1654 // Packaged apps and regular extensions need to be enabled specifically for
1655 // incognito (and split mode should be set).
1656 return extension->incognito_split_mode() &&
1657 IsIncognitoEnabled(extension->id());
1658}
1659
[email protected]168389f2011-12-20 17:12:481660void ExtensionService::OnExtensionMoved(
1661 const std::string& moved_extension_id,
1662 const std::string& predecessor_extension_id,
1663 const std::string& successor_extension_id) {
[email protected]3569b502012-01-12 20:08:231664 extension_prefs_->extension_sorting()->OnExtensionMoved(
1665 moved_extension_id,
1666 predecessor_extension_id,
1667 successor_extension_id);
[email protected]168389f2011-12-20 17:12:481668
1669 const Extension* extension = GetInstalledExtension(moved_extension_id);
1670 if (extension)
1671 SyncExtensionChangeIfNeeded(*extension);
1672}
1673
[email protected]eaa7dd182010-12-14 11:09:001674bool ExtensionService::AllowFileAccess(const Extension* extension) {
[email protected]05c82182010-06-24 17:49:081675 return (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]334e04a2010-06-24 23:34:441676 switches::kDisableExtensionsFileAccessCheck) ||
[email protected]05c82182010-06-24 17:49:081677 extension_prefs_->AllowFileAccess(extension->id()));
1678}
1679
[email protected]eaa7dd182010-12-14 11:09:001680void ExtensionService::SetAllowFileAccess(const Extension* extension,
[email protected]3aff9ad2011-04-01 20:26:481681 bool allow) {
1682 // Reload to update browser state. Only bother if the value changed and the
1683 // extension is actually enabled, since there is no UI otherwise.
1684 bool old_allow = AllowFileAccess(extension);
1685 if (allow == old_allow)
1686 return;
1687
[email protected]05c82182010-06-24 17:49:081688 extension_prefs_->SetAllowFileAccess(extension->id(), allow);
[email protected]3aff9ad2011-04-01 20:26:481689
[email protected]84df8332011-12-06 18:22:461690 bool extension_is_enabled = extensions_.Contains(extension->id());
[email protected]3aff9ad2011-04-01 20:26:481691 if (extension_is_enabled)
1692 ReloadExtension(extension->id());
[email protected]05c82182010-06-24 17:49:081693}
1694
[email protected]eaa7dd182010-12-14 11:09:001695bool ExtensionService::GetBrowserActionVisibility(const Extension* extension) {
[email protected]f681c782010-11-19 11:19:391696 return extension_prefs_->GetBrowserActionVisibility(extension);
1697}
1698
[email protected]eaa7dd182010-12-14 11:09:001699void ExtensionService::SetBrowserActionVisibility(const Extension* extension,
[email protected]8e4560b62011-01-14 10:09:141700 bool visible) {
[email protected]f681c782010-11-19 11:19:391701 extension_prefs_->SetBrowserActionVisibility(extension, visible);
1702}
1703
[email protected]8e4560b62011-01-14 10:09:141704// Some extensions will autoupdate themselves externally from Chrome. These
1705// are typically part of some larger client application package. To support
1706// these, the extension will register its location in the the preferences file
1707// (and also, on Windows, in the registry) and this code will periodically
1708// check that location for a .crx file, which it will then install locally if
1709// a new version is available.
1710// Errors are reported through ExtensionErrorReporter. Succcess is not
1711// reported.
[email protected]eaa7dd182010-12-14 11:09:001712void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121713 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141714
1715 // Note that this installation is intentionally silent (since it didn't
1716 // go through the front-end). Extensions that are registered in this
1717 // way are effectively considered 'pre-bundled', and so implicitly
1718 // trusted. In general, if something has HKLM or filesystem access,
1719 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121720
1721 // If any external extension records give a URL, a provider will set
1722 // this to true. Used by OnExternalProviderReady() to see if we need
1723 // to start an update check to fetch a new external extension.
1724 external_extension_url_added_ = false;
[email protected]8e4560b62011-01-14 10:09:141725
1726 // Ask each external extension provider to give us a call back for each
1727 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
1728 ProviderCollection::const_iterator i;
1729 for (i = external_extension_providers_.begin();
1730 i != external_extension_providers_.end(); ++i) {
1731 ExternalExtensionProviderInterface* provider = i->get();
1732 provider->VisitRegisteredExtension();
1733 }
1734
[email protected]50067e52011-10-20 23:17:071735 // Do any required work that we would have done after completion of all
1736 // providers.
1737 if (external_extension_providers_.empty()) {
1738 OnAllExternalProvidersReady();
1739 }
[email protected]9f1087e2009-06-15 17:29:321740}
1741
[email protected]50067e52011-10-20 23:17:071742void ExtensionService::OnExternalProviderReady(
1743 const ExternalExtensionProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121744 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071745 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121746
1747 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141748 // if all of them are finished. So we check them first.
1749 ProviderCollection::const_iterator i;
1750 for (i = external_extension_providers_.begin();
1751 i != external_extension_providers_.end(); ++i) {
1752 ExternalExtensionProviderInterface* provider = i->get();
[email protected]a29a517a2011-01-21 21:11:121753 if (!provider->IsReady())
[email protected]8e4560b62011-01-14 10:09:141754 return;
[email protected]8e4560b62011-01-14 10:09:141755 }
1756
[email protected]50067e52011-10-20 23:17:071757 OnAllExternalProvidersReady();
1758}
1759
1760void ExtensionService::OnAllExternalProvidersReady() {
1761 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1762
1763 // Install any pending extensions.
[email protected]a29a517a2011-01-21 21:11:121764 if (external_extension_url_added_ && updater()) {
1765 external_extension_url_added_ = false;
[email protected]8e4560b62011-01-14 10:09:141766 updater()->CheckNow();
1767 }
1768
1769 // Uninstall all the unclaimed extensions.
1770 scoped_ptr<ExtensionPrefs::ExtensionsInfo> extensions_info(
1771 extension_prefs_->GetInstalledExtensionsInfo());
1772 for (size_t i = 0; i < extensions_info->size(); ++i) {
1773 ExtensionInfo* info = extensions_info->at(i).get();
1774 if (Extension::IsExternalLocation(info->extension_location))
1775 CheckExternalUninstall(info->extension_id);
1776 }
[email protected]e5af875f2011-10-10 21:09:141777 IdentifyAlertableExtensions();
[email protected]e5af875f2011-10-10 21:09:141778}
1779
1780void ExtensionService::IdentifyAlertableExtensions() {
1781 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1782
[email protected]07c9f2f42012-02-29 18:45:221783 // Build up the lists of extensions that require acknowledgment. If this is
1784 // the first time, grandfather extensions that would have caused
1785 // notification.
[email protected]a9aa5932012-01-25 08:27:401786 extension_global_error_.reset(new ExtensionGlobalError(this));
[email protected]e5af875f2011-10-10 21:09:141787
[email protected]a9aa5932012-01-25 08:27:401788 bool did_show_alert = false;
[email protected]07c9f2f42012-02-29 18:45:221789 if (PopulateExtensionGlobalError(extension_global_error_.get())) {
[email protected]e5af875f2011-10-10 21:09:141790 if (extension_prefs_->SetAlertSystemFirstRun()) {
[email protected]a9aa5932012-01-25 08:27:401791 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1792 Browser* browser = BrowserList::GetLastActiveWithProfile(profile_);
1793 if (browser) {
1794 extension_global_error_->ShowBubbleView(browser);
1795 did_show_alert = true;
1796 }
[email protected]e5af875f2011-10-10 21:09:141797 } else {
1798 // First run. Just acknowledge all the extensions, silently, by
1799 // shortcutting the display of the UI and going straight to the
1800 // callback for pressing the Accept button.
[email protected]a9aa5932012-01-25 08:27:401801 HandleExtensionAlertAccept();
[email protected]e5af875f2011-10-10 21:09:141802 }
1803 }
[email protected]a9aa5932012-01-25 08:27:401804
1805 if (!did_show_alert)
1806 extension_global_error_.reset();
[email protected]e5af875f2011-10-10 21:09:141807}
1808
[email protected]07c9f2f42012-02-29 18:45:221809bool ExtensionService::PopulateExtensionGlobalError(
1810 ExtensionGlobalError* extension_global_error) {
1811 bool needs_alert = false;
1812 for (ExtensionSet::const_iterator iter = extensions_.begin();
1813 iter != extensions_.end(); ++iter) {
1814 const Extension* e = *iter;
1815 if (Extension::IsExternalLocation(e->location())) {
1816 if (!e->is_hosted_app()) {
1817 if (!extension_prefs_->IsExternalExtensionAcknowledged(e->id())) {
1818 extension_global_error->AddExternalExtension(e->id());
1819 needs_alert = true;
1820 }
1821 }
1822 }
1823 if (extension_prefs_->IsExtensionBlacklisted(e->id())) {
1824 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(e->id())) {
1825 extension_global_error->AddBlacklistedExtension(e->id());
1826 needs_alert = true;
1827 }
1828 }
1829 if (extension_prefs_->IsExtensionOrphaned(e->id())) {
1830 if (!extension_prefs_->IsOrphanedExtensionAcknowledged(e->id())) {
1831 extension_global_error->AddOrphanedExtension(e->id());
1832 needs_alert = true;
1833 }
1834 }
1835 }
1836 return needs_alert;
1837}
1838
[email protected]a9aa5932012-01-25 08:27:401839void ExtensionService::HandleExtensionAlertClosed() {
1840 extension_global_error_.reset();
[email protected]62433d32011-10-12 22:33:121841}
1842
[email protected]a9aa5932012-01-25 08:27:401843void ExtensionService::HandleExtensionAlertAccept() {
[email protected]e5af875f2011-10-10 21:09:141844 const ExtensionIdSet *extension_ids =
[email protected]a9aa5932012-01-25 08:27:401845 extension_global_error_->get_external_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141846 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1847 iter != extension_ids->end(); ++iter) {
[email protected]47fc70c2011-12-06 07:29:511848 AcknowledgeExternalExtension(*iter);
[email protected]e5af875f2011-10-10 21:09:141849 }
[email protected]a9aa5932012-01-25 08:27:401850 extension_ids = extension_global_error_->get_blacklisted_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141851 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1852 iter != extension_ids->end(); ++iter) {
1853 extension_prefs_->AcknowledgeBlacklistedExtension(*iter);
1854 }
[email protected]a9aa5932012-01-25 08:27:401855 extension_ids = extension_global_error_->get_orphaned_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141856 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1857 iter != extension_ids->end(); ++iter) {
1858 extension_prefs_->AcknowledgeOrphanedExtension(*iter);
1859 }
1860}
1861
[email protected]47fc70c2011-12-06 07:29:511862void ExtensionService::AcknowledgeExternalExtension(const std::string& id) {
1863 extension_prefs_->AcknowledgeExternalExtension(id);
1864}
1865
[email protected]a9aa5932012-01-25 08:27:401866void ExtensionService::HandleExtensionAlertDetails(Browser* browser) {
1867 DCHECK(browser);
1868 browser->ShowExtensionsTab();
[email protected]6c751e72010-11-23 10:11:101869}
1870
[email protected]a9f39a312010-12-23 22:14:271871void ExtensionService::UnloadExtension(
1872 const std::string& extension_id,
[email protected]814a7bf0f2011-08-13 05:30:591873 extension_misc::UnloadedExtensionReason reason) {
[email protected]27e469a2010-01-11 20:35:091874 // Make sure the extension gets deleted after we return from this function.
[email protected]9adb9692010-10-29 23:14:021875 scoped_refptr<const Extension> extension(
[email protected]8001df22011-04-28 19:59:471876 GetExtensionByIdInternal(extension_id, true, true, false));
[email protected]631cf822009-05-15 07:01:251877
[email protected]fa0c96732010-11-17 00:14:231878 // This method can be called via PostTask, so the extension may have been
1879 // unloaded by the time this runs.
[email protected]dd163fb02011-05-04 22:22:171880 if (!extension) {
1881 // In case the extension may have crashed/uninstalled. Allow the profile to
1882 // clean up its RequestContexts.
[email protected]e15be2b2012-03-09 08:42:111883 profile_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231884 return;
[email protected]dd163fb02011-05-04 22:22:171885 }
[email protected]0c6da502009-08-14 22:32:391886
[email protected]1eb175082010-02-10 09:26:161887 // Keep information about the extension so that we can reload it later
1888 // even if it's not permanently installed.
1889 unloaded_extension_paths_[extension->id()] = extension->path();
1890
[email protected]f17dbd42010-08-16 23:21:101891 // Clean up if the extension is meant to be enabled after a reload.
1892 disabled_extension_paths_.erase(extension->id());
1893
[email protected]d7e9a862010-11-03 21:57:491894 // Clean up runtime data.
1895 extension_runtime_data_.erase(extension_id);
1896
[email protected]84df8332011-12-06 18:22:461897if (disabled_extensions_.Contains(extension->id())) {
[email protected]a9f39a312010-12-23 22:14:271898 UnloadedExtensionInfo details(extension, reason);
1899 details.already_disabled = true;
[email protected]84df8332011-12-06 18:22:461900 disabled_extensions_.Remove(extension->id());
[email protected]ad50def52011-10-19 23:17:071901 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271902 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531903 content::Source<Profile>(profile_),
1904 content::Details<UnloadedExtensionInfo>(&details));
[email protected]dd163fb02011-05-04 22:22:171905 // Make sure the profile cleans up its RequestContexts when an already
1906 // disabled extension is unloaded (since they are also tracking the disabled
1907 // extensions).
[email protected]e15be2b2012-03-09 08:42:111908 profile_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]0c6da502009-08-14 22:32:391909 return;
1910 }
1911
[email protected]84df8332011-12-06 18:22:461912// Remove the extension from our list.
1913 extensions_.Remove(extension->id());
[email protected]631cf822009-05-15 07:01:251914
[email protected]a9f39a312010-12-23 22:14:271915 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]631cf822009-05-15 07:01:251916}
1917
[email protected]eaa7dd182010-12-14 11:09:001918void ExtensionService::UnloadAllExtensions() {
[email protected]c8d407e2011-04-28 21:27:171919 profile_->GetExtensionSpecialStoragePolicy()->
1920 RevokeRightsForAllExtensions();
1921
[email protected]84df8332011-12-06 18:22:461922 extensions_.Clear();
1923 disabled_extensions_.Clear();
1924 terminated_extensions_.Clear();
[email protected]d7e9a862010-11-03 21:57:491925 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:321926
[email protected]c6e4a3412009-06-24 15:45:291927 // TODO(erikkay) should there be a notification for this? We can't use
1928 // EXTENSION_UNLOADED since that implies that the extension has been disabled
1929 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:321930}
1931
[email protected]eaa7dd182010-12-14 11:09:001932void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:321933 UnloadAllExtensions();
[email protected]d8c8f25f2011-11-02 18:18:011934 component_loader_->LoadAll();
1935 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]9f1087e2009-06-15 17:29:321936}
1937
[email protected]eaa7dd182010-12-14 11:09:001938void ExtensionService::GarbageCollectExtensions() {
[email protected]39d9f62c2010-12-03 10:48:501939 if (extension_prefs_->pref_service()->ReadOnly())
[email protected]ba399672010-04-06 15:42:391940 return;
1941
[email protected]ca3dbf52010-05-19 22:27:061942 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(
1943 extension_prefs_->GetInstalledExtensionsInfo());
1944
1945 std::map<std::string, FilePath> extension_paths;
1946 for (size_t i = 0; i < info->size(); ++i)
1947 extension_paths[info->at(i)->extension_id] = info->at(i)->extension_path;
1948
[email protected]14908b72011-04-20 06:54:361949 if (!BrowserThread::PostTask(
1950 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:361951 base::Bind(
[email protected]14908b72011-04-20 06:54:361952 &extension_file_util::GarbageCollectExtensions,
1953 install_directory_,
1954 extension_paths)))
1955 NOTREACHED();
[email protected]f6ccd652010-11-17 00:48:341956
1957 // Also garbage-collect themes. We check |profile_| to be
1958 // defensive; in the future, we may call GarbageCollectExtensions()
1959 // from somewhere other than Init() (e.g., in a timer).
1960 if (profile_) {
[email protected]18280372011-03-22 18:05:221961 ThemeServiceFactory::GetForProfile(profile_)->RemoveUnusedThemes();
[email protected]f6ccd652010-11-17 00:48:341962 }
[email protected]3cf4f0992009-02-03 23:00:301963}
1964
[email protected]fb82dcd2012-03-21 14:15:461965void ExtensionService::SyncExtensionChangeIfNeeded(const Extension& extension) {
1966 SyncBundle* sync_bundle = GetSyncBundleForExtension(extension);
1967 if (sync_bundle) {
1968 ExtensionSyncData extension_sync_data(
1969 extension,
1970 IsExtensionEnabled(extension.id()),
1971 IsIncognitoEnabled(extension.id()),
1972 extension_prefs_->GetAppNotificationClientId(extension.id()),
1973 extension_prefs_->IsAppNotificationDisabled(extension.id()),
1974 extension_prefs_->extension_sorting()->
1975 GetAppLaunchOrdinal(extension.id()),
1976 extension_prefs_->extension_sorting()->GetPageOrdinal(extension.id()));
1977
1978 SyncChangeList sync_change_list(1, extension_sync_data.GetSyncChange(
1979 sync_bundle->HasExtensionId(extension.id()) ?
1980 SyncChange::ACTION_UPDATE : SyncChange::ACTION_ADD));
1981 sync_bundle->sync_processor->ProcessSyncChanges(
1982 FROM_HERE, sync_change_list);
1983 sync_bundle->synced_extensions.insert(extension.id());
1984 sync_bundle->pending_sync_data.erase(extension.id());
1985 }
1986}
1987
[email protected]eaa7dd182010-12-14 11:09:001988void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]93fd78f42009-07-10 16:43:171989 if (updater_.get()) {
1990 updater_->Start();
1991 }
[email protected]fa6a9102010-11-22 15:38:501992
1993 ready_ = true;
[email protected]ad50def52011-10-19 23:17:071994 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271995 chrome::NOTIFICATION_EXTENSIONS_READY,
[email protected]6c2381d2011-10-19 02:52:531996 content::Source<Profile>(profile_),
[email protected]ad50def52011-10-19 23:17:071997 content::NotificationService::NoDetails());
[email protected]e72e8eb82009-06-18 17:21:511998}
1999
[email protected]61f5fc82012-02-15 20:10:452000bool ExtensionService::AddExtension(const Extension* extension) {
[email protected]ae09ca62009-08-21 19:46:462001 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:022002 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]9f1087e2009-06-15 17:29:322003
[email protected]c8d407e2011-04-28 21:27:172004 // TODO(jstritar): We may be able to get rid of this branch by overriding the
2005 // default extension state to DISABLED when the --disable-extensions flag
2006 // is set (http://crbug.com/29067).
2007 if (!extensions_enabled() &&
2008 !extension->is_theme() &&
2009 extension->location() != Extension::COMPONENT &&
2010 !Extension::IsExternalLocation(extension->location()))
[email protected]61f5fc82012-02-15 20:10:452011 return false;
[email protected]c8d407e2011-04-28 21:27:172012
2013 SetBeingUpgraded(extension, false);
2014
[email protected]1eb175082010-02-10 09:26:162015 // The extension is now loaded, remove its data from unloaded extension map.
2016 unloaded_extension_paths_.erase(extension->id());
2017
[email protected]bb7f40952011-01-13 00:21:202018 // If a terminated extension is loaded, remove it from the terminated list.
2019 UntrackTerminatedExtension(extension->id());
2020
[email protected]f17dbd42010-08-16 23:21:102021 // If the extension was disabled for a reload, then enable it.
2022 if (disabled_extension_paths_.erase(extension->id()) > 0)
2023 EnableExtension(extension->id());
2024
[email protected]d728e002010-12-08 04:46:232025 // Check if the extension's privileges have changed and disable the
2026 // extension if necessary.
[email protected]902fd7b2011-07-27 18:42:312027 InitializePermissions(extension);
[email protected]0c6da502009-08-14 22:32:392028
[email protected]ad83ca242011-07-29 01:32:252029 bool disabled = extension_prefs_->IsExtensionDisabled(extension->id());
[email protected]36429da2011-07-11 20:25:182030 if (disabled) {
[email protected]84df8332011-12-06 18:22:462031 disabled_extensions_.Insert(scoped_extension);
[email protected]3bdba0d2011-08-23 07:17:302032 SyncExtensionChangeIfNeeded(*extension);
[email protected]62f051c2012-03-29 17:04:442033
2034 if (extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
2035 content::NotificationService::current()->Notify(
2036 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
2037 content::Source<Profile>(profile_),
2038 content::Details<const Extension>(extension));
2039 extensions::AddExtensionDisabledError(this, extension);
2040 }
[email protected]61f5fc82012-02-15 20:10:452041 // Although the extension is disabled, we technically did succeed in adding
2042 // it to the list of installed extensions.
2043 return true;
[email protected]e72e8eb82009-06-18 17:21:512044 }
[email protected]aab98a52009-12-02 03:22:352045
[email protected]b873cd92012-02-09 21:51:482046 // All apps that are displayed in the launcher are ordered by their ordinals
2047 // so we must ensure they have valid ordinals.
2048 if (extension->ShouldDisplayInLauncher())
2049 extension_prefs_->extension_sorting()->EnsureValidOrdinals(extension->id());
2050
[email protected]84df8332011-12-06 18:22:462051 extensions_.Insert(scoped_extension);
[email protected]3bdba0d2011-08-23 07:17:302052 SyncExtensionChangeIfNeeded(*extension);
[email protected]c8d407e2011-04-28 21:27:172053 NotifyExtensionLoaded(extension);
[email protected]61f5fc82012-02-15 20:10:452054
2055 return true;
[email protected]aab98a52009-12-02 03:22:352056}
2057
[email protected]902fd7b2011-07-27 18:42:312058void ExtensionService::InitializePermissions(const Extension* extension) {
2059 // If the extension has used the optional permissions API, it will have a
2060 // custom set of active permissions defined in the extension prefs. Here,
2061 // we update the extension's active permissions based on the prefs.
2062 scoped_refptr<ExtensionPermissionSet> active_permissions =
2063 extension_prefs()->GetActivePermissions(extension->id());
2064
2065 if (active_permissions.get()) {
2066 // We restrict the active permissions to be within the bounds defined in the
2067 // extension's manifest.
2068 // a) active permissions must be a subset of optional + default permissions
2069 // b) active permissions must contains all default permissions
2070 scoped_refptr<ExtensionPermissionSet> total_permissions =
2071 ExtensionPermissionSet::CreateUnion(
2072 extension->required_permission_set(),
2073 extension->optional_permission_set());
2074
2075 // Make sure the active permissions contain no more than optional + default.
2076 scoped_refptr<ExtensionPermissionSet> adjusted_active =
2077 ExtensionPermissionSet::CreateIntersection(
2078 total_permissions.get(), active_permissions.get());
2079
2080 // Make sure the active permissions contain the default permissions.
2081 adjusted_active = ExtensionPermissionSet::CreateUnion(
2082 extension->required_permission_set(), adjusted_active.get());
2083
[email protected]c333e792012-01-06 16:57:392084 extensions::PermissionsUpdater perms_updater(profile());
2085 perms_updater.UpdateActivePermissions(extension, adjusted_active);
[email protected]902fd7b2011-07-27 18:42:312086 }
2087
[email protected]8d888c12010-11-30 00:00:252088 // We keep track of all permissions the user has granted each extension.
2089 // This allows extensions to gracefully support backwards compatibility
2090 // by including unknown permissions in their manifests. When the user
2091 // installs the extension, only the recognized permissions are recorded.
2092 // When the unknown permissions become recognized (e.g., through browser
2093 // upgrade), we can prompt the user to accept these new permissions.
2094 // Extensions can also silently upgrade to less permissions, and then
2095 // silently upgrade to a version that adds these permissions back.
2096 //
2097 // For example, pretend that Chrome 10 includes a permission "omnibox"
2098 // for an API that adds suggestions to the omnibox. An extension can
2099 // maintain backwards compatibility while still having "omnibox" in the
2100 // manifest. If a user installs the extension on Chrome 9, the browser
2101 // will record the permissions it recognized, not including "omnibox."
2102 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
2103 // will disable the extension and prompt the user to approve the increase
2104 // in privileges. The extension could then release a new version that
2105 // removes the "omnibox" permission. When the user upgrades, Chrome will
2106 // still remember that "omnibox" had been granted, so that if the
2107 // extension once again includes "omnibox" in an upgrade, the extension
2108 // can upgrade without requiring this user's approval.
2109 const Extension* old = GetExtensionByIdInternal(extension->id(),
[email protected]8001df22011-04-28 19:59:472110 true, true, false);
[email protected]8d888c12010-11-30 00:00:252111 bool is_extension_upgrade = old != NULL;
2112 bool is_privilege_increase = false;
2113
[email protected]902fd7b2011-07-27 18:42:312114 // We only need to compare the granted permissions to the current permissions
2115 // if the extension is not allowed to silently increase its permissions.
2116 if (!extension->CanSilentlyIncreasePermissions()) {
[email protected]8d888c12010-11-30 00:00:252117 // Add all the recognized permissions if the granted permissions list
2118 // hasn't been initialized yet.
[email protected]902fd7b2011-07-27 18:42:312119 scoped_refptr<ExtensionPermissionSet> granted_permissions =
2120 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:522121 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:252122
2123 // Here, we check if an extension's privileges have increased in a manner
2124 // that requires the user's approval. This could occur because the browser
2125 // upgraded and recognized additional privileges, or an extension upgrades
2126 // to a version that requires additional privileges.
[email protected]0d3e4a22011-06-23 19:02:522127 is_privilege_increase =
[email protected]902fd7b2011-07-27 18:42:312128 granted_permissions->HasLessPrivilegesThan(
2129 extension->GetActivePermissions());
[email protected]8d888c12010-11-30 00:00:252130 }
2131
2132 if (is_extension_upgrade) {
[email protected]66e26872010-12-03 20:07:252133 // Other than for unpacked extensions, CrxInstaller should have guaranteed
2134 // that we aren't downgrading.
2135 if (extension->location() != Extension::LOAD)
2136 CHECK(extension->version()->CompareTo(*(old->version())) >= 0);
[email protected]8d888c12010-11-30 00:00:252137
2138 // Extensions get upgraded if the privileges are allowed to increase or
2139 // the privileges haven't increased.
2140 if (!is_privilege_increase) {
2141 SetBeingUpgraded(old, true);
2142 SetBeingUpgraded(extension, true);
2143 }
2144
2145 // To upgrade an extension in place, unload the old one and
2146 // then load the new one.
[email protected]814a7bf0f2011-08-13 05:30:592147 UnloadExtension(old->id(), extension_misc::UNLOAD_REASON_UPDATE);
[email protected]8d888c12010-11-30 00:00:252148 old = NULL;
2149 }
2150
2151 // Extension has changed permissions significantly. Disable it. A
2152 // notification should be sent by the caller.
2153 if (is_privilege_increase) {
[email protected]fe2dd7742011-04-19 22:52:492154 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
2155 RecordPermissionMessagesHistogram(
2156 extension, "Extensions.Permissions_AutoDisable");
2157 }
[email protected]b3317ad2011-04-28 23:46:002158 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:252159 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
2160 }
2161}
2162
[email protected]eaa7dd182010-12-14 11:09:002163void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:392164 std::set<std::string> extension_ids;
[email protected]84df8332011-12-06 18:22:462165 for (ExtensionSet::const_iterator iter = extensions_.begin();
2166 iter != extensions_.end(); ++iter) {
2167 const Extension* extension = *iter;
2168 if (!extension->is_theme() && extension->location() != Extension::COMPONENT)
2169 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:352170 }
2171
2172 child_process_logging::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:252173}
2174
[email protected]8266d662011-07-12 21:53:262175void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:502176 const Extension* extension,
2177 bool from_webstore,
2178 const StringOrdinal& page_ordinal) {
[email protected]a29a517a2011-01-21 21:11:122179 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:202180
[email protected]4416c5a2010-06-26 01:28:572181 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:022182 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]c3cfb012011-04-06 22:07:352183 const std::string& id = extension->id();
[email protected]8a87a5332011-08-11 17:54:592184 // Extensions installed by policy can't be disabled. So even if a previous
2185 // installation disabled the extension, make sure it is now enabled.
2186 bool initial_enable =
2187 !extension_prefs_->IsExtensionDisabled(id) ||
2188 !Extension::UserMayDisable(extension->location());
[email protected]b2907fd2011-03-25 16:43:372189 PendingExtensionInfo pending_extension_info;
[email protected]c3cfb012011-04-06 22:07:352190 if (pending_extension_manager()->GetById(id, &pending_extension_info)) {
2191 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:322192
[email protected]483d1ff2011-03-03 17:12:022193 if (!pending_extension_info.ShouldAllowInstall(*extension)) {
[email protected]4416c5a2010-06-26 01:28:572194 LOG(WARNING)
[email protected]ae6cb3022011-04-04 20:11:542195 << "ShouldAllowInstall() returned false for "
[email protected]c3cfb012011-04-06 22:07:352196 << id << " of type " << extension->GetType()
[email protected]7fa19f82010-12-21 19:40:082197 << " and update URL " << extension->update_url().spec()
2198 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:542199
[email protected]ad50def52011-10-19 23:17:072200 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272201 chrome::NOTIFICATION_EXTENSION_INSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:532202 content::Source<Profile>(profile_),
2203 content::Details<const Extension>(extension));
[email protected]ae6cb3022011-04-04 20:11:542204
[email protected]4416c5a2010-06-26 01:28:572205 // Delete the extension directory since we're not going to
2206 // load it.
[email protected]14908b72011-04-20 06:54:362207 if (!BrowserThread::PostTask(
2208 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:362209 base::Bind(&extension_file_util::DeleteFile,
2210 extension->path(), true)))
[email protected]14908b72011-04-20 06:54:362211 NOTREACHED();
[email protected]4416c5a2010-06-26 01:28:572212 return;
2213 }
[email protected]4416c5a2010-06-26 01:28:572214 } else {
[email protected]c3cfb012011-04-06 22:07:352215 // We explicitly want to re-enable an uninstalled external
2216 // extension; if we're here, that means the user is manually
2217 // installing the extension.
[email protected]6cc7dbae2011-04-29 21:18:332218 if (IsExternalExtensionUninstalled(id)) {
2219 initial_enable = true;
2220 }
[email protected]aa142702010-03-26 01:26:332221 }
2222
[email protected]07533022011-06-27 20:42:552223 // Do not record the install histograms for upgrades.
2224 if (!GetExtensionByIdInternal(extension->id(), true, true, false)) {
2225 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
2226 extension->GetType(), 100);
2227 RecordPermissionMessagesHistogram(
2228 extension, "Extensions.Permissions_Install");
2229 }
2230
[email protected]a39921b42012-02-28 03:42:542231 // Certain extension locations are specific enough that we can
2232 // auto-acknowledge any extension that came from one of them.
2233 if (extension->location() == Extension::EXTERNAL_POLICY_DOWNLOAD) {
2234 AcknowledgeExternalExtension(extension->id());
2235 }
2236
[email protected]4416c5a2010-06-26 01:28:572237 extension_prefs_->OnExtensionInstalled(
[email protected]8266d662011-07-12 21:53:262238 extension,
2239 initial_enable ? Extension::ENABLED : Extension::DISABLED,
[email protected]cc829cf2011-08-22 21:01:352240 from_webstore,
[email protected]36a5c4c2011-12-14 16:34:502241 page_ordinal);
[email protected]25b34332009-06-05 21:53:192242
[email protected]cdfca9702011-08-08 16:07:012243 // Unpacked extensions default to allowing file access, but if that has been
2244 // overridden, don't reset the value.
[email protected]b6e64fd2011-08-09 19:49:192245 if (Extension::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]cdfca9702011-08-08 16:07:012246 !extension_prefs_->HasAllowFileAccessSetting(id)) {
2247 extension_prefs_->SetAllowFileAccess(id, true);
2248 }
2249
[email protected]49cd1572011-02-08 21:38:452250 // Transfer ownership of |extension| to AddExtension.
[email protected]61f5fc82012-02-15 20:10:452251 if (AddExtension(scoped_extension)) {
2252 content::NotificationService::current()->Notify(
2253 chrome::NOTIFICATION_EXTENSION_INSTALLED,
2254 content::Source<Profile>(profile_),
2255 content::Details<const Extension>(extension));
2256 }
[email protected]4a190632009-05-09 01:07:422257}
2258
[email protected]eaa7dd182010-12-14 11:09:002259const Extension* ExtensionService::GetExtensionByIdInternal(
[email protected]8001df22011-04-28 19:59:472260 const std::string& id, bool include_enabled, bool include_disabled,
2261 bool include_terminated) const {
[email protected]e957fe52009-06-23 16:51:052262 std::string lowercase_id = StringToLowerASCII(id);
[email protected]0c6da502009-08-14 22:32:392263 if (include_enabled) {
[email protected]84df8332011-12-06 18:22:462264 const Extension* extension = extensions_.GetByID(lowercase_id);
2265 if (extension)
2266 return extension;
[email protected]0c6da502009-08-14 22:32:392267 }
2268 if (include_disabled) {
[email protected]84df8332011-12-06 18:22:462269 const Extension* extension = disabled_extensions_.GetByID(lowercase_id);
2270 if (extension)
2271 return extension;
[email protected]ce5c4502009-05-06 16:46:112272 }
[email protected]8001df22011-04-28 19:59:472273 if (include_terminated) {
[email protected]84df8332011-12-06 18:22:462274 const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
2275 if (extension)
2276 return extension;
[email protected]8001df22011-04-28 19:59:472277 }
[email protected]ce5c4502009-05-06 16:46:112278 return NULL;
2279}
2280
[email protected]bb7f40952011-01-13 00:21:202281void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
[email protected]84df8332011-12-06 18:22:462282 if (!terminated_extensions_.Contains(extension->id()))
2283 terminated_extensions_.Insert(make_scoped_refptr(extension));
[email protected]fa2416f2011-05-03 08:41:202284
[email protected]b3f7fe22011-11-11 19:27:562285 UnloadExtension(extension->id(), extension_misc::UNLOAD_REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:202286}
2287
2288void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]fa2416f2011-05-03 08:41:202289 std::string lowercase_id = StringToLowerASCII(id);
[email protected]84df8332011-12-06 18:22:462290 terminated_extensions_.Remove(lowercase_id);
[email protected]bb7f40952011-01-13 00:21:202291}
2292
[email protected]0dfe05c2011-02-23 23:03:362293const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:472294 const std::string& id) const {
2295 return GetExtensionByIdInternal(id, false, false, true);
2296}
2297
2298const Extension* ExtensionService::GetInstalledExtension(
2299 const std::string& id) const {
2300 return GetExtensionByIdInternal(id, true, true, true);
[email protected]0dfe05c2011-02-23 23:03:362301}
2302
[email protected]eaa7dd182010-12-14 11:09:002303const Extension* ExtensionService::GetWebStoreApp() {
[email protected]d3071992010-10-08 15:24:072304 return GetExtensionById(extension_misc::kWebStoreAppId, false);
2305}
2306
[email protected]eaa7dd182010-12-14 11:09:002307bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:442308 // Allow bindings for all packaged extensions and component hosted apps.
2309 const Extension* extension = extensions_.GetExtensionOrAppByURL(
2310 ExtensionURLInfo(url));
2311 return extension && (!extension->is_hosted_app() ||
2312 extension->location() == Extension::COMPONENT);
[email protected]6d2e60bd2010-06-03 22:37:392313}
2314
[email protected]eaa7dd182010-12-14 11:09:002315const SkBitmap& ExtensionService::GetOmniboxIcon(
[email protected]b671760b2010-07-15 21:13:472316 const std::string& extension_id) {
2317 return omnibox_icon_manager_.GetIcon(extension_id);
2318}
2319
[email protected]eaa7dd182010-12-14 11:09:002320const SkBitmap& ExtensionService::GetOmniboxPopupIcon(
[email protected]29d0d4ac2010-09-08 21:10:312321 const std::string& extension_id) {
2322 return omnibox_popup_icon_manager_.GetIcon(extension_id);
2323}
2324
[email protected]9060d8b02012-01-13 02:14:302325bool ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322326 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:142327 const Version* version,
[email protected]8ef78fd2010-08-19 17:14:322328 const FilePath& path,
[email protected]1bf73cc32011-10-26 22:38:312329 Extension::Location location,
[email protected]47fc70c2011-12-06 07:29:512330 int creation_flags,
2331 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:382332 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2333 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012334 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]9060d8b02012-01-13 02:14:302335 return false;
[email protected]a8af9fdb2010-10-28 21:52:202336
[email protected]8e4560b62011-01-14 10:09:142337 DCHECK(version);
2338
[email protected]7577a5c52009-07-30 06:21:582339 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492340 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582341 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022342 const Extension* existing = GetExtensionById(id, true);
[email protected]7577a5c52009-07-30 06:21:582343 if (existing) {
[email protected]8e4560b62011-01-14 10:09:142344 switch (existing->version()->CompareTo(*version)) {
[email protected]7577a5c52009-07-30 06:21:582345 case -1: // existing version is older, we should upgrade
2346 break;
2347 case 0: // existing version is same, do nothing
[email protected]9060d8b02012-01-13 02:14:302348 return false;
[email protected]7577a5c52009-07-30 06:21:582349 case 1: // existing version is newer, uh-oh
2350 LOG(WARNING) << "Found external version of extension " << id
2351 << "that is older than current version. Current version "
2352 << "is: " << existing->VersionString() << ". New version "
[email protected]16d900042012-01-04 21:24:322353 << "is: " << version->GetString()
2354 << ". Keeping current version.";
[email protected]9060d8b02012-01-13 02:14:302355 return false;
[email protected]7577a5c52009-07-30 06:21:582356 }
2357 }
2358
[email protected]9060d8b02012-01-13 02:14:302359 // If the extension is already pending, don't start an install.
2360 if (!pending_extension_manager()->AddFromExternalFile(id, location))
2361 return false;
[email protected]9c635f22010-12-02 09:36:362362
[email protected]14908b72011-04-20 06:54:362363 // no client (silent install)
[email protected]d8c8f25f2011-11-02 18:18:012364 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(this, NULL));
[email protected]6dfbbf82010-03-12 23:09:162365 installer->set_install_source(location);
2366 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072367 installer->set_expected_version(*version);
2368 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]1bf73cc32011-10-26 22:38:312369 installer->set_creation_flags(creation_flags);
[email protected]6dfbbf82010-03-12 23:09:162370 installer->InstallCrx(path);
[email protected]47fc70c2011-12-06 07:29:512371
2372 // Depending on the source, a new external extension might not need a user
2373 // notification on installation. For such extensions, mark them acknowledged
2374 // now to suppress the notification.
2375 if (mark_acknowledged)
2376 AcknowledgeExternalExtension(id);
[email protected]9060d8b02012-01-13 02:14:302377
2378 return true;
[email protected]7577a5c52009-07-30 06:21:582379}
2380
[email protected]eaa7dd182010-12-14 11:09:002381void ExtensionService::ReportExtensionLoadError(
[email protected]d11c8e92009-10-20 23:26:402382 const FilePath& extension_path,
2383 const std::string &error,
[email protected]d11c8e92009-10-20 23:26:402384 bool be_noisy) {
[email protected]d8c8f25f2011-11-02 18:18:012385 content::NotificationService::current()->Notify(
2386 chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
2387 content::Source<Profile>(profile_),
2388 content::Details<const std::string>(&error));
[email protected]d11c8e92009-10-20 23:26:402389
[email protected]8a205c02011-02-04 20:41:332390 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]fc670822011-12-17 09:33:492391 string16 message = ASCIIToUTF16(base::StringPrintf(
[email protected]18d4b6c2010-09-21 03:21:042392 "Could not load extension from '%s'. %s",
[email protected]fc670822011-12-17 09:33:492393 path_str.c_str(), error.c_str()));
[email protected]d11c8e92009-10-20 23:26:402394 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2395}
2396
[email protected]eaa7dd182010-12-14 11:09:002397void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]406027c02010-09-27 08:03:182398 ExtensionHost* host) {
2399 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332400 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182401 if (iter == orphaned_dev_tools_.end())
2402 return;
2403
[email protected]0e12d7d2011-12-01 16:21:442404 DevToolsAgentHost* agent = DevToolsAgentHostRegistry::GetDevToolsAgentHost(
2405 host->render_view_host());
2406 content::DevToolsManager::GetInstance()->AttachClientHost(iter->second,
2407 agent);
[email protected]406027c02010-09-27 08:03:182408 orphaned_dev_tools_.erase(iter);
2409}
2410
[email protected]432115822011-07-10 15:52:272411void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532412 const content::NotificationSource& source,
2413 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272414 switch (type) {
2415 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532416 if (profile_ !=
2417 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322418 break;
[email protected]6c2381d2011-10-19 02:52:532419 }
[email protected]a4ed6282009-12-14 20:51:162420
[email protected]6c2381d2011-10-19 02:52:532421 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262422
[email protected]fa2416f2011-05-03 08:41:202423 // Mark the extension as terminated and Unload it. We want it to
2424 // be in a consistent state: either fully working or not loaded
2425 // at all, but never half-crashed. We do it in a PostTask so
2426 // that other handlers of this notification will still have
2427 // access to the Extension and ExtensionHost.
[email protected]14908b72011-04-20 06:54:362428 MessageLoop::current()->PostTask(
2429 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362430 base::Bind(
[email protected]fa2416f2011-05-03 08:41:202431 &ExtensionService::TrackTerminatedExtension,
[email protected]d8c8f25f2011-11-02 18:18:012432 AsWeakPtr(),
[email protected]fa2416f2011-05-03 08:41:202433 host->extension()));
[email protected]31f77262009-12-02 20:48:532434 break;
2435 }
[email protected]432115822011-07-10 15:52:272436 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
[email protected]f3b1a082011-11-18 00:34:302437 content::RenderProcessHost* process =
2438 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192439 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302440 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192441 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2442 break;
2443
[email protected]77a6970c2011-04-23 16:58:562444 // Valid extension function names, used to setup bindings in renderer.
2445 std::vector<std::string> function_names;
2446 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names);
2447 process->Send(new ExtensionMsg_SetFunctionNames(function_names));
[email protected]31f77262009-12-02 20:48:532448
[email protected]77a6970c2011-04-23 16:58:562449 // Scripting whitelist. This is modified by tests and must be communicated
2450 // to renderers.
2451 process->Send(new ExtensionMsg_SetScriptingWhitelist(
2452 *Extension::GetScriptingWhitelist()));
2453
2454 // Loaded extensions.
[email protected]9776e82e2011-11-15 02:17:532455 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions;
[email protected]84df8332011-12-06 18:22:462456 for (ExtensionSet::const_iterator iter = extensions_.begin();
2457 iter != extensions_.end(); ++iter) {
[email protected]19647262011-12-16 09:57:492458 // Renderers don't need to know about themes.
2459 if (!(*iter)->is_theme())
2460 loaded_extensions.push_back(ExtensionMsg_Loaded_Params(*iter));
[email protected]77a6970c2011-04-23 16:58:562461 }
[email protected]9776e82e2011-11-15 02:17:532462 process->Send(new ExtensionMsg_Loaded(loaded_extensions));
[email protected]77a6970c2011-04-23 16:58:562463 break;
2464 }
[email protected]432115822011-07-10 15:52:272465 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302466 content::RenderProcessHost* process =
2467 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192468 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302469 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192470 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2471 break;
2472
[email protected]f3b1a082011-11-18 00:34:302473 installed_app_hosts_.erase(process->GetID());
[email protected]6f371442011-11-09 06:45:462474
[email protected]6bc04fd82011-12-04 02:29:352475 process_map_.RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:462476 BrowserThread::PostTask(
2477 BrowserThread::IO, FROM_HERE,
2478 base::Bind(&ExtensionInfoMap::UnregisterAllExtensionsInProcess,
[email protected]e15be2b2012-03-09 08:42:112479 profile_->GetExtensionInfoMap(),
[email protected]f3b1a082011-11-18 00:34:302480 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212481 break;
2482 }
[email protected]432115822011-07-10 15:52:272483 case chrome::NOTIFICATION_PREF_CHANGED: {
[email protected]6c2381d2011-10-19 02:52:532484 std::string* pref_name = content::Details<std::string>(details).ptr();
[email protected]6c751e72010-11-23 10:11:102485 if (*pref_name == prefs::kExtensionInstallAllowList ||
2486 *pref_name == prefs::kExtensionInstallDenyList) {
2487 CheckAdminBlacklist();
[email protected]6c751e72010-11-23 10:11:102488 } else {
2489 NOTREACHED() << "Unexpected preference name.";
2490 }
[email protected]aa96d3a2010-08-21 08:45:252491 break;
2492 }
[email protected]d91268022011-08-26 13:17:372493 case chrome::NOTIFICATION_IMPORT_FINISHED: {
[email protected]cf593af2011-12-30 05:44:392494 InitAfterImport();
[email protected]d91268022011-08-26 13:17:372495 break;
2496 }
[email protected]aa96d3a2010-08-21 08:45:252497
[email protected]4814b512009-11-07 00:12:292498 default:
2499 NOTREACHED() << "Unexpected notification type.";
2500 }
2501}
2502
[email protected]eaa7dd182010-12-14 11:09:002503bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182504 return !GetAppIds().empty();
2505}
[email protected]377011d2010-07-20 04:18:502506
[email protected]eaa7dd182010-12-14 11:09:002507ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182508 ExtensionIdSet result;
[email protected]84df8332011-12-06 18:22:462509 for (ExtensionSet::const_iterator it = extensions_.begin();
[email protected]377011d2010-07-20 04:18:502510 it != extensions_.end(); ++it) {
[email protected]ec5b50d2010-10-09 16:35:182511 if ((*it)->is_app() && (*it)->location() != Extension::COMPONENT)
2512 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502513 }
2514
[email protected]ec5b50d2010-10-09 16:35:182515 return result;
[email protected]377011d2010-07-20 04:18:502516}
[email protected]d7e9a862010-11-03 21:57:492517
[email protected]eaa7dd182010-12-14 11:09:002518bool ExtensionService::IsBackgroundPageReady(const Extension* extension) {
[email protected]4762e7902012-03-28 23:34:042519 return (!extension->has_persistent_background_page() ||
[email protected]d7e9a862010-11-03 21:57:492520 extension_runtime_data_[extension->id()].background_page_ready);
2521}
2522
[email protected]eaa7dd182010-12-14 11:09:002523void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]a03d4448f2012-01-10 23:25:282524 DCHECK(extension->has_background_page());
[email protected]d7e9a862010-11-03 21:57:492525 extension_runtime_data_[extension->id()].background_page_ready = true;
[email protected]ad50def52011-10-19 23:17:072526 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272527 chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
[email protected]6c2381d2011-10-19 02:52:532528 content::Source<const Extension>(extension),
[email protected]ad50def52011-10-19 23:17:072529 content::NotificationService::NoDetails());
[email protected]d7e9a862010-11-03 21:57:492530}
2531
[email protected]eaa7dd182010-12-14 11:09:002532bool ExtensionService::IsBeingUpgraded(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492533 return extension_runtime_data_[extension->id()].being_upgraded;
2534}
2535
[email protected]eaa7dd182010-12-14 11:09:002536void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]d7e9a862010-11-03 21:57:492537 bool value) {
2538 extension_runtime_data_[extension->id()].being_upgraded = value;
2539}
[email protected]1bead0712010-11-27 17:41:532540
[email protected]39a5b532011-10-22 01:47:072541bool ExtensionService::HasUsedWebRequest(const Extension* extension) {
2542 return extension_runtime_data_[extension->id()].has_used_webrequest;
2543}
2544
2545void ExtensionService::SetHasUsedWebRequest(const Extension* extension,
2546 bool value) {
2547 extension_runtime_data_[extension->id()].has_used_webrequest = value;
2548}
2549
[email protected]45644f62011-11-23 00:58:232550base::PropertyBag* ExtensionService::GetPropertyBag(
2551 const Extension* extension) {
[email protected]1bead0712010-11-27 17:41:532552 return &extension_runtime_data_[extension->id()].property_bag;
2553}
[email protected]cebc3dc2011-04-18 17:15:002554
2555void ExtensionService::RegisterNaClModule(const GURL& url,
2556 const std::string& mime_type) {
2557 NaClModuleInfo info;
2558 info.url = url;
2559 info.mime_type = mime_type;
2560
2561 DCHECK(FindNaClModule(url) == nacl_module_list_.end());
2562 nacl_module_list_.push_front(info);
2563}
2564
2565void ExtensionService::UnregisterNaClModule(const GURL& url) {
2566 NaClModuleInfoList::iterator iter = FindNaClModule(url);
2567 DCHECK(iter != nacl_module_list_.end());
2568 nacl_module_list_.erase(iter);
2569}
2570
[email protected]ed0ba002011-05-26 16:55:132571void ExtensionService::UpdatePluginListWithNaClModules() {
[email protected]300c0ea2011-07-15 23:04:332572 // An extension has been added which has a nacl_module component, which means
2573 // there is a MIME type that module wants to handle, so we need to add that
2574 // MIME type to plugins which handle NaCl modules in order to allow the
2575 // individual modules to handle these types.
[email protected]1e127022011-11-29 21:11:472576 FilePath path;
2577 if (!PathService::Get(chrome::FILE_NACL_PLUGIN, &path))
2578 return;
2579 const content::PepperPluginInfo* pepper_info =
2580 PluginService::GetInstance()->GetRegisteredPpapiPluginInfo(path);
2581 if (!pepper_info)
2582 return;
[email protected]300c0ea2011-07-15 23:04:332583
[email protected]1e127022011-11-29 21:11:472584 std::vector<webkit::WebPluginMimeType>::const_iterator mime_iter;
2585 // Check each MIME type the plugins handle for the NaCl MIME type.
2586 for (mime_iter = pepper_info->mime_types.begin();
2587 mime_iter != pepper_info->mime_types.end(); ++mime_iter) {
2588 if (mime_iter->mime_type == kNaClPluginMimeType) {
2589 // This plugin handles "application/x-nacl".
[email protected]300c0ea2011-07-15 23:04:332590
[email protected]1e127022011-11-29 21:11:472591 PluginService::GetInstance()->
2592 UnregisterInternalPlugin(pepper_info->path);
[email protected]300c0ea2011-07-15 23:04:332593
[email protected]1e127022011-11-29 21:11:472594 webkit::WebPluginInfo info = pepper_info->ToWebPluginInfo();
[email protected]300c0ea2011-07-15 23:04:332595
[email protected]1e127022011-11-29 21:11:472596 for (ExtensionService::NaClModuleInfoList::const_iterator iter =
2597 nacl_module_list_.begin();
2598 iter != nacl_module_list_.end(); ++iter) {
2599 // Add the MIME type specified in the extension to this NaCl plugin,
2600 // With an extra "nacl" argument to specify the location of the NaCl
2601 // manifest file.
2602 webkit::WebPluginMimeType mime_type_info;
2603 mime_type_info.mime_type = iter->mime_type;
2604 mime_type_info.additional_param_names.push_back(UTF8ToUTF16("nacl"));
2605 mime_type_info.additional_param_values.push_back(
2606 UTF8ToUTF16(iter->url.spec()));
2607 info.mime_types.push_back(mime_type_info);
[email protected]300c0ea2011-07-15 23:04:332608 }
[email protected]1e127022011-11-29 21:11:472609
2610 PluginService::GetInstance()->RefreshPlugins();
[email protected]c6f3dea2012-01-14 02:23:112611 PluginService::GetInstance()->RegisterInternalPlugin(info, true);
[email protected]1e127022011-11-29 21:11:472612 // This plugin has been modified, no need to check the rest of its
2613 // types, but continue checking other plugins.
2614 break;
[email protected]ed0ba002011-05-26 16:55:132615 }
2616 }
[email protected]ed0ba002011-05-26 16:55:132617}
2618
[email protected]cebc3dc2011-04-18 17:15:002619ExtensionService::NaClModuleInfoList::iterator
2620 ExtensionService::FindNaClModule(const GURL& url) {
2621 for (NaClModuleInfoList::iterator iter = nacl_module_list_.begin();
2622 iter != nacl_module_list_.end(); ++iter) {
2623 if (iter->url == url)
2624 return iter;
2625 }
2626 return nacl_module_list_.end();
2627}
[email protected]9de83282011-12-12 11:06:372628
[email protected]58edca52012-02-16 21:51:382629extensions::APIResourceController*
2630ExtensionService::api_resource_controller() {
2631 // TODO(miket): Find a better place for this thing to live. It needs to be
2632 // scoped such that it can be created and destroyed on the IO thread.
[email protected]e1fae1c2011-12-13 22:17:172633 //
2634 // To coexist with certain unit tests that don't have an IO thread message
2635 // loop available at ExtensionService shutdown, we lazy-initialize this
[email protected]58edca52012-02-16 21:51:382636 // object so that those cases neither create nor destroy an
2637 // APIResourceController.
[email protected]e1fae1c2011-12-13 22:17:172638 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]58edca52012-02-16 21:51:382639 if (!api_resource_controller_) {
2640 api_resource_controller_ = new extensions::APIResourceController();
[email protected]e1fae1c2011-12-13 22:17:172641 }
[email protected]58edca52012-02-16 21:51:382642 return api_resource_controller_;
[email protected]e1fae1c2011-12-13 22:17:172643}
[email protected]e0a65f72012-02-10 01:45:462644
2645extensions::RulesRegistryService* ExtensionService::GetRulesRegistryService() {
2646 if (!rules_registry_service_.get()) {
2647 rules_registry_service_.reset(
2648 new extensions::RulesRegistryService(profile_));
2649 }
2650 return rules_registry_service_.get();
2651}