blob: 34427a020d8bde46390411d7ec974dfcfaf0885c [file] [log] [blame]
[email protected]8e4560b62011-01-14 10:09:141// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]6014d672008-12-05 00:38:252// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]eaa7dd182010-12-14 11:09:005#include "chrome/browser/extensions/extension_service.h"
[email protected]6014d672008-12-05 00:38:256
[email protected]654512b2010-09-01 02:09:427#include <algorithm>
[email protected]97d2f1d2011-01-15 00:41:088#include <set>
[email protected]654512b2010-09-01 02:09:429
[email protected]24b538a2010-02-27 01:22:4410#include "base/basictypes.h"
[email protected]e2eb43112009-05-29 21:19:5411#include "base/command_line.h"
[email protected]6014d672008-12-05 00:38:2512#include "base/file_util.h"
[email protected]835d7c82010-10-14 04:38:3813#include "base/metrics/histogram.h"
[email protected]cd500f72010-06-25 23:44:3214#include "base/stl_util-inl.h"
[email protected]24b538a2010-02-27 01:22:4415#include "base/string16.h"
[email protected]e83326f2010-07-31 17:29:2516#include "base/string_number_conversions.h"
[email protected]6014d672008-12-05 00:38:2517#include "base/string_util.h"
[email protected]18d4b6c2010-09-21 03:21:0418#include "base/stringprintf.h"
[email protected]34b99632011-01-01 01:01:0619#include "base/threading/thread_restrictions.h"
[email protected]cc2c3432009-11-06 17:24:3620#include "base/time.h"
[email protected]ce7f62e32010-08-10 23:43:5921#include "base/utf_string_conversions.h"
[email protected]cc655912009-01-29 23:19:1922#include "base/values.h"
[email protected]aa142702010-03-26 01:26:3323#include "base/version.h"
[email protected]15730c42009-09-03 00:03:2024#include "chrome/browser/browser_process.h"
[email protected]4814b512009-11-07 00:12:2925#include "chrome/browser/debugger/devtools_manager.h"
[email protected]7577a5c52009-07-30 06:21:5826#include "chrome/browser/extensions/crx_installer.h"
[email protected]18cc5a52011-04-15 16:03:3827#include "chrome/browser/extensions/apps_promo.h"
[email protected]5cbe1e22010-01-30 01:18:5628#include "chrome/browser/extensions/extension_accessibility_api.h"
[email protected]840b0db2009-11-20 03:00:3829#include "chrome/browser/extensions/extension_bookmarks_module.h"
[email protected]b68d5ed2009-04-16 02:41:2830#include "chrome/browser/extensions/extension_browser_event_router.h"
[email protected]2c5e1e12010-06-10 13:14:4431#include "chrome/browser/extensions/extension_cookies_api.h"
[email protected]c10da4b02010-03-25 14:38:3232#include "chrome/browser/extensions/extension_data_deleter.h"
[email protected]14a000d2010-04-29 21:44:2433#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]de768a832009-10-30 05:25:0134#include "chrome/browser/extensions/extension_history_api.h"
[email protected]b1748b1d82009-11-30 20:32:5635#include "chrome/browser/extensions/extension_host.h"
[email protected]7596ce72010-08-30 05:10:4636#include "chrome/browser/extensions/extension_management_api.h"
[email protected]118de7892011-03-16 13:31:4037#include "chrome/browser/extensions/extension_preference_api.h"
[email protected]4814b512009-11-07 00:12:2938#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]8a661f82010-10-19 21:47:1139#include "chrome/browser/extensions/extension_processes_api.h"
[email protected]19eb80152011-02-26 00:28:4340#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]93fd78f42009-07-10 16:43:1741#include "chrome/browser/extensions/extension_updater.h"
[email protected]8f9d4eb2011-02-05 01:39:1042#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]784688a62010-09-13 07:06:5243#include "chrome/browser/extensions/extension_webnavigation_api.h"
[email protected]8e4560b62011-01-14 10:09:1444#include "chrome/browser/extensions/external_extension_provider_impl.h"
[email protected]42b795652011-02-22 23:32:3845#include "chrome/browser/extensions/external_extension_provider_interface.h"
[email protected]b2907fd2011-03-25 16:43:3746#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]56ad3792010-05-28 17:45:3347#include "chrome/browser/net/chrome_url_request_context.h"
[email protected]37858e52010-08-26 00:22:0248#include "chrome/browser/prefs/pref_service.h"
[email protected]8ecad5e2010-12-02 21:18:3349#include "chrome/browser/profiles/profile.h"
[email protected]56ad3792010-05-28 17:45:3350#include "chrome/browser/search_engines/template_url_model.h"
[email protected]18280372011-03-22 18:05:2251#include "chrome/browser/themes/theme_service.h"
[email protected]a0ea76c2011-03-23 17:36:4252#include "chrome/browser/themes/theme_service_factory.h"
[email protected]fd42ac30f2011-02-27 19:33:3653#include "chrome/browser/ui/webui/shown_sections_handler.h"
[email protected]aab98a52009-12-02 03:22:3554#include "chrome/common/child_process_logging.h"
[email protected]e2eb43112009-05-29 21:19:5455#include "chrome/common/chrome_switches.h"
[email protected]5b1a0e22009-05-26 19:00:5856#include "chrome/common/extensions/extension.h"
[email protected]d7b36dc2009-10-29 21:47:4057#include "chrome/common/extensions/extension_constants.h"
[email protected]05c82182010-06-24 17:49:0858#include "chrome/common/extensions/extension_error_utils.h"
[email protected]7c927b62010-02-24 09:54:1359#include "chrome/common/extensions/extension_file_util.h"
[email protected]c6d474f82009-12-16 21:11:0660#include "chrome/common/extensions/extension_l10n_util.h"
[email protected]a315ba92010-11-16 14:12:2161#include "chrome/common/extensions/extension_resource.h"
[email protected]42b795652011-02-22 23:32:3862#include "chrome/common/json_value_serializer.h"
[email protected]84396dbc2011-04-14 06:33:4263#include "chrome/common/pepper_plugin_registry.h"
[email protected]25b34332009-06-05 21:53:1964#include "chrome/common/pref_names.h"
[email protected]a57209872009-05-04 22:53:1465#include "chrome/common/url_constants.h"
[email protected]5f945a0e2011-03-01 17:47:5366#include "content/browser/browser_thread.h"
[email protected]2de307592011-04-05 21:16:5867#include "content/browser/plugin_process_host.h"
68#include "content/browser/plugin_service.h"
[email protected]b3841c502011-03-09 01:21:3169#include "content/common/notification_service.h"
70#include "content/common/notification_type.h"
[email protected]c10da4b02010-03-25 14:38:3271#include "googleurl/src/gurl.h"
[email protected]1debbbb62010-10-06 17:23:4472#include "net/base/registry_controlled_domain.h"
[email protected]24b538a2010-02-27 01:22:4473#include "webkit/database/database_tracker.h"
74#include "webkit/database/database_util.h"
[email protected]2de307592011-04-05 21:16:5875#include "webkit/plugins/npapi/plugin_list.h"
[email protected]c64631652009-04-29 22:24:3176
[email protected]eed367e2011-04-12 03:43:3177#if defined(OS_CHROMEOS)
78#include "chrome/browser/chromeos/extensions/file_browser_event_router.h"
79#endif
80
[email protected]5ef47ec2010-01-28 05:58:0581using base::Time;
82
[email protected]c6d474f82009-12-16 21:11:0683namespace errors = extension_manifest_errors;
84
[email protected]b6ab96d2009-08-20 18:58:1985namespace {
86
[email protected]29d0d4ac2010-09-08 21:10:3187#if defined(OS_LINUX)
88static const int kOmniboxIconPaddingLeft = 2;
89static const int kOmniboxIconPaddingRight = 2;
90#elif defined(OS_MACOSX)
91static const int kOmniboxIconPaddingLeft = 0;
92static const int kOmniboxIconPaddingRight = 2;
93#else
94static const int kOmniboxIconPaddingLeft = 0;
95static const int kOmniboxIconPaddingRight = 0;
96#endif
97
[email protected]a315ba92010-11-16 14:12:2198// The following enumeration is used in histograms matching
99// Extensions.ManifestReload* . Values may be added, as long
100// as existing values are not changed.
101enum ManifestReloadReason {
102 NOT_NEEDED = 0, // Reload not needed.
103 UNPACKED_DIR, // Unpacked directory
104 NEEDS_RELOCALIZATION, // The local has changed since we read this extension.
105 NUM_MANIFEST_RELOAD_REASONS
106};
[email protected]2111b1a2010-03-12 18:12:44107
[email protected]a315ba92010-11-16 14:12:21108ManifestReloadReason ShouldReloadExtensionManifest(const ExtensionInfo& info) {
109 // Always reload manifests of unpacked extensions, because they can change
110 // on disk independent of the manifest in our prefs.
111 if (info.extension_location == Extension::LOAD)
112 return UNPACKED_DIR;
113
114 // Reload the manifest if it needs to be relocalized.
115 if (extension_l10n_util::ShouldRelocalizeManifest(info))
116 return NEEDS_RELOCALIZATION;
117
118 return NOT_NEEDED;
[email protected]2111b1a2010-03-12 18:12:44119}
120
[email protected]2de307592011-04-05 21:16:58121static void ForceShutdownPlugin(const FilePath& plugin_path) {
122 PluginProcessHost* plugin =
123 PluginService::GetInstance()->FindNpapiPluginProcess(plugin_path);
124 if (plugin)
125 plugin->ForceShutdown();
126}
127
[email protected]c6d474f82009-12-16 21:11:06128} // namespace
[email protected]b6ab96d2009-08-20 18:58:19129
[email protected]eaa7dd182010-12-14 11:09:00130ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49131 : background_page_ready(false),
132 being_upgraded(false) {
133}
134
[email protected]eaa7dd182010-12-14 11:09:00135ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49136}
137
[email protected]eaa7dd182010-12-14 11:09:00138// ExtensionService.
[email protected]6014d672008-12-05 00:38:25139
[email protected]eaa7dd182010-12-14 11:09:00140const char* ExtensionService::kInstallDirectoryName = "Extensions";
141const char* ExtensionService::kCurrentVersionFileName = "Current Version";
[email protected]494c06e2009-07-25 01:06:42142
[email protected]eaa7dd182010-12-14 11:09:00143// Implements IO for the ExtensionService.
[email protected]7a4c6852010-09-16 03:44:22144
[email protected]eaa7dd182010-12-14 11:09:00145class ExtensionServiceBackend
[email protected]8e4560b62011-01-14 10:09:14146 : public base::RefCountedThreadSafe<ExtensionServiceBackend> {
[email protected]7a4c6852010-09-16 03:44:22147 public:
[email protected]1f830eb2010-09-28 08:25:14148 // |install_directory| is a path where to look for extensions to load.
[email protected]8e4560b62011-01-14 10:09:14149 explicit ExtensionServiceBackend(const FilePath& install_directory);
[email protected]7a4c6852010-09-16 03:44:22150
151 // Loads a single extension from |path| where |path| is the top directory of
152 // a specific extension where its manifest file lives.
153 // Errors are reported through ExtensionErrorReporter. On success,
[email protected]49cd1572011-02-08 21:38:45154 // AddExtension() is called.
[email protected]7a4c6852010-09-16 03:44:22155 // TODO(erikkay): It might be useful to be able to load a packed extension
156 // (presumably into memory) without installing it.
157 void LoadSingleExtension(const FilePath &path,
[email protected]eaa7dd182010-12-14 11:09:00158 scoped_refptr<ExtensionService> frontend);
[email protected]7a4c6852010-09-16 03:44:22159
[email protected]7a4c6852010-09-16 03:44:22160 private:
[email protected]eaa7dd182010-12-14 11:09:00161 friend class base::RefCountedThreadSafe<ExtensionServiceBackend>;
[email protected]7a4c6852010-09-16 03:44:22162
[email protected]eaa7dd182010-12-14 11:09:00163 virtual ~ExtensionServiceBackend();
[email protected]7a4c6852010-09-16 03:44:22164
165 // Finish installing the extension in |crx_path| after it has been unpacked to
166 // |unpacked_path|. If |expected_id| is not empty, it's verified against the
167 // extension's manifest before installation. If |silent| is true, there will
168 // be no install confirmation dialog. |from_gallery| indicates whether the
169 // crx was installed from our gallery, which results in different UI.
170 //
171 // Note: We take ownership of |extension|.
172 void OnExtensionUnpacked(const FilePath& crx_path,
173 const FilePath& unpacked_path,
[email protected]9adb9692010-10-29 23:14:02174 const Extension* extension,
[email protected]7a4c6852010-09-16 03:44:22175 const std::string expected_id);
176
177 // Notify the frontend that there was an error loading an extension.
178 void ReportExtensionLoadError(const FilePath& extension_path,
179 const std::string& error);
180
[email protected]7a4c6852010-09-16 03:44:22181 // This is a naked pointer which is set by each entry point.
182 // The entry point is responsible for ensuring lifetime.
[email protected]eaa7dd182010-12-14 11:09:00183 ExtensionService* frontend_;
[email protected]7a4c6852010-09-16 03:44:22184
185 // The top-level extensions directory being installed to.
186 FilePath install_directory_;
187
188 // Whether errors result in noisy alerts.
189 bool alert_on_error_;
190
[email protected]eaa7dd182010-12-14 11:09:00191 DISALLOW_COPY_AND_ASSIGN(ExtensionServiceBackend);
[email protected]7a4c6852010-09-16 03:44:22192};
193
[email protected]eaa7dd182010-12-14 11:09:00194ExtensionServiceBackend::ExtensionServiceBackend(
[email protected]aebe23a32010-12-10 22:15:48195 const FilePath& install_directory)
[email protected]7a4c6852010-09-16 03:44:22196 : frontend_(NULL),
197 install_directory_(install_directory),
[email protected]8e4560b62011-01-14 10:09:14198 alert_on_error_(false) {
[email protected]7a4c6852010-09-16 03:44:22199}
200
[email protected]eaa7dd182010-12-14 11:09:00201ExtensionServiceBackend::~ExtensionServiceBackend() {
[email protected]7a4c6852010-09-16 03:44:22202}
203
[email protected]eaa7dd182010-12-14 11:09:00204void ExtensionServiceBackend::LoadSingleExtension(
205 const FilePath& path_in, scoped_refptr<ExtensionService> frontend) {
[email protected]a29a517a2011-01-21 21:11:12206 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]a8af9fdb2010-10-28 21:52:20207
[email protected]7a4c6852010-09-16 03:44:22208 frontend_ = frontend;
209
210 // Explicit UI loads are always noisy.
211 alert_on_error_ = true;
212
213 FilePath extension_path = path_in;
214 file_util::AbsolutePath(&extension_path);
215
[email protected]3aff9ad2011-04-01 20:26:48216 int flags = Extension::ShouldAlwaysAllowFileAccess(Extension::LOAD) ?
217 Extension::ALLOW_FILE_ACCESS : Extension::NO_FLAGS;
[email protected]83048a22011-03-29 00:14:13218 if (Extension::ShouldDoStrictErrorChecking(Extension::LOAD))
219 flags |= Extension::STRICT_ERROR_CHECKS;
[email protected]7a4c6852010-09-16 03:44:22220 std::string error;
[email protected]ad8e04a2010-11-01 04:16:27221 scoped_refptr<const Extension> extension(extension_file_util::LoadExtension(
[email protected]7a4c6852010-09-16 03:44:22222 extension_path,
[email protected]92888082010-10-18 19:24:57223 Extension::LOAD,
[email protected]83048a22011-03-29 00:14:13224 flags,
[email protected]ad8e04a2010-11-01 04:16:27225 &error));
[email protected]7a4c6852010-09-16 03:44:22226
227 if (!extension) {
228 ReportExtensionLoadError(extension_path, error);
229 return;
230 }
231
[email protected]7a4c6852010-09-16 03:44:22232 // Report this as an installed extension so that it gets remembered in the
233 // prefs.
[email protected]ca4b5fa32010-10-09 12:42:18234 BrowserThread::PostTask(
235 BrowserThread::UI, FROM_HERE,
[email protected]8d888c12010-11-30 00:00:25236 NewRunnableMethod(frontend_,
[email protected]eaa7dd182010-12-14 11:09:00237 &ExtensionService::OnExtensionInstalled,
[email protected]8d888c12010-11-30 00:00:25238 extension));
[email protected]7a4c6852010-09-16 03:44:22239}
240
[email protected]eaa7dd182010-12-14 11:09:00241void ExtensionServiceBackend::ReportExtensionLoadError(
[email protected]7a4c6852010-09-16 03:44:22242 const FilePath& extension_path, const std::string &error) {
[email protected]a29a517a2011-01-21 21:11:12243 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]ca4b5fa32010-10-09 12:42:18244 BrowserThread::PostTask(
245 BrowserThread::UI, FROM_HERE,
[email protected]7a4c6852010-09-16 03:44:22246 NewRunnableMethod(
247 frontend_,
[email protected]eaa7dd182010-12-14 11:09:00248 &ExtensionService::ReportExtensionLoadError, extension_path,
[email protected]7a4c6852010-09-16 03:44:22249 error, NotificationType::EXTENSION_INSTALL_ERROR, alert_on_error_));
250}
251
[email protected]8e4560b62011-01-14 10:09:14252void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12253 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20254
[email protected]7a4c6852010-09-16 03:44:22255 // Check if the providers know about this extension.
[email protected]0a60a2e2010-10-25 16:15:21256 ProviderCollection::const_iterator i;
257 for (i = external_extension_providers_.begin();
258 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14259 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21260 if (i->get()->HasExtension(id))
261 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22262 }
263
[email protected]7a4c6852010-09-16 03:44:22264 // This is an external extension that we don't have registered. Uninstall.
[email protected]d6ebc9792011-04-07 18:18:33265 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22266}
267
[email protected]8e4560b62011-01-14 10:09:14268void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22269 external_extension_providers_.clear();
270}
271
[email protected]8e4560b62011-01-14 10:09:14272void ExtensionService::AddProviderForTesting(
273 ExternalExtensionProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12274 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21275 external_extension_providers_.push_back(
[email protected]8e4560b62011-01-14 10:09:14276 linked_ptr<ExternalExtensionProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22277}
278
[email protected]8e4560b62011-01-14 10:09:14279void ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22280 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42281 const GURL& update_url,
282 Extension::Location location) {
[email protected]ab22ba42011-01-14 16:36:38283 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
284 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20285
[email protected]8e4560b62011-01-14 10:09:14286 if (GetExtensionById(id, true)) {
[email protected]7a4c6852010-09-16 03:44:22287 // Already installed. Do not change the update URL that the extension set.
288 return;
289 }
[email protected]b2907fd2011-03-25 16:43:37290 pending_extension_manager()->AddFromExternalUpdateUrl(
291 id, update_url, location);
[email protected]a29a517a2011-01-21 21:11:12292 external_extension_url_added_ |= true;
[email protected]7a4c6852010-09-16 03:44:22293}
294
[email protected]eaa7dd182010-12-14 11:09:00295bool ExtensionService::IsDownloadFromGallery(const GURL& download_url,
[email protected]a29a517a2011-01-21 21:11:12296 const GURL& referrer_url) {
[email protected]d3071992010-10-08 15:24:07297 // Special-case the themes mini-gallery.
298 // TODO(erikkay) When that gallery goes away, remove this code.
299 if (IsDownloadFromMiniGallery(download_url) &&
300 StartsWithASCII(referrer_url.spec(),
301 extension_urls::kMiniGalleryBrowsePrefix, false)) {
[email protected]334e04a2010-06-24 23:34:44302 return true;
[email protected]1debbbb62010-10-06 17:23:44303 }
[email protected]473ff6e2010-05-12 15:31:55304
[email protected]9adb9692010-10-29 23:14:02305 const Extension* download_extension = GetExtensionByWebExtent(download_url);
306 const Extension* referrer_extension = GetExtensionByWebExtent(referrer_url);
307 const Extension* webstore_app = GetWebStoreApp();
[email protected]d3071992010-10-08 15:24:07308
309 bool referrer_valid = (referrer_extension == webstore_app);
310 bool download_valid = (download_extension == webstore_app);
311
312 // If the command-line gallery URL is set, then be a bit more lenient.
313 GURL store_url =
314 GURL(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
315 switches::kAppsGalleryURL));
316 if (!store_url.is_empty()) {
317 std::string store_tld =
318 net::RegistryControlledDomainService::GetDomainAndRegistry(store_url);
319 if (!referrer_valid) {
320 std::string referrer_tld =
321 net::RegistryControlledDomainService::GetDomainAndRegistry(
322 referrer_url);
323 // The referrer gets stripped when transitioning from https to http,
324 // or when hitting an unknown test cert and that commonly happens in
325 // testing environments. Given this, we allow an empty referrer when
326 // the command-line flag is set.
327 // Otherwise, the TLD must match the TLD of the command-line url.
328 referrer_valid = referrer_url.is_empty() || (referrer_tld == store_tld);
329 }
330
331 if (!download_valid) {
332 std::string download_tld =
333 net::RegistryControlledDomainService::GetDomainAndRegistry(
334 GURL(download_url));
335
336 // Otherwise, the TLD must match the TLD of the command-line url.
337 download_valid = (download_tld == store_tld);
338 }
339 }
340
341 return (referrer_valid && download_valid);
[email protected]b7c2f252009-12-08 00:47:23342}
343
[email protected]eaa7dd182010-12-14 11:09:00344bool ExtensionService::IsDownloadFromMiniGallery(const GURL& download_url) {
[email protected]ac025282009-12-16 19:16:38345 return StartsWithASCII(download_url.spec(),
346 extension_urls::kMiniGalleryDownloadPrefix,
347 false); // case_sensitive
348}
349
[email protected]d9696672011-03-15 22:45:09350const Extension* ExtensionService::GetInstalledApp(const GURL& url) {
[email protected]ffb204f22010-12-05 23:20:27351 // Check for hosted app.
[email protected]d9696672011-03-15 22:45:09352 const Extension* app = GetExtensionByWebExtent(url);
353 if (app)
354 return app;
[email protected]ffb204f22010-12-05 23:20:27355
356 // Check for packaged app.
[email protected]d9696672011-03-15 22:45:09357 app = GetExtensionByURL(url);
358 if (app && app->is_app())
359 return app;
360
361 return NULL;
362}
363
364bool ExtensionService::IsInstalledApp(const GURL& url) {
365 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27366}
367
[email protected]6aeac8342010-10-01 20:21:18368// static
[email protected]d6ebc9792011-04-07 18:18:33369// This function is used to implement the command-line switch
370// --uninstall-extension. The LOG statements within this function are used to
371// inform the user if the uninstall cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00372bool ExtensionService::UninstallExtensionHelper(
373 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18374 const std::string& extension_id) {
[email protected]6aeac8342010-10-01 20:21:18375
[email protected]95da88c42011-03-31 10:07:33376 const Extension* extension =
377 extensions_service->GetExtensionById(extension_id, true);
378 if (!extension)
379 extension = extensions_service->GetTerminatedExtension(extension_id);
380
[email protected]d6ebc9792011-04-07 18:18:33381 // We can't call UninstallExtension with an invalid extension ID.
[email protected]95da88c42011-03-31 10:07:33382 if (!extension) {
[email protected]6aeac8342010-10-01 20:21:18383 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33384 << "id: " << extension_id;
385 return false;
[email protected]6aeac8342010-10-01 20:21:18386 }
387
[email protected]d6ebc9792011-04-07 18:18:33388 // The following call to UninstallExtension will not allow an uninstall of a
389 // policy-controlled extension.
390 std::string error;
391 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
392 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
393 << ": " << error;
394 return false;
395 }
[email protected]95da88c42011-03-31 10:07:33396
[email protected]6aeac8342010-10-01 20:21:18397 return true;
398}
399
[email protected]eaa7dd182010-12-14 11:09:00400ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24401 const CommandLine* command_line,
402 const FilePath& install_directory,
403 ExtensionPrefs* extension_prefs,
[email protected]0436b102011-04-15 18:30:03404 bool autoupdate_enabled,
405 bool extensions_enabled)
[email protected]6ef635e42009-07-26 06:16:12406 : profile_(profile),
[email protected]73c47932010-12-06 18:13:43407 extension_prefs_(extension_prefs),
[email protected]b2907fd2011-03-25 16:43:37408 ALLOW_THIS_IN_INITIALIZER_LIST(pending_extension_manager_(*this)),
[email protected]a9b00ac2009-06-25 21:03:23409 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03410 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13411 show_extensions_prompts_(true),
[email protected]3ecda252010-11-18 19:50:55412 ready_(false),
[email protected]ec5b50d2010-10-09 16:35:18413 ALLOW_THIS_IN_INITIALIZER_LIST(toolbar_model_(this)),
[email protected]18cc5a52011-04-15 16:03:38414 apps_promo_(profile->GetPrefs()),
[email protected]b3d62312b12010-10-14 21:10:18415 event_routers_initialized_(false) {
[email protected]a29a517a2011-01-21 21:11:12416 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20417
[email protected]36a784c2009-06-23 06:21:08418 // Figure out if extension installation should be enabled.
[email protected]6d60703b2009-08-29 01:29:23419 if (command_line->HasSwitch(switches::kDisableExtensions)) {
420 extensions_enabled_ = false;
421 } else if (profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
422 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18423 }
[email protected]36a784c2009-06-23 06:21:08424
[email protected]a4ed6282009-12-14 20:51:16425 registrar_.Add(this, NotificationType::EXTENSION_PROCESS_TERMINATED,
[email protected]bc535ee52010-08-31 18:40:32426 NotificationService::AllSources());
[email protected]2fb7dc982010-09-29 12:24:28427 pref_change_registrar_.Init(profile->GetPrefs());
428 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, this);
429 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, this);
[email protected]4814b512009-11-07 00:12:29430
[email protected]93fd78f42009-07-10 16:43:17431 // Set up the ExtensionUpdater
432 if (autoupdate_enabled) {
433 int update_frequency = kDefaultUpdateFrequencySeconds;
434 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25435 base::StringToInt(command_line->GetSwitchValueASCII(
436 switches::kExtensionsUpdateFrequency),
437 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17438 }
[email protected]82b6e512011-04-12 20:33:27439 updater_.reset(new ExtensionUpdater(this,
440 extension_prefs,
441 profile->GetPrefs(),
442 profile,
443 update_frequency));
[email protected]93fd78f42009-07-10 16:43:17444 }
445
[email protected]8e4560b62011-01-14 10:09:14446 backend_ = new ExtensionServiceBackend(install_directory_);
447
[email protected]0436b102011-04-15 18:30:03448 if (extensions_enabled_) {
[email protected]873531342011-03-09 12:16:05449 ExternalExtensionProviderImpl::CreateExternalProviders(
450 this, profile_, &external_extension_providers_);
451 }
[email protected]b671760b2010-07-15 21:13:47452
[email protected]aa96d3a2010-08-21 08:45:25453 // Use monochrome icons for Omnibox icons.
[email protected]29d0d4ac2010-09-08 21:10:31454 omnibox_popup_icon_manager_.set_monochrome(true);
[email protected]b671760b2010-07-15 21:13:47455 omnibox_icon_manager_.set_monochrome(true);
[email protected]29d0d4ac2010-09-08 21:10:31456 omnibox_icon_manager_.set_padding(gfx::Insets(0, kOmniboxIconPaddingLeft,
457 0, kOmniboxIconPaddingRight));
[email protected]6014d672008-12-05 00:38:25458}
459
[email protected]eaa7dd182010-12-14 11:09:00460const ExtensionList* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45461 return &extensions_;
462}
463
[email protected]eaa7dd182010-12-14 11:09:00464const ExtensionList* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45465 return &disabled_extensions_;
466}
467
[email protected]bb7f40952011-01-13 00:21:20468const ExtensionList* ExtensionService::terminated_extensions() const {
469 return &terminated_extensions_;
470}
471
[email protected]b2907fd2011-03-25 16:43:37472PendingExtensionManager* ExtensionService::pending_extension_manager() {
473 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45474}
475
[email protected]eaa7dd182010-12-14 11:09:00476ExtensionService::~ExtensionService() {
[email protected]2fb7dc982010-09-29 12:24:28477 DCHECK(!profile_); // Profile should have told us it's going away.
[email protected]9f1087e2009-06-15 17:29:32478 UnloadAllExtensions();
[email protected]94b4ffc2011-02-04 05:55:17479
[email protected]8e4560b62011-01-14 10:09:14480 ProviderCollection::const_iterator i;
481 for (i = external_extension_providers_.begin();
482 i != external_extension_providers_.end(); ++i) {
483 ExternalExtensionProviderInterface* provider = i->get();
484 provider->ServiceShutdown();
485 }
[email protected]eed367e2011-04-12 03:43:31486
487#if defined(OS_CHROMEOS)
488 if (event_routers_initialized_) {
489 ExtensionFileBrowserEventRouter::GetInstance()->
490 StopObservingFileSystemEvents();
491 }
492#endif
[email protected]6014d672008-12-05 00:38:25493}
494
[email protected]eaa7dd182010-12-14 11:09:00495void ExtensionService::InitEventRouters() {
[email protected]b3d62312b12010-10-14 21:10:18496 if (event_routers_initialized_)
497 return;
498
[email protected]c5ae74ab2010-04-15 18:14:37499 ExtensionHistoryEventRouter::GetInstance()->ObserveProfile(profile_);
500 ExtensionAccessibilityEventRouter::GetInstance()->ObserveProfile(profile_);
[email protected]97d2f1d2011-01-15 00:41:08501 browser_event_router_.reset(new ExtensionBrowserEventRouter(profile_));
502 browser_event_router_->Init();
[email protected]118de7892011-03-16 13:31:40503 preference_event_router_.reset(new ExtensionPreferenceEventRouter(profile_));
[email protected]8e8bb6d2010-12-13 08:18:55504 ExtensionBookmarkEventRouter::GetInstance()->Observe(
[email protected]c5ae74ab2010-04-15 18:14:37505 profile_->GetBookmarkModel());
[email protected]2c5e1e12010-06-10 13:14:44506 ExtensionCookiesEventRouter::GetInstance()->Init();
[email protected]7596ce72010-08-30 05:10:46507 ExtensionManagementEventRouter::GetInstance()->Init();
[email protected]8a661f82010-10-19 21:47:11508 ExtensionProcessesEventRouter::GetInstance()->ObserveProfile(profile_);
[email protected]784688a62010-09-13 07:06:52509 ExtensionWebNavigationEventRouter::GetInstance()->Init();
[email protected]eed367e2011-04-12 03:43:31510#if defined(OS_CHROMEOS)
511 ExtensionFileBrowserEventRouter::GetInstance()->ObserveFileSystemEvents(
512 profile_);
513#endif
[email protected]b3d62312b12010-10-14 21:10:18514 event_routers_initialized_ = true;
[email protected]c5ae74ab2010-04-15 18:14:37515}
516
[email protected]b2907fd2011-03-25 16:43:37517const Extension* ExtensionService::GetExtensionById(
518 const std::string& id, bool include_disabled) const {
[email protected]78994ab02010-12-08 18:06:44519 return GetExtensionByIdInternal(id, true, include_disabled);
520}
521
[email protected]eaa7dd182010-12-14 11:09:00522void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12523 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20524
[email protected]fa6a9102010-11-22 15:38:50525 DCHECK(!ready_); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17526 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32527
[email protected]95dd38f2009-10-20 20:09:15528 // Hack: we need to ensure the ResourceDispatcherHost is ready before we load
529 // the first extension, because its members listen for loaded notifications.
530 g_browser_process->resource_dispatcher_host();
531
[email protected]9f1087e2009-06-15 17:29:32532 LoadAllExtensions();
[email protected]894bb502009-05-21 22:39:57533
[email protected]9f1087e2009-06-15 17:29:32534 // TODO(erikkay) this should probably be deferred to a future point
535 // rather than running immediately at startup.
[email protected]93fd78f42009-07-10 16:43:17536 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57537
[email protected]9f1087e2009-06-15 17:29:32538 // TODO(erikkay) this should probably be deferred as well.
539 GarbageCollectExtensions();
[email protected]6014d672008-12-05 00:38:25540}
541
[email protected]eaa7dd182010-12-14 11:09:00542void ExtensionService::UpdateExtension(const std::string& id,
[email protected]9a8c4022011-01-25 14:25:33543 const FilePath& extension_path,
544 const GURL& download_url) {
[email protected]a29a517a2011-01-21 21:11:12545 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20546
[email protected]b2907fd2011-03-25 16:43:37547 PendingExtensionInfo pending_extension_info;
548 bool is_pending_extension = pending_extension_manager_.GetById(
549 id, &pending_extension_info);
[email protected]8ef78fd2010-08-19 17:14:32550
[email protected]5eb375e92010-11-26 07:50:41551 const Extension* extension = GetExtensionByIdInternal(id, true, true);
552 if (!is_pending_extension && !extension) {
[email protected]aa142702010-03-26 01:26:33553 LOG(WARNING) << "Will not update extension " << id
554 << " because it is not installed or pending";
555 // Delete extension_path since we're not creating a CrxInstaller
556 // that would do it for us.
[email protected]ca4b5fa32010-10-09 12:42:18557 BrowserThread::PostTask(
558 BrowserThread::FILE, FROM_HERE,
[email protected]53da2c962011-03-03 17:08:05559 NewRunnableFunction(
560 extension_file_util::DeleteFile, extension_path, false));
[email protected]4c967932009-07-31 01:15:49561 return;
[email protected]e957fe52009-06-23 16:51:05562 }
563
[email protected]aa142702010-03-26 01:26:33564 // We want a silent install only for non-pending extensions and
565 // pending extensions that have install_silently set.
566 ExtensionInstallUI* client =
[email protected]b2907fd2011-03-25 16:43:37567 (!is_pending_extension || pending_extension_info.install_silently()) ?
[email protected]aa142702010-03-26 01:26:33568 NULL : new ExtensionInstallUI(profile_);
569
[email protected]6dfbbf82010-03-12 23:09:16570 scoped_refptr<CrxInstaller> installer(
[email protected]bb461532010-11-26 21:50:23571 new CrxInstaller(this, // frontend
[email protected]aa142702010-03-26 01:26:33572 client));
[email protected]6dfbbf82010-03-12 23:09:16573 installer->set_expected_id(id);
[email protected]ec5b50d2010-10-09 16:35:18574 if (is_pending_extension)
[email protected]b2907fd2011-03-25 16:43:37575 installer->set_install_source(pending_extension_info.install_source());
[email protected]5eb375e92010-11-26 07:50:41576 else if (extension)
577 installer->set_install_source(extension->location());
[email protected]6dfbbf82010-03-12 23:09:16578 installer->set_delete_source(true);
[email protected]5c8516202010-03-18 21:43:34579 installer->set_original_url(download_url);
[email protected]6dfbbf82010-03-12 23:09:16580 installer->InstallCrx(extension_path);
[email protected]e957fe52009-06-23 16:51:05581}
582
[email protected]eaa7dd182010-12-14 11:09:00583void ExtensionService::ReloadExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12584 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b65272f2009-08-31 15:47:06585 FilePath path;
[email protected]9adb9692010-10-29 23:14:02586 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40587
[email protected]f17dbd42010-08-16 23:21:10588 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06589 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29590 // If the extension has an inspector open for its background page, detach
591 // the inspector and hang onto a cookie for it, so that we can reattach
592 // later.
593 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
594 ExtensionHost* host = manager->GetBackgroundHostForExtension(
595 current_extension);
596 if (host) {
597 // Look for an open inspector for the background page.
598 int devtools_cookie = DevToolsManager::GetInstance()->DetachClientHost(
599 host->render_view_host());
600 if (devtools_cookie >= 0)
601 orphaned_dev_tools_[extension_id] = devtools_cookie;
602 }
603
[email protected]b65272f2009-08-31 15:47:06604 path = current_extension->path();
[email protected]f17dbd42010-08-16 23:21:10605 DisableExtension(extension_id);
606 disabled_extension_paths_[extension_id] = path;
[email protected]1eb175082010-02-10 09:26:16607 } else {
608 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06609 }
610
[email protected]e6090e42010-03-23 22:44:08611 // Check the installed extensions to see if what we're reloading was already
612 // installed.
613 scoped_ptr<ExtensionInfo> installed_extension(
614 extension_prefs_->GetInstalledExtensionInfo(extension_id));
615 if (installed_extension.get() &&
616 installed_extension->extension_manifest.get()) {
617 LoadInstalledExtension(*installed_extension, false);
618 } else {
619 // We should always be able to remember the extension's path. If it's not in
620 // the map, someone failed to update |unloaded_extension_paths_|.
621 CHECK(!path.empty());
622 LoadExtension(path);
623 }
[email protected]9cddd4702009-07-27 22:09:40624}
625
[email protected]d6ebc9792011-04-07 18:18:33626bool ExtensionService::UninstallExtension(const std::string& extension_id,
627 bool external_uninstall,
628 std::string* error) {
[email protected]a29a517a2011-01-21 21:11:12629 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20630
[email protected]9adb9692010-10-29 23:14:02631 const Extension* extension =
632 GetExtensionByIdInternal(extension_id, true, true);
[email protected]0dfe05c2011-02-23 23:03:36633 if (!extension)
634 extension = GetTerminatedExtension(extension_id);
[email protected]631cf822009-05-15 07:01:25635
[email protected]e7afe2452010-08-22 16:19:13636 // Callers should not send us nonexistent extensions.
[email protected]aebe23a32010-12-10 22:15:48637 CHECK(extension);
[email protected]9f1087e2009-06-15 17:29:32638
[email protected]831aa212010-03-26 13:55:19639 // Get hold of information we need after unloading, since the extension
640 // pointer will be invalid then.
641 GURL extension_url(extension->url());
642 Extension::Location location(extension->location());
[email protected]95da88c42011-03-31 10:07:33643
644 // Policy change which triggers an uninstall will always set
645 // |external_uninstall| to true so this is the only way to uninstall
646 // managed extensions.
[email protected]d6ebc9792011-04-07 18:18:33647 if (!Extension::UserMayDisable(location) && !external_uninstall) {
648 NotificationService::current()->Notify(
649 NotificationType::EXTENSION_UNINSTALL_NOT_ALLOWED,
650 Source<Profile>(profile_),
651 Details<const Extension>(extension));
652 if (error != NULL) {
653 *error = errors::kCannotUninstallManagedExtension;
654 }
655 return false;
656 }
[email protected]95da88c42011-03-31 10:07:33657
[email protected]211030342010-09-30 18:41:06658 UninstalledExtensionInfo uninstalled_extension_info(*extension);
[email protected]831aa212010-03-26 13:55:19659
[email protected]9b217652010-10-08 22:04:23660 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08661 extension->GetType(), 100);
[email protected]9b217652010-10-08 22:04:23662
[email protected]831aa212010-03-26 13:55:19663 // Also copy the extension identifier since the reference might have been
664 // obtained via Extension::id().
665 std::string extension_id_copy(extension_id);
666
[email protected]56ad3792010-05-28 17:45:33667 if (profile_->GetTemplateURLModel())
668 profile_->GetTemplateURLModel()->UnregisterExtensionKeyword(extension);
669
[email protected]831aa212010-03-26 13:55:19670 // Unload before doing more cleanup to ensure that nothing is hanging on to
671 // any of these resources.
[email protected]a9f39a312010-12-23 22:14:27672 UnloadExtension(extension_id, UnloadedExtensionInfo::UNINSTALL);
[email protected]831aa212010-03-26 13:55:19673
674 extension_prefs_->OnExtensionUninstalled(extension_id_copy, location,
675 external_uninstall);
[email protected]9f1087e2009-06-15 17:29:32676
677 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]831aa212010-03-26 13:55:19678 if (Extension::LOAD != location) {
[email protected]ca4b5fa32010-10-09 12:42:18679 BrowserThread::PostTask(
680 BrowserThread::FILE, FROM_HERE,
[email protected]95d29192009-10-30 01:49:06681 NewRunnableFunction(
[email protected]ca3dbf52010-05-19 22:27:06682 &extension_file_util::UninstallExtension,
683 install_directory_,
684 extension_id_copy));
[email protected]9f1087e2009-06-15 17:29:32685 }
686
[email protected]c10da4b02010-03-25 14:38:32687 ClearExtensionData(extension_url);
[email protected]0dfe05c2011-02-23 23:03:36688 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06689
690 // Notify interested parties that we've uninstalled this extension.
691 NotificationService::current()->Notify(
692 NotificationType::EXTENSION_UNINSTALLED,
693 Source<Profile>(profile_),
694 Details<UninstalledExtensionInfo>(&uninstalled_extension_info));
[email protected]d6ebc9792011-04-07 18:18:33695
696 return true;
[email protected]c10da4b02010-03-25 14:38:32697}
698
[email protected]eaa7dd182010-12-14 11:09:00699void ExtensionService::ClearExtensionData(const GURL& extension_url) {
[email protected]c10da4b02010-03-25 14:38:32700 scoped_refptr<ExtensionDataDeleter> deleter(
701 new ExtensionDataDeleter(profile_, extension_url));
702 deleter->StartDeleting();
[email protected]9f1087e2009-06-15 17:29:32703}
704
[email protected]c3cfb012011-04-06 22:07:35705bool ExtensionService::IsExtensionEnabled(
706 const std::string& extension_id) const {
707 // TODO(akalin): GetExtensionState() isn't very safe as it returns
708 // Extension::ENABLED by default; either change it to return
709 // something else by default or create a separate function that does
710 // so.
711 return
712 extension_prefs_->GetExtensionState(extension_id) == Extension::ENABLED;
713}
714
715bool ExtensionService::IsExternalExtensionUninstalled(
716 const std::string& extension_id) const {
717 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
718}
719
[email protected]eaa7dd182010-12-14 11:09:00720void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12721 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20722
[email protected]9adb9692010-10-29 23:14:02723 const Extension* extension =
724 GetExtensionByIdInternal(extension_id, false, true);
[email protected]f681c782010-11-19 11:19:39725 if (!extension)
[email protected]0c6da502009-08-14 22:32:39726 return;
[email protected]0c6da502009-08-14 22:32:39727
[email protected]e8c729a2010-03-09 19:55:19728 extension_prefs_->SetExtensionState(extension, Extension::ENABLED);
[email protected]1784e83a2009-09-08 21:01:52729
[email protected]0c6da502009-08-14 22:32:39730 // Move it over to the enabled list.
[email protected]00cd9c42010-11-02 20:15:57731 extensions_.push_back(make_scoped_refptr(extension));
[email protected]0c6da502009-08-14 22:32:39732 ExtensionList::iterator iter = std::find(disabled_extensions_.begin(),
733 disabled_extensions_.end(),
734 extension);
735 disabled_extensions_.erase(iter);
736
[email protected]f681c782010-11-19 11:19:39737 // Make sure any browser action contained within it is not hidden.
738 extension_prefs_->SetBrowserActionVisibility(extension, true);
739
[email protected]8f9d4eb2011-02-05 01:39:10740 ExtensionWebUI::RegisterChromeURLOverrides(profile_,
[email protected]86c008e82009-08-28 20:26:05741 extension->GetChromeURLOverrides());
742
[email protected]62d30f42009-10-01 22:36:06743 NotifyExtensionLoaded(extension);
[email protected]aab98a52009-12-02 03:22:35744 UpdateActiveExtensionsInCrashReporter();
[email protected]0c6da502009-08-14 22:32:39745}
746
[email protected]eaa7dd182010-12-14 11:09:00747void ExtensionService::DisableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12748 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20749
[email protected]9adb9692010-10-29 23:14:02750 const Extension* extension =
751 GetExtensionByIdInternal(extension_id, true, false);
[email protected]b2ba9962009-12-10 20:10:15752 // The extension may have been disabled already.
753 if (!extension)
[email protected]1784e83a2009-09-08 21:01:52754 return;
[email protected]1784e83a2009-09-08 21:01:52755
[email protected]95da88c42011-03-31 10:07:33756 if (!Extension::UserMayDisable(extension->location()))
757 return;
758
[email protected]e8c729a2010-03-09 19:55:19759 extension_prefs_->SetExtensionState(extension, Extension::DISABLED);
[email protected]1784e83a2009-09-08 21:01:52760
761 // Move it over to the disabled list.
[email protected]00cd9c42010-11-02 20:15:57762 disabled_extensions_.push_back(make_scoped_refptr(extension));
[email protected]1784e83a2009-09-08 21:01:52763 ExtensionList::iterator iter = std::find(extensions_.begin(),
764 extensions_.end(),
765 extension);
766 extensions_.erase(iter);
767
[email protected]8f9d4eb2011-02-05 01:39:10768 ExtensionWebUI::UnregisterChromeURLOverrides(profile_,
[email protected]1784e83a2009-09-08 21:01:52769 extension->GetChromeURLOverrides());
770
[email protected]a9f39a312010-12-23 22:14:27771 NotifyExtensionUnloaded(extension, UnloadedExtensionInfo::DISABLE);
[email protected]aab98a52009-12-02 03:22:35772 UpdateActiveExtensionsInCrashReporter();
[email protected]1784e83a2009-09-08 21:01:52773}
774
[email protected]eaa7dd182010-12-14 11:09:00775void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]8d888c12010-11-30 00:00:25776 CHECK(extension);
777
778 // We only maintain the granted permissions prefs for INTERNAL extensions.
[email protected]139e5732011-03-16 19:29:51779 CHECK_EQ(Extension::INTERNAL, extension->location());
[email protected]8d888c12010-11-30 00:00:25780
781 ExtensionExtent effective_hosts = extension->GetEffectiveHostPermissions();
782 extension_prefs_->AddGrantedPermissions(extension->id(),
783 extension->HasFullPermissions(),
784 extension->api_permissions(),
785 effective_hosts);
786}
787
[email protected]eaa7dd182010-12-14 11:09:00788void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]8d888c12010-11-30 00:00:25789 const Extension* extension) {
790 CHECK(extension);
791 GrantPermissions(extension);
792 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
793 EnableExtension(extension->id());
794}
795
[email protected]eaa7dd182010-12-14 11:09:00796void ExtensionService::LoadExtension(const FilePath& extension_path) {
[email protected]ca4b5fa32010-10-09 12:42:18797 BrowserThread::PostTask(
798 BrowserThread::FILE, FROM_HERE,
[email protected]95d29192009-10-30 01:49:06799 NewRunnableMethod(
800 backend_.get(),
[email protected]eaa7dd182010-12-14 11:09:00801 &ExtensionServiceBackend::LoadSingleExtension,
802 extension_path, scoped_refptr<ExtensionService>(this)));
[email protected]9f1087e2009-06-15 17:29:32803}
804
[email protected]eaa7dd182010-12-14 11:09:00805void ExtensionService::LoadComponentExtensions() {
[email protected]1952c7d2010-03-04 23:48:34806 for (RegisteredComponentExtensions::iterator it =
807 component_extension_manifests_.begin();
808 it != component_extension_manifests_.end(); ++it) {
[email protected]61b55b62011-03-24 09:03:10809 LoadComponentExtension(*it);
[email protected]1952c7d2010-03-04 23:48:34810 }
811}
812
[email protected]a964e112011-04-14 21:52:51813const Extension* ExtensionService::LoadComponentExtension(
[email protected]61b55b62011-03-24 09:03:10814 const ComponentExtensionInfo &info) {
815 JSONStringValueSerializer serializer(info.manifest);
816 scoped_ptr<Value> manifest(serializer.Deserialize(NULL, NULL));
817 if (!manifest.get()) {
818 DLOG(ERROR) << "Failed to parse manifest for extension";
[email protected]a964e112011-04-14 21:52:51819 return NULL;
[email protected]61b55b62011-03-24 09:03:10820 }
821
[email protected]83048a22011-03-29 00:14:13822 int flags = Extension::REQUIRE_KEY;
823 if (Extension::ShouldDoStrictErrorChecking(Extension::COMPONENT))
824 flags |= Extension::STRICT_ERROR_CHECKS;
[email protected]61b55b62011-03-24 09:03:10825 std::string error;
826 scoped_refptr<const Extension> extension(Extension::Create(
827 info.root_directory,
828 Extension::COMPONENT,
829 *static_cast<DictionaryValue*>(manifest.get()),
[email protected]83048a22011-03-29 00:14:13830 flags,
[email protected]61b55b62011-03-24 09:03:10831 &error));
832 if (!extension.get()) {
833 NOTREACHED() << error;
[email protected]a964e112011-04-14 21:52:51834 return NULL;
[email protected]61b55b62011-03-24 09:03:10835 }
836 AddExtension(extension);
[email protected]a964e112011-04-14 21:52:51837 return extension;
[email protected]61b55b62011-03-24 09:03:10838}
839
[email protected]eaa7dd182010-12-14 11:09:00840void ExtensionService::LoadAllExtensions() {
[email protected]a29a517a2011-01-21 21:11:12841 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a315ba92010-11-16 14:12:21842
[email protected]cc2c3432009-11-06 17:24:36843 base::TimeTicks start_time = base::TimeTicks::Now();
844
[email protected]1952c7d2010-03-04 23:48:34845 // Load any component extensions.
846 LoadComponentExtensions();
847
[email protected]e72e8eb82009-06-18 17:21:51848 // Load the previously installed extensions.
[email protected]a315ba92010-11-16 14:12:21849 scoped_ptr<ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]e6090e42010-03-23 22:44:08850 extension_prefs_->GetInstalledExtensionsInfo());
[email protected]c6d474f82009-12-16 21:11:06851
[email protected]a315ba92010-11-16 14:12:21852 std::vector<int> reload_reason_counts(NUM_MANIFEST_RELOAD_REASONS, 0);
853 bool should_write_prefs = false;
854
855 for (size_t i = 0; i < extensions_info->size(); ++i) {
856 ExtensionInfo* info = extensions_info->at(i).get();
857
858 ManifestReloadReason reload_reason = ShouldReloadExtensionManifest(*info);
859 ++reload_reason_counts[reload_reason];
860 UMA_HISTOGRAM_ENUMERATION("Extensions.ManifestReloadEnumValue",
861 reload_reason, 100);
862
863 if (reload_reason != NOT_NEEDED) {
864 // Reloading and extension reads files from disk. We do this on the
865 // UI thread because reloads should be very rare, and the complexity
866 // added by delaying the time when the extensions service knows about
867 // all extensions is significant. See crbug.com/37548 for details.
868 // |allow_io| disables tests that file operations run on the file
869 // thread.
870 base::ThreadRestrictions::ScopedAllowIO allow_io;
871
[email protected]83048a22011-03-29 00:14:13872 int flags = Extension::NO_FLAGS;
873 if (Extension::ShouldDoStrictErrorChecking(info->extension_location))
874 flags |= Extension::STRICT_ERROR_CHECKS;
[email protected]3aff9ad2011-04-01 20:26:48875 if (extension_prefs_->AllowFileAccess(info->extension_id))
876 flags |= Extension::ALLOW_FILE_ACCESS;
[email protected]a315ba92010-11-16 14:12:21877 std::string error;
878 scoped_refptr<const Extension> extension(
879 extension_file_util::LoadExtension(
[email protected]542258c2011-03-04 21:25:31880 info->extension_path,
881 info->extension_location,
[email protected]83048a22011-03-29 00:14:13882 flags,
[email protected]542258c2011-03-04 21:25:31883 &error));
[email protected]a315ba92010-11-16 14:12:21884
885 if (extension.get()) {
886 extensions_info->at(i)->extension_manifest.reset(
887 static_cast<DictionaryValue*>(
888 extension->manifest_value()->DeepCopy()));
889 should_write_prefs = true;
890 }
[email protected]c6d474f82009-12-16 21:11:06891 }
892 }
893
[email protected]a315ba92010-11-16 14:12:21894 for (size_t i = 0; i < extensions_info->size(); ++i) {
895 LoadInstalledExtension(*extensions_info->at(i), should_write_prefs);
[email protected]c6d474f82009-12-16 21:11:06896 }
897
[email protected]ae09ca62009-08-21 19:46:46898 OnLoadedInstalledExtensions();
[email protected]cc2c3432009-11-06 17:24:36899
[email protected]a315ba92010-11-16 14:12:21900 // The histograms Extensions.ManifestReload* allow us to validate
901 // the assumption that reloading manifest is a rare event.
902 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadNotNeeded",
903 reload_reason_counts[NOT_NEEDED]);
904 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadUnpackedDir",
905 reload_reason_counts[UNPACKED_DIR]);
906 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadNeedsRelocalization",
907 reload_reason_counts[NEEDS_RELOCALIZATION]);
908
[email protected]cc2c3432009-11-06 17:24:36909 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadAll", extensions_.size());
910 UMA_HISTOGRAM_COUNTS_100("Extensions.Disabled", disabled_extensions_.size());
911
[email protected]1952c7d2010-03-04 23:48:34912 UMA_HISTOGRAM_TIMES("Extensions.LoadAllTime",
913 base::TimeTicks::Now() - start_time);
[email protected]cc2c3432009-11-06 17:24:36914
[email protected]9b217652010-10-08 22:04:23915 int app_count = 0;
916 int hosted_app_count = 0;
917 int packaged_app_count = 0;
[email protected]1952c7d2010-03-04 23:48:34918 int user_script_count = 0;
919 int extension_count = 0;
920 int theme_count = 0;
921 int external_count = 0;
922 int page_action_count = 0;
923 int browser_action_count = 0;
924 ExtensionList::iterator ex;
925 for (ex = extensions_.begin(); ex != extensions_.end(); ++ex) {
[email protected]9b217652010-10-08 22:04:23926 Extension::Location location = (*ex)->location();
[email protected]7fa19f82010-12-21 19:40:08927 Extension::Type type = (*ex)->GetType();
[email protected]9b217652010-10-08 22:04:23928 if ((*ex)->is_app()) {
929 UMA_HISTOGRAM_ENUMERATION("Extensions.AppLocation",
930 location, 100);
931 } else if (type == Extension::TYPE_EXTENSION) {
932 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionLocation",
933 location, 100);
934 }
935
[email protected]1952c7d2010-03-04 23:48:34936 // Don't count component extensions, since they are only extensions as an
937 // implementation detail.
[email protected]9b217652010-10-08 22:04:23938 if (location == Extension::COMPONENT)
[email protected]1952c7d2010-03-04 23:48:34939 continue;
940
[email protected]e8c729a2010-03-09 19:55:19941 // Don't count unpacked extensions, since they're a developer-specific
942 // feature.
[email protected]9b217652010-10-08 22:04:23943 if (location == Extension::LOAD)
[email protected]e8c729a2010-03-09 19:55:19944 continue;
945
[email protected]9b217652010-10-08 22:04:23946 // Using an enumeration shows us the total installed ratio across all users.
947 // Using the totals per user at each startup tells us the distribution of
948 // usage for each user (e.g. 40% of users have at least one app installed).
949 UMA_HISTOGRAM_ENUMERATION("Extensions.LoadType", type, 100);
950 switch (type) {
951 case Extension::TYPE_THEME:
[email protected]a315ba92010-11-16 14:12:21952 ++theme_count;
[email protected]9b217652010-10-08 22:04:23953 break;
954 case Extension::TYPE_USER_SCRIPT:
[email protected]a315ba92010-11-16 14:12:21955 ++user_script_count;
[email protected]9b217652010-10-08 22:04:23956 break;
957 case Extension::TYPE_HOSTED_APP:
[email protected]a315ba92010-11-16 14:12:21958 ++app_count;
959 ++hosted_app_count;
[email protected]9b217652010-10-08 22:04:23960 break;
961 case Extension::TYPE_PACKAGED_APP:
[email protected]a315ba92010-11-16 14:12:21962 ++app_count;
963 ++packaged_app_count;
[email protected]9b217652010-10-08 22:04:23964 break;
965 case Extension::TYPE_EXTENSION:
966 default:
[email protected]a315ba92010-11-16 14:12:21967 ++extension_count;
[email protected]9b217652010-10-08 22:04:23968 break;
[email protected]cc2c3432009-11-06 17:24:36969 }
[email protected]9b217652010-10-08 22:04:23970 if (Extension::IsExternalLocation(location))
[email protected]a315ba92010-11-16 14:12:21971 ++external_count;
[email protected]9b217652010-10-08 22:04:23972 if ((*ex)->page_action() != NULL)
[email protected]a315ba92010-11-16 14:12:21973 ++page_action_count;
[email protected]9b217652010-10-08 22:04:23974 if ((*ex)->browser_action() != NULL)
[email protected]a315ba92010-11-16 14:12:21975 ++browser_action_count;
[email protected]cc2c3432009-11-06 17:24:36976 }
[email protected]9b217652010-10-08 22:04:23977 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadApp", app_count);
978 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadHostedApp", hosted_app_count);
979 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadPackagedApp", packaged_app_count);
[email protected]1952c7d2010-03-04 23:48:34980 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExtension", extension_count);
981 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadUserScript", user_script_count);
982 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadTheme", theme_count);
983 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExternal", external_count);
984 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadPageAction", page_action_count);
985 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadBrowserAction",
986 browser_action_count);
[email protected]ae09ca62009-08-21 19:46:46987}
988
[email protected]eaa7dd182010-12-14 11:09:00989void ExtensionService::LoadInstalledExtension(const ExtensionInfo& info,
[email protected]542258c2011-03-04 21:25:31990 bool write_to_prefs) {
[email protected]ae09ca62009-08-21 19:46:46991 std::string error;
[email protected]9adb9692010-10-29 23:14:02992 scoped_refptr<const Extension> extension(NULL);
[email protected]306a2bd2010-08-11 14:56:36993 if (!extension_prefs_->IsExtensionAllowedByPolicy(info.extension_id)) {
994 error = errors::kDisabledByPolicy;
995 } else if (info.extension_manifest.get()) {
[email protected]83048a22011-03-29 00:14:13996 int flags = Extension::NO_FLAGS;
997 if (info.extension_location != Extension::LOAD)
998 flags |= Extension::REQUIRE_KEY;
999 if (Extension::ShouldDoStrictErrorChecking(info.extension_location))
1000 flags |= Extension::STRICT_ERROR_CHECKS;
[email protected]3aff9ad2011-04-01 20:26:481001 if (extension_prefs_->AllowFileAccess(info.extension_id))
1002 flags |= Extension::ALLOW_FILE_ACCESS;
[email protected]66e4eb32010-10-27 20:37:411003 extension = Extension::Create(
[email protected]542258c2011-03-04 21:25:311004 info.extension_path,
1005 info.extension_location,
1006 *info.extension_manifest,
[email protected]83048a22011-03-29 00:14:131007 flags,
[email protected]542258c2011-03-04 21:25:311008 &error);
[email protected]ae09ca62009-08-21 19:46:461009 } else {
[email protected]c6d474f82009-12-16 21:11:061010 error = errors::kManifestUnreadable;
[email protected]ae09ca62009-08-21 19:46:461011 }
1012
1013 if (!extension) {
[email protected]c6d474f82009-12-16 21:11:061014 ReportExtensionLoadError(info.extension_path,
[email protected]d11c8e92009-10-20 23:26:401015 error,
1016 NotificationType::EXTENSION_INSTALL_ERROR,
1017 false);
[email protected]ae09ca62009-08-21 19:46:461018 return;
1019 }
1020
[email protected]c6d474f82009-12-16 21:11:061021 if (write_to_prefs)
1022 extension_prefs_->UpdateManifest(extension);
1023
[email protected]49cd1572011-02-08 21:38:451024 AddExtension(extension);
[email protected]9f1087e2009-06-15 17:29:321025}
1026
[email protected]eaa7dd182010-12-14 11:09:001027void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:421028 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061029 // was loaded, otherwise a race can arise where a renderer that is created
1030 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421031 // that the request context doesn't yet know about. The profile is responsible
1032 // for ensuring its URLRequestContexts appropriately discover the loaded
1033 // extension.
1034 if (profile_) {
1035 profile_->RegisterExtensionWithRequestContexts(extension);
[email protected]19eb80152011-02-26 00:28:431036 profile_->GetExtensionSpecialStoragePolicy()->
1037 GrantRightsForExtension(extension);
[email protected]62d30f42009-10-01 22:36:061038 }
1039
1040 NotificationService::current()->Notify(
1041 NotificationType::EXTENSION_LOADED,
[email protected]24e7a9d2009-11-04 11:11:341042 Source<Profile>(profile_),
[email protected]9adb9692010-10-29 23:14:021043 Details<const Extension>(extension));
[email protected]2de307592011-04-05 21:16:581044
1045 bool plugins_changed = false;
1046 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1047 const Extension::PluginInfo& plugin = extension->plugins()[i];
1048 webkit::npapi::PluginList::Singleton()->RefreshPlugins();
1049 webkit::npapi::PluginList::Singleton()->AddExtraPluginPath(plugin.path);
1050 plugins_changed = true;
1051 if (!plugin.is_public) {
1052 PluginService::GetInstance()->RestrictPluginToUrl(
1053 plugin.path, extension->url());
1054 }
1055 }
[email protected]84396dbc2011-04-14 06:33:421056
1057 bool nacl_modules_changed = false;
1058 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1059 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
1060 PepperPluginRegistry::GetInstance()->RegisterNaClModule(module.url,
1061 module.mime_type);
1062 nacl_modules_changed = true;
1063 }
1064
1065 if (nacl_modules_changed)
1066 PepperPluginRegistry::GetInstance()->UpdatePluginListWithNaClModules();
1067
1068 if (plugins_changed || nacl_modules_changed)
[email protected]2de307592011-04-05 21:16:581069 PluginService::GetInstance()->PurgePluginListCache(false);
[email protected]62d30f42009-10-01 22:36:061070}
1071
[email protected]a9f39a312010-12-23 22:14:271072void ExtensionService::NotifyExtensionUnloaded(
1073 const Extension* extension, UnloadedExtensionInfo::Reason reason) {
1074 UnloadedExtensionInfo details(extension, reason);
[email protected]62d30f42009-10-01 22:36:061075 NotificationService::current()->Notify(
1076 NotificationType::EXTENSION_UNLOADED,
[email protected]24e7a9d2009-11-04 11:11:341077 Source<Profile>(profile_),
[email protected]a9f39a312010-12-23 22:14:271078 Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061079
[email protected]57a777f72010-03-31 01:09:421080 if (profile_) {
1081 profile_->UnregisterExtensionWithRequestContexts(extension);
[email protected]19eb80152011-02-26 00:28:431082 profile_->GetExtensionSpecialStoragePolicy()->
1083 RevokeRightsForExtension(extension);
[email protected]62d30f42009-10-01 22:36:061084 }
[email protected]2de307592011-04-05 21:16:581085
1086 bool plugins_changed = false;
1087 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1088 const Extension::PluginInfo& plugin = extension->plugins()[i];
1089 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
1090 NewRunnableFunction(&ForceShutdownPlugin,
1091 plugin.path));
1092 webkit::npapi::PluginList::Singleton()->RefreshPlugins();
1093 webkit::npapi::PluginList::Singleton()->RemoveExtraPluginPath(
1094 plugin.path);
1095 plugins_changed = true;
1096 if (!plugin.is_public)
1097 PluginService::GetInstance()->RestrictPluginToUrl(plugin.path, GURL());
1098 }
[email protected]84396dbc2011-04-14 06:33:421099
1100 bool nacl_modules_changed = false;
1101 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1102 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
1103 PepperPluginRegistry::GetInstance()->UnregisterNaClModule(module.url);
1104 nacl_modules_changed = true;
1105 }
1106
1107 if (nacl_modules_changed)
1108 PepperPluginRegistry::GetInstance()->UpdatePluginListWithNaClModules();
1109
1110 if (plugins_changed || nacl_modules_changed)
[email protected]2de307592011-04-05 21:16:581111 PluginService::GetInstance()->PurgePluginListCache(false);
[email protected]62d30f42009-10-01 22:36:061112}
1113
[email protected]eaa7dd182010-12-14 11:09:001114void ExtensionService::UpdateExtensionBlacklist(
[email protected]6b75ec32009-08-14 06:37:181115 const std::vector<std::string>& blacklist) {
1116 // Use this set to indicate if an extension in the blacklist has been used.
1117 std::set<std::string> blacklist_set;
1118 for (unsigned int i = 0; i < blacklist.size(); ++i) {
1119 if (Extension::IdIsValid(blacklist[i])) {
1120 blacklist_set.insert(blacklist[i]);
1121 }
1122 }
1123 extension_prefs_->UpdateBlacklist(blacklist_set);
1124 std::vector<std::string> to_be_removed;
1125 // Loop current extensions, unload installed extensions.
1126 for (ExtensionList::const_iterator iter = extensions_.begin();
1127 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021128 const Extension* extension = (*iter);
[email protected]6b75ec32009-08-14 06:37:181129 if (blacklist_set.find(extension->id()) != blacklist_set.end()) {
1130 to_be_removed.push_back(extension->id());
1131 }
1132 }
1133
1134 // UnloadExtension will change the extensions_ list. So, we should
1135 // call it outside the iterator loop.
1136 for (unsigned int i = 0; i < to_be_removed.size(); ++i) {
[email protected]a9f39a312010-12-23 22:14:271137 UnloadExtension(to_be_removed[i], UnloadedExtensionInfo::DISABLE);
[email protected]6b75ec32009-08-14 06:37:181138 }
1139}
1140
[email protected]0a071a32011-02-08 00:18:241141Profile* ExtensionService::profile() {
1142 return profile_;
1143}
1144
[email protected]eaa7dd182010-12-14 11:09:001145void ExtensionService::DestroyingProfile() {
[email protected]94b4ffc2011-02-04 05:55:171146 if (updater_.get()) {
1147 updater_->Stop();
1148 }
[email protected]97d2f1d2011-01-15 00:41:081149 browser_event_router_.reset();
[email protected]118de7892011-03-16 13:31:401150 preference_event_router_.reset();
[email protected]2fb7dc982010-09-29 12:24:281151 pref_change_registrar_.RemoveAll();
[email protected]aa96d3a2010-08-21 08:45:251152 profile_ = NULL;
[email protected]2fb7dc982010-09-29 12:24:281153 toolbar_model_.DestroyingProfile();
[email protected]aa96d3a2010-08-21 08:45:251154}
1155
[email protected]eaa7dd182010-12-14 11:09:001156ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451157 return extension_prefs_;
1158}
1159
[email protected]2859946f2011-04-04 18:18:061160ExtensionUpdater* ExtensionService::updater() {
1161 return updater_.get();
1162}
1163
[email protected]eaa7dd182010-12-14 11:09:001164void ExtensionService::CheckAdminBlacklist() {
[email protected]aa96d3a2010-08-21 08:45:251165 std::vector<std::string> to_be_removed;
1166 // Loop through extensions list, unload installed extensions.
1167 for (ExtensionList::const_iterator iter = extensions_.begin();
1168 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021169 const Extension* extension = (*iter);
[email protected]aa96d3a2010-08-21 08:45:251170 if (!extension_prefs_->IsExtensionAllowedByPolicy(extension->id()))
1171 to_be_removed.push_back(extension->id());
1172 }
1173
1174 // UnloadExtension will change the extensions_ list. So, we should
1175 // call it outside the iterator loop.
1176 for (unsigned int i = 0; i < to_be_removed.size(); ++i)
[email protected]a9f39a312010-12-23 22:14:271177 UnloadExtension(to_be_removed[i], UnloadedExtensionInfo::DISABLE);
[email protected]aa96d3a2010-08-21 08:45:251178}
1179
[email protected]31206602011-04-13 23:07:321180void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351181 if (updater()) {
[email protected]31206602011-04-13 23:07:321182 updater()->CheckSoon();
[email protected]c3cfb012011-04-06 22:07:351183 } else {
[email protected]31206602011-04-13 23:07:321184 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351185 }
1186}
1187
1188bool ExtensionService::IsIncognitoEnabled(
1189 const std::string& extension_id) const {
1190 // If this is an existing component extension we always allow it to
1191 // work in incognito mode.
1192 const Extension* extension = GetExtensionById(extension_id, true);
1193 if (extension && extension->location() == Extension::COMPONENT)
[email protected]cb0ce1e022010-03-10 19:54:411194 return true;
1195
1196 // Check the prefs.
[email protected]c3cfb012011-04-06 22:07:351197 return extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]db7331a2010-02-25 22:10:501198}
[email protected]55a35692010-02-11 23:25:211199
[email protected]c3cfb012011-04-06 22:07:351200void ExtensionService::SetIsIncognitoEnabled(
1201 const std::string& extension_id, bool enabled) {
1202 const Extension* extension = GetExtensionById(extension_id, false);
1203 if (extension && extension->location() == Extension::COMPONENT) {
1204 // This shouldn't be called for component extensions.
1205 NOTREACHED();
1206 return;
1207 }
1208
[email protected]8c6c8c22011-03-09 12:52:341209 // Broadcast unloaded and loaded events to update browser state. Only bother
1210 // if the value changed and the extension is actually enabled, since there is
1211 // no UI otherwise.
[email protected]c3cfb012011-04-06 22:07:351212 bool old_enabled = extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]8c6c8c22011-03-09 12:52:341213 if (enabled == old_enabled)
1214 return;
1215
[email protected]c3cfb012011-04-06 22:07:351216 extension_prefs_->SetIsIncognitoEnabled(extension_id, enabled);
1217 if (extension) {
[email protected]a9f39a312010-12-23 22:14:271218 NotifyExtensionUnloaded(extension, UnloadedExtensionInfo::DISABLE);
[email protected]568f33d2010-08-04 17:06:411219 NotifyExtensionLoaded(extension);
1220 }
[email protected]55a35692010-02-11 23:25:211221}
1222
[email protected]eaa7dd182010-12-14 11:09:001223bool ExtensionService::CanCrossIncognito(const Extension* extension) {
[email protected]2a8f24e2010-11-03 21:37:051224 // We allow the extension to see events and data from another profile iff it
1225 // uses "spanning" behavior and it has incognito access. "split" mode
1226 // extensions only see events for a matching profile.
[email protected]c3cfb012011-04-06 22:07:351227 return IsIncognitoEnabled(extension->id()) &&
1228 !extension->incognito_split_mode();
[email protected]2a8f24e2010-11-03 21:37:051229}
1230
[email protected]eaa7dd182010-12-14 11:09:001231bool ExtensionService::AllowFileAccess(const Extension* extension) {
[email protected]05c82182010-06-24 17:49:081232 return (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]334e04a2010-06-24 23:34:441233 switches::kDisableExtensionsFileAccessCheck) ||
[email protected]05c82182010-06-24 17:49:081234 extension_prefs_->AllowFileAccess(extension->id()));
1235}
1236
[email protected]eaa7dd182010-12-14 11:09:001237void ExtensionService::SetAllowFileAccess(const Extension* extension,
[email protected]3aff9ad2011-04-01 20:26:481238 bool allow) {
1239 // Reload to update browser state. Only bother if the value changed and the
1240 // extension is actually enabled, since there is no UI otherwise.
1241 bool old_allow = AllowFileAccess(extension);
1242 if (allow == old_allow)
1243 return;
1244
[email protected]05c82182010-06-24 17:49:081245 extension_prefs_->SetAllowFileAccess(extension->id(), allow);
[email protected]3aff9ad2011-04-01 20:26:481246
1247 bool extension_is_enabled = std::find(extensions_.begin(), extensions_.end(),
1248 extension) != extensions_.end();
1249 if (extension_is_enabled)
1250 ReloadExtension(extension->id());
[email protected]05c82182010-06-24 17:49:081251}
1252
[email protected]eaa7dd182010-12-14 11:09:001253bool ExtensionService::GetBrowserActionVisibility(const Extension* extension) {
[email protected]f681c782010-11-19 11:19:391254 return extension_prefs_->GetBrowserActionVisibility(extension);
1255}
1256
[email protected]eaa7dd182010-12-14 11:09:001257void ExtensionService::SetBrowserActionVisibility(const Extension* extension,
[email protected]8e4560b62011-01-14 10:09:141258 bool visible) {
[email protected]f681c782010-11-19 11:19:391259 extension_prefs_->SetBrowserActionVisibility(extension, visible);
1260}
1261
[email protected]8e4560b62011-01-14 10:09:141262// Some extensions will autoupdate themselves externally from Chrome. These
1263// are typically part of some larger client application package. To support
1264// these, the extension will register its location in the the preferences file
1265// (and also, on Windows, in the registry) and this code will periodically
1266// check that location for a .crx file, which it will then install locally if
1267// a new version is available.
1268// Errors are reported through ExtensionErrorReporter. Succcess is not
1269// reported.
[email protected]eaa7dd182010-12-14 11:09:001270void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121271 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141272
1273 // Note that this installation is intentionally silent (since it didn't
1274 // go through the front-end). Extensions that are registered in this
1275 // way are effectively considered 'pre-bundled', and so implicitly
1276 // trusted. In general, if something has HKLM or filesystem access,
1277 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121278
1279 // If any external extension records give a URL, a provider will set
1280 // this to true. Used by OnExternalProviderReady() to see if we need
1281 // to start an update check to fetch a new external extension.
1282 external_extension_url_added_ = false;
[email protected]8e4560b62011-01-14 10:09:141283
1284 // Ask each external extension provider to give us a call back for each
1285 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
1286 ProviderCollection::const_iterator i;
1287 for (i = external_extension_providers_.begin();
1288 i != external_extension_providers_.end(); ++i) {
1289 ExternalExtensionProviderInterface* provider = i->get();
1290 provider->VisitRegisteredExtension();
1291 }
1292
1293 // Uninstall of unclaimed extensions will happen after all the providers
[email protected]a29a517a2011-01-21 21:11:121294 // had reported ready. Every provider calls OnExternalProviderReady()
1295 // when it finishes, and OnExternalProviderReady() only acts when all
1296 // providers are ready. In case there are no providers, we call it
1297 // to trigger removal of extensions that used to have an external source.
1298 if (external_extension_providers_.empty())
1299 OnExternalProviderReady();
[email protected]9f1087e2009-06-15 17:29:321300}
1301
[email protected]8e4560b62011-01-14 10:09:141302void ExtensionService::OnExternalProviderReady() {
[email protected]a29a517a2011-01-21 21:11:121303 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1304
1305 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141306 // if all of them are finished. So we check them first.
1307 ProviderCollection::const_iterator i;
1308 for (i = external_extension_providers_.begin();
1309 i != external_extension_providers_.end(); ++i) {
1310 ExternalExtensionProviderInterface* provider = i->get();
[email protected]a29a517a2011-01-21 21:11:121311 if (!provider->IsReady())
[email protected]8e4560b62011-01-14 10:09:141312 return;
[email protected]8e4560b62011-01-14 10:09:141313 }
1314
[email protected]a29a517a2011-01-21 21:11:121315 // All the providers are ready. Install any pending extensions.
1316 if (external_extension_url_added_ && updater()) {
1317 external_extension_url_added_ = false;
[email protected]8e4560b62011-01-14 10:09:141318 updater()->CheckNow();
1319 }
1320
1321 // Uninstall all the unclaimed extensions.
1322 scoped_ptr<ExtensionPrefs::ExtensionsInfo> extensions_info(
1323 extension_prefs_->GetInstalledExtensionsInfo());
1324 for (size_t i = 0; i < extensions_info->size(); ++i) {
1325 ExtensionInfo* info = extensions_info->at(i).get();
1326 if (Extension::IsExternalLocation(info->extension_location))
1327 CheckExternalUninstall(info->extension_id);
1328 }
[email protected]6c751e72010-11-23 10:11:101329}
1330
[email protected]a9f39a312010-12-23 22:14:271331void ExtensionService::UnloadExtension(
1332 const std::string& extension_id,
1333 UnloadedExtensionInfo::Reason reason) {
[email protected]27e469a2010-01-11 20:35:091334 // Make sure the extension gets deleted after we return from this function.
[email protected]9adb9692010-10-29 23:14:021335 scoped_refptr<const Extension> extension(
[email protected]0c6da502009-08-14 22:32:391336 GetExtensionByIdInternal(extension_id, true, true));
[email protected]631cf822009-05-15 07:01:251337
[email protected]fa0c96732010-11-17 00:14:231338 // This method can be called via PostTask, so the extension may have been
1339 // unloaded by the time this runs.
1340 if (!extension)
1341 return;
[email protected]0c6da502009-08-14 22:32:391342
[email protected]1eb175082010-02-10 09:26:161343 // Keep information about the extension so that we can reload it later
1344 // even if it's not permanently installed.
1345 unloaded_extension_paths_[extension->id()] = extension->path();
1346
[email protected]f17dbd42010-08-16 23:21:101347 // Clean up if the extension is meant to be enabled after a reload.
1348 disabled_extension_paths_.erase(extension->id());
1349
[email protected]d7e9a862010-11-03 21:57:491350 // Clean up runtime data.
1351 extension_runtime_data_.erase(extension_id);
1352
[email protected]8f9d4eb2011-02-05 01:39:101353 ExtensionWebUI::UnregisterChromeURLOverrides(profile_,
[email protected]86c008e82009-08-28 20:26:051354 extension->GetChromeURLOverrides());
1355
[email protected]0c6da502009-08-14 22:32:391356 ExtensionList::iterator iter = std::find(disabled_extensions_.begin(),
1357 disabled_extensions_.end(),
1358 extension.get());
1359 if (iter != disabled_extensions_.end()) {
[email protected]a9f39a312010-12-23 22:14:271360 UnloadedExtensionInfo details(extension, reason);
1361 details.already_disabled = true;
[email protected]0c6da502009-08-14 22:32:391362 disabled_extensions_.erase(iter);
[email protected]866930682009-08-18 22:53:471363 NotificationService::current()->Notify(
[email protected]a9f39a312010-12-23 22:14:271364 NotificationType::EXTENSION_UNLOADED,
[email protected]24e7a9d2009-11-04 11:11:341365 Source<Profile>(profile_),
[email protected]a9f39a312010-12-23 22:14:271366 Details<UnloadedExtensionInfo>(&details));
[email protected]0c6da502009-08-14 22:32:391367 return;
1368 }
1369
1370 iter = std::find(extensions_.begin(), extensions_.end(), extension.get());
[email protected]894bb502009-05-21 22:39:571371
[email protected]631cf822009-05-15 07:01:251372 // Remove the extension from our list.
1373 extensions_.erase(iter);
1374
[email protected]a9f39a312010-12-23 22:14:271375 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]aab98a52009-12-02 03:22:351376 UpdateActiveExtensionsInCrashReporter();
[email protected]631cf822009-05-15 07:01:251377}
1378
[email protected]eaa7dd182010-12-14 11:09:001379void ExtensionService::UnloadAllExtensions() {
[email protected]19eb80152011-02-26 00:28:431380 if (profile_) {
1381 profile_->GetExtensionSpecialStoragePolicy()->
1382 RevokeRightsForAllExtensions();
1383 }
[email protected]9f1087e2009-06-15 17:29:321384 extensions_.clear();
[email protected]cd500f72010-06-25 23:44:321385 disabled_extensions_.clear();
[email protected]bb7f40952011-01-13 00:21:201386 terminated_extension_ids_.clear();
1387 terminated_extensions_.clear();
[email protected]d7e9a862010-11-03 21:57:491388 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:321389
[email protected]c6e4a3412009-06-24 15:45:291390 // TODO(erikkay) should there be a notification for this? We can't use
1391 // EXTENSION_UNLOADED since that implies that the extension has been disabled
1392 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:321393}
1394
[email protected]eaa7dd182010-12-14 11:09:001395void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:321396 UnloadAllExtensions();
1397 LoadAllExtensions();
1398}
1399
[email protected]eaa7dd182010-12-14 11:09:001400void ExtensionService::GarbageCollectExtensions() {
[email protected]39d9f62c2010-12-03 10:48:501401 if (extension_prefs_->pref_service()->ReadOnly())
[email protected]ba399672010-04-06 15:42:391402 return;
1403
[email protected]ca3dbf52010-05-19 22:27:061404 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(
1405 extension_prefs_->GetInstalledExtensionsInfo());
1406
1407 std::map<std::string, FilePath> extension_paths;
1408 for (size_t i = 0; i < info->size(); ++i)
1409 extension_paths[info->at(i)->extension_id] = info->at(i)->extension_path;
1410
[email protected]ca4b5fa32010-10-09 12:42:181411 BrowserThread::PostTask(
1412 BrowserThread::FILE, FROM_HERE,
[email protected]95d29192009-10-30 01:49:061413 NewRunnableFunction(
1414 &extension_file_util::GarbageCollectExtensions, install_directory_,
[email protected]ca3dbf52010-05-19 22:27:061415 extension_paths));
[email protected]f6ccd652010-11-17 00:48:341416
1417 // Also garbage-collect themes. We check |profile_| to be
1418 // defensive; in the future, we may call GarbageCollectExtensions()
1419 // from somewhere other than Init() (e.g., in a timer).
1420 if (profile_) {
[email protected]18280372011-03-22 18:05:221421 ThemeServiceFactory::GetForProfile(profile_)->RemoveUnusedThemes();
[email protected]f6ccd652010-11-17 00:48:341422 }
[email protected]3cf4f0992009-02-03 23:00:301423}
1424
[email protected]eaa7dd182010-12-14 11:09:001425void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]93fd78f42009-07-10 16:43:171426 if (updater_.get()) {
1427 updater_->Start();
1428 }
[email protected]fa6a9102010-11-22 15:38:501429
1430 ready_ = true;
[email protected]e72e8eb82009-06-18 17:21:511431 NotificationService::current()->Notify(
1432 NotificationType::EXTENSIONS_READY,
[email protected]24e7a9d2009-11-04 11:11:341433 Source<Profile>(profile_),
[email protected]e72e8eb82009-06-18 17:21:511434 NotificationService::NoDetails());
1435}
1436
[email protected]49cd1572011-02-08 21:38:451437void ExtensionService::AddExtension(const Extension* extension) {
[email protected]ae09ca62009-08-21 19:46:461438 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:021439 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]9f1087e2009-06-15 17:29:321440
[email protected]1eb175082010-02-10 09:26:161441 // The extension is now loaded, remove its data from unloaded extension map.
1442 unloaded_extension_paths_.erase(extension->id());
1443
[email protected]bb7f40952011-01-13 00:21:201444 // If a terminated extension is loaded, remove it from the terminated list.
1445 UntrackTerminatedExtension(extension->id());
1446
[email protected]f17dbd42010-08-16 23:21:101447 // If the extension was disabled for a reload, then enable it.
1448 if (disabled_extension_paths_.erase(extension->id()) > 0)
1449 EnableExtension(extension->id());
1450
[email protected]d728e002010-12-08 04:46:231451 // TODO(jstritar): We may be able to get rid of this branch by overriding the
1452 // default extension state to DISABLED when the --disable-extensions flag
1453 // is set (http://crbug.com/29067).
1454 if (!extensions_enabled() &&
1455 !extension->is_theme() &&
[email protected]aebe23a32010-12-10 22:15:481456 extension->location() != Extension::COMPONENT &&
1457 !Extension::IsExternalLocation(extension->location()))
[email protected]d728e002010-12-08 04:46:231458 return;
1459
1460 // Check if the extension's privileges have changed and disable the
1461 // extension if necessary.
[email protected]8d888c12010-11-30 00:00:251462 DisableIfPrivilegeIncrease(extension);
[email protected]0c6da502009-08-14 22:32:391463
[email protected]8d888c12010-11-30 00:00:251464 switch (extension_prefs_->GetExtensionState(extension->id())) {
1465 case Extension::ENABLED:
1466 extensions_.push_back(scoped_extension);
[email protected]1e8c93f2010-02-08 22:58:311467
[email protected]8d888c12010-11-30 00:00:251468 NotifyExtensionLoaded(extension);
[email protected]0c6da502009-08-14 22:32:391469
[email protected]8f9d4eb2011-02-05 01:39:101470 ExtensionWebUI::RegisterChromeURLOverrides(
[email protected]8d888c12010-11-30 00:00:251471 profile_, extension->GetChromeURLOverrides());
1472 break;
1473 case Extension::DISABLED:
1474 disabled_extensions_.push_back(scoped_extension);
1475 NotificationService::current()->Notify(
1476 NotificationType::EXTENSION_UPDATE_DISABLED,
1477 Source<Profile>(profile_),
1478 Details<const Extension>(extension));
1479 break;
1480 default:
1481 NOTREACHED();
1482 break;
[email protected]e72e8eb82009-06-18 17:21:511483 }
[email protected]aab98a52009-12-02 03:22:351484
[email protected]d7e9a862010-11-03 21:57:491485 SetBeingUpgraded(extension, false);
[email protected]1e8c93f2010-02-08 22:58:311486
[email protected]aab98a52009-12-02 03:22:351487 UpdateActiveExtensionsInCrashReporter();
[email protected]0b004da2010-07-02 17:54:311488
1489 if (profile_->GetTemplateURLModel())
1490 profile_->GetTemplateURLModel()->RegisterExtensionKeyword(extension);
[email protected]b671760b2010-07-15 21:13:471491
1492 // Load the icon for omnibox-enabled extensions so it will be ready to display
1493 // in the URL bar.
[email protected]29d0d4ac2010-09-08 21:10:311494 if (!extension->omnibox_keyword().empty()) {
1495 omnibox_popup_icon_manager_.LoadIcon(extension);
[email protected]b671760b2010-07-15 21:13:471496 omnibox_icon_manager_.LoadIcon(extension);
[email protected]29d0d4ac2010-09-08 21:10:311497 }
[email protected]aab98a52009-12-02 03:22:351498}
1499
[email protected]eaa7dd182010-12-14 11:09:001500void ExtensionService::DisableIfPrivilegeIncrease(const Extension* extension) {
[email protected]8d888c12010-11-30 00:00:251501 // We keep track of all permissions the user has granted each extension.
1502 // This allows extensions to gracefully support backwards compatibility
1503 // by including unknown permissions in their manifests. When the user
1504 // installs the extension, only the recognized permissions are recorded.
1505 // When the unknown permissions become recognized (e.g., through browser
1506 // upgrade), we can prompt the user to accept these new permissions.
1507 // Extensions can also silently upgrade to less permissions, and then
1508 // silently upgrade to a version that adds these permissions back.
1509 //
1510 // For example, pretend that Chrome 10 includes a permission "omnibox"
1511 // for an API that adds suggestions to the omnibox. An extension can
1512 // maintain backwards compatibility while still having "omnibox" in the
1513 // manifest. If a user installs the extension on Chrome 9, the browser
1514 // will record the permissions it recognized, not including "omnibox."
1515 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
1516 // will disable the extension and prompt the user to approve the increase
1517 // in privileges. The extension could then release a new version that
1518 // removes the "omnibox" permission. When the user upgrades, Chrome will
1519 // still remember that "omnibox" had been granted, so that if the
1520 // extension once again includes "omnibox" in an upgrade, the extension
1521 // can upgrade without requiring this user's approval.
1522 const Extension* old = GetExtensionByIdInternal(extension->id(),
1523 true, true);
1524 bool granted_full_access;
1525 std::set<std::string> granted_apis;
1526 ExtensionExtent granted_extent;
1527
1528 bool is_extension_upgrade = old != NULL;
1529 bool is_privilege_increase = false;
1530
1531 // We only record the granted permissions for INTERNAL extensions, since
1532 // they can't silently increase privileges.
1533 if (extension->location() == Extension::INTERNAL) {
1534 // Add all the recognized permissions if the granted permissions list
1535 // hasn't been initialized yet.
1536 if (!extension_prefs_->GetGrantedPermissions(extension->id(),
1537 &granted_full_access,
1538 &granted_apis,
1539 &granted_extent)) {
1540 GrantPermissions(extension);
1541 CHECK(extension_prefs_->GetGrantedPermissions(extension->id(),
1542 &granted_full_access,
1543 &granted_apis,
1544 &granted_extent));
1545 }
1546
1547 // Here, we check if an extension's privileges have increased in a manner
1548 // that requires the user's approval. This could occur because the browser
1549 // upgraded and recognized additional privileges, or an extension upgrades
1550 // to a version that requires additional privileges.
1551 is_privilege_increase = Extension::IsPrivilegeIncrease(
1552 granted_full_access, granted_apis, granted_extent, extension);
1553 }
1554
1555 if (is_extension_upgrade) {
[email protected]66e26872010-12-03 20:07:251556 // Other than for unpacked extensions, CrxInstaller should have guaranteed
1557 // that we aren't downgrading.
1558 if (extension->location() != Extension::LOAD)
1559 CHECK(extension->version()->CompareTo(*(old->version())) >= 0);
[email protected]8d888c12010-11-30 00:00:251560
1561 // Extensions get upgraded if the privileges are allowed to increase or
1562 // the privileges haven't increased.
1563 if (!is_privilege_increase) {
1564 SetBeingUpgraded(old, true);
1565 SetBeingUpgraded(extension, true);
1566 }
1567
1568 // To upgrade an extension in place, unload the old one and
1569 // then load the new one.
[email protected]a9f39a312010-12-23 22:14:271570 UnloadExtension(old->id(), UnloadedExtensionInfo::UPDATE);
[email protected]8d888c12010-11-30 00:00:251571 old = NULL;
1572 }
1573
1574 // Extension has changed permissions significantly. Disable it. A
1575 // notification should be sent by the caller.
1576 if (is_privilege_increase) {
1577 extension_prefs_->SetExtensionState(extension, Extension::DISABLED);
1578 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
1579 }
1580}
1581
[email protected]eaa7dd182010-12-14 11:09:001582void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:391583 std::set<std::string> extension_ids;
[email protected]aab98a52009-12-02 03:22:351584 for (size_t i = 0; i < extensions_.size(); ++i) {
[email protected]2a500c1b2010-11-05 15:27:341585 if (!extensions_[i]->is_theme() &&
1586 extensions_[i]->location() != Extension::COMPONENT)
[email protected]c8865962009-12-16 07:47:391587 extension_ids.insert(extensions_[i]->id());
[email protected]aab98a52009-12-02 03:22:351588 }
1589
1590 child_process_logging::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:251591}
1592
[email protected]eaa7dd182010-12-14 11:09:001593void ExtensionService::OnExtensionInstalled(const Extension* extension) {
[email protected]a29a517a2011-01-21 21:11:121594 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:201595
[email protected]4416c5a2010-06-26 01:28:571596 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:021597 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]c3cfb012011-04-06 22:07:351598 const std::string& id = extension->id();
1599 bool initial_enable = false;
[email protected]4416c5a2010-06-26 01:28:571600 bool initial_enable_incognito = false;
[email protected]b2907fd2011-03-25 16:43:371601
1602 PendingExtensionInfo pending_extension_info;
[email protected]c3cfb012011-04-06 22:07:351603 if (pending_extension_manager()->GetById(id, &pending_extension_info)) {
1604 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:321605
[email protected]483d1ff2011-03-03 17:12:021606 if (!pending_extension_info.ShouldAllowInstall(*extension)) {
[email protected]4416c5a2010-06-26 01:28:571607 LOG(WARNING)
[email protected]ae6cb3022011-04-04 20:11:541608 << "ShouldAllowInstall() returned false for "
[email protected]c3cfb012011-04-06 22:07:351609 << id << " of type " << extension->GetType()
[email protected]7fa19f82010-12-21 19:40:081610 << " and update URL " << extension->update_url().spec()
1611 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:541612
1613 NotificationService::current()->Notify(
1614 NotificationType::EXTENSION_INSTALL_NOT_ALLOWED,
1615 Source<Profile>(profile_),
1616 Details<const Extension>(extension));
1617
[email protected]4416c5a2010-06-26 01:28:571618 // Delete the extension directory since we're not going to
1619 // load it.
[email protected]ca4b5fa32010-10-09 12:42:181620 BrowserThread::PostTask(
1621 BrowserThread::FILE, FROM_HERE,
[email protected]b2907fd2011-03-25 16:43:371622 NewRunnableFunction(&extension_file_util::DeleteFile,
1623 extension->path(), true));
[email protected]4416c5a2010-06-26 01:28:571624 return;
1625 }
[email protected]8ef78fd2010-08-19 17:14:321626
[email protected]8ef78fd2010-08-19 17:14:321627 if (extension->is_theme()) {
[email protected]483d1ff2011-03-03 17:12:021628 DCHECK(pending_extension_info.enable_on_install());
[email protected]c3cfb012011-04-06 22:07:351629 initial_enable = true;
[email protected]483d1ff2011-03-03 17:12:021630 DCHECK(!pending_extension_info.enable_incognito_on_install());
[email protected]4416c5a2010-06-26 01:28:571631 initial_enable_incognito = false;
1632 } else {
[email protected]c3cfb012011-04-06 22:07:351633 initial_enable = pending_extension_info.enable_on_install();
[email protected]4416c5a2010-06-26 01:28:571634 initial_enable_incognito =
[email protected]483d1ff2011-03-03 17:12:021635 pending_extension_info.enable_incognito_on_install();
[email protected]4416c5a2010-06-26 01:28:571636 }
[email protected]4416c5a2010-06-26 01:28:571637 } else {
[email protected]c3cfb012011-04-06 22:07:351638 // We explicitly want to re-enable an uninstalled external
1639 // extension; if we're here, that means the user is manually
1640 // installing the extension.
1641 initial_enable =
1642 IsExtensionEnabled(id) || IsExternalExtensionUninstalled(id);
1643 initial_enable_incognito = IsIncognitoEnabled(id);
[email protected]aa142702010-03-26 01:26:331644 }
1645
[email protected]9b217652010-10-08 22:04:231646 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
[email protected]7fa19f82010-12-21 19:40:081647 extension->GetType(), 100);
[email protected]eda3c362010-11-12 08:08:231648 ShownSectionsHandler::OnExtensionInstalled(profile_->GetPrefs(), extension);
[email protected]4416c5a2010-06-26 01:28:571649 extension_prefs_->OnExtensionInstalled(
[email protected]c3cfb012011-04-06 22:07:351650 extension, initial_enable ? Extension::ENABLED : Extension::DISABLED,
1651 initial_enable_incognito);
[email protected]25b34332009-06-05 21:53:191652
[email protected]3aff9ad2011-04-01 20:26:481653 // Unpacked extensions default to allowing file access, but if that has been
1654 // overridden, don't reset the value.
1655 if (Extension::ShouldAlwaysAllowFileAccess(Extension::LOAD) &&
[email protected]c3cfb012011-04-06 22:07:351656 !extension_prefs_->HasAllowFileAccessSetting(id)) {
1657 extension_prefs_->SetAllowFileAccess(id, true);
[email protected]3aff9ad2011-04-01 20:26:481658 }
[email protected]92a5b1d2010-07-20 00:42:001659
[email protected]a3e61e82011-04-15 20:32:081660 NotificationService::current()->Notify(
1661 NotificationType::EXTENSION_INSTALLED,
1662 Source<Profile>(profile_),
1663 Details<const Extension>(extension));
[email protected]7577a5c52009-07-30 06:21:581664
[email protected]49cd1572011-02-08 21:38:451665 // Transfer ownership of |extension| to AddExtension.
1666 AddExtension(scoped_extension);
[email protected]4a190632009-05-09 01:07:421667}
1668
[email protected]eaa7dd182010-12-14 11:09:001669const Extension* ExtensionService::GetExtensionByIdInternal(
[email protected]b2907fd2011-03-25 16:43:371670 const std::string& id, bool include_enabled, bool include_disabled) const {
[email protected]e957fe52009-06-23 16:51:051671 std::string lowercase_id = StringToLowerASCII(id);
[email protected]0c6da502009-08-14 22:32:391672 if (include_enabled) {
1673 for (ExtensionList::const_iterator iter = extensions_.begin();
1674 iter != extensions_.end(); ++iter) {
1675 if ((*iter)->id() == lowercase_id)
1676 return *iter;
1677 }
1678 }
1679 if (include_disabled) {
1680 for (ExtensionList::const_iterator iter = disabled_extensions_.begin();
1681 iter != disabled_extensions_.end(); ++iter) {
1682 if ((*iter)->id() == lowercase_id)
1683 return *iter;
1684 }
[email protected]ce5c4502009-05-06 16:46:111685 }
1686 return NULL;
1687}
1688
[email protected]bb7f40952011-01-13 00:21:201689void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
1690 if (terminated_extension_ids_.insert(extension->id()).second)
1691 terminated_extensions_.push_back(make_scoped_refptr(extension));
1692}
1693
1694void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
1695 if (terminated_extension_ids_.erase(id) <= 0)
1696 return;
1697
1698 std::string lowercase_id = StringToLowerASCII(id);
1699 for (ExtensionList::iterator iter = terminated_extensions_.begin();
1700 iter != terminated_extensions_.end(); ++iter) {
1701 if ((*iter)->id() == lowercase_id) {
1702 terminated_extensions_.erase(iter);
1703 return;
1704 }
1705 }
1706}
1707
[email protected]0dfe05c2011-02-23 23:03:361708const Extension* ExtensionService::GetTerminatedExtension(
1709 const std::string& id) {
1710 std::string lowercase_id = StringToLowerASCII(id);
1711 for (ExtensionList::const_iterator iter = terminated_extensions_.begin();
1712 iter != terminated_extensions_.end(); ++iter) {
1713 if ((*iter)->id() == lowercase_id)
1714 return *iter;
1715 }
1716 return NULL;
1717}
1718
[email protected]eaa7dd182010-12-14 11:09:001719const Extension* ExtensionService::GetWebStoreApp() {
[email protected]d3071992010-10-08 15:24:071720 return GetExtensionById(extension_misc::kWebStoreAppId, false);
1721}
1722
[email protected]eaa7dd182010-12-14 11:09:001723const Extension* ExtensionService::GetExtensionByURL(const GURL& url) {
[email protected]a888b29e62010-04-01 13:38:571724 return url.scheme() != chrome::kExtensionScheme ? NULL :
1725 GetExtensionById(url.host(), false);
1726}
1727
[email protected]eaa7dd182010-12-14 11:09:001728const Extension* ExtensionService::GetExtensionByWebExtent(const GURL& url) {
[email protected]a888b29e62010-04-01 13:38:571729 for (size_t i = 0; i < extensions_.size(); ++i) {
1730 if (extensions_[i]->web_extent().ContainsURL(url))
1731 return extensions_[i];
1732 }
1733 return NULL;
[email protected]9f1087e2009-06-15 17:29:321734}
1735
[email protected]eaa7dd182010-12-14 11:09:001736bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]583d45c12010-08-31 02:48:121737 // Allow bindings for all packaged extension.
1738 if (GetExtensionByURL(url))
1739 return true;
1740
1741 // Allow bindings for all component, hosted apps.
[email protected]9adb9692010-10-29 23:14:021742 const Extension* extension = GetExtensionByWebExtent(url);
[email protected]583d45c12010-08-31 02:48:121743 return (extension && extension->location() == Extension::COMPONENT);
1744}
1745
[email protected]eaa7dd182010-12-14 11:09:001746const Extension* ExtensionService::GetExtensionByOverlappingWebExtent(
[email protected]9f72aa02010-06-25 10:01:051747 const ExtensionExtent& extent) {
[email protected]22c966c2010-06-26 06:35:021748 for (size_t i = 0; i < extensions_.size(); ++i) {
1749 if (extensions_[i]->web_extent().OverlapsWith(extent))
1750 return extensions_[i];
1751 }
1752
[email protected]6d2e60bd2010-06-03 22:37:391753 return NULL;
1754}
1755
[email protected]eaa7dd182010-12-14 11:09:001756const SkBitmap& ExtensionService::GetOmniboxIcon(
[email protected]b671760b2010-07-15 21:13:471757 const std::string& extension_id) {
1758 return omnibox_icon_manager_.GetIcon(extension_id);
1759}
1760
[email protected]eaa7dd182010-12-14 11:09:001761const SkBitmap& ExtensionService::GetOmniboxPopupIcon(
[email protected]29d0d4ac2010-09-08 21:10:311762 const std::string& extension_id) {
1763 return omnibox_popup_icon_manager_.GetIcon(extension_id);
1764}
1765
[email protected]eaa7dd182010-12-14 11:09:001766void ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:321767 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:141768 const Version* version,
[email protected]8ef78fd2010-08-19 17:14:321769 const FilePath& path,
1770 Extension::Location location) {
[email protected]ab22ba42011-01-14 16:36:381771 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1772 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:011773 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]683d0702010-12-06 16:25:571774 return;
[email protected]a8af9fdb2010-10-28 21:52:201775
[email protected]8e4560b62011-01-14 10:09:141776 DCHECK(version);
1777
[email protected]7577a5c52009-07-30 06:21:581778 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:491779 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:581780 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:021781 const Extension* existing = GetExtensionById(id, true);
[email protected]7577a5c52009-07-30 06:21:581782 if (existing) {
[email protected]8e4560b62011-01-14 10:09:141783 switch (existing->version()->CompareTo(*version)) {
[email protected]7577a5c52009-07-30 06:21:581784 case -1: // existing version is older, we should upgrade
1785 break;
1786 case 0: // existing version is same, do nothing
1787 return;
1788 case 1: // existing version is newer, uh-oh
1789 LOG(WARNING) << "Found external version of extension " << id
1790 << "that is older than current version. Current version "
1791 << "is: " << existing->VersionString() << ". New version "
1792 << "is: " << version << ". Keeping current version.";
1793 return;
1794 }
1795 }
1796
[email protected]b2907fd2011-03-25 16:43:371797 pending_extension_manager()->AddFromExternalFile(id, location);
[email protected]9c635f22010-12-02 09:36:361798
[email protected]6dfbbf82010-03-12 23:09:161799 scoped_refptr<CrxInstaller> installer(
[email protected]bb461532010-11-26 21:50:231800 new CrxInstaller(this, // frontend
[email protected]6dfbbf82010-03-12 23:09:161801 NULL)); // no client (silent install)
1802 installer->set_install_source(location);
1803 installer->set_expected_id(id);
[email protected]604322d2011-03-22 16:51:561804 installer->set_expected_version(*version),
[email protected]6dfbbf82010-03-12 23:09:161805 installer->InstallCrx(path);
[email protected]7577a5c52009-07-30 06:21:581806}
1807
[email protected]eaa7dd182010-12-14 11:09:001808void ExtensionService::ReportExtensionLoadError(
[email protected]d11c8e92009-10-20 23:26:401809 const FilePath& extension_path,
1810 const std::string &error,
1811 NotificationType type,
1812 bool be_noisy) {
1813 NotificationService* service = NotificationService::current();
1814 service->Notify(type,
[email protected]24e7a9d2009-11-04 11:11:341815 Source<Profile>(profile_),
[email protected]d11c8e92009-10-20 23:26:401816 Details<const std::string>(&error));
1817
[email protected]8a205c02011-02-04 20:41:331818 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]18d4b6c2010-09-21 03:21:041819 std::string message = base::StringPrintf(
1820 "Could not load extension from '%s'. %s",
1821 path_str.c_str(), error.c_str());
[email protected]d11c8e92009-10-20 23:26:401822 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
1823}
1824
[email protected]eaa7dd182010-12-14 11:09:001825void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]406027c02010-09-27 08:03:181826 ExtensionHost* host) {
1827 OrphanedDevTools::iterator iter =
1828 orphaned_dev_tools_.find(host->extension()->id());
1829 if (iter == orphaned_dev_tools_.end())
1830 return;
1831
1832 DevToolsManager::GetInstance()->AttachClientHost(
1833 iter->second, host->render_view_host());
1834 orphaned_dev_tools_.erase(iter);
1835}
1836
[email protected]eaa7dd182010-12-14 11:09:001837void ExtensionService::Observe(NotificationType type,
[email protected]4814b512009-11-07 00:12:291838 const NotificationSource& source,
1839 const NotificationDetails& details) {
1840 switch (type.value) {
[email protected]a4ed6282009-12-14 20:51:161841 case NotificationType::EXTENSION_PROCESS_TERMINATED: {
[email protected]bc535ee52010-08-31 18:40:321842 if (profile_ != Source<Profile>(source).ptr()->GetOriginalProfile())
1843 break;
[email protected]a4ed6282009-12-14 20:51:161844
[email protected]f128af42010-08-05 18:05:261845 ExtensionHost* host = Details<ExtensionHost>(details).ptr();
[email protected]bb7f40952011-01-13 00:21:201846 TrackTerminatedExtension(host->extension());
[email protected]f128af42010-08-05 18:05:261847
[email protected]31f77262009-12-02 20:48:531848 // Unload the entire extension. We want it to be in a consistent state:
1849 // either fully working or not loaded at all, but never half-crashed.
[email protected]bc535ee52010-08-31 18:40:321850 // We do it in a PostTask so that other handlers of this notification will
1851 // still have access to the Extension and ExtensionHost.
1852 MessageLoop::current()->PostTask(FROM_HERE,
[email protected]a9f39a312010-12-23 22:14:271853 NewRunnableMethod(this,
1854 &ExtensionService::UnloadExtension,
1855 host->extension()->id(),
1856 UnloadedExtensionInfo::DISABLE));
[email protected]31f77262009-12-02 20:48:531857 break;
1858 }
1859
[email protected]aa96d3a2010-08-21 08:45:251860 case NotificationType::PREF_CHANGED: {
1861 std::string* pref_name = Details<std::string>(details).ptr();
[email protected]6c751e72010-11-23 10:11:101862 if (*pref_name == prefs::kExtensionInstallAllowList ||
1863 *pref_name == prefs::kExtensionInstallDenyList) {
1864 CheckAdminBlacklist();
[email protected]6c751e72010-11-23 10:11:101865 } else {
1866 NOTREACHED() << "Unexpected preference name.";
1867 }
[email protected]aa96d3a2010-08-21 08:45:251868 break;
1869 }
1870
[email protected]4814b512009-11-07 00:12:291871 default:
1872 NOTREACHED() << "Unexpected notification type.";
1873 }
1874}
1875
[email protected]eaa7dd182010-12-14 11:09:001876bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:181877 return !GetAppIds().empty();
1878}
[email protected]377011d2010-07-20 04:18:501879
[email protected]eaa7dd182010-12-14 11:09:001880ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:181881 ExtensionIdSet result;
[email protected]377011d2010-07-20 04:18:501882 for (ExtensionList::const_iterator it = extensions_.begin();
1883 it != extensions_.end(); ++it) {
[email protected]ec5b50d2010-10-09 16:35:181884 if ((*it)->is_app() && (*it)->location() != Extension::COMPONENT)
1885 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:501886 }
1887
[email protected]ec5b50d2010-10-09 16:35:181888 return result;
[email protected]377011d2010-07-20 04:18:501889}
[email protected]d7e9a862010-11-03 21:57:491890
[email protected]eaa7dd182010-12-14 11:09:001891bool ExtensionService::IsBackgroundPageReady(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:491892 return (extension->background_url().is_empty() ||
1893 extension_runtime_data_[extension->id()].background_page_ready);
1894}
1895
[email protected]eaa7dd182010-12-14 11:09:001896void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:491897 DCHECK(!extension->background_url().is_empty());
1898 extension_runtime_data_[extension->id()].background_page_ready = true;
1899 NotificationService::current()->Notify(
1900 NotificationType::EXTENSION_BACKGROUND_PAGE_READY,
1901 Source<const Extension>(extension),
1902 NotificationService::NoDetails());
1903}
1904
[email protected]eaa7dd182010-12-14 11:09:001905bool ExtensionService::IsBeingUpgraded(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:491906 return extension_runtime_data_[extension->id()].being_upgraded;
1907}
1908
[email protected]eaa7dd182010-12-14 11:09:001909void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]d7e9a862010-11-03 21:57:491910 bool value) {
1911 extension_runtime_data_[extension->id()].being_upgraded = value;
1912}
[email protected]1bead0712010-11-27 17:41:531913
[email protected]eaa7dd182010-12-14 11:09:001914PropertyBag* ExtensionService::GetPropertyBag(const Extension* extension) {
[email protected]1bead0712010-11-27 17:41:531915 return &extension_runtime_data_[extension->id()].property_bag;
1916}