blob: 0462912d1af739981c5a761f0e6358855b530271 [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]14908b72011-04-20 06:54:3613#include "base/logging.h"
[email protected]835d7c82010-10-14 04:38:3814#include "base/metrics/histogram.h"
[email protected]cebc3dc2011-04-18 17:15:0015#include "base/path_service.h"
[email protected]cd500f72010-06-25 23:44:3216#include "base/stl_util-inl.h"
[email protected]24b538a2010-02-27 01:22:4417#include "base/string16.h"
[email protected]e83326f2010-07-31 17:29:2518#include "base/string_number_conversions.h"
[email protected]6014d672008-12-05 00:38:2519#include "base/string_util.h"
[email protected]18d4b6c2010-09-21 03:21:0420#include "base/stringprintf.h"
[email protected]34b99632011-01-01 01:01:0621#include "base/threading/thread_restrictions.h"
[email protected]cc2c3432009-11-06 17:24:3622#include "base/time.h"
[email protected]ce7f62e32010-08-10 23:43:5923#include "base/utf_string_conversions.h"
[email protected]cc655912009-01-29 23:19:1924#include "base/values.h"
[email protected]aa142702010-03-26 01:26:3325#include "base/version.h"
[email protected]15730c42009-09-03 00:03:2026#include "chrome/browser/browser_process.h"
[email protected]4814b512009-11-07 00:12:2927#include "chrome/browser/debugger/devtools_manager.h"
[email protected]7577a5c52009-07-30 06:21:5828#include "chrome/browser/extensions/crx_installer.h"
[email protected]18cc5a52011-04-15 16:03:3829#include "chrome/browser/extensions/apps_promo.h"
[email protected]5cbe1e22010-01-30 01:18:5630#include "chrome/browser/extensions/extension_accessibility_api.h"
[email protected]840b0db2009-11-20 03:00:3831#include "chrome/browser/extensions/extension_bookmarks_module.h"
[email protected]b68d5ed2009-04-16 02:41:2832#include "chrome/browser/extensions/extension_browser_event_router.h"
[email protected]2c5e1e12010-06-10 13:14:4433#include "chrome/browser/extensions/extension_cookies_api.h"
[email protected]c10da4b02010-03-25 14:38:3234#include "chrome/browser/extensions/extension_data_deleter.h"
[email protected]14a000d2010-04-29 21:44:2435#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]de768a832009-10-30 05:25:0136#include "chrome/browser/extensions/extension_history_api.h"
[email protected]b1748b1d82009-11-30 20:32:5637#include "chrome/browser/extensions/extension_host.h"
[email protected]7596ce72010-08-30 05:10:4638#include "chrome/browser/extensions/extension_management_api.h"
[email protected]118de7892011-03-16 13:31:4039#include "chrome/browser/extensions/extension_preference_api.h"
[email protected]4814b512009-11-07 00:12:2940#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]8a661f82010-10-19 21:47:1141#include "chrome/browser/extensions/extension_processes_api.h"
[email protected]19eb80152011-02-26 00:28:4342#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]90310d92011-04-17 07:35:0443#include "chrome/browser/extensions/extension_sync_data.h"
[email protected]93fd78f42009-07-10 16:43:1744#include "chrome/browser/extensions/extension_updater.h"
[email protected]8f9d4eb2011-02-05 01:39:1045#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]784688a62010-09-13 07:06:5246#include "chrome/browser/extensions/extension_webnavigation_api.h"
[email protected]8e4560b62011-01-14 10:09:1447#include "chrome/browser/extensions/external_extension_provider_impl.h"
[email protected]42b795652011-02-22 23:32:3848#include "chrome/browser/extensions/external_extension_provider_interface.h"
[email protected]b2907fd2011-03-25 16:43:3749#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]56ad3792010-05-28 17:45:3350#include "chrome/browser/net/chrome_url_request_context.h"
[email protected]37858e52010-08-26 00:22:0251#include "chrome/browser/prefs/pref_service.h"
[email protected]8ecad5e2010-12-02 21:18:3352#include "chrome/browser/profiles/profile.h"
[email protected]56ad3792010-05-28 17:45:3353#include "chrome/browser/search_engines/template_url_model.h"
[email protected]18280372011-03-22 18:05:2254#include "chrome/browser/themes/theme_service.h"
[email protected]a0ea76c2011-03-23 17:36:4255#include "chrome/browser/themes/theme_service_factory.h"
[email protected]fd42ac30f2011-02-27 19:33:3656#include "chrome/browser/ui/webui/shown_sections_handler.h"
[email protected]aab98a52009-12-02 03:22:3557#include "chrome/common/child_process_logging.h"
[email protected]cebc3dc2011-04-18 17:15:0058#include "chrome/common/chrome_paths.h"
[email protected]e2eb43112009-05-29 21:19:5459#include "chrome/common/chrome_switches.h"
[email protected]5b1a0e22009-05-26 19:00:5860#include "chrome/common/extensions/extension.h"
[email protected]d7b36dc2009-10-29 21:47:4061#include "chrome/common/extensions/extension_constants.h"
[email protected]05c82182010-06-24 17:49:0862#include "chrome/common/extensions/extension_error_utils.h"
[email protected]7c927b62010-02-24 09:54:1363#include "chrome/common/extensions/extension_file_util.h"
[email protected]c6d474f82009-12-16 21:11:0664#include "chrome/common/extensions/extension_l10n_util.h"
[email protected]77a6970c2011-04-23 16:58:5665#include "chrome/common/extensions/extension_messages.h"
[email protected]a315ba92010-11-16 14:12:2166#include "chrome/common/extensions/extension_resource.h"
[email protected]25b34332009-06-05 21:53:1967#include "chrome/common/pref_names.h"
[email protected]a57209872009-05-04 22:53:1468#include "chrome/common/url_constants.h"
[email protected]5f945a0e2011-03-01 17:47:5369#include "content/browser/browser_thread.h"
[email protected]2de307592011-04-05 21:16:5870#include "content/browser/plugin_process_host.h"
71#include "content/browser/plugin_service.h"
[email protected]77a6970c2011-04-23 16:58:5672#include "content/browser/renderer_host/render_process_host.h"
[email protected]c3113022011-04-16 03:26:3073#include "content/common/json_value_serializer.h"
[email protected]b3841c502011-03-09 01:21:3174#include "content/common/notification_service.h"
75#include "content/common/notification_type.h"
[email protected]cebc3dc2011-04-18 17:15:0076#include "content/common/pepper_plugin_registry.h"
[email protected]c10da4b02010-03-25 14:38:3277#include "googleurl/src/gurl.h"
[email protected]1debbbb62010-10-06 17:23:4478#include "net/base/registry_controlled_domain.h"
[email protected]24b538a2010-02-27 01:22:4479#include "webkit/database/database_tracker.h"
80#include "webkit/database/database_util.h"
[email protected]2de307592011-04-05 21:16:5881#include "webkit/plugins/npapi/plugin_list.h"
[email protected]c64631652009-04-29 22:24:3182
[email protected]eed367e2011-04-12 03:43:3183#if defined(OS_CHROMEOS)
84#include "chrome/browser/chromeos/extensions/file_browser_event_router.h"
[email protected]b777b332011-04-16 04:01:0885#include "webkit/fileapi/file_system_context.h"
86#include "webkit/fileapi/file_system_mount_point_provider.h"
87#include "webkit/fileapi/file_system_path_manager.h"
[email protected]eed367e2011-04-12 03:43:3188#endif
89
[email protected]5ef47ec2010-01-28 05:58:0590using base::Time;
91
[email protected]c6d474f82009-12-16 21:11:0692namespace errors = extension_manifest_errors;
93
[email protected]b6ab96d2009-08-20 18:58:1994namespace {
95
[email protected]29d0d4ac2010-09-08 21:10:3196#if defined(OS_LINUX)
97static const int kOmniboxIconPaddingLeft = 2;
98static const int kOmniboxIconPaddingRight = 2;
99#elif defined(OS_MACOSX)
100static const int kOmniboxIconPaddingLeft = 0;
101static const int kOmniboxIconPaddingRight = 2;
102#else
103static const int kOmniboxIconPaddingLeft = 0;
104static const int kOmniboxIconPaddingRight = 0;
105#endif
106
[email protected]a315ba92010-11-16 14:12:21107// The following enumeration is used in histograms matching
108// Extensions.ManifestReload* . Values may be added, as long
109// as existing values are not changed.
110enum ManifestReloadReason {
111 NOT_NEEDED = 0, // Reload not needed.
112 UNPACKED_DIR, // Unpacked directory
113 NEEDS_RELOCALIZATION, // The local has changed since we read this extension.
114 NUM_MANIFEST_RELOAD_REASONS
115};
[email protected]2111b1a2010-03-12 18:12:44116
[email protected]a315ba92010-11-16 14:12:21117ManifestReloadReason ShouldReloadExtensionManifest(const ExtensionInfo& info) {
118 // Always reload manifests of unpacked extensions, because they can change
119 // on disk independent of the manifest in our prefs.
120 if (info.extension_location == Extension::LOAD)
121 return UNPACKED_DIR;
122
123 // Reload the manifest if it needs to be relocalized.
124 if (extension_l10n_util::ShouldRelocalizeManifest(info))
125 return NEEDS_RELOCALIZATION;
126
127 return NOT_NEEDED;
[email protected]2111b1a2010-03-12 18:12:44128}
129
[email protected]2de307592011-04-05 21:16:58130static void ForceShutdownPlugin(const FilePath& plugin_path) {
131 PluginProcessHost* plugin =
132 PluginService::GetInstance()->FindNpapiPluginProcess(plugin_path);
133 if (plugin)
134 plugin->ForceShutdown();
135}
136
[email protected]c6d474f82009-12-16 21:11:06137} // namespace
[email protected]b6ab96d2009-08-20 18:58:19138
[email protected]eaa7dd182010-12-14 11:09:00139ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49140 : background_page_ready(false),
141 being_upgraded(false) {
142}
143
[email protected]eaa7dd182010-12-14 11:09:00144ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49145}
146
[email protected]cebc3dc2011-04-18 17:15:00147ExtensionService::NaClModuleInfo::NaClModuleInfo() {
148}
149
150ExtensionService::NaClModuleInfo::~NaClModuleInfo() {
151}
152
[email protected]eaa7dd182010-12-14 11:09:00153// ExtensionService.
[email protected]6014d672008-12-05 00:38:25154
[email protected]eaa7dd182010-12-14 11:09:00155const char* ExtensionService::kInstallDirectoryName = "Extensions";
156const char* ExtensionService::kCurrentVersionFileName = "Current Version";
[email protected]494c06e2009-07-25 01:06:42157
[email protected]eaa7dd182010-12-14 11:09:00158// Implements IO for the ExtensionService.
[email protected]7a4c6852010-09-16 03:44:22159
[email protected]eaa7dd182010-12-14 11:09:00160class ExtensionServiceBackend
[email protected]8e4560b62011-01-14 10:09:14161 : public base::RefCountedThreadSafe<ExtensionServiceBackend> {
[email protected]7a4c6852010-09-16 03:44:22162 public:
[email protected]1f830eb2010-09-28 08:25:14163 // |install_directory| is a path where to look for extensions to load.
[email protected]14908b72011-04-20 06:54:36164 ExtensionServiceBackend(
165 base::WeakPtr<ExtensionService> frontend,
166 const FilePath& install_directory);
[email protected]7a4c6852010-09-16 03:44:22167
168 // Loads a single extension from |path| where |path| is the top directory of
169 // a specific extension where its manifest file lives.
170 // Errors are reported through ExtensionErrorReporter. On success,
[email protected]49cd1572011-02-08 21:38:45171 // AddExtension() is called.
[email protected]7a4c6852010-09-16 03:44:22172 // TODO(erikkay): It might be useful to be able to load a packed extension
173 // (presumably into memory) without installing it.
[email protected]14908b72011-04-20 06:54:36174 void LoadSingleExtension(const FilePath &path);
[email protected]7a4c6852010-09-16 03:44:22175
[email protected]7a4c6852010-09-16 03:44:22176 private:
[email protected]eaa7dd182010-12-14 11:09:00177 friend class base::RefCountedThreadSafe<ExtensionServiceBackend>;
[email protected]7a4c6852010-09-16 03:44:22178
[email protected]eaa7dd182010-12-14 11:09:00179 virtual ~ExtensionServiceBackend();
[email protected]7a4c6852010-09-16 03:44:22180
[email protected]7a4c6852010-09-16 03:44:22181 // Notify the frontend that there was an error loading an extension.
182 void ReportExtensionLoadError(const FilePath& extension_path,
183 const std::string& error);
184
[email protected]14908b72011-04-20 06:54:36185 // Notify the frontend that an extension was installed.
186 void OnExtensionInstalled(const scoped_refptr<const Extension>& extension);
187
188 base::WeakPtr<ExtensionService> frontend_;
[email protected]7a4c6852010-09-16 03:44:22189
190 // The top-level extensions directory being installed to.
191 FilePath install_directory_;
192
[email protected]eaa7dd182010-12-14 11:09:00193 DISALLOW_COPY_AND_ASSIGN(ExtensionServiceBackend);
[email protected]7a4c6852010-09-16 03:44:22194};
195
[email protected]eaa7dd182010-12-14 11:09:00196ExtensionServiceBackend::ExtensionServiceBackend(
[email protected]14908b72011-04-20 06:54:36197 base::WeakPtr<ExtensionService> frontend,
[email protected]aebe23a32010-12-10 22:15:48198 const FilePath& install_directory)
[email protected]14908b72011-04-20 06:54:36199 : frontend_(frontend),
200 install_directory_(install_directory) {
201 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]7a4c6852010-09-16 03:44:22202}
203
[email protected]eaa7dd182010-12-14 11:09:00204ExtensionServiceBackend::~ExtensionServiceBackend() {
[email protected]14908b72011-04-20 06:54:36205 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
206 BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]7a4c6852010-09-16 03:44:22207}
208
[email protected]14908b72011-04-20 06:54:36209void ExtensionServiceBackend::LoadSingleExtension(const FilePath& path_in) {
[email protected]a29a517a2011-01-21 21:11:12210 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]a8af9fdb2010-10-28 21:52:20211
[email protected]7a4c6852010-09-16 03:44:22212 FilePath extension_path = path_in;
213 file_util::AbsolutePath(&extension_path);
214
[email protected]3aff9ad2011-04-01 20:26:48215 int flags = Extension::ShouldAlwaysAllowFileAccess(Extension::LOAD) ?
216 Extension::ALLOW_FILE_ACCESS : Extension::NO_FLAGS;
[email protected]83048a22011-03-29 00:14:13217 if (Extension::ShouldDoStrictErrorChecking(Extension::LOAD))
218 flags |= Extension::STRICT_ERROR_CHECKS;
[email protected]7a4c6852010-09-16 03:44:22219 std::string error;
[email protected]ad8e04a2010-11-01 04:16:27220 scoped_refptr<const Extension> extension(extension_file_util::LoadExtension(
[email protected]7a4c6852010-09-16 03:44:22221 extension_path,
[email protected]92888082010-10-18 19:24:57222 Extension::LOAD,
[email protected]83048a22011-03-29 00:14:13223 flags,
[email protected]ad8e04a2010-11-01 04:16:27224 &error));
[email protected]7a4c6852010-09-16 03:44:22225
226 if (!extension) {
[email protected]14908b72011-04-20 06:54:36227 if (!BrowserThread::PostTask(
228 BrowserThread::UI, FROM_HERE,
229 NewRunnableMethod(
230 this,
231 &ExtensionServiceBackend::ReportExtensionLoadError,
232 extension_path, error)))
233 NOTREACHED() << error;
[email protected]7a4c6852010-09-16 03:44:22234 return;
235 }
236
[email protected]7a4c6852010-09-16 03:44:22237 // Report this as an installed extension so that it gets remembered in the
238 // prefs.
[email protected]14908b72011-04-20 06:54:36239 if (!BrowserThread::PostTask(
240 BrowserThread::UI, FROM_HERE,
241 NewRunnableMethod(
242 this,
243 &ExtensionServiceBackend::OnExtensionInstalled,
244 extension)))
245 NOTREACHED();
[email protected]7a4c6852010-09-16 03:44:22246}
247
[email protected]eaa7dd182010-12-14 11:09:00248void ExtensionServiceBackend::ReportExtensionLoadError(
[email protected]7a4c6852010-09-16 03:44:22249 const FilePath& extension_path, const std::string &error) {
[email protected]14908b72011-04-20 06:54:36250 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
251 if (frontend_.get())
252 frontend_->ReportExtensionLoadError(
253 extension_path, error, NotificationType::EXTENSION_INSTALL_ERROR,
254 true /* alert_on_error */);
255}
256
257void ExtensionServiceBackend::OnExtensionInstalled(
258 const scoped_refptr<const Extension>& extension) {
259 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
260 if (frontend_.get())
261 frontend_->OnExtensionInstalled(extension);
[email protected]7a4c6852010-09-16 03:44:22262}
263
[email protected]8e4560b62011-01-14 10:09:14264void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12265 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20266
[email protected]7a4c6852010-09-16 03:44:22267 // Check if the providers know about this extension.
[email protected]0a60a2e2010-10-25 16:15:21268 ProviderCollection::const_iterator i;
269 for (i = external_extension_providers_.begin();
270 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14271 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21272 if (i->get()->HasExtension(id))
273 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22274 }
275
[email protected]7a4c6852010-09-16 03:44:22276 // This is an external extension that we don't have registered. Uninstall.
[email protected]d6ebc9792011-04-07 18:18:33277 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22278}
279
[email protected]8e4560b62011-01-14 10:09:14280void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22281 external_extension_providers_.clear();
282}
283
[email protected]8e4560b62011-01-14 10:09:14284void ExtensionService::AddProviderForTesting(
285 ExternalExtensionProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12286 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21287 external_extension_providers_.push_back(
[email protected]8e4560b62011-01-14 10:09:14288 linked_ptr<ExternalExtensionProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22289}
290
[email protected]8e4560b62011-01-14 10:09:14291void ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22292 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42293 const GURL& update_url,
294 Extension::Location location) {
[email protected]ab22ba42011-01-14 16:36:38295 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
296 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20297
[email protected]8e4560b62011-01-14 10:09:14298 if (GetExtensionById(id, true)) {
[email protected]7a4c6852010-09-16 03:44:22299 // Already installed. Do not change the update URL that the extension set.
300 return;
301 }
[email protected]b2907fd2011-03-25 16:43:37302 pending_extension_manager()->AddFromExternalUpdateUrl(
303 id, update_url, location);
[email protected]a29a517a2011-01-21 21:11:12304 external_extension_url_added_ |= true;
[email protected]7a4c6852010-09-16 03:44:22305}
306
[email protected]eaa7dd182010-12-14 11:09:00307bool ExtensionService::IsDownloadFromGallery(const GURL& download_url,
[email protected]a29a517a2011-01-21 21:11:12308 const GURL& referrer_url) {
[email protected]d3071992010-10-08 15:24:07309 // Special-case the themes mini-gallery.
310 // TODO(erikkay) When that gallery goes away, remove this code.
311 if (IsDownloadFromMiniGallery(download_url) &&
312 StartsWithASCII(referrer_url.spec(),
313 extension_urls::kMiniGalleryBrowsePrefix, false)) {
[email protected]334e04a2010-06-24 23:34:44314 return true;
[email protected]1debbbb62010-10-06 17:23:44315 }
[email protected]473ff6e2010-05-12 15:31:55316
[email protected]9adb9692010-10-29 23:14:02317 const Extension* download_extension = GetExtensionByWebExtent(download_url);
318 const Extension* referrer_extension = GetExtensionByWebExtent(referrer_url);
319 const Extension* webstore_app = GetWebStoreApp();
[email protected]d3071992010-10-08 15:24:07320
321 bool referrer_valid = (referrer_extension == webstore_app);
322 bool download_valid = (download_extension == webstore_app);
323
324 // If the command-line gallery URL is set, then be a bit more lenient.
325 GURL store_url =
326 GURL(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
327 switches::kAppsGalleryURL));
328 if (!store_url.is_empty()) {
329 std::string store_tld =
330 net::RegistryControlledDomainService::GetDomainAndRegistry(store_url);
331 if (!referrer_valid) {
332 std::string referrer_tld =
333 net::RegistryControlledDomainService::GetDomainAndRegistry(
334 referrer_url);
335 // The referrer gets stripped when transitioning from https to http,
336 // or when hitting an unknown test cert and that commonly happens in
337 // testing environments. Given this, we allow an empty referrer when
338 // the command-line flag is set.
339 // Otherwise, the TLD must match the TLD of the command-line url.
340 referrer_valid = referrer_url.is_empty() || (referrer_tld == store_tld);
341 }
342
343 if (!download_valid) {
344 std::string download_tld =
345 net::RegistryControlledDomainService::GetDomainAndRegistry(
346 GURL(download_url));
347
348 // Otherwise, the TLD must match the TLD of the command-line url.
349 download_valid = (download_tld == store_tld);
350 }
351 }
352
353 return (referrer_valid && download_valid);
[email protected]b7c2f252009-12-08 00:47:23354}
355
[email protected]eaa7dd182010-12-14 11:09:00356bool ExtensionService::IsDownloadFromMiniGallery(const GURL& download_url) {
[email protected]ac025282009-12-16 19:16:38357 return StartsWithASCII(download_url.spec(),
358 extension_urls::kMiniGalleryDownloadPrefix,
359 false); // case_sensitive
360}
361
[email protected]d9696672011-03-15 22:45:09362const Extension* ExtensionService::GetInstalledApp(const GURL& url) {
[email protected]ffb204f22010-12-05 23:20:27363 // Check for hosted app.
[email protected]d9696672011-03-15 22:45:09364 const Extension* app = GetExtensionByWebExtent(url);
365 if (app)
366 return app;
[email protected]ffb204f22010-12-05 23:20:27367
368 // Check for packaged app.
[email protected]d9696672011-03-15 22:45:09369 app = GetExtensionByURL(url);
370 if (app && app->is_app())
371 return app;
372
373 return NULL;
374}
375
376bool ExtensionService::IsInstalledApp(const GURL& url) {
377 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27378}
379
[email protected]da5683db2011-04-23 17:12:21380void ExtensionService::SetInstalledAppForRenderer(int renderer_child_id,
381 const Extension* app) {
382 installed_app_hosts_[renderer_child_id] = app;
383}
384
385const Extension* ExtensionService::GetInstalledAppForRenderer(
386 int renderer_child_id) {
387 InstalledAppMap::iterator i = installed_app_hosts_.find(renderer_child_id);
388 if (i == installed_app_hosts_.end())
389 return NULL;
390 return i->second;
391}
392
[email protected]6aeac8342010-10-01 20:21:18393// static
[email protected]d6ebc9792011-04-07 18:18:33394// This function is used to implement the command-line switch
395// --uninstall-extension. The LOG statements within this function are used to
396// inform the user if the uninstall cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00397bool ExtensionService::UninstallExtensionHelper(
398 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18399 const std::string& extension_id) {
[email protected]6aeac8342010-10-01 20:21:18400
[email protected]95da88c42011-03-31 10:07:33401 const Extension* extension =
402 extensions_service->GetExtensionById(extension_id, true);
403 if (!extension)
404 extension = extensions_service->GetTerminatedExtension(extension_id);
405
[email protected]d6ebc9792011-04-07 18:18:33406 // We can't call UninstallExtension with an invalid extension ID.
[email protected]95da88c42011-03-31 10:07:33407 if (!extension) {
[email protected]6aeac8342010-10-01 20:21:18408 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33409 << "id: " << extension_id;
410 return false;
[email protected]6aeac8342010-10-01 20:21:18411 }
412
[email protected]d6ebc9792011-04-07 18:18:33413 // The following call to UninstallExtension will not allow an uninstall of a
414 // policy-controlled extension.
415 std::string error;
416 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
417 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
418 << ": " << error;
419 return false;
420 }
[email protected]95da88c42011-03-31 10:07:33421
[email protected]6aeac8342010-10-01 20:21:18422 return true;
423}
424
[email protected]eaa7dd182010-12-14 11:09:00425ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24426 const CommandLine* command_line,
427 const FilePath& install_directory,
428 ExtensionPrefs* extension_prefs,
[email protected]0436b102011-04-15 18:30:03429 bool autoupdate_enabled,
430 bool extensions_enabled)
[email protected]14908b72011-04-20 06:54:36431 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
432 method_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
433 profile_(profile),
[email protected]73c47932010-12-06 18:13:43434 extension_prefs_(extension_prefs),
[email protected]14908b72011-04-20 06:54:36435 pending_extension_manager_(*ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]a9b00ac2009-06-25 21:03:23436 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03437 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13438 show_extensions_prompts_(true),
[email protected]3ecda252010-11-18 19:50:55439 ready_(false),
[email protected]14908b72011-04-20 06:54:36440 toolbar_model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]18cc5a52011-04-15 16:03:38441 apps_promo_(profile->GetPrefs()),
[email protected]b3d62312b12010-10-14 21:10:18442 event_routers_initialized_(false) {
[email protected]a29a517a2011-01-21 21:11:12443 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20444
[email protected]36a784c2009-06-23 06:21:08445 // Figure out if extension installation should be enabled.
[email protected]6d60703b2009-08-29 01:29:23446 if (command_line->HasSwitch(switches::kDisableExtensions)) {
447 extensions_enabled_ = false;
448 } else if (profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
449 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18450 }
[email protected]36a784c2009-06-23 06:21:08451
[email protected]a4ed6282009-12-14 20:51:16452 registrar_.Add(this, NotificationType::EXTENSION_PROCESS_TERMINATED,
[email protected]bc535ee52010-08-31 18:40:32453 NotificationService::AllSources());
[email protected]77a6970c2011-04-23 16:58:56454 registrar_.Add(this, NotificationType::RENDERER_PROCESS_CREATED,
455 NotificationService::AllSources());
[email protected]da5683db2011-04-23 17:12:21456 registrar_.Add(this, NotificationType::RENDERER_PROCESS_TERMINATED,
457 NotificationService::AllSources());
[email protected]2fb7dc982010-09-29 12:24:28458 pref_change_registrar_.Init(profile->GetPrefs());
459 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, this);
460 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, this);
[email protected]4814b512009-11-07 00:12:29461
[email protected]93fd78f42009-07-10 16:43:17462 // Set up the ExtensionUpdater
463 if (autoupdate_enabled) {
464 int update_frequency = kDefaultUpdateFrequencySeconds;
465 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25466 base::StringToInt(command_line->GetSwitchValueASCII(
467 switches::kExtensionsUpdateFrequency),
468 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17469 }
[email protected]82b6e512011-04-12 20:33:27470 updater_.reset(new ExtensionUpdater(this,
471 extension_prefs,
472 profile->GetPrefs(),
473 profile,
474 update_frequency));
[email protected]93fd78f42009-07-10 16:43:17475 }
476
[email protected]14908b72011-04-20 06:54:36477 backend_ =
478 new ExtensionServiceBackend(weak_ptr_factory_.GetWeakPtr(),
479 install_directory_);
[email protected]8e4560b62011-01-14 10:09:14480
[email protected]0436b102011-04-15 18:30:03481 if (extensions_enabled_) {
[email protected]873531342011-03-09 12:16:05482 ExternalExtensionProviderImpl::CreateExternalProviders(
483 this, profile_, &external_extension_providers_);
484 }
[email protected]b671760b2010-07-15 21:13:47485
[email protected]aa96d3a2010-08-21 08:45:25486 // Use monochrome icons for Omnibox icons.
[email protected]29d0d4ac2010-09-08 21:10:31487 omnibox_popup_icon_manager_.set_monochrome(true);
[email protected]b671760b2010-07-15 21:13:47488 omnibox_icon_manager_.set_monochrome(true);
[email protected]29d0d4ac2010-09-08 21:10:31489 omnibox_icon_manager_.set_padding(gfx::Insets(0, kOmniboxIconPaddingLeft,
490 0, kOmniboxIconPaddingRight));
[email protected]6014d672008-12-05 00:38:25491}
492
[email protected]eaa7dd182010-12-14 11:09:00493const ExtensionList* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45494 return &extensions_;
495}
496
[email protected]eaa7dd182010-12-14 11:09:00497const ExtensionList* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45498 return &disabled_extensions_;
499}
500
[email protected]bb7f40952011-01-13 00:21:20501const ExtensionList* ExtensionService::terminated_extensions() const {
502 return &terminated_extensions_;
503}
504
[email protected]b2907fd2011-03-25 16:43:37505PendingExtensionManager* ExtensionService::pending_extension_manager() {
506 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45507}
508
[email protected]eaa7dd182010-12-14 11:09:00509ExtensionService::~ExtensionService() {
[email protected]14908b72011-04-20 06:54:36510 CHECK(!profile_); // Profile should have told us it's going away.
[email protected]9f1087e2009-06-15 17:29:32511 UnloadAllExtensions();
[email protected]94b4ffc2011-02-04 05:55:17512
[email protected]8e4560b62011-01-14 10:09:14513 ProviderCollection::const_iterator i;
514 for (i = external_extension_providers_.begin();
515 i != external_extension_providers_.end(); ++i) {
516 ExternalExtensionProviderInterface* provider = i->get();
517 provider->ServiceShutdown();
518 }
[email protected]eed367e2011-04-12 03:43:31519
520#if defined(OS_CHROMEOS)
521 if (event_routers_initialized_) {
522 ExtensionFileBrowserEventRouter::GetInstance()->
523 StopObservingFileSystemEvents();
524 }
525#endif
[email protected]6014d672008-12-05 00:38:25526}
527
[email protected]eaa7dd182010-12-14 11:09:00528void ExtensionService::InitEventRouters() {
[email protected]b3d62312b12010-10-14 21:10:18529 if (event_routers_initialized_)
530 return;
531
[email protected]c5ae74ab2010-04-15 18:14:37532 ExtensionHistoryEventRouter::GetInstance()->ObserveProfile(profile_);
533 ExtensionAccessibilityEventRouter::GetInstance()->ObserveProfile(profile_);
[email protected]97d2f1d2011-01-15 00:41:08534 browser_event_router_.reset(new ExtensionBrowserEventRouter(profile_));
535 browser_event_router_->Init();
[email protected]118de7892011-03-16 13:31:40536 preference_event_router_.reset(new ExtensionPreferenceEventRouter(profile_));
[email protected]8e8bb6d2010-12-13 08:18:55537 ExtensionBookmarkEventRouter::GetInstance()->Observe(
[email protected]c5ae74ab2010-04-15 18:14:37538 profile_->GetBookmarkModel());
[email protected]2c5e1e12010-06-10 13:14:44539 ExtensionCookiesEventRouter::GetInstance()->Init();
[email protected]7596ce72010-08-30 05:10:46540 ExtensionManagementEventRouter::GetInstance()->Init();
[email protected]8a661f82010-10-19 21:47:11541 ExtensionProcessesEventRouter::GetInstance()->ObserveProfile(profile_);
[email protected]784688a62010-09-13 07:06:52542 ExtensionWebNavigationEventRouter::GetInstance()->Init();
[email protected]eed367e2011-04-12 03:43:31543#if defined(OS_CHROMEOS)
544 ExtensionFileBrowserEventRouter::GetInstance()->ObserveFileSystemEvents(
545 profile_);
546#endif
[email protected]b3d62312b12010-10-14 21:10:18547 event_routers_initialized_ = true;
[email protected]c5ae74ab2010-04-15 18:14:37548}
549
[email protected]b2907fd2011-03-25 16:43:37550const Extension* ExtensionService::GetExtensionById(
551 const std::string& id, bool include_disabled) const {
[email protected]78994ab02010-12-08 18:06:44552 return GetExtensionByIdInternal(id, true, include_disabled);
553}
554
[email protected]eaa7dd182010-12-14 11:09:00555void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12556 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20557
[email protected]fa6a9102010-11-22 15:38:50558 DCHECK(!ready_); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17559 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32560
[email protected]95dd38f2009-10-20 20:09:15561 // Hack: we need to ensure the ResourceDispatcherHost is ready before we load
562 // the first extension, because its members listen for loaded notifications.
563 g_browser_process->resource_dispatcher_host();
564
[email protected]9f1087e2009-06-15 17:29:32565 LoadAllExtensions();
[email protected]894bb502009-05-21 22:39:57566
[email protected]9f1087e2009-06-15 17:29:32567 // TODO(erikkay) this should probably be deferred to a future point
568 // rather than running immediately at startup.
[email protected]93fd78f42009-07-10 16:43:17569 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57570
[email protected]9f1087e2009-06-15 17:29:32571 // TODO(erikkay) this should probably be deferred as well.
572 GarbageCollectExtensions();
[email protected]6014d672008-12-05 00:38:25573}
574
[email protected]eaa7dd182010-12-14 11:09:00575void ExtensionService::UpdateExtension(const std::string& id,
[email protected]9a8c4022011-01-25 14:25:33576 const FilePath& extension_path,
577 const GURL& download_url) {
[email protected]a29a517a2011-01-21 21:11:12578 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20579
[email protected]b2907fd2011-03-25 16:43:37580 PendingExtensionInfo pending_extension_info;
581 bool is_pending_extension = pending_extension_manager_.GetById(
582 id, &pending_extension_info);
[email protected]8ef78fd2010-08-19 17:14:32583
[email protected]5eb375e92010-11-26 07:50:41584 const Extension* extension = GetExtensionByIdInternal(id, true, true);
585 if (!is_pending_extension && !extension) {
[email protected]aa142702010-03-26 01:26:33586 LOG(WARNING) << "Will not update extension " << id
587 << " because it is not installed or pending";
588 // Delete extension_path since we're not creating a CrxInstaller
589 // that would do it for us.
[email protected]14908b72011-04-20 06:54:36590 if (!BrowserThread::PostTask(
591 BrowserThread::FILE, FROM_HERE,
592 NewRunnableFunction(
593 extension_file_util::DeleteFile, extension_path, false)))
594 NOTREACHED();
[email protected]4c967932009-07-31 01:15:49595 return;
[email protected]e957fe52009-06-23 16:51:05596 }
597
[email protected]aa142702010-03-26 01:26:33598 // We want a silent install only for non-pending extensions and
599 // pending extensions that have install_silently set.
600 ExtensionInstallUI* client =
[email protected]b2907fd2011-03-25 16:43:37601 (!is_pending_extension || pending_extension_info.install_silently()) ?
[email protected]aa142702010-03-26 01:26:33602 NULL : new ExtensionInstallUI(profile_);
603
[email protected]14908b72011-04-20 06:54:36604 scoped_refptr<CrxInstaller> installer(MakeCrxInstaller(client));
[email protected]6dfbbf82010-03-12 23:09:16605 installer->set_expected_id(id);
[email protected]ec5b50d2010-10-09 16:35:18606 if (is_pending_extension)
[email protected]b2907fd2011-03-25 16:43:37607 installer->set_install_source(pending_extension_info.install_source());
[email protected]5eb375e92010-11-26 07:50:41608 else if (extension)
609 installer->set_install_source(extension->location());
[email protected]6dfbbf82010-03-12 23:09:16610 installer->set_delete_source(true);
[email protected]5c8516202010-03-18 21:43:34611 installer->set_original_url(download_url);
[email protected]6dfbbf82010-03-12 23:09:16612 installer->InstallCrx(extension_path);
[email protected]e957fe52009-06-23 16:51:05613}
614
[email protected]eaa7dd182010-12-14 11:09:00615void ExtensionService::ReloadExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12616 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b65272f2009-08-31 15:47:06617 FilePath path;
[email protected]9adb9692010-10-29 23:14:02618 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40619
[email protected]f17dbd42010-08-16 23:21:10620 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06621 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29622 // If the extension has an inspector open for its background page, detach
623 // the inspector and hang onto a cookie for it, so that we can reattach
624 // later.
625 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
626 ExtensionHost* host = manager->GetBackgroundHostForExtension(
627 current_extension);
628 if (host) {
629 // Look for an open inspector for the background page.
630 int devtools_cookie = DevToolsManager::GetInstance()->DetachClientHost(
631 host->render_view_host());
632 if (devtools_cookie >= 0)
633 orphaned_dev_tools_[extension_id] = devtools_cookie;
634 }
635
[email protected]b65272f2009-08-31 15:47:06636 path = current_extension->path();
[email protected]f17dbd42010-08-16 23:21:10637 DisableExtension(extension_id);
638 disabled_extension_paths_[extension_id] = path;
[email protected]1eb175082010-02-10 09:26:16639 } else {
640 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06641 }
642
[email protected]e6090e42010-03-23 22:44:08643 // Check the installed extensions to see if what we're reloading was already
644 // installed.
645 scoped_ptr<ExtensionInfo> installed_extension(
646 extension_prefs_->GetInstalledExtensionInfo(extension_id));
647 if (installed_extension.get() &&
648 installed_extension->extension_manifest.get()) {
649 LoadInstalledExtension(*installed_extension, false);
650 } else {
651 // We should always be able to remember the extension's path. If it's not in
652 // the map, someone failed to update |unloaded_extension_paths_|.
653 CHECK(!path.empty());
654 LoadExtension(path);
655 }
[email protected]9cddd4702009-07-27 22:09:40656}
657
[email protected]d6ebc9792011-04-07 18:18:33658bool ExtensionService::UninstallExtension(const std::string& extension_id,
659 bool external_uninstall,
660 std::string* error) {
[email protected]a29a517a2011-01-21 21:11:12661 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20662
[email protected]9adb9692010-10-29 23:14:02663 const Extension* extension =
664 GetExtensionByIdInternal(extension_id, true, true);
[email protected]0dfe05c2011-02-23 23:03:36665 if (!extension)
666 extension = GetTerminatedExtension(extension_id);
[email protected]631cf822009-05-15 07:01:25667
[email protected]e7afe2452010-08-22 16:19:13668 // Callers should not send us nonexistent extensions.
[email protected]aebe23a32010-12-10 22:15:48669 CHECK(extension);
[email protected]9f1087e2009-06-15 17:29:32670
[email protected]831aa212010-03-26 13:55:19671 // Get hold of information we need after unloading, since the extension
672 // pointer will be invalid then.
673 GURL extension_url(extension->url());
674 Extension::Location location(extension->location());
[email protected]95da88c42011-03-31 10:07:33675
676 // Policy change which triggers an uninstall will always set
677 // |external_uninstall| to true so this is the only way to uninstall
678 // managed extensions.
[email protected]d6ebc9792011-04-07 18:18:33679 if (!Extension::UserMayDisable(location) && !external_uninstall) {
680 NotificationService::current()->Notify(
681 NotificationType::EXTENSION_UNINSTALL_NOT_ALLOWED,
682 Source<Profile>(profile_),
683 Details<const Extension>(extension));
684 if (error != NULL) {
685 *error = errors::kCannotUninstallManagedExtension;
686 }
687 return false;
688 }
[email protected]95da88c42011-03-31 10:07:33689
[email protected]211030342010-09-30 18:41:06690 UninstalledExtensionInfo uninstalled_extension_info(*extension);
[email protected]831aa212010-03-26 13:55:19691
[email protected]9b217652010-10-08 22:04:23692 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08693 extension->GetType(), 100);
[email protected]fe2dd7742011-04-19 22:52:49694 RecordPermissionMessagesHistogram(
695 extension, "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23696
[email protected]831aa212010-03-26 13:55:19697 // Also copy the extension identifier since the reference might have been
698 // obtained via Extension::id().
699 std::string extension_id_copy(extension_id);
700
[email protected]56ad3792010-05-28 17:45:33701 if (profile_->GetTemplateURLModel())
702 profile_->GetTemplateURLModel()->UnregisterExtensionKeyword(extension);
703
[email protected]831aa212010-03-26 13:55:19704 // Unload before doing more cleanup to ensure that nothing is hanging on to
705 // any of these resources.
[email protected]a9f39a312010-12-23 22:14:27706 UnloadExtension(extension_id, UnloadedExtensionInfo::UNINSTALL);
[email protected]831aa212010-03-26 13:55:19707
708 extension_prefs_->OnExtensionUninstalled(extension_id_copy, location,
709 external_uninstall);
[email protected]9f1087e2009-06-15 17:29:32710
711 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]831aa212010-03-26 13:55:19712 if (Extension::LOAD != location) {
[email protected]14908b72011-04-20 06:54:36713 if (!BrowserThread::PostTask(
714 BrowserThread::FILE, FROM_HERE,
715 NewRunnableFunction(
716 &extension_file_util::UninstallExtension,
717 install_directory_,
718 extension_id_copy)))
719 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32720 }
721
[email protected]c10da4b02010-03-25 14:38:32722 ClearExtensionData(extension_url);
[email protected]0dfe05c2011-02-23 23:03:36723 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06724
725 // Notify interested parties that we've uninstalled this extension.
726 NotificationService::current()->Notify(
727 NotificationType::EXTENSION_UNINSTALLED,
728 Source<Profile>(profile_),
729 Details<UninstalledExtensionInfo>(&uninstalled_extension_info));
[email protected]d6ebc9792011-04-07 18:18:33730
731 return true;
[email protected]c10da4b02010-03-25 14:38:32732}
733
[email protected]eaa7dd182010-12-14 11:09:00734void ExtensionService::ClearExtensionData(const GURL& extension_url) {
[email protected]c10da4b02010-03-25 14:38:32735 scoped_refptr<ExtensionDataDeleter> deleter(
736 new ExtensionDataDeleter(profile_, extension_url));
737 deleter->StartDeleting();
[email protected]9f1087e2009-06-15 17:29:32738}
739
[email protected]c3cfb012011-04-06 22:07:35740bool ExtensionService::IsExtensionEnabled(
741 const std::string& extension_id) const {
742 // TODO(akalin): GetExtensionState() isn't very safe as it returns
743 // Extension::ENABLED by default; either change it to return
744 // something else by default or create a separate function that does
745 // so.
746 return
747 extension_prefs_->GetExtensionState(extension_id) == Extension::ENABLED;
748}
749
750bool ExtensionService::IsExternalExtensionUninstalled(
751 const std::string& extension_id) const {
752 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
753}
754
[email protected]eaa7dd182010-12-14 11:09:00755void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12756 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20757
[email protected]9adb9692010-10-29 23:14:02758 const Extension* extension =
759 GetExtensionByIdInternal(extension_id, false, true);
[email protected]f681c782010-11-19 11:19:39760 if (!extension)
[email protected]0c6da502009-08-14 22:32:39761 return;
[email protected]0c6da502009-08-14 22:32:39762
[email protected]e8c729a2010-03-09 19:55:19763 extension_prefs_->SetExtensionState(extension, Extension::ENABLED);
[email protected]1784e83a2009-09-08 21:01:52764
[email protected]0c6da502009-08-14 22:32:39765 // Move it over to the enabled list.
[email protected]00cd9c42010-11-02 20:15:57766 extensions_.push_back(make_scoped_refptr(extension));
[email protected]0c6da502009-08-14 22:32:39767 ExtensionList::iterator iter = std::find(disabled_extensions_.begin(),
768 disabled_extensions_.end(),
769 extension);
770 disabled_extensions_.erase(iter);
771
[email protected]f681c782010-11-19 11:19:39772 // Make sure any browser action contained within it is not hidden.
773 extension_prefs_->SetBrowserActionVisibility(extension, true);
774
[email protected]8f9d4eb2011-02-05 01:39:10775 ExtensionWebUI::RegisterChromeURLOverrides(profile_,
[email protected]86c008e82009-08-28 20:26:05776 extension->GetChromeURLOverrides());
777
[email protected]62d30f42009-10-01 22:36:06778 NotifyExtensionLoaded(extension);
[email protected]aab98a52009-12-02 03:22:35779 UpdateActiveExtensionsInCrashReporter();
[email protected]0c6da502009-08-14 22:32:39780}
781
[email protected]eaa7dd182010-12-14 11:09:00782void ExtensionService::DisableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12783 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20784
[email protected]9adb9692010-10-29 23:14:02785 const Extension* extension =
786 GetExtensionByIdInternal(extension_id, true, false);
[email protected]b2ba9962009-12-10 20:10:15787 // The extension may have been disabled already.
788 if (!extension)
[email protected]1784e83a2009-09-08 21:01:52789 return;
[email protected]1784e83a2009-09-08 21:01:52790
[email protected]95da88c42011-03-31 10:07:33791 if (!Extension::UserMayDisable(extension->location()))
792 return;
793
[email protected]e8c729a2010-03-09 19:55:19794 extension_prefs_->SetExtensionState(extension, Extension::DISABLED);
[email protected]1784e83a2009-09-08 21:01:52795
796 // Move it over to the disabled list.
[email protected]00cd9c42010-11-02 20:15:57797 disabled_extensions_.push_back(make_scoped_refptr(extension));
[email protected]1784e83a2009-09-08 21:01:52798 ExtensionList::iterator iter = std::find(extensions_.begin(),
799 extensions_.end(),
800 extension);
801 extensions_.erase(iter);
802
[email protected]8f9d4eb2011-02-05 01:39:10803 ExtensionWebUI::UnregisterChromeURLOverrides(profile_,
[email protected]1784e83a2009-09-08 21:01:52804 extension->GetChromeURLOverrides());
805
[email protected]a9f39a312010-12-23 22:14:27806 NotifyExtensionUnloaded(extension, UnloadedExtensionInfo::DISABLE);
[email protected]aab98a52009-12-02 03:22:35807 UpdateActiveExtensionsInCrashReporter();
[email protected]1784e83a2009-09-08 21:01:52808}
809
[email protected]eaa7dd182010-12-14 11:09:00810void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]8d888c12010-11-30 00:00:25811 CHECK(extension);
812
813 // We only maintain the granted permissions prefs for INTERNAL extensions.
[email protected]139e5732011-03-16 19:29:51814 CHECK_EQ(Extension::INTERNAL, extension->location());
[email protected]8d888c12010-11-30 00:00:25815
816 ExtensionExtent effective_hosts = extension->GetEffectiveHostPermissions();
817 extension_prefs_->AddGrantedPermissions(extension->id(),
818 extension->HasFullPermissions(),
819 extension->api_permissions(),
820 effective_hosts);
821}
822
[email protected]eaa7dd182010-12-14 11:09:00823void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]8d888c12010-11-30 00:00:25824 const Extension* extension) {
825 CHECK(extension);
[email protected]fe2dd7742011-04-19 22:52:49826 RecordPermissionMessagesHistogram(
827 extension, "Extensions.Permissions_ReEnable");
[email protected]8d888c12010-11-30 00:00:25828 GrantPermissions(extension);
829 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
830 EnableExtension(extension->id());
831}
832
[email protected]eaa7dd182010-12-14 11:09:00833void ExtensionService::LoadExtension(const FilePath& extension_path) {
[email protected]14908b72011-04-20 06:54:36834 if (!BrowserThread::PostTask(
835 BrowserThread::FILE, FROM_HERE,
836 NewRunnableMethod(
837 backend_.get(),
838 &ExtensionServiceBackend::LoadSingleExtension,
839 extension_path)))
840 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32841}
842
[email protected]eaa7dd182010-12-14 11:09:00843void ExtensionService::LoadComponentExtensions() {
[email protected]1952c7d2010-03-04 23:48:34844 for (RegisteredComponentExtensions::iterator it =
845 component_extension_manifests_.begin();
846 it != component_extension_manifests_.end(); ++it) {
[email protected]61b55b62011-03-24 09:03:10847 LoadComponentExtension(*it);
[email protected]1952c7d2010-03-04 23:48:34848 }
849}
850
[email protected]a964e112011-04-14 21:52:51851const Extension* ExtensionService::LoadComponentExtension(
[email protected]61b55b62011-03-24 09:03:10852 const ComponentExtensionInfo &info) {
853 JSONStringValueSerializer serializer(info.manifest);
854 scoped_ptr<Value> manifest(serializer.Deserialize(NULL, NULL));
855 if (!manifest.get()) {
856 DLOG(ERROR) << "Failed to parse manifest for extension";
[email protected]a964e112011-04-14 21:52:51857 return NULL;
[email protected]61b55b62011-03-24 09:03:10858 }
859
[email protected]83048a22011-03-29 00:14:13860 int flags = Extension::REQUIRE_KEY;
861 if (Extension::ShouldDoStrictErrorChecking(Extension::COMPONENT))
862 flags |= Extension::STRICT_ERROR_CHECKS;
[email protected]61b55b62011-03-24 09:03:10863 std::string error;
864 scoped_refptr<const Extension> extension(Extension::Create(
865 info.root_directory,
866 Extension::COMPONENT,
867 *static_cast<DictionaryValue*>(manifest.get()),
[email protected]83048a22011-03-29 00:14:13868 flags,
[email protected]61b55b62011-03-24 09:03:10869 &error));
870 if (!extension.get()) {
871 NOTREACHED() << error;
[email protected]a964e112011-04-14 21:52:51872 return NULL;
[email protected]61b55b62011-03-24 09:03:10873 }
874 AddExtension(extension);
[email protected]a964e112011-04-14 21:52:51875 return extension;
[email protected]61b55b62011-03-24 09:03:10876}
877
[email protected]eaa7dd182010-12-14 11:09:00878void ExtensionService::LoadAllExtensions() {
[email protected]a29a517a2011-01-21 21:11:12879 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a315ba92010-11-16 14:12:21880
[email protected]cc2c3432009-11-06 17:24:36881 base::TimeTicks start_time = base::TimeTicks::Now();
882
[email protected]1952c7d2010-03-04 23:48:34883 // Load any component extensions.
884 LoadComponentExtensions();
885
[email protected]e72e8eb82009-06-18 17:21:51886 // Load the previously installed extensions.
[email protected]a315ba92010-11-16 14:12:21887 scoped_ptr<ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]e6090e42010-03-23 22:44:08888 extension_prefs_->GetInstalledExtensionsInfo());
[email protected]c6d474f82009-12-16 21:11:06889
[email protected]a315ba92010-11-16 14:12:21890 std::vector<int> reload_reason_counts(NUM_MANIFEST_RELOAD_REASONS, 0);
891 bool should_write_prefs = false;
892
893 for (size_t i = 0; i < extensions_info->size(); ++i) {
894 ExtensionInfo* info = extensions_info->at(i).get();
895
896 ManifestReloadReason reload_reason = ShouldReloadExtensionManifest(*info);
897 ++reload_reason_counts[reload_reason];
898 UMA_HISTOGRAM_ENUMERATION("Extensions.ManifestReloadEnumValue",
899 reload_reason, 100);
900
901 if (reload_reason != NOT_NEEDED) {
902 // Reloading and extension reads files from disk. We do this on the
903 // UI thread because reloads should be very rare, and the complexity
904 // added by delaying the time when the extensions service knows about
905 // all extensions is significant. See crbug.com/37548 for details.
906 // |allow_io| disables tests that file operations run on the file
907 // thread.
908 base::ThreadRestrictions::ScopedAllowIO allow_io;
909
[email protected]83048a22011-03-29 00:14:13910 int flags = Extension::NO_FLAGS;
911 if (Extension::ShouldDoStrictErrorChecking(info->extension_location))
912 flags |= Extension::STRICT_ERROR_CHECKS;
[email protected]3aff9ad2011-04-01 20:26:48913 if (extension_prefs_->AllowFileAccess(info->extension_id))
914 flags |= Extension::ALLOW_FILE_ACCESS;
[email protected]a315ba92010-11-16 14:12:21915 std::string error;
916 scoped_refptr<const Extension> extension(
917 extension_file_util::LoadExtension(
[email protected]542258c2011-03-04 21:25:31918 info->extension_path,
919 info->extension_location,
[email protected]83048a22011-03-29 00:14:13920 flags,
[email protected]542258c2011-03-04 21:25:31921 &error));
[email protected]a315ba92010-11-16 14:12:21922
923 if (extension.get()) {
924 extensions_info->at(i)->extension_manifest.reset(
925 static_cast<DictionaryValue*>(
926 extension->manifest_value()->DeepCopy()));
927 should_write_prefs = true;
928 }
[email protected]c6d474f82009-12-16 21:11:06929 }
930 }
931
[email protected]a315ba92010-11-16 14:12:21932 for (size_t i = 0; i < extensions_info->size(); ++i) {
933 LoadInstalledExtension(*extensions_info->at(i), should_write_prefs);
[email protected]c6d474f82009-12-16 21:11:06934 }
935
[email protected]ae09ca62009-08-21 19:46:46936 OnLoadedInstalledExtensions();
[email protected]cc2c3432009-11-06 17:24:36937
[email protected]a315ba92010-11-16 14:12:21938 // The histograms Extensions.ManifestReload* allow us to validate
939 // the assumption that reloading manifest is a rare event.
940 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadNotNeeded",
941 reload_reason_counts[NOT_NEEDED]);
942 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadUnpackedDir",
943 reload_reason_counts[UNPACKED_DIR]);
944 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadNeedsRelocalization",
945 reload_reason_counts[NEEDS_RELOCALIZATION]);
946
[email protected]cc2c3432009-11-06 17:24:36947 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadAll", extensions_.size());
948 UMA_HISTOGRAM_COUNTS_100("Extensions.Disabled", disabled_extensions_.size());
949
[email protected]1952c7d2010-03-04 23:48:34950 UMA_HISTOGRAM_TIMES("Extensions.LoadAllTime",
951 base::TimeTicks::Now() - start_time);
[email protected]cc2c3432009-11-06 17:24:36952
[email protected]9b217652010-10-08 22:04:23953 int app_count = 0;
954 int hosted_app_count = 0;
955 int packaged_app_count = 0;
[email protected]1952c7d2010-03-04 23:48:34956 int user_script_count = 0;
957 int extension_count = 0;
958 int theme_count = 0;
959 int external_count = 0;
960 int page_action_count = 0;
961 int browser_action_count = 0;
962 ExtensionList::iterator ex;
963 for (ex = extensions_.begin(); ex != extensions_.end(); ++ex) {
[email protected]9b217652010-10-08 22:04:23964 Extension::Location location = (*ex)->location();
[email protected]7fa19f82010-12-21 19:40:08965 Extension::Type type = (*ex)->GetType();
[email protected]9b217652010-10-08 22:04:23966 if ((*ex)->is_app()) {
967 UMA_HISTOGRAM_ENUMERATION("Extensions.AppLocation",
968 location, 100);
969 } else if (type == Extension::TYPE_EXTENSION) {
970 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionLocation",
971 location, 100);
972 }
973
[email protected]1952c7d2010-03-04 23:48:34974 // Don't count component extensions, since they are only extensions as an
975 // implementation detail.
[email protected]9b217652010-10-08 22:04:23976 if (location == Extension::COMPONENT)
[email protected]1952c7d2010-03-04 23:48:34977 continue;
978
[email protected]e8c729a2010-03-09 19:55:19979 // Don't count unpacked extensions, since they're a developer-specific
980 // feature.
[email protected]9b217652010-10-08 22:04:23981 if (location == Extension::LOAD)
[email protected]e8c729a2010-03-09 19:55:19982 continue;
983
[email protected]9b217652010-10-08 22:04:23984 // Using an enumeration shows us the total installed ratio across all users.
985 // Using the totals per user at each startup tells us the distribution of
986 // usage for each user (e.g. 40% of users have at least one app installed).
987 UMA_HISTOGRAM_ENUMERATION("Extensions.LoadType", type, 100);
988 switch (type) {
989 case Extension::TYPE_THEME:
[email protected]a315ba92010-11-16 14:12:21990 ++theme_count;
[email protected]9b217652010-10-08 22:04:23991 break;
992 case Extension::TYPE_USER_SCRIPT:
[email protected]a315ba92010-11-16 14:12:21993 ++user_script_count;
[email protected]9b217652010-10-08 22:04:23994 break;
995 case Extension::TYPE_HOSTED_APP:
[email protected]a315ba92010-11-16 14:12:21996 ++app_count;
997 ++hosted_app_count;
[email protected]9b217652010-10-08 22:04:23998 break;
999 case Extension::TYPE_PACKAGED_APP:
[email protected]a315ba92010-11-16 14:12:211000 ++app_count;
1001 ++packaged_app_count;
[email protected]9b217652010-10-08 22:04:231002 break;
1003 case Extension::TYPE_EXTENSION:
1004 default:
[email protected]a315ba92010-11-16 14:12:211005 ++extension_count;
[email protected]9b217652010-10-08 22:04:231006 break;
[email protected]cc2c3432009-11-06 17:24:361007 }
[email protected]9b217652010-10-08 22:04:231008 if (Extension::IsExternalLocation(location))
[email protected]a315ba92010-11-16 14:12:211009 ++external_count;
[email protected]9b217652010-10-08 22:04:231010 if ((*ex)->page_action() != NULL)
[email protected]a315ba92010-11-16 14:12:211011 ++page_action_count;
[email protected]9b217652010-10-08 22:04:231012 if ((*ex)->browser_action() != NULL)
[email protected]a315ba92010-11-16 14:12:211013 ++browser_action_count;
[email protected]fe2dd7742011-04-19 22:52:491014
1015 RecordPermissionMessagesHistogram(
1016 ex->get(), "Extensions.Permissions_Load");
[email protected]cc2c3432009-11-06 17:24:361017 }
[email protected]9b217652010-10-08 22:04:231018 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadApp", app_count);
1019 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadHostedApp", hosted_app_count);
1020 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadPackagedApp", packaged_app_count);
[email protected]1952c7d2010-03-04 23:48:341021 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExtension", extension_count);
1022 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadUserScript", user_script_count);
1023 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadTheme", theme_count);
1024 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExternal", external_count);
1025 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadPageAction", page_action_count);
1026 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadBrowserAction",
1027 browser_action_count);
[email protected]ae09ca62009-08-21 19:46:461028}
1029
[email protected]fe2dd7742011-04-19 22:52:491030// static
1031void ExtensionService::RecordPermissionMessagesHistogram(
1032 const Extension* e, const char* histogram) {
1033 // Since this is called from multiple sources, and since the Histogram macros
1034 // use statics, we need to manually lookup the Histogram ourselves.
1035 base::Histogram* counter = base::LinearHistogram::FactoryGet(
1036 histogram,
1037 1,
1038 Extension::PermissionMessage::ID_ENUM_BOUNDARY,
1039 Extension::PermissionMessage::ID_ENUM_BOUNDARY + 1,
1040 base::Histogram::kUmaTargetedHistogramFlag);
1041
1042 std::vector<Extension::PermissionMessage> permissions =
1043 e->GetPermissionMessages();
1044 if (permissions.empty()) {
1045 counter->Add(Extension::PermissionMessage::ID_NONE);
1046 } else {
1047 std::vector<Extension::PermissionMessage>::iterator it;
1048 for (it = permissions.begin(); it != permissions.end(); ++it)
1049 counter->Add(it->message_id());
1050 }
1051}
1052
[email protected]eaa7dd182010-12-14 11:09:001053void ExtensionService::LoadInstalledExtension(const ExtensionInfo& info,
[email protected]542258c2011-03-04 21:25:311054 bool write_to_prefs) {
[email protected]ae09ca62009-08-21 19:46:461055 std::string error;
[email protected]9adb9692010-10-29 23:14:021056 scoped_refptr<const Extension> extension(NULL);
[email protected]306a2bd2010-08-11 14:56:361057 if (!extension_prefs_->IsExtensionAllowedByPolicy(info.extension_id)) {
1058 error = errors::kDisabledByPolicy;
1059 } else if (info.extension_manifest.get()) {
[email protected]83048a22011-03-29 00:14:131060 int flags = Extension::NO_FLAGS;
1061 if (info.extension_location != Extension::LOAD)
1062 flags |= Extension::REQUIRE_KEY;
1063 if (Extension::ShouldDoStrictErrorChecking(info.extension_location))
1064 flags |= Extension::STRICT_ERROR_CHECKS;
[email protected]3aff9ad2011-04-01 20:26:481065 if (extension_prefs_->AllowFileAccess(info.extension_id))
1066 flags |= Extension::ALLOW_FILE_ACCESS;
[email protected]66e4eb32010-10-27 20:37:411067 extension = Extension::Create(
[email protected]542258c2011-03-04 21:25:311068 info.extension_path,
1069 info.extension_location,
1070 *info.extension_manifest,
[email protected]83048a22011-03-29 00:14:131071 flags,
[email protected]542258c2011-03-04 21:25:311072 &error);
[email protected]ae09ca62009-08-21 19:46:461073 } else {
[email protected]c6d474f82009-12-16 21:11:061074 error = errors::kManifestUnreadable;
[email protected]ae09ca62009-08-21 19:46:461075 }
1076
1077 if (!extension) {
[email protected]c6d474f82009-12-16 21:11:061078 ReportExtensionLoadError(info.extension_path,
[email protected]d11c8e92009-10-20 23:26:401079 error,
1080 NotificationType::EXTENSION_INSTALL_ERROR,
1081 false);
[email protected]ae09ca62009-08-21 19:46:461082 return;
1083 }
1084
[email protected]c6d474f82009-12-16 21:11:061085 if (write_to_prefs)
1086 extension_prefs_->UpdateManifest(extension);
1087
[email protected]49cd1572011-02-08 21:38:451088 AddExtension(extension);
[email protected]9f1087e2009-06-15 17:29:321089}
1090
[email protected]eaa7dd182010-12-14 11:09:001091void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:421092 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061093 // was loaded, otherwise a race can arise where a renderer that is created
1094 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421095 // that the request context doesn't yet know about. The profile is responsible
1096 // for ensuring its URLRequestContexts appropriately discover the loaded
1097 // extension.
1098 if (profile_) {
1099 profile_->RegisterExtensionWithRequestContexts(extension);
[email protected]19eb80152011-02-26 00:28:431100 profile_->GetExtensionSpecialStoragePolicy()->
1101 GrantRightsForExtension(extension);
[email protected]62d30f42009-10-01 22:36:061102 }
1103
1104 NotificationService::current()->Notify(
1105 NotificationType::EXTENSION_LOADED,
[email protected]24e7a9d2009-11-04 11:11:341106 Source<Profile>(profile_),
[email protected]9adb9692010-10-29 23:14:021107 Details<const Extension>(extension));
[email protected]2de307592011-04-05 21:16:581108
[email protected]77a6970c2011-04-23 16:58:561109 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
1110 !i.IsAtEnd(); i.Advance()) {
1111 i.GetCurrentValue()->Send(
1112 new ExtensionMsg_Loaded(ExtensionMsg_Loaded_Params(extension)));
1113 }
1114
[email protected]2de307592011-04-05 21:16:581115 bool plugins_changed = false;
1116 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1117 const Extension::PluginInfo& plugin = extension->plugins()[i];
1118 webkit::npapi::PluginList::Singleton()->RefreshPlugins();
1119 webkit::npapi::PluginList::Singleton()->AddExtraPluginPath(plugin.path);
1120 plugins_changed = true;
1121 if (!plugin.is_public) {
1122 PluginService::GetInstance()->RestrictPluginToUrl(
1123 plugin.path, extension->url());
1124 }
1125 }
[email protected]84396dbc2011-04-14 06:33:421126
1127 bool nacl_modules_changed = false;
1128 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1129 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001130 RegisterNaClModule(module.url, module.mime_type);
[email protected]84396dbc2011-04-14 06:33:421131 nacl_modules_changed = true;
1132 }
1133
1134 if (nacl_modules_changed)
[email protected]cebc3dc2011-04-18 17:15:001135 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421136
1137 if (plugins_changed || nacl_modules_changed)
[email protected]2de307592011-04-05 21:16:581138 PluginService::GetInstance()->PurgePluginListCache(false);
[email protected]62d30f42009-10-01 22:36:061139}
1140
[email protected]a9f39a312010-12-23 22:14:271141void ExtensionService::NotifyExtensionUnloaded(
1142 const Extension* extension, UnloadedExtensionInfo::Reason reason) {
1143 UnloadedExtensionInfo details(extension, reason);
[email protected]62d30f42009-10-01 22:36:061144 NotificationService::current()->Notify(
1145 NotificationType::EXTENSION_UNLOADED,
[email protected]24e7a9d2009-11-04 11:11:341146 Source<Profile>(profile_),
[email protected]a9f39a312010-12-23 22:14:271147 Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061148
[email protected]77a6970c2011-04-23 16:58:561149 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
1150 !i.IsAtEnd(); i.Advance()) {
1151 i.GetCurrentValue()->Send(new ExtensionMsg_Unloaded(extension->id()));
1152 }
1153
[email protected]57a777f72010-03-31 01:09:421154 if (profile_) {
1155 profile_->UnregisterExtensionWithRequestContexts(extension);
[email protected]19eb80152011-02-26 00:28:431156 profile_->GetExtensionSpecialStoragePolicy()->
1157 RevokeRightsForExtension(extension);
[email protected]b777b332011-04-16 04:01:081158#if defined(OS_CHROMEOS)
1159 // Revoke external file access to
[email protected]45194082011-04-16 10:33:191160 if (profile_->GetFileSystemContext() &&
1161 profile_->GetFileSystemContext()->path_manager() &&
[email protected]bbee71d2011-04-16 10:24:541162 profile_->GetFileSystemContext()->path_manager()->external_provider()) {
1163 profile_->GetFileSystemContext()->path_manager()->external_provider()->
1164 RevokeAccessForExtension(extension->id());
1165 }
[email protected]b777b332011-04-16 04:01:081166#endif
[email protected]62d30f42009-10-01 22:36:061167 }
[email protected]2de307592011-04-05 21:16:581168
1169 bool plugins_changed = false;
1170 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1171 const Extension::PluginInfo& plugin = extension->plugins()[i];
[email protected]14908b72011-04-20 06:54:361172 if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
1173 NewRunnableFunction(&ForceShutdownPlugin,
1174 plugin.path)))
1175 NOTREACHED();
[email protected]2de307592011-04-05 21:16:581176 webkit::npapi::PluginList::Singleton()->RefreshPlugins();
1177 webkit::npapi::PluginList::Singleton()->RemoveExtraPluginPath(
1178 plugin.path);
1179 plugins_changed = true;
1180 if (!plugin.is_public)
1181 PluginService::GetInstance()->RestrictPluginToUrl(plugin.path, GURL());
1182 }
[email protected]84396dbc2011-04-14 06:33:421183
1184 bool nacl_modules_changed = false;
1185 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1186 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001187 UnregisterNaClModule(module.url);
[email protected]84396dbc2011-04-14 06:33:421188 nacl_modules_changed = true;
1189 }
1190
1191 if (nacl_modules_changed)
[email protected]cebc3dc2011-04-18 17:15:001192 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421193
1194 if (plugins_changed || nacl_modules_changed)
[email protected]2de307592011-04-05 21:16:581195 PluginService::GetInstance()->PurgePluginListCache(false);
[email protected]62d30f42009-10-01 22:36:061196}
1197
[email protected]eaa7dd182010-12-14 11:09:001198void ExtensionService::UpdateExtensionBlacklist(
[email protected]6b75ec32009-08-14 06:37:181199 const std::vector<std::string>& blacklist) {
1200 // Use this set to indicate if an extension in the blacklist has been used.
1201 std::set<std::string> blacklist_set;
1202 for (unsigned int i = 0; i < blacklist.size(); ++i) {
1203 if (Extension::IdIsValid(blacklist[i])) {
1204 blacklist_set.insert(blacklist[i]);
1205 }
1206 }
1207 extension_prefs_->UpdateBlacklist(blacklist_set);
1208 std::vector<std::string> to_be_removed;
1209 // Loop current extensions, unload installed extensions.
1210 for (ExtensionList::const_iterator iter = extensions_.begin();
1211 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021212 const Extension* extension = (*iter);
[email protected]6b75ec32009-08-14 06:37:181213 if (blacklist_set.find(extension->id()) != blacklist_set.end()) {
1214 to_be_removed.push_back(extension->id());
1215 }
1216 }
1217
1218 // UnloadExtension will change the extensions_ list. So, we should
1219 // call it outside the iterator loop.
1220 for (unsigned int i = 0; i < to_be_removed.size(); ++i) {
[email protected]a9f39a312010-12-23 22:14:271221 UnloadExtension(to_be_removed[i], UnloadedExtensionInfo::DISABLE);
[email protected]6b75ec32009-08-14 06:37:181222 }
1223}
1224
[email protected]0a071a32011-02-08 00:18:241225Profile* ExtensionService::profile() {
1226 return profile_;
1227}
1228
[email protected]eaa7dd182010-12-14 11:09:001229void ExtensionService::DestroyingProfile() {
[email protected]94b4ffc2011-02-04 05:55:171230 if (updater_.get()) {
1231 updater_->Stop();
1232 }
[email protected]97d2f1d2011-01-15 00:41:081233 browser_event_router_.reset();
[email protected]118de7892011-03-16 13:31:401234 preference_event_router_.reset();
[email protected]2fb7dc982010-09-29 12:24:281235 pref_change_registrar_.RemoveAll();
[email protected]aa96d3a2010-08-21 08:45:251236 profile_ = NULL;
[email protected]2fb7dc982010-09-29 12:24:281237 toolbar_model_.DestroyingProfile();
[email protected]14908b72011-04-20 06:54:361238 method_factory_.RevokeAll();
1239 weak_ptr_factory_.InvalidateWeakPtrs();
[email protected]aa96d3a2010-08-21 08:45:251240}
1241
[email protected]eaa7dd182010-12-14 11:09:001242ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451243 return extension_prefs_;
1244}
1245
[email protected]2859946f2011-04-04 18:18:061246ExtensionUpdater* ExtensionService::updater() {
1247 return updater_.get();
1248}
1249
[email protected]eaa7dd182010-12-14 11:09:001250void ExtensionService::CheckAdminBlacklist() {
[email protected]aa96d3a2010-08-21 08:45:251251 std::vector<std::string> to_be_removed;
1252 // Loop through extensions list, unload installed extensions.
1253 for (ExtensionList::const_iterator iter = extensions_.begin();
1254 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021255 const Extension* extension = (*iter);
[email protected]aa96d3a2010-08-21 08:45:251256 if (!extension_prefs_->IsExtensionAllowedByPolicy(extension->id()))
1257 to_be_removed.push_back(extension->id());
1258 }
1259
1260 // UnloadExtension will change the extensions_ list. So, we should
1261 // call it outside the iterator loop.
1262 for (unsigned int i = 0; i < to_be_removed.size(); ++i)
[email protected]a9f39a312010-12-23 22:14:271263 UnloadExtension(to_be_removed[i], UnloadedExtensionInfo::DISABLE);
[email protected]aa96d3a2010-08-21 08:45:251264}
1265
[email protected]31206602011-04-13 23:07:321266void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351267 if (updater()) {
[email protected]31206602011-04-13 23:07:321268 updater()->CheckSoon();
[email protected]c3cfb012011-04-06 22:07:351269 } else {
[email protected]31206602011-04-13 23:07:321270 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351271 }
1272}
1273
[email protected]b05fb9ff2011-04-23 00:07:561274ExtensionSyncData ExtensionService::GetSyncDataHelper(
1275 const Extension& extension) const {
1276 const std::string& id = extension.id();
1277 ExtensionSyncData data;
1278 data.id = id;
1279 data.uninstalled = false;
1280 data.enabled = IsExtensionEnabled(id);
1281 data.incognito_enabled = IsIncognitoEnabled(id);
1282 data.version = *extension.version();
1283 data.update_url = extension.update_url();
1284 data.name = extension.name();
1285 return data;
1286}
1287
1288bool ExtensionService::GetSyncData(
1289 const std::string& id,
1290 ExtensionFilter filter,
1291 ExtensionSyncData* extension_sync_data) const {
1292 DCHECK(Extension::IdIsValid(id));
1293 // TODO(akalin): Figure out what to do with terminated extensions.
1294 const Extension* extension = GetExtensionById(id, true);
1295 if (!extension || !(*filter)(*extension)) {
1296 return false;
1297 }
1298 *extension_sync_data = GetSyncDataHelper(*extension);
1299 return true;
1300}
1301
1302void ExtensionService::GetSyncDataListHelper(
1303 const ExtensionList& extensions,
1304 ExtensionFilter filter,
1305 std::vector<ExtensionSyncData>* sync_data_list) const {
1306 for (ExtensionList::const_iterator it = extensions.begin();
1307 it != extensions.end(); ++it) {
1308 const Extension& extension = **it;
1309 if ((*filter)(extension)) {
1310 sync_data_list->push_back(GetSyncDataHelper(extension));
1311 }
1312 }
1313}
1314
1315std::vector<ExtensionSyncData> ExtensionService::GetSyncDataList(
1316 ExtensionFilter filter) const {
1317 std::vector<ExtensionSyncData> sync_data_list;
1318 GetSyncDataListHelper(extensions_, filter, &sync_data_list);
1319 GetSyncDataListHelper(disabled_extensions_, filter, &sync_data_list);
1320 // TODO(akalin): Figure out what to do with terminated extensions.
1321 return sync_data_list;
1322}
1323
[email protected]90310d92011-04-17 07:35:041324void ExtensionService::ProcessSyncData(
1325 const ExtensionSyncData& extension_sync_data,
[email protected]b05fb9ff2011-04-23 00:07:561326 ExtensionFilter filter) {
[email protected]90310d92011-04-17 07:35:041327 const std::string& id = extension_sync_data.id;
1328
1329 // Handle uninstalls first.
1330 if (extension_sync_data.uninstalled) {
1331 std::string error;
1332 if (!UninstallExtensionHelper(this, id)) {
1333 LOG(WARNING) << "Could not uninstall extension " << id
1334 << " for sync";
1335 }
1336 return;
1337 }
1338
1339 const Extension* extension = GetExtensionByIdInternal(id, true, true);
1340 // TODO(akalin): Figure out what to do with terminated extensions.
1341
1342 // Handle already-installed extensions (just update settings).
1343 //
1344 // TODO(akalin): Ideally, we should be able to set prefs for an
1345 // extension regardless of whether or not it's installed (and have
1346 // it automatially apply on install).
1347 if (extension) {
1348 if (extension_sync_data.enabled) {
1349 EnableExtension(id);
1350 } else {
1351 DisableExtension(id);
1352 }
1353 SetIsIncognitoEnabled(id, extension_sync_data.incognito_enabled);
1354 int result = extension->version()->CompareTo(extension_sync_data.version);
1355 if (result < 0) {
1356 // Extension is outdated.
1357 CheckForUpdatesSoon();
1358 } else if (result > 0) {
1359 // Sync version is outdated. Do nothing for now, as sync code
1360 // in other places will eventually update the sync data.
1361 //
1362 // TODO(akalin): Move that code here.
1363 }
1364 return;
1365 }
1366
1367 // Handle not-yet-installed extensions.
1368 //
1369 // TODO(akalin): Replace silent update with a list of enabled
1370 // permissions.
1371 pending_extension_manager()->AddFromSync(
1372 id,
1373 extension_sync_data.update_url,
[email protected]b05fb9ff2011-04-23 00:07:561374 filter,
[email protected]90310d92011-04-17 07:35:041375 true, // install_silently
1376 extension_sync_data.enabled,
1377 extension_sync_data.incognito_enabled);
1378 CheckForUpdatesSoon();
1379}
1380
[email protected]c3cfb012011-04-06 22:07:351381bool ExtensionService::IsIncognitoEnabled(
1382 const std::string& extension_id) const {
1383 // If this is an existing component extension we always allow it to
1384 // work in incognito mode.
1385 const Extension* extension = GetExtensionById(extension_id, true);
1386 if (extension && extension->location() == Extension::COMPONENT)
[email protected]cb0ce1e022010-03-10 19:54:411387 return true;
1388
1389 // Check the prefs.
[email protected]c3cfb012011-04-06 22:07:351390 return extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]db7331a2010-02-25 22:10:501391}
[email protected]55a35692010-02-11 23:25:211392
[email protected]c3cfb012011-04-06 22:07:351393void ExtensionService::SetIsIncognitoEnabled(
1394 const std::string& extension_id, bool enabled) {
1395 const Extension* extension = GetExtensionById(extension_id, false);
1396 if (extension && extension->location() == Extension::COMPONENT) {
1397 // This shouldn't be called for component extensions.
1398 NOTREACHED();
1399 return;
1400 }
1401
[email protected]8c6c8c22011-03-09 12:52:341402 // Broadcast unloaded and loaded events to update browser state. Only bother
1403 // if the value changed and the extension is actually enabled, since there is
1404 // no UI otherwise.
[email protected]c3cfb012011-04-06 22:07:351405 bool old_enabled = extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]8c6c8c22011-03-09 12:52:341406 if (enabled == old_enabled)
1407 return;
1408
[email protected]c3cfb012011-04-06 22:07:351409 extension_prefs_->SetIsIncognitoEnabled(extension_id, enabled);
1410 if (extension) {
[email protected]a9f39a312010-12-23 22:14:271411 NotifyExtensionUnloaded(extension, UnloadedExtensionInfo::DISABLE);
[email protected]568f33d2010-08-04 17:06:411412 NotifyExtensionLoaded(extension);
1413 }
[email protected]55a35692010-02-11 23:25:211414}
1415
[email protected]eaa7dd182010-12-14 11:09:001416bool ExtensionService::CanCrossIncognito(const Extension* extension) {
[email protected]2a8f24e2010-11-03 21:37:051417 // We allow the extension to see events and data from another profile iff it
1418 // uses "spanning" behavior and it has incognito access. "split" mode
1419 // extensions only see events for a matching profile.
[email protected]c3cfb012011-04-06 22:07:351420 return IsIncognitoEnabled(extension->id()) &&
1421 !extension->incognito_split_mode();
[email protected]2a8f24e2010-11-03 21:37:051422}
1423
[email protected]eaa7dd182010-12-14 11:09:001424bool ExtensionService::AllowFileAccess(const Extension* extension) {
[email protected]05c82182010-06-24 17:49:081425 return (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]334e04a2010-06-24 23:34:441426 switches::kDisableExtensionsFileAccessCheck) ||
[email protected]05c82182010-06-24 17:49:081427 extension_prefs_->AllowFileAccess(extension->id()));
1428}
1429
[email protected]eaa7dd182010-12-14 11:09:001430void ExtensionService::SetAllowFileAccess(const Extension* extension,
[email protected]3aff9ad2011-04-01 20:26:481431 bool allow) {
1432 // Reload to update browser state. Only bother if the value changed and the
1433 // extension is actually enabled, since there is no UI otherwise.
1434 bool old_allow = AllowFileAccess(extension);
1435 if (allow == old_allow)
1436 return;
1437
[email protected]05c82182010-06-24 17:49:081438 extension_prefs_->SetAllowFileAccess(extension->id(), allow);
[email protected]3aff9ad2011-04-01 20:26:481439
1440 bool extension_is_enabled = std::find(extensions_.begin(), extensions_.end(),
1441 extension) != extensions_.end();
1442 if (extension_is_enabled)
1443 ReloadExtension(extension->id());
[email protected]05c82182010-06-24 17:49:081444}
1445
[email protected]eaa7dd182010-12-14 11:09:001446bool ExtensionService::GetBrowserActionVisibility(const Extension* extension) {
[email protected]f681c782010-11-19 11:19:391447 return extension_prefs_->GetBrowserActionVisibility(extension);
1448}
1449
[email protected]eaa7dd182010-12-14 11:09:001450void ExtensionService::SetBrowserActionVisibility(const Extension* extension,
[email protected]8e4560b62011-01-14 10:09:141451 bool visible) {
[email protected]f681c782010-11-19 11:19:391452 extension_prefs_->SetBrowserActionVisibility(extension, visible);
1453}
1454
[email protected]8e4560b62011-01-14 10:09:141455// Some extensions will autoupdate themselves externally from Chrome. These
1456// are typically part of some larger client application package. To support
1457// these, the extension will register its location in the the preferences file
1458// (and also, on Windows, in the registry) and this code will periodically
1459// check that location for a .crx file, which it will then install locally if
1460// a new version is available.
1461// Errors are reported through ExtensionErrorReporter. Succcess is not
1462// reported.
[email protected]eaa7dd182010-12-14 11:09:001463void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121464 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141465
1466 // Note that this installation is intentionally silent (since it didn't
1467 // go through the front-end). Extensions that are registered in this
1468 // way are effectively considered 'pre-bundled', and so implicitly
1469 // trusted. In general, if something has HKLM or filesystem access,
1470 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121471
1472 // If any external extension records give a URL, a provider will set
1473 // this to true. Used by OnExternalProviderReady() to see if we need
1474 // to start an update check to fetch a new external extension.
1475 external_extension_url_added_ = false;
[email protected]8e4560b62011-01-14 10:09:141476
1477 // Ask each external extension provider to give us a call back for each
1478 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
1479 ProviderCollection::const_iterator i;
1480 for (i = external_extension_providers_.begin();
1481 i != external_extension_providers_.end(); ++i) {
1482 ExternalExtensionProviderInterface* provider = i->get();
1483 provider->VisitRegisteredExtension();
1484 }
1485
1486 // Uninstall of unclaimed extensions will happen after all the providers
[email protected]a29a517a2011-01-21 21:11:121487 // had reported ready. Every provider calls OnExternalProviderReady()
1488 // when it finishes, and OnExternalProviderReady() only acts when all
1489 // providers are ready. In case there are no providers, we call it
1490 // to trigger removal of extensions that used to have an external source.
1491 if (external_extension_providers_.empty())
1492 OnExternalProviderReady();
[email protected]9f1087e2009-06-15 17:29:321493}
1494
[email protected]8e4560b62011-01-14 10:09:141495void ExtensionService::OnExternalProviderReady() {
[email protected]a29a517a2011-01-21 21:11:121496 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1497
1498 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141499 // if all of them are finished. So we check them first.
1500 ProviderCollection::const_iterator i;
1501 for (i = external_extension_providers_.begin();
1502 i != external_extension_providers_.end(); ++i) {
1503 ExternalExtensionProviderInterface* provider = i->get();
[email protected]a29a517a2011-01-21 21:11:121504 if (!provider->IsReady())
[email protected]8e4560b62011-01-14 10:09:141505 return;
[email protected]8e4560b62011-01-14 10:09:141506 }
1507
[email protected]a29a517a2011-01-21 21:11:121508 // All the providers are ready. Install any pending extensions.
1509 if (external_extension_url_added_ && updater()) {
1510 external_extension_url_added_ = false;
[email protected]8e4560b62011-01-14 10:09:141511 updater()->CheckNow();
1512 }
1513
1514 // Uninstall all the unclaimed extensions.
1515 scoped_ptr<ExtensionPrefs::ExtensionsInfo> extensions_info(
1516 extension_prefs_->GetInstalledExtensionsInfo());
1517 for (size_t i = 0; i < extensions_info->size(); ++i) {
1518 ExtensionInfo* info = extensions_info->at(i).get();
1519 if (Extension::IsExternalLocation(info->extension_location))
1520 CheckExternalUninstall(info->extension_id);
1521 }
[email protected]6c751e72010-11-23 10:11:101522}
1523
[email protected]a9f39a312010-12-23 22:14:271524void ExtensionService::UnloadExtension(
1525 const std::string& extension_id,
1526 UnloadedExtensionInfo::Reason reason) {
[email protected]27e469a2010-01-11 20:35:091527 // Make sure the extension gets deleted after we return from this function.
[email protected]9adb9692010-10-29 23:14:021528 scoped_refptr<const Extension> extension(
[email protected]0c6da502009-08-14 22:32:391529 GetExtensionByIdInternal(extension_id, true, true));
[email protected]631cf822009-05-15 07:01:251530
[email protected]fa0c96732010-11-17 00:14:231531 // This method can be called via PostTask, so the extension may have been
1532 // unloaded by the time this runs.
1533 if (!extension)
1534 return;
[email protected]0c6da502009-08-14 22:32:391535
[email protected]1eb175082010-02-10 09:26:161536 // Keep information about the extension so that we can reload it later
1537 // even if it's not permanently installed.
1538 unloaded_extension_paths_[extension->id()] = extension->path();
1539
[email protected]f17dbd42010-08-16 23:21:101540 // Clean up if the extension is meant to be enabled after a reload.
1541 disabled_extension_paths_.erase(extension->id());
1542
[email protected]d7e9a862010-11-03 21:57:491543 // Clean up runtime data.
1544 extension_runtime_data_.erase(extension_id);
1545
[email protected]8f9d4eb2011-02-05 01:39:101546 ExtensionWebUI::UnregisterChromeURLOverrides(profile_,
[email protected]86c008e82009-08-28 20:26:051547 extension->GetChromeURLOverrides());
1548
[email protected]0c6da502009-08-14 22:32:391549 ExtensionList::iterator iter = std::find(disabled_extensions_.begin(),
1550 disabled_extensions_.end(),
1551 extension.get());
1552 if (iter != disabled_extensions_.end()) {
[email protected]a9f39a312010-12-23 22:14:271553 UnloadedExtensionInfo details(extension, reason);
1554 details.already_disabled = true;
[email protected]0c6da502009-08-14 22:32:391555 disabled_extensions_.erase(iter);
[email protected]866930682009-08-18 22:53:471556 NotificationService::current()->Notify(
[email protected]a9f39a312010-12-23 22:14:271557 NotificationType::EXTENSION_UNLOADED,
[email protected]24e7a9d2009-11-04 11:11:341558 Source<Profile>(profile_),
[email protected]a9f39a312010-12-23 22:14:271559 Details<UnloadedExtensionInfo>(&details));
[email protected]0c6da502009-08-14 22:32:391560 return;
1561 }
1562
1563 iter = std::find(extensions_.begin(), extensions_.end(), extension.get());
[email protected]894bb502009-05-21 22:39:571564
[email protected]631cf822009-05-15 07:01:251565 // Remove the extension from our list.
1566 extensions_.erase(iter);
1567
[email protected]a9f39a312010-12-23 22:14:271568 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]aab98a52009-12-02 03:22:351569 UpdateActiveExtensionsInCrashReporter();
[email protected]631cf822009-05-15 07:01:251570}
1571
[email protected]eaa7dd182010-12-14 11:09:001572void ExtensionService::UnloadAllExtensions() {
[email protected]19eb80152011-02-26 00:28:431573 if (profile_) {
1574 profile_->GetExtensionSpecialStoragePolicy()->
1575 RevokeRightsForAllExtensions();
1576 }
[email protected]9f1087e2009-06-15 17:29:321577 extensions_.clear();
[email protected]cd500f72010-06-25 23:44:321578 disabled_extensions_.clear();
[email protected]bb7f40952011-01-13 00:21:201579 terminated_extension_ids_.clear();
1580 terminated_extensions_.clear();
[email protected]d7e9a862010-11-03 21:57:491581 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:321582
[email protected]c6e4a3412009-06-24 15:45:291583 // TODO(erikkay) should there be a notification for this? We can't use
1584 // EXTENSION_UNLOADED since that implies that the extension has been disabled
1585 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:321586}
1587
[email protected]eaa7dd182010-12-14 11:09:001588void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:321589 UnloadAllExtensions();
1590 LoadAllExtensions();
1591}
1592
[email protected]eaa7dd182010-12-14 11:09:001593void ExtensionService::GarbageCollectExtensions() {
[email protected]39d9f62c2010-12-03 10:48:501594 if (extension_prefs_->pref_service()->ReadOnly())
[email protected]ba399672010-04-06 15:42:391595 return;
1596
[email protected]ca3dbf52010-05-19 22:27:061597 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(
1598 extension_prefs_->GetInstalledExtensionsInfo());
1599
1600 std::map<std::string, FilePath> extension_paths;
1601 for (size_t i = 0; i < info->size(); ++i)
1602 extension_paths[info->at(i)->extension_id] = info->at(i)->extension_path;
1603
[email protected]14908b72011-04-20 06:54:361604 if (!BrowserThread::PostTask(
1605 BrowserThread::FILE, FROM_HERE,
1606 NewRunnableFunction(
1607 &extension_file_util::GarbageCollectExtensions,
1608 install_directory_,
1609 extension_paths)))
1610 NOTREACHED();
[email protected]f6ccd652010-11-17 00:48:341611
1612 // Also garbage-collect themes. We check |profile_| to be
1613 // defensive; in the future, we may call GarbageCollectExtensions()
1614 // from somewhere other than Init() (e.g., in a timer).
1615 if (profile_) {
[email protected]18280372011-03-22 18:05:221616 ThemeServiceFactory::GetForProfile(profile_)->RemoveUnusedThemes();
[email protected]f6ccd652010-11-17 00:48:341617 }
[email protected]3cf4f0992009-02-03 23:00:301618}
1619
[email protected]eaa7dd182010-12-14 11:09:001620void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]93fd78f42009-07-10 16:43:171621 if (updater_.get()) {
1622 updater_->Start();
1623 }
[email protected]fa6a9102010-11-22 15:38:501624
1625 ready_ = true;
[email protected]e72e8eb82009-06-18 17:21:511626 NotificationService::current()->Notify(
1627 NotificationType::EXTENSIONS_READY,
[email protected]24e7a9d2009-11-04 11:11:341628 Source<Profile>(profile_),
[email protected]e72e8eb82009-06-18 17:21:511629 NotificationService::NoDetails());
1630}
1631
[email protected]49cd1572011-02-08 21:38:451632void ExtensionService::AddExtension(const Extension* extension) {
[email protected]ae09ca62009-08-21 19:46:461633 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:021634 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]9f1087e2009-06-15 17:29:321635
[email protected]1eb175082010-02-10 09:26:161636 // The extension is now loaded, remove its data from unloaded extension map.
1637 unloaded_extension_paths_.erase(extension->id());
1638
[email protected]bb7f40952011-01-13 00:21:201639 // If a terminated extension is loaded, remove it from the terminated list.
1640 UntrackTerminatedExtension(extension->id());
1641
[email protected]f17dbd42010-08-16 23:21:101642 // If the extension was disabled for a reload, then enable it.
1643 if (disabled_extension_paths_.erase(extension->id()) > 0)
1644 EnableExtension(extension->id());
1645
[email protected]d728e002010-12-08 04:46:231646 // TODO(jstritar): We may be able to get rid of this branch by overriding the
1647 // default extension state to DISABLED when the --disable-extensions flag
1648 // is set (http://crbug.com/29067).
1649 if (!extensions_enabled() &&
1650 !extension->is_theme() &&
[email protected]aebe23a32010-12-10 22:15:481651 extension->location() != Extension::COMPONENT &&
1652 !Extension::IsExternalLocation(extension->location()))
[email protected]d728e002010-12-08 04:46:231653 return;
1654
1655 // Check if the extension's privileges have changed and disable the
1656 // extension if necessary.
[email protected]8d888c12010-11-30 00:00:251657 DisableIfPrivilegeIncrease(extension);
[email protected]0c6da502009-08-14 22:32:391658
[email protected]8d888c12010-11-30 00:00:251659 switch (extension_prefs_->GetExtensionState(extension->id())) {
1660 case Extension::ENABLED:
1661 extensions_.push_back(scoped_extension);
[email protected]1e8c93f2010-02-08 22:58:311662
[email protected]8d888c12010-11-30 00:00:251663 NotifyExtensionLoaded(extension);
[email protected]0c6da502009-08-14 22:32:391664
[email protected]8f9d4eb2011-02-05 01:39:101665 ExtensionWebUI::RegisterChromeURLOverrides(
[email protected]8d888c12010-11-30 00:00:251666 profile_, extension->GetChromeURLOverrides());
1667 break;
1668 case Extension::DISABLED:
1669 disabled_extensions_.push_back(scoped_extension);
1670 NotificationService::current()->Notify(
1671 NotificationType::EXTENSION_UPDATE_DISABLED,
1672 Source<Profile>(profile_),
1673 Details<const Extension>(extension));
1674 break;
1675 default:
1676 NOTREACHED();
1677 break;
[email protected]e72e8eb82009-06-18 17:21:511678 }
[email protected]aab98a52009-12-02 03:22:351679
[email protected]d7e9a862010-11-03 21:57:491680 SetBeingUpgraded(extension, false);
[email protected]1e8c93f2010-02-08 22:58:311681
[email protected]aab98a52009-12-02 03:22:351682 UpdateActiveExtensionsInCrashReporter();
[email protected]0b004da2010-07-02 17:54:311683
1684 if (profile_->GetTemplateURLModel())
1685 profile_->GetTemplateURLModel()->RegisterExtensionKeyword(extension);
[email protected]b671760b2010-07-15 21:13:471686
1687 // Load the icon for omnibox-enabled extensions so it will be ready to display
1688 // in the URL bar.
[email protected]29d0d4ac2010-09-08 21:10:311689 if (!extension->omnibox_keyword().empty()) {
1690 omnibox_popup_icon_manager_.LoadIcon(extension);
[email protected]b671760b2010-07-15 21:13:471691 omnibox_icon_manager_.LoadIcon(extension);
[email protected]29d0d4ac2010-09-08 21:10:311692 }
[email protected]aab98a52009-12-02 03:22:351693}
1694
[email protected]eaa7dd182010-12-14 11:09:001695void ExtensionService::DisableIfPrivilegeIncrease(const Extension* extension) {
[email protected]8d888c12010-11-30 00:00:251696 // We keep track of all permissions the user has granted each extension.
1697 // This allows extensions to gracefully support backwards compatibility
1698 // by including unknown permissions in their manifests. When the user
1699 // installs the extension, only the recognized permissions are recorded.
1700 // When the unknown permissions become recognized (e.g., through browser
1701 // upgrade), we can prompt the user to accept these new permissions.
1702 // Extensions can also silently upgrade to less permissions, and then
1703 // silently upgrade to a version that adds these permissions back.
1704 //
1705 // For example, pretend that Chrome 10 includes a permission "omnibox"
1706 // for an API that adds suggestions to the omnibox. An extension can
1707 // maintain backwards compatibility while still having "omnibox" in the
1708 // manifest. If a user installs the extension on Chrome 9, the browser
1709 // will record the permissions it recognized, not including "omnibox."
1710 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
1711 // will disable the extension and prompt the user to approve the increase
1712 // in privileges. The extension could then release a new version that
1713 // removes the "omnibox" permission. When the user upgrades, Chrome will
1714 // still remember that "omnibox" had been granted, so that if the
1715 // extension once again includes "omnibox" in an upgrade, the extension
1716 // can upgrade without requiring this user's approval.
1717 const Extension* old = GetExtensionByIdInternal(extension->id(),
1718 true, true);
1719 bool granted_full_access;
1720 std::set<std::string> granted_apis;
1721 ExtensionExtent granted_extent;
1722
1723 bool is_extension_upgrade = old != NULL;
1724 bool is_privilege_increase = false;
1725
1726 // We only record the granted permissions for INTERNAL extensions, since
1727 // they can't silently increase privileges.
1728 if (extension->location() == Extension::INTERNAL) {
1729 // Add all the recognized permissions if the granted permissions list
1730 // hasn't been initialized yet.
1731 if (!extension_prefs_->GetGrantedPermissions(extension->id(),
1732 &granted_full_access,
1733 &granted_apis,
1734 &granted_extent)) {
1735 GrantPermissions(extension);
1736 CHECK(extension_prefs_->GetGrantedPermissions(extension->id(),
1737 &granted_full_access,
1738 &granted_apis,
1739 &granted_extent));
1740 }
1741
1742 // Here, we check if an extension's privileges have increased in a manner
1743 // that requires the user's approval. This could occur because the browser
1744 // upgraded and recognized additional privileges, or an extension upgrades
1745 // to a version that requires additional privileges.
1746 is_privilege_increase = Extension::IsPrivilegeIncrease(
1747 granted_full_access, granted_apis, granted_extent, extension);
1748 }
1749
1750 if (is_extension_upgrade) {
[email protected]66e26872010-12-03 20:07:251751 // Other than for unpacked extensions, CrxInstaller should have guaranteed
1752 // that we aren't downgrading.
1753 if (extension->location() != Extension::LOAD)
1754 CHECK(extension->version()->CompareTo(*(old->version())) >= 0);
[email protected]8d888c12010-11-30 00:00:251755
1756 // Extensions get upgraded if the privileges are allowed to increase or
1757 // the privileges haven't increased.
1758 if (!is_privilege_increase) {
1759 SetBeingUpgraded(old, true);
1760 SetBeingUpgraded(extension, true);
1761 }
1762
1763 // To upgrade an extension in place, unload the old one and
1764 // then load the new one.
[email protected]a9f39a312010-12-23 22:14:271765 UnloadExtension(old->id(), UnloadedExtensionInfo::UPDATE);
[email protected]8d888c12010-11-30 00:00:251766 old = NULL;
1767 }
1768
1769 // Extension has changed permissions significantly. Disable it. A
1770 // notification should be sent by the caller.
1771 if (is_privilege_increase) {
[email protected]fe2dd7742011-04-19 22:52:491772 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
1773 RecordPermissionMessagesHistogram(
1774 extension, "Extensions.Permissions_AutoDisable");
1775 }
[email protected]8d888c12010-11-30 00:00:251776 extension_prefs_->SetExtensionState(extension, Extension::DISABLED);
1777 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
1778 }
1779}
1780
[email protected]eaa7dd182010-12-14 11:09:001781void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:391782 std::set<std::string> extension_ids;
[email protected]aab98a52009-12-02 03:22:351783 for (size_t i = 0; i < extensions_.size(); ++i) {
[email protected]2a500c1b2010-11-05 15:27:341784 if (!extensions_[i]->is_theme() &&
1785 extensions_[i]->location() != Extension::COMPONENT)
[email protected]c8865962009-12-16 07:47:391786 extension_ids.insert(extensions_[i]->id());
[email protected]aab98a52009-12-02 03:22:351787 }
1788
1789 child_process_logging::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:251790}
1791
[email protected]eaa7dd182010-12-14 11:09:001792void ExtensionService::OnExtensionInstalled(const Extension* extension) {
[email protected]a29a517a2011-01-21 21:11:121793 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:201794
[email protected]4416c5a2010-06-26 01:28:571795 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:021796 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]c3cfb012011-04-06 22:07:351797 const std::string& id = extension->id();
1798 bool initial_enable = false;
[email protected]4416c5a2010-06-26 01:28:571799 bool initial_enable_incognito = false;
[email protected]b2907fd2011-03-25 16:43:371800
1801 PendingExtensionInfo pending_extension_info;
[email protected]c3cfb012011-04-06 22:07:351802 if (pending_extension_manager()->GetById(id, &pending_extension_info)) {
1803 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:321804
[email protected]483d1ff2011-03-03 17:12:021805 if (!pending_extension_info.ShouldAllowInstall(*extension)) {
[email protected]4416c5a2010-06-26 01:28:571806 LOG(WARNING)
[email protected]ae6cb3022011-04-04 20:11:541807 << "ShouldAllowInstall() returned false for "
[email protected]c3cfb012011-04-06 22:07:351808 << id << " of type " << extension->GetType()
[email protected]7fa19f82010-12-21 19:40:081809 << " and update URL " << extension->update_url().spec()
1810 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:541811
1812 NotificationService::current()->Notify(
1813 NotificationType::EXTENSION_INSTALL_NOT_ALLOWED,
1814 Source<Profile>(profile_),
1815 Details<const Extension>(extension));
1816
[email protected]4416c5a2010-06-26 01:28:571817 // Delete the extension directory since we're not going to
1818 // load it.
[email protected]14908b72011-04-20 06:54:361819 if (!BrowserThread::PostTask(
1820 BrowserThread::FILE, FROM_HERE,
1821 NewRunnableFunction(&extension_file_util::DeleteFile,
1822 extension->path(), true)))
1823 NOTREACHED();
[email protected]4416c5a2010-06-26 01:28:571824 return;
1825 }
[email protected]8ef78fd2010-08-19 17:14:321826
[email protected]8ef78fd2010-08-19 17:14:321827 if (extension->is_theme()) {
[email protected]483d1ff2011-03-03 17:12:021828 DCHECK(pending_extension_info.enable_on_install());
[email protected]c3cfb012011-04-06 22:07:351829 initial_enable = true;
[email protected]483d1ff2011-03-03 17:12:021830 DCHECK(!pending_extension_info.enable_incognito_on_install());
[email protected]4416c5a2010-06-26 01:28:571831 initial_enable_incognito = false;
1832 } else {
[email protected]c3cfb012011-04-06 22:07:351833 initial_enable = pending_extension_info.enable_on_install();
[email protected]4416c5a2010-06-26 01:28:571834 initial_enable_incognito =
[email protected]483d1ff2011-03-03 17:12:021835 pending_extension_info.enable_incognito_on_install();
[email protected]4416c5a2010-06-26 01:28:571836 }
[email protected]4416c5a2010-06-26 01:28:571837 } else {
[email protected]c3cfb012011-04-06 22:07:351838 // We explicitly want to re-enable an uninstalled external
1839 // extension; if we're here, that means the user is manually
1840 // installing the extension.
1841 initial_enable =
1842 IsExtensionEnabled(id) || IsExternalExtensionUninstalled(id);
1843 initial_enable_incognito = IsIncognitoEnabled(id);
[email protected]aa142702010-03-26 01:26:331844 }
1845
[email protected]9b217652010-10-08 22:04:231846 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
[email protected]7fa19f82010-12-21 19:40:081847 extension->GetType(), 100);
[email protected]fe2dd7742011-04-19 22:52:491848 RecordPermissionMessagesHistogram(
1849 extension, "Extensions.Permissions_Install");
[email protected]eda3c362010-11-12 08:08:231850 ShownSectionsHandler::OnExtensionInstalled(profile_->GetPrefs(), extension);
[email protected]4416c5a2010-06-26 01:28:571851 extension_prefs_->OnExtensionInstalled(
[email protected]c3cfb012011-04-06 22:07:351852 extension, initial_enable ? Extension::ENABLED : Extension::DISABLED,
1853 initial_enable_incognito);
[email protected]25b34332009-06-05 21:53:191854
[email protected]3aff9ad2011-04-01 20:26:481855 // Unpacked extensions default to allowing file access, but if that has been
1856 // overridden, don't reset the value.
1857 if (Extension::ShouldAlwaysAllowFileAccess(Extension::LOAD) &&
[email protected]c3cfb012011-04-06 22:07:351858 !extension_prefs_->HasAllowFileAccessSetting(id)) {
1859 extension_prefs_->SetAllowFileAccess(id, true);
[email protected]3aff9ad2011-04-01 20:26:481860 }
[email protected]92a5b1d2010-07-20 00:42:001861
[email protected]a3e61e82011-04-15 20:32:081862 NotificationService::current()->Notify(
1863 NotificationType::EXTENSION_INSTALLED,
1864 Source<Profile>(profile_),
1865 Details<const Extension>(extension));
[email protected]7577a5c52009-07-30 06:21:581866
[email protected]49cd1572011-02-08 21:38:451867 // Transfer ownership of |extension| to AddExtension.
1868 AddExtension(scoped_extension);
[email protected]4a190632009-05-09 01:07:421869}
1870
[email protected]eaa7dd182010-12-14 11:09:001871const Extension* ExtensionService::GetExtensionByIdInternal(
[email protected]b2907fd2011-03-25 16:43:371872 const std::string& id, bool include_enabled, bool include_disabled) const {
[email protected]e957fe52009-06-23 16:51:051873 std::string lowercase_id = StringToLowerASCII(id);
[email protected]0c6da502009-08-14 22:32:391874 if (include_enabled) {
1875 for (ExtensionList::const_iterator iter = extensions_.begin();
1876 iter != extensions_.end(); ++iter) {
1877 if ((*iter)->id() == lowercase_id)
1878 return *iter;
1879 }
1880 }
1881 if (include_disabled) {
1882 for (ExtensionList::const_iterator iter = disabled_extensions_.begin();
1883 iter != disabled_extensions_.end(); ++iter) {
1884 if ((*iter)->id() == lowercase_id)
1885 return *iter;
1886 }
[email protected]ce5c4502009-05-06 16:46:111887 }
1888 return NULL;
1889}
1890
[email protected]bb7f40952011-01-13 00:21:201891void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
1892 if (terminated_extension_ids_.insert(extension->id()).second)
1893 terminated_extensions_.push_back(make_scoped_refptr(extension));
1894}
1895
1896void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
1897 if (terminated_extension_ids_.erase(id) <= 0)
1898 return;
1899
1900 std::string lowercase_id = StringToLowerASCII(id);
1901 for (ExtensionList::iterator iter = terminated_extensions_.begin();
1902 iter != terminated_extensions_.end(); ++iter) {
1903 if ((*iter)->id() == lowercase_id) {
1904 terminated_extensions_.erase(iter);
1905 return;
1906 }
1907 }
1908}
1909
[email protected]0dfe05c2011-02-23 23:03:361910const Extension* ExtensionService::GetTerminatedExtension(
1911 const std::string& id) {
1912 std::string lowercase_id = StringToLowerASCII(id);
1913 for (ExtensionList::const_iterator iter = terminated_extensions_.begin();
1914 iter != terminated_extensions_.end(); ++iter) {
1915 if ((*iter)->id() == lowercase_id)
1916 return *iter;
1917 }
1918 return NULL;
1919}
1920
[email protected]eaa7dd182010-12-14 11:09:001921const Extension* ExtensionService::GetWebStoreApp() {
[email protected]d3071992010-10-08 15:24:071922 return GetExtensionById(extension_misc::kWebStoreAppId, false);
1923}
1924
[email protected]eaa7dd182010-12-14 11:09:001925const Extension* ExtensionService::GetExtensionByURL(const GURL& url) {
[email protected]a888b29e62010-04-01 13:38:571926 return url.scheme() != chrome::kExtensionScheme ? NULL :
1927 GetExtensionById(url.host(), false);
1928}
1929
[email protected]eaa7dd182010-12-14 11:09:001930const Extension* ExtensionService::GetExtensionByWebExtent(const GURL& url) {
[email protected]a888b29e62010-04-01 13:38:571931 for (size_t i = 0; i < extensions_.size(); ++i) {
1932 if (extensions_[i]->web_extent().ContainsURL(url))
1933 return extensions_[i];
1934 }
1935 return NULL;
[email protected]9f1087e2009-06-15 17:29:321936}
1937
[email protected]eaa7dd182010-12-14 11:09:001938bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]583d45c12010-08-31 02:48:121939 // Allow bindings for all packaged extension.
1940 if (GetExtensionByURL(url))
1941 return true;
1942
1943 // Allow bindings for all component, hosted apps.
[email protected]9adb9692010-10-29 23:14:021944 const Extension* extension = GetExtensionByWebExtent(url);
[email protected]583d45c12010-08-31 02:48:121945 return (extension && extension->location() == Extension::COMPONENT);
1946}
1947
[email protected]eaa7dd182010-12-14 11:09:001948const Extension* ExtensionService::GetExtensionByOverlappingWebExtent(
[email protected]9f72aa02010-06-25 10:01:051949 const ExtensionExtent& extent) {
[email protected]22c966c2010-06-26 06:35:021950 for (size_t i = 0; i < extensions_.size(); ++i) {
1951 if (extensions_[i]->web_extent().OverlapsWith(extent))
1952 return extensions_[i];
1953 }
1954
[email protected]6d2e60bd2010-06-03 22:37:391955 return NULL;
1956}
1957
[email protected]eaa7dd182010-12-14 11:09:001958const SkBitmap& ExtensionService::GetOmniboxIcon(
[email protected]b671760b2010-07-15 21:13:471959 const std::string& extension_id) {
1960 return omnibox_icon_manager_.GetIcon(extension_id);
1961}
1962
[email protected]eaa7dd182010-12-14 11:09:001963const SkBitmap& ExtensionService::GetOmniboxPopupIcon(
[email protected]29d0d4ac2010-09-08 21:10:311964 const std::string& extension_id) {
1965 return omnibox_popup_icon_manager_.GetIcon(extension_id);
1966}
1967
[email protected]eaa7dd182010-12-14 11:09:001968void ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:321969 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:141970 const Version* version,
[email protected]8ef78fd2010-08-19 17:14:321971 const FilePath& path,
1972 Extension::Location location) {
[email protected]ab22ba42011-01-14 16:36:381973 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1974 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:011975 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]683d0702010-12-06 16:25:571976 return;
[email protected]a8af9fdb2010-10-28 21:52:201977
[email protected]8e4560b62011-01-14 10:09:141978 DCHECK(version);
1979
[email protected]7577a5c52009-07-30 06:21:581980 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:491981 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:581982 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:021983 const Extension* existing = GetExtensionById(id, true);
[email protected]7577a5c52009-07-30 06:21:581984 if (existing) {
[email protected]8e4560b62011-01-14 10:09:141985 switch (existing->version()->CompareTo(*version)) {
[email protected]7577a5c52009-07-30 06:21:581986 case -1: // existing version is older, we should upgrade
1987 break;
1988 case 0: // existing version is same, do nothing
1989 return;
1990 case 1: // existing version is newer, uh-oh
1991 LOG(WARNING) << "Found external version of extension " << id
1992 << "that is older than current version. Current version "
1993 << "is: " << existing->VersionString() << ". New version "
1994 << "is: " << version << ". Keeping current version.";
1995 return;
1996 }
1997 }
1998
[email protected]b2907fd2011-03-25 16:43:371999 pending_extension_manager()->AddFromExternalFile(id, location);
[email protected]9c635f22010-12-02 09:36:362000
[email protected]14908b72011-04-20 06:54:362001 // no client (silent install)
2002 scoped_refptr<CrxInstaller> installer(MakeCrxInstaller(NULL));
[email protected]6dfbbf82010-03-12 23:09:162003 installer->set_install_source(location);
2004 installer->set_expected_id(id);
[email protected]604322d2011-03-22 16:51:562005 installer->set_expected_version(*version),
[email protected]6dfbbf82010-03-12 23:09:162006 installer->InstallCrx(path);
[email protected]7577a5c52009-07-30 06:21:582007}
2008
[email protected]eaa7dd182010-12-14 11:09:002009void ExtensionService::ReportExtensionLoadError(
[email protected]d11c8e92009-10-20 23:26:402010 const FilePath& extension_path,
2011 const std::string &error,
2012 NotificationType type,
2013 bool be_noisy) {
2014 NotificationService* service = NotificationService::current();
2015 service->Notify(type,
[email protected]24e7a9d2009-11-04 11:11:342016 Source<Profile>(profile_),
[email protected]d11c8e92009-10-20 23:26:402017 Details<const std::string>(&error));
2018
[email protected]8a205c02011-02-04 20:41:332019 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]18d4b6c2010-09-21 03:21:042020 std::string message = base::StringPrintf(
2021 "Could not load extension from '%s'. %s",
2022 path_str.c_str(), error.c_str());
[email protected]d11c8e92009-10-20 23:26:402023 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2024}
2025
[email protected]eaa7dd182010-12-14 11:09:002026void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]406027c02010-09-27 08:03:182027 ExtensionHost* host) {
2028 OrphanedDevTools::iterator iter =
2029 orphaned_dev_tools_.find(host->extension()->id());
2030 if (iter == orphaned_dev_tools_.end())
2031 return;
2032
2033 DevToolsManager::GetInstance()->AttachClientHost(
2034 iter->second, host->render_view_host());
2035 orphaned_dev_tools_.erase(iter);
2036}
2037
[email protected]eaa7dd182010-12-14 11:09:002038void ExtensionService::Observe(NotificationType type,
[email protected]4814b512009-11-07 00:12:292039 const NotificationSource& source,
2040 const NotificationDetails& details) {
2041 switch (type.value) {
[email protected]a4ed6282009-12-14 20:51:162042 case NotificationType::EXTENSION_PROCESS_TERMINATED: {
[email protected]bc535ee52010-08-31 18:40:322043 if (profile_ != Source<Profile>(source).ptr()->GetOriginalProfile())
2044 break;
[email protected]a4ed6282009-12-14 20:51:162045
[email protected]f128af42010-08-05 18:05:262046 ExtensionHost* host = Details<ExtensionHost>(details).ptr();
[email protected]bb7f40952011-01-13 00:21:202047 TrackTerminatedExtension(host->extension());
[email protected]f128af42010-08-05 18:05:262048
[email protected]31f77262009-12-02 20:48:532049 // Unload the entire extension. We want it to be in a consistent state:
2050 // either fully working or not loaded at all, but never half-crashed.
[email protected]bc535ee52010-08-31 18:40:322051 // We do it in a PostTask so that other handlers of this notification will
2052 // still have access to the Extension and ExtensionHost.
[email protected]14908b72011-04-20 06:54:362053 MessageLoop::current()->PostTask(
2054 FROM_HERE,
2055 method_factory_.NewRunnableMethod(
2056 &ExtensionService::UnloadExtension,
2057 host->extension()->id(),
2058 UnloadedExtensionInfo::DISABLE));
[email protected]31f77262009-12-02 20:48:532059 break;
2060 }
[email protected]77a6970c2011-04-23 16:58:562061 case NotificationType::RENDERER_PROCESS_CREATED: {
2062 RenderProcessHost* process = Source<RenderProcessHost>(source).ptr();
2063 // Valid extension function names, used to setup bindings in renderer.
2064 std::vector<std::string> function_names;
2065 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names);
2066 process->Send(new ExtensionMsg_SetFunctionNames(function_names));
[email protected]31f77262009-12-02 20:48:532067
[email protected]77a6970c2011-04-23 16:58:562068 // Scripting whitelist. This is modified by tests and must be communicated
2069 // to renderers.
2070 process->Send(new ExtensionMsg_SetScriptingWhitelist(
2071 *Extension::GetScriptingWhitelist()));
2072
2073 // Loaded extensions.
2074 for (size_t i = 0; i < extensions_.size(); ++i) {
2075 process->Send(new ExtensionMsg_Loaded(
2076 ExtensionMsg_Loaded_Params(extensions_[i])));
2077 }
2078 break;
2079 }
[email protected]da5683db2011-04-23 17:12:212080 case NotificationType::RENDERER_PROCESS_TERMINATED: {
2081 RenderProcessHost* process = Source<RenderProcessHost>(source).ptr();
2082 installed_app_hosts_.erase(process->id());
2083 break;
2084 }
[email protected]aa96d3a2010-08-21 08:45:252085 case NotificationType::PREF_CHANGED: {
2086 std::string* pref_name = Details<std::string>(details).ptr();
[email protected]6c751e72010-11-23 10:11:102087 if (*pref_name == prefs::kExtensionInstallAllowList ||
2088 *pref_name == prefs::kExtensionInstallDenyList) {
2089 CheckAdminBlacklist();
[email protected]6c751e72010-11-23 10:11:102090 } else {
2091 NOTREACHED() << "Unexpected preference name.";
2092 }
[email protected]aa96d3a2010-08-21 08:45:252093 break;
2094 }
2095
[email protected]4814b512009-11-07 00:12:292096 default:
2097 NOTREACHED() << "Unexpected notification type.";
2098 }
2099}
2100
[email protected]eaa7dd182010-12-14 11:09:002101bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182102 return !GetAppIds().empty();
2103}
[email protected]377011d2010-07-20 04:18:502104
[email protected]eaa7dd182010-12-14 11:09:002105ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182106 ExtensionIdSet result;
[email protected]377011d2010-07-20 04:18:502107 for (ExtensionList::const_iterator it = extensions_.begin();
2108 it != extensions_.end(); ++it) {
[email protected]ec5b50d2010-10-09 16:35:182109 if ((*it)->is_app() && (*it)->location() != Extension::COMPONENT)
2110 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502111 }
2112
[email protected]ec5b50d2010-10-09 16:35:182113 return result;
[email protected]377011d2010-07-20 04:18:502114}
[email protected]d7e9a862010-11-03 21:57:492115
[email protected]14908b72011-04-20 06:54:362116scoped_refptr<CrxInstaller> ExtensionService::MakeCrxInstaller(
2117 ExtensionInstallUI* client) {
2118 return new CrxInstaller(weak_ptr_factory_.GetWeakPtr(), client);
2119}
2120
[email protected]eaa7dd182010-12-14 11:09:002121bool ExtensionService::IsBackgroundPageReady(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492122 return (extension->background_url().is_empty() ||
2123 extension_runtime_data_[extension->id()].background_page_ready);
2124}
2125
[email protected]eaa7dd182010-12-14 11:09:002126void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492127 DCHECK(!extension->background_url().is_empty());
2128 extension_runtime_data_[extension->id()].background_page_ready = true;
2129 NotificationService::current()->Notify(
2130 NotificationType::EXTENSION_BACKGROUND_PAGE_READY,
2131 Source<const Extension>(extension),
2132 NotificationService::NoDetails());
2133}
2134
[email protected]eaa7dd182010-12-14 11:09:002135bool ExtensionService::IsBeingUpgraded(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492136 return extension_runtime_data_[extension->id()].being_upgraded;
2137}
2138
[email protected]eaa7dd182010-12-14 11:09:002139void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]d7e9a862010-11-03 21:57:492140 bool value) {
2141 extension_runtime_data_[extension->id()].being_upgraded = value;
2142}
[email protected]1bead0712010-11-27 17:41:532143
[email protected]eaa7dd182010-12-14 11:09:002144PropertyBag* ExtensionService::GetPropertyBag(const Extension* extension) {
[email protected]1bead0712010-11-27 17:41:532145 return &extension_runtime_data_[extension->id()].property_bag;
2146}
[email protected]cebc3dc2011-04-18 17:15:002147
2148void ExtensionService::RegisterNaClModule(const GURL& url,
2149 const std::string& mime_type) {
2150 NaClModuleInfo info;
2151 info.url = url;
2152 info.mime_type = mime_type;
2153
2154 DCHECK(FindNaClModule(url) == nacl_module_list_.end());
2155 nacl_module_list_.push_front(info);
2156}
2157
2158void ExtensionService::UnregisterNaClModule(const GURL& url) {
2159 NaClModuleInfoList::iterator iter = FindNaClModule(url);
2160 DCHECK(iter != nacl_module_list_.end());
2161 nacl_module_list_.erase(iter);
2162}
2163
2164void ExtensionService::UpdatePluginListWithNaClModules() {
2165 FilePath path;
2166 PathService::Get(chrome::FILE_NACL_PLUGIN, &path);
2167
2168 webkit::npapi::PluginList::Singleton()->UnregisterInternalPlugin(path);
2169
2170 const PepperPluginInfo* pepper_info =
2171 PepperPluginRegistry::GetInstance()->GetInfoForPlugin(path);
2172 webkit::npapi::WebPluginInfo info = pepper_info->ToWebPluginInfo();
2173
2174 DCHECK(nacl_module_list_.size() <= 1);
2175 for (NaClModuleInfoList::iterator iter = nacl_module_list_.begin();
2176 iter != nacl_module_list_.end(); ++iter) {
2177 webkit::npapi::WebPluginMimeType mime_type_info;
2178 mime_type_info.mime_type = iter->mime_type;
2179 mime_type_info.additional_param_names.push_back(UTF8ToUTF16("nacl"));
2180 mime_type_info.additional_param_values.push_back(
2181 UTF8ToUTF16(iter->url.spec()));
2182 info.mime_types.push_back(mime_type_info);
2183 }
2184
2185 webkit::npapi::PluginList::Singleton()->RefreshPlugins();
2186 webkit::npapi::PluginList::Singleton()->RegisterInternalPlugin(info);
2187}
2188
2189ExtensionService::NaClModuleInfoList::iterator
2190 ExtensionService::FindNaClModule(const GURL& url) {
2191 for (NaClModuleInfoList::iterator iter = nacl_module_list_.begin();
2192 iter != nacl_module_list_.end(); ++iter) {
2193 if (iter->url == url)
2194 return iter;
2195 }
2196 return nacl_module_list_.end();
2197}