blob: 3bbd9268243bcc9a66d7c5801cf5b94d8b62731b [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]e5af875f2011-10-10 21:09:1411#include "base/callback.h"
[email protected]62433d32011-10-12 22:33:1212#include "base/command_line.h"
[email protected]6014d672008-12-05 00:38:2513#include "base/file_util.h"
[email protected]005bab02011-10-07 18:36:0014#include "base/json/json_value_serializer.h"
[email protected]14908b72011-04-20 06:54:3615#include "base/logging.h"
[email protected]49098f702011-10-13 03:47:1816#include "base/metrics/field_trial.h"
[email protected]835d7c82010-10-14 04:38:3817#include "base/metrics/histogram.h"
[email protected]cebc3dc2011-04-18 17:15:0018#include "base/path_service.h"
[email protected]7286e3fc2011-07-19 22:13:2419#include "base/stl_util.h"
[email protected]24b538a2010-02-27 01:22:4420#include "base/string16.h"
[email protected]e83326f2010-07-31 17:29:2521#include "base/string_number_conversions.h"
[email protected]6014d672008-12-05 00:38:2522#include "base/string_util.h"
[email protected]18d4b6c2010-09-21 03:21:0423#include "base/stringprintf.h"
[email protected]34b99632011-01-01 01:01:0624#include "base/threading/thread_restrictions.h"
[email protected]cc2c3432009-11-06 17:24:3625#include "base/time.h"
[email protected]ce7f62e32010-08-10 23:43:5926#include "base/utf_string_conversions.h"
[email protected]cc655912009-01-29 23:19:1927#include "base/values.h"
[email protected]aa142702010-03-26 01:26:3328#include "base/version.h"
[email protected]15730c42009-09-03 00:03:2029#include "chrome/browser/browser_process.h"
[email protected]dfba8762011-09-02 12:49:5430#include "chrome/browser/chrome_plugin_service_filter.h"
[email protected]af9db5f2011-10-05 05:13:1531#include "chrome/browser/extensions/app_notification_manager.h"
[email protected]18cc5a52011-04-15 16:03:3832#include "chrome/browser/extensions/apps_promo.h"
[email protected]e0785902011-05-19 23:34:1733#include "chrome/browser/extensions/crx_installer.h"
[email protected]49098f702011-10-13 03:47:1834#include "chrome/browser/extensions/default_apps_trial.h"
[email protected]5cbe1e22010-01-30 01:18:5635#include "chrome/browser/extensions/extension_accessibility_api.h"
[email protected]840b0db2009-11-20 03:00:3836#include "chrome/browser/extensions/extension_bookmarks_module.h"
[email protected]b68d5ed2009-04-16 02:41:2837#include "chrome/browser/extensions/extension_browser_event_router.h"
[email protected]2c5e1e12010-06-10 13:14:4438#include "chrome/browser/extensions/extension_cookies_api.h"
[email protected]c10da4b02010-03-25 14:38:3239#include "chrome/browser/extensions/extension_data_deleter.h"
[email protected]370e9502011-09-14 19:31:4440#include "chrome/browser/extensions/extension_downloads_api.h"
[email protected]14a000d2010-04-29 21:44:2441#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]e5af875f2011-10-10 21:09:1442#include "chrome/browser/extensions/extension_global_error.h"
[email protected]de768a832009-10-30 05:25:0143#include "chrome/browser/extensions/extension_history_api.h"
[email protected]b1748b1d82009-11-30 20:32:5644#include "chrome/browser/extensions/extension_host.h"
[email protected]14da67b02011-08-02 05:49:1345#include "chrome/browser/extensions/extension_input_ime_api.h"
[email protected]5fb889382011-06-03 00:29:2046#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]7596ce72010-08-30 05:10:4647#include "chrome/browser/extensions/extension_management_api.h"
[email protected]118de7892011-03-16 13:31:4048#include "chrome/browser/extensions/extension_preference_api.h"
[email protected]4814b512009-11-07 00:12:2949#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]8a661f82010-10-19 21:47:1150#include "chrome/browser/extensions/extension_processes_api.h"
[email protected]19eb80152011-02-26 00:28:4351#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]90310d92011-04-17 07:35:0452#include "chrome/browser/extensions/extension_sync_data.h"
[email protected]93fd78f42009-07-10 16:43:1753#include "chrome/browser/extensions/extension_updater.h"
[email protected]8f9d4eb2011-02-05 01:39:1054#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]784688a62010-09-13 07:06:5255#include "chrome/browser/extensions/extension_webnavigation_api.h"
[email protected]8e4560b62011-01-14 10:09:1456#include "chrome/browser/extensions/external_extension_provider_impl.h"
[email protected]42b795652011-02-22 23:32:3857#include "chrome/browser/extensions/external_extension_provider_interface.h"
[email protected]b2907fd2011-03-25 16:43:3758#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]56ad3792010-05-28 17:45:3359#include "chrome/browser/net/chrome_url_request_context.h"
[email protected]37858e52010-08-26 00:22:0260#include "chrome/browser/prefs/pref_service.h"
[email protected]8ecad5e2010-12-02 21:18:3361#include "chrome/browser/profiles/profile.h"
[email protected]8e5c89a2011-06-07 18:13:3362#include "chrome/browser/search_engines/template_url_service.h"
63#include "chrome/browser/search_engines/template_url_service_factory.h"
[email protected]3bdba0d2011-08-23 07:17:3064#include "chrome/browser/sync/api/sync_change.h"
[email protected]18280372011-03-22 18:05:2265#include "chrome/browser/themes/theme_service.h"
[email protected]a0ea76c2011-03-23 17:36:4266#include "chrome/browser/themes/theme_service_factory.h"
[email protected]e5af875f2011-10-10 21:09:1467#include "chrome/browser/ui/browser.h"
[email protected]62433d32011-10-12 22:33:1268#include "chrome/browser/ui/browser_list.h"
[email protected]e5af875f2011-10-10 21:09:1469#include "chrome/browser/ui/global_error_service.h"
70#include "chrome/browser/ui/global_error_service_factory.h"
[email protected]c8d407e2011-04-28 21:27:1771#include "chrome/browser/ui/webui/chrome_url_data_manager.h"
72#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]7c5c6f3a2011-04-28 19:56:4973#include "chrome/browser/ui/webui/ntp/shown_sections_handler.h"
[email protected]aab98a52009-12-02 03:22:3574#include "chrome/common/child_process_logging.h"
[email protected]432115822011-07-10 15:52:2775#include "chrome/common/chrome_notification_types.h"
[email protected]cebc3dc2011-04-18 17:15:0076#include "chrome/common/chrome_paths.h"
[email protected]e2eb43112009-05-29 21:19:5477#include "chrome/common/chrome_switches.h"
[email protected]5b1a0e22009-05-26 19:00:5878#include "chrome/common/extensions/extension.h"
[email protected]d7b36dc2009-10-29 21:47:4079#include "chrome/common/extensions/extension_constants.h"
[email protected]05c82182010-06-24 17:49:0880#include "chrome/common/extensions/extension_error_utils.h"
[email protected]7c927b62010-02-24 09:54:1381#include "chrome/common/extensions/extension_file_util.h"
[email protected]c6d474f82009-12-16 21:11:0682#include "chrome/common/extensions/extension_l10n_util.h"
[email protected]77a6970c2011-04-23 16:58:5683#include "chrome/common/extensions/extension_messages.h"
[email protected]a315ba92010-11-16 14:12:2184#include "chrome/common/extensions/extension_resource.h"
[email protected]25b34332009-06-05 21:53:1985#include "chrome/common/pref_names.h"
[email protected]a57209872009-05-04 22:53:1486#include "chrome/common/url_constants.h"
[email protected]5f945a0e2011-03-01 17:47:5387#include "content/browser/browser_thread.h"
[email protected]b46442d7e2011-06-29 02:16:0688#include "content/browser/debugger/devtools_manager.h"
[email protected]2de307592011-04-05 21:16:5889#include "content/browser/plugin_process_host.h"
90#include "content/browser/plugin_service.h"
[email protected]77a6970c2011-04-23 16:58:5691#include "content/browser/renderer_host/render_process_host.h"
[email protected]333b1de2011-09-12 18:28:5092#include "content/browser/user_metrics.h"
[email protected]b3841c502011-03-09 01:21:3193#include "content/common/notification_service.h"
[email protected]cebc3dc2011-04-18 17:15:0094#include "content/common/pepper_plugin_registry.h"
[email protected]0d6e9bd2011-10-18 04:29:1695#include "content/public/browser/notification_types.h"
[email protected]c10da4b02010-03-25 14:38:3296#include "googleurl/src/gurl.h"
[email protected]1debbbb62010-10-06 17:23:4497#include "net/base/registry_controlled_domain.h"
[email protected]24b538a2010-02-27 01:22:4498#include "webkit/database/database_tracker.h"
99#include "webkit/database/database_util.h"
[email protected]2de307592011-04-05 21:16:58100#include "webkit/plugins/npapi/plugin_list.h"
[email protected]c64631652009-04-29 22:24:31101
[email protected]eed367e2011-04-12 03:43:31102#if defined(OS_CHROMEOS)
[email protected]14da67b02011-08-02 05:49:13103#include "chrome/browser/chromeos/cros/cros_library.h"
[email protected]eed367e2011-04-12 03:43:31104#include "chrome/browser/chromeos/extensions/file_browser_event_router.h"
[email protected]be5bd5742011-07-14 07:21:01105#include "chrome/browser/chromeos/extensions/input_method_event_router.h"
[email protected]ce9802042011-05-27 10:11:40106#include "chrome/browser/chromeos/extensions/media_player_event_router.h"
[email protected]14da67b02011-08-02 05:49:13107#include "chrome/browser/chromeos/input_method/input_method_manager.h"
108#include "chrome/browser/extensions/extension_input_ime_api.h"
[email protected]b777b332011-04-16 04:01:08109#include "webkit/fileapi/file_system_context.h"
110#include "webkit/fileapi/file_system_mount_point_provider.h"
111#include "webkit/fileapi/file_system_path_manager.h"
[email protected]eed367e2011-04-12 03:43:31112#endif
113
[email protected]27072cad2011-05-09 19:46:40114#if defined(OS_CHROMEOS) && defined(TOUCH_UI)
115#include "chrome/browser/extensions/extension_input_ui_api.h"
116#endif
117
[email protected]5ef47ec2010-01-28 05:58:05118using base::Time;
119
[email protected]c6d474f82009-12-16 21:11:06120namespace errors = extension_manifest_errors;
121
[email protected]b6ab96d2009-08-20 18:58:19122namespace {
123
[email protected]29d0d4ac2010-09-08 21:10:31124#if defined(OS_LINUX)
125static const int kOmniboxIconPaddingLeft = 2;
126static const int kOmniboxIconPaddingRight = 2;
127#elif defined(OS_MACOSX)
128static const int kOmniboxIconPaddingLeft = 0;
129static const int kOmniboxIconPaddingRight = 2;
130#else
131static const int kOmniboxIconPaddingLeft = 0;
132static const int kOmniboxIconPaddingRight = 0;
133#endif
134
[email protected]300c0ea2011-07-15 23:04:33135const char* kNaClPluginMimeType = "application/x-nacl";
136
[email protected]a315ba92010-11-16 14:12:21137// The following enumeration is used in histograms matching
138// Extensions.ManifestReload* . Values may be added, as long
139// as existing values are not changed.
140enum ManifestReloadReason {
141 NOT_NEEDED = 0, // Reload not needed.
142 UNPACKED_DIR, // Unpacked directory
143 NEEDS_RELOCALIZATION, // The local has changed since we read this extension.
144 NUM_MANIFEST_RELOAD_REASONS
145};
[email protected]2111b1a2010-03-12 18:12:44146
[email protected]a315ba92010-11-16 14:12:21147ManifestReloadReason ShouldReloadExtensionManifest(const ExtensionInfo& info) {
148 // Always reload manifests of unpacked extensions, because they can change
149 // on disk independent of the manifest in our prefs.
150 if (info.extension_location == Extension::LOAD)
151 return UNPACKED_DIR;
152
153 // Reload the manifest if it needs to be relocalized.
154 if (extension_l10n_util::ShouldRelocalizeManifest(info))
155 return NEEDS_RELOCALIZATION;
156
157 return NOT_NEEDED;
[email protected]2111b1a2010-03-12 18:12:44158}
159
[email protected]2de307592011-04-05 21:16:58160static void ForceShutdownPlugin(const FilePath& plugin_path) {
161 PluginProcessHost* plugin =
162 PluginService::GetInstance()->FindNpapiPluginProcess(plugin_path);
163 if (plugin)
164 plugin->ForceShutdown();
165}
166
[email protected]3bdba0d2011-08-23 07:17:30167static bool IsSyncableExtension(const Extension& extension) {
168 return extension.GetSyncType() == Extension::SYNC_TYPE_EXTENSION;
169}
170
171static bool IsSyncableApp(const Extension& extension) {
172 return extension.GetSyncType() == Extension::SYNC_TYPE_APP;
173}
174
[email protected]5fb889382011-06-03 00:29:20175// Manages an ExtensionInstallUI for a particular extension.
176class SimpleExtensionLoadPrompt : public ExtensionInstallUI::Delegate {
177 public:
178 SimpleExtensionLoadPrompt(Profile* profile,
179 base::WeakPtr<ExtensionService> extension_service,
180 const Extension* extension);
181 ~SimpleExtensionLoadPrompt();
182
183 void ShowPrompt();
184
185 // ExtensionInstallUI::Delegate
186 virtual void InstallUIProceed();
[email protected]d828fac2011-06-28 05:43:04187 virtual void InstallUIAbort(bool user_initiated);
[email protected]5fb889382011-06-03 00:29:20188
189 private:
190 base::WeakPtr<ExtensionService> extension_service_;
191 scoped_ptr<ExtensionInstallUI> install_ui_;
192 scoped_refptr<const Extension> extension_;
193};
194
195SimpleExtensionLoadPrompt::SimpleExtensionLoadPrompt(
196 Profile* profile,
197 base::WeakPtr<ExtensionService> extension_service,
198 const Extension* extension)
199 : extension_service_(extension_service),
200 install_ui_(new ExtensionInstallUI(profile)),
201 extension_(extension) {
202}
203
204SimpleExtensionLoadPrompt::~SimpleExtensionLoadPrompt() {
205}
206
207void SimpleExtensionLoadPrompt::ShowPrompt() {
208 install_ui_->ConfirmInstall(this, extension_);
209}
210
211void SimpleExtensionLoadPrompt::InstallUIProceed() {
212 if (extension_service_.get())
[email protected]8266d662011-07-12 21:53:26213 extension_service_->OnExtensionInstalled(
[email protected]f1b66652011-08-25 23:30:58214 extension_, false, -1); // Not from web store.
[email protected]5fb889382011-06-03 00:29:20215 delete this;
216}
217
[email protected]d828fac2011-06-28 05:43:04218void SimpleExtensionLoadPrompt::InstallUIAbort(bool user_initiated) {
[email protected]5fb889382011-06-03 00:29:20219 delete this;
220}
221
[email protected]c6d474f82009-12-16 21:11:06222} // namespace
[email protected]b6ab96d2009-08-20 18:58:19223
[email protected]ad63a792011-06-16 06:41:11224bool ExtensionService::ComponentExtensionInfo::Equals(
225 const ComponentExtensionInfo& other) const {
226 return other.manifest == manifest && other.root_directory == root_directory;
227}
228
[email protected]eaa7dd182010-12-14 11:09:00229ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49230 : background_page_ready(false),
231 being_upgraded(false) {
232}
233
[email protected]eaa7dd182010-12-14 11:09:00234ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49235}
236
[email protected]cebc3dc2011-04-18 17:15:00237ExtensionService::NaClModuleInfo::NaClModuleInfo() {
238}
239
240ExtensionService::NaClModuleInfo::~NaClModuleInfo() {
241}
242
[email protected]eaa7dd182010-12-14 11:09:00243// ExtensionService.
[email protected]6014d672008-12-05 00:38:25244
[email protected]eaa7dd182010-12-14 11:09:00245const char* ExtensionService::kInstallDirectoryName = "Extensions";
[email protected]b7f853e282011-08-10 09:24:20246const char* ExtensionService::kSettingsDirectoryName = "Extension Settings";
[email protected]494c06e2009-07-25 01:06:42247
[email protected]eaa7dd182010-12-14 11:09:00248// Implements IO for the ExtensionService.
[email protected]7a4c6852010-09-16 03:44:22249
[email protected]eaa7dd182010-12-14 11:09:00250class ExtensionServiceBackend
[email protected]8e4560b62011-01-14 10:09:14251 : public base::RefCountedThreadSafe<ExtensionServiceBackend> {
[email protected]7a4c6852010-09-16 03:44:22252 public:
[email protected]1f830eb2010-09-28 08:25:14253 // |install_directory| is a path where to look for extensions to load.
[email protected]14908b72011-04-20 06:54:36254 ExtensionServiceBackend(
255 base::WeakPtr<ExtensionService> frontend,
256 const FilePath& install_directory);
[email protected]7a4c6852010-09-16 03:44:22257
258 // Loads a single extension from |path| where |path| is the top directory of
[email protected]e49002a2011-07-13 03:40:44259 // a specific extension where its manifest file lives. If |prompt_for_plugins|
260 // is true and the extension contains plugins, we prompt the user before
261 // loading.
[email protected]7a4c6852010-09-16 03:44:22262 // Errors are reported through ExtensionErrorReporter. On success,
[email protected]49cd1572011-02-08 21:38:45263 // AddExtension() is called.
[email protected]7a4c6852010-09-16 03:44:22264 // TODO(erikkay): It might be useful to be able to load a packed extension
265 // (presumably into memory) without installing it.
[email protected]e49002a2011-07-13 03:40:44266 void LoadSingleExtension(const FilePath &path, bool prompt_for_plugins);
[email protected]7a4c6852010-09-16 03:44:22267
[email protected]7a4c6852010-09-16 03:44:22268 private:
[email protected]eaa7dd182010-12-14 11:09:00269 friend class base::RefCountedThreadSafe<ExtensionServiceBackend>;
[email protected]7a4c6852010-09-16 03:44:22270
[email protected]eaa7dd182010-12-14 11:09:00271 virtual ~ExtensionServiceBackend();
[email protected]7a4c6852010-09-16 03:44:22272
[email protected]d10f4602011-06-16 15:44:50273 // LoadSingleExtension needs to check the file access preference, which needs
274 // to happen back on the UI thread, so it posts CheckExtensionFileAccess on
275 // the UI thread. In turn, once that gets the pref, it goes back to the
276 // file thread with LoadSingleExtensionWithFileAccess.
[email protected]e49002a2011-07-13 03:40:44277 void CheckExtensionFileAccess(const FilePath& extension_path,
278 bool prompt_for_plugins);
[email protected]d10f4602011-06-16 15:44:50279 void LoadSingleExtensionWithFileAccess(
[email protected]e49002a2011-07-13 03:40:44280 const FilePath &path, bool allow_file_access, bool prompt_for_plugins);
[email protected]d10f4602011-06-16 15:44:50281
[email protected]7a4c6852010-09-16 03:44:22282 // Notify the frontend that there was an error loading an extension.
283 void ReportExtensionLoadError(const FilePath& extension_path,
284 const std::string& error);
285
[email protected]14908b72011-04-20 06:54:36286 // Notify the frontend that an extension was installed.
[email protected]e49002a2011-07-13 03:40:44287 void OnLoadSingleExtension(const scoped_refptr<const Extension>& extension,
288 bool prompt_for_plugins);
[email protected]14908b72011-04-20 06:54:36289
290 base::WeakPtr<ExtensionService> frontend_;
[email protected]7a4c6852010-09-16 03:44:22291
292 // The top-level extensions directory being installed to.
293 FilePath install_directory_;
294
[email protected]eaa7dd182010-12-14 11:09:00295 DISALLOW_COPY_AND_ASSIGN(ExtensionServiceBackend);
[email protected]7a4c6852010-09-16 03:44:22296};
297
[email protected]eaa7dd182010-12-14 11:09:00298ExtensionServiceBackend::ExtensionServiceBackend(
[email protected]14908b72011-04-20 06:54:36299 base::WeakPtr<ExtensionService> frontend,
[email protected]aebe23a32010-12-10 22:15:48300 const FilePath& install_directory)
[email protected]14908b72011-04-20 06:54:36301 : frontend_(frontend),
302 install_directory_(install_directory) {
303 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]7a4c6852010-09-16 03:44:22304}
305
[email protected]eaa7dd182010-12-14 11:09:00306ExtensionServiceBackend::~ExtensionServiceBackend() {
[email protected]14908b72011-04-20 06:54:36307 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
308 BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]7a4c6852010-09-16 03:44:22309}
310
[email protected]e49002a2011-07-13 03:40:44311void ExtensionServiceBackend::LoadSingleExtension(const FilePath& path_in,
312 bool prompt_for_plugins) {
[email protected]a29a517a2011-01-21 21:11:12313 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]a8af9fdb2010-10-28 21:52:20314
[email protected]7a4c6852010-09-16 03:44:22315 FilePath extension_path = path_in;
316 file_util::AbsolutePath(&extension_path);
317
[email protected]e49002a2011-07-13 03:40:44318 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
[email protected]704d77e2011-10-17 18:05:29319 NewRunnableMethod(this,
320 &ExtensionServiceBackend::CheckExtensionFileAccess,
321 extension_path, prompt_for_plugins));
[email protected]d10f4602011-06-16 15:44:50322}
323
324void ExtensionServiceBackend::CheckExtensionFileAccess(
[email protected]e49002a2011-07-13 03:40:44325 const FilePath& extension_path, bool prompt_for_plugins) {
[email protected]d10f4602011-06-16 15:44:50326 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
327 std::string id = Extension::GenerateIdForPath(extension_path);
[email protected]cdfca9702011-08-08 16:07:01328 // Unpacked extensions default to allowing file access, but if that has been
329 // overridden, don't reset the value.
330 bool allow_file_access =
331 Extension::ShouldAlwaysAllowFileAccess(Extension::LOAD);
332 if (frontend_->extension_prefs()->HasAllowFileAccessSetting(id))
333 allow_file_access = frontend_->extension_prefs()->AllowFileAccess(id);
[email protected]d10f4602011-06-16 15:44:50334
[email protected]e49002a2011-07-13 03:40:44335 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
[email protected]704d77e2011-10-17 18:05:29336 NewRunnableMethod(
337 this,
[email protected]e49002a2011-07-13 03:40:44338 &ExtensionServiceBackend::LoadSingleExtensionWithFileAccess,
[email protected]704d77e2011-10-17 18:05:29339 extension_path, allow_file_access, prompt_for_plugins));
[email protected]d10f4602011-06-16 15:44:50340}
341
[email protected]d10f4602011-06-16 15:44:50342void ExtensionServiceBackend::LoadSingleExtensionWithFileAccess(
[email protected]e49002a2011-07-13 03:40:44343 const FilePath& extension_path,
344 bool allow_file_access,
345 bool prompt_for_plugins) {
[email protected]d10f4602011-06-16 15:44:50346 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
347 int flags = allow_file_access ?
[email protected]3aff9ad2011-04-01 20:26:48348 Extension::ALLOW_FILE_ACCESS : Extension::NO_FLAGS;
[email protected]83048a22011-03-29 00:14:13349 if (Extension::ShouldDoStrictErrorChecking(Extension::LOAD))
350 flags |= Extension::STRICT_ERROR_CHECKS;
[email protected]7a4c6852010-09-16 03:44:22351 std::string error;
[email protected]ad8e04a2010-11-01 04:16:27352 scoped_refptr<const Extension> extension(extension_file_util::LoadExtension(
[email protected]7a4c6852010-09-16 03:44:22353 extension_path,
[email protected]92888082010-10-18 19:24:57354 Extension::LOAD,
[email protected]83048a22011-03-29 00:14:13355 flags,
[email protected]ad8e04a2010-11-01 04:16:27356 &error));
[email protected]7a4c6852010-09-16 03:44:22357
358 if (!extension) {
[email protected]e49002a2011-07-13 03:40:44359 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
[email protected]704d77e2011-10-17 18:05:29360 NewRunnableMethod(
[email protected]8a4fd81b2011-10-17 17:27:31361 this,
[email protected]704d77e2011-10-17 18:05:29362 &ExtensionServiceBackend::ReportExtensionLoadError,
[email protected]e49002a2011-07-13 03:40:44363 extension_path, error));
[email protected]7a4c6852010-09-16 03:44:22364 return;
365 }
366
[email protected]7a4c6852010-09-16 03:44:22367 // Report this as an installed extension so that it gets remembered in the
368 // prefs.
[email protected]e49002a2011-07-13 03:40:44369 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
[email protected]704d77e2011-10-17 18:05:29370 NewRunnableMethod(
371 this,
[email protected]e49002a2011-07-13 03:40:44372 &ExtensionServiceBackend::OnLoadSingleExtension,
[email protected]704d77e2011-10-17 18:05:29373 extension, prompt_for_plugins));
[email protected]7a4c6852010-09-16 03:44:22374}
375
[email protected]eaa7dd182010-12-14 11:09:00376void ExtensionServiceBackend::ReportExtensionLoadError(
[email protected]7a4c6852010-09-16 03:44:22377 const FilePath& extension_path, const std::string &error) {
[email protected]14908b72011-04-20 06:54:36378 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
379 if (frontend_.get())
380 frontend_->ReportExtensionLoadError(
[email protected]655b2b1a2011-10-13 17:13:06381 extension_path, error, true /* alert_on_error */);
[email protected]14908b72011-04-20 06:54:36382}
383
[email protected]5fb889382011-06-03 00:29:20384void ExtensionServiceBackend::OnLoadSingleExtension(
[email protected]e49002a2011-07-13 03:40:44385 const scoped_refptr<const Extension>& extension, bool prompt_for_plugins) {
[email protected]14908b72011-04-20 06:54:36386 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
387 if (frontend_.get())
[email protected]e49002a2011-07-13 03:40:44388 frontend_->OnLoadSingleExtension(extension, prompt_for_plugins);
[email protected]7a4c6852010-09-16 03:44:22389}
390
[email protected]8e4560b62011-01-14 10:09:14391void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12392 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20393
[email protected]7a4c6852010-09-16 03:44:22394 // Check if the providers know about this extension.
[email protected]0a60a2e2010-10-25 16:15:21395 ProviderCollection::const_iterator i;
396 for (i = external_extension_providers_.begin();
397 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14398 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21399 if (i->get()->HasExtension(id))
400 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22401 }
402
[email protected]0f48fca2011-05-19 18:46:35403 // We get the list of external extensions to check from preferences.
404 // It is possible that an extension has preferences but is not loaded.
405 // For example, an extension that requires experimental permissions
406 // will not be loaded if the experimental command line flag is not used.
407 // In this case, do not uninstall.
408 const Extension* extension = GetInstalledExtension(id);
409 if (!extension) {
410 // We can't call UninstallExtension with an unloaded/invalid
411 // extension ID.
412 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
413 << "with id: " << id;
414 return;
415 }
[email protected]d6ebc9792011-04-07 18:18:33416 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22417}
418
[email protected]8e4560b62011-01-14 10:09:14419void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22420 external_extension_providers_.clear();
421}
422
[email protected]8e4560b62011-01-14 10:09:14423void ExtensionService::AddProviderForTesting(
424 ExternalExtensionProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12425 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21426 external_extension_providers_.push_back(
[email protected]8e4560b62011-01-14 10:09:14427 linked_ptr<ExternalExtensionProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22428}
429
[email protected]8e4560b62011-01-14 10:09:14430void ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22431 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42432 const GURL& update_url,
433 Extension::Location location) {
[email protected]ab22ba42011-01-14 16:36:38434 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
435 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20436
[email protected]8a87a5332011-08-11 17:54:59437 const Extension* extension = GetExtensionById(id, true);
438 if (extension) {
439 // Already installed. Skip this install if the current location has
440 // higher priority than |location|.
441 Extension::Location current = extension->location();
442 if (current == Extension::GetHigherPriorityLocation(current, location))
443 return;
444 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22445 }
[email protected]b2907fd2011-03-25 16:43:37446 pending_extension_manager()->AddFromExternalUpdateUrl(
447 id, update_url, location);
[email protected]a29a517a2011-01-21 21:11:12448 external_extension_url_added_ |= true;
[email protected]7a4c6852010-09-16 03:44:22449}
450
[email protected]454434f2011-08-24 04:00:00451// If a download url matches one of these patterns and has a referrer of the
452// webstore, then we're willing to treat that as a gallery download.
453static const char* kAllowedDownloadURLPatterns[] = {
454 "https://clients2.google.com/service/update2*",
455 "https://clients2.googleusercontent.com/crx/*"
456};
457
[email protected]eaa7dd182010-12-14 11:09:00458bool ExtensionService::IsDownloadFromGallery(const GURL& download_url,
[email protected]a29a517a2011-01-21 21:11:12459 const GURL& referrer_url) {
[email protected]d3071992010-10-08 15:24:07460 // Special-case the themes mini-gallery.
461 // TODO(erikkay) When that gallery goes away, remove this code.
462 if (IsDownloadFromMiniGallery(download_url) &&
463 StartsWithASCII(referrer_url.spec(),
464 extension_urls::kMiniGalleryBrowsePrefix, false)) {
[email protected]334e04a2010-06-24 23:34:44465 return true;
[email protected]1debbbb62010-10-06 17:23:44466 }
[email protected]473ff6e2010-05-12 15:31:55467
[email protected]9adb9692010-10-29 23:14:02468 const Extension* download_extension = GetExtensionByWebExtent(download_url);
469 const Extension* referrer_extension = GetExtensionByWebExtent(referrer_url);
470 const Extension* webstore_app = GetWebStoreApp();
[email protected]d3071992010-10-08 15:24:07471
472 bool referrer_valid = (referrer_extension == webstore_app);
473 bool download_valid = (download_extension == webstore_app);
474
[email protected]454434f2011-08-24 04:00:00475 // We also allow the download to be from a small set of trusted paths.
476 if (!download_valid) {
477 for (size_t i = 0; i < arraysize(kAllowedDownloadURLPatterns); i++) {
478 URLPattern pattern(URLPattern::SCHEME_HTTPS,
479 kAllowedDownloadURLPatterns[i]);
480 if (pattern.MatchesURL(download_url)) {
481 download_valid = true;
482 break;
483 }
484 }
485 }
486
[email protected]d3071992010-10-08 15:24:07487 // If the command-line gallery URL is set, then be a bit more lenient.
488 GURL store_url =
489 GURL(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
490 switches::kAppsGalleryURL));
491 if (!store_url.is_empty()) {
492 std::string store_tld =
493 net::RegistryControlledDomainService::GetDomainAndRegistry(store_url);
494 if (!referrer_valid) {
495 std::string referrer_tld =
496 net::RegistryControlledDomainService::GetDomainAndRegistry(
497 referrer_url);
498 // The referrer gets stripped when transitioning from https to http,
499 // or when hitting an unknown test cert and that commonly happens in
500 // testing environments. Given this, we allow an empty referrer when
501 // the command-line flag is set.
502 // Otherwise, the TLD must match the TLD of the command-line url.
503 referrer_valid = referrer_url.is_empty() || (referrer_tld == store_tld);
504 }
505
506 if (!download_valid) {
507 std::string download_tld =
508 net::RegistryControlledDomainService::GetDomainAndRegistry(
[email protected]04866c42011-05-03 20:03:50509 download_url);
[email protected]d3071992010-10-08 15:24:07510
511 // Otherwise, the TLD must match the TLD of the command-line url.
512 download_valid = (download_tld == store_tld);
513 }
514 }
515
516 return (referrer_valid && download_valid);
[email protected]b7c2f252009-12-08 00:47:23517}
518
[email protected]eaa7dd182010-12-14 11:09:00519bool ExtensionService::IsDownloadFromMiniGallery(const GURL& download_url) {
[email protected]ac025282009-12-16 19:16:38520 return StartsWithASCII(download_url.spec(),
521 extension_urls::kMiniGalleryDownloadPrefix,
522 false); // case_sensitive
523}
524
[email protected]d9696672011-03-15 22:45:09525const Extension* ExtensionService::GetInstalledApp(const GURL& url) {
[email protected]ffb204f22010-12-05 23:20:27526 // Check for hosted app.
[email protected]d9696672011-03-15 22:45:09527 const Extension* app = GetExtensionByWebExtent(url);
528 if (app)
529 return app;
[email protected]ffb204f22010-12-05 23:20:27530
531 // Check for packaged app.
[email protected]d9696672011-03-15 22:45:09532 app = GetExtensionByURL(url);
533 if (app && app->is_app())
534 return app;
535
536 return NULL;
537}
538
539bool ExtensionService::IsInstalledApp(const GURL& url) {
540 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27541}
542
[email protected]da5683db2011-04-23 17:12:21543void ExtensionService::SetInstalledAppForRenderer(int renderer_child_id,
544 const Extension* app) {
545 installed_app_hosts_[renderer_child_id] = app;
546}
547
548const Extension* ExtensionService::GetInstalledAppForRenderer(
549 int renderer_child_id) {
550 InstalledAppMap::iterator i = installed_app_hosts_.find(renderer_child_id);
551 if (i == installed_app_hosts_.end())
552 return NULL;
553 return i->second;
554}
555
[email protected]6aeac8342010-10-01 20:21:18556// static
[email protected]d6ebc9792011-04-07 18:18:33557// This function is used to implement the command-line switch
[email protected]3bdba0d2011-08-23 07:17:30558// --uninstall-extension, and to uninstall an extension via sync. The LOG
559// statements within this function are used to inform the user if the uninstall
560// cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00561bool ExtensionService::UninstallExtensionHelper(
562 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18563 const std::string& extension_id) {
[email protected]6aeac8342010-10-01 20:21:18564
[email protected]95da88c42011-03-31 10:07:33565 const Extension* extension =
[email protected]8001df22011-04-28 19:59:47566 extensions_service->GetInstalledExtension(extension_id);
[email protected]95da88c42011-03-31 10:07:33567
[email protected]d6ebc9792011-04-07 18:18:33568 // We can't call UninstallExtension with an invalid extension ID.
[email protected]95da88c42011-03-31 10:07:33569 if (!extension) {
[email protected]6aeac8342010-10-01 20:21:18570 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33571 << "id: " << extension_id;
572 return false;
[email protected]6aeac8342010-10-01 20:21:18573 }
574
[email protected]d6ebc9792011-04-07 18:18:33575 // The following call to UninstallExtension will not allow an uninstall of a
576 // policy-controlled extension.
577 std::string error;
578 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
579 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
580 << ": " << error;
581 return false;
582 }
[email protected]95da88c42011-03-31 10:07:33583
[email protected]6aeac8342010-10-01 20:21:18584 return true;
585}
586
[email protected]eaa7dd182010-12-14 11:09:00587ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24588 const CommandLine* command_line,
589 const FilePath& install_directory,
590 ExtensionPrefs* extension_prefs,
[email protected]0436b102011-04-15 18:30:03591 bool autoupdate_enabled,
592 bool extensions_enabled)
[email protected]14908b72011-04-20 06:54:36593 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]704d77e2011-10-17 18:05:29594 method_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]14908b72011-04-20 06:54:36595 profile_(profile),
[email protected]73c47932010-12-06 18:13:43596 extension_prefs_(extension_prefs),
[email protected]fc84e0e2011-09-27 19:18:22597 extension_settings_frontend_(
[email protected]adfc5462011-09-21 19:07:13598 profile->GetPath().AppendASCII(kSettingsDirectoryName)),
[email protected]14908b72011-04-20 06:54:36599 pending_extension_manager_(*ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]a9b00ac2009-06-25 21:03:23600 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03601 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13602 show_extensions_prompts_(true),
[email protected]3ecda252010-11-18 19:50:55603 ready_(false),
[email protected]14908b72011-04-20 06:54:36604 toolbar_model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]8b280302011-10-13 22:22:23605 menu_manager_(profile),
[email protected]af9db5f2011-10-05 05:13:15606 app_notification_manager_(new AppNotificationManager(profile)),
[email protected]902fd7b2011-07-27 18:42:31607 permissions_manager_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]18cc5a52011-04-15 16:03:38608 apps_promo_(profile->GetPrefs()),
[email protected]655b2b1a2011-10-13 17:13:06609 webstore_installer_(profile),
[email protected]7c1490da2011-10-11 18:53:25610 event_routers_initialized_(false),
611 extension_warnings_(profile) {
[email protected]a29a517a2011-01-21 21:11:12612 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20613
[email protected]36a784c2009-06-23 06:21:08614 // Figure out if extension installation should be enabled.
[email protected]6d60703b2009-08-29 01:29:23615 if (command_line->HasSwitch(switches::kDisableExtensions)) {
616 extensions_enabled_ = false;
617 } else if (profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
618 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18619 }
[email protected]36a784c2009-06-23 06:21:08620
[email protected]432115822011-07-10 15:52:27621 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]2f48b8d92011-10-17 15:14:35622 NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27623 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
[email protected]2f48b8d92011-10-17 15:14:35624 NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27625 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]2f48b8d92011-10-17 15:14:35626 NotificationService::AllBrowserContextsAndSources());
[email protected]2fb7dc982010-09-29 12:24:28627 pref_change_registrar_.Init(profile->GetPrefs());
628 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, this);
629 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, this);
[email protected]4814b512009-11-07 00:12:29630
[email protected]93fd78f42009-07-10 16:43:17631 // Set up the ExtensionUpdater
632 if (autoupdate_enabled) {
633 int update_frequency = kDefaultUpdateFrequencySeconds;
634 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25635 base::StringToInt(command_line->GetSwitchValueASCII(
636 switches::kExtensionsUpdateFrequency),
637 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17638 }
[email protected]82b6e512011-04-12 20:33:27639 updater_.reset(new ExtensionUpdater(this,
640 extension_prefs,
641 profile->GetPrefs(),
642 profile,
643 update_frequency));
[email protected]93fd78f42009-07-10 16:43:17644 }
645
[email protected]14908b72011-04-20 06:54:36646 backend_ =
647 new ExtensionServiceBackend(weak_ptr_factory_.GetWeakPtr(),
648 install_directory_);
[email protected]8e4560b62011-01-14 10:09:14649
[email protected]af9db5f2011-10-05 05:13:15650 app_notification_manager_->Init();
651
[email protected]0436b102011-04-15 18:30:03652 if (extensions_enabled_) {
[email protected]873531342011-03-09 12:16:05653 ExternalExtensionProviderImpl::CreateExternalProviders(
654 this, profile_, &external_extension_providers_);
655 }
[email protected]b671760b2010-07-15 21:13:47656
[email protected]aa96d3a2010-08-21 08:45:25657 // Use monochrome icons for Omnibox icons.
[email protected]29d0d4ac2010-09-08 21:10:31658 omnibox_popup_icon_manager_.set_monochrome(true);
[email protected]b671760b2010-07-15 21:13:47659 omnibox_icon_manager_.set_monochrome(true);
[email protected]29d0d4ac2010-09-08 21:10:31660 omnibox_icon_manager_.set_padding(gfx::Insets(0, kOmniboxIconPaddingLeft,
661 0, kOmniboxIconPaddingRight));
[email protected]cb0e50312011-05-09 15:03:07662
663 // How long is the path to the Extensions directory?
664 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
665 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25666}
667
[email protected]eaa7dd182010-12-14 11:09:00668const ExtensionList* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45669 return &extensions_;
670}
671
[email protected]eaa7dd182010-12-14 11:09:00672const ExtensionList* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45673 return &disabled_extensions_;
674}
675
[email protected]bb7f40952011-01-13 00:21:20676const ExtensionList* ExtensionService::terminated_extensions() const {
677 return &terminated_extensions_;
678}
679
[email protected]b2907fd2011-03-25 16:43:37680PendingExtensionManager* ExtensionService::pending_extension_manager() {
681 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45682}
683
[email protected]ad63a792011-06-16 06:41:11684void ExtensionService::UnregisterComponentExtension(
685 const ComponentExtensionInfo& info) {
686 RegisteredComponentExtensions new_component_extension_manifests;
687 for (RegisteredComponentExtensions::iterator it =
688 component_extension_manifests_.begin();
689 it != component_extension_manifests_.end(); ++it) {
690 if (!it->Equals(info))
691 new_component_extension_manifests.push_back(*it);
692 }
693 component_extension_manifests_.swap(new_component_extension_manifests);
694}
695
[email protected]eaa7dd182010-12-14 11:09:00696ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17697 // No need to unload extensions here because they are profile-scoped, and the
698 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17699
[email protected]8e4560b62011-01-14 10:09:14700 ProviderCollection::const_iterator i;
701 for (i = external_extension_providers_.begin();
702 i != external_extension_providers_.end(); ++i) {
703 ExternalExtensionProviderInterface* provider = i->get();
704 provider->ServiceShutdown();
705 }
[email protected]6014d672008-12-05 00:38:25706}
707
[email protected]d91268022011-08-26 13:17:37708void ExtensionService::InitEventRoutersAfterImport() {
709 registrar_.Add(this, chrome::NOTIFICATION_IMPORT_FINISHED,
710 Source<Profile>(profile_));
711}
712
[email protected]eaa7dd182010-12-14 11:09:00713void ExtensionService::InitEventRouters() {
[email protected]b3d62312b12010-10-14 21:10:18714 if (event_routers_initialized_)
715 return;
716
[email protected]370e9502011-09-14 19:31:44717 downloads_event_router_.reset(new ExtensionDownloadsEventRouter(profile_));
[email protected]f825df22011-06-28 17:36:10718 history_event_router_.reset(new ExtensionHistoryEventRouter());
719 history_event_router_->ObserveProfile(profile_);
[email protected]97d2f1d2011-01-15 00:41:08720 browser_event_router_.reset(new ExtensionBrowserEventRouter(profile_));
721 browser_event_router_->Init();
[email protected]118de7892011-03-16 13:31:40722 preference_event_router_.reset(new ExtensionPreferenceEventRouter(profile_));
[email protected]f825df22011-06-28 17:36:10723 bookmark_event_router_.reset(new ExtensionBookmarkEventRouter(
724 profile_->GetBookmarkModel()));
725 bookmark_event_router_->Init();
726 cookies_event_router_.reset(new ExtensionCookiesEventRouter(profile_));
727 cookies_event_router_->Init();
728 management_event_router_.reset(new ExtensionManagementEventRouter(profile_));
729 management_event_router_->Init();
[email protected]7da9a8232011-05-27 21:07:11730 ExtensionProcessesEventRouter::GetInstance()->ObserveProfile(profile_);
[email protected]f825df22011-06-28 17:36:10731 web_navigation_event_router_.reset(
732 new ExtensionWebNavigationEventRouter(profile_));
733 web_navigation_event_router_->Init();
[email protected]27072cad2011-05-09 19:46:40734
[email protected]eed367e2011-04-12 03:43:31735#if defined(OS_CHROMEOS)
[email protected]b16fc2e2011-07-21 21:32:58736 file_browser_event_router_.reset(
737 new ExtensionFileBrowserEventRouter(profile_));
738 file_browser_event_router_->ObserveFileSystemEvents();
[email protected]771a6182011-10-05 16:32:52739
740 input_method_event_router_.reset(
741 new chromeos::ExtensionInputMethodEventRouter);
[email protected]be5bd5742011-07-14 07:21:01742
[email protected]c8bf09be2011-06-25 03:22:27743 ExtensionMediaPlayerEventRouter::GetInstance()->Init(profile_);
[email protected]14da67b02011-08-02 05:49:13744 ExtensionInputImeEventRouter::GetInstance()->Init();
[email protected]eed367e2011-04-12 03:43:31745#endif
[email protected]27072cad2011-05-09 19:46:40746
747#if defined(OS_CHROMEOS) && defined(TOUCH_UI)
748 ExtensionInputUiEventRouter::GetInstance()->Init();
749#endif
750
[email protected]b3d62312b12010-10-14 21:10:18751 event_routers_initialized_ = true;
[email protected]c5ae74ab2010-04-15 18:14:37752}
753
[email protected]b2907fd2011-03-25 16:43:37754const Extension* ExtensionService::GetExtensionById(
755 const std::string& id, bool include_disabled) const {
[email protected]8001df22011-04-28 19:59:47756 return GetExtensionByIdInternal(id, true, include_disabled, false);
[email protected]78994ab02010-12-08 18:06:44757}
758
[email protected]eaa7dd182010-12-14 11:09:00759void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12760 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20761
[email protected]fa6a9102010-11-22 15:38:50762 DCHECK(!ready_); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17763 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32764
[email protected]95dd38f2009-10-20 20:09:15765 // Hack: we need to ensure the ResourceDispatcherHost is ready before we load
766 // the first extension, because its members listen for loaded notifications.
767 g_browser_process->resource_dispatcher_host();
768
[email protected]9f1087e2009-06-15 17:29:32769 LoadAllExtensions();
[email protected]894bb502009-05-21 22:39:57770
[email protected]9f1087e2009-06-15 17:29:32771 // TODO(erikkay) this should probably be deferred to a future point
772 // rather than running immediately at startup.
[email protected]93fd78f42009-07-10 16:43:17773 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57774
[email protected]9f1087e2009-06-15 17:29:32775 // TODO(erikkay) this should probably be deferred as well.
776 GarbageCollectExtensions();
[email protected]6014d672008-12-05 00:38:25777}
778
[email protected]420a0ec2011-06-01 01:07:03779bool ExtensionService::UpdateExtension(
780 const std::string& id,
781 const FilePath& extension_path,
782 const GURL& download_url,
783 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12784 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20785
[email protected]b2907fd2011-03-25 16:43:37786 PendingExtensionInfo pending_extension_info;
787 bool is_pending_extension = pending_extension_manager_.GetById(
788 id, &pending_extension_info);
[email protected]8ef78fd2010-08-19 17:14:32789
[email protected]8001df22011-04-28 19:59:47790 const Extension* extension =
791 GetExtensionByIdInternal(id, true, true, false);
[email protected]5eb375e92010-11-26 07:50:41792 if (!is_pending_extension && !extension) {
[email protected]aa142702010-03-26 01:26:33793 LOG(WARNING) << "Will not update extension " << id
794 << " because it is not installed or pending";
795 // Delete extension_path since we're not creating a CrxInstaller
796 // that would do it for us.
[email protected]14908b72011-04-20 06:54:36797 if (!BrowserThread::PostTask(
798 BrowserThread::FILE, FROM_HERE,
[email protected]704d77e2011-10-17 18:05:29799 NewRunnableFunction(
[email protected]14908b72011-04-20 06:54:36800 extension_file_util::DeleteFile, extension_path, false)))
801 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03802
803 return false;
[email protected]e957fe52009-06-23 16:51:05804 }
805
[email protected]aa142702010-03-26 01:26:33806 // We want a silent install only for non-pending extensions and
807 // pending extensions that have install_silently set.
808 ExtensionInstallUI* client =
[email protected]b2907fd2011-03-25 16:43:37809 (!is_pending_extension || pending_extension_info.install_silently()) ?
[email protected]aa142702010-03-26 01:26:33810 NULL : new ExtensionInstallUI(profile_);
811
[email protected]14908b72011-04-20 06:54:36812 scoped_refptr<CrxInstaller> installer(MakeCrxInstaller(client));
[email protected]6dfbbf82010-03-12 23:09:16813 installer->set_expected_id(id);
[email protected]ec5b50d2010-10-09 16:35:18814 if (is_pending_extension)
[email protected]b2907fd2011-03-25 16:43:37815 installer->set_install_source(pending_extension_info.install_source());
[email protected]5eb375e92010-11-26 07:50:41816 else if (extension)
817 installer->set_install_source(extension->location());
[email protected]0d3e4a22011-06-23 19:02:52818 if (pending_extension_info.install_silently())
819 installer->set_allow_silent_install(true);
[email protected]3d729722011-09-20 02:57:09820 if (extension && extension->from_webstore())
821 installer->set_is_gallery_install(true);
[email protected]6dfbbf82010-03-12 23:09:16822 installer->set_delete_source(true);
[email protected]655b2b1a2011-10-13 17:13:06823 installer->set_download_url(download_url);
[email protected]cb0e50312011-05-09 15:03:07824 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16825 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03826
827 if (out_crx_installer)
828 *out_crx_installer = installer;
829
830 return true;
[email protected]e957fe52009-06-23 16:51:05831}
832
[email protected]eaa7dd182010-12-14 11:09:00833void ExtensionService::ReloadExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12834 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b65272f2009-08-31 15:47:06835 FilePath path;
[email protected]9adb9692010-10-29 23:14:02836 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40837
[email protected]f17dbd42010-08-16 23:21:10838 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06839 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29840 // If the extension has an inspector open for its background page, detach
841 // the inspector and hang onto a cookie for it, so that we can reattach
842 // later.
843 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
844 ExtensionHost* host = manager->GetBackgroundHostForExtension(
845 current_extension);
846 if (host) {
847 // Look for an open inspector for the background page.
848 int devtools_cookie = DevToolsManager::GetInstance()->DetachClientHost(
849 host->render_view_host());
850 if (devtools_cookie >= 0)
851 orphaned_dev_tools_[extension_id] = devtools_cookie;
852 }
853
[email protected]b65272f2009-08-31 15:47:06854 path = current_extension->path();
[email protected]f17dbd42010-08-16 23:21:10855 DisableExtension(extension_id);
856 disabled_extension_paths_[extension_id] = path;
[email protected]1eb175082010-02-10 09:26:16857 } else {
858 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06859 }
860
[email protected]e6090e42010-03-23 22:44:08861 // Check the installed extensions to see if what we're reloading was already
862 // installed.
863 scoped_ptr<ExtensionInfo> installed_extension(
864 extension_prefs_->GetInstalledExtensionInfo(extension_id));
865 if (installed_extension.get() &&
866 installed_extension->extension_manifest.get()) {
867 LoadInstalledExtension(*installed_extension, false);
868 } else {
869 // We should always be able to remember the extension's path. If it's not in
870 // the map, someone failed to update |unloaded_extension_paths_|.
871 CHECK(!path.empty());
872 LoadExtension(path);
873 }
[email protected]9cddd4702009-07-27 22:09:40874}
875
[email protected]fa2416f2011-05-03 08:41:20876bool ExtensionService::UninstallExtension(
877 const std::string& extension_id_unsafe,
878 bool external_uninstall,
879 std::string* error) {
[email protected]a29a517a2011-01-21 21:11:12880 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20881
[email protected]fa2416f2011-05-03 08:41:20882 // Copy the extension identifier since the reference might have been
883 // obtained via Extension::id() and the extension may be deleted in
884 // this function.
885 std::string extension_id(extension_id_unsafe);
886
[email protected]0d6ec3a72011-09-02 02:09:43887 scoped_refptr<const Extension> extension(GetInstalledExtension(extension_id));
[email protected]631cf822009-05-15 07:01:25888
[email protected]e7afe2452010-08-22 16:19:13889 // Callers should not send us nonexistent extensions.
[email protected]aebe23a32010-12-10 22:15:48890 CHECK(extension);
[email protected]9f1087e2009-06-15 17:29:32891
[email protected]95da88c42011-03-31 10:07:33892 // Policy change which triggers an uninstall will always set
893 // |external_uninstall| to true so this is the only way to uninstall
894 // managed extensions.
[email protected]0d6ec3a72011-09-02 02:09:43895 if (!Extension::UserMayDisable(extension->location()) &&
896 !external_uninstall) {
[email protected]d6ebc9792011-04-07 18:18:33897 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27898 chrome::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]d6ebc9792011-04-07 18:18:33899 Source<Profile>(profile_),
900 Details<const Extension>(extension));
901 if (error != NULL) {
902 *error = errors::kCannotUninstallManagedExtension;
903 }
904 return false;
905 }
[email protected]95da88c42011-03-31 10:07:33906
[email protected]3bdba0d2011-08-23 07:17:30907 // Extract the data we need for sync now, but don't actually sync until we've
908 // completed the uninstallation.
909 SyncBundle* sync_bundle = GetSyncBundleForExtension(*extension);
910
911 SyncChange sync_change;
912 if (sync_bundle) {
913 ExtensionSyncData extension_sync_data(*extension,
914 IsExtensionEnabled(extension_id),
915 IsIncognitoEnabled(extension_id));
916 sync_change = extension_sync_data.GetSyncChange(SyncChange::ACTION_DELETE);
917 }
918
[email protected]9b217652010-10-08 22:04:23919 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08920 extension->GetType(), 100);
[email protected]fe2dd7742011-04-19 22:52:49921 RecordPermissionMessagesHistogram(
922 extension, "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23923
[email protected]8e5c89a2011-06-07 18:13:33924 TemplateURLService* url_service =
925 TemplateURLServiceFactory::GetForProfile(profile_);
926 if (url_service)
927 url_service->UnregisterExtensionKeyword(extension);
[email protected]56ad3792010-05-28 17:45:33928
[email protected]831aa212010-03-26 13:55:19929 // Unload before doing more cleanup to ensure that nothing is hanging on to
930 // any of these resources.
[email protected]814a7bf0f2011-08-13 05:30:59931 UnloadExtension(extension_id, extension_misc::UNLOAD_REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19932
[email protected]0d6ec3a72011-09-02 02:09:43933 extension_prefs_->OnExtensionUninstalled(extension_id, extension->location(),
[email protected]831aa212010-03-26 13:55:19934 external_uninstall);
[email protected]9f1087e2009-06-15 17:29:32935
936 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]0d6ec3a72011-09-02 02:09:43937 if (Extension::LOAD != extension->location()) {
[email protected]14908b72011-04-20 06:54:36938 if (!BrowserThread::PostTask(
939 BrowserThread::FILE, FROM_HERE,
[email protected]704d77e2011-10-17 18:05:29940 NewRunnableFunction(
[email protected]14908b72011-04-20 06:54:36941 &extension_file_util::UninstallExtension,
942 install_directory_,
[email protected]fa2416f2011-05-03 08:41:20943 extension_id)))
[email protected]14908b72011-04-20 06:54:36944 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32945 }
946
[email protected]0d6ec3a72011-09-02 02:09:43947 GURL launch_web_url_origin(extension->launch_web_url());
948 launch_web_url_origin = launch_web_url_origin.GetOrigin();
949 bool is_storage_isolated =
950 (extension->is_storage_isolated() &&
951 extension->HasAPIPermission(ExtensionAPIPermission::kExperimental));
952
953 if (extension->is_hosted_app() &&
954 !profile_->GetExtensionSpecialStoragePolicy()->
955 IsStorageProtected(launch_web_url_origin)) {
[email protected]dc0b5a12011-10-14 00:06:13956 ExtensionDataDeleter::StartDeleting(
957 profile_, extension_id, launch_web_url_origin, is_storage_isolated);
[email protected]0d6ec3a72011-09-02 02:09:43958 }
[email protected]dc0b5a12011-10-14 00:06:13959 ExtensionDataDeleter::StartDeleting(
960 profile_, extension_id, extension->url(), is_storage_isolated);
[email protected]0d6ec3a72011-09-02 02:09:43961
[email protected]0dfe05c2011-02-23 23:03:36962 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06963
964 // Notify interested parties that we've uninstalled this extension.
965 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27966 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]211030342010-09-30 18:41:06967 Source<Profile>(profile_),
[email protected]3e873e52011-10-10 00:30:25968 Details<const std::string>(&extension_id));
[email protected]d6ebc9792011-04-07 18:18:33969
[email protected]3bdba0d2011-08-23 07:17:30970 if (sync_bundle && sync_bundle->HasExtensionId(extension_id)) {
971 sync_bundle->sync_processor->ProcessSyncChanges(
972 FROM_HERE, SyncChangeList(1, sync_change));
973 sync_bundle->synced_extensions.erase(extension_id);
974 }
975
[email protected]333b1de2011-09-12 18:28:50976 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18977 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
978
979 static bool default_apps_trial_exists =
980 base::FieldTrialList::TrialExists(kDefaultAppsTrial_Name);
981 if (default_apps_trial_exists) {
982 UMA_HISTOGRAM_ENUMERATION(
983 base::FieldTrial::MakeName("Extensions.ExtensionUninstalled",
984 kDefaultAppsTrial_Name),
985 1, 2);
986 }
[email protected]333b1de2011-09-12 18:28:50987
[email protected]7c1490da2011-10-11 18:53:25988 // Uninstalling one extension might have solved the problems of others.
989 // Therefore, we clear warnings of this type for all extensions.
990 std::set<ExtensionWarningSet::WarningType> warnings;
991 extension_warnings_.GetWarningsAffectingExtension(extension_id, &warnings);
992 extension_warnings_.ClearWarnings(warnings);
993
[email protected]d6ebc9792011-04-07 18:18:33994 return true;
[email protected]c10da4b02010-03-25 14:38:32995}
996
[email protected]c3cfb012011-04-06 22:07:35997bool ExtensionService::IsExtensionEnabled(
998 const std::string& extension_id) const {
[email protected]36429da2011-07-11 20:25:18999 const Extension* extension =
[email protected]ad83ca242011-07-29 01:32:251000 GetExtensionByIdInternal(extension_id, true, false, true);
[email protected]36429da2011-07-11 20:25:181001 if (extension)
1002 return true;
1003
[email protected]ad83ca242011-07-29 01:32:251004 extension =
1005 GetExtensionByIdInternal(extension_id, false, true, false);
1006 if (extension)
1007 return false;
1008
1009 // If the extension hasn't been loaded yet, check the prefs for it. Assume
1010 // enabled unless otherwise noted.
1011 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
1012 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:351013}
1014
1015bool ExtensionService::IsExternalExtensionUninstalled(
1016 const std::string& extension_id) const {
1017 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
1018}
1019
[email protected]eaa7dd182010-12-14 11:09:001020void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:121021 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:201022
[email protected]06f92562011-04-29 19:27:311023 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:391024 return;
[email protected]0c6da502009-08-14 22:32:391025
[email protected]b3317ad2011-04-28 23:46:001026 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]1784e83a2009-09-08 21:01:521027
[email protected]06f92562011-04-29 19:27:311028 const Extension* extension =
1029 GetExtensionByIdInternal(extension_id, false, true, false);
1030 // This can happen if sync enables an extension that is not
1031 // installed yet.
1032 if (!extension)
1033 return;
1034
[email protected]0c6da502009-08-14 22:32:391035 // Move it over to the enabled list.
[email protected]00cd9c42010-11-02 20:15:571036 extensions_.push_back(make_scoped_refptr(extension));
[email protected]0c6da502009-08-14 22:32:391037 ExtensionList::iterator iter = std::find(disabled_extensions_.begin(),
1038 disabled_extensions_.end(),
1039 extension);
1040 disabled_extensions_.erase(iter);
1041
[email protected]f681c782010-11-19 11:19:391042 // Make sure any browser action contained within it is not hidden.
1043 extension_prefs_->SetBrowserActionVisibility(extension, true);
1044
[email protected]62d30f42009-10-01 22:36:061045 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:301046
1047 SyncExtensionChangeIfNeeded(*extension);
[email protected]0c6da502009-08-14 22:32:391048}
1049
[email protected]eaa7dd182010-12-14 11:09:001050void ExtensionService::DisableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:121051 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:201052
[email protected]b2ba9962009-12-10 20:10:151053 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:311054 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:521055 return;
[email protected]1784e83a2009-09-08 21:01:521056
[email protected]06f92562011-04-29 19:27:311057 const Extension* extension = GetInstalledExtension(extension_id);
1058 // |extension| can be NULL if sync disables an extension that is not
1059 // installed yet.
1060 if (extension && !Extension::UserMayDisable(extension->location()))
[email protected]95da88c42011-03-31 10:07:331061 return;
1062
[email protected]b3317ad2011-04-28 23:46:001063 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]1784e83a2009-09-08 21:01:521064
[email protected]06f92562011-04-29 19:27:311065 extension = GetExtensionByIdInternal(extension_id, true, false, true);
1066 if (!extension)
1067 return;
1068
[email protected]1784e83a2009-09-08 21:01:521069 // Move it over to the disabled list.
[email protected]00cd9c42010-11-02 20:15:571070 disabled_extensions_.push_back(make_scoped_refptr(extension));
[email protected]1784e83a2009-09-08 21:01:521071 ExtensionList::iterator iter = std::find(extensions_.begin(),
1072 extensions_.end(),
1073 extension);
[email protected]06f92562011-04-29 19:27:311074 if (iter != extensions_.end()) {
1075 extensions_.erase(iter);
1076 } else {
1077 iter = std::find(terminated_extensions_.begin(),
1078 terminated_extensions_.end(),
1079 extension);
1080 terminated_extensions_.erase(iter);
1081 }
[email protected]1784e83a2009-09-08 21:01:521082
[email protected]814a7bf0f2011-08-13 05:30:591083 NotifyExtensionUnloaded(extension, extension_misc::UNLOAD_REASON_DISABLE);
[email protected]3bdba0d2011-08-23 07:17:301084
1085 SyncExtensionChangeIfNeeded(*extension);
[email protected]7c1490da2011-10-11 18:53:251086
1087 // Deactivating one extension might have solved the problems of others.
1088 // Therefore, we clear warnings of this type for all extensions.
1089 std::set<ExtensionWarningSet::WarningType> warnings;
1090 extension_warnings_.GetWarningsAffectingExtension(extension_id, &warnings);
1091 extension_warnings_.ClearWarnings(warnings);
[email protected]1784e83a2009-09-08 21:01:521092}
1093
[email protected]eaa7dd182010-12-14 11:09:001094void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]8d888c12010-11-30 00:00:251095 CHECK(extension);
1096
[email protected]902fd7b2011-07-27 18:42:311097 // We only maintain the granted permissions prefs for extensions that can't
1098 // silently increase their permissions.
1099 if (extension->CanSilentlyIncreasePermissions())
1100 return;
[email protected]8d888c12010-11-30 00:00:251101
[email protected]8d888c12010-11-30 00:00:251102 extension_prefs_->AddGrantedPermissions(extension->id(),
[email protected]902fd7b2011-07-27 18:42:311103 extension->GetActivePermissions());
[email protected]8d888c12010-11-30 00:00:251104}
1105
[email protected]eaa7dd182010-12-14 11:09:001106void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]8d888c12010-11-30 00:00:251107 const Extension* extension) {
1108 CHECK(extension);
[email protected]fe2dd7742011-04-19 22:52:491109 RecordPermissionMessagesHistogram(
1110 extension, "Extensions.Permissions_ReEnable");
[email protected]8d888c12010-11-30 00:00:251111 GrantPermissions(extension);
1112 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
1113 EnableExtension(extension->id());
1114}
1115
[email protected]902fd7b2011-07-27 18:42:311116void ExtensionService::UpdateActivePermissions(
1117 const Extension* extension,
1118 const ExtensionPermissionSet* permissions) {
1119 extension_prefs()->SetActivePermissions(extension->id(), permissions);
1120 extension->SetActivePermissions(permissions);
1121}
1122
[email protected]eaa7dd182010-12-14 11:09:001123void ExtensionService::LoadExtension(const FilePath& extension_path) {
[email protected]71eb1e6d2011-08-02 22:57:401124 LoadExtension(extension_path, true);
1125}
1126
1127void ExtensionService::LoadExtension(const FilePath& extension_path,
1128 bool prompt_for_plugins) {
[email protected]e49002a2011-07-13 03:40:441129 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
[email protected]704d77e2011-10-17 18:05:291130 NewRunnableMethod(backend_.get(),
1131 &ExtensionServiceBackend::LoadSingleExtension,
1132 extension_path, prompt_for_plugins));
[email protected]e49002a2011-07-13 03:40:441133}
1134
1135void ExtensionService::LoadExtensionFromCommandLine(
[email protected]67d2d802011-07-21 02:54:281136 const FilePath& path_in) {
1137
1138 // Load extensions from the command line synchronously to avoid a race
1139 // between extension loading and loading an URL from the command line.
1140 base::ThreadRestrictions::ScopedAllowIO allow_io;
1141
1142 FilePath extension_path = path_in;
1143 file_util::AbsolutePath(&extension_path);
1144
1145 std::string id = Extension::GenerateIdForPath(extension_path);
[email protected]cdfca9702011-08-08 16:07:011146 bool allow_file_access =
1147 Extension::ShouldAlwaysAllowFileAccess(Extension::LOAD);
1148 if (extension_prefs()->HasAllowFileAccessSetting(id))
1149 allow_file_access = extension_prefs()->AllowFileAccess(id);
[email protected]67d2d802011-07-21 02:54:281150
1151 int flags = Extension::NO_FLAGS;
[email protected]cdfca9702011-08-08 16:07:011152 if (allow_file_access)
[email protected]67d2d802011-07-21 02:54:281153 flags |= Extension::ALLOW_FILE_ACCESS;
1154 if (Extension::ShouldDoStrictErrorChecking(Extension::LOAD))
1155 flags |= Extension::STRICT_ERROR_CHECKS;
1156
1157 std::string error;
1158 scoped_refptr<const Extension> extension(extension_file_util::LoadExtension(
1159 extension_path,
1160 Extension::LOAD,
1161 flags,
1162 &error));
1163
1164 if (!extension) {
[email protected]655b2b1a2011-10-13 17:13:061165 ReportExtensionLoadError(extension_path, error, true);
[email protected]67d2d802011-07-21 02:54:281166 return;
1167 }
1168
1169 OnLoadSingleExtension(extension, false);
[email protected]9f1087e2009-06-15 17:29:321170}
1171
[email protected]eaa7dd182010-12-14 11:09:001172void ExtensionService::LoadComponentExtensions() {
[email protected]1952c7d2010-03-04 23:48:341173 for (RegisteredComponentExtensions::iterator it =
1174 component_extension_manifests_.begin();
1175 it != component_extension_manifests_.end(); ++it) {
[email protected]61b55b62011-03-24 09:03:101176 LoadComponentExtension(*it);
[email protected]1952c7d2010-03-04 23:48:341177 }
1178}
1179
[email protected]a964e112011-04-14 21:52:511180const Extension* ExtensionService::LoadComponentExtension(
[email protected]61b55b62011-03-24 09:03:101181 const ComponentExtensionInfo &info) {
1182 JSONStringValueSerializer serializer(info.manifest);
1183 scoped_ptr<Value> manifest(serializer.Deserialize(NULL, NULL));
1184 if (!manifest.get()) {
[email protected]0b05c262011-08-11 01:42:211185 LOG(ERROR) << "Failed to parse manifest for extension";
[email protected]a964e112011-04-14 21:52:511186 return NULL;
[email protected]61b55b62011-03-24 09:03:101187 }
1188
[email protected]83048a22011-03-29 00:14:131189 int flags = Extension::REQUIRE_KEY;
1190 if (Extension::ShouldDoStrictErrorChecking(Extension::COMPONENT))
1191 flags |= Extension::STRICT_ERROR_CHECKS;
[email protected]61b55b62011-03-24 09:03:101192 std::string error;
1193 scoped_refptr<const Extension> extension(Extension::Create(
1194 info.root_directory,
1195 Extension::COMPONENT,
1196 *static_cast<DictionaryValue*>(manifest.get()),
[email protected]83048a22011-03-29 00:14:131197 flags,
[email protected]61b55b62011-03-24 09:03:101198 &error));
1199 if (!extension.get()) {
[email protected]0b05c262011-08-11 01:42:211200 LOG(ERROR) << error;
[email protected]a964e112011-04-14 21:52:511201 return NULL;
[email protected]61b55b62011-03-24 09:03:101202 }
1203 AddExtension(extension);
[email protected]a964e112011-04-14 21:52:511204 return extension;
[email protected]61b55b62011-03-24 09:03:101205}
1206
[email protected]ad63a792011-06-16 06:41:111207void ExtensionService::UnloadComponentExtension(
1208 const ComponentExtensionInfo& info) {
1209 JSONStringValueSerializer serializer(info.manifest);
1210 scoped_ptr<Value> manifest(serializer.Deserialize(NULL, NULL));
1211 if (!manifest.get()) {
[email protected]0b05c262011-08-11 01:42:211212 LOG(ERROR) << "Failed to parse manifest for extension";
[email protected]ad63a792011-06-16 06:41:111213 return;
1214 }
1215 std::string public_key;
1216 std::string public_key_bytes;
1217 std::string id;
1218 if (!static_cast<DictionaryValue*>(manifest.get())->
1219 GetString(extension_manifest_keys::kPublicKey, &public_key) ||
1220 !Extension::ParsePEMKeyBytes(public_key, &public_key_bytes) ||
1221 !Extension::GenerateId(public_key_bytes, &id)) {
[email protected]0b05c262011-08-11 01:42:211222 LOG(ERROR) << "Failed to get extension id";
[email protected]ad63a792011-06-16 06:41:111223 return;
1224 }
[email protected]814a7bf0f2011-08-13 05:30:591225 UnloadExtension(id, extension_misc::UNLOAD_REASON_DISABLE);
[email protected]ad63a792011-06-16 06:41:111226}
1227
[email protected]eaa7dd182010-12-14 11:09:001228void ExtensionService::LoadAllExtensions() {
[email protected]a29a517a2011-01-21 21:11:121229 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a315ba92010-11-16 14:12:211230
[email protected]cc2c3432009-11-06 17:24:361231 base::TimeTicks start_time = base::TimeTicks::Now();
1232
[email protected]1952c7d2010-03-04 23:48:341233 // Load any component extensions.
1234 LoadComponentExtensions();
1235
[email protected]e72e8eb82009-06-18 17:21:511236 // Load the previously installed extensions.
[email protected]a315ba92010-11-16 14:12:211237 scoped_ptr<ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]e6090e42010-03-23 22:44:081238 extension_prefs_->GetInstalledExtensionsInfo());
[email protected]c6d474f82009-12-16 21:11:061239
[email protected]a315ba92010-11-16 14:12:211240 std::vector<int> reload_reason_counts(NUM_MANIFEST_RELOAD_REASONS, 0);
1241 bool should_write_prefs = false;
1242
1243 for (size_t i = 0; i < extensions_info->size(); ++i) {
1244 ExtensionInfo* info = extensions_info->at(i).get();
1245
1246 ManifestReloadReason reload_reason = ShouldReloadExtensionManifest(*info);
1247 ++reload_reason_counts[reload_reason];
1248 UMA_HISTOGRAM_ENUMERATION("Extensions.ManifestReloadEnumValue",
1249 reload_reason, 100);
1250
1251 if (reload_reason != NOT_NEEDED) {
1252 // Reloading and extension reads files from disk. We do this on the
1253 // UI thread because reloads should be very rare, and the complexity
1254 // added by delaying the time when the extensions service knows about
1255 // all extensions is significant. See crbug.com/37548 for details.
1256 // |allow_io| disables tests that file operations run on the file
1257 // thread.
1258 base::ThreadRestrictions::ScopedAllowIO allow_io;
1259
1260 std::string error;
1261 scoped_refptr<const Extension> extension(
1262 extension_file_util::LoadExtension(
[email protected]542258c2011-03-04 21:25:311263 info->extension_path,
1264 info->extension_location,
[email protected]2af666c2011-10-16 16:23:191265 GetExtensionCreateFlagsForInstalledExtension(info),
[email protected]542258c2011-03-04 21:25:311266 &error));
[email protected]a315ba92010-11-16 14:12:211267
1268 if (extension.get()) {
1269 extensions_info->at(i)->extension_manifest.reset(
1270 static_cast<DictionaryValue*>(
1271 extension->manifest_value()->DeepCopy()));
1272 should_write_prefs = true;
1273 }
[email protected]c6d474f82009-12-16 21:11:061274 }
1275 }
1276
[email protected]a315ba92010-11-16 14:12:211277 for (size_t i = 0; i < extensions_info->size(); ++i) {
1278 LoadInstalledExtension(*extensions_info->at(i), should_write_prefs);
[email protected]c6d474f82009-12-16 21:11:061279 }
1280
[email protected]ae09ca62009-08-21 19:46:461281 OnLoadedInstalledExtensions();
[email protected]cc2c3432009-11-06 17:24:361282
[email protected]a315ba92010-11-16 14:12:211283 // The histograms Extensions.ManifestReload* allow us to validate
1284 // the assumption that reloading manifest is a rare event.
1285 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadNotNeeded",
1286 reload_reason_counts[NOT_NEEDED]);
1287 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadUnpackedDir",
1288 reload_reason_counts[UNPACKED_DIR]);
1289 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadNeedsRelocalization",
1290 reload_reason_counts[NEEDS_RELOCALIZATION]);
1291
[email protected]cc2c3432009-11-06 17:24:361292 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadAll", extensions_.size());
1293 UMA_HISTOGRAM_COUNTS_100("Extensions.Disabled", disabled_extensions_.size());
1294
[email protected]1952c7d2010-03-04 23:48:341295 UMA_HISTOGRAM_TIMES("Extensions.LoadAllTime",
1296 base::TimeTicks::Now() - start_time);
[email protected]cc2c3432009-11-06 17:24:361297
[email protected]d101c4a2011-07-22 22:51:361298 int app_user_count = 0;
1299 int app_external_count = 0;
[email protected]9b217652010-10-08 22:04:231300 int hosted_app_count = 0;
1301 int packaged_app_count = 0;
[email protected]1952c7d2010-03-04 23:48:341302 int user_script_count = 0;
[email protected]d101c4a2011-07-22 22:51:361303 int extension_user_count = 0;
1304 int extension_external_count = 0;
[email protected]1952c7d2010-03-04 23:48:341305 int theme_count = 0;
[email protected]1952c7d2010-03-04 23:48:341306 int page_action_count = 0;
1307 int browser_action_count = 0;
1308 ExtensionList::iterator ex;
1309 for (ex = extensions_.begin(); ex != extensions_.end(); ++ex) {
[email protected]9b217652010-10-08 22:04:231310 Extension::Location location = (*ex)->location();
[email protected]7fa19f82010-12-21 19:40:081311 Extension::Type type = (*ex)->GetType();
[email protected]9b217652010-10-08 22:04:231312 if ((*ex)->is_app()) {
1313 UMA_HISTOGRAM_ENUMERATION("Extensions.AppLocation",
1314 location, 100);
1315 } else if (type == Extension::TYPE_EXTENSION) {
1316 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionLocation",
1317 location, 100);
1318 }
1319
[email protected]1952c7d2010-03-04 23:48:341320 // Don't count component extensions, since they are only extensions as an
1321 // implementation detail.
[email protected]9b217652010-10-08 22:04:231322 if (location == Extension::COMPONENT)
[email protected]1952c7d2010-03-04 23:48:341323 continue;
1324
[email protected]e8c729a2010-03-09 19:55:191325 // Don't count unpacked extensions, since they're a developer-specific
1326 // feature.
[email protected]9b217652010-10-08 22:04:231327 if (location == Extension::LOAD)
[email protected]e8c729a2010-03-09 19:55:191328 continue;
1329
[email protected]9b217652010-10-08 22:04:231330 // Using an enumeration shows us the total installed ratio across all users.
1331 // Using the totals per user at each startup tells us the distribution of
1332 // usage for each user (e.g. 40% of users have at least one app installed).
1333 UMA_HISTOGRAM_ENUMERATION("Extensions.LoadType", type, 100);
1334 switch (type) {
1335 case Extension::TYPE_THEME:
[email protected]a315ba92010-11-16 14:12:211336 ++theme_count;
[email protected]9b217652010-10-08 22:04:231337 break;
1338 case Extension::TYPE_USER_SCRIPT:
[email protected]a315ba92010-11-16 14:12:211339 ++user_script_count;
[email protected]9b217652010-10-08 22:04:231340 break;
1341 case Extension::TYPE_HOSTED_APP:
[email protected]a315ba92010-11-16 14:12:211342 ++hosted_app_count;
[email protected]d101c4a2011-07-22 22:51:361343 if (Extension::IsExternalLocation(location)) {
1344 ++app_external_count;
1345 } else {
1346 ++app_user_count;
1347 }
[email protected]9b217652010-10-08 22:04:231348 break;
1349 case Extension::TYPE_PACKAGED_APP:
[email protected]a315ba92010-11-16 14:12:211350 ++packaged_app_count;
[email protected]d101c4a2011-07-22 22:51:361351 if (Extension::IsExternalLocation(location)) {
1352 ++app_external_count;
1353 } else {
1354 ++app_user_count;
1355 }
[email protected]9b217652010-10-08 22:04:231356 break;
1357 case Extension::TYPE_EXTENSION:
1358 default:
[email protected]d101c4a2011-07-22 22:51:361359 if (Extension::IsExternalLocation(location)) {
1360 ++extension_external_count;
1361 } else {
1362 ++extension_user_count;
1363 }
[email protected]9b217652010-10-08 22:04:231364 break;
[email protected]cc2c3432009-11-06 17:24:361365 }
[email protected]9b217652010-10-08 22:04:231366 if ((*ex)->page_action() != NULL)
[email protected]a315ba92010-11-16 14:12:211367 ++page_action_count;
[email protected]9b217652010-10-08 22:04:231368 if ((*ex)->browser_action() != NULL)
[email protected]a315ba92010-11-16 14:12:211369 ++browser_action_count;
[email protected]fe2dd7742011-04-19 22:52:491370
1371 RecordPermissionMessagesHistogram(
1372 ex->get(), "Extensions.Permissions_Load");
[email protected]cc2c3432009-11-06 17:24:361373 }
[email protected]d101c4a2011-07-22 22:51:361374 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadApp",
1375 app_user_count + app_external_count);
1376 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadAppUser", app_user_count);
1377 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadAppExternal", app_external_count);
[email protected]9b217652010-10-08 22:04:231378 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadHostedApp", hosted_app_count);
1379 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadPackagedApp", packaged_app_count);
[email protected]d101c4a2011-07-22 22:51:361380 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExtension",
1381 extension_user_count + extension_external_count);
1382 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExtensionUser",
1383 extension_user_count);
1384 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExtensionExternal",
1385 extension_external_count);
[email protected]1952c7d2010-03-04 23:48:341386 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadUserScript", user_script_count);
1387 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadTheme", theme_count);
[email protected]1952c7d2010-03-04 23:48:341388 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadPageAction", page_action_count);
1389 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadBrowserAction",
1390 browser_action_count);
[email protected]ae09ca62009-08-21 19:46:461391}
1392
[email protected]fe2dd7742011-04-19 22:52:491393// static
1394void ExtensionService::RecordPermissionMessagesHistogram(
1395 const Extension* e, const char* histogram) {
1396 // Since this is called from multiple sources, and since the Histogram macros
1397 // use statics, we need to manually lookup the Histogram ourselves.
1398 base::Histogram* counter = base::LinearHistogram::FactoryGet(
1399 histogram,
1400 1,
[email protected]0d3e4a22011-06-23 19:02:521401 ExtensionPermissionMessage::kEnumBoundary,
1402 ExtensionPermissionMessage::kEnumBoundary + 1,
[email protected]fe2dd7742011-04-19 22:52:491403 base::Histogram::kUmaTargetedHistogramFlag);
1404
[email protected]0d3e4a22011-06-23 19:02:521405 ExtensionPermissionMessages permissions = e->GetPermissionMessages();
[email protected]fe2dd7742011-04-19 22:52:491406 if (permissions.empty()) {
[email protected]0d3e4a22011-06-23 19:02:521407 counter->Add(ExtensionPermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:491408 } else {
[email protected]0d3e4a22011-06-23 19:02:521409 for (ExtensionPermissionMessages::iterator it = permissions.begin();
1410 it != permissions.end(); ++it)
1411 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:491412 }
1413}
1414
[email protected]eaa7dd182010-12-14 11:09:001415void ExtensionService::LoadInstalledExtension(const ExtensionInfo& info,
[email protected]542258c2011-03-04 21:25:311416 bool write_to_prefs) {
[email protected]ae09ca62009-08-21 19:46:461417 std::string error;
[email protected]9adb9692010-10-29 23:14:021418 scoped_refptr<const Extension> extension(NULL);
[email protected]306a2bd2010-08-11 14:56:361419 if (!extension_prefs_->IsExtensionAllowedByPolicy(info.extension_id)) {
1420 error = errors::kDisabledByPolicy;
1421 } else if (info.extension_manifest.get()) {
[email protected]66e4eb32010-10-27 20:37:411422 extension = Extension::Create(
[email protected]542258c2011-03-04 21:25:311423 info.extension_path,
1424 info.extension_location,
1425 *info.extension_manifest,
[email protected]2af666c2011-10-16 16:23:191426 GetExtensionCreateFlagsForInstalledExtension(&info),
[email protected]542258c2011-03-04 21:25:311427 &error);
[email protected]ae09ca62009-08-21 19:46:461428 } else {
[email protected]c6d474f82009-12-16 21:11:061429 error = errors::kManifestUnreadable;
[email protected]ae09ca62009-08-21 19:46:461430 }
1431
[email protected]199cc8bc72011-09-27 16:22:341432 // Once installed, non-unpacked extensions cannot change their IDs (e.g., by
1433 // updating the 'key' field in their manifest).
1434 if (extension &&
1435 extension->location() != Extension::LOAD &&
1436 info.extension_id != extension->id()) {
1437 error = errors::kCannotChangeExtensionID;
1438 extension = NULL;
1439 UserMetrics::RecordAction(UserMetricsAction("Extensions.IDChangedError"));
1440 }
1441
[email protected]ae09ca62009-08-21 19:46:461442 if (!extension) {
[email protected]655b2b1a2011-10-13 17:13:061443 ReportExtensionLoadError(info.extension_path, error, false);
[email protected]ae09ca62009-08-21 19:46:461444 return;
1445 }
1446
[email protected]c6d474f82009-12-16 21:11:061447 if (write_to_prefs)
1448 extension_prefs_->UpdateManifest(extension);
1449
[email protected]49cd1572011-02-08 21:38:451450 AddExtension(extension);
[email protected]9f1087e2009-06-15 17:29:321451}
1452
[email protected]2af666c2011-10-16 16:23:191453int ExtensionService::GetExtensionCreateFlagsForInstalledExtension(
1454 const ExtensionInfo* info) {
1455 int flags = Extension::NO_FLAGS;
1456 if (info->extension_location != Extension::LOAD)
1457 flags |= Extension::REQUIRE_KEY;
1458 if (Extension::ShouldDoStrictErrorChecking(info->extension_location))
1459 flags |= Extension::STRICT_ERROR_CHECKS;
1460 if (extension_prefs_->AllowFileAccess(info->extension_id))
1461 flags |= Extension::ALLOW_FILE_ACCESS;
1462 if (extension_prefs_->IsFromWebStore(info->extension_id))
1463 flags |= Extension::FROM_WEBSTORE;
1464 if (extension_prefs_->IsFromBookmark(info->extension_id))
1465 flags |= Extension::FROM_BOOKMARK;
1466 return flags;
1467}
1468
[email protected]eaa7dd182010-12-14 11:09:001469void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:421470 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061471 // was loaded, otherwise a race can arise where a renderer that is created
1472 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421473 // that the request context doesn't yet know about. The profile is responsible
1474 // for ensuring its URLRequestContexts appropriately discover the loaded
1475 // extension.
[email protected]c8d407e2011-04-28 21:27:171476 profile_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:061477
[email protected]c8d407e2011-04-28 21:27:171478 // Tell subsystems that use the EXTENSION_LOADED notification about the new
1479 // extension.
[email protected]62d30f42009-10-01 22:36:061480 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271481 chrome::NOTIFICATION_EXTENSION_LOADED,
[email protected]24e7a9d2009-11-04 11:11:341482 Source<Profile>(profile_),
[email protected]9adb9692010-10-29 23:14:021483 Details<const Extension>(extension));
[email protected]2de307592011-04-05 21:16:581484
[email protected]c8d407e2011-04-28 21:27:171485 // Tell renderers about the new extension.
[email protected]77a6970c2011-04-23 16:58:561486 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
1487 !i.IsAtEnd(); i.Advance()) {
[email protected]c8d407e2011-04-28 21:27:171488 RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081489 Profile* host_profile =
1490 Profile::FromBrowserContext(host->browser_context());
1491 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile()) {
[email protected]c8d407e2011-04-28 21:27:171492 host->Send(
[email protected]15379642011-08-11 22:22:051493 new ExtensionMsg_Loaded(ExtensionMsg_Loaded_Params(extension)));
[email protected]c8d407e2011-04-28 21:27:171494 }
[email protected]77a6970c2011-04-23 16:58:561495 }
1496
[email protected]c8d407e2011-04-28 21:27:171497 // Tell a random-ass collection of other subsystems about the new extension.
1498 // TODO(aa): What should we do with all this goop? Can it move into the
1499 // relevant objects via EXTENSION_LOADED?
1500
1501 profile_->GetExtensionSpecialStoragePolicy()->
1502 GrantRightsForExtension(extension);
1503
1504 UpdateActiveExtensionsInCrashReporter();
1505
1506 ExtensionWebUI::RegisterChromeURLOverrides(
1507 profile_, extension->GetChromeURLOverrides());
1508
[email protected]8e5c89a2011-06-07 18:13:331509 TemplateURLService* url_service =
1510 TemplateURLServiceFactory::GetForProfile(profile_);
1511 if (url_service)
1512 url_service->RegisterExtensionKeyword(extension);
[email protected]c8d407e2011-04-28 21:27:171513
1514 // Load the icon for omnibox-enabled extensions so it will be ready to display
1515 // in the URL bar.
1516 if (!extension->omnibox_keyword().empty()) {
1517 omnibox_popup_icon_manager_.LoadIcon(extension);
1518 omnibox_icon_manager_.LoadIcon(extension);
1519 }
1520
1521 // If the extension has permission to load chrome://favicon/ resources we need
1522 // to make sure that the FaviconSource is registered with the
1523 // ChromeURLDataManager.
1524 if (extension->HasHostPermission(GURL(chrome::kChromeUIFaviconURL))) {
1525 FaviconSource* favicon_source = new FaviconSource(profile_,
1526 FaviconSource::FAVICON);
1527 profile_->GetChromeURLDataManager()->AddDataSource(favicon_source);
1528 }
1529
1530 // TODO(mpcomplete): This ends up affecting all profiles. See crbug.com/80757.
[email protected]2de307592011-04-05 21:16:581531 bool plugins_changed = false;
1532 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1533 const Extension::PluginInfo& plugin = extension->plugins()[i];
1534 webkit::npapi::PluginList::Singleton()->RefreshPlugins();
1535 webkit::npapi::PluginList::Singleton()->AddExtraPluginPath(plugin.path);
1536 plugins_changed = true;
[email protected]476a27032011-09-13 13:40:491537 ChromePluginServiceFilter* filter =
1538 ChromePluginServiceFilter::GetInstance();
1539 if (plugin.is_public) {
1540 filter->RestrictPluginToProfileAndOrigin(
1541 plugin.path, profile_, GURL());
1542 } else {
1543 filter->RestrictPluginToProfileAndOrigin(
1544 plugin.path, profile_, extension->url());
[email protected]2de307592011-04-05 21:16:581545 }
1546 }
[email protected]84396dbc2011-04-14 06:33:421547
1548 bool nacl_modules_changed = false;
1549 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1550 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001551 RegisterNaClModule(module.url, module.mime_type);
[email protected]84396dbc2011-04-14 06:33:421552 nacl_modules_changed = true;
1553 }
1554
[email protected]ed0ba002011-05-26 16:55:131555 if (nacl_modules_changed)
1556 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421557
1558 if (plugins_changed || nacl_modules_changed)
[email protected]45a22e62011-10-12 09:48:021559 PluginService::GetInstance()->PurgePluginListCache(profile_, false);
[email protected]b4d9d122011-06-17 01:58:541560
[email protected]14da67b02011-08-02 05:49:131561#if defined(OS_CHROMEOS)
1562#if defined(TOUCH_UI)
[email protected]c8d4e9462011-06-29 23:18:571563 chromeos::input_method::InputMethodManager* input_method_manager =
1564 chromeos::input_method::InputMethodManager::GetInstance();
[email protected]14da67b02011-08-02 05:49:131565#endif
[email protected]b4d9d122011-06-17 01:58:541566 for (std::vector<Extension::InputComponentInfo>::const_iterator component =
1567 extension->input_components().begin();
1568 component != extension->input_components().end();
1569 ++component) {
[email protected]14da67b02011-08-02 05:49:131570 if (component->type == Extension::INPUT_COMPONENT_TYPE_IME) {
1571 ExtensionInputImeEventRouter::GetInstance()->RegisterIme(
1572 profile_, extension->id(), *component);
1573 }
1574#if defined(TOUCH_UI)
[email protected]b4d9d122011-06-17 01:58:541575 if (component->type == Extension::INPUT_COMPONENT_TYPE_VIRTUAL_KEYBOARD &&
1576 !component->layouts.empty()) {
1577 const bool is_system =
1578 !Extension::IsExternalLocation(extension->location());
[email protected]cbd4c042011-08-13 18:22:191579 input_method_manager->RegisterVirtualKeyboard(
1580 extension->url(),
1581 component->name, // human-readable name of the keyboard extension.
1582 component->layouts,
1583 is_system);
[email protected]b4d9d122011-06-17 01:58:541584 }
[email protected]14da67b02011-08-02 05:49:131585#endif
[email protected]b4d9d122011-06-17 01:58:541586 }
1587#endif
[email protected]62d30f42009-10-01 22:36:061588}
1589
[email protected]a9f39a312010-12-23 22:14:271590void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591591 const Extension* extension,
1592 extension_misc::UnloadedExtensionReason reason) {
[email protected]a9f39a312010-12-23 22:14:271593 UnloadedExtensionInfo details(extension, reason);
[email protected]62d30f42009-10-01 22:36:061594 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271595 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]24e7a9d2009-11-04 11:11:341596 Source<Profile>(profile_),
[email protected]a9f39a312010-12-23 22:14:271597 Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061598
[email protected]77a6970c2011-04-23 16:58:561599 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
1600 !i.IsAtEnd(); i.Advance()) {
[email protected]c8d407e2011-04-28 21:27:171601 RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081602 Profile* host_profile =
1603 Profile::FromBrowserContext(host->browser_context());
1604 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171605 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561606 }
1607
[email protected]dd163fb02011-05-04 22:22:171608 profile_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]c8d407e2011-04-28 21:27:171609 profile_->GetExtensionSpecialStoragePolicy()->
1610 RevokeRightsForExtension(extension);
1611
1612 ExtensionWebUI::UnregisterChromeURLOverrides(
1613 profile_, extension->GetChromeURLOverrides());
1614
[email protected]b777b332011-04-16 04:01:081615#if defined(OS_CHROMEOS)
1616 // Revoke external file access to
[email protected]c8d407e2011-04-28 21:27:171617 if (profile_->GetFileSystemContext() &&
1618 profile_->GetFileSystemContext()->path_manager() &&
1619 profile_->GetFileSystemContext()->path_manager()->external_provider()) {
1620 profile_->GetFileSystemContext()->path_manager()->external_provider()->
1621 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061622 }
[email protected]c8d407e2011-04-28 21:27:171623#endif
1624
1625 UpdateActiveExtensionsInCrashReporter();
[email protected]2de307592011-04-05 21:16:581626
1627 bool plugins_changed = false;
1628 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1629 const Extension::PluginInfo& plugin = extension->plugins()[i];
[email protected]14908b72011-04-20 06:54:361630 if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
[email protected]704d77e2011-10-17 18:05:291631 NewRunnableFunction(&ForceShutdownPlugin,
1632 plugin.path)))
[email protected]14908b72011-04-20 06:54:361633 NOTREACHED();
[email protected]2de307592011-04-05 21:16:581634 webkit::npapi::PluginList::Singleton()->RefreshPlugins();
1635 webkit::npapi::PluginList::Singleton()->RemoveExtraPluginPath(
1636 plugin.path);
1637 plugins_changed = true;
[email protected]476a27032011-09-13 13:40:491638 ChromePluginServiceFilter::GetInstance()->UnrestrictPlugin(plugin.path);
[email protected]2de307592011-04-05 21:16:581639 }
[email protected]84396dbc2011-04-14 06:33:421640
1641 bool nacl_modules_changed = false;
1642 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1643 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001644 UnregisterNaClModule(module.url);
[email protected]84396dbc2011-04-14 06:33:421645 nacl_modules_changed = true;
1646 }
1647
[email protected]ed0ba002011-05-26 16:55:131648 if (nacl_modules_changed)
1649 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421650
1651 if (plugins_changed || nacl_modules_changed)
[email protected]45a22e62011-10-12 09:48:021652 PluginService::GetInstance()->PurgePluginListCache(profile_, false);
[email protected]62d30f42009-10-01 22:36:061653}
1654
[email protected]eaa7dd182010-12-14 11:09:001655void ExtensionService::UpdateExtensionBlacklist(
[email protected]6b75ec32009-08-14 06:37:181656 const std::vector<std::string>& blacklist) {
1657 // Use this set to indicate if an extension in the blacklist has been used.
1658 std::set<std::string> blacklist_set;
1659 for (unsigned int i = 0; i < blacklist.size(); ++i) {
1660 if (Extension::IdIsValid(blacklist[i])) {
1661 blacklist_set.insert(blacklist[i]);
1662 }
1663 }
1664 extension_prefs_->UpdateBlacklist(blacklist_set);
1665 std::vector<std::string> to_be_removed;
1666 // Loop current extensions, unload installed extensions.
1667 for (ExtensionList::const_iterator iter = extensions_.begin();
1668 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021669 const Extension* extension = (*iter);
[email protected]6b75ec32009-08-14 06:37:181670 if (blacklist_set.find(extension->id()) != blacklist_set.end()) {
1671 to_be_removed.push_back(extension->id());
1672 }
1673 }
1674
1675 // UnloadExtension will change the extensions_ list. So, we should
1676 // call it outside the iterator loop.
1677 for (unsigned int i = 0; i < to_be_removed.size(); ++i) {
[email protected]814a7bf0f2011-08-13 05:30:591678 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]6b75ec32009-08-14 06:37:181679 }
1680}
1681
[email protected]0a071a32011-02-08 00:18:241682Profile* ExtensionService::profile() {
1683 return profile_;
1684}
1685
[email protected]eaa7dd182010-12-14 11:09:001686ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451687 return extension_prefs_;
1688}
1689
[email protected]fc84e0e2011-09-27 19:18:221690ExtensionSettingsFrontend* ExtensionService::extension_settings_frontend() {
1691 return &extension_settings_frontend_;
[email protected]b7f853e282011-08-10 09:24:201692}
1693
[email protected]b790b072011-05-20 09:46:441694ExtensionContentSettingsStore*
1695 ExtensionService::GetExtensionContentSettingsStore() {
1696 return extension_prefs()->content_settings_store();
1697}
1698
[email protected]2859946f2011-04-04 18:18:061699ExtensionUpdater* ExtensionService::updater() {
1700 return updater_.get();
1701}
1702
[email protected]eaa7dd182010-12-14 11:09:001703void ExtensionService::CheckAdminBlacklist() {
[email protected]aa96d3a2010-08-21 08:45:251704 std::vector<std::string> to_be_removed;
1705 // Loop through extensions list, unload installed extensions.
1706 for (ExtensionList::const_iterator iter = extensions_.begin();
1707 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021708 const Extension* extension = (*iter);
[email protected]aa96d3a2010-08-21 08:45:251709 if (!extension_prefs_->IsExtensionAllowedByPolicy(extension->id()))
1710 to_be_removed.push_back(extension->id());
1711 }
1712
1713 // UnloadExtension will change the extensions_ list. So, we should
1714 // call it outside the iterator loop.
1715 for (unsigned int i = 0; i < to_be_removed.size(); ++i)
[email protected]814a7bf0f2011-08-13 05:30:591716 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]aa96d3a2010-08-21 08:45:251717}
1718
[email protected]31206602011-04-13 23:07:321719void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351720 if (updater()) {
[email protected]31206602011-04-13 23:07:321721 updater()->CheckSoon();
[email protected]c3cfb012011-04-06 22:07:351722 } else {
[email protected]31206602011-04-13 23:07:321723 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351724 }
1725}
1726
[email protected]3bdba0d2011-08-23 07:17:301727namespace {
1728 bool IsSyncableNone(const Extension& extension) { return false; }
1729} // namespace
1730
1731ExtensionService::SyncBundle::SyncBundle()
1732 : filter(IsSyncableNone),
1733 sync_processor(NULL) {
[email protected]b05fb9ff2011-04-23 00:07:561734}
1735
[email protected]3bdba0d2011-08-23 07:17:301736ExtensionService::SyncBundle::~SyncBundle() {
1737}
1738
1739bool ExtensionService::SyncBundle::HasExtensionId(const std::string& id) const {
1740 return synced_extensions.find(id) != synced_extensions.end();
1741}
1742
1743bool ExtensionService::SyncBundle::HasPendingExtensionId(const std::string& id)
1744 const {
1745 return pending_sync_data.find(id) != pending_sync_data.end();
1746}
1747
1748void ExtensionService::SyncExtensionChangeIfNeeded(const Extension& extension) {
1749 SyncBundle* sync_bundle = GetSyncBundleForExtension(extension);
1750 if (sync_bundle) {
1751 ExtensionSyncData extension_sync_data(extension,
1752 IsExtensionEnabled(extension.id()),
1753 IsIncognitoEnabled(extension.id()));
1754
1755 SyncChangeList sync_change_list(1, extension_sync_data.GetSyncChange(
1756 sync_bundle->HasExtensionId(extension.id()) ?
1757 SyncChange::ACTION_UPDATE : SyncChange::ACTION_ADD));
1758 sync_bundle->sync_processor->ProcessSyncChanges(
1759 FROM_HERE, sync_change_list);
1760 sync_bundle->synced_extensions.insert(extension.id());
1761 sync_bundle->pending_sync_data.erase(extension.id());
[email protected]b05fb9ff2011-04-23 00:07:561762 }
[email protected]3bdba0d2011-08-23 07:17:301763}
1764
1765ExtensionService::SyncBundle* ExtensionService::GetSyncBundleForExtension(
1766 const Extension& extension) {
1767 if (app_sync_bundle_.filter(extension))
1768 return &app_sync_bundle_;
1769 else if (extension_sync_bundle_.filter(extension))
1770 return &extension_sync_bundle_;
1771 else
1772 return NULL;
1773}
1774
1775ExtensionService::SyncBundle*
1776 ExtensionService::GetSyncBundleForExtensionSyncData(
1777 const ExtensionSyncData& extension_sync_data) {
1778 switch (extension_sync_data.type()) {
1779 case Extension::SYNC_TYPE_APP:
1780 return &app_sync_bundle_;
1781 case Extension::SYNC_TYPE_EXTENSION:
1782 return &extension_sync_bundle_;
1783 default:
1784 NOTREACHED();
1785 return NULL;
1786 }
1787}
1788
1789#define GET_SYNC_BUNDLE_FOR_MODEL_TYPE_BODY() \
1790 do { \
1791 switch (type) { \
1792 case syncable::APPS: \
1793 return &app_sync_bundle_; \
1794 case syncable::EXTENSIONS: \
1795 return &extension_sync_bundle_; \
1796 default: \
1797 NOTREACHED(); \
1798 return NULL; \
1799 } \
1800 } while (0)
1801
1802const ExtensionService::SyncBundle*
1803 ExtensionService::GetSyncBundleForModelTypeConst(
1804 syncable::ModelType type) const {
1805 GET_SYNC_BUNDLE_FOR_MODEL_TYPE_BODY();
1806}
1807
1808ExtensionService::SyncBundle* ExtensionService::GetSyncBundleForModelType(
1809 syncable::ModelType type) {
1810 GET_SYNC_BUNDLE_FOR_MODEL_TYPE_BODY();
1811}
1812
1813#undef GET_SYNC_BUNDLE_FOR_MODEL_TYPE_BODY
1814
1815SyncError ExtensionService::MergeDataAndStartSyncing(
1816 syncable::ModelType type,
1817 const SyncDataList& initial_sync_data,
1818 SyncChangeProcessor* sync_processor) {
1819 CHECK(sync_processor);
1820
1821 SyncBundle* bundle = NULL;
1822
1823 switch (type) {
1824 case syncable::EXTENSIONS:
1825 bundle = &extension_sync_bundle_;
1826 bundle->filter = IsSyncableExtension;
1827 break;
1828
1829 case syncable::APPS:
1830 bundle = &app_sync_bundle_;
1831 bundle->filter = IsSyncableApp;
1832 break;
1833
1834 default:
1835 LOG(FATAL) << "Got " << type << " ModelType";
1836 }
1837
1838 bundle->sync_processor = sync_processor;
1839
1840 for (SyncDataList::const_iterator i = initial_sync_data.begin();
1841 i != initial_sync_data.end();
1842 ++i) {
1843 ExtensionSyncData extension_sync_data = ExtensionSyncData(*i);
1844 bundle->synced_extensions.insert(extension_sync_data.id());
1845 ProcessExtensionSyncData(extension_sync_data, *bundle);
1846 }
1847
1848 SyncDataList sync_data_list = GetAllSyncData(type);
1849 SyncChangeList sync_change_list;
1850 for (SyncDataList::const_iterator i = sync_data_list.begin();
1851 i != sync_data_list.end();
1852 ++i) {
1853 if (bundle->HasExtensionId(i->GetTag()))
1854 sync_change_list.push_back(SyncChange(SyncChange::ACTION_UPDATE, *i));
1855 else
1856 sync_change_list.push_back(SyncChange(SyncChange::ACTION_ADD, *i));
1857 }
1858 bundle->sync_processor->ProcessSyncChanges(FROM_HERE, sync_change_list);
1859
1860 return SyncError();
1861}
1862
1863void ExtensionService::StopSyncing(syncable::ModelType type) {
1864 SyncBundle* bundle = GetSyncBundleForModelType(type);
1865 CHECK(bundle);
1866 // This is the simplest way to clear out the bundle.
1867 *bundle = SyncBundle();
1868}
1869
1870SyncDataList ExtensionService::GetAllSyncData(syncable::ModelType type) const {
1871 const SyncBundle* bundle = GetSyncBundleForModelTypeConst(type);
1872 CHECK(bundle);
1873 std::vector<ExtensionSyncData> extension_sync_data = GetSyncDataList(*bundle);
1874 SyncDataList result(extension_sync_data.size());
1875 for (int i = 0; i < static_cast<int>(extension_sync_data.size()); ++i) {
1876 result[i] = extension_sync_data[i].GetSyncData();
1877 }
1878 return result;
1879}
1880
1881SyncError ExtensionService::ProcessSyncChanges(
1882 const tracked_objects::Location& from_here,
1883 const SyncChangeList& change_list) {
1884 for (SyncChangeList::const_iterator i = change_list.begin();
1885 i != change_list.end();
1886 ++i) {
1887 ExtensionSyncData extension_sync_data = ExtensionSyncData(*i);
1888 SyncBundle* bundle = GetSyncBundleForExtensionSyncData(extension_sync_data);
1889 CHECK(bundle);
1890
1891 if (extension_sync_data.uninstalled())
1892 bundle->synced_extensions.erase(extension_sync_data.id());
1893 else
1894 bundle->synced_extensions.insert(extension_sync_data.id());
1895 ProcessExtensionSyncData(extension_sync_data, *bundle);
1896 }
1897
1898 return SyncError();
[email protected]b05fb9ff2011-04-23 00:07:561899}
1900
1901void ExtensionService::GetSyncDataListHelper(
1902 const ExtensionList& extensions,
[email protected]3bdba0d2011-08-23 07:17:301903 const SyncBundle& bundle,
[email protected]b05fb9ff2011-04-23 00:07:561904 std::vector<ExtensionSyncData>* sync_data_list) const {
1905 for (ExtensionList::const_iterator it = extensions.begin();
1906 it != extensions.end(); ++it) {
1907 const Extension& extension = **it;
[email protected]3bdba0d2011-08-23 07:17:301908 if (bundle.filter(extension) &&
1909 // If we have pending extension data for this extension, then this
1910 // version is out of date. We'll sync back the version we got from
1911 // sync.
1912 !bundle.HasPendingExtensionId(extension.id())) {
1913 sync_data_list->push_back(
1914 ExtensionSyncData(extension,
1915 IsExtensionEnabled(extension.id()),
1916 IsIncognitoEnabled(extension.id())));
[email protected]b05fb9ff2011-04-23 00:07:561917 }
1918 }
1919}
1920
1921std::vector<ExtensionSyncData> ExtensionService::GetSyncDataList(
[email protected]3bdba0d2011-08-23 07:17:301922 const SyncBundle& bundle) const {
1923 std::vector<ExtensionSyncData> extension_sync_list;
1924 GetSyncDataListHelper(extensions_, bundle, &extension_sync_list);
1925 GetSyncDataListHelper(disabled_extensions_, bundle, &extension_sync_list);
1926 GetSyncDataListHelper(terminated_extensions_, bundle, &extension_sync_list);
1927
1928 for (std::map<std::string, ExtensionSyncData>::const_iterator i =
1929 bundle.pending_sync_data.begin();
1930 i != bundle.pending_sync_data.end();
1931 ++i) {
1932 extension_sync_list.push_back(i->second);
1933 }
1934
1935 return extension_sync_list;
[email protected]b05fb9ff2011-04-23 00:07:561936}
1937
[email protected]3bdba0d2011-08-23 07:17:301938void ExtensionService::ProcessExtensionSyncData(
[email protected]90310d92011-04-17 07:35:041939 const ExtensionSyncData& extension_sync_data,
[email protected]3bdba0d2011-08-23 07:17:301940 SyncBundle& bundle) {
1941 const std::string& id = extension_sync_data.id();
[email protected]96e989b2011-08-30 19:35:061942 const Extension* extension = GetInstalledExtension(id);
1943
1944 // TODO(bolms): we should really handle this better. The particularly bad
1945 // case is where an app becomes an extension or vice versa, and we end up with
1946 // a zombie extension that won't go away.
1947 if (extension && !bundle.filter(*extension))
1948 return;
[email protected]90310d92011-04-17 07:35:041949
1950 // Handle uninstalls first.
[email protected]3bdba0d2011-08-23 07:17:301951 if (extension_sync_data.uninstalled()) {
[email protected]90310d92011-04-17 07:35:041952 std::string error;
1953 if (!UninstallExtensionHelper(this, id)) {
1954 LOG(WARNING) << "Could not uninstall extension " << id
1955 << " for sync";
1956 }
1957 return;
1958 }
1959
[email protected]06f92562011-04-29 19:27:311960 // Set user settings.
[email protected]3bdba0d2011-08-23 07:17:301961 if (extension_sync_data.enabled()) {
[email protected]06f92562011-04-29 19:27:311962 EnableExtension(id);
1963 } else {
1964 DisableExtension(id);
1965 }
[email protected]3bdba0d2011-08-23 07:17:301966 SetIsIncognitoEnabled(id, extension_sync_data.incognito_enabled());
[email protected]81b14cc2011-04-29 00:39:371967
[email protected]90310d92011-04-17 07:35:041968 if (extension) {
[email protected]06f92562011-04-29 19:27:311969 // If the extension is already installed, check if it's outdated.
[email protected]3bdba0d2011-08-23 07:17:301970 int result = extension->version()->CompareTo(extension_sync_data.version());
[email protected]90310d92011-04-17 07:35:041971 if (result < 0) {
1972 // Extension is outdated.
[email protected]3bdba0d2011-08-23 07:17:301973 bundle.pending_sync_data[extension_sync_data.id()] = extension_sync_data;
[email protected]90310d92011-04-17 07:35:041974 CheckForUpdatesSoon();
[email protected]90310d92011-04-17 07:35:041975 }
[email protected]06f92562011-04-29 19:27:311976 } else {
[email protected]06f92562011-04-29 19:27:311977 // TODO(akalin): Replace silent update with a list of enabled
1978 // permissions.
[email protected]6cc7dbae2011-04-29 21:18:331979 const bool kInstallSilently = true;
[email protected]06f92562011-04-29 19:27:311980 if (!pending_extension_manager()->AddFromSync(
1981 id,
[email protected]3bdba0d2011-08-23 07:17:301982 extension_sync_data.update_url(),
1983 bundle.filter,
[email protected]6cc7dbae2011-04-29 21:18:331984 kInstallSilently)) {
[email protected]06f92562011-04-29 19:27:311985 LOG(WARNING) << "Could not add pending extension for " << id;
[email protected]3bdba0d2011-08-23 07:17:301986 // This means that the extension is already pending installation, with a
1987 // non-INTERNAL location. Add to pending_sync_data, even though it will
1988 // never be removed (we'll never install a syncable version of the
1989 // extension), so that GetAllSyncData() continues to send it.
[email protected]06f92562011-04-29 19:27:311990 }
[email protected]3bdba0d2011-08-23 07:17:301991 // Track pending extensions so that we can return them in GetAllSyncData().
1992 bundle.pending_sync_data[extension_sync_data.id()] = extension_sync_data;
[email protected]06f92562011-04-29 19:27:311993 CheckForUpdatesSoon();
[email protected]90310d92011-04-17 07:35:041994 }
[email protected]90310d92011-04-17 07:35:041995}
1996
[email protected]c3cfb012011-04-06 22:07:351997bool ExtensionService::IsIncognitoEnabled(
1998 const std::string& extension_id) const {
1999 // If this is an existing component extension we always allow it to
2000 // work in incognito mode.
[email protected]81b14cc2011-04-29 00:39:372001 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]c3cfb012011-04-06 22:07:352002 if (extension && extension->location() == Extension::COMPONENT)
[email protected]cb0ce1e022010-03-10 19:54:412003 return true;
2004
2005 // Check the prefs.
[email protected]c3cfb012011-04-06 22:07:352006 return extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]db7331a2010-02-25 22:10:502007}
[email protected]55a35692010-02-11 23:25:212008
[email protected]c3cfb012011-04-06 22:07:352009void ExtensionService::SetIsIncognitoEnabled(
2010 const std::string& extension_id, bool enabled) {
[email protected]81b14cc2011-04-29 00:39:372011 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]c3cfb012011-04-06 22:07:352012 if (extension && extension->location() == Extension::COMPONENT) {
2013 // This shouldn't be called for component extensions.
2014 NOTREACHED();
2015 return;
2016 }
2017
[email protected]8c6c8c22011-03-09 12:52:342018 // Broadcast unloaded and loaded events to update browser state. Only bother
2019 // if the value changed and the extension is actually enabled, since there is
2020 // no UI otherwise.
[email protected]c3cfb012011-04-06 22:07:352021 bool old_enabled = extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]8c6c8c22011-03-09 12:52:342022 if (enabled == old_enabled)
2023 return;
2024
[email protected]c3cfb012011-04-06 22:07:352025 extension_prefs_->SetIsIncognitoEnabled(extension_id, enabled);
[email protected]81b14cc2011-04-29 00:39:372026
2027 // If the extension is enabled (and not terminated), unload and
2028 // reload it to update UI.
2029 const Extension* enabled_extension = GetExtensionById(extension_id, false);
2030 if (enabled_extension) {
[email protected]814a7bf0f2011-08-13 05:30:592031 NotifyExtensionUnloaded(
2032 enabled_extension, extension_misc::UNLOAD_REASON_DISABLE);
[email protected]81b14cc2011-04-29 00:39:372033 NotifyExtensionLoaded(enabled_extension);
[email protected]568f33d2010-08-04 17:06:412034 }
[email protected]3bdba0d2011-08-23 07:17:302035
2036 if (extension)
2037 SyncExtensionChangeIfNeeded(*extension);
[email protected]55a35692010-02-11 23:25:212038}
2039
[email protected]eaa7dd182010-12-14 11:09:002040bool ExtensionService::CanCrossIncognito(const Extension* extension) {
[email protected]2a8f24e2010-11-03 21:37:052041 // We allow the extension to see events and data from another profile iff it
2042 // uses "spanning" behavior and it has incognito access. "split" mode
2043 // extensions only see events for a matching profile.
[email protected]0c41298f2011-09-13 21:17:222044 CHECK(extension);
[email protected]c3cfb012011-04-06 22:07:352045 return IsIncognitoEnabled(extension->id()) &&
2046 !extension->incognito_split_mode();
[email protected]2a8f24e2010-11-03 21:37:052047}
2048
[email protected]5ef7b002011-05-12 23:16:202049bool ExtensionService::CanLoadInIncognito(const Extension* extension) const {
2050 if (extension->is_hosted_app())
2051 return true;
2052 // Packaged apps and regular extensions need to be enabled specifically for
2053 // incognito (and split mode should be set).
2054 return extension->incognito_split_mode() &&
2055 IsIncognitoEnabled(extension->id());
2056}
2057
[email protected]eaa7dd182010-12-14 11:09:002058bool ExtensionService::AllowFileAccess(const Extension* extension) {
[email protected]05c82182010-06-24 17:49:082059 return (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]334e04a2010-06-24 23:34:442060 switches::kDisableExtensionsFileAccessCheck) ||
[email protected]05c82182010-06-24 17:49:082061 extension_prefs_->AllowFileAccess(extension->id()));
2062}
2063
[email protected]eaa7dd182010-12-14 11:09:002064void ExtensionService::SetAllowFileAccess(const Extension* extension,
[email protected]3aff9ad2011-04-01 20:26:482065 bool allow) {
2066 // Reload to update browser state. Only bother if the value changed and the
2067 // extension is actually enabled, since there is no UI otherwise.
2068 bool old_allow = AllowFileAccess(extension);
2069 if (allow == old_allow)
2070 return;
2071
[email protected]05c82182010-06-24 17:49:082072 extension_prefs_->SetAllowFileAccess(extension->id(), allow);
[email protected]3aff9ad2011-04-01 20:26:482073
2074 bool extension_is_enabled = std::find(extensions_.begin(), extensions_.end(),
2075 extension) != extensions_.end();
2076 if (extension_is_enabled)
2077 ReloadExtension(extension->id());
[email protected]05c82182010-06-24 17:49:082078}
2079
[email protected]eaa7dd182010-12-14 11:09:002080bool ExtensionService::GetBrowserActionVisibility(const Extension* extension) {
[email protected]f681c782010-11-19 11:19:392081 return extension_prefs_->GetBrowserActionVisibility(extension);
2082}
2083
[email protected]eaa7dd182010-12-14 11:09:002084void ExtensionService::SetBrowserActionVisibility(const Extension* extension,
[email protected]8e4560b62011-01-14 10:09:142085 bool visible) {
[email protected]f681c782010-11-19 11:19:392086 extension_prefs_->SetBrowserActionVisibility(extension, visible);
2087}
2088
[email protected]8e4560b62011-01-14 10:09:142089// Some extensions will autoupdate themselves externally from Chrome. These
2090// are typically part of some larger client application package. To support
2091// these, the extension will register its location in the the preferences file
2092// (and also, on Windows, in the registry) and this code will periodically
2093// check that location for a .crx file, which it will then install locally if
2094// a new version is available.
2095// Errors are reported through ExtensionErrorReporter. Succcess is not
2096// reported.
[email protected]eaa7dd182010-12-14 11:09:002097void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:122098 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:142099
2100 // Note that this installation is intentionally silent (since it didn't
2101 // go through the front-end). Extensions that are registered in this
2102 // way are effectively considered 'pre-bundled', and so implicitly
2103 // trusted. In general, if something has HKLM or filesystem access,
2104 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:122105
2106 // If any external extension records give a URL, a provider will set
2107 // this to true. Used by OnExternalProviderReady() to see if we need
2108 // to start an update check to fetch a new external extension.
2109 external_extension_url_added_ = false;
[email protected]8e4560b62011-01-14 10:09:142110
2111 // Ask each external extension provider to give us a call back for each
2112 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
2113 ProviderCollection::const_iterator i;
2114 for (i = external_extension_providers_.begin();
2115 i != external_extension_providers_.end(); ++i) {
2116 ExternalExtensionProviderInterface* provider = i->get();
2117 provider->VisitRegisteredExtension();
2118 }
2119
2120 // Uninstall of unclaimed extensions will happen after all the providers
[email protected]a29a517a2011-01-21 21:11:122121 // had reported ready. Every provider calls OnExternalProviderReady()
2122 // when it finishes, and OnExternalProviderReady() only acts when all
2123 // providers are ready. In case there are no providers, we call it
2124 // to trigger removal of extensions that used to have an external source.
2125 if (external_extension_providers_.empty())
2126 OnExternalProviderReady();
[email protected]9f1087e2009-06-15 17:29:322127}
2128
[email protected]8e4560b62011-01-14 10:09:142129void ExtensionService::OnExternalProviderReady() {
[email protected]a29a517a2011-01-21 21:11:122130 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2131
2132 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:142133 // if all of them are finished. So we check them first.
2134 ProviderCollection::const_iterator i;
2135 for (i = external_extension_providers_.begin();
2136 i != external_extension_providers_.end(); ++i) {
2137 ExternalExtensionProviderInterface* provider = i->get();
[email protected]a29a517a2011-01-21 21:11:122138 if (!provider->IsReady())
[email protected]8e4560b62011-01-14 10:09:142139 return;
[email protected]8e4560b62011-01-14 10:09:142140 }
2141
[email protected]a29a517a2011-01-21 21:11:122142 // All the providers are ready. Install any pending extensions.
2143 if (external_extension_url_added_ && updater()) {
2144 external_extension_url_added_ = false;
[email protected]8e4560b62011-01-14 10:09:142145 updater()->CheckNow();
2146 }
2147
2148 // Uninstall all the unclaimed extensions.
2149 scoped_ptr<ExtensionPrefs::ExtensionsInfo> extensions_info(
2150 extension_prefs_->GetInstalledExtensionsInfo());
2151 for (size_t i = 0; i < extensions_info->size(); ++i) {
2152 ExtensionInfo* info = extensions_info->at(i).get();
2153 if (Extension::IsExternalLocation(info->extension_location))
2154 CheckExternalUninstall(info->extension_id);
2155 }
[email protected]e5af875f2011-10-10 21:09:142156 IdentifyAlertableExtensions();
[email protected]e5af875f2011-10-10 21:09:142157}
2158
2159void ExtensionService::IdentifyAlertableExtensions() {
[email protected]d1282c22011-10-18 02:00:392160#if 0 // TODO(miket): enable upon completion of feature.
[email protected]e5af875f2011-10-10 21:09:142161 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2162
2163 // Build up the lists of extensions that require acknowledgment.
2164 // If this is the first time, grandfather extensions that would have
2165 // caused notification.
2166 scoped_ptr<ExtensionGlobalError> global_error(
2167 new ExtensionGlobalError(weak_ptr_factory_.GetWeakPtr()));
2168 bool needs_alert = false;
2169 for (ExtensionList::const_iterator iter = extensions_.begin();
2170 iter != extensions_.end(); ++iter) {
2171 const Extension* e = *iter;
2172 if (!IsExtensionEnabled(e->id())) {
2173 continue;
2174 }
2175 if (Extension::IsExternalLocation(e->location())) {
2176 if (!extension_prefs_->IsExternalExtensionAcknowledged(e->id())) {
2177 global_error->AddExternalExtension(e->id());
2178 needs_alert = true;
2179 }
2180 }
2181 if (extension_prefs_->IsExtensionBlacklisted(e->id())) {
2182 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(e->id())) {
2183 global_error->AddBlacklistedExtension(e->id());
2184 needs_alert = true;
2185 }
2186 }
2187 if (extension_prefs_->IsExtensionOrphaned(e->id())) {
2188 if (!extension_prefs_->IsOrphanedExtensionAcknowledged(e->id())) {
2189 global_error->AddOrphanedExtension(e->id());
2190 needs_alert = true;
2191 }
2192 }
2193 }
2194
2195 if (needs_alert) {
2196 if (extension_prefs_->SetAlertSystemFirstRun()) {
2197 global_error->set_accept_callback(
2198 base::Bind(&ExtensionService::HandleExtensionAlertAccept,
2199 base::Unretained(this)));
2200 global_error->set_cancel_callback(
2201 base::Bind(&ExtensionService::HandleExtensionAlertDetails,
2202 base::Unretained(this)));
[email protected]62433d32011-10-12 22:33:122203 ShowExtensionAlert(global_error.release());
[email protected]e5af875f2011-10-10 21:09:142204 } else {
2205 // First run. Just acknowledge all the extensions, silently, by
2206 // shortcutting the display of the UI and going straight to the
2207 // callback for pressing the Accept button.
[email protected]62433d32011-10-12 22:33:122208 HandleExtensionAlertAccept(*global_error.get(), NULL);
[email protected]e5af875f2011-10-10 21:09:142209 }
2210 }
[email protected]d1282c22011-10-18 02:00:392211#endif
[email protected]e5af875f2011-10-10 21:09:142212}
2213
[email protected]62433d32011-10-12 22:33:122214void ExtensionService::ShowExtensionAlert(ExtensionGlobalError* global_error) {
2215 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2216 Browser* browser = BrowserList::GetLastActiveWithProfile(profile_);
2217 if (browser) {
2218 global_error->ShowBubbleView(browser);
2219 }
2220}
2221
[email protected]e5af875f2011-10-10 21:09:142222void ExtensionService::HandleExtensionAlertAccept(
[email protected]62433d32011-10-12 22:33:122223 const ExtensionGlobalError& global_error, Browser* browser) {
[email protected]e5af875f2011-10-10 21:09:142224 const ExtensionIdSet *extension_ids =
2225 global_error.get_external_extension_ids();
2226 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
2227 iter != extension_ids->end(); ++iter) {
2228 extension_prefs_->AcknowledgeExternalExtension(*iter);
2229 }
2230 extension_ids = global_error.get_blacklisted_extension_ids();
2231 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
2232 iter != extension_ids->end(); ++iter) {
2233 extension_prefs_->AcknowledgeBlacklistedExtension(*iter);
2234 }
2235 extension_ids = global_error.get_orphaned_extension_ids();
2236 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
2237 iter != extension_ids->end(); ++iter) {
2238 extension_prefs_->AcknowledgeOrphanedExtension(*iter);
2239 }
2240}
2241
2242void ExtensionService::HandleExtensionAlertDetails(
[email protected]62433d32011-10-12 22:33:122243 const ExtensionGlobalError& global_error, Browser* browser) {
2244 if (browser) {
2245 browser->ShowExtensionsTab();
2246 }
[email protected]6c751e72010-11-23 10:11:102247}
2248
[email protected]a9f39a312010-12-23 22:14:272249void ExtensionService::UnloadExtension(
2250 const std::string& extension_id,
[email protected]814a7bf0f2011-08-13 05:30:592251 extension_misc::UnloadedExtensionReason reason) {
[email protected]27e469a2010-01-11 20:35:092252 // Make sure the extension gets deleted after we return from this function.
[email protected]9adb9692010-10-29 23:14:022253 scoped_refptr<const Extension> extension(
[email protected]8001df22011-04-28 19:59:472254 GetExtensionByIdInternal(extension_id, true, true, false));
[email protected]631cf822009-05-15 07:01:252255
[email protected]fa0c96732010-11-17 00:14:232256 // This method can be called via PostTask, so the extension may have been
2257 // unloaded by the time this runs.
[email protected]dd163fb02011-05-04 22:22:172258 if (!extension) {
2259 // In case the extension may have crashed/uninstalled. Allow the profile to
2260 // clean up its RequestContexts.
2261 profile_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:232262 return;
[email protected]dd163fb02011-05-04 22:22:172263 }
[email protected]0c6da502009-08-14 22:32:392264
[email protected]1eb175082010-02-10 09:26:162265 // Keep information about the extension so that we can reload it later
2266 // even if it's not permanently installed.
2267 unloaded_extension_paths_[extension->id()] = extension->path();
2268
[email protected]f17dbd42010-08-16 23:21:102269 // Clean up if the extension is meant to be enabled after a reload.
2270 disabled_extension_paths_.erase(extension->id());
2271
[email protected]d7e9a862010-11-03 21:57:492272 // Clean up runtime data.
2273 extension_runtime_data_.erase(extension_id);
2274
[email protected]0c6da502009-08-14 22:32:392275 ExtensionList::iterator iter = std::find(disabled_extensions_.begin(),
2276 disabled_extensions_.end(),
2277 extension.get());
2278 if (iter != disabled_extensions_.end()) {
[email protected]a9f39a312010-12-23 22:14:272279 UnloadedExtensionInfo details(extension, reason);
2280 details.already_disabled = true;
[email protected]0c6da502009-08-14 22:32:392281 disabled_extensions_.erase(iter);
[email protected]866930682009-08-18 22:53:472282 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272283 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]24e7a9d2009-11-04 11:11:342284 Source<Profile>(profile_),
[email protected]a9f39a312010-12-23 22:14:272285 Details<UnloadedExtensionInfo>(&details));
[email protected]dd163fb02011-05-04 22:22:172286 // Make sure the profile cleans up its RequestContexts when an already
2287 // disabled extension is unloaded (since they are also tracking the disabled
2288 // extensions).
2289 profile_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]0c6da502009-08-14 22:32:392290 return;
2291 }
2292
2293 iter = std::find(extensions_.begin(), extensions_.end(), extension.get());
[email protected]894bb502009-05-21 22:39:572294
[email protected]631cf822009-05-15 07:01:252295 // Remove the extension from our list.
2296 extensions_.erase(iter);
2297
[email protected]a9f39a312010-12-23 22:14:272298 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]631cf822009-05-15 07:01:252299}
2300
[email protected]eaa7dd182010-12-14 11:09:002301void ExtensionService::UnloadAllExtensions() {
[email protected]c8d407e2011-04-28 21:27:172302 profile_->GetExtensionSpecialStoragePolicy()->
2303 RevokeRightsForAllExtensions();
2304
[email protected]9f1087e2009-06-15 17:29:322305 extensions_.clear();
[email protected]cd500f72010-06-25 23:44:322306 disabled_extensions_.clear();
[email protected]bb7f40952011-01-13 00:21:202307 terminated_extension_ids_.clear();
2308 terminated_extensions_.clear();
[email protected]d7e9a862010-11-03 21:57:492309 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:322310
[email protected]c6e4a3412009-06-24 15:45:292311 // TODO(erikkay) should there be a notification for this? We can't use
2312 // EXTENSION_UNLOADED since that implies that the extension has been disabled
2313 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:322314}
2315
[email protected]eaa7dd182010-12-14 11:09:002316void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:322317 UnloadAllExtensions();
2318 LoadAllExtensions();
2319}
2320
[email protected]eaa7dd182010-12-14 11:09:002321void ExtensionService::GarbageCollectExtensions() {
[email protected]39d9f62c2010-12-03 10:48:502322 if (extension_prefs_->pref_service()->ReadOnly())
[email protected]ba399672010-04-06 15:42:392323 return;
2324
[email protected]ca3dbf52010-05-19 22:27:062325 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(
2326 extension_prefs_->GetInstalledExtensionsInfo());
2327
2328 std::map<std::string, FilePath> extension_paths;
2329 for (size_t i = 0; i < info->size(); ++i)
2330 extension_paths[info->at(i)->extension_id] = info->at(i)->extension_path;
2331
[email protected]14908b72011-04-20 06:54:362332 if (!BrowserThread::PostTask(
2333 BrowserThread::FILE, FROM_HERE,
[email protected]704d77e2011-10-17 18:05:292334 NewRunnableFunction(
[email protected]14908b72011-04-20 06:54:362335 &extension_file_util::GarbageCollectExtensions,
2336 install_directory_,
2337 extension_paths)))
2338 NOTREACHED();
[email protected]f6ccd652010-11-17 00:48:342339
2340 // Also garbage-collect themes. We check |profile_| to be
2341 // defensive; in the future, we may call GarbageCollectExtensions()
2342 // from somewhere other than Init() (e.g., in a timer).
2343 if (profile_) {
[email protected]18280372011-03-22 18:05:222344 ThemeServiceFactory::GetForProfile(profile_)->RemoveUnusedThemes();
[email protected]f6ccd652010-11-17 00:48:342345 }
[email protected]3cf4f0992009-02-03 23:00:302346}
2347
[email protected]eaa7dd182010-12-14 11:09:002348void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]93fd78f42009-07-10 16:43:172349 if (updater_.get()) {
2350 updater_->Start();
2351 }
[email protected]fa6a9102010-11-22 15:38:502352
2353 ready_ = true;
[email protected]e72e8eb82009-06-18 17:21:512354 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272355 chrome::NOTIFICATION_EXTENSIONS_READY,
[email protected]24e7a9d2009-11-04 11:11:342356 Source<Profile>(profile_),
[email protected]e72e8eb82009-06-18 17:21:512357 NotificationService::NoDetails());
2358}
2359
[email protected]49cd1572011-02-08 21:38:452360void ExtensionService::AddExtension(const Extension* extension) {
[email protected]ae09ca62009-08-21 19:46:462361 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:022362 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]9f1087e2009-06-15 17:29:322363
[email protected]c8d407e2011-04-28 21:27:172364 // TODO(jstritar): We may be able to get rid of this branch by overriding the
2365 // default extension state to DISABLED when the --disable-extensions flag
2366 // is set (http://crbug.com/29067).
2367 if (!extensions_enabled() &&
2368 !extension->is_theme() &&
2369 extension->location() != Extension::COMPONENT &&
2370 !Extension::IsExternalLocation(extension->location()))
2371 return;
2372
2373 SetBeingUpgraded(extension, false);
2374
[email protected]1eb175082010-02-10 09:26:162375 // The extension is now loaded, remove its data from unloaded extension map.
2376 unloaded_extension_paths_.erase(extension->id());
2377
[email protected]bb7f40952011-01-13 00:21:202378 // If a terminated extension is loaded, remove it from the terminated list.
2379 UntrackTerminatedExtension(extension->id());
2380
[email protected]f17dbd42010-08-16 23:21:102381 // If the extension was disabled for a reload, then enable it.
2382 if (disabled_extension_paths_.erase(extension->id()) > 0)
2383 EnableExtension(extension->id());
2384
[email protected]d728e002010-12-08 04:46:232385 // Check if the extension's privileges have changed and disable the
2386 // extension if necessary.
[email protected]902fd7b2011-07-27 18:42:312387 InitializePermissions(extension);
[email protected]0c6da502009-08-14 22:32:392388
[email protected]ad83ca242011-07-29 01:32:252389 bool disabled = extension_prefs_->IsExtensionDisabled(extension->id());
[email protected]36429da2011-07-11 20:25:182390 if (disabled) {
[email protected]c8d407e2011-04-28 21:27:172391 disabled_extensions_.push_back(scoped_extension);
2392 // TODO(aa): This seems dodgy. It seems that AddExtension() could get called
2393 // with a disabled extension for other reasons other than that an update was
2394 // disabled.
2395 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272396 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
[email protected]c8d407e2011-04-28 21:27:172397 Source<Profile>(profile_),
2398 Details<const Extension>(extension));
[email protected]3bdba0d2011-08-23 07:17:302399 SyncExtensionChangeIfNeeded(*extension);
[email protected]c8d407e2011-04-28 21:27:172400 return;
[email protected]e72e8eb82009-06-18 17:21:512401 }
[email protected]aab98a52009-12-02 03:22:352402
[email protected]c8d407e2011-04-28 21:27:172403 extensions_.push_back(scoped_extension);
[email protected]3bdba0d2011-08-23 07:17:302404 SyncExtensionChangeIfNeeded(*extension);
[email protected]c8d407e2011-04-28 21:27:172405 NotifyExtensionLoaded(extension);
[email protected]d1282c22011-10-18 02:00:392406 IdentifyAlertableExtensions();
[email protected]aab98a52009-12-02 03:22:352407}
2408
[email protected]902fd7b2011-07-27 18:42:312409void ExtensionService::InitializePermissions(const Extension* extension) {
2410 // If the extension has used the optional permissions API, it will have a
2411 // custom set of active permissions defined in the extension prefs. Here,
2412 // we update the extension's active permissions based on the prefs.
2413 scoped_refptr<ExtensionPermissionSet> active_permissions =
2414 extension_prefs()->GetActivePermissions(extension->id());
2415
2416 if (active_permissions.get()) {
2417 // We restrict the active permissions to be within the bounds defined in the
2418 // extension's manifest.
2419 // a) active permissions must be a subset of optional + default permissions
2420 // b) active permissions must contains all default permissions
2421 scoped_refptr<ExtensionPermissionSet> total_permissions =
2422 ExtensionPermissionSet::CreateUnion(
2423 extension->required_permission_set(),
2424 extension->optional_permission_set());
2425
2426 // Make sure the active permissions contain no more than optional + default.
2427 scoped_refptr<ExtensionPermissionSet> adjusted_active =
2428 ExtensionPermissionSet::CreateIntersection(
2429 total_permissions.get(), active_permissions.get());
2430
2431 // Make sure the active permissions contain the default permissions.
2432 adjusted_active = ExtensionPermissionSet::CreateUnion(
2433 extension->required_permission_set(), adjusted_active.get());
2434
2435 UpdateActivePermissions(extension, adjusted_active);
2436 }
2437
[email protected]8d888c12010-11-30 00:00:252438 // We keep track of all permissions the user has granted each extension.
2439 // This allows extensions to gracefully support backwards compatibility
2440 // by including unknown permissions in their manifests. When the user
2441 // installs the extension, only the recognized permissions are recorded.
2442 // When the unknown permissions become recognized (e.g., through browser
2443 // upgrade), we can prompt the user to accept these new permissions.
2444 // Extensions can also silently upgrade to less permissions, and then
2445 // silently upgrade to a version that adds these permissions back.
2446 //
2447 // For example, pretend that Chrome 10 includes a permission "omnibox"
2448 // for an API that adds suggestions to the omnibox. An extension can
2449 // maintain backwards compatibility while still having "omnibox" in the
2450 // manifest. If a user installs the extension on Chrome 9, the browser
2451 // will record the permissions it recognized, not including "omnibox."
2452 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
2453 // will disable the extension and prompt the user to approve the increase
2454 // in privileges. The extension could then release a new version that
2455 // removes the "omnibox" permission. When the user upgrades, Chrome will
2456 // still remember that "omnibox" had been granted, so that if the
2457 // extension once again includes "omnibox" in an upgrade, the extension
2458 // can upgrade without requiring this user's approval.
2459 const Extension* old = GetExtensionByIdInternal(extension->id(),
[email protected]8001df22011-04-28 19:59:472460 true, true, false);
[email protected]8d888c12010-11-30 00:00:252461 bool is_extension_upgrade = old != NULL;
2462 bool is_privilege_increase = false;
2463
[email protected]902fd7b2011-07-27 18:42:312464 // We only need to compare the granted permissions to the current permissions
2465 // if the extension is not allowed to silently increase its permissions.
2466 if (!extension->CanSilentlyIncreasePermissions()) {
[email protected]8d888c12010-11-30 00:00:252467 // Add all the recognized permissions if the granted permissions list
2468 // hasn't been initialized yet.
[email protected]902fd7b2011-07-27 18:42:312469 scoped_refptr<ExtensionPermissionSet> granted_permissions =
2470 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:522471 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:252472
2473 // Here, we check if an extension's privileges have increased in a manner
2474 // that requires the user's approval. This could occur because the browser
2475 // upgraded and recognized additional privileges, or an extension upgrades
2476 // to a version that requires additional privileges.
[email protected]0d3e4a22011-06-23 19:02:522477 is_privilege_increase =
[email protected]902fd7b2011-07-27 18:42:312478 granted_permissions->HasLessPrivilegesThan(
2479 extension->GetActivePermissions());
[email protected]8d888c12010-11-30 00:00:252480 }
2481
2482 if (is_extension_upgrade) {
[email protected]66e26872010-12-03 20:07:252483 // Other than for unpacked extensions, CrxInstaller should have guaranteed
2484 // that we aren't downgrading.
2485 if (extension->location() != Extension::LOAD)
2486 CHECK(extension->version()->CompareTo(*(old->version())) >= 0);
[email protected]8d888c12010-11-30 00:00:252487
2488 // Extensions get upgraded if the privileges are allowed to increase or
2489 // the privileges haven't increased.
2490 if (!is_privilege_increase) {
2491 SetBeingUpgraded(old, true);
2492 SetBeingUpgraded(extension, true);
2493 }
2494
2495 // To upgrade an extension in place, unload the old one and
2496 // then load the new one.
[email protected]814a7bf0f2011-08-13 05:30:592497 UnloadExtension(old->id(), extension_misc::UNLOAD_REASON_UPDATE);
[email protected]8d888c12010-11-30 00:00:252498 old = NULL;
2499 }
2500
2501 // Extension has changed permissions significantly. Disable it. A
2502 // notification should be sent by the caller.
2503 if (is_privilege_increase) {
[email protected]fe2dd7742011-04-19 22:52:492504 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
2505 RecordPermissionMessagesHistogram(
2506 extension, "Extensions.Permissions_AutoDisable");
2507 }
[email protected]b3317ad2011-04-28 23:46:002508 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:252509 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
2510 }
2511}
2512
[email protected]eaa7dd182010-12-14 11:09:002513void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:392514 std::set<std::string> extension_ids;
[email protected]aab98a52009-12-02 03:22:352515 for (size_t i = 0; i < extensions_.size(); ++i) {
[email protected]2a500c1b2010-11-05 15:27:342516 if (!extensions_[i]->is_theme() &&
2517 extensions_[i]->location() != Extension::COMPONENT)
[email protected]c8865962009-12-16 07:47:392518 extension_ids.insert(extensions_[i]->id());
[email protected]aab98a52009-12-02 03:22:352519 }
2520
2521 child_process_logging::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:252522}
2523
[email protected]e49002a2011-07-13 03:40:442524void ExtensionService::OnLoadSingleExtension(const Extension* extension,
2525 bool prompt_for_plugins) {
[email protected]5fb889382011-06-03 00:29:202526 // If this is a new install of an extension with plugins, prompt the user
2527 // first.
[email protected]e49002a2011-07-13 03:40:442528 if (show_extensions_prompts_ && prompt_for_plugins &&
[email protected]5fb889382011-06-03 00:29:202529 !extension->plugins().empty() &&
2530 disabled_extension_paths_.find(extension->id()) ==
2531 disabled_extension_paths_.end()) {
2532 SimpleExtensionLoadPrompt* prompt = new SimpleExtensionLoadPrompt(
2533 profile_, weak_ptr_factory_.GetWeakPtr(), extension);
2534 prompt->ShowPrompt();
2535 return; // continues in SimpleExtensionLoadPrompt::InstallUI*
2536 }
[email protected]f1b66652011-08-25 23:30:582537 OnExtensionInstalled(extension, false, -1); // Not from web store.
[email protected]5fb889382011-06-03 00:29:202538}
2539
[email protected]8266d662011-07-12 21:53:262540void ExtensionService::OnExtensionInstalled(
[email protected]cc829cf2011-08-22 21:01:352541 const Extension* extension, bool from_webstore, int page_index) {
[email protected]a29a517a2011-01-21 21:11:122542 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:202543
[email protected]4416c5a2010-06-26 01:28:572544 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:022545 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]c3cfb012011-04-06 22:07:352546 const std::string& id = extension->id();
[email protected]8a87a5332011-08-11 17:54:592547 // Extensions installed by policy can't be disabled. So even if a previous
2548 // installation disabled the extension, make sure it is now enabled.
2549 bool initial_enable =
2550 !extension_prefs_->IsExtensionDisabled(id) ||
2551 !Extension::UserMayDisable(extension->location());
[email protected]b2907fd2011-03-25 16:43:372552 PendingExtensionInfo pending_extension_info;
[email protected]c3cfb012011-04-06 22:07:352553 if (pending_extension_manager()->GetById(id, &pending_extension_info)) {
2554 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:322555
[email protected]483d1ff2011-03-03 17:12:022556 if (!pending_extension_info.ShouldAllowInstall(*extension)) {
[email protected]4416c5a2010-06-26 01:28:572557 LOG(WARNING)
[email protected]ae6cb3022011-04-04 20:11:542558 << "ShouldAllowInstall() returned false for "
[email protected]c3cfb012011-04-06 22:07:352559 << id << " of type " << extension->GetType()
[email protected]7fa19f82010-12-21 19:40:082560 << " and update URL " << extension->update_url().spec()
2561 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:542562
2563 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272564 chrome::NOTIFICATION_EXTENSION_INSTALL_NOT_ALLOWED,
[email protected]ae6cb3022011-04-04 20:11:542565 Source<Profile>(profile_),
2566 Details<const Extension>(extension));
2567
[email protected]4416c5a2010-06-26 01:28:572568 // Delete the extension directory since we're not going to
2569 // load it.
[email protected]14908b72011-04-20 06:54:362570 if (!BrowserThread::PostTask(
2571 BrowserThread::FILE, FROM_HERE,
[email protected]704d77e2011-10-17 18:05:292572 NewRunnableFunction(&extension_file_util::DeleteFile,
2573 extension->path(), true)))
[email protected]14908b72011-04-20 06:54:362574 NOTREACHED();
[email protected]4416c5a2010-06-26 01:28:572575 return;
2576 }
[email protected]4416c5a2010-06-26 01:28:572577 } else {
[email protected]c3cfb012011-04-06 22:07:352578 // We explicitly want to re-enable an uninstalled external
2579 // extension; if we're here, that means the user is manually
2580 // installing the extension.
[email protected]6cc7dbae2011-04-29 21:18:332581 if (IsExternalExtensionUninstalled(id)) {
2582 initial_enable = true;
2583 }
[email protected]aa142702010-03-26 01:26:332584 }
2585
[email protected]07533022011-06-27 20:42:552586 // Do not record the install histograms for upgrades.
2587 if (!GetExtensionByIdInternal(extension->id(), true, true, false)) {
2588 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
2589 extension->GetType(), 100);
2590 RecordPermissionMessagesHistogram(
2591 extension, "Extensions.Permissions_Install");
2592 }
2593
[email protected]eda3c362010-11-12 08:08:232594 ShownSectionsHandler::OnExtensionInstalled(profile_->GetPrefs(), extension);
[email protected]4416c5a2010-06-26 01:28:572595 extension_prefs_->OnExtensionInstalled(
[email protected]8266d662011-07-12 21:53:262596 extension,
2597 initial_enable ? Extension::ENABLED : Extension::DISABLED,
[email protected]cc829cf2011-08-22 21:01:352598 from_webstore,
2599 page_index);
[email protected]25b34332009-06-05 21:53:192600
[email protected]cdfca9702011-08-08 16:07:012601 // Unpacked extensions default to allowing file access, but if that has been
2602 // overridden, don't reset the value.
[email protected]b6e64fd2011-08-09 19:49:192603 if (Extension::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]cdfca9702011-08-08 16:07:012604 !extension_prefs_->HasAllowFileAccessSetting(id)) {
2605 extension_prefs_->SetAllowFileAccess(id, true);
2606 }
2607
[email protected]b6cf240f2011-10-15 22:09:532608 // If the extension should automatically block network startup (e.g., it uses
2609 // the webRequest API), set the preference. Otherwise clear it, in case the
2610 // extension stopped using a relevant API.
2611 extension_prefs_->SetDelaysNetworkRequests(
2612 extension->id(), extension->ImplicitlyDelaysNetworkStartup());
2613
[email protected]a3e61e82011-04-15 20:32:082614 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272615 chrome::NOTIFICATION_EXTENSION_INSTALLED,
[email protected]a3e61e82011-04-15 20:32:082616 Source<Profile>(profile_),
2617 Details<const Extension>(extension));
[email protected]7577a5c52009-07-30 06:21:582618
[email protected]49cd1572011-02-08 21:38:452619 // Transfer ownership of |extension| to AddExtension.
2620 AddExtension(scoped_extension);
[email protected]4a190632009-05-09 01:07:422621}
2622
[email protected]eaa7dd182010-12-14 11:09:002623const Extension* ExtensionService::GetExtensionByIdInternal(
[email protected]8001df22011-04-28 19:59:472624 const std::string& id, bool include_enabled, bool include_disabled,
2625 bool include_terminated) const {
[email protected]e957fe52009-06-23 16:51:052626 std::string lowercase_id = StringToLowerASCII(id);
[email protected]0c6da502009-08-14 22:32:392627 if (include_enabled) {
2628 for (ExtensionList::const_iterator iter = extensions_.begin();
2629 iter != extensions_.end(); ++iter) {
2630 if ((*iter)->id() == lowercase_id)
2631 return *iter;
2632 }
2633 }
2634 if (include_disabled) {
2635 for (ExtensionList::const_iterator iter = disabled_extensions_.begin();
2636 iter != disabled_extensions_.end(); ++iter) {
2637 if ((*iter)->id() == lowercase_id)
2638 return *iter;
2639 }
[email protected]ce5c4502009-05-06 16:46:112640 }
[email protected]8001df22011-04-28 19:59:472641 if (include_terminated) {
2642 for (ExtensionList::const_iterator iter = terminated_extensions_.begin();
2643 iter != terminated_extensions_.end(); ++iter) {
2644 if ((*iter)->id() == lowercase_id)
2645 return *iter;
2646 }
2647 }
[email protected]ce5c4502009-05-06 16:46:112648 return NULL;
2649}
2650
[email protected]bb7f40952011-01-13 00:21:202651void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
2652 if (terminated_extension_ids_.insert(extension->id()).second)
2653 terminated_extensions_.push_back(make_scoped_refptr(extension));
[email protected]fa2416f2011-05-03 08:41:202654
[email protected]814a7bf0f2011-08-13 05:30:592655 UnloadExtension(extension->id(), extension_misc::UNLOAD_REASON_DISABLE);
[email protected]bb7f40952011-01-13 00:21:202656}
2657
2658void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]fa2416f2011-05-03 08:41:202659 std::string lowercase_id = StringToLowerASCII(id);
2660 if (terminated_extension_ids_.erase(lowercase_id) <= 0)
[email protected]bb7f40952011-01-13 00:21:202661 return;
2662
[email protected]bb7f40952011-01-13 00:21:202663 for (ExtensionList::iterator iter = terminated_extensions_.begin();
2664 iter != terminated_extensions_.end(); ++iter) {
2665 if ((*iter)->id() == lowercase_id) {
2666 terminated_extensions_.erase(iter);
2667 return;
2668 }
2669 }
2670}
2671
[email protected]0dfe05c2011-02-23 23:03:362672const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:472673 const std::string& id) const {
2674 return GetExtensionByIdInternal(id, false, false, true);
2675}
2676
2677const Extension* ExtensionService::GetInstalledExtension(
2678 const std::string& id) const {
2679 return GetExtensionByIdInternal(id, true, true, true);
[email protected]0dfe05c2011-02-23 23:03:362680}
2681
[email protected]eaa7dd182010-12-14 11:09:002682const Extension* ExtensionService::GetWebStoreApp() {
[email protected]d3071992010-10-08 15:24:072683 return GetExtensionById(extension_misc::kWebStoreAppId, false);
2684}
2685
[email protected]eaa7dd182010-12-14 11:09:002686const Extension* ExtensionService::GetExtensionByURL(const GURL& url) {
[email protected]a888b29e62010-04-01 13:38:572687 return url.scheme() != chrome::kExtensionScheme ? NULL :
2688 GetExtensionById(url.host(), false);
2689}
2690
[email protected]eaa7dd182010-12-14 11:09:002691const Extension* ExtensionService::GetExtensionByWebExtent(const GURL& url) {
[email protected]a888b29e62010-04-01 13:38:572692 for (size_t i = 0; i < extensions_.size(); ++i) {
[email protected]cced75a2011-05-20 08:31:122693 if (extensions_[i]->web_extent().MatchesURL(url))
[email protected]a888b29e62010-04-01 13:38:572694 return extensions_[i];
2695 }
2696 return NULL;
[email protected]9f1087e2009-06-15 17:29:322697}
2698
[email protected]f9c292b32011-09-13 16:14:052699const Extension* ExtensionService::GetDisabledExtensionByWebExtent(
2700 const GURL& url) {
2701 for (size_t i = 0; i < disabled_extensions_.size(); ++i) {
2702 if (disabled_extensions_[i]->web_extent().MatchesURL(url))
2703 return disabled_extensions_[i];
2704 }
2705 return NULL;
2706}
2707
[email protected]eaa7dd182010-12-14 11:09:002708bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]056ad2a2011-07-12 02:13:552709 // Allow bindings for all packaged extensions.
2710 // Note that GetExtensionByURL may return an Extension for hosted apps
2711 // if the URL came from GetEffectiveURL.
2712 const Extension* extension = GetExtensionByURL(url);
2713 if (extension && extension->GetType() != Extension::TYPE_HOSTED_APP)
[email protected]583d45c12010-08-31 02:48:122714 return true;
2715
2716 // Allow bindings for all component, hosted apps.
[email protected]056ad2a2011-07-12 02:13:552717 if (!extension)
2718 extension = GetExtensionByWebExtent(url);
[email protected]583d45c12010-08-31 02:48:122719 return (extension && extension->location() == Extension::COMPONENT);
2720}
2721
[email protected]eaa7dd182010-12-14 11:09:002722const Extension* ExtensionService::GetExtensionByOverlappingWebExtent(
[email protected]cced75a2011-05-20 08:31:122723 const URLPatternSet& extent) {
[email protected]22c966c2010-06-26 06:35:022724 for (size_t i = 0; i < extensions_.size(); ++i) {
2725 if (extensions_[i]->web_extent().OverlapsWith(extent))
2726 return extensions_[i];
2727 }
2728
[email protected]6d2e60bd2010-06-03 22:37:392729 return NULL;
2730}
2731
[email protected]eaa7dd182010-12-14 11:09:002732const SkBitmap& ExtensionService::GetOmniboxIcon(
[email protected]b671760b2010-07-15 21:13:472733 const std::string& extension_id) {
2734 return omnibox_icon_manager_.GetIcon(extension_id);
2735}
2736
[email protected]eaa7dd182010-12-14 11:09:002737const SkBitmap& ExtensionService::GetOmniboxPopupIcon(
[email protected]29d0d4ac2010-09-08 21:10:312738 const std::string& extension_id) {
2739 return omnibox_popup_icon_manager_.GetIcon(extension_id);
2740}
2741
[email protected]eaa7dd182010-12-14 11:09:002742void ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322743 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:142744 const Version* version,
[email protected]8ef78fd2010-08-19 17:14:322745 const FilePath& path,
2746 Extension::Location location) {
[email protected]ab22ba42011-01-14 16:36:382747 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2748 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012749 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]683d0702010-12-06 16:25:572750 return;
[email protected]a8af9fdb2010-10-28 21:52:202751
[email protected]8e4560b62011-01-14 10:09:142752 DCHECK(version);
2753
[email protected]7577a5c52009-07-30 06:21:582754 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492755 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582756 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022757 const Extension* existing = GetExtensionById(id, true);
[email protected]7577a5c52009-07-30 06:21:582758 if (existing) {
[email protected]8e4560b62011-01-14 10:09:142759 switch (existing->version()->CompareTo(*version)) {
[email protected]7577a5c52009-07-30 06:21:582760 case -1: // existing version is older, we should upgrade
2761 break;
2762 case 0: // existing version is same, do nothing
2763 return;
2764 case 1: // existing version is newer, uh-oh
2765 LOG(WARNING) << "Found external version of extension " << id
2766 << "that is older than current version. Current version "
2767 << "is: " << existing->VersionString() << ". New version "
2768 << "is: " << version << ". Keeping current version.";
2769 return;
2770 }
2771 }
2772
[email protected]b2907fd2011-03-25 16:43:372773 pending_extension_manager()->AddFromExternalFile(id, location);
[email protected]9c635f22010-12-02 09:36:362774
[email protected]14908b72011-04-20 06:54:362775 // no client (silent install)
2776 scoped_refptr<CrxInstaller> installer(MakeCrxInstaller(NULL));
[email protected]6dfbbf82010-03-12 23:09:162777 installer->set_install_source(location);
2778 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072779 installer->set_expected_version(*version);
2780 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]6dfbbf82010-03-12 23:09:162781 installer->InstallCrx(path);
[email protected]7577a5c52009-07-30 06:21:582782}
2783
[email protected]eaa7dd182010-12-14 11:09:002784void ExtensionService::ReportExtensionLoadError(
[email protected]d11c8e92009-10-20 23:26:402785 const FilePath& extension_path,
2786 const std::string &error,
[email protected]d11c8e92009-10-20 23:26:402787 bool be_noisy) {
2788 NotificationService* service = NotificationService::current();
[email protected]655b2b1a2011-10-13 17:13:062789 service->Notify(chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
[email protected]24e7a9d2009-11-04 11:11:342790 Source<Profile>(profile_),
[email protected]d11c8e92009-10-20 23:26:402791 Details<const std::string>(&error));
2792
[email protected]8a205c02011-02-04 20:41:332793 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]18d4b6c2010-09-21 03:21:042794 std::string message = base::StringPrintf(
2795 "Could not load extension from '%s'. %s",
2796 path_str.c_str(), error.c_str());
[email protected]d11c8e92009-10-20 23:26:402797 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2798}
2799
[email protected]eaa7dd182010-12-14 11:09:002800void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]406027c02010-09-27 08:03:182801 ExtensionHost* host) {
2802 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332803 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182804 if (iter == orphaned_dev_tools_.end())
2805 return;
2806
2807 DevToolsManager::GetInstance()->AttachClientHost(
2808 iter->second, host->render_view_host());
2809 orphaned_dev_tools_.erase(iter);
2810}
2811
[email protected]432115822011-07-10 15:52:272812void ExtensionService::Observe(int type,
[email protected]fa2416f2011-05-03 08:41:202813 const NotificationSource& source,
2814 const NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272815 switch (type) {
2816 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]bc535ee52010-08-31 18:40:322817 if (profile_ != Source<Profile>(source).ptr()->GetOriginalProfile())
2818 break;
[email protected]a4ed6282009-12-14 20:51:162819
[email protected]f128af42010-08-05 18:05:262820 ExtensionHost* host = Details<ExtensionHost>(details).ptr();
2821
[email protected]fa2416f2011-05-03 08:41:202822 // Mark the extension as terminated and Unload it. We want it to
2823 // be in a consistent state: either fully working or not loaded
2824 // at all, but never half-crashed. We do it in a PostTask so
2825 // that other handlers of this notification will still have
2826 // access to the Extension and ExtensionHost.
[email protected]14908b72011-04-20 06:54:362827 MessageLoop::current()->PostTask(
2828 FROM_HERE,
[email protected]704d77e2011-10-17 18:05:292829 method_factory_.NewRunnableMethod(
[email protected]fa2416f2011-05-03 08:41:202830 &ExtensionService::TrackTerminatedExtension,
2831 host->extension()));
[email protected]31f77262009-12-02 20:48:532832 break;
2833 }
[email protected]432115822011-07-10 15:52:272834 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
[email protected]77a6970c2011-04-23 16:58:562835 RenderProcessHost* process = Source<RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192836 Profile* host_profile =
2837 Profile::FromBrowserContext(process->browser_context());
2838 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2839 break;
2840
[email protected]77a6970c2011-04-23 16:58:562841 // Valid extension function names, used to setup bindings in renderer.
2842 std::vector<std::string> function_names;
2843 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names);
2844 process->Send(new ExtensionMsg_SetFunctionNames(function_names));
[email protected]31f77262009-12-02 20:48:532845
[email protected]77a6970c2011-04-23 16:58:562846 // Scripting whitelist. This is modified by tests and must be communicated
2847 // to renderers.
2848 process->Send(new ExtensionMsg_SetScriptingWhitelist(
2849 *Extension::GetScriptingWhitelist()));
2850
2851 // Loaded extensions.
2852 for (size_t i = 0; i < extensions_.size(); ++i) {
2853 process->Send(new ExtensionMsg_Loaded(
[email protected]15379642011-08-11 22:22:052854 ExtensionMsg_Loaded_Params(extensions_[i])));
[email protected]77a6970c2011-04-23 16:58:562855 }
2856 break;
2857 }
[email protected]432115822011-07-10 15:52:272858 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]da5683db2011-04-23 17:12:212859 RenderProcessHost* process = Source<RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192860 Profile* host_profile =
2861 Profile::FromBrowserContext(process->browser_context());
2862 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2863 break;
2864
[email protected]da5683db2011-04-23 17:12:212865 installed_app_hosts_.erase(process->id());
2866 break;
2867 }
[email protected]432115822011-07-10 15:52:272868 case chrome::NOTIFICATION_PREF_CHANGED: {
[email protected]aa96d3a2010-08-21 08:45:252869 std::string* pref_name = Details<std::string>(details).ptr();
[email protected]6c751e72010-11-23 10:11:102870 if (*pref_name == prefs::kExtensionInstallAllowList ||
2871 *pref_name == prefs::kExtensionInstallDenyList) {
2872 CheckAdminBlacklist();
[email protected]6c751e72010-11-23 10:11:102873 } else {
2874 NOTREACHED() << "Unexpected preference name.";
2875 }
[email protected]aa96d3a2010-08-21 08:45:252876 break;
2877 }
[email protected]d91268022011-08-26 13:17:372878 case chrome::NOTIFICATION_IMPORT_FINISHED: {
2879 registrar_.Remove(this, chrome::NOTIFICATION_IMPORT_FINISHED,
2880 Source<Profile>(profile_));
2881 InitEventRouters();
2882 break;
2883 }
[email protected]aa96d3a2010-08-21 08:45:252884
[email protected]4814b512009-11-07 00:12:292885 default:
2886 NOTREACHED() << "Unexpected notification type.";
2887 }
2888}
2889
[email protected]eaa7dd182010-12-14 11:09:002890bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182891 return !GetAppIds().empty();
2892}
[email protected]377011d2010-07-20 04:18:502893
[email protected]eaa7dd182010-12-14 11:09:002894ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182895 ExtensionIdSet result;
[email protected]377011d2010-07-20 04:18:502896 for (ExtensionList::const_iterator it = extensions_.begin();
2897 it != extensions_.end(); ++it) {
[email protected]ec5b50d2010-10-09 16:35:182898 if ((*it)->is_app() && (*it)->location() != Extension::COMPONENT)
2899 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502900 }
2901
[email protected]ec5b50d2010-10-09 16:35:182902 return result;
[email protected]377011d2010-07-20 04:18:502903}
[email protected]d7e9a862010-11-03 21:57:492904
[email protected]14908b72011-04-20 06:54:362905scoped_refptr<CrxInstaller> ExtensionService::MakeCrxInstaller(
2906 ExtensionInstallUI* client) {
2907 return new CrxInstaller(weak_ptr_factory_.GetWeakPtr(), client);
2908}
2909
[email protected]eaa7dd182010-12-14 11:09:002910bool ExtensionService::IsBackgroundPageReady(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492911 return (extension->background_url().is_empty() ||
2912 extension_runtime_data_[extension->id()].background_page_ready);
2913}
2914
[email protected]eaa7dd182010-12-14 11:09:002915void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492916 DCHECK(!extension->background_url().is_empty());
2917 extension_runtime_data_[extension->id()].background_page_ready = true;
2918 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272919 chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
[email protected]d7e9a862010-11-03 21:57:492920 Source<const Extension>(extension),
2921 NotificationService::NoDetails());
2922}
2923
[email protected]eaa7dd182010-12-14 11:09:002924bool ExtensionService::IsBeingUpgraded(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492925 return extension_runtime_data_[extension->id()].being_upgraded;
2926}
2927
[email protected]eaa7dd182010-12-14 11:09:002928void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]d7e9a862010-11-03 21:57:492929 bool value) {
2930 extension_runtime_data_[extension->id()].being_upgraded = value;
2931}
[email protected]1bead0712010-11-27 17:41:532932
[email protected]eaa7dd182010-12-14 11:09:002933PropertyBag* ExtensionService::GetPropertyBag(const Extension* extension) {
[email protected]1bead0712010-11-27 17:41:532934 return &extension_runtime_data_[extension->id()].property_bag;
2935}
[email protected]cebc3dc2011-04-18 17:15:002936
2937void ExtensionService::RegisterNaClModule(const GURL& url,
2938 const std::string& mime_type) {
2939 NaClModuleInfo info;
2940 info.url = url;
2941 info.mime_type = mime_type;
2942
2943 DCHECK(FindNaClModule(url) == nacl_module_list_.end());
2944 nacl_module_list_.push_front(info);
2945}
2946
2947void ExtensionService::UnregisterNaClModule(const GURL& url) {
2948 NaClModuleInfoList::iterator iter = FindNaClModule(url);
2949 DCHECK(iter != nacl_module_list_.end());
2950 nacl_module_list_.erase(iter);
2951}
2952
[email protected]ed0ba002011-05-26 16:55:132953void ExtensionService::UpdatePluginListWithNaClModules() {
[email protected]300c0ea2011-07-15 23:04:332954 // An extension has been added which has a nacl_module component, which means
2955 // there is a MIME type that module wants to handle, so we need to add that
2956 // MIME type to plugins which handle NaCl modules in order to allow the
2957 // individual modules to handle these types.
[email protected]ed0ba002011-05-26 16:55:132958 const PepperPluginInfo* pepper_info = NULL;
2959 std::vector<PepperPluginInfo> plugins;
2960 PepperPluginRegistry::ComputeList(&plugins);
[email protected]300c0ea2011-07-15 23:04:332961
2962 // Search the entire plugin list for plugins that handle the NaCl MIME type.
2963 // There can be multiple plugins like this, for instance the internal NaCl
2964 // plugin and a plugin registered by --register-pepper-plugins during tests.
[email protected]ed0ba002011-05-26 16:55:132965 for (size_t i = 0; i < plugins.size(); ++i) {
[email protected]300c0ea2011-07-15 23:04:332966 pepper_info = &plugins[i];
2967 CHECK(pepper_info);
[email protected]91d9f3d2011-08-14 05:24:442968 std::vector<webkit::WebPluginMimeType>::const_iterator mime_iter;
[email protected]300c0ea2011-07-15 23:04:332969 // Check each MIME type the plugins handle for the NaCl MIME type.
2970 for (mime_iter = pepper_info->mime_types.begin();
2971 mime_iter != pepper_info->mime_types.end(); ++mime_iter) {
2972 if (mime_iter->mime_type == kNaClPluginMimeType) {
2973 // This plugin handles "application/x-nacl".
2974
2975 webkit::npapi::PluginList::Singleton()->
2976 UnregisterInternalPlugin(pepper_info->path);
2977
[email protected]91d9f3d2011-08-14 05:24:442978 webkit::WebPluginInfo info = pepper_info->ToWebPluginInfo();
[email protected]300c0ea2011-07-15 23:04:332979
2980 for (ExtensionService::NaClModuleInfoList::const_iterator iter =
2981 nacl_module_list_.begin();
2982 iter != nacl_module_list_.end(); ++iter) {
2983 // Add the MIME type specified in the extension to this NaCl plugin,
2984 // With an extra "nacl" argument to specify the location of the NaCl
2985 // manifest file.
[email protected]91d9f3d2011-08-14 05:24:442986 webkit::WebPluginMimeType mime_type_info;
[email protected]300c0ea2011-07-15 23:04:332987 mime_type_info.mime_type = iter->mime_type;
2988 mime_type_info.additional_param_names.push_back(UTF8ToUTF16("nacl"));
2989 mime_type_info.additional_param_values.push_back(
2990 UTF8ToUTF16(iter->url.spec()));
2991 info.mime_types.push_back(mime_type_info);
2992 }
2993
2994 webkit::npapi::PluginList::Singleton()->RefreshPlugins();
2995 webkit::npapi::PluginList::Singleton()->RegisterInternalPlugin(info);
2996 // This plugin has been modified, no need to check the rest of its
2997 // types, but continue checking other plugins.
2998 break;
2999 }
[email protected]ed0ba002011-05-26 16:55:133000 }
3001 }
[email protected]ed0ba002011-05-26 16:55:133002}
3003
[email protected]cebc3dc2011-04-18 17:15:003004ExtensionService::NaClModuleInfoList::iterator
3005 ExtensionService::FindNaClModule(const GURL& url) {
3006 for (NaClModuleInfoList::iterator iter = nacl_module_list_.begin();
3007 iter != nacl_module_list_.end(); ++iter) {
3008 if (iter->url == url)
3009 return iter;
3010 }
3011 return nacl_module_list_.end();
3012}