blob: db233aab64d7d1b7f0342e8289816c48361a0bb3 [file] [log] [blame]
[email protected]1e8c93f2010-02-08 22:58:311// Copyright (c) 2010 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
5#include "chrome/browser/extensions/extensions_service.h"
6
[email protected]24b538a2010-02-27 01:22:447#include "base/basictypes.h"
[email protected]e2eb43112009-05-29 21:19:548#include "base/command_line.h"
[email protected]6014d672008-12-05 00:38:259#include "base/file_util.h"
[email protected]cc2c3432009-11-06 17:24:3610#include "base/histogram.h"
[email protected]24b538a2010-02-27 01:22:4411#include "base/string16.h"
[email protected]6014d672008-12-05 00:38:2512#include "base/string_util.h"
[email protected]cc2c3432009-11-06 17:24:3613#include "base/time.h"
[email protected]cc655912009-01-29 23:19:1914#include "base/values.h"
[email protected]15730c42009-09-03 00:03:2015#include "chrome/browser/browser_process.h"
[email protected]dbb92e0d2009-08-20 16:18:2116#include "chrome/browser/chrome_thread.h"
[email protected]4814b512009-11-07 00:12:2917#include "chrome/browser/debugger/devtools_manager.h"
[email protected]7577a5c52009-07-30 06:21:5818#include "chrome/browser/extensions/crx_installer.h"
[email protected]5cbe1e22010-01-30 01:18:5619#include "chrome/browser/extensions/extension_accessibility_api.h"
[email protected]840b0db2009-11-20 03:00:3820#include "chrome/browser/extensions/extension_bookmarks_module.h"
[email protected]b68d5ed2009-04-16 02:41:2821#include "chrome/browser/extensions/extension_browser_event_router.h"
[email protected]86c008e82009-08-28 20:26:0522#include "chrome/browser/extensions/extension_dom_ui.h"
[email protected]de768a832009-10-30 05:25:0123#include "chrome/browser/extensions/extension_history_api.h"
[email protected]b1748b1d82009-11-30 20:32:5624#include "chrome/browser/extensions/extension_host.h"
[email protected]4814b512009-11-07 00:12:2925#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]93fd78f42009-07-10 16:43:1726#include "chrome/browser/extensions/extension_updater.h"
[email protected]a1257b12009-06-12 02:51:3427#include "chrome/browser/extensions/external_extension_provider.h"
28#include "chrome/browser/extensions/external_pref_extension_provider.h"
[email protected]052313b2010-02-19 09:43:0829#include "chrome/browser/pref_service.h"
[email protected]81e63782009-02-27 19:35:0930#include "chrome/browser/profile.h"
[email protected]62d30f42009-10-01 22:36:0631#include "chrome/browser/net/chrome_url_request_context.h"
[email protected]aab98a52009-12-02 03:22:3532#include "chrome/common/child_process_logging.h"
[email protected]e2eb43112009-05-29 21:19:5433#include "chrome/common/chrome_switches.h"
[email protected]5b1a0e22009-05-26 19:00:5834#include "chrome/common/extensions/extension.h"
[email protected]d7b36dc2009-10-29 21:47:4035#include "chrome/common/extensions/extension_constants.h"
[email protected]5b1a0e22009-05-26 19:00:5836#include "chrome/common/extensions/extension_error_reporter.h"
[email protected]7c927b62010-02-24 09:54:1337#include "chrome/common/extensions/extension_file_util.h"
[email protected]c6d474f82009-12-16 21:11:0638#include "chrome/common/extensions/extension_l10n_util.h"
[email protected]82891262008-12-24 00:21:2639#include "chrome/common/notification_service.h"
[email protected]4814b512009-11-07 00:12:2940#include "chrome/common/notification_type.h"
[email protected]1952c7d2010-03-04 23:48:3441#include "chrome/common/json_value_serializer.h"
[email protected]25b34332009-06-05 21:53:1942#include "chrome/common/pref_names.h"
[email protected]a57209872009-05-04 22:53:1443#include "chrome/common/url_constants.h"
[email protected]24b538a2010-02-27 01:22:4444#include "webkit/database/database_tracker.h"
45#include "webkit/database/database_util.h"
[email protected]c64631652009-04-29 22:24:3146
[email protected]79db6232009-02-13 20:51:2047#if defined(OS_WIN)
[email protected]a1257b12009-06-12 02:51:3448#include "chrome/browser/extensions/external_registry_extension_provider_win.h"
[email protected]79db6232009-02-13 20:51:2049#endif
[email protected]6014d672008-12-05 00:38:2550
[email protected]5ef47ec2010-01-28 05:58:0551using base::Time;
52
[email protected]c6d474f82009-12-16 21:11:0653namespace errors = extension_manifest_errors;
54
[email protected]b6ab96d2009-08-20 18:58:1955namespace {
56
57// Helper class to collect the IDs of every extension listed in the prefs.
58class InstalledExtensionSet {
59 public:
[email protected]c6d474f82009-12-16 21:11:0660 explicit InstalledExtensionSet(ExtensionPrefs* prefs) {
61 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(
62 ExtensionPrefs::CollectExtensionsInfo(prefs));
63
64 for (size_t i = 0; i < info->size(); ++i) {
65 std::string version;
66 const DictionaryValue* manifest = info->at(i)->extension_manifest.get();
67 if (!manifest ||
68 !manifest->GetString(extension_manifest_keys::kVersion, &version)) {
69 // Without a version, the extension is invalid. Ignoring it here will
70 // cause it to get garbage collected.
71 continue;
72 }
73 extensions_.insert(info->at(i)->extension_id);
74 versions_[info->at(i)->extension_id] = version;
75 }
[email protected]b6ab96d2009-08-20 18:58:1976 }
77
78 const std::set<std::string>& extensions() { return extensions_; }
[email protected]4559a7d2009-12-02 01:42:4179 const std::map<std::string, std::string>& versions() { return versions_; }
[email protected]b6ab96d2009-08-20 18:58:1980
81 private:
[email protected]b6ab96d2009-08-20 18:58:1982 std::set<std::string> extensions_;
[email protected]4559a7d2009-12-02 01:42:4183 std::map<std::string, std::string> versions_;
[email protected]b6ab96d2009-08-20 18:58:1984};
85
[email protected]2111b1a2010-03-12 18:12:4486static bool ShouldReloadExtensionManifest(const ExtensionInfo& info) {
87 // Always reload LOAD extension manifests, because they can change on disk
88 // independent of the manifest in our prefs.
89 if (info.extension_location == Extension::LOAD)
90 return true;
91
92 // Otherwise, reload the manifest it needs to be relocalized.
93 return extension_l10n_util::ShouldRelocalizeManifest(info);
94}
95
[email protected]c6d474f82009-12-16 21:11:0696} // namespace
[email protected]b6ab96d2009-08-20 18:58:1997
[email protected]25b34332009-06-05 21:53:1998// ExtensionsService.
[email protected]6014d672008-12-05 00:38:2599
[email protected]cc655912009-01-29 23:19:19100const char* ExtensionsService::kInstallDirectoryName = "Extensions";
101const char* ExtensionsService::kCurrentVersionFileName = "Current Version";
[email protected]494c06e2009-07-25 01:06:42102
[email protected]b7c2f252009-12-08 00:47:23103// static
104bool ExtensionsService::IsDownloadFromGallery(const GURL& download_url,
105 const GURL& referrer_url) {
106 if (StartsWithASCII(download_url.spec(),
107 extension_urls::kMiniGalleryDownloadPrefix, false) &&
108 StartsWithASCII(referrer_url.spec(),
109 extension_urls::kMiniGalleryBrowsePrefix, false)) {
110 return true;
111 }
112
113 if (StartsWithASCII(download_url.spec(),
114 extension_urls::kGalleryDownloadPrefix, false) &&
115 StartsWithASCII(referrer_url.spec(),
116 extension_urls::kGalleryBrowsePrefix, false)) {
117 return true;
118 }
119
120 return false;
121}
122
[email protected]ac025282009-12-16 19:16:38123bool ExtensionsService::IsDownloadFromMiniGallery(const GURL& download_url) {
124 return StartsWithASCII(download_url.spec(),
125 extension_urls::kMiniGalleryDownloadPrefix,
126 false); // case_sensitive
127}
128
[email protected]81e63782009-02-27 19:35:09129ExtensionsService::ExtensionsService(Profile* profile,
[email protected]36a784c2009-06-23 06:21:08130 const CommandLine* command_line,
[email protected]a9b00ac2009-06-25 21:03:23131 PrefService* prefs,
132 const FilePath& install_directory,
[email protected]93fd78f42009-07-10 16:43:17133 bool autoupdate_enabled)
[email protected]6ef635e42009-07-26 06:16:12134 : profile_(profile),
135 extension_prefs_(new ExtensionPrefs(prefs, install_directory)),
[email protected]a9b00ac2009-06-25 21:03:23136 install_directory_(install_directory),
[email protected]6d60703b2009-08-29 01:29:23137 extensions_enabled_(true),
[email protected]e81dba32009-06-19 20:19:13138 show_extensions_prompts_(true),
[email protected]e0360f2c2009-12-07 22:34:31139 ready_(false),
140 ALLOW_THIS_IN_INITIALIZER_LIST(toolbar_model_(this)) {
[email protected]36a784c2009-06-23 06:21:08141 // Figure out if extension installation should be enabled.
[email protected]6d60703b2009-08-29 01:29:23142 if (command_line->HasSwitch(switches::kDisableExtensions)) {
143 extensions_enabled_ = false;
144 } else if (profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
145 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18146 }
[email protected]36a784c2009-06-23 06:21:08147
[email protected]4814b512009-11-07 00:12:29148 registrar_.Add(this, NotificationType::EXTENSION_HOST_DID_STOP_LOADING,
149 NotificationService::AllSources());
[email protected]a4ed6282009-12-14 20:51:16150 registrar_.Add(this, NotificationType::EXTENSION_PROCESS_TERMINATED,
[email protected]31f77262009-12-02 20:48:53151 Source<Profile>(profile_));
[email protected]4814b512009-11-07 00:12:29152
[email protected]93fd78f42009-07-10 16:43:17153 // Set up the ExtensionUpdater
154 if (autoupdate_enabled) {
155 int update_frequency = kDefaultUpdateFrequencySeconds;
156 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]c4e52f0d2009-11-06 19:55:16157 update_frequency = StringToInt(command_line->GetSwitchValueASCII(
158 switches::kExtensionsUpdateFrequency));
[email protected]93fd78f42009-07-10 16:43:17159 }
[email protected]95d29192009-10-30 01:49:06160 updater_ = new ExtensionUpdater(this, prefs, update_frequency);
[email protected]93fd78f42009-07-10 16:43:17161 }
162
[email protected]95d29192009-10-30 01:49:06163 backend_ = new ExtensionsServiceBackend(install_directory_);
[email protected]6014d672008-12-05 00:38:25164}
165
166ExtensionsService::~ExtensionsService() {
[email protected]9f1087e2009-06-15 17:29:32167 UnloadAllExtensions();
[email protected]93fd78f42009-07-10 16:43:17168 if (updater_.get()) {
169 updater_->Stop();
170 }
[email protected]6014d672008-12-05 00:38:25171}
172
[email protected]9f1087e2009-06-15 17:29:32173void ExtensionsService::Init() {
[email protected]c6e4a3412009-06-24 15:45:29174 DCHECK(!ready_);
[email protected]93fd78f42009-07-10 16:43:17175 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32176
[email protected]95dd38f2009-10-20 20:09:15177 // Hack: we need to ensure the ResourceDispatcherHost is ready before we load
178 // the first extension, because its members listen for loaded notifications.
179 g_browser_process->resource_dispatcher_host();
180
[email protected]de768a832009-10-30 05:25:01181 // Start up the extension event routers.
182 ExtensionHistoryEventRouter::GetInstance()->ObserveProfile(profile_);
[email protected]5cbe1e22010-01-30 01:18:56183 ExtensionAccessibilityEventRouter::GetInstance()->ObserveProfile(profile_);
[email protected]de768a832009-10-30 05:25:01184
[email protected]9f1087e2009-06-15 17:29:32185 LoadAllExtensions();
[email protected]894bb502009-05-21 22:39:57186
[email protected]9f1087e2009-06-15 17:29:32187 // TODO(erikkay) this should probably be deferred to a future point
188 // rather than running immediately at startup.
[email protected]93fd78f42009-07-10 16:43:17189 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57190
[email protected]9f1087e2009-06-15 17:29:32191 // TODO(erikkay) this should probably be deferred as well.
192 GarbageCollectExtensions();
[email protected]6014d672008-12-05 00:38:25193}
194
[email protected]3cf4f0992009-02-03 23:00:30195void ExtensionsService::InstallExtension(const FilePath& extension_path) {
[email protected]6dfbbf82010-03-12 23:09:16196 scoped_refptr<CrxInstaller> installer(
197 new CrxInstaller(install_directory_,
198 this, // frontend
199 NULL)); // no client (silent install)
200 installer->set_allow_privilege_increase(true);
201 installer->InstallCrx(extension_path);
[email protected]3cf4f0992009-02-03 23:00:30202}
203
[email protected]e957fe52009-06-23 16:51:05204void ExtensionsService::UpdateExtension(const std::string& id,
[email protected]5c8516202010-03-18 21:43:34205 const FilePath& extension_path,
206 const GURL& download_url) {
[email protected]0c6da502009-08-14 22:32:39207 if (!GetExtensionByIdInternal(id, true, true)) {
[email protected]e957fe52009-06-23 16:51:05208 LOG(WARNING) << "Will not update extension " << id << " because it is not "
[email protected]4c967932009-07-31 01:15:49209 << "installed";
210 return;
[email protected]e957fe52009-06-23 16:51:05211 }
212
[email protected]6dfbbf82010-03-12 23:09:16213 scoped_refptr<CrxInstaller> installer(
214 new CrxInstaller(install_directory_,
215 this, // frontend
216 NULL)); // no client (silent install)
217 installer->set_expected_id(id);
218 installer->set_delete_source(true);
[email protected]867a73e12010-03-19 20:45:46219 installer->set_force_web_origin_to_download_url(true);
[email protected]5c8516202010-03-18 21:43:34220 installer->set_original_url(download_url);
[email protected]6dfbbf82010-03-12 23:09:16221 installer->InstallCrx(extension_path);
[email protected]e957fe52009-06-23 16:51:05222}
223
[email protected]9cddd4702009-07-27 22:09:40224void ExtensionsService::ReloadExtension(const std::string& extension_id) {
[email protected]b65272f2009-08-31 15:47:06225 FilePath path;
[email protected]61b411612009-11-10 23:17:41226 Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40227
[email protected]b65272f2009-08-31 15:47:06228 // Unload the extension if it's loaded. It might not be loaded if it crashed.
229 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29230 // If the extension has an inspector open for its background page, detach
231 // the inspector and hang onto a cookie for it, so that we can reattach
232 // later.
233 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
234 ExtensionHost* host = manager->GetBackgroundHostForExtension(
235 current_extension);
236 if (host) {
237 // Look for an open inspector for the background page.
238 int devtools_cookie = DevToolsManager::GetInstance()->DetachClientHost(
239 host->render_view_host());
240 if (devtools_cookie >= 0)
241 orphaned_dev_tools_[extension_id] = devtools_cookie;
242 }
243
[email protected]b65272f2009-08-31 15:47:06244 path = current_extension->path();
245 UnloadExtension(extension_id);
[email protected]1eb175082010-02-10 09:26:16246 } else {
247 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06248 }
249
[email protected]1eb175082010-02-10 09:26:16250 // We should always be able to remember the extension's path. If it's not in
251 // the map, someone failed to update |unloaded_extension_paths_|.
252 CHECK(!path.empty());
[email protected]b65272f2009-08-31 15:47:06253
[email protected]1eb175082010-02-10 09:26:16254 LoadExtension(path);
[email protected]9cddd4702009-07-27 22:09:40255}
256
[email protected]27b985d2009-06-25 17:53:15257void ExtensionsService::UninstallExtension(const std::string& extension_id,
258 bool external_uninstall) {
[email protected]0c6da502009-08-14 22:32:39259 Extension* extension = GetExtensionByIdInternal(extension_id, true, true);
[email protected]631cf822009-05-15 07:01:25260
[email protected]9f1087e2009-06-15 17:29:32261 // Callers should not send us nonexistant extensions.
[email protected]e72e8eb82009-06-18 17:21:51262 DCHECK(extension);
[email protected]9f1087e2009-06-15 17:29:32263
[email protected]27b985d2009-06-25 17:53:15264 extension_prefs_->OnExtensionUninstalled(extension, external_uninstall);
[email protected]9f1087e2009-06-15 17:29:32265
266 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]e72e8eb82009-06-18 17:21:51267 if (Extension::LOAD != extension->location()) {
[email protected]95d29192009-10-30 01:49:06268 ChromeThread::PostTask(
269 ChromeThread::FILE, FROM_HERE,
270 NewRunnableFunction(
271 &extension_file_util::UninstallExtension, extension_id,
272 install_directory_));
[email protected]9f1087e2009-06-15 17:29:32273 }
274
[email protected]86c008e82009-08-28 20:26:05275 ExtensionDOMUI::UnregisterChromeURLOverrides(profile_,
276 extension->GetChromeURLOverrides());
277
[email protected]9f1087e2009-06-15 17:29:32278 UnloadExtension(extension_id);
279}
280
[email protected]0c6da502009-08-14 22:32:39281void ExtensionsService::EnableExtension(const std::string& extension_id) {
282 Extension* extension = GetExtensionByIdInternal(extension_id, false, true);
283 if (!extension) {
284 NOTREACHED() << "Trying to enable an extension that isn't disabled.";
285 return;
286 }
287
[email protected]e8c729a2010-03-09 19:55:19288 extension_prefs_->SetExtensionState(extension, Extension::ENABLED);
[email protected]1784e83a2009-09-08 21:01:52289
[email protected]0c6da502009-08-14 22:32:39290 // Move it over to the enabled list.
[email protected]0c6da502009-08-14 22:32:39291 extensions_.push_back(extension);
292 ExtensionList::iterator iter = std::find(disabled_extensions_.begin(),
293 disabled_extensions_.end(),
294 extension);
295 disabled_extensions_.erase(iter);
296
[email protected]86c008e82009-08-28 20:26:05297 ExtensionDOMUI::RegisterChromeURLOverrides(profile_,
298 extension->GetChromeURLOverrides());
299
[email protected]62d30f42009-10-01 22:36:06300 NotifyExtensionLoaded(extension);
[email protected]aab98a52009-12-02 03:22:35301 UpdateActiveExtensionsInCrashReporter();
[email protected]0c6da502009-08-14 22:32:39302}
303
[email protected]1784e83a2009-09-08 21:01:52304void ExtensionsService::DisableExtension(const std::string& extension_id) {
305 Extension* extension = GetExtensionByIdInternal(extension_id, true, false);
[email protected]b2ba9962009-12-10 20:10:15306 // The extension may have been disabled already.
307 if (!extension)
[email protected]1784e83a2009-09-08 21:01:52308 return;
[email protected]1784e83a2009-09-08 21:01:52309
[email protected]e8c729a2010-03-09 19:55:19310 extension_prefs_->SetExtensionState(extension, Extension::DISABLED);
[email protected]1784e83a2009-09-08 21:01:52311
312 // Move it over to the disabled list.
313 disabled_extensions_.push_back(extension);
314 ExtensionList::iterator iter = std::find(extensions_.begin(),
315 extensions_.end(),
316 extension);
317 extensions_.erase(iter);
318
319 ExtensionDOMUI::UnregisterChromeURLOverrides(profile_,
320 extension->GetChromeURLOverrides());
321
[email protected]62d30f42009-10-01 22:36:06322 NotifyExtensionUnloaded(extension);
[email protected]aab98a52009-12-02 03:22:35323 UpdateActiveExtensionsInCrashReporter();
[email protected]1784e83a2009-09-08 21:01:52324}
325
[email protected]9f1087e2009-06-15 17:29:32326void ExtensionsService::LoadExtension(const FilePath& extension_path) {
[email protected]95d29192009-10-30 01:49:06327 ChromeThread::PostTask(
328 ChromeThread::FILE, FROM_HERE,
329 NewRunnableMethod(
330 backend_.get(),
331 &ExtensionsServiceBackend::LoadSingleExtension,
332 extension_path, scoped_refptr<ExtensionsService>(this)));
[email protected]9f1087e2009-06-15 17:29:32333}
334
[email protected]1952c7d2010-03-04 23:48:34335void ExtensionsService::LoadComponentExtensions() {
336 for (RegisteredComponentExtensions::iterator it =
337 component_extension_manifests_.begin();
338 it != component_extension_manifests_.end(); ++it) {
339 JSONStringValueSerializer serializer(it->manifest);
340 scoped_ptr<Value> manifest(serializer.Deserialize(NULL));
[email protected]999731f2010-03-22 19:13:53341 if (!manifest.get()) {
342 NOTREACHED() << "Failed to retrieve manifest for extension";
343 continue;
344 }
[email protected]1952c7d2010-03-04 23:48:34345
346 scoped_ptr<Extension> extension(new Extension(it->root_directory));
347 extension->set_location(Extension::COMPONENT);
348
349 std::string error;
350 if (!extension->InitFromValue(
351 *static_cast<DictionaryValue*>(manifest.get()),
352 true, // require key
353 &error)) {
354 NOTREACHED();
355 return;
356 }
357
358 OnExtensionLoaded(extension.release(), false); // Don't allow privilege
359 // increase.
360 }
361}
362
[email protected]9f1087e2009-06-15 17:29:32363void ExtensionsService::LoadAllExtensions() {
[email protected]cc2c3432009-11-06 17:24:36364 base::TimeTicks start_time = base::TimeTicks::Now();
365
[email protected]1952c7d2010-03-04 23:48:34366 // Load any component extensions.
367 LoadComponentExtensions();
368
[email protected]e72e8eb82009-06-18 17:21:51369 // Load the previously installed extensions.
[email protected]c6d474f82009-12-16 21:11:06370 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(
371 ExtensionPrefs::CollectExtensionsInfo(extension_prefs_.get()));
372
373 // If any extensions need localization, we bounce them all to the file thread
374 // for re-reading and localization.
375 for (size_t i = 0; i < info->size(); ++i) {
[email protected]2111b1a2010-03-12 18:12:44376 if (ShouldReloadExtensionManifest(*info->at(i))) {
[email protected]c6d474f82009-12-16 21:11:06377 ChromeThread::PostTask(
378 ChromeThread::FILE, FROM_HERE, NewRunnableMethod(
379 backend_.get(),
[email protected]2111b1a2010-03-12 18:12:44380 &ExtensionsServiceBackend::ReloadExtensionManifests,
[email protected]c6d474f82009-12-16 21:11:06381 info.release(), // Callee takes ownership of the memory.
382 start_time,
383 scoped_refptr<ExtensionsService>(this)));
384 return;
385 }
386 }
387
388 // Don't update prefs.
389 // Callee takes ownership of the memory.
390 ContinueLoadAllExtensions(info.release(), start_time, false);
391}
392
393void ExtensionsService::ContinueLoadAllExtensions(
394 ExtensionPrefs::ExtensionsInfo* extensions_info,
395 base::TimeTicks start_time,
396 bool write_to_prefs) {
397 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(extensions_info);
398
399 for (size_t i = 0; i < info->size(); ++i) {
400 LoadInstalledExtension(*info->at(i), write_to_prefs);
401 }
402
[email protected]ae09ca62009-08-21 19:46:46403 OnLoadedInstalledExtensions();
[email protected]cc2c3432009-11-06 17:24:36404
405 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadAll", extensions_.size());
406 UMA_HISTOGRAM_COUNTS_100("Extensions.Disabled", disabled_extensions_.size());
407
[email protected]1952c7d2010-03-04 23:48:34408 UMA_HISTOGRAM_TIMES("Extensions.LoadAllTime",
409 base::TimeTicks::Now() - start_time);
[email protected]cc2c3432009-11-06 17:24:36410
[email protected]1952c7d2010-03-04 23:48:34411 int user_script_count = 0;
412 int extension_count = 0;
413 int theme_count = 0;
414 int external_count = 0;
415 int page_action_count = 0;
416 int browser_action_count = 0;
417 ExtensionList::iterator ex;
418 for (ex = extensions_.begin(); ex != extensions_.end(); ++ex) {
419 // Don't count component extensions, since they are only extensions as an
420 // implementation detail.
421 if ((*ex)->location() == Extension::COMPONENT)
422 continue;
423
[email protected]e8c729a2010-03-09 19:55:19424 // Don't count unpacked extensions, since they're a developer-specific
425 // feature.
426 if ((*ex)->location() == Extension::LOAD)
427 continue;
428
[email protected]1952c7d2010-03-04 23:48:34429 if ((*ex)->IsTheme()) {
430 theme_count++;
431 } else if ((*ex)->converted_from_user_script()) {
432 user_script_count++;
433 } else {
434 extension_count++;
[email protected]cc2c3432009-11-06 17:24:36435 }
[email protected]1952c7d2010-03-04 23:48:34436 if (Extension::IsExternalLocation((*ex)->location())) {
437 external_count++;
438 }
439 if ((*ex)->page_action() != NULL) {
440 page_action_count++;
441 }
442 if ((*ex)->browser_action() != NULL) {
443 browser_action_count++;
444 }
[email protected]cc2c3432009-11-06 17:24:36445 }
[email protected]1952c7d2010-03-04 23:48:34446 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExtension", extension_count);
447 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadUserScript", user_script_count);
448 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadTheme", theme_count);
449 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExternal", external_count);
450 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadPageAction", page_action_count);
451 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadBrowserAction",
452 browser_action_count);
[email protected]ae09ca62009-08-21 19:46:46453}
454
[email protected]c6d474f82009-12-16 21:11:06455void ExtensionsService::LoadInstalledExtension(const ExtensionInfo& info,
456 bool write_to_prefs) {
[email protected]ae09ca62009-08-21 19:46:46457 std::string error;
458 Extension* extension = NULL;
[email protected]c6d474f82009-12-16 21:11:06459 if (info.extension_manifest.get()) {
460 scoped_ptr<Extension> tmp(new Extension(info.extension_path));
[email protected]e8c729a2010-03-09 19:55:19461 bool require_key = info.extension_location != Extension::LOAD;
462 if (tmp->InitFromValue(*info.extension_manifest, require_key, &error))
[email protected]ae09ca62009-08-21 19:46:46463 extension = tmp.release();
[email protected]ae09ca62009-08-21 19:46:46464 } else {
[email protected]c6d474f82009-12-16 21:11:06465 error = errors::kManifestUnreadable;
[email protected]ae09ca62009-08-21 19:46:46466 }
467
468 if (!extension) {
[email protected]c6d474f82009-12-16 21:11:06469 ReportExtensionLoadError(info.extension_path,
[email protected]d11c8e92009-10-20 23:26:40470 error,
471 NotificationType::EXTENSION_INSTALL_ERROR,
472 false);
[email protected]ae09ca62009-08-21 19:46:46473 return;
474 }
475
[email protected]c6d474f82009-12-16 21:11:06476 extension->set_location(info.extension_location);
477
478 if (write_to_prefs)
479 extension_prefs_->UpdateManifest(extension);
480
[email protected]2a409532009-08-28 19:39:44481 OnExtensionLoaded(extension, true);
[email protected]ae09ca62009-08-21 19:46:46482
[email protected]c6d474f82009-12-16 21:11:06483 if (info.extension_location == Extension::EXTERNAL_PREF ||
484 info.extension_location == Extension::EXTERNAL_REGISTRY) {
[email protected]95d29192009-10-30 01:49:06485 ChromeThread::PostTask(
486 ChromeThread::FILE, FROM_HERE,
487 NewRunnableMethod(
[email protected]c6d474f82009-12-16 21:11:06488 backend_.get(),
489 &ExtensionsServiceBackend::CheckExternalUninstall,
490 scoped_refptr<ExtensionsService>(this),
491 info.extension_id,
492 info.extension_location));
[email protected]ae09ca62009-08-21 19:46:46493 }
[email protected]9f1087e2009-06-15 17:29:32494}
495
[email protected]62d30f42009-10-01 22:36:06496void ExtensionsService::NotifyExtensionLoaded(Extension* extension) {
497 LOG(INFO) << "Sending EXTENSION_LOADED";
498
499 // The ChromeURLRequestContext needs to be first to know that the extension
500 // was loaded, otherwise a race can arise where a renderer that is created
501 // for the extension may try to load an extension URL with an extension id
502 // that the request context doesn't yet know about.
503 if (profile_ && !profile_->IsOffTheRecord()) {
[email protected]be180c802009-10-23 06:33:31504 ChromeURLRequestContextGetter* context_getter =
505 static_cast<ChromeURLRequestContextGetter*>(
506 profile_->GetRequestContext());
507 if (context_getter) {
[email protected]95d29192009-10-30 01:49:06508 ChromeThread::PostTask(
509 ChromeThread::IO, FROM_HERE,
510 NewRunnableMethod(
511 context_getter,
512 &ChromeURLRequestContextGetter::OnNewExtensions,
513 extension->id(),
[email protected]0ce3f5982010-01-28 23:04:27514 new ChromeURLRequestContext::ExtensionInfo(
515 extension->path(),
516 extension->default_locale(),
[email protected]867a73e12010-03-19 20:45:46517 std::vector<URLPattern>(),
[email protected]0ce3f5982010-01-28 23:04:27518 extension->api_permissions())));
[email protected]62d30f42009-10-01 22:36:06519 }
[email protected]24b538a2010-02-27 01:22:44520
521 // Check if this permission requires unlimited storage quota
522 if (extension->HasApiPermission(Extension::kUnlimitedStoragePermission)) {
523 string16 origin_identifier =
524 webkit_database::DatabaseUtil::GetOriginIdentifier(extension->url());
525 ChromeThread::PostTask(
526 ChromeThread::FILE, FROM_HERE,
527 NewRunnableMethod(
528 profile_->GetDatabaseTracker(),
529 &webkit_database::DatabaseTracker::SetOriginQuotaInMemory,
530 origin_identifier,
531 kint64max));
532 }
[email protected]62d30f42009-10-01 22:36:06533 }
534
535 NotificationService::current()->Notify(
536 NotificationType::EXTENSION_LOADED,
[email protected]24e7a9d2009-11-04 11:11:34537 Source<Profile>(profile_),
[email protected]62d30f42009-10-01 22:36:06538 Details<Extension>(extension));
539}
540
541void ExtensionsService::NotifyExtensionUnloaded(Extension* extension) {
542 LOG(INFO) << "Sending EXTENSION_UNLOADED";
543
544 NotificationService::current()->Notify(
545 NotificationType::EXTENSION_UNLOADED,
[email protected]24e7a9d2009-11-04 11:11:34546 Source<Profile>(profile_),
[email protected]62d30f42009-10-01 22:36:06547 Details<Extension>(extension));
548
549 if (profile_ && !profile_->IsOffTheRecord()) {
[email protected]be180c802009-10-23 06:33:31550 ChromeURLRequestContextGetter* context_getter =
551 static_cast<ChromeURLRequestContextGetter*>(
552 profile_->GetRequestContext());
553 if (context_getter) {
[email protected]95d29192009-10-30 01:49:06554 ChromeThread::PostTask(
555 ChromeThread::IO, FROM_HERE,
[email protected]be180c802009-10-23 06:33:31556 NewRunnableMethod(
557 context_getter,
558 &ChromeURLRequestContextGetter::OnUnloadedExtension,
559 extension->id()));
[email protected]62d30f42009-10-01 22:36:06560 }
561 }
562}
563
[email protected]6b75ec32009-08-14 06:37:18564void ExtensionsService::UpdateExtensionBlacklist(
565 const std::vector<std::string>& blacklist) {
566 // Use this set to indicate if an extension in the blacklist has been used.
567 std::set<std::string> blacklist_set;
568 for (unsigned int i = 0; i < blacklist.size(); ++i) {
569 if (Extension::IdIsValid(blacklist[i])) {
570 blacklist_set.insert(blacklist[i]);
571 }
572 }
573 extension_prefs_->UpdateBlacklist(blacklist_set);
574 std::vector<std::string> to_be_removed;
575 // Loop current extensions, unload installed extensions.
576 for (ExtensionList::const_iterator iter = extensions_.begin();
577 iter != extensions_.end(); ++iter) {
578 Extension* extension = (*iter);
579 if (blacklist_set.find(extension->id()) != blacklist_set.end()) {
580 to_be_removed.push_back(extension->id());
581 }
582 }
583
584 // UnloadExtension will change the extensions_ list. So, we should
585 // call it outside the iterator loop.
586 for (unsigned int i = 0; i < to_be_removed.size(); ++i) {
587 UnloadExtension(to_be_removed[i]);
588 }
589}
590
[email protected]5ef47ec2010-01-28 05:58:05591void ExtensionsService::SetLastPingDay(const std::string& extension_id,
592 const base::Time& time) {
593 extension_prefs_->SetLastPingDay(extension_id, time);
594}
595
596base::Time ExtensionsService::LastPingDay(const std::string& extension_id) {
597 return extension_prefs_->LastPingDay(extension_id);
598}
599
[email protected]cb0ce1e022010-03-10 19:54:41600bool ExtensionsService::IsIncognitoEnabled(const Extension* extension) {
601 // If this is a component extension we always allow it to work in incognito
602 // mode.
603 if (extension->location() == Extension::COMPONENT)
604 return true;
605
606 // Check the prefs.
607 return extension_prefs_->IsIncognitoEnabled(extension->id());
[email protected]db7331a2010-02-25 22:10:50608}
[email protected]55a35692010-02-11 23:25:21609
[email protected]cb0ce1e022010-03-10 19:54:41610void ExtensionsService::SetIsIncognitoEnabled(Extension* extension,
[email protected]db7331a2010-02-25 22:10:50611 bool enabled) {
[email protected]cb0ce1e022010-03-10 19:54:41612 extension_prefs_->SetIsIncognitoEnabled(extension->id(), enabled);
[email protected]c1499f3d2010-03-05 00:33:24613
614 // Broadcast unloaded and loaded events to update browser state.
615 NotifyExtensionUnloaded(extension);
616 NotifyExtensionLoaded(extension);
[email protected]55a35692010-02-11 23:25:21617}
618
[email protected]93fd78f42009-07-10 16:43:17619void ExtensionsService::CheckForExternalUpdates() {
[email protected]9f1087e2009-06-15 17:29:32620 // This installs or updates externally provided extensions.
[email protected]7577a5c52009-07-30 06:21:58621 // TODO(aa): Why pass this list into the provider, why not just filter it
622 // later?
[email protected]9f1087e2009-06-15 17:29:32623 std::set<std::string> killed_extensions;
[email protected]e72e8eb82009-06-18 17:21:51624 extension_prefs_->GetKilledExtensionIds(&killed_extensions);
[email protected]95d29192009-10-30 01:49:06625 ChromeThread::PostTask(
626 ChromeThread::FILE, FROM_HERE,
627 NewRunnableMethod(
628 backend_.get(), &ExtensionsServiceBackend::CheckForExternalUpdates,
629 killed_extensions, scoped_refptr<ExtensionsService>(this)));
[email protected]9f1087e2009-06-15 17:29:32630}
631
632void ExtensionsService::UnloadExtension(const std::string& extension_id) {
[email protected]27e469a2010-01-11 20:35:09633 // Make sure the extension gets deleted after we return from this function.
[email protected]0c6da502009-08-14 22:32:39634 scoped_ptr<Extension> extension(
635 GetExtensionByIdInternal(extension_id, true, true));
[email protected]631cf822009-05-15 07:01:25636
[email protected]894bb502009-05-21 22:39:57637 // Callers should not send us nonexistant extensions.
[email protected]0c6da502009-08-14 22:32:39638 CHECK(extension.get());
639
[email protected]1eb175082010-02-10 09:26:16640 // Keep information about the extension so that we can reload it later
641 // even if it's not permanently installed.
642 unloaded_extension_paths_[extension->id()] = extension->path();
643
[email protected]86c008e82009-08-28 20:26:05644 ExtensionDOMUI::UnregisterChromeURLOverrides(profile_,
645 extension->GetChromeURLOverrides());
646
[email protected]0c6da502009-08-14 22:32:39647 ExtensionList::iterator iter = std::find(disabled_extensions_.begin(),
648 disabled_extensions_.end(),
649 extension.get());
650 if (iter != disabled_extensions_.end()) {
[email protected]0c6da502009-08-14 22:32:39651 disabled_extensions_.erase(iter);
[email protected]866930682009-08-18 22:53:47652 NotificationService::current()->Notify(
653 NotificationType::EXTENSION_UNLOADED_DISABLED,
[email protected]24e7a9d2009-11-04 11:11:34654 Source<Profile>(profile_),
[email protected]866930682009-08-18 22:53:47655 Details<Extension>(extension.get()));
[email protected]0c6da502009-08-14 22:32:39656 return;
657 }
658
659 iter = std::find(extensions_.begin(), extensions_.end(), extension.get());
[email protected]894bb502009-05-21 22:39:57660
[email protected]631cf822009-05-15 07:01:25661 // Remove the extension from our list.
662 extensions_.erase(iter);
663
[email protected]62d30f42009-10-01 22:36:06664 NotifyExtensionUnloaded(extension.get());
[email protected]aab98a52009-12-02 03:22:35665 UpdateActiveExtensionsInCrashReporter();
[email protected]631cf822009-05-15 07:01:25666}
667
[email protected]9f1087e2009-06-15 17:29:32668void ExtensionsService::UnloadAllExtensions() {
669 ExtensionList::iterator iter;
[email protected]c6e4a3412009-06-24 15:45:29670 for (iter = extensions_.begin(); iter != extensions_.end(); ++iter)
[email protected]9f1087e2009-06-15 17:29:32671 delete *iter;
[email protected]9f1087e2009-06-15 17:29:32672 extensions_.clear();
[email protected]c6e4a3412009-06-24 15:45:29673
674 // TODO(erikkay) should there be a notification for this? We can't use
675 // EXTENSION_UNLOADED since that implies that the extension has been disabled
676 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:32677}
678
679void ExtensionsService::ReloadExtensions() {
680 UnloadAllExtensions();
681 LoadAllExtensions();
682}
683
684void ExtensionsService::GarbageCollectExtensions() {
[email protected]c6d474f82009-12-16 21:11:06685 InstalledExtensionSet installed(extension_prefs_.get());
[email protected]95d29192009-10-30 01:49:06686 ChromeThread::PostTask(
687 ChromeThread::FILE, FROM_HERE,
688 NewRunnableFunction(
689 &extension_file_util::GarbageCollectExtensions, install_directory_,
[email protected]4559a7d2009-12-02 01:42:41690 installed.extensions(), installed.versions()));
[email protected]3cf4f0992009-02-03 23:00:30691}
692
[email protected]e72e8eb82009-06-18 17:21:51693void ExtensionsService::OnLoadedInstalledExtensions() {
[email protected]e81dba32009-06-19 20:19:13694 ready_ = true;
[email protected]93fd78f42009-07-10 16:43:17695 if (updater_.get()) {
696 updater_->Start();
697 }
[email protected]e72e8eb82009-06-18 17:21:51698 NotificationService::current()->Notify(
699 NotificationType::EXTENSIONS_READY,
[email protected]24e7a9d2009-11-04 11:11:34700 Source<Profile>(profile_),
[email protected]e72e8eb82009-06-18 17:21:51701 NotificationService::NoDetails());
702}
703
[email protected]2a409532009-08-28 19:39:44704void ExtensionsService::OnExtensionLoaded(Extension* extension,
705 bool allow_privilege_increase) {
[email protected]ae09ca62009-08-21 19:46:46706 // Ensure extension is deleted unless we transfer ownership.
707 scoped_ptr<Extension> scoped_extension(extension);
[email protected]9f1087e2009-06-15 17:29:32708
[email protected]1eb175082010-02-10 09:26:16709 // The extension is now loaded, remove its data from unloaded extension map.
710 unloaded_extension_paths_.erase(extension->id());
711
[email protected]ceefd3d2010-03-12 09:10:29712 // TODO(aa): Need to re-evaluate this branch. Does this still make sense now
713 // that extensions are enabled by default?
[email protected]ae09ca62009-08-21 19:46:46714 if (extensions_enabled() ||
715 extension->IsTheme() ||
716 extension->location() == Extension::LOAD ||
717 Extension::IsExternalLocation(extension->location())) {
718 Extension* old = GetExtensionByIdInternal(extension->id(), true, true);
719 if (old) {
720 if (extension->version()->CompareTo(*(old->version())) > 0) {
[email protected]2a409532009-08-28 19:39:44721 bool allow_silent_upgrade =
722 allow_privilege_increase || !Extension::IsPrivilegeIncrease(
723 old, extension);
[email protected]0c6da502009-08-14 22:32:39724
[email protected]1e8c93f2010-02-08 22:58:31725 // Extensions get upgraded if silent upgrades are allowed, otherwise
726 // they get disabled.
727 if (allow_silent_upgrade) {
728 old->set_being_upgraded(true);
729 extension->set_being_upgraded(true);
730 }
731
[email protected]ae09ca62009-08-21 19:46:46732 // To upgrade an extension in place, unload the old one and
733 // then load the new one.
734 UnloadExtension(old->id());
735 old = NULL;
[email protected]0c6da502009-08-14 22:32:39736
[email protected]b24d8312009-08-27 06:47:46737 if (!allow_silent_upgrade) {
[email protected]6d27a7b2009-12-18 23:25:45738 // Extension has changed permissions significantly. Disable it. We
739 // send a notification below.
[email protected]ae09ca62009-08-21 19:46:46740 extension_prefs_->SetExtensionState(extension, Extension::DISABLED);
[email protected]7d845862010-01-04 21:28:57741 extension_prefs_->SetShowInstallWarningOnEnable(extension, true);
[email protected]9f1087e2009-06-15 17:29:32742 }
[email protected]ae09ca62009-08-21 19:46:46743 } else {
744 // We already have the extension of the same or older version.
[email protected]d11c8e92009-10-20 23:26:40745 std::string error_message("Duplicate extension load attempt: ");
746 error_message += extension->id();
747 LOG(WARNING) << error_message;
748 ReportExtensionLoadError(extension->path(),
749 error_message,
750 NotificationType::EXTENSION_OVERINSTALL_ERROR,
751 false);
[email protected]ae09ca62009-08-21 19:46:46752 return;
[email protected]0c6da502009-08-14 22:32:39753 }
[email protected]ba74f352009-06-11 18:54:45754 }
[email protected]86a274072009-06-11 02:06:45755
[email protected]ae09ca62009-08-21 19:46:46756 switch (extension_prefs_->GetExtensionState(extension->id())) {
757 case Extension::ENABLED:
758 extensions_.push_back(scoped_extension.release());
759
[email protected]aeb53b32009-10-29 07:34:45760 // We delay starting up the browser event router until at least one
761 // extension that needs it is loaded.
762 if (extension->HasApiPermission(Extension::kTabPermission)) {
763 ExtensionBrowserEventRouter::GetInstance()->Init();
764 }
[email protected]840b0db2009-11-20 03:00:38765 if (extension->HasApiPermission(Extension::kBookmarkPermission)) {
766 ExtensionBookmarkEventRouter::GetSingleton()->Observe(
767 profile_->GetBookmarkModel());
768 }
[email protected]aeb53b32009-10-29 07:34:45769
[email protected]62d30f42009-10-01 22:36:06770 NotifyExtensionLoaded(extension);
[email protected]ae09ca62009-08-21 19:46:46771
[email protected]e8c729a2010-03-09 19:55:19772 ExtensionDOMUI::RegisterChromeURLOverrides(profile_,
773 extension->GetChromeURLOverrides());
[email protected]ae09ca62009-08-21 19:46:46774 break;
775 case Extension::DISABLED:
[email protected]6d27a7b2009-12-18 23:25:45776 disabled_extensions_.push_back(scoped_extension.release());
[email protected]d11c8e92009-10-20 23:26:40777 NotificationService::current()->Notify(
778 NotificationType::EXTENSION_UPDATE_DISABLED,
[email protected]24e7a9d2009-11-04 11:11:34779 Source<Profile>(profile_),
[email protected]d11c8e92009-10-20 23:26:40780 Details<Extension>(extension));
[email protected]ae09ca62009-08-21 19:46:46781 break;
782 default:
[email protected]d11c8e92009-10-20 23:26:40783 NOTREACHED();
[email protected]ae09ca62009-08-21 19:46:46784 break;
[email protected]811f3432009-07-25 19:38:21785 }
[email protected]e72e8eb82009-06-18 17:21:51786 }
[email protected]aab98a52009-12-02 03:22:35787
[email protected]1e8c93f2010-02-08 22:58:31788 extension->set_being_upgraded(false);
789
[email protected]aab98a52009-12-02 03:22:35790 UpdateActiveExtensionsInCrashReporter();
791}
792
793void ExtensionsService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:39794 std::set<std::string> extension_ids;
[email protected]aab98a52009-12-02 03:22:35795 for (size_t i = 0; i < extensions_.size(); ++i) {
796 if (!extensions_[i]->IsTheme())
[email protected]c8865962009-12-16 07:47:39797 extension_ids.insert(extensions_[i]->id());
[email protected]aab98a52009-12-02 03:22:35798 }
799
800 child_process_logging::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:25801}
802
[email protected]2a409532009-08-28 19:39:44803void ExtensionsService::OnExtensionInstalled(Extension* extension,
804 bool allow_privilege_increase) {
[email protected]b6ab96d2009-08-20 18:58:19805 extension_prefs_->OnExtensionInstalled(extension);
[email protected]25b34332009-06-05 21:53:19806
[email protected]4a190632009-05-09 01:07:42807 // If the extension is a theme, tell the profile (and therefore ThemeProvider)
808 // to apply it.
809 if (extension->IsTheme()) {
[email protected]9ceb07342009-07-26 04:09:23810 NotificationService::current()->Notify(
811 NotificationType::THEME_INSTALLED,
[email protected]24e7a9d2009-11-04 11:11:34812 Source<Profile>(profile_),
[email protected]9ceb07342009-07-26 04:09:23813 Details<Extension>(extension));
[email protected]9197f3b2009-06-02 00:49:27814 } else {
815 NotificationService::current()->Notify(
816 NotificationType::EXTENSION_INSTALLED,
[email protected]24e7a9d2009-11-04 11:11:34817 Source<Profile>(profile_),
[email protected]9197f3b2009-06-02 00:49:27818 Details<Extension>(extension));
[email protected]4a190632009-05-09 01:07:42819 }
[email protected]7577a5c52009-07-30 06:21:58820
821 // Also load the extension.
[email protected]2a409532009-08-28 19:39:44822 OnExtensionLoaded(extension, allow_privilege_increase);
[email protected]4a190632009-05-09 01:07:42823}
824
[email protected]7577a5c52009-07-30 06:21:58825void ExtensionsService::OnExtensionOverinstallAttempted(const std::string& id) {
[email protected]61b411612009-11-10 23:17:41826 Extension* extension = GetExtensionById(id, false);
[email protected]4a190632009-05-09 01:07:42827 if (extension && extension->IsTheme()) {
[email protected]9ceb07342009-07-26 04:09:23828 NotificationService::current()->Notify(
829 NotificationType::THEME_INSTALLED,
[email protected]24e7a9d2009-11-04 11:11:34830 Source<Profile>(profile_),
[email protected]9ceb07342009-07-26 04:09:23831 Details<Extension>(extension));
[email protected]91e1bd82009-09-03 22:04:40832 } else {
833 NotificationService::current()->Notify(
834 NotificationType::NO_THEME_DETECTED,
[email protected]24e7a9d2009-11-04 11:11:34835 Source<Profile>(profile_),
[email protected]91e1bd82009-09-03 22:04:40836 NotificationService::NoDetails());
[email protected]4a190632009-05-09 01:07:42837 }
[email protected]cc655912009-01-29 23:19:19838}
839
[email protected]0c6da502009-08-14 22:32:39840Extension* ExtensionsService::GetExtensionByIdInternal(const std::string& id,
841 bool include_enabled,
842 bool include_disabled) {
[email protected]e957fe52009-06-23 16:51:05843 std::string lowercase_id = StringToLowerASCII(id);
[email protected]0c6da502009-08-14 22:32:39844 if (include_enabled) {
845 for (ExtensionList::const_iterator iter = extensions_.begin();
846 iter != extensions_.end(); ++iter) {
847 if ((*iter)->id() == lowercase_id)
848 return *iter;
849 }
850 }
851 if (include_disabled) {
852 for (ExtensionList::const_iterator iter = disabled_extensions_.begin();
853 iter != disabled_extensions_.end(); ++iter) {
854 if ((*iter)->id() == lowercase_id)
855 return *iter;
856 }
[email protected]ce5c4502009-05-06 16:46:11857 }
858 return NULL;
859}
860
[email protected]9f1087e2009-06-15 17:29:32861Extension* ExtensionsService::GetExtensionByURL(const GURL& url) {
862 std::string host = url.host();
[email protected]61b411612009-11-10 23:17:41863 return GetExtensionById(host, false);
[email protected]9f1087e2009-06-15 17:29:32864}
865
[email protected]a1257b12009-06-12 02:51:34866void ExtensionsService::ClearProvidersForTesting() {
[email protected]95d29192009-10-30 01:49:06867 ChromeThread::PostTask(
868 ChromeThread::FILE, FROM_HERE,
869 NewRunnableMethod(
870 backend_.get(), &ExtensionsServiceBackend::ClearProvidersForTesting));
[email protected]a1257b12009-06-12 02:51:34871}
872
873void ExtensionsService::SetProviderForTesting(
874 Extension::Location location, ExternalExtensionProvider* test_provider) {
[email protected]95d29192009-10-30 01:49:06875 ChromeThread::PostTask(
876 ChromeThread::FILE, FROM_HERE,
877 NewRunnableMethod(
878 backend_.get(), &ExtensionsServiceBackend::SetProviderForTesting,
879 location, test_provider));
[email protected]a1257b12009-06-12 02:51:34880}
881
[email protected]7577a5c52009-07-30 06:21:58882void ExtensionsService::OnExternalExtensionFound(const std::string& id,
883 const std::string& version,
884 const FilePath& path,
885 Extension::Location location) {
886 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:49887 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:58888 // installed on every startup.
[email protected]61b411612009-11-10 23:17:41889 Extension* existing = GetExtensionById(id, true);
[email protected]a3a63ff82009-08-04 06:44:11890 scoped_ptr<Version> other(Version::GetVersionFromString(version));
[email protected]7577a5c52009-07-30 06:21:58891 if (existing) {
[email protected]a3a63ff82009-08-04 06:44:11892 switch (existing->version()->CompareTo(*other)) {
[email protected]7577a5c52009-07-30 06:21:58893 case -1: // existing version is older, we should upgrade
894 break;
895 case 0: // existing version is same, do nothing
896 return;
897 case 1: // existing version is newer, uh-oh
898 LOG(WARNING) << "Found external version of extension " << id
899 << "that is older than current version. Current version "
900 << "is: " << existing->VersionString() << ". New version "
901 << "is: " << version << ". Keeping current version.";
902 return;
903 }
904 }
905
[email protected]6dfbbf82010-03-12 23:09:16906 scoped_refptr<CrxInstaller> installer(
907 new CrxInstaller(install_directory_,
908 this, // frontend
909 NULL)); // no client (silent install)
910 installer->set_install_source(location);
911 installer->set_expected_id(id);
912 installer->set_allow_privilege_increase(true);
913 installer->InstallCrx(path);
[email protected]7577a5c52009-07-30 06:21:58914}
915
[email protected]d11c8e92009-10-20 23:26:40916void ExtensionsService::ReportExtensionLoadError(
917 const FilePath& extension_path,
918 const std::string &error,
919 NotificationType type,
920 bool be_noisy) {
921 NotificationService* service = NotificationService::current();
922 service->Notify(type,
[email protected]24e7a9d2009-11-04 11:11:34923 Source<Profile>(profile_),
[email protected]d11c8e92009-10-20 23:26:40924 Details<const std::string>(&error));
925
926 // TODO(port): note that this isn't guaranteed to work properly on Linux.
[email protected]99efb7b12009-12-18 02:39:16927 std::string path_str = WideToUTF8(extension_path.ToWStringHack());
[email protected]d11c8e92009-10-20 23:26:40928 std::string message = StringPrintf("Could not load extension from '%s'. %s",
929 path_str.c_str(), error.c_str());
930 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
931}
932
[email protected]4814b512009-11-07 00:12:29933void ExtensionsService::Observe(NotificationType type,
934 const NotificationSource& source,
935 const NotificationDetails& details) {
936 switch (type.value) {
937 case NotificationType::EXTENSION_HOST_DID_STOP_LOADING: {
938 ExtensionHost* host = Details<ExtensionHost>(details).ptr();
939 OrphanedDevTools::iterator iter =
940 orphaned_dev_tools_.find(host->extension()->id());
941 if (iter == orphaned_dev_tools_.end())
942 return;
943
944 DevToolsManager::GetInstance()->AttachClientHost(
945 iter->second, host->render_view_host());
946 orphaned_dev_tools_.erase(iter);
947 break;
948 }
949
[email protected]a4ed6282009-12-14 20:51:16950 case NotificationType::EXTENSION_PROCESS_TERMINATED: {
[email protected]31f77262009-12-02 20:48:53951 DCHECK_EQ(profile_, Source<Profile>(source).ptr());
[email protected]a4ed6282009-12-14 20:51:16952
[email protected]31f77262009-12-02 20:48:53953 // Unload the entire extension. We want it to be in a consistent state:
954 // either fully working or not loaded at all, but never half-crashed.
[email protected]597cceb2010-03-22 16:27:01955 UnloadExtension(Details<ExtensionHost>(details).ptr()->extension()->id());
[email protected]31f77262009-12-02 20:48:53956 break;
957 }
958
[email protected]4814b512009-11-07 00:12:29959 default:
960 NOTREACHED() << "Unexpected notification type.";
961 }
962}
963
964
[email protected]6014d672008-12-05 00:38:25965// ExtensionsServicesBackend
966
[email protected]894bb502009-05-21 22:39:57967ExtensionsServiceBackend::ExtensionsServiceBackend(
[email protected]95d29192009-10-30 01:49:06968 const FilePath& install_directory)
[email protected]0c7bc4b2009-05-30 01:47:08969 : frontend_(NULL),
970 install_directory_(install_directory),
[email protected]95d29192009-10-30 01:49:06971 alert_on_error_(false) {
[email protected]7577a5c52009-07-30 06:21:58972 // TODO(aa): This ends up doing blocking IO on the UI thread because it reads
973 // pref data in the ctor and that is called on the UI thread. Would be better
974 // to re-read data each time we list external extensions, anyway.
[email protected]a1257b12009-06-12 02:51:34975 external_extension_providers_[Extension::EXTERNAL_PREF] =
[email protected]da50530a2009-06-15 17:43:01976 linked_ptr<ExternalExtensionProvider>(
[email protected]27b985d2009-06-25 17:53:15977 new ExternalPrefExtensionProvider());
[email protected]a1257b12009-06-12 02:51:34978#if defined(OS_WIN)
979 external_extension_providers_[Extension::EXTERNAL_REGISTRY] =
[email protected]da50530a2009-06-15 17:43:01980 linked_ptr<ExternalExtensionProvider>(
981 new ExternalRegistryExtensionProvider());
[email protected]a1257b12009-06-12 02:51:34982#endif
983}
984
985ExtensionsServiceBackend::~ExtensionsServiceBackend() {
[email protected]894bb502009-05-21 22:39:57986}
987
[email protected]b0beaa662009-02-26 00:04:15988void ExtensionsServiceBackend::LoadSingleExtension(
[email protected]894bb502009-05-21 22:39:57989 const FilePath& path_in, scoped_refptr<ExtensionsService> frontend) {
[email protected]b0beaa662009-02-26 00:04:15990 frontend_ = frontend;
991
992 // Explicit UI loads are always noisy.
993 alert_on_error_ = true;
994
[email protected]cc5da332009-03-04 08:02:51995 FilePath extension_path = path_in;
[email protected]f36fa4fb2009-06-19 18:23:50996 file_util::AbsolutePath(&extension_path);
[email protected]bf24d2c2009-02-24 23:07:45997
998 LOG(INFO) << "Loading single extension from " <<
[email protected]99efb7b12009-12-18 02:39:16999 extension_path.BaseName().value();
[email protected]bf24d2c2009-02-24 23:07:451000
[email protected]ab6f2b22009-07-28 23:28:371001 std::string error;
1002 Extension* extension = extension_file_util::LoadExtension(
1003 extension_path,
1004 false, // Don't require id
1005 &error);
1006
1007 if (!extension) {
1008 ReportExtensionLoadError(extension_path, error);
1009 return;
[email protected]0877fd92009-02-03 16:34:061010 }
[email protected]ab6f2b22009-07-28 23:28:371011
1012 extension->set_location(Extension::LOAD);
[email protected]e8c729a2010-03-09 19:55:191013
1014 // Report this as an installed extension so that it gets remembered in the
1015 // prefs.
1016 ChromeThread::PostTask(
1017 ChromeThread::UI, FROM_HERE,
1018 NewRunnableMethod(frontend_, &ExtensionsService::OnExtensionInstalled,
1019 extension, true));
[email protected]0877fd92009-02-03 16:34:061020}
1021
[email protected]6014d672008-12-05 00:38:251022void ExtensionsServiceBackend::ReportExtensionLoadError(
[email protected]cc5da332009-03-04 08:02:511023 const FilePath& extension_path, const std::string &error) {
[email protected]95d29192009-10-30 01:49:061024 ChromeThread::PostTask(
1025 ChromeThread::UI, FROM_HERE,
1026 NewRunnableMethod(
1027 frontend_,
[email protected]d11c8e92009-10-20 23:26:401028 &ExtensionsService::ReportExtensionLoadError, extension_path,
1029 error, NotificationType::EXTENSION_INSTALL_ERROR, alert_on_error_));
[email protected]6014d672008-12-05 00:38:251030}
1031
[email protected]a1257b12009-06-12 02:51:341032bool ExtensionsServiceBackend::LookupExternalExtension(
1033 const std::string& id, Version** version, Extension::Location* location) {
1034 scoped_ptr<Version> extension_version;
1035 for (ProviderMap::const_iterator i = external_extension_providers_.begin();
1036 i != external_extension_providers_.end(); ++i) {
[email protected]da50530a2009-06-15 17:43:011037 const ExternalExtensionProvider* provider = i->second.get();
[email protected]a1257b12009-06-12 02:51:341038 extension_version.reset(provider->RegisteredVersion(id, location));
1039 if (extension_version.get()) {
1040 if (version)
1041 *version = extension_version.release();
1042 return true;
1043 }
1044 }
1045 return false;
1046}
1047
[email protected]b0beaa662009-02-26 00:04:151048// Some extensions will autoupdate themselves externally from Chrome. These
1049// are typically part of some larger client application package. To support
[email protected]25b34332009-06-05 21:53:191050// these, the extension will register its location in the the preferences file
1051// (and also, on Windows, in the registry) and this code will periodically
[email protected]b0beaa662009-02-26 00:04:151052// check that location for a .crx file, which it will then install locally if
1053// a new version is available.
1054void ExtensionsServiceBackend::CheckForExternalUpdates(
[email protected]894bb502009-05-21 22:39:571055 std::set<std::string> ids_to_ignore,
1056 scoped_refptr<ExtensionsService> frontend) {
[email protected]b0beaa662009-02-26 00:04:151057 // Note that this installation is intentionally silent (since it didn't
1058 // go through the front-end). Extensions that are registered in this
1059 // way are effectively considered 'pre-bundled', and so implicitly
1060 // trusted. In general, if something has HKLM or filesystem access,
1061 // they could install an extension manually themselves anyway.
1062 alert_on_error_ = false;
1063 frontend_ = frontend;
[email protected]b0beaa662009-02-26 00:04:151064
[email protected]a1257b12009-06-12 02:51:341065 // Ask each external extension provider to give us a call back for each
1066 // extension they know about. See OnExternalExtensionFound.
1067 for (ProviderMap::const_iterator i = external_extension_providers_.begin();
1068 i != external_extension_providers_.end(); ++i) {
[email protected]da50530a2009-06-15 17:43:011069 ExternalExtensionProvider* provider = i->second.get();
[email protected]a1257b12009-06-12 02:51:341070 provider->VisitRegisteredExtension(this, ids_to_ignore);
[email protected]25b34332009-06-05 21:53:191071 }
[email protected]b0beaa662009-02-26 00:04:151072}
1073
[email protected]ae09ca62009-08-21 19:46:461074void ExtensionsServiceBackend::CheckExternalUninstall(
1075 scoped_refptr<ExtensionsService> frontend, const std::string& id,
1076 Extension::Location location) {
[email protected]a1257b12009-06-12 02:51:341077 // Check if the providers know about this extension.
1078 ProviderMap::const_iterator i = external_extension_providers_.find(location);
[email protected]ae09ca62009-08-21 19:46:461079 if (i == external_extension_providers_.end()) {
1080 NOTREACHED() << "CheckExternalUninstall called for non-external extension "
1081 << location;
1082 return;
[email protected]b0beaa662009-02-26 00:04:151083 }
[email protected]25b34332009-06-05 21:53:191084
[email protected]ae09ca62009-08-21 19:46:461085 scoped_ptr<Version> version;
1086 version.reset(i->second->RegisteredVersion(id, NULL));
1087 if (version.get())
1088 return; // Yup, known extension, don't uninstall.
1089
1090 // This is an external extension that we don't have registered. Uninstall.
[email protected]95d29192009-10-30 01:49:061091 ChromeThread::PostTask(
1092 ChromeThread::UI, FROM_HERE,
1093 NewRunnableMethod(
1094 frontend.get(), &ExtensionsService::UninstallExtension, id, true));
[email protected]b0beaa662009-02-26 00:04:151095}
1096
[email protected]a1257b12009-06-12 02:51:341097void ExtensionsServiceBackend::ClearProvidersForTesting() {
1098 external_extension_providers_.clear();
1099}
1100
1101void ExtensionsServiceBackend::SetProviderForTesting(
1102 Extension::Location location,
1103 ExternalExtensionProvider* test_provider) {
1104 DCHECK(test_provider);
[email protected]da50530a2009-06-15 17:43:011105 external_extension_providers_[location] =
1106 linked_ptr<ExternalExtensionProvider>(test_provider);
[email protected]a1257b12009-06-12 02:51:341107}
1108
1109void ExtensionsServiceBackend::OnExternalExtensionFound(
[email protected]7577a5c52009-07-30 06:21:581110 const std::string& id, const Version* version, const FilePath& path,
1111 Extension::Location location) {
[email protected]95d29192009-10-30 01:49:061112 ChromeThread::PostTask(
1113 ChromeThread::UI, FROM_HERE,
1114 NewRunnableMethod(
1115 frontend_, &ExtensionsService::OnExternalExtensionFound, id,
1116 version->GetString(), path, location));
[email protected]cc655912009-01-29 23:19:191117}
[email protected]c6d474f82009-12-16 21:11:061118
[email protected]2111b1a2010-03-12 18:12:441119void ExtensionsServiceBackend::ReloadExtensionManifests(
[email protected]c6d474f82009-12-16 21:11:061120 ExtensionPrefs::ExtensionsInfo* extensions_to_reload,
1121 base::TimeTicks start_time,
1122 scoped_refptr<ExtensionsService> frontend) {
1123 frontend_ = frontend;
1124
1125 for (size_t i = 0; i < extensions_to_reload->size(); ++i) {
1126 ExtensionInfo* info = extensions_to_reload->at(i).get();
[email protected]2111b1a2010-03-12 18:12:441127 if (!ShouldReloadExtensionManifest(*info))
[email protected]c6d474f82009-12-16 21:11:061128 continue;
1129
1130 // We need to reload original manifest in order to localize properly.
1131 std::string error;
1132 scoped_ptr<Extension> extension(extension_file_util::LoadExtension(
1133 info->extension_path, false, &error));
1134
1135 if (extension.get())
1136 extensions_to_reload->at(i)->extension_manifest.reset(
1137 static_cast<DictionaryValue*>(
1138 extension->manifest_value()->DeepCopy()));
1139 }
1140
1141 // Finish installing on UI thread.
1142 ChromeThread::PostTask(
1143 ChromeThread::UI, FROM_HERE,
1144 NewRunnableMethod(
1145 frontend_,
1146 &ExtensionsService::ContinueLoadAllExtensions,
1147 extensions_to_reload,
1148 start_time,
1149 true));
1150}