blob: c5fe8579246e777a0cdf87f1de8d5fc30771b4f0 [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]7286e3fc2011-07-19 22:13:2416#include "base/stl_util.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]18cc5a52011-04-15 16:03:3827#include "chrome/browser/extensions/apps_promo.h"
[email protected]e0785902011-05-19 23:34:1728#include "chrome/browser/extensions/crx_installer.h"
[email protected]5cbe1e22010-01-30 01:18:5629#include "chrome/browser/extensions/extension_accessibility_api.h"
[email protected]840b0db2009-11-20 03:00:3830#include "chrome/browser/extensions/extension_bookmarks_module.h"
[email protected]b68d5ed2009-04-16 02:41:2831#include "chrome/browser/extensions/extension_browser_event_router.h"
[email protected]2c5e1e12010-06-10 13:14:4432#include "chrome/browser/extensions/extension_cookies_api.h"
[email protected]c10da4b02010-03-25 14:38:3233#include "chrome/browser/extensions/extension_data_deleter.h"
[email protected]14a000d2010-04-29 21:44:2434#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]de768a832009-10-30 05:25:0135#include "chrome/browser/extensions/extension_history_api.h"
[email protected]b1748b1d82009-11-30 20:32:5636#include "chrome/browser/extensions/extension_host.h"
[email protected]14da67b02011-08-02 05:49:1337#include "chrome/browser/extensions/extension_input_ime_api.h"
[email protected]5fb889382011-06-03 00:29:2038#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]7596ce72010-08-30 05:10:4639#include "chrome/browser/extensions/extension_management_api.h"
[email protected]118de7892011-03-16 13:31:4040#include "chrome/browser/extensions/extension_preference_api.h"
[email protected]4814b512009-11-07 00:12:2941#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]8a661f82010-10-19 21:47:1142#include "chrome/browser/extensions/extension_processes_api.h"
[email protected]19eb80152011-02-26 00:28:4343#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]90310d92011-04-17 07:35:0444#include "chrome/browser/extensions/extension_sync_data.h"
[email protected]93fd78f42009-07-10 16:43:1745#include "chrome/browser/extensions/extension_updater.h"
[email protected]8f9d4eb2011-02-05 01:39:1046#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]784688a62010-09-13 07:06:5247#include "chrome/browser/extensions/extension_webnavigation_api.h"
[email protected]8e4560b62011-01-14 10:09:1448#include "chrome/browser/extensions/external_extension_provider_impl.h"
[email protected]42b795652011-02-22 23:32:3849#include "chrome/browser/extensions/external_extension_provider_interface.h"
[email protected]b2907fd2011-03-25 16:43:3750#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]56ad3792010-05-28 17:45:3351#include "chrome/browser/net/chrome_url_request_context.h"
[email protected]37858e52010-08-26 00:22:0252#include "chrome/browser/prefs/pref_service.h"
[email protected]8ecad5e2010-12-02 21:18:3353#include "chrome/browser/profiles/profile.h"
[email protected]8e5c89a2011-06-07 18:13:3354#include "chrome/browser/search_engines/template_url_service.h"
55#include "chrome/browser/search_engines/template_url_service_factory.h"
[email protected]3bdba0d2011-08-23 07:17:3056#include "chrome/browser/sync/api/sync_change.h"
[email protected]18280372011-03-22 18:05:2257#include "chrome/browser/themes/theme_service.h"
[email protected]a0ea76c2011-03-23 17:36:4258#include "chrome/browser/themes/theme_service_factory.h"
[email protected]c8d407e2011-04-28 21:27:1759#include "chrome/browser/ui/webui/chrome_url_data_manager.h"
60#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]7c5c6f3a2011-04-28 19:56:4961#include "chrome/browser/ui/webui/ntp/shown_sections_handler.h"
[email protected]aab98a52009-12-02 03:22:3562#include "chrome/common/child_process_logging.h"
[email protected]432115822011-07-10 15:52:2763#include "chrome/common/chrome_notification_types.h"
[email protected]cebc3dc2011-04-18 17:15:0064#include "chrome/common/chrome_paths.h"
[email protected]e2eb43112009-05-29 21:19:5465#include "chrome/common/chrome_switches.h"
[email protected]5b1a0e22009-05-26 19:00:5866#include "chrome/common/extensions/extension.h"
[email protected]d7b36dc2009-10-29 21:47:4067#include "chrome/common/extensions/extension_constants.h"
[email protected]05c82182010-06-24 17:49:0868#include "chrome/common/extensions/extension_error_utils.h"
[email protected]7c927b62010-02-24 09:54:1369#include "chrome/common/extensions/extension_file_util.h"
[email protected]c6d474f82009-12-16 21:11:0670#include "chrome/common/extensions/extension_l10n_util.h"
[email protected]77a6970c2011-04-23 16:58:5671#include "chrome/common/extensions/extension_messages.h"
[email protected]a315ba92010-11-16 14:12:2172#include "chrome/common/extensions/extension_resource.h"
[email protected]25b34332009-06-05 21:53:1973#include "chrome/common/pref_names.h"
[email protected]a57209872009-05-04 22:53:1474#include "chrome/common/url_constants.h"
[email protected]5f945a0e2011-03-01 17:47:5375#include "content/browser/browser_thread.h"
[email protected]b46442d7e2011-06-29 02:16:0676#include "content/browser/debugger/devtools_manager.h"
[email protected]2de307592011-04-05 21:16:5877#include "content/browser/plugin_process_host.h"
78#include "content/browser/plugin_service.h"
[email protected]77a6970c2011-04-23 16:58:5679#include "content/browser/renderer_host/render_process_host.h"
[email protected]432115822011-07-10 15:52:2780#include "content/common/content_notification_types.h"
[email protected]c3113022011-04-16 03:26:3081#include "content/common/json_value_serializer.h"
[email protected]b3841c502011-03-09 01:21:3182#include "content/common/notification_service.h"
[email protected]cebc3dc2011-04-18 17:15:0083#include "content/common/pepper_plugin_registry.h"
[email protected]c10da4b02010-03-25 14:38:3284#include "googleurl/src/gurl.h"
[email protected]1debbbb62010-10-06 17:23:4485#include "net/base/registry_controlled_domain.h"
[email protected]24b538a2010-02-27 01:22:4486#include "webkit/database/database_tracker.h"
87#include "webkit/database/database_util.h"
[email protected]2de307592011-04-05 21:16:5888#include "webkit/plugins/npapi/plugin_list.h"
[email protected]c64631652009-04-29 22:24:3189
[email protected]eed367e2011-04-12 03:43:3190#if defined(OS_CHROMEOS)
[email protected]14da67b02011-08-02 05:49:1391#include "chrome/browser/chromeos/cros/cros_library.h"
[email protected]eed367e2011-04-12 03:43:3192#include "chrome/browser/chromeos/extensions/file_browser_event_router.h"
[email protected]be5bd5742011-07-14 07:21:0193#include "chrome/browser/chromeos/extensions/input_method_event_router.h"
[email protected]ce9802042011-05-27 10:11:4094#include "chrome/browser/chromeos/extensions/media_player_event_router.h"
[email protected]14da67b02011-08-02 05:49:1395#include "chrome/browser/chromeos/input_method/input_method_manager.h"
96#include "chrome/browser/extensions/extension_input_ime_api.h"
[email protected]b777b332011-04-16 04:01:0897#include "webkit/fileapi/file_system_context.h"
98#include "webkit/fileapi/file_system_mount_point_provider.h"
99#include "webkit/fileapi/file_system_path_manager.h"
[email protected]eed367e2011-04-12 03:43:31100#endif
101
[email protected]27072cad2011-05-09 19:46:40102#if defined(OS_CHROMEOS) && defined(TOUCH_UI)
[email protected]27072cad2011-05-09 19:46:40103#include "chrome/browser/extensions/extension_input_ui_api.h"
104#endif
105
[email protected]5ef47ec2010-01-28 05:58:05106using base::Time;
107
[email protected]c6d474f82009-12-16 21:11:06108namespace errors = extension_manifest_errors;
109
[email protected]b6ab96d2009-08-20 18:58:19110namespace {
111
[email protected]29d0d4ac2010-09-08 21:10:31112#if defined(OS_LINUX)
113static const int kOmniboxIconPaddingLeft = 2;
114static const int kOmniboxIconPaddingRight = 2;
115#elif defined(OS_MACOSX)
116static const int kOmniboxIconPaddingLeft = 0;
117static const int kOmniboxIconPaddingRight = 2;
118#else
119static const int kOmniboxIconPaddingLeft = 0;
120static const int kOmniboxIconPaddingRight = 0;
121#endif
122
[email protected]300c0ea2011-07-15 23:04:33123const char* kNaClPluginMimeType = "application/x-nacl";
124
[email protected]a315ba92010-11-16 14:12:21125// The following enumeration is used in histograms matching
126// Extensions.ManifestReload* . Values may be added, as long
127// as existing values are not changed.
128enum ManifestReloadReason {
129 NOT_NEEDED = 0, // Reload not needed.
130 UNPACKED_DIR, // Unpacked directory
131 NEEDS_RELOCALIZATION, // The local has changed since we read this extension.
132 NUM_MANIFEST_RELOAD_REASONS
133};
[email protected]2111b1a2010-03-12 18:12:44134
[email protected]a315ba92010-11-16 14:12:21135ManifestReloadReason ShouldReloadExtensionManifest(const ExtensionInfo& info) {
136 // Always reload manifests of unpacked extensions, because they can change
137 // on disk independent of the manifest in our prefs.
138 if (info.extension_location == Extension::LOAD)
139 return UNPACKED_DIR;
140
141 // Reload the manifest if it needs to be relocalized.
142 if (extension_l10n_util::ShouldRelocalizeManifest(info))
143 return NEEDS_RELOCALIZATION;
144
145 return NOT_NEEDED;
[email protected]2111b1a2010-03-12 18:12:44146}
147
[email protected]2de307592011-04-05 21:16:58148static void ForceShutdownPlugin(const FilePath& plugin_path) {
149 PluginProcessHost* plugin =
150 PluginService::GetInstance()->FindNpapiPluginProcess(plugin_path);
151 if (plugin)
152 plugin->ForceShutdown();
153}
154
[email protected]3bdba0d2011-08-23 07:17:30155static bool IsSyncableExtension(const Extension& extension) {
156 return extension.GetSyncType() == Extension::SYNC_TYPE_EXTENSION;
157}
158
159static bool IsSyncableApp(const Extension& extension) {
160 return extension.GetSyncType() == Extension::SYNC_TYPE_APP;
161}
162
[email protected]5fb889382011-06-03 00:29:20163// Manages an ExtensionInstallUI for a particular extension.
164class SimpleExtensionLoadPrompt : public ExtensionInstallUI::Delegate {
165 public:
166 SimpleExtensionLoadPrompt(Profile* profile,
167 base::WeakPtr<ExtensionService> extension_service,
168 const Extension* extension);
169 ~SimpleExtensionLoadPrompt();
170
171 void ShowPrompt();
172
173 // ExtensionInstallUI::Delegate
174 virtual void InstallUIProceed();
[email protected]d828fac2011-06-28 05:43:04175 virtual void InstallUIAbort(bool user_initiated);
[email protected]5fb889382011-06-03 00:29:20176
177 private:
178 base::WeakPtr<ExtensionService> extension_service_;
179 scoped_ptr<ExtensionInstallUI> install_ui_;
180 scoped_refptr<const Extension> extension_;
181};
182
183SimpleExtensionLoadPrompt::SimpleExtensionLoadPrompt(
184 Profile* profile,
185 base::WeakPtr<ExtensionService> extension_service,
186 const Extension* extension)
187 : extension_service_(extension_service),
188 install_ui_(new ExtensionInstallUI(profile)),
189 extension_(extension) {
190}
191
192SimpleExtensionLoadPrompt::~SimpleExtensionLoadPrompt() {
193}
194
195void SimpleExtensionLoadPrompt::ShowPrompt() {
196 install_ui_->ConfirmInstall(this, extension_);
197}
198
199void SimpleExtensionLoadPrompt::InstallUIProceed() {
200 if (extension_service_.get())
[email protected]8266d662011-07-12 21:53:26201 extension_service_->OnExtensionInstalled(
[email protected]f1b66652011-08-25 23:30:58202 extension_, false, -1); // Not from web store.
[email protected]5fb889382011-06-03 00:29:20203 delete this;
204}
205
[email protected]d828fac2011-06-28 05:43:04206void SimpleExtensionLoadPrompt::InstallUIAbort(bool user_initiated) {
[email protected]5fb889382011-06-03 00:29:20207 delete this;
208}
209
[email protected]c6d474f82009-12-16 21:11:06210} // namespace
[email protected]b6ab96d2009-08-20 18:58:19211
[email protected]ad63a792011-06-16 06:41:11212bool ExtensionService::ComponentExtensionInfo::Equals(
213 const ComponentExtensionInfo& other) const {
214 return other.manifest == manifest && other.root_directory == root_directory;
215}
216
[email protected]eaa7dd182010-12-14 11:09:00217ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49218 : background_page_ready(false),
219 being_upgraded(false) {
220}
221
[email protected]eaa7dd182010-12-14 11:09:00222ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49223}
224
[email protected]cebc3dc2011-04-18 17:15:00225ExtensionService::NaClModuleInfo::NaClModuleInfo() {
226}
227
228ExtensionService::NaClModuleInfo::~NaClModuleInfo() {
229}
230
[email protected]eaa7dd182010-12-14 11:09:00231// ExtensionService.
[email protected]6014d672008-12-05 00:38:25232
[email protected]eaa7dd182010-12-14 11:09:00233const char* ExtensionService::kInstallDirectoryName = "Extensions";
234const char* ExtensionService::kCurrentVersionFileName = "Current Version";
[email protected]b7f853e282011-08-10 09:24:20235const char* ExtensionService::kSettingsDirectoryName = "Extension Settings";
[email protected]494c06e2009-07-25 01:06:42236
[email protected]eaa7dd182010-12-14 11:09:00237// Implements IO for the ExtensionService.
[email protected]7a4c6852010-09-16 03:44:22238
[email protected]eaa7dd182010-12-14 11:09:00239class ExtensionServiceBackend
[email protected]8e4560b62011-01-14 10:09:14240 : public base::RefCountedThreadSafe<ExtensionServiceBackend> {
[email protected]7a4c6852010-09-16 03:44:22241 public:
[email protected]1f830eb2010-09-28 08:25:14242 // |install_directory| is a path where to look for extensions to load.
[email protected]14908b72011-04-20 06:54:36243 ExtensionServiceBackend(
244 base::WeakPtr<ExtensionService> frontend,
245 const FilePath& install_directory);
[email protected]7a4c6852010-09-16 03:44:22246
247 // Loads a single extension from |path| where |path| is the top directory of
[email protected]e49002a2011-07-13 03:40:44248 // a specific extension where its manifest file lives. If |prompt_for_plugins|
249 // is true and the extension contains plugins, we prompt the user before
250 // loading.
[email protected]7a4c6852010-09-16 03:44:22251 // Errors are reported through ExtensionErrorReporter. On success,
[email protected]49cd1572011-02-08 21:38:45252 // AddExtension() is called.
[email protected]7a4c6852010-09-16 03:44:22253 // TODO(erikkay): It might be useful to be able to load a packed extension
254 // (presumably into memory) without installing it.
[email protected]e49002a2011-07-13 03:40:44255 void LoadSingleExtension(const FilePath &path, bool prompt_for_plugins);
[email protected]7a4c6852010-09-16 03:44:22256
[email protected]7a4c6852010-09-16 03:44:22257 private:
[email protected]eaa7dd182010-12-14 11:09:00258 friend class base::RefCountedThreadSafe<ExtensionServiceBackend>;
[email protected]7a4c6852010-09-16 03:44:22259
[email protected]eaa7dd182010-12-14 11:09:00260 virtual ~ExtensionServiceBackend();
[email protected]7a4c6852010-09-16 03:44:22261
[email protected]d10f4602011-06-16 15:44:50262 // LoadSingleExtension needs to check the file access preference, which needs
263 // to happen back on the UI thread, so it posts CheckExtensionFileAccess on
264 // the UI thread. In turn, once that gets the pref, it goes back to the
265 // file thread with LoadSingleExtensionWithFileAccess.
[email protected]e49002a2011-07-13 03:40:44266 void CheckExtensionFileAccess(const FilePath& extension_path,
267 bool prompt_for_plugins);
[email protected]d10f4602011-06-16 15:44:50268 void LoadSingleExtensionWithFileAccess(
[email protected]e49002a2011-07-13 03:40:44269 const FilePath &path, bool allow_file_access, bool prompt_for_plugins);
[email protected]d10f4602011-06-16 15:44:50270
[email protected]7a4c6852010-09-16 03:44:22271 // Notify the frontend that there was an error loading an extension.
272 void ReportExtensionLoadError(const FilePath& extension_path,
273 const std::string& error);
274
[email protected]14908b72011-04-20 06:54:36275 // Notify the frontend that an extension was installed.
[email protected]e49002a2011-07-13 03:40:44276 void OnLoadSingleExtension(const scoped_refptr<const Extension>& extension,
277 bool prompt_for_plugins);
[email protected]14908b72011-04-20 06:54:36278
279 base::WeakPtr<ExtensionService> frontend_;
[email protected]7a4c6852010-09-16 03:44:22280
281 // The top-level extensions directory being installed to.
282 FilePath install_directory_;
283
[email protected]eaa7dd182010-12-14 11:09:00284 DISALLOW_COPY_AND_ASSIGN(ExtensionServiceBackend);
[email protected]7a4c6852010-09-16 03:44:22285};
286
[email protected]eaa7dd182010-12-14 11:09:00287ExtensionServiceBackend::ExtensionServiceBackend(
[email protected]14908b72011-04-20 06:54:36288 base::WeakPtr<ExtensionService> frontend,
[email protected]aebe23a32010-12-10 22:15:48289 const FilePath& install_directory)
[email protected]14908b72011-04-20 06:54:36290 : frontend_(frontend),
291 install_directory_(install_directory) {
292 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]7a4c6852010-09-16 03:44:22293}
294
[email protected]eaa7dd182010-12-14 11:09:00295ExtensionServiceBackend::~ExtensionServiceBackend() {
[email protected]14908b72011-04-20 06:54:36296 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
297 BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]7a4c6852010-09-16 03:44:22298}
299
[email protected]e49002a2011-07-13 03:40:44300void ExtensionServiceBackend::LoadSingleExtension(const FilePath& path_in,
301 bool prompt_for_plugins) {
[email protected]a29a517a2011-01-21 21:11:12302 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]a8af9fdb2010-10-28 21:52:20303
[email protected]7a4c6852010-09-16 03:44:22304 FilePath extension_path = path_in;
305 file_util::AbsolutePath(&extension_path);
306
[email protected]e49002a2011-07-13 03:40:44307 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
308 NewRunnableMethod(this,
309 &ExtensionServiceBackend::CheckExtensionFileAccess,
310 extension_path, prompt_for_plugins));
[email protected]d10f4602011-06-16 15:44:50311}
312
313void ExtensionServiceBackend::CheckExtensionFileAccess(
[email protected]e49002a2011-07-13 03:40:44314 const FilePath& extension_path, bool prompt_for_plugins) {
[email protected]d10f4602011-06-16 15:44:50315 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
316 std::string id = Extension::GenerateIdForPath(extension_path);
[email protected]cdfca9702011-08-08 16:07:01317 // Unpacked extensions default to allowing file access, but if that has been
318 // overridden, don't reset the value.
319 bool allow_file_access =
320 Extension::ShouldAlwaysAllowFileAccess(Extension::LOAD);
321 if (frontend_->extension_prefs()->HasAllowFileAccessSetting(id))
322 allow_file_access = frontend_->extension_prefs()->AllowFileAccess(id);
[email protected]d10f4602011-06-16 15:44:50323
[email protected]e49002a2011-07-13 03:40:44324 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
325 NewRunnableMethod(
326 this,
327 &ExtensionServiceBackend::LoadSingleExtensionWithFileAccess,
328 extension_path, allow_file_access, prompt_for_plugins));
[email protected]d10f4602011-06-16 15:44:50329}
330
[email protected]d10f4602011-06-16 15:44:50331void ExtensionServiceBackend::LoadSingleExtensionWithFileAccess(
[email protected]e49002a2011-07-13 03:40:44332 const FilePath& extension_path,
333 bool allow_file_access,
334 bool prompt_for_plugins) {
[email protected]d10f4602011-06-16 15:44:50335 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
336 int flags = allow_file_access ?
[email protected]3aff9ad2011-04-01 20:26:48337 Extension::ALLOW_FILE_ACCESS : Extension::NO_FLAGS;
[email protected]83048a22011-03-29 00:14:13338 if (Extension::ShouldDoStrictErrorChecking(Extension::LOAD))
339 flags |= Extension::STRICT_ERROR_CHECKS;
[email protected]7a4c6852010-09-16 03:44:22340 std::string error;
[email protected]ad8e04a2010-11-01 04:16:27341 scoped_refptr<const Extension> extension(extension_file_util::LoadExtension(
[email protected]7a4c6852010-09-16 03:44:22342 extension_path,
[email protected]92888082010-10-18 19:24:57343 Extension::LOAD,
[email protected]83048a22011-03-29 00:14:13344 flags,
[email protected]ad8e04a2010-11-01 04:16:27345 &error));
[email protected]7a4c6852010-09-16 03:44:22346
347 if (!extension) {
[email protected]e49002a2011-07-13 03:40:44348 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
349 NewRunnableMethod(
350 this,
351 &ExtensionServiceBackend::ReportExtensionLoadError,
352 extension_path, error));
[email protected]7a4c6852010-09-16 03:44:22353 return;
354 }
355
[email protected]7a4c6852010-09-16 03:44:22356 // Report this as an installed extension so that it gets remembered in the
357 // prefs.
[email protected]e49002a2011-07-13 03:40:44358 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
359 NewRunnableMethod(
360 this,
361 &ExtensionServiceBackend::OnLoadSingleExtension,
362 extension, prompt_for_plugins));
[email protected]7a4c6852010-09-16 03:44:22363}
364
[email protected]eaa7dd182010-12-14 11:09:00365void ExtensionServiceBackend::ReportExtensionLoadError(
[email protected]7a4c6852010-09-16 03:44:22366 const FilePath& extension_path, const std::string &error) {
[email protected]14908b72011-04-20 06:54:36367 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
368 if (frontend_.get())
369 frontend_->ReportExtensionLoadError(
[email protected]432115822011-07-10 15:52:27370 extension_path, error, chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR,
[email protected]14908b72011-04-20 06:54:36371 true /* alert_on_error */);
372}
373
[email protected]5fb889382011-06-03 00:29:20374void ExtensionServiceBackend::OnLoadSingleExtension(
[email protected]e49002a2011-07-13 03:40:44375 const scoped_refptr<const Extension>& extension, bool prompt_for_plugins) {
[email protected]14908b72011-04-20 06:54:36376 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
377 if (frontend_.get())
[email protected]e49002a2011-07-13 03:40:44378 frontend_->OnLoadSingleExtension(extension, prompt_for_plugins);
[email protected]7a4c6852010-09-16 03:44:22379}
380
[email protected]8e4560b62011-01-14 10:09:14381void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12382 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20383
[email protected]7a4c6852010-09-16 03:44:22384 // Check if the providers know about this extension.
[email protected]0a60a2e2010-10-25 16:15:21385 ProviderCollection::const_iterator i;
386 for (i = external_extension_providers_.begin();
387 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14388 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21389 if (i->get()->HasExtension(id))
390 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22391 }
392
[email protected]0f48fca2011-05-19 18:46:35393 // We get the list of external extensions to check from preferences.
394 // It is possible that an extension has preferences but is not loaded.
395 // For example, an extension that requires experimental permissions
396 // will not be loaded if the experimental command line flag is not used.
397 // In this case, do not uninstall.
398 const Extension* extension = GetInstalledExtension(id);
399 if (!extension) {
400 // We can't call UninstallExtension with an unloaded/invalid
401 // extension ID.
402 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
403 << "with id: " << id;
404 return;
405 }
[email protected]d6ebc9792011-04-07 18:18:33406 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22407}
408
[email protected]8e4560b62011-01-14 10:09:14409void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22410 external_extension_providers_.clear();
411}
412
[email protected]8e4560b62011-01-14 10:09:14413void ExtensionService::AddProviderForTesting(
414 ExternalExtensionProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12415 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21416 external_extension_providers_.push_back(
[email protected]8e4560b62011-01-14 10:09:14417 linked_ptr<ExternalExtensionProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22418}
419
[email protected]8e4560b62011-01-14 10:09:14420void ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22421 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42422 const GURL& update_url,
423 Extension::Location location) {
[email protected]ab22ba42011-01-14 16:36:38424 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
425 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20426
[email protected]8a87a5332011-08-11 17:54:59427 const Extension* extension = GetExtensionById(id, true);
428 if (extension) {
429 // Already installed. Skip this install if the current location has
430 // higher priority than |location|.
431 Extension::Location current = extension->location();
432 if (current == Extension::GetHigherPriorityLocation(current, location))
433 return;
434 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22435 }
[email protected]b2907fd2011-03-25 16:43:37436 pending_extension_manager()->AddFromExternalUpdateUrl(
437 id, update_url, location);
[email protected]a29a517a2011-01-21 21:11:12438 external_extension_url_added_ |= true;
[email protected]7a4c6852010-09-16 03:44:22439}
440
[email protected]454434f2011-08-24 04:00:00441// If a download url matches one of these patterns and has a referrer of the
442// webstore, then we're willing to treat that as a gallery download.
443static const char* kAllowedDownloadURLPatterns[] = {
444 "https://clients2.google.com/service/update2*",
445 "https://clients2.googleusercontent.com/crx/*"
446};
447
[email protected]eaa7dd182010-12-14 11:09:00448bool ExtensionService::IsDownloadFromGallery(const GURL& download_url,
[email protected]a29a517a2011-01-21 21:11:12449 const GURL& referrer_url) {
[email protected]d3071992010-10-08 15:24:07450 // Special-case the themes mini-gallery.
451 // TODO(erikkay) When that gallery goes away, remove this code.
452 if (IsDownloadFromMiniGallery(download_url) &&
453 StartsWithASCII(referrer_url.spec(),
454 extension_urls::kMiniGalleryBrowsePrefix, false)) {
[email protected]334e04a2010-06-24 23:34:44455 return true;
[email protected]1debbbb62010-10-06 17:23:44456 }
[email protected]473ff6e2010-05-12 15:31:55457
[email protected]9adb9692010-10-29 23:14:02458 const Extension* download_extension = GetExtensionByWebExtent(download_url);
459 const Extension* referrer_extension = GetExtensionByWebExtent(referrer_url);
460 const Extension* webstore_app = GetWebStoreApp();
[email protected]d3071992010-10-08 15:24:07461
462 bool referrer_valid = (referrer_extension == webstore_app);
463 bool download_valid = (download_extension == webstore_app);
464
[email protected]454434f2011-08-24 04:00:00465 // We also allow the download to be from a small set of trusted paths.
466 if (!download_valid) {
467 for (size_t i = 0; i < arraysize(kAllowedDownloadURLPatterns); i++) {
468 URLPattern pattern(URLPattern::SCHEME_HTTPS,
469 kAllowedDownloadURLPatterns[i]);
470 if (pattern.MatchesURL(download_url)) {
471 download_valid = true;
472 break;
473 }
474 }
475 }
476
[email protected]d3071992010-10-08 15:24:07477 // If the command-line gallery URL is set, then be a bit more lenient.
478 GURL store_url =
479 GURL(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
480 switches::kAppsGalleryURL));
481 if (!store_url.is_empty()) {
482 std::string store_tld =
483 net::RegistryControlledDomainService::GetDomainAndRegistry(store_url);
484 if (!referrer_valid) {
485 std::string referrer_tld =
486 net::RegistryControlledDomainService::GetDomainAndRegistry(
487 referrer_url);
488 // The referrer gets stripped when transitioning from https to http,
489 // or when hitting an unknown test cert and that commonly happens in
490 // testing environments. Given this, we allow an empty referrer when
491 // the command-line flag is set.
492 // Otherwise, the TLD must match the TLD of the command-line url.
493 referrer_valid = referrer_url.is_empty() || (referrer_tld == store_tld);
494 }
495
496 if (!download_valid) {
497 std::string download_tld =
498 net::RegistryControlledDomainService::GetDomainAndRegistry(
[email protected]04866c42011-05-03 20:03:50499 download_url);
[email protected]d3071992010-10-08 15:24:07500
501 // Otherwise, the TLD must match the TLD of the command-line url.
502 download_valid = (download_tld == store_tld);
503 }
504 }
505
506 return (referrer_valid && download_valid);
[email protected]b7c2f252009-12-08 00:47:23507}
508
[email protected]eaa7dd182010-12-14 11:09:00509bool ExtensionService::IsDownloadFromMiniGallery(const GURL& download_url) {
[email protected]ac025282009-12-16 19:16:38510 return StartsWithASCII(download_url.spec(),
511 extension_urls::kMiniGalleryDownloadPrefix,
512 false); // case_sensitive
513}
514
[email protected]d9696672011-03-15 22:45:09515const Extension* ExtensionService::GetInstalledApp(const GURL& url) {
[email protected]ffb204f22010-12-05 23:20:27516 // Check for hosted app.
[email protected]d9696672011-03-15 22:45:09517 const Extension* app = GetExtensionByWebExtent(url);
518 if (app)
519 return app;
[email protected]ffb204f22010-12-05 23:20:27520
521 // Check for packaged app.
[email protected]d9696672011-03-15 22:45:09522 app = GetExtensionByURL(url);
523 if (app && app->is_app())
524 return app;
525
526 return NULL;
527}
528
529bool ExtensionService::IsInstalledApp(const GURL& url) {
530 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27531}
532
[email protected]da5683db2011-04-23 17:12:21533void ExtensionService::SetInstalledAppForRenderer(int renderer_child_id,
534 const Extension* app) {
535 installed_app_hosts_[renderer_child_id] = app;
536}
537
538const Extension* ExtensionService::GetInstalledAppForRenderer(
539 int renderer_child_id) {
540 InstalledAppMap::iterator i = installed_app_hosts_.find(renderer_child_id);
541 if (i == installed_app_hosts_.end())
542 return NULL;
543 return i->second;
544}
545
[email protected]6aeac8342010-10-01 20:21:18546// static
[email protected]d6ebc9792011-04-07 18:18:33547// This function is used to implement the command-line switch
[email protected]3bdba0d2011-08-23 07:17:30548// --uninstall-extension, and to uninstall an extension via sync. The LOG
549// statements within this function are used to inform the user if the uninstall
550// cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00551bool ExtensionService::UninstallExtensionHelper(
552 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18553 const std::string& extension_id) {
[email protected]6aeac8342010-10-01 20:21:18554
[email protected]95da88c42011-03-31 10:07:33555 const Extension* extension =
[email protected]8001df22011-04-28 19:59:47556 extensions_service->GetInstalledExtension(extension_id);
[email protected]95da88c42011-03-31 10:07:33557
[email protected]d6ebc9792011-04-07 18:18:33558 // We can't call UninstallExtension with an invalid extension ID.
[email protected]95da88c42011-03-31 10:07:33559 if (!extension) {
[email protected]6aeac8342010-10-01 20:21:18560 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33561 << "id: " << extension_id;
562 return false;
[email protected]6aeac8342010-10-01 20:21:18563 }
564
[email protected]d6ebc9792011-04-07 18:18:33565 // The following call to UninstallExtension will not allow an uninstall of a
566 // policy-controlled extension.
567 std::string error;
568 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
569 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
570 << ": " << error;
571 return false;
572 }
[email protected]95da88c42011-03-31 10:07:33573
[email protected]6aeac8342010-10-01 20:21:18574 return true;
575}
576
[email protected]eaa7dd182010-12-14 11:09:00577ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24578 const CommandLine* command_line,
579 const FilePath& install_directory,
580 ExtensionPrefs* extension_prefs,
[email protected]b7f853e282011-08-10 09:24:20581 ExtensionSettings* extension_settings,
[email protected]0436b102011-04-15 18:30:03582 bool autoupdate_enabled,
583 bool extensions_enabled)
[email protected]14908b72011-04-20 06:54:36584 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
585 method_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
586 profile_(profile),
[email protected]73c47932010-12-06 18:13:43587 extension_prefs_(extension_prefs),
[email protected]b7f853e282011-08-10 09:24:20588 extension_settings_(extension_settings),
[email protected]14908b72011-04-20 06:54:36589 pending_extension_manager_(*ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]a9b00ac2009-06-25 21:03:23590 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03591 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13592 show_extensions_prompts_(true),
[email protected]3ecda252010-11-18 19:50:55593 ready_(false),
[email protected]14908b72011-04-20 06:54:36594 toolbar_model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]902fd7b2011-07-27 18:42:31595 permissions_manager_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]18cc5a52011-04-15 16:03:38596 apps_promo_(profile->GetPrefs()),
[email protected]b3d62312b12010-10-14 21:10:18597 event_routers_initialized_(false) {
[email protected]a29a517a2011-01-21 21:11:12598 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20599
[email protected]36a784c2009-06-23 06:21:08600 // Figure out if extension installation should be enabled.
[email protected]6d60703b2009-08-29 01:29:23601 if (command_line->HasSwitch(switches::kDisableExtensions)) {
602 extensions_enabled_ = false;
603 } else if (profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
604 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18605 }
[email protected]36a784c2009-06-23 06:21:08606
[email protected]432115822011-07-10 15:52:27607 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]bc535ee52010-08-31 18:40:32608 NotificationService::AllSources());
[email protected]432115822011-07-10 15:52:27609 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
[email protected]77a6970c2011-04-23 16:58:56610 NotificationService::AllSources());
[email protected]432115822011-07-10 15:52:27611 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]da5683db2011-04-23 17:12:21612 NotificationService::AllSources());
[email protected]2fb7dc982010-09-29 12:24:28613 pref_change_registrar_.Init(profile->GetPrefs());
614 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, this);
615 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, this);
[email protected]4814b512009-11-07 00:12:29616
[email protected]93fd78f42009-07-10 16:43:17617 // Set up the ExtensionUpdater
618 if (autoupdate_enabled) {
619 int update_frequency = kDefaultUpdateFrequencySeconds;
620 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25621 base::StringToInt(command_line->GetSwitchValueASCII(
622 switches::kExtensionsUpdateFrequency),
623 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17624 }
[email protected]82b6e512011-04-12 20:33:27625 updater_.reset(new ExtensionUpdater(this,
626 extension_prefs,
627 profile->GetPrefs(),
628 profile,
629 update_frequency));
[email protected]93fd78f42009-07-10 16:43:17630 }
631
[email protected]14908b72011-04-20 06:54:36632 backend_ =
633 new ExtensionServiceBackend(weak_ptr_factory_.GetWeakPtr(),
634 install_directory_);
[email protected]8e4560b62011-01-14 10:09:14635
[email protected]0436b102011-04-15 18:30:03636 if (extensions_enabled_) {
[email protected]873531342011-03-09 12:16:05637 ExternalExtensionProviderImpl::CreateExternalProviders(
638 this, profile_, &external_extension_providers_);
639 }
[email protected]b671760b2010-07-15 21:13:47640
[email protected]aa96d3a2010-08-21 08:45:25641 // Use monochrome icons for Omnibox icons.
[email protected]29d0d4ac2010-09-08 21:10:31642 omnibox_popup_icon_manager_.set_monochrome(true);
[email protected]b671760b2010-07-15 21:13:47643 omnibox_icon_manager_.set_monochrome(true);
[email protected]29d0d4ac2010-09-08 21:10:31644 omnibox_icon_manager_.set_padding(gfx::Insets(0, kOmniboxIconPaddingLeft,
645 0, kOmniboxIconPaddingRight));
[email protected]cb0e50312011-05-09 15:03:07646
647 // How long is the path to the Extensions directory?
648 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
649 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25650}
651
[email protected]eaa7dd182010-12-14 11:09:00652const ExtensionList* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45653 return &extensions_;
654}
655
[email protected]eaa7dd182010-12-14 11:09:00656const ExtensionList* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45657 return &disabled_extensions_;
658}
659
[email protected]bb7f40952011-01-13 00:21:20660const ExtensionList* ExtensionService::terminated_extensions() const {
661 return &terminated_extensions_;
662}
663
[email protected]b2907fd2011-03-25 16:43:37664PendingExtensionManager* ExtensionService::pending_extension_manager() {
665 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45666}
667
[email protected]ad63a792011-06-16 06:41:11668void ExtensionService::UnregisterComponentExtension(
669 const ComponentExtensionInfo& info) {
670 RegisteredComponentExtensions new_component_extension_manifests;
671 for (RegisteredComponentExtensions::iterator it =
672 component_extension_manifests_.begin();
673 it != component_extension_manifests_.end(); ++it) {
674 if (!it->Equals(info))
675 new_component_extension_manifests.push_back(*it);
676 }
677 component_extension_manifests_.swap(new_component_extension_manifests);
678}
679
[email protected]eaa7dd182010-12-14 11:09:00680ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17681 // No need to unload extensions here because they are profile-scoped, and the
682 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17683
[email protected]8e4560b62011-01-14 10:09:14684 ProviderCollection::const_iterator i;
685 for (i = external_extension_providers_.begin();
686 i != external_extension_providers_.end(); ++i) {
687 ExternalExtensionProviderInterface* provider = i->get();
688 provider->ServiceShutdown();
689 }
[email protected]6014d672008-12-05 00:38:25690}
691
[email protected]d91268022011-08-26 13:17:37692void ExtensionService::InitEventRoutersAfterImport() {
693 registrar_.Add(this, chrome::NOTIFICATION_IMPORT_FINISHED,
694 Source<Profile>(profile_));
695}
696
[email protected]eaa7dd182010-12-14 11:09:00697void ExtensionService::InitEventRouters() {
[email protected]b3d62312b12010-10-14 21:10:18698 if (event_routers_initialized_)
699 return;
700
[email protected]f825df22011-06-28 17:36:10701 history_event_router_.reset(new ExtensionHistoryEventRouter());
702 history_event_router_->ObserveProfile(profile_);
[email protected]7da9a8232011-05-27 21:07:11703 ExtensionAccessibilityEventRouter::GetInstance()->ObserveProfile(profile_);
[email protected]97d2f1d2011-01-15 00:41:08704 browser_event_router_.reset(new ExtensionBrowserEventRouter(profile_));
705 browser_event_router_->Init();
[email protected]118de7892011-03-16 13:31:40706 preference_event_router_.reset(new ExtensionPreferenceEventRouter(profile_));
[email protected]f825df22011-06-28 17:36:10707 bookmark_event_router_.reset(new ExtensionBookmarkEventRouter(
708 profile_->GetBookmarkModel()));
709 bookmark_event_router_->Init();
710 cookies_event_router_.reset(new ExtensionCookiesEventRouter(profile_));
711 cookies_event_router_->Init();
712 management_event_router_.reset(new ExtensionManagementEventRouter(profile_));
713 management_event_router_->Init();
[email protected]7da9a8232011-05-27 21:07:11714 ExtensionProcessesEventRouter::GetInstance()->ObserveProfile(profile_);
[email protected]f825df22011-06-28 17:36:10715 web_navigation_event_router_.reset(
716 new ExtensionWebNavigationEventRouter(profile_));
717 web_navigation_event_router_->Init();
[email protected]27072cad2011-05-09 19:46:40718
[email protected]eed367e2011-04-12 03:43:31719#if defined(OS_CHROMEOS)
[email protected]b16fc2e2011-07-21 21:32:58720 file_browser_event_router_.reset(
721 new ExtensionFileBrowserEventRouter(profile_));
722 file_browser_event_router_->ObserveFileSystemEvents();
[email protected]be5bd5742011-07-14 07:21:01723 // Lazy initialization.
724 chromeos::ExtensionInputMethodEventRouter::GetInstance();
725
[email protected]c8bf09be2011-06-25 03:22:27726 ExtensionMediaPlayerEventRouter::GetInstance()->Init(profile_);
[email protected]14da67b02011-08-02 05:49:13727 ExtensionInputImeEventRouter::GetInstance()->Init();
[email protected]eed367e2011-04-12 03:43:31728#endif
[email protected]27072cad2011-05-09 19:46:40729
730#if defined(OS_CHROMEOS) && defined(TOUCH_UI)
731 ExtensionInputUiEventRouter::GetInstance()->Init();
732#endif
733
[email protected]b3d62312b12010-10-14 21:10:18734 event_routers_initialized_ = true;
[email protected]c5ae74ab2010-04-15 18:14:37735}
736
[email protected]b2907fd2011-03-25 16:43:37737const Extension* ExtensionService::GetExtensionById(
738 const std::string& id, bool include_disabled) const {
[email protected]8001df22011-04-28 19:59:47739 return GetExtensionByIdInternal(id, true, include_disabled, false);
[email protected]78994ab02010-12-08 18:06:44740}
741
[email protected]eaa7dd182010-12-14 11:09:00742void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12743 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20744
[email protected]fa6a9102010-11-22 15:38:50745 DCHECK(!ready_); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17746 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32747
[email protected]95dd38f2009-10-20 20:09:15748 // Hack: we need to ensure the ResourceDispatcherHost is ready before we load
749 // the first extension, because its members listen for loaded notifications.
750 g_browser_process->resource_dispatcher_host();
751
[email protected]9f1087e2009-06-15 17:29:32752 LoadAllExtensions();
[email protected]894bb502009-05-21 22:39:57753
[email protected]9f1087e2009-06-15 17:29:32754 // TODO(erikkay) this should probably be deferred to a future point
755 // rather than running immediately at startup.
[email protected]93fd78f42009-07-10 16:43:17756 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57757
[email protected]9f1087e2009-06-15 17:29:32758 // TODO(erikkay) this should probably be deferred as well.
759 GarbageCollectExtensions();
[email protected]6014d672008-12-05 00:38:25760}
761
[email protected]420a0ec2011-06-01 01:07:03762bool ExtensionService::UpdateExtension(
763 const std::string& id,
764 const FilePath& extension_path,
765 const GURL& download_url,
766 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12767 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20768
[email protected]b2907fd2011-03-25 16:43:37769 PendingExtensionInfo pending_extension_info;
770 bool is_pending_extension = pending_extension_manager_.GetById(
771 id, &pending_extension_info);
[email protected]8ef78fd2010-08-19 17:14:32772
[email protected]8001df22011-04-28 19:59:47773 const Extension* extension =
774 GetExtensionByIdInternal(id, true, true, false);
[email protected]5eb375e92010-11-26 07:50:41775 if (!is_pending_extension && !extension) {
[email protected]aa142702010-03-26 01:26:33776 LOG(WARNING) << "Will not update extension " << id
777 << " because it is not installed or pending";
778 // Delete extension_path since we're not creating a CrxInstaller
779 // that would do it for us.
[email protected]14908b72011-04-20 06:54:36780 if (!BrowserThread::PostTask(
781 BrowserThread::FILE, FROM_HERE,
782 NewRunnableFunction(
783 extension_file_util::DeleteFile, extension_path, false)))
784 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03785
786 return false;
[email protected]e957fe52009-06-23 16:51:05787 }
788
[email protected]aa142702010-03-26 01:26:33789 // We want a silent install only for non-pending extensions and
790 // pending extensions that have install_silently set.
791 ExtensionInstallUI* client =
[email protected]b2907fd2011-03-25 16:43:37792 (!is_pending_extension || pending_extension_info.install_silently()) ?
[email protected]aa142702010-03-26 01:26:33793 NULL : new ExtensionInstallUI(profile_);
794
[email protected]14908b72011-04-20 06:54:36795 scoped_refptr<CrxInstaller> installer(MakeCrxInstaller(client));
[email protected]6dfbbf82010-03-12 23:09:16796 installer->set_expected_id(id);
[email protected]ec5b50d2010-10-09 16:35:18797 if (is_pending_extension)
[email protected]b2907fd2011-03-25 16:43:37798 installer->set_install_source(pending_extension_info.install_source());
[email protected]5eb375e92010-11-26 07:50:41799 else if (extension)
800 installer->set_install_source(extension->location());
[email protected]0d3e4a22011-06-23 19:02:52801 if (pending_extension_info.install_silently())
802 installer->set_allow_silent_install(true);
[email protected]6dfbbf82010-03-12 23:09:16803 installer->set_delete_source(true);
[email protected]5c8516202010-03-18 21:43:34804 installer->set_original_url(download_url);
[email protected]cb0e50312011-05-09 15:03:07805 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16806 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03807
808 if (out_crx_installer)
809 *out_crx_installer = installer;
810
811 return true;
[email protected]e957fe52009-06-23 16:51:05812}
813
[email protected]eaa7dd182010-12-14 11:09:00814void ExtensionService::ReloadExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12815 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b65272f2009-08-31 15:47:06816 FilePath path;
[email protected]9adb9692010-10-29 23:14:02817 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40818
[email protected]f17dbd42010-08-16 23:21:10819 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06820 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29821 // If the extension has an inspector open for its background page, detach
822 // the inspector and hang onto a cookie for it, so that we can reattach
823 // later.
824 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
825 ExtensionHost* host = manager->GetBackgroundHostForExtension(
826 current_extension);
827 if (host) {
828 // Look for an open inspector for the background page.
829 int devtools_cookie = DevToolsManager::GetInstance()->DetachClientHost(
830 host->render_view_host());
831 if (devtools_cookie >= 0)
832 orphaned_dev_tools_[extension_id] = devtools_cookie;
833 }
834
[email protected]b65272f2009-08-31 15:47:06835 path = current_extension->path();
[email protected]f17dbd42010-08-16 23:21:10836 DisableExtension(extension_id);
837 disabled_extension_paths_[extension_id] = path;
[email protected]1eb175082010-02-10 09:26:16838 } else {
839 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06840 }
841
[email protected]e6090e42010-03-23 22:44:08842 // Check the installed extensions to see if what we're reloading was already
843 // installed.
844 scoped_ptr<ExtensionInfo> installed_extension(
845 extension_prefs_->GetInstalledExtensionInfo(extension_id));
846 if (installed_extension.get() &&
847 installed_extension->extension_manifest.get()) {
848 LoadInstalledExtension(*installed_extension, false);
849 } else {
850 // We should always be able to remember the extension's path. If it's not in
851 // the map, someone failed to update |unloaded_extension_paths_|.
852 CHECK(!path.empty());
853 LoadExtension(path);
854 }
[email protected]9cddd4702009-07-27 22:09:40855}
856
[email protected]fa2416f2011-05-03 08:41:20857bool ExtensionService::UninstallExtension(
858 const std::string& extension_id_unsafe,
859 bool external_uninstall,
860 std::string* error) {
[email protected]a29a517a2011-01-21 21:11:12861 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20862
[email protected]fa2416f2011-05-03 08:41:20863 // Copy the extension identifier since the reference might have been
864 // obtained via Extension::id() and the extension may be deleted in
865 // this function.
866 std::string extension_id(extension_id_unsafe);
867
[email protected]8001df22011-04-28 19:59:47868 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]631cf822009-05-15 07:01:25869
[email protected]e7afe2452010-08-22 16:19:13870 // Callers should not send us nonexistent extensions.
[email protected]aebe23a32010-12-10 22:15:48871 CHECK(extension);
[email protected]9f1087e2009-06-15 17:29:32872
[email protected]831aa212010-03-26 13:55:19873 // Get hold of information we need after unloading, since the extension
874 // pointer will be invalid then.
875 GURL extension_url(extension->url());
876 Extension::Location location(extension->location());
[email protected]95da88c42011-03-31 10:07:33877
878 // Policy change which triggers an uninstall will always set
879 // |external_uninstall| to true so this is the only way to uninstall
880 // managed extensions.
[email protected]d6ebc9792011-04-07 18:18:33881 if (!Extension::UserMayDisable(location) && !external_uninstall) {
882 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27883 chrome::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]d6ebc9792011-04-07 18:18:33884 Source<Profile>(profile_),
885 Details<const Extension>(extension));
886 if (error != NULL) {
887 *error = errors::kCannotUninstallManagedExtension;
888 }
889 return false;
890 }
[email protected]95da88c42011-03-31 10:07:33891
[email protected]3bdba0d2011-08-23 07:17:30892 // Extract the data we need for sync now, but don't actually sync until we've
893 // completed the uninstallation.
894 SyncBundle* sync_bundle = GetSyncBundleForExtension(*extension);
895
896 SyncChange sync_change;
897 if (sync_bundle) {
898 ExtensionSyncData extension_sync_data(*extension,
899 IsExtensionEnabled(extension_id),
900 IsIncognitoEnabled(extension_id));
901 sync_change = extension_sync_data.GetSyncChange(SyncChange::ACTION_DELETE);
902 }
903
[email protected]211030342010-09-30 18:41:06904 UninstalledExtensionInfo uninstalled_extension_info(*extension);
[email protected]831aa212010-03-26 13:55:19905
[email protected]9b217652010-10-08 22:04:23906 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08907 extension->GetType(), 100);
[email protected]fe2dd7742011-04-19 22:52:49908 RecordPermissionMessagesHistogram(
909 extension, "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23910
[email protected]8e5c89a2011-06-07 18:13:33911 TemplateURLService* url_service =
912 TemplateURLServiceFactory::GetForProfile(profile_);
913 if (url_service)
914 url_service->UnregisterExtensionKeyword(extension);
[email protected]56ad3792010-05-28 17:45:33915
[email protected]831aa212010-03-26 13:55:19916 // Unload before doing more cleanup to ensure that nothing is hanging on to
917 // any of these resources.
[email protected]814a7bf0f2011-08-13 05:30:59918 UnloadExtension(extension_id, extension_misc::UNLOAD_REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19919
[email protected]fa2416f2011-05-03 08:41:20920 extension_prefs_->OnExtensionUninstalled(extension_id, location,
[email protected]831aa212010-03-26 13:55:19921 external_uninstall);
[email protected]9f1087e2009-06-15 17:29:32922
923 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]831aa212010-03-26 13:55:19924 if (Extension::LOAD != location) {
[email protected]14908b72011-04-20 06:54:36925 if (!BrowserThread::PostTask(
926 BrowserThread::FILE, FROM_HERE,
927 NewRunnableFunction(
928 &extension_file_util::UninstallExtension,
929 install_directory_,
[email protected]fa2416f2011-05-03 08:41:20930 extension_id)))
[email protected]14908b72011-04-20 06:54:36931 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32932 }
933
[email protected]c10da4b02010-03-25 14:38:32934 ClearExtensionData(extension_url);
[email protected]0dfe05c2011-02-23 23:03:36935 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06936
937 // Notify interested parties that we've uninstalled this extension.
938 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27939 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]211030342010-09-30 18:41:06940 Source<Profile>(profile_),
941 Details<UninstalledExtensionInfo>(&uninstalled_extension_info));
[email protected]d6ebc9792011-04-07 18:18:33942
[email protected]3bdba0d2011-08-23 07:17:30943 if (sync_bundle && sync_bundle->HasExtensionId(extension_id)) {
944 sync_bundle->sync_processor->ProcessSyncChanges(
945 FROM_HERE, SyncChangeList(1, sync_change));
946 sync_bundle->synced_extensions.erase(extension_id);
947 }
948
[email protected]d6ebc9792011-04-07 18:18:33949 return true;
[email protected]c10da4b02010-03-25 14:38:32950}
951
[email protected]eaa7dd182010-12-14 11:09:00952void ExtensionService::ClearExtensionData(const GURL& extension_url) {
[email protected]c10da4b02010-03-25 14:38:32953 scoped_refptr<ExtensionDataDeleter> deleter(
954 new ExtensionDataDeleter(profile_, extension_url));
955 deleter->StartDeleting();
[email protected]9f1087e2009-06-15 17:29:32956}
957
[email protected]c3cfb012011-04-06 22:07:35958bool ExtensionService::IsExtensionEnabled(
959 const std::string& extension_id) const {
[email protected]36429da2011-07-11 20:25:18960 const Extension* extension =
[email protected]ad83ca242011-07-29 01:32:25961 GetExtensionByIdInternal(extension_id, true, false, true);
[email protected]36429da2011-07-11 20:25:18962 if (extension)
963 return true;
964
[email protected]ad83ca242011-07-29 01:32:25965 extension =
966 GetExtensionByIdInternal(extension_id, false, true, false);
967 if (extension)
968 return false;
969
970 // If the extension hasn't been loaded yet, check the prefs for it. Assume
971 // enabled unless otherwise noted.
972 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
973 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35974}
975
976bool ExtensionService::IsExternalExtensionUninstalled(
977 const std::string& extension_id) const {
978 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
979}
980
[email protected]eaa7dd182010-12-14 11:09:00981void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12982 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20983
[email protected]06f92562011-04-29 19:27:31984 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39985 return;
[email protected]0c6da502009-08-14 22:32:39986
[email protected]b3317ad2011-04-28 23:46:00987 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]1784e83a2009-09-08 21:01:52988
[email protected]06f92562011-04-29 19:27:31989 const Extension* extension =
990 GetExtensionByIdInternal(extension_id, false, true, false);
991 // This can happen if sync enables an extension that is not
992 // installed yet.
993 if (!extension)
994 return;
995
[email protected]0c6da502009-08-14 22:32:39996 // Move it over to the enabled list.
[email protected]00cd9c42010-11-02 20:15:57997 extensions_.push_back(make_scoped_refptr(extension));
[email protected]0c6da502009-08-14 22:32:39998 ExtensionList::iterator iter = std::find(disabled_extensions_.begin(),
999 disabled_extensions_.end(),
1000 extension);
1001 disabled_extensions_.erase(iter);
1002
[email protected]f681c782010-11-19 11:19:391003 // Make sure any browser action contained within it is not hidden.
1004 extension_prefs_->SetBrowserActionVisibility(extension, true);
1005
[email protected]62d30f42009-10-01 22:36:061006 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:301007
1008 SyncExtensionChangeIfNeeded(*extension);
[email protected]0c6da502009-08-14 22:32:391009}
1010
[email protected]eaa7dd182010-12-14 11:09:001011void ExtensionService::DisableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:121012 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:201013
[email protected]b2ba9962009-12-10 20:10:151014 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:311015 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:521016 return;
[email protected]1784e83a2009-09-08 21:01:521017
[email protected]06f92562011-04-29 19:27:311018 const Extension* extension = GetInstalledExtension(extension_id);
1019 // |extension| can be NULL if sync disables an extension that is not
1020 // installed yet.
1021 if (extension && !Extension::UserMayDisable(extension->location()))
[email protected]95da88c42011-03-31 10:07:331022 return;
1023
[email protected]b3317ad2011-04-28 23:46:001024 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]1784e83a2009-09-08 21:01:521025
[email protected]06f92562011-04-29 19:27:311026 extension = GetExtensionByIdInternal(extension_id, true, false, true);
1027 if (!extension)
1028 return;
1029
[email protected]1784e83a2009-09-08 21:01:521030 // Move it over to the disabled list.
[email protected]00cd9c42010-11-02 20:15:571031 disabled_extensions_.push_back(make_scoped_refptr(extension));
[email protected]1784e83a2009-09-08 21:01:521032 ExtensionList::iterator iter = std::find(extensions_.begin(),
1033 extensions_.end(),
1034 extension);
[email protected]06f92562011-04-29 19:27:311035 if (iter != extensions_.end()) {
1036 extensions_.erase(iter);
1037 } else {
1038 iter = std::find(terminated_extensions_.begin(),
1039 terminated_extensions_.end(),
1040 extension);
1041 terminated_extensions_.erase(iter);
1042 }
[email protected]1784e83a2009-09-08 21:01:521043
[email protected]814a7bf0f2011-08-13 05:30:591044 NotifyExtensionUnloaded(extension, extension_misc::UNLOAD_REASON_DISABLE);
[email protected]3bdba0d2011-08-23 07:17:301045
1046 SyncExtensionChangeIfNeeded(*extension);
[email protected]1784e83a2009-09-08 21:01:521047}
1048
[email protected]eaa7dd182010-12-14 11:09:001049void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]8d888c12010-11-30 00:00:251050 CHECK(extension);
1051
[email protected]902fd7b2011-07-27 18:42:311052 // We only maintain the granted permissions prefs for extensions that can't
1053 // silently increase their permissions.
1054 if (extension->CanSilentlyIncreasePermissions())
1055 return;
[email protected]8d888c12010-11-30 00:00:251056
[email protected]8d888c12010-11-30 00:00:251057 extension_prefs_->AddGrantedPermissions(extension->id(),
[email protected]902fd7b2011-07-27 18:42:311058 extension->GetActivePermissions());
[email protected]8d888c12010-11-30 00:00:251059}
1060
[email protected]eaa7dd182010-12-14 11:09:001061void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]8d888c12010-11-30 00:00:251062 const Extension* extension) {
1063 CHECK(extension);
[email protected]fe2dd7742011-04-19 22:52:491064 RecordPermissionMessagesHistogram(
1065 extension, "Extensions.Permissions_ReEnable");
[email protected]8d888c12010-11-30 00:00:251066 GrantPermissions(extension);
1067 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
1068 EnableExtension(extension->id());
1069}
1070
[email protected]902fd7b2011-07-27 18:42:311071void ExtensionService::UpdateActivePermissions(
1072 const Extension* extension,
1073 const ExtensionPermissionSet* permissions) {
1074 extension_prefs()->SetActivePermissions(extension->id(), permissions);
1075 extension->SetActivePermissions(permissions);
1076}
1077
[email protected]eaa7dd182010-12-14 11:09:001078void ExtensionService::LoadExtension(const FilePath& extension_path) {
[email protected]71eb1e6d2011-08-02 22:57:401079 LoadExtension(extension_path, true);
1080}
1081
1082void ExtensionService::LoadExtension(const FilePath& extension_path,
1083 bool prompt_for_plugins) {
[email protected]e49002a2011-07-13 03:40:441084 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
1085 NewRunnableMethod(backend_.get(),
1086 &ExtensionServiceBackend::LoadSingleExtension,
[email protected]71eb1e6d2011-08-02 22:57:401087 extension_path, prompt_for_plugins));
[email protected]e49002a2011-07-13 03:40:441088}
1089
1090void ExtensionService::LoadExtensionFromCommandLine(
[email protected]67d2d802011-07-21 02:54:281091 const FilePath& path_in) {
1092
1093 // Load extensions from the command line synchronously to avoid a race
1094 // between extension loading and loading an URL from the command line.
1095 base::ThreadRestrictions::ScopedAllowIO allow_io;
1096
1097 FilePath extension_path = path_in;
1098 file_util::AbsolutePath(&extension_path);
1099
1100 std::string id = Extension::GenerateIdForPath(extension_path);
[email protected]cdfca9702011-08-08 16:07:011101 bool allow_file_access =
1102 Extension::ShouldAlwaysAllowFileAccess(Extension::LOAD);
1103 if (extension_prefs()->HasAllowFileAccessSetting(id))
1104 allow_file_access = extension_prefs()->AllowFileAccess(id);
[email protected]67d2d802011-07-21 02:54:281105
1106 int flags = Extension::NO_FLAGS;
[email protected]cdfca9702011-08-08 16:07:011107 if (allow_file_access)
[email protected]67d2d802011-07-21 02:54:281108 flags |= Extension::ALLOW_FILE_ACCESS;
1109 if (Extension::ShouldDoStrictErrorChecking(Extension::LOAD))
1110 flags |= Extension::STRICT_ERROR_CHECKS;
1111
1112 std::string error;
1113 scoped_refptr<const Extension> extension(extension_file_util::LoadExtension(
1114 extension_path,
1115 Extension::LOAD,
1116 flags,
1117 &error));
1118
1119 if (!extension) {
1120 ReportExtensionLoadError(
1121 extension_path,
1122 error,
1123 chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR,
1124 true);
1125 return;
1126 }
1127
1128 OnLoadSingleExtension(extension, false);
[email protected]9f1087e2009-06-15 17:29:321129}
1130
[email protected]eaa7dd182010-12-14 11:09:001131void ExtensionService::LoadComponentExtensions() {
[email protected]1952c7d2010-03-04 23:48:341132 for (RegisteredComponentExtensions::iterator it =
1133 component_extension_manifests_.begin();
1134 it != component_extension_manifests_.end(); ++it) {
[email protected]61b55b62011-03-24 09:03:101135 LoadComponentExtension(*it);
[email protected]1952c7d2010-03-04 23:48:341136 }
1137}
1138
[email protected]a964e112011-04-14 21:52:511139const Extension* ExtensionService::LoadComponentExtension(
[email protected]61b55b62011-03-24 09:03:101140 const ComponentExtensionInfo &info) {
1141 JSONStringValueSerializer serializer(info.manifest);
1142 scoped_ptr<Value> manifest(serializer.Deserialize(NULL, NULL));
1143 if (!manifest.get()) {
[email protected]0b05c262011-08-11 01:42:211144 LOG(ERROR) << "Failed to parse manifest for extension";
[email protected]a964e112011-04-14 21:52:511145 return NULL;
[email protected]61b55b62011-03-24 09:03:101146 }
1147
[email protected]83048a22011-03-29 00:14:131148 int flags = Extension::REQUIRE_KEY;
1149 if (Extension::ShouldDoStrictErrorChecking(Extension::COMPONENT))
1150 flags |= Extension::STRICT_ERROR_CHECKS;
[email protected]61b55b62011-03-24 09:03:101151 std::string error;
1152 scoped_refptr<const Extension> extension(Extension::Create(
1153 info.root_directory,
1154 Extension::COMPONENT,
1155 *static_cast<DictionaryValue*>(manifest.get()),
[email protected]83048a22011-03-29 00:14:131156 flags,
[email protected]61b55b62011-03-24 09:03:101157 &error));
1158 if (!extension.get()) {
[email protected]0b05c262011-08-11 01:42:211159 LOG(ERROR) << error;
[email protected]a964e112011-04-14 21:52:511160 return NULL;
[email protected]61b55b62011-03-24 09:03:101161 }
1162 AddExtension(extension);
[email protected]a964e112011-04-14 21:52:511163 return extension;
[email protected]61b55b62011-03-24 09:03:101164}
1165
[email protected]ad63a792011-06-16 06:41:111166void ExtensionService::UnloadComponentExtension(
1167 const ComponentExtensionInfo& info) {
1168 JSONStringValueSerializer serializer(info.manifest);
1169 scoped_ptr<Value> manifest(serializer.Deserialize(NULL, NULL));
1170 if (!manifest.get()) {
[email protected]0b05c262011-08-11 01:42:211171 LOG(ERROR) << "Failed to parse manifest for extension";
[email protected]ad63a792011-06-16 06:41:111172 return;
1173 }
1174 std::string public_key;
1175 std::string public_key_bytes;
1176 std::string id;
1177 if (!static_cast<DictionaryValue*>(manifest.get())->
1178 GetString(extension_manifest_keys::kPublicKey, &public_key) ||
1179 !Extension::ParsePEMKeyBytes(public_key, &public_key_bytes) ||
1180 !Extension::GenerateId(public_key_bytes, &id)) {
[email protected]0b05c262011-08-11 01:42:211181 LOG(ERROR) << "Failed to get extension id";
[email protected]ad63a792011-06-16 06:41:111182 return;
1183 }
[email protected]814a7bf0f2011-08-13 05:30:591184 UnloadExtension(id, extension_misc::UNLOAD_REASON_DISABLE);
[email protected]ad63a792011-06-16 06:41:111185}
1186
[email protected]eaa7dd182010-12-14 11:09:001187void ExtensionService::LoadAllExtensions() {
[email protected]a29a517a2011-01-21 21:11:121188 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a315ba92010-11-16 14:12:211189
[email protected]cc2c3432009-11-06 17:24:361190 base::TimeTicks start_time = base::TimeTicks::Now();
1191
[email protected]1952c7d2010-03-04 23:48:341192 // Load any component extensions.
1193 LoadComponentExtensions();
1194
[email protected]e72e8eb82009-06-18 17:21:511195 // Load the previously installed extensions.
[email protected]a315ba92010-11-16 14:12:211196 scoped_ptr<ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]e6090e42010-03-23 22:44:081197 extension_prefs_->GetInstalledExtensionsInfo());
[email protected]c6d474f82009-12-16 21:11:061198
[email protected]a315ba92010-11-16 14:12:211199 std::vector<int> reload_reason_counts(NUM_MANIFEST_RELOAD_REASONS, 0);
1200 bool should_write_prefs = false;
1201
1202 for (size_t i = 0; i < extensions_info->size(); ++i) {
1203 ExtensionInfo* info = extensions_info->at(i).get();
1204
1205 ManifestReloadReason reload_reason = ShouldReloadExtensionManifest(*info);
1206 ++reload_reason_counts[reload_reason];
1207 UMA_HISTOGRAM_ENUMERATION("Extensions.ManifestReloadEnumValue",
1208 reload_reason, 100);
1209
1210 if (reload_reason != NOT_NEEDED) {
1211 // Reloading and extension reads files from disk. We do this on the
1212 // UI thread because reloads should be very rare, and the complexity
1213 // added by delaying the time when the extensions service knows about
1214 // all extensions is significant. See crbug.com/37548 for details.
1215 // |allow_io| disables tests that file operations run on the file
1216 // thread.
1217 base::ThreadRestrictions::ScopedAllowIO allow_io;
1218
[email protected]83048a22011-03-29 00:14:131219 int flags = Extension::NO_FLAGS;
1220 if (Extension::ShouldDoStrictErrorChecking(info->extension_location))
1221 flags |= Extension::STRICT_ERROR_CHECKS;
[email protected]3aff9ad2011-04-01 20:26:481222 if (extension_prefs_->AllowFileAccess(info->extension_id))
1223 flags |= Extension::ALLOW_FILE_ACCESS;
[email protected]620db1762011-07-15 21:57:341224 if (extension_prefs_->IsFromWebStore(info->extension_id))
1225 flags |= Extension::FROM_WEBSTORE;
[email protected]e805baf2011-07-26 18:23:051226 if (extension_prefs_->IsFromBookmark(info->extension_id))
1227 flags |= Extension::FROM_BOOKMARK;
[email protected]a315ba92010-11-16 14:12:211228 std::string error;
1229 scoped_refptr<const Extension> extension(
1230 extension_file_util::LoadExtension(
[email protected]542258c2011-03-04 21:25:311231 info->extension_path,
1232 info->extension_location,
[email protected]83048a22011-03-29 00:14:131233 flags,
[email protected]542258c2011-03-04 21:25:311234 &error));
[email protected]a315ba92010-11-16 14:12:211235
1236 if (extension.get()) {
1237 extensions_info->at(i)->extension_manifest.reset(
1238 static_cast<DictionaryValue*>(
1239 extension->manifest_value()->DeepCopy()));
1240 should_write_prefs = true;
1241 }
[email protected]c6d474f82009-12-16 21:11:061242 }
1243 }
1244
[email protected]a315ba92010-11-16 14:12:211245 for (size_t i = 0; i < extensions_info->size(); ++i) {
1246 LoadInstalledExtension(*extensions_info->at(i), should_write_prefs);
[email protected]c6d474f82009-12-16 21:11:061247 }
1248
[email protected]ae09ca62009-08-21 19:46:461249 OnLoadedInstalledExtensions();
[email protected]cc2c3432009-11-06 17:24:361250
[email protected]a315ba92010-11-16 14:12:211251 // The histograms Extensions.ManifestReload* allow us to validate
1252 // the assumption that reloading manifest is a rare event.
1253 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadNotNeeded",
1254 reload_reason_counts[NOT_NEEDED]);
1255 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadUnpackedDir",
1256 reload_reason_counts[UNPACKED_DIR]);
1257 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadNeedsRelocalization",
1258 reload_reason_counts[NEEDS_RELOCALIZATION]);
1259
[email protected]cc2c3432009-11-06 17:24:361260 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadAll", extensions_.size());
1261 UMA_HISTOGRAM_COUNTS_100("Extensions.Disabled", disabled_extensions_.size());
1262
[email protected]1952c7d2010-03-04 23:48:341263 UMA_HISTOGRAM_TIMES("Extensions.LoadAllTime",
1264 base::TimeTicks::Now() - start_time);
[email protected]cc2c3432009-11-06 17:24:361265
[email protected]d101c4a2011-07-22 22:51:361266 int app_user_count = 0;
1267 int app_external_count = 0;
[email protected]9b217652010-10-08 22:04:231268 int hosted_app_count = 0;
1269 int packaged_app_count = 0;
[email protected]1952c7d2010-03-04 23:48:341270 int user_script_count = 0;
[email protected]d101c4a2011-07-22 22:51:361271 int extension_user_count = 0;
1272 int extension_external_count = 0;
[email protected]1952c7d2010-03-04 23:48:341273 int theme_count = 0;
[email protected]1952c7d2010-03-04 23:48:341274 int page_action_count = 0;
1275 int browser_action_count = 0;
1276 ExtensionList::iterator ex;
1277 for (ex = extensions_.begin(); ex != extensions_.end(); ++ex) {
[email protected]9b217652010-10-08 22:04:231278 Extension::Location location = (*ex)->location();
[email protected]7fa19f82010-12-21 19:40:081279 Extension::Type type = (*ex)->GetType();
[email protected]9b217652010-10-08 22:04:231280 if ((*ex)->is_app()) {
1281 UMA_HISTOGRAM_ENUMERATION("Extensions.AppLocation",
1282 location, 100);
1283 } else if (type == Extension::TYPE_EXTENSION) {
1284 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionLocation",
1285 location, 100);
1286 }
1287
[email protected]1952c7d2010-03-04 23:48:341288 // Don't count component extensions, since they are only extensions as an
1289 // implementation detail.
[email protected]9b217652010-10-08 22:04:231290 if (location == Extension::COMPONENT)
[email protected]1952c7d2010-03-04 23:48:341291 continue;
1292
[email protected]e8c729a2010-03-09 19:55:191293 // Don't count unpacked extensions, since they're a developer-specific
1294 // feature.
[email protected]9b217652010-10-08 22:04:231295 if (location == Extension::LOAD)
[email protected]e8c729a2010-03-09 19:55:191296 continue;
1297
[email protected]9b217652010-10-08 22:04:231298 // Using an enumeration shows us the total installed ratio across all users.
1299 // Using the totals per user at each startup tells us the distribution of
1300 // usage for each user (e.g. 40% of users have at least one app installed).
1301 UMA_HISTOGRAM_ENUMERATION("Extensions.LoadType", type, 100);
1302 switch (type) {
1303 case Extension::TYPE_THEME:
[email protected]a315ba92010-11-16 14:12:211304 ++theme_count;
[email protected]9b217652010-10-08 22:04:231305 break;
1306 case Extension::TYPE_USER_SCRIPT:
[email protected]a315ba92010-11-16 14:12:211307 ++user_script_count;
[email protected]9b217652010-10-08 22:04:231308 break;
1309 case Extension::TYPE_HOSTED_APP:
[email protected]a315ba92010-11-16 14:12:211310 ++hosted_app_count;
[email protected]d101c4a2011-07-22 22:51:361311 if (Extension::IsExternalLocation(location)) {
1312 ++app_external_count;
1313 } else {
1314 ++app_user_count;
1315 }
[email protected]9b217652010-10-08 22:04:231316 break;
1317 case Extension::TYPE_PACKAGED_APP:
[email protected]a315ba92010-11-16 14:12:211318 ++packaged_app_count;
[email protected]d101c4a2011-07-22 22:51:361319 if (Extension::IsExternalLocation(location)) {
1320 ++app_external_count;
1321 } else {
1322 ++app_user_count;
1323 }
[email protected]9b217652010-10-08 22:04:231324 break;
1325 case Extension::TYPE_EXTENSION:
1326 default:
[email protected]d101c4a2011-07-22 22:51:361327 if (Extension::IsExternalLocation(location)) {
1328 ++extension_external_count;
1329 } else {
1330 ++extension_user_count;
1331 }
[email protected]9b217652010-10-08 22:04:231332 break;
[email protected]cc2c3432009-11-06 17:24:361333 }
[email protected]9b217652010-10-08 22:04:231334 if ((*ex)->page_action() != NULL)
[email protected]a315ba92010-11-16 14:12:211335 ++page_action_count;
[email protected]9b217652010-10-08 22:04:231336 if ((*ex)->browser_action() != NULL)
[email protected]a315ba92010-11-16 14:12:211337 ++browser_action_count;
[email protected]fe2dd7742011-04-19 22:52:491338
1339 RecordPermissionMessagesHistogram(
1340 ex->get(), "Extensions.Permissions_Load");
[email protected]cc2c3432009-11-06 17:24:361341 }
[email protected]d101c4a2011-07-22 22:51:361342 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadApp",
1343 app_user_count + app_external_count);
1344 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadAppUser", app_user_count);
1345 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadAppExternal", app_external_count);
[email protected]9b217652010-10-08 22:04:231346 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadHostedApp", hosted_app_count);
1347 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadPackagedApp", packaged_app_count);
[email protected]d101c4a2011-07-22 22:51:361348 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExtension",
1349 extension_user_count + extension_external_count);
1350 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExtensionUser",
1351 extension_user_count);
1352 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExtensionExternal",
1353 extension_external_count);
[email protected]1952c7d2010-03-04 23:48:341354 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadUserScript", user_script_count);
1355 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadTheme", theme_count);
[email protected]1952c7d2010-03-04 23:48:341356 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadPageAction", page_action_count);
1357 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadBrowserAction",
1358 browser_action_count);
[email protected]ae09ca62009-08-21 19:46:461359}
1360
[email protected]fe2dd7742011-04-19 22:52:491361// static
1362void ExtensionService::RecordPermissionMessagesHistogram(
1363 const Extension* e, const char* histogram) {
1364 // Since this is called from multiple sources, and since the Histogram macros
1365 // use statics, we need to manually lookup the Histogram ourselves.
1366 base::Histogram* counter = base::LinearHistogram::FactoryGet(
1367 histogram,
1368 1,
[email protected]0d3e4a22011-06-23 19:02:521369 ExtensionPermissionMessage::kEnumBoundary,
1370 ExtensionPermissionMessage::kEnumBoundary + 1,
[email protected]fe2dd7742011-04-19 22:52:491371 base::Histogram::kUmaTargetedHistogramFlag);
1372
[email protected]0d3e4a22011-06-23 19:02:521373 ExtensionPermissionMessages permissions = e->GetPermissionMessages();
[email protected]fe2dd7742011-04-19 22:52:491374 if (permissions.empty()) {
[email protected]0d3e4a22011-06-23 19:02:521375 counter->Add(ExtensionPermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:491376 } else {
[email protected]0d3e4a22011-06-23 19:02:521377 for (ExtensionPermissionMessages::iterator it = permissions.begin();
1378 it != permissions.end(); ++it)
1379 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:491380 }
1381}
1382
[email protected]eaa7dd182010-12-14 11:09:001383void ExtensionService::LoadInstalledExtension(const ExtensionInfo& info,
[email protected]542258c2011-03-04 21:25:311384 bool write_to_prefs) {
[email protected]ae09ca62009-08-21 19:46:461385 std::string error;
[email protected]9adb9692010-10-29 23:14:021386 scoped_refptr<const Extension> extension(NULL);
[email protected]306a2bd2010-08-11 14:56:361387 if (!extension_prefs_->IsExtensionAllowedByPolicy(info.extension_id)) {
1388 error = errors::kDisabledByPolicy;
1389 } else if (info.extension_manifest.get()) {
[email protected]83048a22011-03-29 00:14:131390 int flags = Extension::NO_FLAGS;
1391 if (info.extension_location != Extension::LOAD)
1392 flags |= Extension::REQUIRE_KEY;
1393 if (Extension::ShouldDoStrictErrorChecking(info.extension_location))
1394 flags |= Extension::STRICT_ERROR_CHECKS;
[email protected]3aff9ad2011-04-01 20:26:481395 if (extension_prefs_->AllowFileAccess(info.extension_id))
1396 flags |= Extension::ALLOW_FILE_ACCESS;
[email protected]620db1762011-07-15 21:57:341397 if (extension_prefs_->IsFromWebStore(info.extension_id))
1398 flags |= Extension::FROM_WEBSTORE;
[email protected]e805baf2011-07-26 18:23:051399 if (extension_prefs_->IsFromBookmark(info.extension_id))
1400 flags |= Extension::FROM_BOOKMARK;
[email protected]66e4eb32010-10-27 20:37:411401 extension = Extension::Create(
[email protected]542258c2011-03-04 21:25:311402 info.extension_path,
1403 info.extension_location,
1404 *info.extension_manifest,
[email protected]83048a22011-03-29 00:14:131405 flags,
[email protected]542258c2011-03-04 21:25:311406 &error);
[email protected]ae09ca62009-08-21 19:46:461407 } else {
[email protected]c6d474f82009-12-16 21:11:061408 error = errors::kManifestUnreadable;
[email protected]ae09ca62009-08-21 19:46:461409 }
1410
1411 if (!extension) {
[email protected]c6d474f82009-12-16 21:11:061412 ReportExtensionLoadError(info.extension_path,
[email protected]d11c8e92009-10-20 23:26:401413 error,
[email protected]432115822011-07-10 15:52:271414 chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR,
[email protected]d11c8e92009-10-20 23:26:401415 false);
[email protected]ae09ca62009-08-21 19:46:461416 return;
1417 }
1418
[email protected]c6d474f82009-12-16 21:11:061419 if (write_to_prefs)
1420 extension_prefs_->UpdateManifest(extension);
1421
[email protected]49cd1572011-02-08 21:38:451422 AddExtension(extension);
[email protected]9f1087e2009-06-15 17:29:321423}
1424
[email protected]eaa7dd182010-12-14 11:09:001425void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:421426 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061427 // was loaded, otherwise a race can arise where a renderer that is created
1428 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421429 // that the request context doesn't yet know about. The profile is responsible
1430 // for ensuring its URLRequestContexts appropriately discover the loaded
1431 // extension.
[email protected]c8d407e2011-04-28 21:27:171432 profile_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:061433
[email protected]c8d407e2011-04-28 21:27:171434 // Tell subsystems that use the EXTENSION_LOADED notification about the new
1435 // extension.
[email protected]62d30f42009-10-01 22:36:061436 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271437 chrome::NOTIFICATION_EXTENSION_LOADED,
[email protected]24e7a9d2009-11-04 11:11:341438 Source<Profile>(profile_),
[email protected]9adb9692010-10-29 23:14:021439 Details<const Extension>(extension));
[email protected]2de307592011-04-05 21:16:581440
[email protected]c8d407e2011-04-28 21:27:171441 // Tell renderers about the new extension.
[email protected]77a6970c2011-04-23 16:58:561442 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
1443 !i.IsAtEnd(); i.Advance()) {
[email protected]c8d407e2011-04-28 21:27:171444 RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081445 Profile* host_profile =
1446 Profile::FromBrowserContext(host->browser_context());
1447 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile()) {
[email protected]c8d407e2011-04-28 21:27:171448 host->Send(
[email protected]15379642011-08-11 22:22:051449 new ExtensionMsg_Loaded(ExtensionMsg_Loaded_Params(extension)));
[email protected]c8d407e2011-04-28 21:27:171450 }
[email protected]77a6970c2011-04-23 16:58:561451 }
1452
[email protected]c8d407e2011-04-28 21:27:171453 // Tell a random-ass collection of other subsystems about the new extension.
1454 // TODO(aa): What should we do with all this goop? Can it move into the
1455 // relevant objects via EXTENSION_LOADED?
1456
1457 profile_->GetExtensionSpecialStoragePolicy()->
1458 GrantRightsForExtension(extension);
1459
1460 UpdateActiveExtensionsInCrashReporter();
1461
1462 ExtensionWebUI::RegisterChromeURLOverrides(
1463 profile_, extension->GetChromeURLOverrides());
1464
[email protected]8e5c89a2011-06-07 18:13:331465 TemplateURLService* url_service =
1466 TemplateURLServiceFactory::GetForProfile(profile_);
1467 if (url_service)
1468 url_service->RegisterExtensionKeyword(extension);
[email protected]c8d407e2011-04-28 21:27:171469
1470 // Load the icon for omnibox-enabled extensions so it will be ready to display
1471 // in the URL bar.
1472 if (!extension->omnibox_keyword().empty()) {
1473 omnibox_popup_icon_manager_.LoadIcon(extension);
1474 omnibox_icon_manager_.LoadIcon(extension);
1475 }
1476
1477 // If the extension has permission to load chrome://favicon/ resources we need
1478 // to make sure that the FaviconSource is registered with the
1479 // ChromeURLDataManager.
1480 if (extension->HasHostPermission(GURL(chrome::kChromeUIFaviconURL))) {
1481 FaviconSource* favicon_source = new FaviconSource(profile_,
1482 FaviconSource::FAVICON);
1483 profile_->GetChromeURLDataManager()->AddDataSource(favicon_source);
1484 }
1485
1486 // TODO(mpcomplete): This ends up affecting all profiles. See crbug.com/80757.
[email protected]2de307592011-04-05 21:16:581487 bool plugins_changed = false;
1488 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1489 const Extension::PluginInfo& plugin = extension->plugins()[i];
1490 webkit::npapi::PluginList::Singleton()->RefreshPlugins();
1491 webkit::npapi::PluginList::Singleton()->AddExtraPluginPath(plugin.path);
1492 plugins_changed = true;
1493 if (!plugin.is_public) {
1494 PluginService::GetInstance()->RestrictPluginToUrl(
1495 plugin.path, extension->url());
1496 }
1497 }
[email protected]84396dbc2011-04-14 06:33:421498
1499 bool nacl_modules_changed = false;
1500 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1501 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001502 RegisterNaClModule(module.url, module.mime_type);
[email protected]84396dbc2011-04-14 06:33:421503 nacl_modules_changed = true;
1504 }
1505
[email protected]ed0ba002011-05-26 16:55:131506 if (nacl_modules_changed)
1507 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421508
1509 if (plugins_changed || nacl_modules_changed)
[email protected]2de307592011-04-05 21:16:581510 PluginService::GetInstance()->PurgePluginListCache(false);
[email protected]b4d9d122011-06-17 01:58:541511
[email protected]14da67b02011-08-02 05:49:131512#if defined(OS_CHROMEOS)
1513#if defined(TOUCH_UI)
[email protected]c8d4e9462011-06-29 23:18:571514 chromeos::input_method::InputMethodManager* input_method_manager =
1515 chromeos::input_method::InputMethodManager::GetInstance();
[email protected]14da67b02011-08-02 05:49:131516#endif
[email protected]b4d9d122011-06-17 01:58:541517 for (std::vector<Extension::InputComponentInfo>::const_iterator component =
1518 extension->input_components().begin();
1519 component != extension->input_components().end();
1520 ++component) {
[email protected]14da67b02011-08-02 05:49:131521 if (component->type == Extension::INPUT_COMPONENT_TYPE_IME) {
1522 ExtensionInputImeEventRouter::GetInstance()->RegisterIme(
1523 profile_, extension->id(), *component);
1524 }
1525#if defined(TOUCH_UI)
[email protected]b4d9d122011-06-17 01:58:541526 if (component->type == Extension::INPUT_COMPONENT_TYPE_VIRTUAL_KEYBOARD &&
1527 !component->layouts.empty()) {
1528 const bool is_system =
1529 !Extension::IsExternalLocation(extension->location());
[email protected]cbd4c042011-08-13 18:22:191530 input_method_manager->RegisterVirtualKeyboard(
1531 extension->url(),
1532 component->name, // human-readable name of the keyboard extension.
1533 component->layouts,
1534 is_system);
[email protected]b4d9d122011-06-17 01:58:541535 }
[email protected]14da67b02011-08-02 05:49:131536#endif
[email protected]b4d9d122011-06-17 01:58:541537 }
1538#endif
[email protected]62d30f42009-10-01 22:36:061539}
1540
[email protected]a9f39a312010-12-23 22:14:271541void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591542 const Extension* extension,
1543 extension_misc::UnloadedExtensionReason reason) {
[email protected]a9f39a312010-12-23 22:14:271544 UnloadedExtensionInfo details(extension, reason);
[email protected]62d30f42009-10-01 22:36:061545 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271546 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]24e7a9d2009-11-04 11:11:341547 Source<Profile>(profile_),
[email protected]a9f39a312010-12-23 22:14:271548 Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061549
[email protected]77a6970c2011-04-23 16:58:561550 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
1551 !i.IsAtEnd(); i.Advance()) {
[email protected]c8d407e2011-04-28 21:27:171552 RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081553 Profile* host_profile =
1554 Profile::FromBrowserContext(host->browser_context());
1555 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171556 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561557 }
1558
[email protected]dd163fb02011-05-04 22:22:171559 profile_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]c8d407e2011-04-28 21:27:171560 profile_->GetExtensionSpecialStoragePolicy()->
1561 RevokeRightsForExtension(extension);
1562
1563 ExtensionWebUI::UnregisterChromeURLOverrides(
1564 profile_, extension->GetChromeURLOverrides());
1565
[email protected]b777b332011-04-16 04:01:081566#if defined(OS_CHROMEOS)
1567 // Revoke external file access to
[email protected]c8d407e2011-04-28 21:27:171568 if (profile_->GetFileSystemContext() &&
1569 profile_->GetFileSystemContext()->path_manager() &&
1570 profile_->GetFileSystemContext()->path_manager()->external_provider()) {
1571 profile_->GetFileSystemContext()->path_manager()->external_provider()->
1572 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061573 }
[email protected]c8d407e2011-04-28 21:27:171574#endif
1575
1576 UpdateActiveExtensionsInCrashReporter();
[email protected]2de307592011-04-05 21:16:581577
1578 bool plugins_changed = false;
1579 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1580 const Extension::PluginInfo& plugin = extension->plugins()[i];
[email protected]14908b72011-04-20 06:54:361581 if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
1582 NewRunnableFunction(&ForceShutdownPlugin,
1583 plugin.path)))
1584 NOTREACHED();
[email protected]2de307592011-04-05 21:16:581585 webkit::npapi::PluginList::Singleton()->RefreshPlugins();
1586 webkit::npapi::PluginList::Singleton()->RemoveExtraPluginPath(
1587 plugin.path);
1588 plugins_changed = true;
1589 if (!plugin.is_public)
1590 PluginService::GetInstance()->RestrictPluginToUrl(plugin.path, GURL());
1591 }
[email protected]84396dbc2011-04-14 06:33:421592
1593 bool nacl_modules_changed = false;
1594 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1595 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001596 UnregisterNaClModule(module.url);
[email protected]84396dbc2011-04-14 06:33:421597 nacl_modules_changed = true;
1598 }
1599
[email protected]ed0ba002011-05-26 16:55:131600 if (nacl_modules_changed)
1601 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421602
1603 if (plugins_changed || nacl_modules_changed)
[email protected]2de307592011-04-05 21:16:581604 PluginService::GetInstance()->PurgePluginListCache(false);
[email protected]62d30f42009-10-01 22:36:061605}
1606
[email protected]eaa7dd182010-12-14 11:09:001607void ExtensionService::UpdateExtensionBlacklist(
[email protected]6b75ec32009-08-14 06:37:181608 const std::vector<std::string>& blacklist) {
1609 // Use this set to indicate if an extension in the blacklist has been used.
1610 std::set<std::string> blacklist_set;
1611 for (unsigned int i = 0; i < blacklist.size(); ++i) {
1612 if (Extension::IdIsValid(blacklist[i])) {
1613 blacklist_set.insert(blacklist[i]);
1614 }
1615 }
1616 extension_prefs_->UpdateBlacklist(blacklist_set);
1617 std::vector<std::string> to_be_removed;
1618 // Loop current extensions, unload installed extensions.
1619 for (ExtensionList::const_iterator iter = extensions_.begin();
1620 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021621 const Extension* extension = (*iter);
[email protected]6b75ec32009-08-14 06:37:181622 if (blacklist_set.find(extension->id()) != blacklist_set.end()) {
1623 to_be_removed.push_back(extension->id());
1624 }
1625 }
1626
1627 // UnloadExtension will change the extensions_ list. So, we should
1628 // call it outside the iterator loop.
1629 for (unsigned int i = 0; i < to_be_removed.size(); ++i) {
[email protected]814a7bf0f2011-08-13 05:30:591630 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]6b75ec32009-08-14 06:37:181631 }
1632}
1633
[email protected]0a071a32011-02-08 00:18:241634Profile* ExtensionService::profile() {
1635 return profile_;
1636}
1637
[email protected]eaa7dd182010-12-14 11:09:001638ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451639 return extension_prefs_;
1640}
1641
[email protected]b7f853e282011-08-10 09:24:201642ExtensionSettings* ExtensionService::extension_settings() {
1643 return extension_settings_;
1644}
1645
[email protected]b790b072011-05-20 09:46:441646ExtensionContentSettingsStore*
1647 ExtensionService::GetExtensionContentSettingsStore() {
1648 return extension_prefs()->content_settings_store();
1649}
1650
[email protected]2859946f2011-04-04 18:18:061651ExtensionUpdater* ExtensionService::updater() {
1652 return updater_.get();
1653}
1654
[email protected]eaa7dd182010-12-14 11:09:001655void ExtensionService::CheckAdminBlacklist() {
[email protected]aa96d3a2010-08-21 08:45:251656 std::vector<std::string> to_be_removed;
1657 // Loop through extensions list, unload installed extensions.
1658 for (ExtensionList::const_iterator iter = extensions_.begin();
1659 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021660 const Extension* extension = (*iter);
[email protected]aa96d3a2010-08-21 08:45:251661 if (!extension_prefs_->IsExtensionAllowedByPolicy(extension->id()))
1662 to_be_removed.push_back(extension->id());
1663 }
1664
1665 // UnloadExtension will change the extensions_ list. So, we should
1666 // call it outside the iterator loop.
1667 for (unsigned int i = 0; i < to_be_removed.size(); ++i)
[email protected]814a7bf0f2011-08-13 05:30:591668 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]aa96d3a2010-08-21 08:45:251669}
1670
[email protected]31206602011-04-13 23:07:321671void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351672 if (updater()) {
[email protected]31206602011-04-13 23:07:321673 updater()->CheckSoon();
[email protected]c3cfb012011-04-06 22:07:351674 } else {
[email protected]31206602011-04-13 23:07:321675 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351676 }
1677}
1678
[email protected]3bdba0d2011-08-23 07:17:301679namespace {
1680 bool IsSyncableNone(const Extension& extension) { return false; }
1681} // namespace
1682
1683ExtensionService::SyncBundle::SyncBundle()
1684 : filter(IsSyncableNone),
1685 sync_processor(NULL) {
[email protected]b05fb9ff2011-04-23 00:07:561686}
1687
[email protected]3bdba0d2011-08-23 07:17:301688ExtensionService::SyncBundle::~SyncBundle() {
1689}
1690
1691bool ExtensionService::SyncBundle::HasExtensionId(const std::string& id) const {
1692 return synced_extensions.find(id) != synced_extensions.end();
1693}
1694
1695bool ExtensionService::SyncBundle::HasPendingExtensionId(const std::string& id)
1696 const {
1697 return pending_sync_data.find(id) != pending_sync_data.end();
1698}
1699
1700void ExtensionService::SyncExtensionChangeIfNeeded(const Extension& extension) {
1701 SyncBundle* sync_bundle = GetSyncBundleForExtension(extension);
1702 if (sync_bundle) {
1703 ExtensionSyncData extension_sync_data(extension,
1704 IsExtensionEnabled(extension.id()),
1705 IsIncognitoEnabled(extension.id()));
1706
1707 SyncChangeList sync_change_list(1, extension_sync_data.GetSyncChange(
1708 sync_bundle->HasExtensionId(extension.id()) ?
1709 SyncChange::ACTION_UPDATE : SyncChange::ACTION_ADD));
1710 sync_bundle->sync_processor->ProcessSyncChanges(
1711 FROM_HERE, sync_change_list);
1712 sync_bundle->synced_extensions.insert(extension.id());
1713 sync_bundle->pending_sync_data.erase(extension.id());
[email protected]b05fb9ff2011-04-23 00:07:561714 }
[email protected]3bdba0d2011-08-23 07:17:301715}
1716
1717ExtensionService::SyncBundle* ExtensionService::GetSyncBundleForExtension(
1718 const Extension& extension) {
1719 if (app_sync_bundle_.filter(extension))
1720 return &app_sync_bundle_;
1721 else if (extension_sync_bundle_.filter(extension))
1722 return &extension_sync_bundle_;
1723 else
1724 return NULL;
1725}
1726
1727ExtensionService::SyncBundle*
1728 ExtensionService::GetSyncBundleForExtensionSyncData(
1729 const ExtensionSyncData& extension_sync_data) {
1730 switch (extension_sync_data.type()) {
1731 case Extension::SYNC_TYPE_APP:
1732 return &app_sync_bundle_;
1733 case Extension::SYNC_TYPE_EXTENSION:
1734 return &extension_sync_bundle_;
1735 default:
1736 NOTREACHED();
1737 return NULL;
1738 }
1739}
1740
1741#define GET_SYNC_BUNDLE_FOR_MODEL_TYPE_BODY() \
1742 do { \
1743 switch (type) { \
1744 case syncable::APPS: \
1745 return &app_sync_bundle_; \
1746 case syncable::EXTENSIONS: \
1747 return &extension_sync_bundle_; \
1748 default: \
1749 NOTREACHED(); \
1750 return NULL; \
1751 } \
1752 } while (0)
1753
1754const ExtensionService::SyncBundle*
1755 ExtensionService::GetSyncBundleForModelTypeConst(
1756 syncable::ModelType type) const {
1757 GET_SYNC_BUNDLE_FOR_MODEL_TYPE_BODY();
1758}
1759
1760ExtensionService::SyncBundle* ExtensionService::GetSyncBundleForModelType(
1761 syncable::ModelType type) {
1762 GET_SYNC_BUNDLE_FOR_MODEL_TYPE_BODY();
1763}
1764
1765#undef GET_SYNC_BUNDLE_FOR_MODEL_TYPE_BODY
1766
1767SyncError ExtensionService::MergeDataAndStartSyncing(
1768 syncable::ModelType type,
1769 const SyncDataList& initial_sync_data,
1770 SyncChangeProcessor* sync_processor) {
1771 CHECK(sync_processor);
1772
1773 SyncBundle* bundle = NULL;
1774
1775 switch (type) {
1776 case syncable::EXTENSIONS:
1777 bundle = &extension_sync_bundle_;
1778 bundle->filter = IsSyncableExtension;
1779 break;
1780
1781 case syncable::APPS:
1782 bundle = &app_sync_bundle_;
1783 bundle->filter = IsSyncableApp;
1784 break;
1785
1786 default:
1787 LOG(FATAL) << "Got " << type << " ModelType";
1788 }
1789
1790 bundle->sync_processor = sync_processor;
1791
1792 for (SyncDataList::const_iterator i = initial_sync_data.begin();
1793 i != initial_sync_data.end();
1794 ++i) {
1795 ExtensionSyncData extension_sync_data = ExtensionSyncData(*i);
1796 bundle->synced_extensions.insert(extension_sync_data.id());
1797 ProcessExtensionSyncData(extension_sync_data, *bundle);
1798 }
1799
1800 SyncDataList sync_data_list = GetAllSyncData(type);
1801 SyncChangeList sync_change_list;
1802 for (SyncDataList::const_iterator i = sync_data_list.begin();
1803 i != sync_data_list.end();
1804 ++i) {
1805 if (bundle->HasExtensionId(i->GetTag()))
1806 sync_change_list.push_back(SyncChange(SyncChange::ACTION_UPDATE, *i));
1807 else
1808 sync_change_list.push_back(SyncChange(SyncChange::ACTION_ADD, *i));
1809 }
1810 bundle->sync_processor->ProcessSyncChanges(FROM_HERE, sync_change_list);
1811
1812 return SyncError();
1813}
1814
1815void ExtensionService::StopSyncing(syncable::ModelType type) {
1816 SyncBundle* bundle = GetSyncBundleForModelType(type);
1817 CHECK(bundle);
1818 // This is the simplest way to clear out the bundle.
1819 *bundle = SyncBundle();
1820}
1821
1822SyncDataList ExtensionService::GetAllSyncData(syncable::ModelType type) const {
1823 const SyncBundle* bundle = GetSyncBundleForModelTypeConst(type);
1824 CHECK(bundle);
1825 std::vector<ExtensionSyncData> extension_sync_data = GetSyncDataList(*bundle);
1826 SyncDataList result(extension_sync_data.size());
1827 for (int i = 0; i < static_cast<int>(extension_sync_data.size()); ++i) {
1828 result[i] = extension_sync_data[i].GetSyncData();
1829 }
1830 return result;
1831}
1832
1833SyncError ExtensionService::ProcessSyncChanges(
1834 const tracked_objects::Location& from_here,
1835 const SyncChangeList& change_list) {
1836 for (SyncChangeList::const_iterator i = change_list.begin();
1837 i != change_list.end();
1838 ++i) {
1839 ExtensionSyncData extension_sync_data = ExtensionSyncData(*i);
1840 SyncBundle* bundle = GetSyncBundleForExtensionSyncData(extension_sync_data);
1841 CHECK(bundle);
1842
1843 if (extension_sync_data.uninstalled())
1844 bundle->synced_extensions.erase(extension_sync_data.id());
1845 else
1846 bundle->synced_extensions.insert(extension_sync_data.id());
1847 ProcessExtensionSyncData(extension_sync_data, *bundle);
1848 }
1849
1850 return SyncError();
[email protected]b05fb9ff2011-04-23 00:07:561851}
1852
1853void ExtensionService::GetSyncDataListHelper(
1854 const ExtensionList& extensions,
[email protected]3bdba0d2011-08-23 07:17:301855 const SyncBundle& bundle,
[email protected]b05fb9ff2011-04-23 00:07:561856 std::vector<ExtensionSyncData>* sync_data_list) const {
1857 for (ExtensionList::const_iterator it = extensions.begin();
1858 it != extensions.end(); ++it) {
1859 const Extension& extension = **it;
[email protected]3bdba0d2011-08-23 07:17:301860 if (bundle.filter(extension) &&
1861 // If we have pending extension data for this extension, then this
1862 // version is out of date. We'll sync back the version we got from
1863 // sync.
1864 !bundle.HasPendingExtensionId(extension.id())) {
1865 sync_data_list->push_back(
1866 ExtensionSyncData(extension,
1867 IsExtensionEnabled(extension.id()),
1868 IsIncognitoEnabled(extension.id())));
[email protected]b05fb9ff2011-04-23 00:07:561869 }
1870 }
1871}
1872
1873std::vector<ExtensionSyncData> ExtensionService::GetSyncDataList(
[email protected]3bdba0d2011-08-23 07:17:301874 const SyncBundle& bundle) const {
1875 std::vector<ExtensionSyncData> extension_sync_list;
1876 GetSyncDataListHelper(extensions_, bundle, &extension_sync_list);
1877 GetSyncDataListHelper(disabled_extensions_, bundle, &extension_sync_list);
1878 GetSyncDataListHelper(terminated_extensions_, bundle, &extension_sync_list);
1879
1880 for (std::map<std::string, ExtensionSyncData>::const_iterator i =
1881 bundle.pending_sync_data.begin();
1882 i != bundle.pending_sync_data.end();
1883 ++i) {
1884 extension_sync_list.push_back(i->second);
1885 }
1886
1887 return extension_sync_list;
[email protected]b05fb9ff2011-04-23 00:07:561888}
1889
[email protected]3bdba0d2011-08-23 07:17:301890void ExtensionService::ProcessExtensionSyncData(
[email protected]90310d92011-04-17 07:35:041891 const ExtensionSyncData& extension_sync_data,
[email protected]3bdba0d2011-08-23 07:17:301892 SyncBundle& bundle) {
1893 const std::string& id = extension_sync_data.id();
[email protected]90310d92011-04-17 07:35:041894
1895 // Handle uninstalls first.
[email protected]3bdba0d2011-08-23 07:17:301896 if (extension_sync_data.uninstalled()) {
[email protected]90310d92011-04-17 07:35:041897 std::string error;
1898 if (!UninstallExtensionHelper(this, id)) {
1899 LOG(WARNING) << "Could not uninstall extension " << id
1900 << " for sync";
1901 }
1902 return;
1903 }
1904
[email protected]06f92562011-04-29 19:27:311905 // Set user settings.
[email protected]3bdba0d2011-08-23 07:17:301906 if (extension_sync_data.enabled()) {
[email protected]06f92562011-04-29 19:27:311907 EnableExtension(id);
1908 } else {
1909 DisableExtension(id);
1910 }
[email protected]3bdba0d2011-08-23 07:17:301911 SetIsIncognitoEnabled(id, extension_sync_data.incognito_enabled());
[email protected]81b14cc2011-04-29 00:39:371912
[email protected]06f92562011-04-29 19:27:311913 const Extension* extension = GetInstalledExtension(id);
[email protected]90310d92011-04-17 07:35:041914 if (extension) {
[email protected]06f92562011-04-29 19:27:311915 // If the extension is already installed, check if it's outdated.
[email protected]3bdba0d2011-08-23 07:17:301916 int result = extension->version()->CompareTo(extension_sync_data.version());
[email protected]90310d92011-04-17 07:35:041917 if (result < 0) {
1918 // Extension is outdated.
[email protected]3bdba0d2011-08-23 07:17:301919 bundle.pending_sync_data[extension_sync_data.id()] = extension_sync_data;
[email protected]90310d92011-04-17 07:35:041920 CheckForUpdatesSoon();
[email protected]90310d92011-04-17 07:35:041921 }
[email protected]06f92562011-04-29 19:27:311922 } else {
[email protected]06f92562011-04-29 19:27:311923 // TODO(akalin): Replace silent update with a list of enabled
1924 // permissions.
[email protected]6cc7dbae2011-04-29 21:18:331925 const bool kInstallSilently = true;
[email protected]06f92562011-04-29 19:27:311926 if (!pending_extension_manager()->AddFromSync(
1927 id,
[email protected]3bdba0d2011-08-23 07:17:301928 extension_sync_data.update_url(),
1929 bundle.filter,
[email protected]6cc7dbae2011-04-29 21:18:331930 kInstallSilently)) {
[email protected]06f92562011-04-29 19:27:311931 LOG(WARNING) << "Could not add pending extension for " << id;
[email protected]3bdba0d2011-08-23 07:17:301932 // This means that the extension is already pending installation, with a
1933 // non-INTERNAL location. Add to pending_sync_data, even though it will
1934 // never be removed (we'll never install a syncable version of the
1935 // extension), so that GetAllSyncData() continues to send it.
[email protected]06f92562011-04-29 19:27:311936 }
[email protected]3bdba0d2011-08-23 07:17:301937 // Track pending extensions so that we can return them in GetAllSyncData().
1938 bundle.pending_sync_data[extension_sync_data.id()] = extension_sync_data;
[email protected]06f92562011-04-29 19:27:311939 CheckForUpdatesSoon();
[email protected]90310d92011-04-17 07:35:041940 }
[email protected]90310d92011-04-17 07:35:041941}
1942
[email protected]c3cfb012011-04-06 22:07:351943bool ExtensionService::IsIncognitoEnabled(
1944 const std::string& extension_id) const {
1945 // If this is an existing component extension we always allow it to
1946 // work in incognito mode.
[email protected]81b14cc2011-04-29 00:39:371947 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]c3cfb012011-04-06 22:07:351948 if (extension && extension->location() == Extension::COMPONENT)
[email protected]cb0ce1e022010-03-10 19:54:411949 return true;
1950
1951 // Check the prefs.
[email protected]c3cfb012011-04-06 22:07:351952 return extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]db7331a2010-02-25 22:10:501953}
[email protected]55a35692010-02-11 23:25:211954
[email protected]c3cfb012011-04-06 22:07:351955void ExtensionService::SetIsIncognitoEnabled(
1956 const std::string& extension_id, bool enabled) {
[email protected]81b14cc2011-04-29 00:39:371957 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]c3cfb012011-04-06 22:07:351958 if (extension && extension->location() == Extension::COMPONENT) {
1959 // This shouldn't be called for component extensions.
1960 NOTREACHED();
1961 return;
1962 }
1963
[email protected]8c6c8c22011-03-09 12:52:341964 // Broadcast unloaded and loaded events to update browser state. Only bother
1965 // if the value changed and the extension is actually enabled, since there is
1966 // no UI otherwise.
[email protected]c3cfb012011-04-06 22:07:351967 bool old_enabled = extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]8c6c8c22011-03-09 12:52:341968 if (enabled == old_enabled)
1969 return;
1970
[email protected]c3cfb012011-04-06 22:07:351971 extension_prefs_->SetIsIncognitoEnabled(extension_id, enabled);
[email protected]81b14cc2011-04-29 00:39:371972
1973 // If the extension is enabled (and not terminated), unload and
1974 // reload it to update UI.
1975 const Extension* enabled_extension = GetExtensionById(extension_id, false);
1976 if (enabled_extension) {
[email protected]814a7bf0f2011-08-13 05:30:591977 NotifyExtensionUnloaded(
1978 enabled_extension, extension_misc::UNLOAD_REASON_DISABLE);
[email protected]81b14cc2011-04-29 00:39:371979 NotifyExtensionLoaded(enabled_extension);
[email protected]568f33d2010-08-04 17:06:411980 }
[email protected]3bdba0d2011-08-23 07:17:301981
1982 if (extension)
1983 SyncExtensionChangeIfNeeded(*extension);
[email protected]55a35692010-02-11 23:25:211984}
1985
[email protected]eaa7dd182010-12-14 11:09:001986bool ExtensionService::CanCrossIncognito(const Extension* extension) {
[email protected]2a8f24e2010-11-03 21:37:051987 // We allow the extension to see events and data from another profile iff it
1988 // uses "spanning" behavior and it has incognito access. "split" mode
1989 // extensions only see events for a matching profile.
[email protected]c3cfb012011-04-06 22:07:351990 return IsIncognitoEnabled(extension->id()) &&
1991 !extension->incognito_split_mode();
[email protected]2a8f24e2010-11-03 21:37:051992}
1993
[email protected]5ef7b002011-05-12 23:16:201994bool ExtensionService::CanLoadInIncognito(const Extension* extension) const {
1995 if (extension->is_hosted_app())
1996 return true;
1997 // Packaged apps and regular extensions need to be enabled specifically for
1998 // incognito (and split mode should be set).
1999 return extension->incognito_split_mode() &&
2000 IsIncognitoEnabled(extension->id());
2001}
2002
[email protected]eaa7dd182010-12-14 11:09:002003bool ExtensionService::AllowFileAccess(const Extension* extension) {
[email protected]05c82182010-06-24 17:49:082004 return (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]334e04a2010-06-24 23:34:442005 switches::kDisableExtensionsFileAccessCheck) ||
[email protected]05c82182010-06-24 17:49:082006 extension_prefs_->AllowFileAccess(extension->id()));
2007}
2008
[email protected]eaa7dd182010-12-14 11:09:002009void ExtensionService::SetAllowFileAccess(const Extension* extension,
[email protected]3aff9ad2011-04-01 20:26:482010 bool allow) {
2011 // Reload to update browser state. Only bother if the value changed and the
2012 // extension is actually enabled, since there is no UI otherwise.
2013 bool old_allow = AllowFileAccess(extension);
2014 if (allow == old_allow)
2015 return;
2016
[email protected]05c82182010-06-24 17:49:082017 extension_prefs_->SetAllowFileAccess(extension->id(), allow);
[email protected]3aff9ad2011-04-01 20:26:482018
2019 bool extension_is_enabled = std::find(extensions_.begin(), extensions_.end(),
2020 extension) != extensions_.end();
2021 if (extension_is_enabled)
2022 ReloadExtension(extension->id());
[email protected]05c82182010-06-24 17:49:082023}
2024
[email protected]eaa7dd182010-12-14 11:09:002025bool ExtensionService::GetBrowserActionVisibility(const Extension* extension) {
[email protected]f681c782010-11-19 11:19:392026 return extension_prefs_->GetBrowserActionVisibility(extension);
2027}
2028
[email protected]eaa7dd182010-12-14 11:09:002029void ExtensionService::SetBrowserActionVisibility(const Extension* extension,
[email protected]8e4560b62011-01-14 10:09:142030 bool visible) {
[email protected]f681c782010-11-19 11:19:392031 extension_prefs_->SetBrowserActionVisibility(extension, visible);
2032}
2033
[email protected]8e4560b62011-01-14 10:09:142034// Some extensions will autoupdate themselves externally from Chrome. These
2035// are typically part of some larger client application package. To support
2036// these, the extension will register its location in the the preferences file
2037// (and also, on Windows, in the registry) and this code will periodically
2038// check that location for a .crx file, which it will then install locally if
2039// a new version is available.
2040// Errors are reported through ExtensionErrorReporter. Succcess is not
2041// reported.
[email protected]eaa7dd182010-12-14 11:09:002042void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:122043 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:142044
2045 // Note that this installation is intentionally silent (since it didn't
2046 // go through the front-end). Extensions that are registered in this
2047 // way are effectively considered 'pre-bundled', and so implicitly
2048 // trusted. In general, if something has HKLM or filesystem access,
2049 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:122050
2051 // If any external extension records give a URL, a provider will set
2052 // this to true. Used by OnExternalProviderReady() to see if we need
2053 // to start an update check to fetch a new external extension.
2054 external_extension_url_added_ = false;
[email protected]8e4560b62011-01-14 10:09:142055
2056 // Ask each external extension provider to give us a call back for each
2057 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
2058 ProviderCollection::const_iterator i;
2059 for (i = external_extension_providers_.begin();
2060 i != external_extension_providers_.end(); ++i) {
2061 ExternalExtensionProviderInterface* provider = i->get();
2062 provider->VisitRegisteredExtension();
2063 }
2064
2065 // Uninstall of unclaimed extensions will happen after all the providers
[email protected]a29a517a2011-01-21 21:11:122066 // had reported ready. Every provider calls OnExternalProviderReady()
2067 // when it finishes, and OnExternalProviderReady() only acts when all
2068 // providers are ready. In case there are no providers, we call it
2069 // to trigger removal of extensions that used to have an external source.
2070 if (external_extension_providers_.empty())
2071 OnExternalProviderReady();
[email protected]9f1087e2009-06-15 17:29:322072}
2073
[email protected]8e4560b62011-01-14 10:09:142074void ExtensionService::OnExternalProviderReady() {
[email protected]a29a517a2011-01-21 21:11:122075 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2076
2077 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:142078 // if all of them are finished. So we check them first.
2079 ProviderCollection::const_iterator i;
2080 for (i = external_extension_providers_.begin();
2081 i != external_extension_providers_.end(); ++i) {
2082 ExternalExtensionProviderInterface* provider = i->get();
[email protected]a29a517a2011-01-21 21:11:122083 if (!provider->IsReady())
[email protected]8e4560b62011-01-14 10:09:142084 return;
[email protected]8e4560b62011-01-14 10:09:142085 }
2086
[email protected]a29a517a2011-01-21 21:11:122087 // All the providers are ready. Install any pending extensions.
2088 if (external_extension_url_added_ && updater()) {
2089 external_extension_url_added_ = false;
[email protected]8e4560b62011-01-14 10:09:142090 updater()->CheckNow();
2091 }
2092
2093 // Uninstall all the unclaimed extensions.
2094 scoped_ptr<ExtensionPrefs::ExtensionsInfo> extensions_info(
2095 extension_prefs_->GetInstalledExtensionsInfo());
2096 for (size_t i = 0; i < extensions_info->size(); ++i) {
2097 ExtensionInfo* info = extensions_info->at(i).get();
2098 if (Extension::IsExternalLocation(info->extension_location))
2099 CheckExternalUninstall(info->extension_id);
2100 }
[email protected]6c751e72010-11-23 10:11:102101}
2102
[email protected]a9f39a312010-12-23 22:14:272103void ExtensionService::UnloadExtension(
2104 const std::string& extension_id,
[email protected]814a7bf0f2011-08-13 05:30:592105 extension_misc::UnloadedExtensionReason reason) {
[email protected]27e469a2010-01-11 20:35:092106 // Make sure the extension gets deleted after we return from this function.
[email protected]9adb9692010-10-29 23:14:022107 scoped_refptr<const Extension> extension(
[email protected]8001df22011-04-28 19:59:472108 GetExtensionByIdInternal(extension_id, true, true, false));
[email protected]631cf822009-05-15 07:01:252109
[email protected]fa0c96732010-11-17 00:14:232110 // This method can be called via PostTask, so the extension may have been
2111 // unloaded by the time this runs.
[email protected]dd163fb02011-05-04 22:22:172112 if (!extension) {
2113 // In case the extension may have crashed/uninstalled. Allow the profile to
2114 // clean up its RequestContexts.
2115 profile_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:232116 return;
[email protected]dd163fb02011-05-04 22:22:172117 }
[email protected]0c6da502009-08-14 22:32:392118
[email protected]1eb175082010-02-10 09:26:162119 // Keep information about the extension so that we can reload it later
2120 // even if it's not permanently installed.
2121 unloaded_extension_paths_[extension->id()] = extension->path();
2122
[email protected]f17dbd42010-08-16 23:21:102123 // Clean up if the extension is meant to be enabled after a reload.
2124 disabled_extension_paths_.erase(extension->id());
2125
[email protected]d7e9a862010-11-03 21:57:492126 // Clean up runtime data.
2127 extension_runtime_data_.erase(extension_id);
2128
[email protected]0c6da502009-08-14 22:32:392129 ExtensionList::iterator iter = std::find(disabled_extensions_.begin(),
2130 disabled_extensions_.end(),
2131 extension.get());
2132 if (iter != disabled_extensions_.end()) {
[email protected]a9f39a312010-12-23 22:14:272133 UnloadedExtensionInfo details(extension, reason);
2134 details.already_disabled = true;
[email protected]0c6da502009-08-14 22:32:392135 disabled_extensions_.erase(iter);
[email protected]866930682009-08-18 22:53:472136 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272137 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]24e7a9d2009-11-04 11:11:342138 Source<Profile>(profile_),
[email protected]a9f39a312010-12-23 22:14:272139 Details<UnloadedExtensionInfo>(&details));
[email protected]dd163fb02011-05-04 22:22:172140 // Make sure the profile cleans up its RequestContexts when an already
2141 // disabled extension is unloaded (since they are also tracking the disabled
2142 // extensions).
2143 profile_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]0c6da502009-08-14 22:32:392144 return;
2145 }
2146
2147 iter = std::find(extensions_.begin(), extensions_.end(), extension.get());
[email protected]894bb502009-05-21 22:39:572148
[email protected]631cf822009-05-15 07:01:252149 // Remove the extension from our list.
2150 extensions_.erase(iter);
2151
[email protected]a9f39a312010-12-23 22:14:272152 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]631cf822009-05-15 07:01:252153}
2154
[email protected]eaa7dd182010-12-14 11:09:002155void ExtensionService::UnloadAllExtensions() {
[email protected]c8d407e2011-04-28 21:27:172156 profile_->GetExtensionSpecialStoragePolicy()->
2157 RevokeRightsForAllExtensions();
2158
[email protected]9f1087e2009-06-15 17:29:322159 extensions_.clear();
[email protected]cd500f72010-06-25 23:44:322160 disabled_extensions_.clear();
[email protected]bb7f40952011-01-13 00:21:202161 terminated_extension_ids_.clear();
2162 terminated_extensions_.clear();
[email protected]d7e9a862010-11-03 21:57:492163 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:322164
[email protected]c6e4a3412009-06-24 15:45:292165 // TODO(erikkay) should there be a notification for this? We can't use
2166 // EXTENSION_UNLOADED since that implies that the extension has been disabled
2167 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:322168}
2169
[email protected]eaa7dd182010-12-14 11:09:002170void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:322171 UnloadAllExtensions();
2172 LoadAllExtensions();
2173}
2174
[email protected]eaa7dd182010-12-14 11:09:002175void ExtensionService::GarbageCollectExtensions() {
[email protected]39d9f62c2010-12-03 10:48:502176 if (extension_prefs_->pref_service()->ReadOnly())
[email protected]ba399672010-04-06 15:42:392177 return;
2178
[email protected]ca3dbf52010-05-19 22:27:062179 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(
2180 extension_prefs_->GetInstalledExtensionsInfo());
2181
2182 std::map<std::string, FilePath> extension_paths;
2183 for (size_t i = 0; i < info->size(); ++i)
2184 extension_paths[info->at(i)->extension_id] = info->at(i)->extension_path;
2185
[email protected]14908b72011-04-20 06:54:362186 if (!BrowserThread::PostTask(
2187 BrowserThread::FILE, FROM_HERE,
2188 NewRunnableFunction(
2189 &extension_file_util::GarbageCollectExtensions,
2190 install_directory_,
2191 extension_paths)))
2192 NOTREACHED();
[email protected]f6ccd652010-11-17 00:48:342193
2194 // Also garbage-collect themes. We check |profile_| to be
2195 // defensive; in the future, we may call GarbageCollectExtensions()
2196 // from somewhere other than Init() (e.g., in a timer).
2197 if (profile_) {
[email protected]18280372011-03-22 18:05:222198 ThemeServiceFactory::GetForProfile(profile_)->RemoveUnusedThemes();
[email protected]f6ccd652010-11-17 00:48:342199 }
[email protected]3cf4f0992009-02-03 23:00:302200}
2201
[email protected]eaa7dd182010-12-14 11:09:002202void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]93fd78f42009-07-10 16:43:172203 if (updater_.get()) {
2204 updater_->Start();
2205 }
[email protected]fa6a9102010-11-22 15:38:502206
2207 ready_ = true;
[email protected]e72e8eb82009-06-18 17:21:512208 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272209 chrome::NOTIFICATION_EXTENSIONS_READY,
[email protected]24e7a9d2009-11-04 11:11:342210 Source<Profile>(profile_),
[email protected]e72e8eb82009-06-18 17:21:512211 NotificationService::NoDetails());
2212}
2213
[email protected]49cd1572011-02-08 21:38:452214void ExtensionService::AddExtension(const Extension* extension) {
[email protected]ae09ca62009-08-21 19:46:462215 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:022216 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]9f1087e2009-06-15 17:29:322217
[email protected]c8d407e2011-04-28 21:27:172218 // TODO(jstritar): We may be able to get rid of this branch by overriding the
2219 // default extension state to DISABLED when the --disable-extensions flag
2220 // is set (http://crbug.com/29067).
2221 if (!extensions_enabled() &&
2222 !extension->is_theme() &&
2223 extension->location() != Extension::COMPONENT &&
2224 !Extension::IsExternalLocation(extension->location()))
2225 return;
2226
2227 SetBeingUpgraded(extension, false);
2228
[email protected]1eb175082010-02-10 09:26:162229 // The extension is now loaded, remove its data from unloaded extension map.
2230 unloaded_extension_paths_.erase(extension->id());
2231
[email protected]bb7f40952011-01-13 00:21:202232 // If a terminated extension is loaded, remove it from the terminated list.
2233 UntrackTerminatedExtension(extension->id());
2234
[email protected]f17dbd42010-08-16 23:21:102235 // If the extension was disabled for a reload, then enable it.
2236 if (disabled_extension_paths_.erase(extension->id()) > 0)
2237 EnableExtension(extension->id());
2238
[email protected]d728e002010-12-08 04:46:232239 // Check if the extension's privileges have changed and disable the
2240 // extension if necessary.
[email protected]902fd7b2011-07-27 18:42:312241 InitializePermissions(extension);
[email protected]0c6da502009-08-14 22:32:392242
[email protected]ad83ca242011-07-29 01:32:252243 bool disabled = extension_prefs_->IsExtensionDisabled(extension->id());
[email protected]36429da2011-07-11 20:25:182244 if (disabled) {
[email protected]c8d407e2011-04-28 21:27:172245 disabled_extensions_.push_back(scoped_extension);
2246 // TODO(aa): This seems dodgy. It seems that AddExtension() could get called
2247 // with a disabled extension for other reasons other than that an update was
2248 // disabled.
2249 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272250 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
[email protected]c8d407e2011-04-28 21:27:172251 Source<Profile>(profile_),
2252 Details<const Extension>(extension));
[email protected]3bdba0d2011-08-23 07:17:302253 SyncExtensionChangeIfNeeded(*extension);
[email protected]c8d407e2011-04-28 21:27:172254 return;
[email protected]e72e8eb82009-06-18 17:21:512255 }
[email protected]aab98a52009-12-02 03:22:352256
[email protected]f1b66652011-08-25 23:30:582257 // Unfortunately, we used to set app launcher indices for non-apps. If this
2258 // extension has an index (page or in-page), set it to -1.
2259 if (!extension->is_app()) {
2260 if (extension_prefs_->GetAppLaunchIndex(extension->id()) != -1)
2261 extension_prefs_->SetAppLaunchIndex(extension->id(), -1);
2262 if (extension_prefs_->GetPageIndex(extension->id()) != -1)
2263 extension_prefs_->SetPageIndex(extension->id(), -1);
2264 }
2265
[email protected]c8d407e2011-04-28 21:27:172266 extensions_.push_back(scoped_extension);
[email protected]3bdba0d2011-08-23 07:17:302267 SyncExtensionChangeIfNeeded(*extension);
[email protected]c8d407e2011-04-28 21:27:172268 NotifyExtensionLoaded(extension);
[email protected]aab98a52009-12-02 03:22:352269}
2270
[email protected]902fd7b2011-07-27 18:42:312271void ExtensionService::InitializePermissions(const Extension* extension) {
2272 // If the extension has used the optional permissions API, it will have a
2273 // custom set of active permissions defined in the extension prefs. Here,
2274 // we update the extension's active permissions based on the prefs.
2275 scoped_refptr<ExtensionPermissionSet> active_permissions =
2276 extension_prefs()->GetActivePermissions(extension->id());
2277
2278 if (active_permissions.get()) {
2279 // We restrict the active permissions to be within the bounds defined in the
2280 // extension's manifest.
2281 // a) active permissions must be a subset of optional + default permissions
2282 // b) active permissions must contains all default permissions
2283 scoped_refptr<ExtensionPermissionSet> total_permissions =
2284 ExtensionPermissionSet::CreateUnion(
2285 extension->required_permission_set(),
2286 extension->optional_permission_set());
2287
2288 // Make sure the active permissions contain no more than optional + default.
2289 scoped_refptr<ExtensionPermissionSet> adjusted_active =
2290 ExtensionPermissionSet::CreateIntersection(
2291 total_permissions.get(), active_permissions.get());
2292
2293 // Make sure the active permissions contain the default permissions.
2294 adjusted_active = ExtensionPermissionSet::CreateUnion(
2295 extension->required_permission_set(), adjusted_active.get());
2296
2297 UpdateActivePermissions(extension, adjusted_active);
2298 }
2299
[email protected]8d888c12010-11-30 00:00:252300 // We keep track of all permissions the user has granted each extension.
2301 // This allows extensions to gracefully support backwards compatibility
2302 // by including unknown permissions in their manifests. When the user
2303 // installs the extension, only the recognized permissions are recorded.
2304 // When the unknown permissions become recognized (e.g., through browser
2305 // upgrade), we can prompt the user to accept these new permissions.
2306 // Extensions can also silently upgrade to less permissions, and then
2307 // silently upgrade to a version that adds these permissions back.
2308 //
2309 // For example, pretend that Chrome 10 includes a permission "omnibox"
2310 // for an API that adds suggestions to the omnibox. An extension can
2311 // maintain backwards compatibility while still having "omnibox" in the
2312 // manifest. If a user installs the extension on Chrome 9, the browser
2313 // will record the permissions it recognized, not including "omnibox."
2314 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
2315 // will disable the extension and prompt the user to approve the increase
2316 // in privileges. The extension could then release a new version that
2317 // removes the "omnibox" permission. When the user upgrades, Chrome will
2318 // still remember that "omnibox" had been granted, so that if the
2319 // extension once again includes "omnibox" in an upgrade, the extension
2320 // can upgrade without requiring this user's approval.
2321 const Extension* old = GetExtensionByIdInternal(extension->id(),
[email protected]8001df22011-04-28 19:59:472322 true, true, false);
[email protected]8d888c12010-11-30 00:00:252323 bool is_extension_upgrade = old != NULL;
2324 bool is_privilege_increase = false;
2325
[email protected]902fd7b2011-07-27 18:42:312326 // We only need to compare the granted permissions to the current permissions
2327 // if the extension is not allowed to silently increase its permissions.
2328 if (!extension->CanSilentlyIncreasePermissions()) {
[email protected]8d888c12010-11-30 00:00:252329 // Add all the recognized permissions if the granted permissions list
2330 // hasn't been initialized yet.
[email protected]902fd7b2011-07-27 18:42:312331 scoped_refptr<ExtensionPermissionSet> granted_permissions =
2332 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:522333 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:252334
2335 // Here, we check if an extension's privileges have increased in a manner
2336 // that requires the user's approval. This could occur because the browser
2337 // upgraded and recognized additional privileges, or an extension upgrades
2338 // to a version that requires additional privileges.
[email protected]0d3e4a22011-06-23 19:02:522339 is_privilege_increase =
[email protected]902fd7b2011-07-27 18:42:312340 granted_permissions->HasLessPrivilegesThan(
2341 extension->GetActivePermissions());
[email protected]8d888c12010-11-30 00:00:252342 }
2343
2344 if (is_extension_upgrade) {
[email protected]66e26872010-12-03 20:07:252345 // Other than for unpacked extensions, CrxInstaller should have guaranteed
2346 // that we aren't downgrading.
2347 if (extension->location() != Extension::LOAD)
2348 CHECK(extension->version()->CompareTo(*(old->version())) >= 0);
[email protected]8d888c12010-11-30 00:00:252349
2350 // Extensions get upgraded if the privileges are allowed to increase or
2351 // the privileges haven't increased.
2352 if (!is_privilege_increase) {
2353 SetBeingUpgraded(old, true);
2354 SetBeingUpgraded(extension, true);
2355 }
2356
2357 // To upgrade an extension in place, unload the old one and
2358 // then load the new one.
[email protected]814a7bf0f2011-08-13 05:30:592359 UnloadExtension(old->id(), extension_misc::UNLOAD_REASON_UPDATE);
[email protected]8d888c12010-11-30 00:00:252360 old = NULL;
2361 }
2362
2363 // Extension has changed permissions significantly. Disable it. A
2364 // notification should be sent by the caller.
2365 if (is_privilege_increase) {
[email protected]fe2dd7742011-04-19 22:52:492366 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
2367 RecordPermissionMessagesHistogram(
2368 extension, "Extensions.Permissions_AutoDisable");
2369 }
[email protected]b3317ad2011-04-28 23:46:002370 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:252371 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
2372 }
2373}
2374
[email protected]eaa7dd182010-12-14 11:09:002375void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:392376 std::set<std::string> extension_ids;
[email protected]aab98a52009-12-02 03:22:352377 for (size_t i = 0; i < extensions_.size(); ++i) {
[email protected]2a500c1b2010-11-05 15:27:342378 if (!extensions_[i]->is_theme() &&
2379 extensions_[i]->location() != Extension::COMPONENT)
[email protected]c8865962009-12-16 07:47:392380 extension_ids.insert(extensions_[i]->id());
[email protected]aab98a52009-12-02 03:22:352381 }
2382
2383 child_process_logging::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:252384}
2385
[email protected]e49002a2011-07-13 03:40:442386void ExtensionService::OnLoadSingleExtension(const Extension* extension,
2387 bool prompt_for_plugins) {
[email protected]5fb889382011-06-03 00:29:202388 // If this is a new install of an extension with plugins, prompt the user
2389 // first.
[email protected]e49002a2011-07-13 03:40:442390 if (show_extensions_prompts_ && prompt_for_plugins &&
[email protected]5fb889382011-06-03 00:29:202391 !extension->plugins().empty() &&
2392 disabled_extension_paths_.find(extension->id()) ==
2393 disabled_extension_paths_.end()) {
2394 SimpleExtensionLoadPrompt* prompt = new SimpleExtensionLoadPrompt(
2395 profile_, weak_ptr_factory_.GetWeakPtr(), extension);
2396 prompt->ShowPrompt();
2397 return; // continues in SimpleExtensionLoadPrompt::InstallUI*
2398 }
[email protected]f1b66652011-08-25 23:30:582399 OnExtensionInstalled(extension, false, -1); // Not from web store.
[email protected]5fb889382011-06-03 00:29:202400}
2401
[email protected]8266d662011-07-12 21:53:262402void ExtensionService::OnExtensionInstalled(
[email protected]cc829cf2011-08-22 21:01:352403 const Extension* extension, bool from_webstore, int page_index) {
[email protected]a29a517a2011-01-21 21:11:122404 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:202405
[email protected]4416c5a2010-06-26 01:28:572406 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:022407 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]c3cfb012011-04-06 22:07:352408 const std::string& id = extension->id();
[email protected]8a87a5332011-08-11 17:54:592409 // Extensions installed by policy can't be disabled. So even if a previous
2410 // installation disabled the extension, make sure it is now enabled.
2411 bool initial_enable =
2412 !extension_prefs_->IsExtensionDisabled(id) ||
2413 !Extension::UserMayDisable(extension->location());
[email protected]b2907fd2011-03-25 16:43:372414 PendingExtensionInfo pending_extension_info;
[email protected]c3cfb012011-04-06 22:07:352415 if (pending_extension_manager()->GetById(id, &pending_extension_info)) {
2416 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:322417
[email protected]483d1ff2011-03-03 17:12:022418 if (!pending_extension_info.ShouldAllowInstall(*extension)) {
[email protected]4416c5a2010-06-26 01:28:572419 LOG(WARNING)
[email protected]ae6cb3022011-04-04 20:11:542420 << "ShouldAllowInstall() returned false for "
[email protected]c3cfb012011-04-06 22:07:352421 << id << " of type " << extension->GetType()
[email protected]7fa19f82010-12-21 19:40:082422 << " and update URL " << extension->update_url().spec()
2423 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:542424
2425 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272426 chrome::NOTIFICATION_EXTENSION_INSTALL_NOT_ALLOWED,
[email protected]ae6cb3022011-04-04 20:11:542427 Source<Profile>(profile_),
2428 Details<const Extension>(extension));
2429
[email protected]4416c5a2010-06-26 01:28:572430 // Delete the extension directory since we're not going to
2431 // load it.
[email protected]14908b72011-04-20 06:54:362432 if (!BrowserThread::PostTask(
2433 BrowserThread::FILE, FROM_HERE,
2434 NewRunnableFunction(&extension_file_util::DeleteFile,
2435 extension->path(), true)))
2436 NOTREACHED();
[email protected]4416c5a2010-06-26 01:28:572437 return;
2438 }
[email protected]4416c5a2010-06-26 01:28:572439 } else {
[email protected]c3cfb012011-04-06 22:07:352440 // We explicitly want to re-enable an uninstalled external
2441 // extension; if we're here, that means the user is manually
2442 // installing the extension.
[email protected]6cc7dbae2011-04-29 21:18:332443 if (IsExternalExtensionUninstalled(id)) {
2444 initial_enable = true;
2445 }
[email protected]aa142702010-03-26 01:26:332446 }
2447
[email protected]07533022011-06-27 20:42:552448 // Do not record the install histograms for upgrades.
2449 if (!GetExtensionByIdInternal(extension->id(), true, true, false)) {
2450 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
2451 extension->GetType(), 100);
2452 RecordPermissionMessagesHistogram(
2453 extension, "Extensions.Permissions_Install");
2454 }
2455
[email protected]eda3c362010-11-12 08:08:232456 ShownSectionsHandler::OnExtensionInstalled(profile_->GetPrefs(), extension);
[email protected]4416c5a2010-06-26 01:28:572457 extension_prefs_->OnExtensionInstalled(
[email protected]8266d662011-07-12 21:53:262458 extension,
2459 initial_enable ? Extension::ENABLED : Extension::DISABLED,
[email protected]cc829cf2011-08-22 21:01:352460 from_webstore,
2461 page_index);
[email protected]25b34332009-06-05 21:53:192462
[email protected]cdfca9702011-08-08 16:07:012463 // Unpacked extensions default to allowing file access, but if that has been
2464 // overridden, don't reset the value.
[email protected]b6e64fd2011-08-09 19:49:192465 if (Extension::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]cdfca9702011-08-08 16:07:012466 !extension_prefs_->HasAllowFileAccessSetting(id)) {
2467 extension_prefs_->SetAllowFileAccess(id, true);
2468 }
2469
[email protected]a3e61e82011-04-15 20:32:082470 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272471 chrome::NOTIFICATION_EXTENSION_INSTALLED,
[email protected]a3e61e82011-04-15 20:32:082472 Source<Profile>(profile_),
2473 Details<const Extension>(extension));
[email protected]7577a5c52009-07-30 06:21:582474
[email protected]49cd1572011-02-08 21:38:452475 // Transfer ownership of |extension| to AddExtension.
2476 AddExtension(scoped_extension);
[email protected]4a190632009-05-09 01:07:422477}
2478
[email protected]eaa7dd182010-12-14 11:09:002479const Extension* ExtensionService::GetExtensionByIdInternal(
[email protected]8001df22011-04-28 19:59:472480 const std::string& id, bool include_enabled, bool include_disabled,
2481 bool include_terminated) const {
[email protected]e957fe52009-06-23 16:51:052482 std::string lowercase_id = StringToLowerASCII(id);
[email protected]0c6da502009-08-14 22:32:392483 if (include_enabled) {
2484 for (ExtensionList::const_iterator iter = extensions_.begin();
2485 iter != extensions_.end(); ++iter) {
2486 if ((*iter)->id() == lowercase_id)
2487 return *iter;
2488 }
2489 }
2490 if (include_disabled) {
2491 for (ExtensionList::const_iterator iter = disabled_extensions_.begin();
2492 iter != disabled_extensions_.end(); ++iter) {
2493 if ((*iter)->id() == lowercase_id)
2494 return *iter;
2495 }
[email protected]ce5c4502009-05-06 16:46:112496 }
[email protected]8001df22011-04-28 19:59:472497 if (include_terminated) {
2498 for (ExtensionList::const_iterator iter = terminated_extensions_.begin();
2499 iter != terminated_extensions_.end(); ++iter) {
2500 if ((*iter)->id() == lowercase_id)
2501 return *iter;
2502 }
2503 }
[email protected]ce5c4502009-05-06 16:46:112504 return NULL;
2505}
2506
[email protected]bb7f40952011-01-13 00:21:202507void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
2508 if (terminated_extension_ids_.insert(extension->id()).second)
2509 terminated_extensions_.push_back(make_scoped_refptr(extension));
[email protected]fa2416f2011-05-03 08:41:202510
[email protected]814a7bf0f2011-08-13 05:30:592511 UnloadExtension(extension->id(), extension_misc::UNLOAD_REASON_DISABLE);
[email protected]bb7f40952011-01-13 00:21:202512}
2513
2514void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]fa2416f2011-05-03 08:41:202515 std::string lowercase_id = StringToLowerASCII(id);
2516 if (terminated_extension_ids_.erase(lowercase_id) <= 0)
[email protected]bb7f40952011-01-13 00:21:202517 return;
2518
[email protected]bb7f40952011-01-13 00:21:202519 for (ExtensionList::iterator iter = terminated_extensions_.begin();
2520 iter != terminated_extensions_.end(); ++iter) {
2521 if ((*iter)->id() == lowercase_id) {
2522 terminated_extensions_.erase(iter);
2523 return;
2524 }
2525 }
2526}
2527
[email protected]0dfe05c2011-02-23 23:03:362528const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:472529 const std::string& id) const {
2530 return GetExtensionByIdInternal(id, false, false, true);
2531}
2532
2533const Extension* ExtensionService::GetInstalledExtension(
2534 const std::string& id) const {
2535 return GetExtensionByIdInternal(id, true, true, true);
[email protected]0dfe05c2011-02-23 23:03:362536}
2537
[email protected]eaa7dd182010-12-14 11:09:002538const Extension* ExtensionService::GetWebStoreApp() {
[email protected]d3071992010-10-08 15:24:072539 return GetExtensionById(extension_misc::kWebStoreAppId, false);
2540}
2541
[email protected]eaa7dd182010-12-14 11:09:002542const Extension* ExtensionService::GetExtensionByURL(const GURL& url) {
[email protected]a888b29e62010-04-01 13:38:572543 return url.scheme() != chrome::kExtensionScheme ? NULL :
2544 GetExtensionById(url.host(), false);
2545}
2546
[email protected]eaa7dd182010-12-14 11:09:002547const Extension* ExtensionService::GetExtensionByWebExtent(const GURL& url) {
[email protected]a888b29e62010-04-01 13:38:572548 for (size_t i = 0; i < extensions_.size(); ++i) {
[email protected]cced75a2011-05-20 08:31:122549 if (extensions_[i]->web_extent().MatchesURL(url))
[email protected]a888b29e62010-04-01 13:38:572550 return extensions_[i];
2551 }
2552 return NULL;
[email protected]9f1087e2009-06-15 17:29:322553}
2554
[email protected]eaa7dd182010-12-14 11:09:002555bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]056ad2a2011-07-12 02:13:552556 // Allow bindings for all packaged extensions.
2557 // Note that GetExtensionByURL may return an Extension for hosted apps
2558 // if the URL came from GetEffectiveURL.
2559 const Extension* extension = GetExtensionByURL(url);
2560 if (extension && extension->GetType() != Extension::TYPE_HOSTED_APP)
[email protected]583d45c12010-08-31 02:48:122561 return true;
2562
2563 // Allow bindings for all component, hosted apps.
[email protected]056ad2a2011-07-12 02:13:552564 if (!extension)
2565 extension = GetExtensionByWebExtent(url);
[email protected]583d45c12010-08-31 02:48:122566 return (extension && extension->location() == Extension::COMPONENT);
2567}
2568
[email protected]eaa7dd182010-12-14 11:09:002569const Extension* ExtensionService::GetExtensionByOverlappingWebExtent(
[email protected]cced75a2011-05-20 08:31:122570 const URLPatternSet& extent) {
[email protected]22c966c2010-06-26 06:35:022571 for (size_t i = 0; i < extensions_.size(); ++i) {
2572 if (extensions_[i]->web_extent().OverlapsWith(extent))
2573 return extensions_[i];
2574 }
2575
[email protected]6d2e60bd2010-06-03 22:37:392576 return NULL;
2577}
2578
[email protected]eaa7dd182010-12-14 11:09:002579const SkBitmap& ExtensionService::GetOmniboxIcon(
[email protected]b671760b2010-07-15 21:13:472580 const std::string& extension_id) {
2581 return omnibox_icon_manager_.GetIcon(extension_id);
2582}
2583
[email protected]eaa7dd182010-12-14 11:09:002584const SkBitmap& ExtensionService::GetOmniboxPopupIcon(
[email protected]29d0d4ac2010-09-08 21:10:312585 const std::string& extension_id) {
2586 return omnibox_popup_icon_manager_.GetIcon(extension_id);
2587}
2588
[email protected]eaa7dd182010-12-14 11:09:002589void ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322590 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:142591 const Version* version,
[email protected]8ef78fd2010-08-19 17:14:322592 const FilePath& path,
2593 Extension::Location location) {
[email protected]ab22ba42011-01-14 16:36:382594 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2595 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012596 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]683d0702010-12-06 16:25:572597 return;
[email protected]a8af9fdb2010-10-28 21:52:202598
[email protected]8e4560b62011-01-14 10:09:142599 DCHECK(version);
2600
[email protected]7577a5c52009-07-30 06:21:582601 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492602 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582603 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022604 const Extension* existing = GetExtensionById(id, true);
[email protected]7577a5c52009-07-30 06:21:582605 if (existing) {
[email protected]8e4560b62011-01-14 10:09:142606 switch (existing->version()->CompareTo(*version)) {
[email protected]7577a5c52009-07-30 06:21:582607 case -1: // existing version is older, we should upgrade
2608 break;
2609 case 0: // existing version is same, do nothing
2610 return;
2611 case 1: // existing version is newer, uh-oh
2612 LOG(WARNING) << "Found external version of extension " << id
2613 << "that is older than current version. Current version "
2614 << "is: " << existing->VersionString() << ". New version "
2615 << "is: " << version << ". Keeping current version.";
2616 return;
2617 }
2618 }
2619
[email protected]b2907fd2011-03-25 16:43:372620 pending_extension_manager()->AddFromExternalFile(id, location);
[email protected]9c635f22010-12-02 09:36:362621
[email protected]14908b72011-04-20 06:54:362622 // no client (silent install)
2623 scoped_refptr<CrxInstaller> installer(MakeCrxInstaller(NULL));
[email protected]6dfbbf82010-03-12 23:09:162624 installer->set_install_source(location);
2625 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072626 installer->set_expected_version(*version);
2627 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]6dfbbf82010-03-12 23:09:162628 installer->InstallCrx(path);
[email protected]7577a5c52009-07-30 06:21:582629}
2630
[email protected]eaa7dd182010-12-14 11:09:002631void ExtensionService::ReportExtensionLoadError(
[email protected]d11c8e92009-10-20 23:26:402632 const FilePath& extension_path,
2633 const std::string &error,
[email protected]432115822011-07-10 15:52:272634 int type,
[email protected]d11c8e92009-10-20 23:26:402635 bool be_noisy) {
2636 NotificationService* service = NotificationService::current();
2637 service->Notify(type,
[email protected]24e7a9d2009-11-04 11:11:342638 Source<Profile>(profile_),
[email protected]d11c8e92009-10-20 23:26:402639 Details<const std::string>(&error));
2640
[email protected]8a205c02011-02-04 20:41:332641 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]18d4b6c2010-09-21 03:21:042642 std::string message = base::StringPrintf(
2643 "Could not load extension from '%s'. %s",
2644 path_str.c_str(), error.c_str());
[email protected]d11c8e92009-10-20 23:26:402645 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2646}
2647
[email protected]eaa7dd182010-12-14 11:09:002648void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]406027c02010-09-27 08:03:182649 ExtensionHost* host) {
2650 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332651 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182652 if (iter == orphaned_dev_tools_.end())
2653 return;
2654
2655 DevToolsManager::GetInstance()->AttachClientHost(
2656 iter->second, host->render_view_host());
2657 orphaned_dev_tools_.erase(iter);
2658}
2659
[email protected]432115822011-07-10 15:52:272660void ExtensionService::Observe(int type,
[email protected]fa2416f2011-05-03 08:41:202661 const NotificationSource& source,
2662 const NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272663 switch (type) {
2664 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]bc535ee52010-08-31 18:40:322665 if (profile_ != Source<Profile>(source).ptr()->GetOriginalProfile())
2666 break;
[email protected]a4ed6282009-12-14 20:51:162667
[email protected]f128af42010-08-05 18:05:262668 ExtensionHost* host = Details<ExtensionHost>(details).ptr();
2669
[email protected]fa2416f2011-05-03 08:41:202670 // Mark the extension as terminated and Unload it. We want it to
2671 // be in a consistent state: either fully working or not loaded
2672 // at all, but never half-crashed. We do it in a PostTask so
2673 // that other handlers of this notification will still have
2674 // access to the Extension and ExtensionHost.
[email protected]14908b72011-04-20 06:54:362675 MessageLoop::current()->PostTask(
2676 FROM_HERE,
2677 method_factory_.NewRunnableMethod(
[email protected]fa2416f2011-05-03 08:41:202678 &ExtensionService::TrackTerminatedExtension,
2679 host->extension()));
[email protected]31f77262009-12-02 20:48:532680 break;
2681 }
[email protected]432115822011-07-10 15:52:272682 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
[email protected]77a6970c2011-04-23 16:58:562683 RenderProcessHost* process = Source<RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192684 Profile* host_profile =
2685 Profile::FromBrowserContext(process->browser_context());
2686 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2687 break;
2688
[email protected]77a6970c2011-04-23 16:58:562689 // Valid extension function names, used to setup bindings in renderer.
2690 std::vector<std::string> function_names;
2691 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names);
2692 process->Send(new ExtensionMsg_SetFunctionNames(function_names));
[email protected]31f77262009-12-02 20:48:532693
[email protected]77a6970c2011-04-23 16:58:562694 // Scripting whitelist. This is modified by tests and must be communicated
2695 // to renderers.
2696 process->Send(new ExtensionMsg_SetScriptingWhitelist(
2697 *Extension::GetScriptingWhitelist()));
2698
2699 // Loaded extensions.
2700 for (size_t i = 0; i < extensions_.size(); ++i) {
2701 process->Send(new ExtensionMsg_Loaded(
[email protected]15379642011-08-11 22:22:052702 ExtensionMsg_Loaded_Params(extensions_[i])));
[email protected]77a6970c2011-04-23 16:58:562703 }
2704 break;
2705 }
[email protected]432115822011-07-10 15:52:272706 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]da5683db2011-04-23 17:12:212707 RenderProcessHost* process = Source<RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192708 Profile* host_profile =
2709 Profile::FromBrowserContext(process->browser_context());
2710 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2711 break;
2712
[email protected]da5683db2011-04-23 17:12:212713 installed_app_hosts_.erase(process->id());
2714 break;
2715 }
[email protected]432115822011-07-10 15:52:272716 case chrome::NOTIFICATION_PREF_CHANGED: {
[email protected]aa96d3a2010-08-21 08:45:252717 std::string* pref_name = Details<std::string>(details).ptr();
[email protected]6c751e72010-11-23 10:11:102718 if (*pref_name == prefs::kExtensionInstallAllowList ||
2719 *pref_name == prefs::kExtensionInstallDenyList) {
2720 CheckAdminBlacklist();
[email protected]6c751e72010-11-23 10:11:102721 } else {
2722 NOTREACHED() << "Unexpected preference name.";
2723 }
[email protected]aa96d3a2010-08-21 08:45:252724 break;
2725 }
[email protected]d91268022011-08-26 13:17:372726 case chrome::NOTIFICATION_IMPORT_FINISHED: {
2727 registrar_.Remove(this, chrome::NOTIFICATION_IMPORT_FINISHED,
2728 Source<Profile>(profile_));
2729 InitEventRouters();
2730 break;
2731 }
[email protected]aa96d3a2010-08-21 08:45:252732
[email protected]4814b512009-11-07 00:12:292733 default:
2734 NOTREACHED() << "Unexpected notification type.";
2735 }
2736}
2737
[email protected]eaa7dd182010-12-14 11:09:002738bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182739 return !GetAppIds().empty();
2740}
[email protected]377011d2010-07-20 04:18:502741
[email protected]eaa7dd182010-12-14 11:09:002742ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182743 ExtensionIdSet result;
[email protected]377011d2010-07-20 04:18:502744 for (ExtensionList::const_iterator it = extensions_.begin();
2745 it != extensions_.end(); ++it) {
[email protected]ec5b50d2010-10-09 16:35:182746 if ((*it)->is_app() && (*it)->location() != Extension::COMPONENT)
2747 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502748 }
2749
[email protected]ec5b50d2010-10-09 16:35:182750 return result;
[email protected]377011d2010-07-20 04:18:502751}
[email protected]d7e9a862010-11-03 21:57:492752
[email protected]14908b72011-04-20 06:54:362753scoped_refptr<CrxInstaller> ExtensionService::MakeCrxInstaller(
2754 ExtensionInstallUI* client) {
2755 return new CrxInstaller(weak_ptr_factory_.GetWeakPtr(), client);
2756}
2757
[email protected]eaa7dd182010-12-14 11:09:002758bool ExtensionService::IsBackgroundPageReady(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492759 return (extension->background_url().is_empty() ||
2760 extension_runtime_data_[extension->id()].background_page_ready);
2761}
2762
[email protected]eaa7dd182010-12-14 11:09:002763void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492764 DCHECK(!extension->background_url().is_empty());
2765 extension_runtime_data_[extension->id()].background_page_ready = true;
2766 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272767 chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
[email protected]d7e9a862010-11-03 21:57:492768 Source<const Extension>(extension),
2769 NotificationService::NoDetails());
2770}
2771
[email protected]eaa7dd182010-12-14 11:09:002772bool ExtensionService::IsBeingUpgraded(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492773 return extension_runtime_data_[extension->id()].being_upgraded;
2774}
2775
[email protected]eaa7dd182010-12-14 11:09:002776void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]d7e9a862010-11-03 21:57:492777 bool value) {
2778 extension_runtime_data_[extension->id()].being_upgraded = value;
2779}
[email protected]1bead0712010-11-27 17:41:532780
[email protected]eaa7dd182010-12-14 11:09:002781PropertyBag* ExtensionService::GetPropertyBag(const Extension* extension) {
[email protected]1bead0712010-11-27 17:41:532782 return &extension_runtime_data_[extension->id()].property_bag;
2783}
[email protected]cebc3dc2011-04-18 17:15:002784
2785void ExtensionService::RegisterNaClModule(const GURL& url,
2786 const std::string& mime_type) {
2787 NaClModuleInfo info;
2788 info.url = url;
2789 info.mime_type = mime_type;
2790
2791 DCHECK(FindNaClModule(url) == nacl_module_list_.end());
2792 nacl_module_list_.push_front(info);
2793}
2794
2795void ExtensionService::UnregisterNaClModule(const GURL& url) {
2796 NaClModuleInfoList::iterator iter = FindNaClModule(url);
2797 DCHECK(iter != nacl_module_list_.end());
2798 nacl_module_list_.erase(iter);
2799}
2800
[email protected]ed0ba002011-05-26 16:55:132801void ExtensionService::UpdatePluginListWithNaClModules() {
[email protected]300c0ea2011-07-15 23:04:332802 // An extension has been added which has a nacl_module component, which means
2803 // there is a MIME type that module wants to handle, so we need to add that
2804 // MIME type to plugins which handle NaCl modules in order to allow the
2805 // individual modules to handle these types.
[email protected]ed0ba002011-05-26 16:55:132806 const PepperPluginInfo* pepper_info = NULL;
2807 std::vector<PepperPluginInfo> plugins;
2808 PepperPluginRegistry::ComputeList(&plugins);
[email protected]300c0ea2011-07-15 23:04:332809
2810 // Search the entire plugin list for plugins that handle the NaCl MIME type.
2811 // There can be multiple plugins like this, for instance the internal NaCl
2812 // plugin and a plugin registered by --register-pepper-plugins during tests.
[email protected]ed0ba002011-05-26 16:55:132813 for (size_t i = 0; i < plugins.size(); ++i) {
[email protected]300c0ea2011-07-15 23:04:332814 pepper_info = &plugins[i];
2815 CHECK(pepper_info);
[email protected]91d9f3d2011-08-14 05:24:442816 std::vector<webkit::WebPluginMimeType>::const_iterator mime_iter;
[email protected]300c0ea2011-07-15 23:04:332817 // Check each MIME type the plugins handle for the NaCl MIME type.
2818 for (mime_iter = pepper_info->mime_types.begin();
2819 mime_iter != pepper_info->mime_types.end(); ++mime_iter) {
2820 if (mime_iter->mime_type == kNaClPluginMimeType) {
2821 // This plugin handles "application/x-nacl".
2822
2823 webkit::npapi::PluginList::Singleton()->
2824 UnregisterInternalPlugin(pepper_info->path);
2825
[email protected]91d9f3d2011-08-14 05:24:442826 webkit::WebPluginInfo info = pepper_info->ToWebPluginInfo();
[email protected]300c0ea2011-07-15 23:04:332827
2828 for (ExtensionService::NaClModuleInfoList::const_iterator iter =
2829 nacl_module_list_.begin();
2830 iter != nacl_module_list_.end(); ++iter) {
2831 // Add the MIME type specified in the extension to this NaCl plugin,
2832 // With an extra "nacl" argument to specify the location of the NaCl
2833 // manifest file.
[email protected]91d9f3d2011-08-14 05:24:442834 webkit::WebPluginMimeType mime_type_info;
[email protected]300c0ea2011-07-15 23:04:332835 mime_type_info.mime_type = iter->mime_type;
2836 mime_type_info.additional_param_names.push_back(UTF8ToUTF16("nacl"));
2837 mime_type_info.additional_param_values.push_back(
2838 UTF8ToUTF16(iter->url.spec()));
2839 info.mime_types.push_back(mime_type_info);
2840 }
2841
2842 webkit::npapi::PluginList::Singleton()->RefreshPlugins();
2843 webkit::npapi::PluginList::Singleton()->RegisterInternalPlugin(info);
2844 // This plugin has been modified, no need to check the rest of its
2845 // types, but continue checking other plugins.
2846 break;
2847 }
[email protected]ed0ba002011-05-26 16:55:132848 }
2849 }
[email protected]ed0ba002011-05-26 16:55:132850}
2851
[email protected]cebc3dc2011-04-18 17:15:002852ExtensionService::NaClModuleInfoList::iterator
2853 ExtensionService::FindNaClModule(const GURL& url) {
2854 for (NaClModuleInfoList::iterator iter = nacl_module_list_.begin();
2855 iter != nacl_module_list_.end(); ++iter) {
2856 if (iter->url == url)
2857 return iter;
2858 }
2859 return nacl_module_list_.end();
2860}