blob: 62432b5ece9e462c5cf4ad3d9ef1a1d5ac893476 [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]5c8516202010-03-18 21:43:34219 installer->set_force_app_origin_to_download_url(true);
220 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));
341 DCHECK(manifest.get());
342
343 scoped_ptr<Extension> extension(new Extension(it->root_directory));
344 extension->set_location(Extension::COMPONENT);
345
346 std::string error;
347 if (!extension->InitFromValue(
348 *static_cast<DictionaryValue*>(manifest.get()),
349 true, // require key
350 &error)) {
351 NOTREACHED();
352 return;
353 }
354
355 OnExtensionLoaded(extension.release(), false); // Don't allow privilege
356 // increase.
357 }
358}
359
[email protected]9f1087e2009-06-15 17:29:32360void ExtensionsService::LoadAllExtensions() {
[email protected]cc2c3432009-11-06 17:24:36361 base::TimeTicks start_time = base::TimeTicks::Now();
362
[email protected]1952c7d2010-03-04 23:48:34363 // Load any component extensions.
364 LoadComponentExtensions();
365
[email protected]e72e8eb82009-06-18 17:21:51366 // Load the previously installed extensions.
[email protected]c6d474f82009-12-16 21:11:06367 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(
368 ExtensionPrefs::CollectExtensionsInfo(extension_prefs_.get()));
369
370 // If any extensions need localization, we bounce them all to the file thread
371 // for re-reading and localization.
372 for (size_t i = 0; i < info->size(); ++i) {
[email protected]2111b1a2010-03-12 18:12:44373 if (ShouldReloadExtensionManifest(*info->at(i))) {
[email protected]c6d474f82009-12-16 21:11:06374 ChromeThread::PostTask(
375 ChromeThread::FILE, FROM_HERE, NewRunnableMethod(
376 backend_.get(),
[email protected]2111b1a2010-03-12 18:12:44377 &ExtensionsServiceBackend::ReloadExtensionManifests,
[email protected]c6d474f82009-12-16 21:11:06378 info.release(), // Callee takes ownership of the memory.
379 start_time,
380 scoped_refptr<ExtensionsService>(this)));
381 return;
382 }
383 }
384
385 // Don't update prefs.
386 // Callee takes ownership of the memory.
387 ContinueLoadAllExtensions(info.release(), start_time, false);
388}
389
390void ExtensionsService::ContinueLoadAllExtensions(
391 ExtensionPrefs::ExtensionsInfo* extensions_info,
392 base::TimeTicks start_time,
393 bool write_to_prefs) {
394 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(extensions_info);
395
396 for (size_t i = 0; i < info->size(); ++i) {
397 LoadInstalledExtension(*info->at(i), write_to_prefs);
398 }
399
[email protected]ae09ca62009-08-21 19:46:46400 OnLoadedInstalledExtensions();
[email protected]cc2c3432009-11-06 17:24:36401
402 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadAll", extensions_.size());
403 UMA_HISTOGRAM_COUNTS_100("Extensions.Disabled", disabled_extensions_.size());
404
[email protected]1952c7d2010-03-04 23:48:34405 UMA_HISTOGRAM_TIMES("Extensions.LoadAllTime",
406 base::TimeTicks::Now() - start_time);
[email protected]cc2c3432009-11-06 17:24:36407
[email protected]1952c7d2010-03-04 23:48:34408 int user_script_count = 0;
409 int extension_count = 0;
410 int theme_count = 0;
411 int external_count = 0;
412 int page_action_count = 0;
413 int browser_action_count = 0;
414 ExtensionList::iterator ex;
415 for (ex = extensions_.begin(); ex != extensions_.end(); ++ex) {
416 // Don't count component extensions, since they are only extensions as an
417 // implementation detail.
418 if ((*ex)->location() == Extension::COMPONENT)
419 continue;
420
[email protected]e8c729a2010-03-09 19:55:19421 // Don't count unpacked extensions, since they're a developer-specific
422 // feature.
423 if ((*ex)->location() == Extension::LOAD)
424 continue;
425
[email protected]1952c7d2010-03-04 23:48:34426 if ((*ex)->IsTheme()) {
427 theme_count++;
428 } else if ((*ex)->converted_from_user_script()) {
429 user_script_count++;
430 } else {
431 extension_count++;
[email protected]cc2c3432009-11-06 17:24:36432 }
[email protected]1952c7d2010-03-04 23:48:34433 if (Extension::IsExternalLocation((*ex)->location())) {
434 external_count++;
435 }
436 if ((*ex)->page_action() != NULL) {
437 page_action_count++;
438 }
439 if ((*ex)->browser_action() != NULL) {
440 browser_action_count++;
441 }
[email protected]cc2c3432009-11-06 17:24:36442 }
[email protected]1952c7d2010-03-04 23:48:34443 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExtension", extension_count);
444 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadUserScript", user_script_count);
445 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadTheme", theme_count);
446 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExternal", external_count);
447 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadPageAction", page_action_count);
448 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadBrowserAction",
449 browser_action_count);
[email protected]ae09ca62009-08-21 19:46:46450}
451
[email protected]c6d474f82009-12-16 21:11:06452void ExtensionsService::LoadInstalledExtension(const ExtensionInfo& info,
453 bool write_to_prefs) {
[email protected]ae09ca62009-08-21 19:46:46454 std::string error;
455 Extension* extension = NULL;
[email protected]c6d474f82009-12-16 21:11:06456 if (info.extension_manifest.get()) {
457 scoped_ptr<Extension> tmp(new Extension(info.extension_path));
[email protected]e8c729a2010-03-09 19:55:19458 bool require_key = info.extension_location != Extension::LOAD;
459 if (tmp->InitFromValue(*info.extension_manifest, require_key, &error))
[email protected]ae09ca62009-08-21 19:46:46460 extension = tmp.release();
[email protected]ae09ca62009-08-21 19:46:46461 } else {
[email protected]c6d474f82009-12-16 21:11:06462 error = errors::kManifestUnreadable;
[email protected]ae09ca62009-08-21 19:46:46463 }
464
465 if (!extension) {
[email protected]c6d474f82009-12-16 21:11:06466 ReportExtensionLoadError(info.extension_path,
[email protected]d11c8e92009-10-20 23:26:40467 error,
468 NotificationType::EXTENSION_INSTALL_ERROR,
469 false);
[email protected]ae09ca62009-08-21 19:46:46470 return;
471 }
472
[email protected]c6d474f82009-12-16 21:11:06473 extension->set_location(info.extension_location);
474
475 if (write_to_prefs)
476 extension_prefs_->UpdateManifest(extension);
477
[email protected]2a409532009-08-28 19:39:44478 OnExtensionLoaded(extension, true);
[email protected]ae09ca62009-08-21 19:46:46479
[email protected]c6d474f82009-12-16 21:11:06480 if (info.extension_location == Extension::EXTERNAL_PREF ||
481 info.extension_location == Extension::EXTERNAL_REGISTRY) {
[email protected]95d29192009-10-30 01:49:06482 ChromeThread::PostTask(
483 ChromeThread::FILE, FROM_HERE,
484 NewRunnableMethod(
[email protected]c6d474f82009-12-16 21:11:06485 backend_.get(),
486 &ExtensionsServiceBackend::CheckExternalUninstall,
487 scoped_refptr<ExtensionsService>(this),
488 info.extension_id,
489 info.extension_location));
[email protected]ae09ca62009-08-21 19:46:46490 }
[email protected]9f1087e2009-06-15 17:29:32491}
492
[email protected]62d30f42009-10-01 22:36:06493void ExtensionsService::NotifyExtensionLoaded(Extension* extension) {
494 LOG(INFO) << "Sending EXTENSION_LOADED";
495
496 // The ChromeURLRequestContext needs to be first to know that the extension
497 // was loaded, otherwise a race can arise where a renderer that is created
498 // for the extension may try to load an extension URL with an extension id
499 // that the request context doesn't yet know about.
500 if (profile_ && !profile_->IsOffTheRecord()) {
[email protected]be180c802009-10-23 06:33:31501 ChromeURLRequestContextGetter* context_getter =
502 static_cast<ChromeURLRequestContextGetter*>(
503 profile_->GetRequestContext());
504 if (context_getter) {
[email protected]95d29192009-10-30 01:49:06505 ChromeThread::PostTask(
506 ChromeThread::IO, FROM_HERE,
507 NewRunnableMethod(
508 context_getter,
509 &ChromeURLRequestContextGetter::OnNewExtensions,
510 extension->id(),
[email protected]0ce3f5982010-01-28 23:04:27511 new ChromeURLRequestContext::ExtensionInfo(
512 extension->path(),
513 extension->default_locale(),
[email protected]b30e0dd2010-01-29 23:33:21514 extension->app_extent(),
[email protected]0ce3f5982010-01-28 23:04:27515 extension->api_permissions())));
[email protected]62d30f42009-10-01 22:36:06516 }
[email protected]24b538a2010-02-27 01:22:44517
518 // Check if this permission requires unlimited storage quota
519 if (extension->HasApiPermission(Extension::kUnlimitedStoragePermission)) {
520 string16 origin_identifier =
521 webkit_database::DatabaseUtil::GetOriginIdentifier(extension->url());
522 ChromeThread::PostTask(
523 ChromeThread::FILE, FROM_HERE,
524 NewRunnableMethod(
525 profile_->GetDatabaseTracker(),
526 &webkit_database::DatabaseTracker::SetOriginQuotaInMemory,
527 origin_identifier,
528 kint64max));
529 }
[email protected]62d30f42009-10-01 22:36:06530 }
531
532 NotificationService::current()->Notify(
533 NotificationType::EXTENSION_LOADED,
[email protected]24e7a9d2009-11-04 11:11:34534 Source<Profile>(profile_),
[email protected]62d30f42009-10-01 22:36:06535 Details<Extension>(extension));
536}
537
538void ExtensionsService::NotifyExtensionUnloaded(Extension* extension) {
539 LOG(INFO) << "Sending EXTENSION_UNLOADED";
540
541 NotificationService::current()->Notify(
542 NotificationType::EXTENSION_UNLOADED,
[email protected]24e7a9d2009-11-04 11:11:34543 Source<Profile>(profile_),
[email protected]62d30f42009-10-01 22:36:06544 Details<Extension>(extension));
545
546 if (profile_ && !profile_->IsOffTheRecord()) {
[email protected]be180c802009-10-23 06:33:31547 ChromeURLRequestContextGetter* context_getter =
548 static_cast<ChromeURLRequestContextGetter*>(
549 profile_->GetRequestContext());
550 if (context_getter) {
[email protected]95d29192009-10-30 01:49:06551 ChromeThread::PostTask(
552 ChromeThread::IO, FROM_HERE,
[email protected]be180c802009-10-23 06:33:31553 NewRunnableMethod(
554 context_getter,
555 &ChromeURLRequestContextGetter::OnUnloadedExtension,
556 extension->id()));
[email protected]62d30f42009-10-01 22:36:06557 }
558 }
559}
560
[email protected]6b75ec32009-08-14 06:37:18561void ExtensionsService::UpdateExtensionBlacklist(
562 const std::vector<std::string>& blacklist) {
563 // Use this set to indicate if an extension in the blacklist has been used.
564 std::set<std::string> blacklist_set;
565 for (unsigned int i = 0; i < blacklist.size(); ++i) {
566 if (Extension::IdIsValid(blacklist[i])) {
567 blacklist_set.insert(blacklist[i]);
568 }
569 }
570 extension_prefs_->UpdateBlacklist(blacklist_set);
571 std::vector<std::string> to_be_removed;
572 // Loop current extensions, unload installed extensions.
573 for (ExtensionList::const_iterator iter = extensions_.begin();
574 iter != extensions_.end(); ++iter) {
575 Extension* extension = (*iter);
576 if (blacklist_set.find(extension->id()) != blacklist_set.end()) {
577 to_be_removed.push_back(extension->id());
578 }
579 }
580
581 // UnloadExtension will change the extensions_ list. So, we should
582 // call it outside the iterator loop.
583 for (unsigned int i = 0; i < to_be_removed.size(); ++i) {
584 UnloadExtension(to_be_removed[i]);
585 }
586}
587
[email protected]5ef47ec2010-01-28 05:58:05588void ExtensionsService::SetLastPingDay(const std::string& extension_id,
589 const base::Time& time) {
590 extension_prefs_->SetLastPingDay(extension_id, time);
591}
592
593base::Time ExtensionsService::LastPingDay(const std::string& extension_id) {
594 return extension_prefs_->LastPingDay(extension_id);
595}
596
[email protected]cb0ce1e022010-03-10 19:54:41597bool ExtensionsService::IsIncognitoEnabled(const Extension* extension) {
598 // If this is a component extension we always allow it to work in incognito
599 // mode.
600 if (extension->location() == Extension::COMPONENT)
601 return true;
602
603 // Check the prefs.
604 return extension_prefs_->IsIncognitoEnabled(extension->id());
[email protected]db7331a2010-02-25 22:10:50605}
[email protected]55a35692010-02-11 23:25:21606
[email protected]cb0ce1e022010-03-10 19:54:41607void ExtensionsService::SetIsIncognitoEnabled(Extension* extension,
[email protected]db7331a2010-02-25 22:10:50608 bool enabled) {
[email protected]cb0ce1e022010-03-10 19:54:41609 extension_prefs_->SetIsIncognitoEnabled(extension->id(), enabled);
[email protected]c1499f3d2010-03-05 00:33:24610
611 // Broadcast unloaded and loaded events to update browser state.
612 NotifyExtensionUnloaded(extension);
613 NotifyExtensionLoaded(extension);
[email protected]55a35692010-02-11 23:25:21614}
615
[email protected]93fd78f42009-07-10 16:43:17616void ExtensionsService::CheckForExternalUpdates() {
[email protected]9f1087e2009-06-15 17:29:32617 // This installs or updates externally provided extensions.
[email protected]7577a5c52009-07-30 06:21:58618 // TODO(aa): Why pass this list into the provider, why not just filter it
619 // later?
[email protected]9f1087e2009-06-15 17:29:32620 std::set<std::string> killed_extensions;
[email protected]e72e8eb82009-06-18 17:21:51621 extension_prefs_->GetKilledExtensionIds(&killed_extensions);
[email protected]95d29192009-10-30 01:49:06622 ChromeThread::PostTask(
623 ChromeThread::FILE, FROM_HERE,
624 NewRunnableMethod(
625 backend_.get(), &ExtensionsServiceBackend::CheckForExternalUpdates,
626 killed_extensions, scoped_refptr<ExtensionsService>(this)));
[email protected]9f1087e2009-06-15 17:29:32627}
628
629void ExtensionsService::UnloadExtension(const std::string& extension_id) {
[email protected]27e469a2010-01-11 20:35:09630 // Make sure the extension gets deleted after we return from this function.
[email protected]0c6da502009-08-14 22:32:39631 scoped_ptr<Extension> extension(
632 GetExtensionByIdInternal(extension_id, true, true));
[email protected]631cf822009-05-15 07:01:25633
[email protected]894bb502009-05-21 22:39:57634 // Callers should not send us nonexistant extensions.
[email protected]0c6da502009-08-14 22:32:39635 CHECK(extension.get());
636
[email protected]1eb175082010-02-10 09:26:16637 // Keep information about the extension so that we can reload it later
638 // even if it's not permanently installed.
639 unloaded_extension_paths_[extension->id()] = extension->path();
640
[email protected]86c008e82009-08-28 20:26:05641 ExtensionDOMUI::UnregisterChromeURLOverrides(profile_,
642 extension->GetChromeURLOverrides());
643
[email protected]0c6da502009-08-14 22:32:39644 ExtensionList::iterator iter = std::find(disabled_extensions_.begin(),
645 disabled_extensions_.end(),
646 extension.get());
647 if (iter != disabled_extensions_.end()) {
[email protected]0c6da502009-08-14 22:32:39648 disabled_extensions_.erase(iter);
[email protected]866930682009-08-18 22:53:47649 NotificationService::current()->Notify(
650 NotificationType::EXTENSION_UNLOADED_DISABLED,
[email protected]24e7a9d2009-11-04 11:11:34651 Source<Profile>(profile_),
[email protected]866930682009-08-18 22:53:47652 Details<Extension>(extension.get()));
[email protected]0c6da502009-08-14 22:32:39653 return;
654 }
655
656 iter = std::find(extensions_.begin(), extensions_.end(), extension.get());
[email protected]894bb502009-05-21 22:39:57657
[email protected]631cf822009-05-15 07:01:25658 // Remove the extension from our list.
659 extensions_.erase(iter);
660
[email protected]62d30f42009-10-01 22:36:06661 NotifyExtensionUnloaded(extension.get());
[email protected]aab98a52009-12-02 03:22:35662 UpdateActiveExtensionsInCrashReporter();
[email protected]631cf822009-05-15 07:01:25663}
664
[email protected]9f1087e2009-06-15 17:29:32665void ExtensionsService::UnloadAllExtensions() {
666 ExtensionList::iterator iter;
[email protected]c6e4a3412009-06-24 15:45:29667 for (iter = extensions_.begin(); iter != extensions_.end(); ++iter)
[email protected]9f1087e2009-06-15 17:29:32668 delete *iter;
[email protected]9f1087e2009-06-15 17:29:32669 extensions_.clear();
[email protected]c6e4a3412009-06-24 15:45:29670
671 // TODO(erikkay) should there be a notification for this? We can't use
672 // EXTENSION_UNLOADED since that implies that the extension has been disabled
673 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:32674}
675
676void ExtensionsService::ReloadExtensions() {
677 UnloadAllExtensions();
678 LoadAllExtensions();
679}
680
681void ExtensionsService::GarbageCollectExtensions() {
[email protected]c6d474f82009-12-16 21:11:06682 InstalledExtensionSet installed(extension_prefs_.get());
[email protected]95d29192009-10-30 01:49:06683 ChromeThread::PostTask(
684 ChromeThread::FILE, FROM_HERE,
685 NewRunnableFunction(
686 &extension_file_util::GarbageCollectExtensions, install_directory_,
[email protected]4559a7d2009-12-02 01:42:41687 installed.extensions(), installed.versions()));
[email protected]3cf4f0992009-02-03 23:00:30688}
689
[email protected]e72e8eb82009-06-18 17:21:51690void ExtensionsService::OnLoadedInstalledExtensions() {
[email protected]e81dba32009-06-19 20:19:13691 ready_ = true;
[email protected]93fd78f42009-07-10 16:43:17692 if (updater_.get()) {
693 updater_->Start();
694 }
[email protected]e72e8eb82009-06-18 17:21:51695 NotificationService::current()->Notify(
696 NotificationType::EXTENSIONS_READY,
[email protected]24e7a9d2009-11-04 11:11:34697 Source<Profile>(profile_),
[email protected]e72e8eb82009-06-18 17:21:51698 NotificationService::NoDetails());
699}
700
[email protected]2a409532009-08-28 19:39:44701void ExtensionsService::OnExtensionLoaded(Extension* extension,
702 bool allow_privilege_increase) {
[email protected]ae09ca62009-08-21 19:46:46703 // Ensure extension is deleted unless we transfer ownership.
704 scoped_ptr<Extension> scoped_extension(extension);
[email protected]9f1087e2009-06-15 17:29:32705
[email protected]1eb175082010-02-10 09:26:16706 // The extension is now loaded, remove its data from unloaded extension map.
707 unloaded_extension_paths_.erase(extension->id());
708
[email protected]ceefd3d2010-03-12 09:10:29709 if (extension->IsApp() &&
710 !CommandLine::ForCurrentProcess()->HasSwitch(
711 switches::kEnableExtensionApps)) {
712 ReportExtensionLoadError(extension->path(), errors::kAppsDisabled,
713 NotificationType::EXTENSION_INSTALL_ERROR,
714 true); // be noisy
715 return;
716 }
717
718 // TODO(aa): Need to re-evaluate this branch. Does this still make sense now
719 // that extensions are enabled by default?
[email protected]ae09ca62009-08-21 19:46:46720 if (extensions_enabled() ||
721 extension->IsTheme() ||
722 extension->location() == Extension::LOAD ||
723 Extension::IsExternalLocation(extension->location())) {
724 Extension* old = GetExtensionByIdInternal(extension->id(), true, true);
725 if (old) {
726 if (extension->version()->CompareTo(*(old->version())) > 0) {
[email protected]2a409532009-08-28 19:39:44727 bool allow_silent_upgrade =
728 allow_privilege_increase || !Extension::IsPrivilegeIncrease(
729 old, extension);
[email protected]0c6da502009-08-14 22:32:39730
[email protected]1e8c93f2010-02-08 22:58:31731 // Extensions get upgraded if silent upgrades are allowed, otherwise
732 // they get disabled.
733 if (allow_silent_upgrade) {
734 old->set_being_upgraded(true);
735 extension->set_being_upgraded(true);
736 }
737
[email protected]ae09ca62009-08-21 19:46:46738 // To upgrade an extension in place, unload the old one and
739 // then load the new one.
740 UnloadExtension(old->id());
741 old = NULL;
[email protected]0c6da502009-08-14 22:32:39742
[email protected]b24d8312009-08-27 06:47:46743 if (!allow_silent_upgrade) {
[email protected]6d27a7b2009-12-18 23:25:45744 // Extension has changed permissions significantly. Disable it. We
745 // send a notification below.
[email protected]ae09ca62009-08-21 19:46:46746 extension_prefs_->SetExtensionState(extension, Extension::DISABLED);
[email protected]7d845862010-01-04 21:28:57747 extension_prefs_->SetShowInstallWarningOnEnable(extension, true);
[email protected]9f1087e2009-06-15 17:29:32748 }
[email protected]ae09ca62009-08-21 19:46:46749 } else {
750 // We already have the extension of the same or older version.
[email protected]d11c8e92009-10-20 23:26:40751 std::string error_message("Duplicate extension load attempt: ");
752 error_message += extension->id();
753 LOG(WARNING) << error_message;
754 ReportExtensionLoadError(extension->path(),
755 error_message,
756 NotificationType::EXTENSION_OVERINSTALL_ERROR,
757 false);
[email protected]ae09ca62009-08-21 19:46:46758 return;
[email protected]0c6da502009-08-14 22:32:39759 }
[email protected]ba74f352009-06-11 18:54:45760 }
[email protected]86a274072009-06-11 02:06:45761
[email protected]ae09ca62009-08-21 19:46:46762 switch (extension_prefs_->GetExtensionState(extension->id())) {
763 case Extension::ENABLED:
764 extensions_.push_back(scoped_extension.release());
765
[email protected]aeb53b32009-10-29 07:34:45766 // We delay starting up the browser event router until at least one
767 // extension that needs it is loaded.
768 if (extension->HasApiPermission(Extension::kTabPermission)) {
769 ExtensionBrowserEventRouter::GetInstance()->Init();
770 }
[email protected]840b0db2009-11-20 03:00:38771 if (extension->HasApiPermission(Extension::kBookmarkPermission)) {
772 ExtensionBookmarkEventRouter::GetSingleton()->Observe(
773 profile_->GetBookmarkModel());
774 }
[email protected]aeb53b32009-10-29 07:34:45775
[email protected]62d30f42009-10-01 22:36:06776 NotifyExtensionLoaded(extension);
[email protected]ae09ca62009-08-21 19:46:46777
[email protected]e8c729a2010-03-09 19:55:19778 ExtensionDOMUI::RegisterChromeURLOverrides(profile_,
779 extension->GetChromeURLOverrides());
[email protected]ae09ca62009-08-21 19:46:46780 break;
781 case Extension::DISABLED:
[email protected]6d27a7b2009-12-18 23:25:45782 disabled_extensions_.push_back(scoped_extension.release());
[email protected]d11c8e92009-10-20 23:26:40783 NotificationService::current()->Notify(
784 NotificationType::EXTENSION_UPDATE_DISABLED,
[email protected]24e7a9d2009-11-04 11:11:34785 Source<Profile>(profile_),
[email protected]d11c8e92009-10-20 23:26:40786 Details<Extension>(extension));
[email protected]ae09ca62009-08-21 19:46:46787 break;
788 default:
[email protected]d11c8e92009-10-20 23:26:40789 NOTREACHED();
[email protected]ae09ca62009-08-21 19:46:46790 break;
[email protected]811f3432009-07-25 19:38:21791 }
[email protected]e72e8eb82009-06-18 17:21:51792 }
[email protected]aab98a52009-12-02 03:22:35793
[email protected]1e8c93f2010-02-08 22:58:31794 extension->set_being_upgraded(false);
795
[email protected]aab98a52009-12-02 03:22:35796 UpdateActiveExtensionsInCrashReporter();
797}
798
799void ExtensionsService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:39800 std::set<std::string> extension_ids;
[email protected]aab98a52009-12-02 03:22:35801 for (size_t i = 0; i < extensions_.size(); ++i) {
802 if (!extensions_[i]->IsTheme())
[email protected]c8865962009-12-16 07:47:39803 extension_ids.insert(extensions_[i]->id());
[email protected]aab98a52009-12-02 03:22:35804 }
805
806 child_process_logging::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:25807}
808
[email protected]2a409532009-08-28 19:39:44809void ExtensionsService::OnExtensionInstalled(Extension* extension,
810 bool allow_privilege_increase) {
[email protected]b6ab96d2009-08-20 18:58:19811 extension_prefs_->OnExtensionInstalled(extension);
[email protected]25b34332009-06-05 21:53:19812
[email protected]4a190632009-05-09 01:07:42813 // If the extension is a theme, tell the profile (and therefore ThemeProvider)
814 // to apply it.
815 if (extension->IsTheme()) {
[email protected]9ceb07342009-07-26 04:09:23816 NotificationService::current()->Notify(
817 NotificationType::THEME_INSTALLED,
[email protected]24e7a9d2009-11-04 11:11:34818 Source<Profile>(profile_),
[email protected]9ceb07342009-07-26 04:09:23819 Details<Extension>(extension));
[email protected]9197f3b2009-06-02 00:49:27820 } else {
821 NotificationService::current()->Notify(
822 NotificationType::EXTENSION_INSTALLED,
[email protected]24e7a9d2009-11-04 11:11:34823 Source<Profile>(profile_),
[email protected]9197f3b2009-06-02 00:49:27824 Details<Extension>(extension));
[email protected]4a190632009-05-09 01:07:42825 }
[email protected]7577a5c52009-07-30 06:21:58826
827 // Also load the extension.
[email protected]2a409532009-08-28 19:39:44828 OnExtensionLoaded(extension, allow_privilege_increase);
[email protected]4a190632009-05-09 01:07:42829}
830
[email protected]7577a5c52009-07-30 06:21:58831void ExtensionsService::OnExtensionOverinstallAttempted(const std::string& id) {
[email protected]61b411612009-11-10 23:17:41832 Extension* extension = GetExtensionById(id, false);
[email protected]4a190632009-05-09 01:07:42833 if (extension && extension->IsTheme()) {
[email protected]9ceb07342009-07-26 04:09:23834 NotificationService::current()->Notify(
835 NotificationType::THEME_INSTALLED,
[email protected]24e7a9d2009-11-04 11:11:34836 Source<Profile>(profile_),
[email protected]9ceb07342009-07-26 04:09:23837 Details<Extension>(extension));
[email protected]91e1bd82009-09-03 22:04:40838 } else {
839 NotificationService::current()->Notify(
840 NotificationType::NO_THEME_DETECTED,
[email protected]24e7a9d2009-11-04 11:11:34841 Source<Profile>(profile_),
[email protected]91e1bd82009-09-03 22:04:40842 NotificationService::NoDetails());
[email protected]4a190632009-05-09 01:07:42843 }
[email protected]cc655912009-01-29 23:19:19844}
845
[email protected]0c6da502009-08-14 22:32:39846Extension* ExtensionsService::GetExtensionByIdInternal(const std::string& id,
847 bool include_enabled,
848 bool include_disabled) {
[email protected]e957fe52009-06-23 16:51:05849 std::string lowercase_id = StringToLowerASCII(id);
[email protected]0c6da502009-08-14 22:32:39850 if (include_enabled) {
851 for (ExtensionList::const_iterator iter = extensions_.begin();
852 iter != extensions_.end(); ++iter) {
853 if ((*iter)->id() == lowercase_id)
854 return *iter;
855 }
856 }
857 if (include_disabled) {
858 for (ExtensionList::const_iterator iter = disabled_extensions_.begin();
859 iter != disabled_extensions_.end(); ++iter) {
860 if ((*iter)->id() == lowercase_id)
861 return *iter;
862 }
[email protected]ce5c4502009-05-06 16:46:11863 }
864 return NULL;
865}
866
[email protected]9f1087e2009-06-15 17:29:32867Extension* ExtensionsService::GetExtensionByURL(const GURL& url) {
868 std::string host = url.host();
[email protected]61b411612009-11-10 23:17:41869 return GetExtensionById(host, false);
[email protected]9f1087e2009-06-15 17:29:32870}
871
[email protected]a1257b12009-06-12 02:51:34872void ExtensionsService::ClearProvidersForTesting() {
[email protected]95d29192009-10-30 01:49:06873 ChromeThread::PostTask(
874 ChromeThread::FILE, FROM_HERE,
875 NewRunnableMethod(
876 backend_.get(), &ExtensionsServiceBackend::ClearProvidersForTesting));
[email protected]a1257b12009-06-12 02:51:34877}
878
879void ExtensionsService::SetProviderForTesting(
880 Extension::Location location, ExternalExtensionProvider* test_provider) {
[email protected]95d29192009-10-30 01:49:06881 ChromeThread::PostTask(
882 ChromeThread::FILE, FROM_HERE,
883 NewRunnableMethod(
884 backend_.get(), &ExtensionsServiceBackend::SetProviderForTesting,
885 location, test_provider));
[email protected]a1257b12009-06-12 02:51:34886}
887
[email protected]7577a5c52009-07-30 06:21:58888void ExtensionsService::OnExternalExtensionFound(const std::string& id,
889 const std::string& version,
890 const FilePath& path,
891 Extension::Location location) {
892 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:49893 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:58894 // installed on every startup.
[email protected]61b411612009-11-10 23:17:41895 Extension* existing = GetExtensionById(id, true);
[email protected]a3a63ff82009-08-04 06:44:11896 scoped_ptr<Version> other(Version::GetVersionFromString(version));
[email protected]7577a5c52009-07-30 06:21:58897 if (existing) {
[email protected]a3a63ff82009-08-04 06:44:11898 switch (existing->version()->CompareTo(*other)) {
[email protected]7577a5c52009-07-30 06:21:58899 case -1: // existing version is older, we should upgrade
900 break;
901 case 0: // existing version is same, do nothing
902 return;
903 case 1: // existing version is newer, uh-oh
904 LOG(WARNING) << "Found external version of extension " << id
905 << "that is older than current version. Current version "
906 << "is: " << existing->VersionString() << ". New version "
907 << "is: " << version << ". Keeping current version.";
908 return;
909 }
910 }
911
[email protected]6dfbbf82010-03-12 23:09:16912 scoped_refptr<CrxInstaller> installer(
913 new CrxInstaller(install_directory_,
914 this, // frontend
915 NULL)); // no client (silent install)
916 installer->set_install_source(location);
917 installer->set_expected_id(id);
918 installer->set_allow_privilege_increase(true);
919 installer->InstallCrx(path);
[email protected]7577a5c52009-07-30 06:21:58920}
921
[email protected]d11c8e92009-10-20 23:26:40922void ExtensionsService::ReportExtensionLoadError(
923 const FilePath& extension_path,
924 const std::string &error,
925 NotificationType type,
926 bool be_noisy) {
927 NotificationService* service = NotificationService::current();
928 service->Notify(type,
[email protected]24e7a9d2009-11-04 11:11:34929 Source<Profile>(profile_),
[email protected]d11c8e92009-10-20 23:26:40930 Details<const std::string>(&error));
931
932 // TODO(port): note that this isn't guaranteed to work properly on Linux.
[email protected]99efb7b12009-12-18 02:39:16933 std::string path_str = WideToUTF8(extension_path.ToWStringHack());
[email protected]d11c8e92009-10-20 23:26:40934 std::string message = StringPrintf("Could not load extension from '%s'. %s",
935 path_str.c_str(), error.c_str());
936 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
937}
938
[email protected]4814b512009-11-07 00:12:29939void ExtensionsService::Observe(NotificationType type,
940 const NotificationSource& source,
941 const NotificationDetails& details) {
942 switch (type.value) {
943 case NotificationType::EXTENSION_HOST_DID_STOP_LOADING: {
944 ExtensionHost* host = Details<ExtensionHost>(details).ptr();
945 OrphanedDevTools::iterator iter =
946 orphaned_dev_tools_.find(host->extension()->id());
947 if (iter == orphaned_dev_tools_.end())
948 return;
949
950 DevToolsManager::GetInstance()->AttachClientHost(
951 iter->second, host->render_view_host());
952 orphaned_dev_tools_.erase(iter);
953 break;
954 }
955
[email protected]a4ed6282009-12-14 20:51:16956 case NotificationType::EXTENSION_PROCESS_TERMINATED: {
[email protected]31f77262009-12-02 20:48:53957 DCHECK_EQ(profile_, Source<Profile>(source).ptr());
[email protected]a4ed6282009-12-14 20:51:16958
[email protected]31f77262009-12-02 20:48:53959 ExtensionHost* host = Details<ExtensionHost>(details).ptr();
[email protected]27e469a2010-01-11 20:35:09960 // TODO(phajdan.jr): Change to DCHECK after fixing http://crbug.com/30405.
961 CHECK(profile_->GetExtensionProcessManager()->HasExtensionHost(host));
962
963 // If we hit one of these assertions it means that the host's
[email protected]b0b567bf2010-01-22 19:22:53964 // Extension pointer became invalid. http://crbug.com/30405
[email protected]27e469a2010-01-11 20:35:09965 // TODO(phajdan.jr): Remove excessive debugging after fixing bug 30405.
966 std::string extension_id(host->extension()->id());
967 CHECK(extension_id.length() == 32U);
968 Extension* extension = GetExtensionById(extension_id, true);
[email protected]b0b567bf2010-01-22 19:22:53969 CHECK(extension == host->extension());
[email protected]6f39aee92010-01-26 22:33:58970 if (!extension) {
971 NOTREACHED();
972 return;
973 }
[email protected]31f77262009-12-02 20:48:53974
975 // Unload the entire extension. We want it to be in a consistent state:
976 // either fully working or not loaded at all, but never half-crashed.
[email protected]27e469a2010-01-11 20:35:09977 UnloadExtension(extension_id);
[email protected]31f77262009-12-02 20:48:53978 break;
979 }
980
[email protected]4814b512009-11-07 00:12:29981 default:
982 NOTREACHED() << "Unexpected notification type.";
983 }
984}
985
986
[email protected]6014d672008-12-05 00:38:25987// ExtensionsServicesBackend
988
[email protected]894bb502009-05-21 22:39:57989ExtensionsServiceBackend::ExtensionsServiceBackend(
[email protected]95d29192009-10-30 01:49:06990 const FilePath& install_directory)
[email protected]0c7bc4b2009-05-30 01:47:08991 : frontend_(NULL),
992 install_directory_(install_directory),
[email protected]95d29192009-10-30 01:49:06993 alert_on_error_(false) {
[email protected]7577a5c52009-07-30 06:21:58994 // TODO(aa): This ends up doing blocking IO on the UI thread because it reads
995 // pref data in the ctor and that is called on the UI thread. Would be better
996 // to re-read data each time we list external extensions, anyway.
[email protected]a1257b12009-06-12 02:51:34997 external_extension_providers_[Extension::EXTERNAL_PREF] =
[email protected]da50530a2009-06-15 17:43:01998 linked_ptr<ExternalExtensionProvider>(
[email protected]27b985d2009-06-25 17:53:15999 new ExternalPrefExtensionProvider());
[email protected]a1257b12009-06-12 02:51:341000#if defined(OS_WIN)
1001 external_extension_providers_[Extension::EXTERNAL_REGISTRY] =
[email protected]da50530a2009-06-15 17:43:011002 linked_ptr<ExternalExtensionProvider>(
1003 new ExternalRegistryExtensionProvider());
[email protected]a1257b12009-06-12 02:51:341004#endif
1005}
1006
1007ExtensionsServiceBackend::~ExtensionsServiceBackend() {
[email protected]894bb502009-05-21 22:39:571008}
1009
[email protected]b0beaa662009-02-26 00:04:151010void ExtensionsServiceBackend::LoadSingleExtension(
[email protected]894bb502009-05-21 22:39:571011 const FilePath& path_in, scoped_refptr<ExtensionsService> frontend) {
[email protected]b0beaa662009-02-26 00:04:151012 frontend_ = frontend;
1013
1014 // Explicit UI loads are always noisy.
1015 alert_on_error_ = true;
1016
[email protected]cc5da332009-03-04 08:02:511017 FilePath extension_path = path_in;
[email protected]f36fa4fb2009-06-19 18:23:501018 file_util::AbsolutePath(&extension_path);
[email protected]bf24d2c2009-02-24 23:07:451019
1020 LOG(INFO) << "Loading single extension from " <<
[email protected]99efb7b12009-12-18 02:39:161021 extension_path.BaseName().value();
[email protected]bf24d2c2009-02-24 23:07:451022
[email protected]ab6f2b22009-07-28 23:28:371023 std::string error;
1024 Extension* extension = extension_file_util::LoadExtension(
1025 extension_path,
1026 false, // Don't require id
1027 &error);
1028
1029 if (!extension) {
1030 ReportExtensionLoadError(extension_path, error);
1031 return;
[email protected]0877fd92009-02-03 16:34:061032 }
[email protected]ab6f2b22009-07-28 23:28:371033
1034 extension->set_location(Extension::LOAD);
[email protected]e8c729a2010-03-09 19:55:191035
1036 // Report this as an installed extension so that it gets remembered in the
1037 // prefs.
1038 ChromeThread::PostTask(
1039 ChromeThread::UI, FROM_HERE,
1040 NewRunnableMethod(frontend_, &ExtensionsService::OnExtensionInstalled,
1041 extension, true));
[email protected]0877fd92009-02-03 16:34:061042}
1043
[email protected]6014d672008-12-05 00:38:251044void ExtensionsServiceBackend::ReportExtensionLoadError(
[email protected]cc5da332009-03-04 08:02:511045 const FilePath& extension_path, const std::string &error) {
[email protected]95d29192009-10-30 01:49:061046 ChromeThread::PostTask(
1047 ChromeThread::UI, FROM_HERE,
1048 NewRunnableMethod(
1049 frontend_,
[email protected]d11c8e92009-10-20 23:26:401050 &ExtensionsService::ReportExtensionLoadError, extension_path,
1051 error, NotificationType::EXTENSION_INSTALL_ERROR, alert_on_error_));
[email protected]6014d672008-12-05 00:38:251052}
1053
[email protected]a1257b12009-06-12 02:51:341054bool ExtensionsServiceBackend::LookupExternalExtension(
1055 const std::string& id, Version** version, Extension::Location* location) {
1056 scoped_ptr<Version> extension_version;
1057 for (ProviderMap::const_iterator i = external_extension_providers_.begin();
1058 i != external_extension_providers_.end(); ++i) {
[email protected]da50530a2009-06-15 17:43:011059 const ExternalExtensionProvider* provider = i->second.get();
[email protected]a1257b12009-06-12 02:51:341060 extension_version.reset(provider->RegisteredVersion(id, location));
1061 if (extension_version.get()) {
1062 if (version)
1063 *version = extension_version.release();
1064 return true;
1065 }
1066 }
1067 return false;
1068}
1069
[email protected]b0beaa662009-02-26 00:04:151070// Some extensions will autoupdate themselves externally from Chrome. These
1071// are typically part of some larger client application package. To support
[email protected]25b34332009-06-05 21:53:191072// these, the extension will register its location in the the preferences file
1073// (and also, on Windows, in the registry) and this code will periodically
[email protected]b0beaa662009-02-26 00:04:151074// check that location for a .crx file, which it will then install locally if
1075// a new version is available.
1076void ExtensionsServiceBackend::CheckForExternalUpdates(
[email protected]894bb502009-05-21 22:39:571077 std::set<std::string> ids_to_ignore,
1078 scoped_refptr<ExtensionsService> frontend) {
[email protected]b0beaa662009-02-26 00:04:151079 // Note that this installation is intentionally silent (since it didn't
1080 // go through the front-end). Extensions that are registered in this
1081 // way are effectively considered 'pre-bundled', and so implicitly
1082 // trusted. In general, if something has HKLM or filesystem access,
1083 // they could install an extension manually themselves anyway.
1084 alert_on_error_ = false;
1085 frontend_ = frontend;
[email protected]b0beaa662009-02-26 00:04:151086
[email protected]a1257b12009-06-12 02:51:341087 // Ask each external extension provider to give us a call back for each
1088 // extension they know about. See OnExternalExtensionFound.
1089 for (ProviderMap::const_iterator i = external_extension_providers_.begin();
1090 i != external_extension_providers_.end(); ++i) {
[email protected]da50530a2009-06-15 17:43:011091 ExternalExtensionProvider* provider = i->second.get();
[email protected]a1257b12009-06-12 02:51:341092 provider->VisitRegisteredExtension(this, ids_to_ignore);
[email protected]25b34332009-06-05 21:53:191093 }
[email protected]b0beaa662009-02-26 00:04:151094}
1095
[email protected]ae09ca62009-08-21 19:46:461096void ExtensionsServiceBackend::CheckExternalUninstall(
1097 scoped_refptr<ExtensionsService> frontend, const std::string& id,
1098 Extension::Location location) {
[email protected]a1257b12009-06-12 02:51:341099 // Check if the providers know about this extension.
1100 ProviderMap::const_iterator i = external_extension_providers_.find(location);
[email protected]ae09ca62009-08-21 19:46:461101 if (i == external_extension_providers_.end()) {
1102 NOTREACHED() << "CheckExternalUninstall called for non-external extension "
1103 << location;
1104 return;
[email protected]b0beaa662009-02-26 00:04:151105 }
[email protected]25b34332009-06-05 21:53:191106
[email protected]ae09ca62009-08-21 19:46:461107 scoped_ptr<Version> version;
1108 version.reset(i->second->RegisteredVersion(id, NULL));
1109 if (version.get())
1110 return; // Yup, known extension, don't uninstall.
1111
1112 // This is an external extension that we don't have registered. Uninstall.
[email protected]95d29192009-10-30 01:49:061113 ChromeThread::PostTask(
1114 ChromeThread::UI, FROM_HERE,
1115 NewRunnableMethod(
1116 frontend.get(), &ExtensionsService::UninstallExtension, id, true));
[email protected]b0beaa662009-02-26 00:04:151117}
1118
[email protected]a1257b12009-06-12 02:51:341119void ExtensionsServiceBackend::ClearProvidersForTesting() {
1120 external_extension_providers_.clear();
1121}
1122
1123void ExtensionsServiceBackend::SetProviderForTesting(
1124 Extension::Location location,
1125 ExternalExtensionProvider* test_provider) {
1126 DCHECK(test_provider);
[email protected]da50530a2009-06-15 17:43:011127 external_extension_providers_[location] =
1128 linked_ptr<ExternalExtensionProvider>(test_provider);
[email protected]a1257b12009-06-12 02:51:341129}
1130
1131void ExtensionsServiceBackend::OnExternalExtensionFound(
[email protected]7577a5c52009-07-30 06:21:581132 const std::string& id, const Version* version, const FilePath& path,
1133 Extension::Location location) {
[email protected]95d29192009-10-30 01:49:061134 ChromeThread::PostTask(
1135 ChromeThread::UI, FROM_HERE,
1136 NewRunnableMethod(
1137 frontend_, &ExtensionsService::OnExternalExtensionFound, id,
1138 version->GetString(), path, location));
[email protected]cc655912009-01-29 23:19:191139}
[email protected]c6d474f82009-12-16 21:11:061140
[email protected]2111b1a2010-03-12 18:12:441141void ExtensionsServiceBackend::ReloadExtensionManifests(
[email protected]c6d474f82009-12-16 21:11:061142 ExtensionPrefs::ExtensionsInfo* extensions_to_reload,
1143 base::TimeTicks start_time,
1144 scoped_refptr<ExtensionsService> frontend) {
1145 frontend_ = frontend;
1146
1147 for (size_t i = 0; i < extensions_to_reload->size(); ++i) {
1148 ExtensionInfo* info = extensions_to_reload->at(i).get();
[email protected]2111b1a2010-03-12 18:12:441149 if (!ShouldReloadExtensionManifest(*info))
[email protected]c6d474f82009-12-16 21:11:061150 continue;
1151
1152 // We need to reload original manifest in order to localize properly.
1153 std::string error;
1154 scoped_ptr<Extension> extension(extension_file_util::LoadExtension(
1155 info->extension_path, false, &error));
1156
1157 if (extension.get())
1158 extensions_to_reload->at(i)->extension_manifest.reset(
1159 static_cast<DictionaryValue*>(
1160 extension->manifest_value()->DeepCopy()));
1161 }
1162
1163 // Finish installing on UI thread.
1164 ChromeThread::PostTask(
1165 ChromeThread::UI, FROM_HERE,
1166 NewRunnableMethod(
1167 frontend_,
1168 &ExtensionsService::ContinueLoadAllExtensions,
1169 extensions_to_reload,
1170 start_time,
1171 true));
1172}