[email protected] | 1e8c93f | 2010-02-08 22:58:31 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/extensions/extensions_service.h" |
| 6 | |
[email protected] | e2eb4311 | 2009-05-29 21:19:54 | [diff] [blame] | 7 | #include "base/command_line.h" |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 8 | #include "base/file_util.h" |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 9 | #include "base/histogram.h" |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 10 | #include "base/string_util.h" |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 11 | #include "base/time.h" |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 12 | #include "base/values.h" |
[email protected] | 15730c4 | 2009-09-03 00:03:20 | [diff] [blame] | 13 | #include "chrome/browser/browser_process.h" |
[email protected] | dbb92e0d | 2009-08-20 16:18:21 | [diff] [blame] | 14 | #include "chrome/browser/chrome_thread.h" |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 15 | #include "chrome/browser/debugger/devtools_manager.h" |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 16 | #include "chrome/browser/extensions/crx_installer.h" |
[email protected] | 5cbe1e2 | 2010-01-30 01:18:56 | [diff] [blame] | 17 | #include "chrome/browser/extensions/extension_accessibility_api.h" |
[email protected] | 840b0db | 2009-11-20 03:00:38 | [diff] [blame] | 18 | #include "chrome/browser/extensions/extension_bookmarks_module.h" |
[email protected] | b68d5ed | 2009-04-16 02:41:28 | [diff] [blame] | 19 | #include "chrome/browser/extensions/extension_browser_event_router.h" |
[email protected] | 86c008e8 | 2009-08-28 20:26:05 | [diff] [blame] | 20 | #include "chrome/browser/extensions/extension_dom_ui.h" |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame] | 21 | #include "chrome/browser/extensions/extension_file_util.h" |
[email protected] | de768a83 | 2009-10-30 05:25:01 | [diff] [blame] | 22 | #include "chrome/browser/extensions/extension_history_api.h" |
[email protected] | b1748b1d8 | 2009-11-30 20:32:56 | [diff] [blame] | 23 | #include "chrome/browser/extensions/extension_host.h" |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 24 | #include "chrome/browser/extensions/extension_process_manager.h" |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 25 | #include "chrome/browser/extensions/extension_updater.h" |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 26 | #include "chrome/browser/extensions/external_extension_provider.h" |
| 27 | #include "chrome/browser/extensions/external_pref_extension_provider.h" |
[email protected] | 81e6378 | 2009-02-27 19:35:09 | [diff] [blame] | 28 | #include "chrome/browser/profile.h" |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 29 | #include "chrome/browser/net/chrome_url_request_context.h" |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 30 | #include "chrome/common/child_process_logging.h" |
[email protected] | e2eb4311 | 2009-05-29 21:19:54 | [diff] [blame] | 31 | #include "chrome/common/chrome_switches.h" |
[email protected] | 5b1a0e2 | 2009-05-26 19:00:58 | [diff] [blame] | 32 | #include "chrome/common/extensions/extension.h" |
[email protected] | d7b36dc | 2009-10-29 21:47:40 | [diff] [blame] | 33 | #include "chrome/common/extensions/extension_constants.h" |
[email protected] | 5b1a0e2 | 2009-05-26 19:00:58 | [diff] [blame] | 34 | #include "chrome/common/extensions/extension_error_reporter.h" |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 35 | #include "chrome/common/extensions/extension_l10n_util.h" |
[email protected] | 8289126 | 2008-12-24 00:21:26 | [diff] [blame] | 36 | #include "chrome/common/notification_service.h" |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 37 | #include "chrome/common/notification_type.h" |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 38 | #include "chrome/common/pref_names.h" |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 39 | #include "chrome/common/pref_service.h" |
[email protected] | a5720987 | 2009-05-04 22:53:14 | [diff] [blame] | 40 | #include "chrome/common/url_constants.h" |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 41 | |
[email protected] | 79db623 | 2009-02-13 20:51:20 | [diff] [blame] | 42 | #if defined(OS_WIN) |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 43 | #include "chrome/browser/extensions/external_registry_extension_provider_win.h" |
[email protected] | 79db623 | 2009-02-13 20:51:20 | [diff] [blame] | 44 | #endif |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 45 | |
[email protected] | 5ef47ec | 2010-01-28 05:58:05 | [diff] [blame] | 46 | using base::Time; |
| 47 | |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 48 | namespace errors = extension_manifest_errors; |
| 49 | |
[email protected] | b6ab96d | 2009-08-20 18:58:19 | [diff] [blame] | 50 | namespace { |
| 51 | |
| 52 | // Helper class to collect the IDs of every extension listed in the prefs. |
| 53 | class InstalledExtensionSet { |
| 54 | public: |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 55 | explicit InstalledExtensionSet(ExtensionPrefs* prefs) { |
| 56 | scoped_ptr<ExtensionPrefs::ExtensionsInfo> info( |
| 57 | ExtensionPrefs::CollectExtensionsInfo(prefs)); |
| 58 | |
| 59 | for (size_t i = 0; i < info->size(); ++i) { |
| 60 | std::string version; |
| 61 | const DictionaryValue* manifest = info->at(i)->extension_manifest.get(); |
| 62 | if (!manifest || |
| 63 | !manifest->GetString(extension_manifest_keys::kVersion, &version)) { |
| 64 | // Without a version, the extension is invalid. Ignoring it here will |
| 65 | // cause it to get garbage collected. |
| 66 | continue; |
| 67 | } |
| 68 | extensions_.insert(info->at(i)->extension_id); |
| 69 | versions_[info->at(i)->extension_id] = version; |
| 70 | } |
[email protected] | b6ab96d | 2009-08-20 18:58:19 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | const std::set<std::string>& extensions() { return extensions_; } |
[email protected] | 4559a7d | 2009-12-02 01:42:41 | [diff] [blame] | 74 | const std::map<std::string, std::string>& versions() { return versions_; } |
[email protected] | b6ab96d | 2009-08-20 18:58:19 | [diff] [blame] | 75 | |
| 76 | private: |
[email protected] | b6ab96d | 2009-08-20 18:58:19 | [diff] [blame] | 77 | std::set<std::string> extensions_; |
[email protected] | 4559a7d | 2009-12-02 01:42:41 | [diff] [blame] | 78 | std::map<std::string, std::string> versions_; |
[email protected] | b6ab96d | 2009-08-20 18:58:19 | [diff] [blame] | 79 | }; |
| 80 | |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 81 | } // namespace |
[email protected] | b6ab96d | 2009-08-20 18:58:19 | [diff] [blame] | 82 | |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 83 | // ExtensionsService. |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 84 | |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 85 | const char* ExtensionsService::kInstallDirectoryName = "Extensions"; |
| 86 | const char* ExtensionsService::kCurrentVersionFileName = "Current Version"; |
[email protected] | 494c06e | 2009-07-25 01:06:42 | [diff] [blame] | 87 | |
[email protected] | b7c2f25 | 2009-12-08 00:47:23 | [diff] [blame] | 88 | // static |
| 89 | bool ExtensionsService::IsDownloadFromGallery(const GURL& download_url, |
| 90 | const GURL& referrer_url) { |
| 91 | if (StartsWithASCII(download_url.spec(), |
| 92 | extension_urls::kMiniGalleryDownloadPrefix, false) && |
| 93 | StartsWithASCII(referrer_url.spec(), |
| 94 | extension_urls::kMiniGalleryBrowsePrefix, false)) { |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | if (StartsWithASCII(download_url.spec(), |
| 99 | extension_urls::kGalleryDownloadPrefix, false) && |
| 100 | StartsWithASCII(referrer_url.spec(), |
| 101 | extension_urls::kGalleryBrowsePrefix, false)) { |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | return false; |
| 106 | } |
| 107 | |
[email protected] | ac02528 | 2009-12-16 19:16:38 | [diff] [blame] | 108 | bool ExtensionsService::IsDownloadFromMiniGallery(const GURL& download_url) { |
| 109 | return StartsWithASCII(download_url.spec(), |
| 110 | extension_urls::kMiniGalleryDownloadPrefix, |
| 111 | false); // case_sensitive |
| 112 | } |
| 113 | |
[email protected] | 81e6378 | 2009-02-27 19:35:09 | [diff] [blame] | 114 | ExtensionsService::ExtensionsService(Profile* profile, |
[email protected] | 36a784c | 2009-06-23 06:21:08 | [diff] [blame] | 115 | const CommandLine* command_line, |
[email protected] | a9b00ac | 2009-06-25 21:03:23 | [diff] [blame] | 116 | PrefService* prefs, |
| 117 | const FilePath& install_directory, |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 118 | bool autoupdate_enabled) |
[email protected] | 6ef635e4 | 2009-07-26 06:16:12 | [diff] [blame] | 119 | : profile_(profile), |
| 120 | extension_prefs_(new ExtensionPrefs(prefs, install_directory)), |
[email protected] | a9b00ac | 2009-06-25 21:03:23 | [diff] [blame] | 121 | install_directory_(install_directory), |
[email protected] | 6d60703b | 2009-08-29 01:29:23 | [diff] [blame] | 122 | extensions_enabled_(true), |
[email protected] | e81dba3 | 2009-06-19 20:19:13 | [diff] [blame] | 123 | show_extensions_prompts_(true), |
[email protected] | e0360f2c | 2009-12-07 22:34:31 | [diff] [blame] | 124 | ready_(false), |
| 125 | ALLOW_THIS_IN_INITIALIZER_LIST(toolbar_model_(this)) { |
[email protected] | 36a784c | 2009-06-23 06:21:08 | [diff] [blame] | 126 | // Figure out if extension installation should be enabled. |
[email protected] | 6d60703b | 2009-08-29 01:29:23 | [diff] [blame] | 127 | if (command_line->HasSwitch(switches::kDisableExtensions)) { |
| 128 | extensions_enabled_ = false; |
| 129 | } else if (profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) { |
| 130 | extensions_enabled_ = false; |
[email protected] | 6b75ec3 | 2009-08-14 06:37:18 | [diff] [blame] | 131 | } |
[email protected] | 36a784c | 2009-06-23 06:21:08 | [diff] [blame] | 132 | |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 133 | registrar_.Add(this, NotificationType::EXTENSION_HOST_DID_STOP_LOADING, |
| 134 | NotificationService::AllSources()); |
[email protected] | a4ed628 | 2009-12-14 20:51:16 | [diff] [blame] | 135 | registrar_.Add(this, NotificationType::EXTENSION_PROCESS_TERMINATED, |
[email protected] | 31f7726 | 2009-12-02 20:48:53 | [diff] [blame] | 136 | Source<Profile>(profile_)); |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 137 | |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 138 | // Set up the ExtensionUpdater |
| 139 | if (autoupdate_enabled) { |
| 140 | int update_frequency = kDefaultUpdateFrequencySeconds; |
| 141 | if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) { |
[email protected] | c4e52f0d | 2009-11-06 19:55:16 | [diff] [blame] | 142 | update_frequency = StringToInt(command_line->GetSwitchValueASCII( |
| 143 | switches::kExtensionsUpdateFrequency)); |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 144 | } |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 145 | updater_ = new ExtensionUpdater(this, prefs, update_frequency); |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 146 | } |
| 147 | |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 148 | backend_ = new ExtensionsServiceBackend(install_directory_); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | ExtensionsService::~ExtensionsService() { |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 152 | UnloadAllExtensions(); |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 153 | if (updater_.get()) { |
| 154 | updater_->Stop(); |
| 155 | } |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 156 | } |
| 157 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 158 | void ExtensionsService::Init() { |
[email protected] | c6e4a341 | 2009-06-24 15:45:29 | [diff] [blame] | 159 | DCHECK(!ready_); |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 160 | DCHECK_EQ(extensions_.size(), 0u); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 161 | |
[email protected] | 95dd38f | 2009-10-20 20:09:15 | [diff] [blame] | 162 | // Hack: we need to ensure the ResourceDispatcherHost is ready before we load |
| 163 | // the first extension, because its members listen for loaded notifications. |
| 164 | g_browser_process->resource_dispatcher_host(); |
| 165 | |
[email protected] | de768a83 | 2009-10-30 05:25:01 | [diff] [blame] | 166 | // Start up the extension event routers. |
| 167 | ExtensionHistoryEventRouter::GetInstance()->ObserveProfile(profile_); |
[email protected] | 5cbe1e2 | 2010-01-30 01:18:56 | [diff] [blame] | 168 | ExtensionAccessibilityEventRouter::GetInstance()->ObserveProfile(profile_); |
[email protected] | de768a83 | 2009-10-30 05:25:01 | [diff] [blame] | 169 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 170 | LoadAllExtensions(); |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 171 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 172 | // TODO(erikkay) this should probably be deferred to a future point |
| 173 | // rather than running immediately at startup. |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 174 | CheckForExternalUpdates(); |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 175 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 176 | // TODO(erikkay) this should probably be deferred as well. |
| 177 | GarbageCollectExtensions(); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 178 | } |
| 179 | |
[email protected] | 3cf4f099 | 2009-02-03 23:00:30 | [diff] [blame] | 180 | void ExtensionsService::InstallExtension(const FilePath& extension_path) { |
[email protected] | 2a464a9 | 2009-08-01 17:58:35 | [diff] [blame] | 181 | CrxInstaller::Start(extension_path, install_directory_, Extension::INTERNAL, |
| 182 | "", // no expected id |
| 183 | false, // don't delete crx when complete |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 184 | true, // allow privilege increase |
[email protected] | 2a464a9 | 2009-08-01 17:58:35 | [diff] [blame] | 185 | this, |
| 186 | NULL); // no client (silent install) |
[email protected] | 3cf4f099 | 2009-02-03 23:00:30 | [diff] [blame] | 187 | } |
| 188 | |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 189 | void ExtensionsService::UpdateExtension(const std::string& id, |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 190 | const FilePath& extension_path) { |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 191 | if (!GetExtensionByIdInternal(id, true, true)) { |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 192 | LOG(WARNING) << "Will not update extension " << id << " because it is not " |
[email protected] | 4c96793 | 2009-07-31 01:15:49 | [diff] [blame] | 193 | << "installed"; |
| 194 | return; |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 195 | } |
| 196 | |
[email protected] | 2a464a9 | 2009-08-01 17:58:35 | [diff] [blame] | 197 | CrxInstaller::Start(extension_path, install_directory_, Extension::INTERNAL, |
| 198 | id, |
| 199 | true, // delete crx when complete |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 200 | false, // do not allow upgrade of privileges |
[email protected] | 2a464a9 | 2009-08-01 17:58:35 | [diff] [blame] | 201 | this, |
| 202 | NULL); // no client (silent install) |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 203 | } |
| 204 | |
[email protected] | 9cddd470 | 2009-07-27 22:09:40 | [diff] [blame] | 205 | void ExtensionsService::ReloadExtension(const std::string& extension_id) { |
[email protected] | b65272f | 2009-08-31 15:47:06 | [diff] [blame] | 206 | FilePath path; |
[email protected] | 61b41161 | 2009-11-10 23:17:41 | [diff] [blame] | 207 | Extension* current_extension = GetExtensionById(extension_id, false); |
[email protected] | 9cddd470 | 2009-07-27 22:09:40 | [diff] [blame] | 208 | |
[email protected] | b65272f | 2009-08-31 15:47:06 | [diff] [blame] | 209 | // Unload the extension if it's loaded. It might not be loaded if it crashed. |
| 210 | if (current_extension) { |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 211 | // If the extension has an inspector open for its background page, detach |
| 212 | // the inspector and hang onto a cookie for it, so that we can reattach |
| 213 | // later. |
| 214 | ExtensionProcessManager* manager = profile_->GetExtensionProcessManager(); |
| 215 | ExtensionHost* host = manager->GetBackgroundHostForExtension( |
| 216 | current_extension); |
| 217 | if (host) { |
| 218 | // Look for an open inspector for the background page. |
| 219 | int devtools_cookie = DevToolsManager::GetInstance()->DetachClientHost( |
| 220 | host->render_view_host()); |
| 221 | if (devtools_cookie >= 0) |
| 222 | orphaned_dev_tools_[extension_id] = devtools_cookie; |
| 223 | } |
| 224 | |
[email protected] | b65272f | 2009-08-31 15:47:06 | [diff] [blame] | 225 | path = current_extension->path(); |
| 226 | UnloadExtension(extension_id); |
[email protected] | 1eb17508 | 2010-02-10 09:26:16 | [diff] [blame^] | 227 | } else { |
| 228 | path = unloaded_extension_paths_[extension_id]; |
[email protected] | b65272f | 2009-08-31 15:47:06 | [diff] [blame] | 229 | } |
| 230 | |
[email protected] | 1eb17508 | 2010-02-10 09:26:16 | [diff] [blame^] | 231 | // We should always be able to remember the extension's path. If it's not in |
| 232 | // the map, someone failed to update |unloaded_extension_paths_|. |
| 233 | CHECK(!path.empty()); |
[email protected] | b65272f | 2009-08-31 15:47:06 | [diff] [blame] | 234 | |
[email protected] | 1eb17508 | 2010-02-10 09:26:16 | [diff] [blame^] | 235 | LoadExtension(path); |
[email protected] | 9cddd470 | 2009-07-27 22:09:40 | [diff] [blame] | 236 | } |
| 237 | |
[email protected] | 27b985d | 2009-06-25 17:53:15 | [diff] [blame] | 238 | void ExtensionsService::UninstallExtension(const std::string& extension_id, |
| 239 | bool external_uninstall) { |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 240 | Extension* extension = GetExtensionByIdInternal(extension_id, true, true); |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 241 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 242 | // Callers should not send us nonexistant extensions. |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 243 | DCHECK(extension); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 244 | |
[email protected] | 27b985d | 2009-06-25 17:53:15 | [diff] [blame] | 245 | extension_prefs_->OnExtensionUninstalled(extension, external_uninstall); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 246 | |
| 247 | // Tell the backend to start deleting installed extensions on the file thread. |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 248 | if (Extension::LOAD != extension->location()) { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 249 | ChromeThread::PostTask( |
| 250 | ChromeThread::FILE, FROM_HERE, |
| 251 | NewRunnableFunction( |
| 252 | &extension_file_util::UninstallExtension, extension_id, |
| 253 | install_directory_)); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 254 | } |
| 255 | |
[email protected] | 86c008e8 | 2009-08-28 20:26:05 | [diff] [blame] | 256 | ExtensionDOMUI::UnregisterChromeURLOverrides(profile_, |
| 257 | extension->GetChromeURLOverrides()); |
| 258 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 259 | UnloadExtension(extension_id); |
| 260 | } |
| 261 | |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 262 | void ExtensionsService::EnableExtension(const std::string& extension_id) { |
| 263 | Extension* extension = GetExtensionByIdInternal(extension_id, false, true); |
| 264 | if (!extension) { |
| 265 | NOTREACHED() << "Trying to enable an extension that isn't disabled."; |
| 266 | return; |
| 267 | } |
| 268 | |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 269 | // Remember that we enabled it, unless it's temporary. |
| 270 | if (extension->location() != Extension::LOAD) |
| 271 | extension_prefs_->SetExtensionState(extension, Extension::ENABLED); |
| 272 | |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 273 | // Move it over to the enabled list. |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 274 | extensions_.push_back(extension); |
| 275 | ExtensionList::iterator iter = std::find(disabled_extensions_.begin(), |
| 276 | disabled_extensions_.end(), |
| 277 | extension); |
| 278 | disabled_extensions_.erase(iter); |
| 279 | |
[email protected] | 86c008e8 | 2009-08-28 20:26:05 | [diff] [blame] | 280 | ExtensionDOMUI::RegisterChromeURLOverrides(profile_, |
| 281 | extension->GetChromeURLOverrides()); |
| 282 | |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 283 | NotifyExtensionLoaded(extension); |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 284 | UpdateActiveExtensionsInCrashReporter(); |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 285 | } |
| 286 | |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 287 | void ExtensionsService::DisableExtension(const std::string& extension_id) { |
| 288 | Extension* extension = GetExtensionByIdInternal(extension_id, true, false); |
[email protected] | b2ba996 | 2009-12-10 20:10:15 | [diff] [blame] | 289 | // The extension may have been disabled already. |
| 290 | if (!extension) |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 291 | return; |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 292 | |
| 293 | // Remember that we disabled it, unless it's temporary. |
| 294 | if (extension->location() != Extension::LOAD) |
| 295 | extension_prefs_->SetExtensionState(extension, Extension::DISABLED); |
| 296 | |
| 297 | // Move it over to the disabled list. |
| 298 | disabled_extensions_.push_back(extension); |
| 299 | ExtensionList::iterator iter = std::find(extensions_.begin(), |
| 300 | extensions_.end(), |
| 301 | extension); |
| 302 | extensions_.erase(iter); |
| 303 | |
| 304 | ExtensionDOMUI::UnregisterChromeURLOverrides(profile_, |
| 305 | extension->GetChromeURLOverrides()); |
| 306 | |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 307 | NotifyExtensionUnloaded(extension); |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 308 | UpdateActiveExtensionsInCrashReporter(); |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 309 | } |
| 310 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 311 | void ExtensionsService::LoadExtension(const FilePath& extension_path) { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 312 | ChromeThread::PostTask( |
| 313 | ChromeThread::FILE, FROM_HERE, |
| 314 | NewRunnableMethod( |
| 315 | backend_.get(), |
| 316 | &ExtensionsServiceBackend::LoadSingleExtension, |
| 317 | extension_path, scoped_refptr<ExtensionsService>(this))); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | void ExtensionsService::LoadAllExtensions() { |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 321 | base::TimeTicks start_time = base::TimeTicks::Now(); |
| 322 | |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 323 | // Load the previously installed extensions. |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 324 | scoped_ptr<ExtensionPrefs::ExtensionsInfo> info( |
| 325 | ExtensionPrefs::CollectExtensionsInfo(extension_prefs_.get())); |
| 326 | |
| 327 | // If any extensions need localization, we bounce them all to the file thread |
| 328 | // for re-reading and localization. |
| 329 | for (size_t i = 0; i < info->size(); ++i) { |
| 330 | if (extension_l10n_util::ShouldRelocalizeManifest(*info->at(i))) { |
| 331 | ChromeThread::PostTask( |
| 332 | ChromeThread::FILE, FROM_HERE, NewRunnableMethod( |
| 333 | backend_.get(), |
| 334 | &ExtensionsServiceBackend::ReloadExtensionManifestsForLocaleChanged, |
| 335 | info.release(), // Callee takes ownership of the memory. |
| 336 | start_time, |
| 337 | scoped_refptr<ExtensionsService>(this))); |
| 338 | return; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | // Don't update prefs. |
| 343 | // Callee takes ownership of the memory. |
| 344 | ContinueLoadAllExtensions(info.release(), start_time, false); |
| 345 | } |
| 346 | |
| 347 | void ExtensionsService::ContinueLoadAllExtensions( |
| 348 | ExtensionPrefs::ExtensionsInfo* extensions_info, |
| 349 | base::TimeTicks start_time, |
| 350 | bool write_to_prefs) { |
| 351 | scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(extensions_info); |
| 352 | |
| 353 | for (size_t i = 0; i < info->size(); ++i) { |
| 354 | LoadInstalledExtension(*info->at(i), write_to_prefs); |
| 355 | } |
| 356 | |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 357 | OnLoadedInstalledExtensions(); |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 358 | |
| 359 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadAll", extensions_.size()); |
| 360 | UMA_HISTOGRAM_COUNTS_100("Extensions.Disabled", disabled_extensions_.size()); |
| 361 | |
| 362 | if (extensions_.size()) { |
| 363 | UMA_HISTOGRAM_TIMES("Extensions.LoadAllTime", |
| 364 | base::TimeTicks::Now() - start_time); |
| 365 | |
| 366 | int user_script_count = 0; |
| 367 | int extension_count = 0; |
| 368 | int theme_count = 0; |
| 369 | int external_count = 0; |
| 370 | int page_action_count = 0; |
| 371 | int browser_action_count = 0; |
| 372 | ExtensionList::iterator ex; |
| 373 | for (ex = extensions_.begin(); ex != extensions_.end(); ++ex) { |
| 374 | if ((*ex)->IsTheme()) { |
| 375 | theme_count++; |
| 376 | } else if ((*ex)->converted_from_user_script()) { |
| 377 | user_script_count++; |
| 378 | } else { |
| 379 | extension_count++; |
| 380 | } |
| 381 | if (Extension::IsExternalLocation((*ex)->location())) { |
| 382 | external_count++; |
| 383 | } |
| 384 | if ((*ex)->page_action() != NULL) { |
| 385 | page_action_count++; |
| 386 | } |
| 387 | if ((*ex)->browser_action() != NULL) { |
| 388 | browser_action_count++; |
| 389 | } |
| 390 | } |
| 391 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExtension", extension_count); |
| 392 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadUserScript", user_script_count); |
| 393 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadTheme", theme_count); |
| 394 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExternal", external_count); |
| 395 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadPageAction", page_action_count); |
| 396 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadBrowserAction", |
| 397 | browser_action_count); |
| 398 | } |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 399 | } |
| 400 | |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 401 | void ExtensionsService::LoadInstalledExtension(const ExtensionInfo& info, |
| 402 | bool write_to_prefs) { |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 403 | std::string error; |
| 404 | Extension* extension = NULL; |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 405 | if (info.extension_manifest.get()) { |
| 406 | scoped_ptr<Extension> tmp(new Extension(info.extension_path)); |
| 407 | if (tmp->InitFromValue(*info.extension_manifest, true, &error)) |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 408 | extension = tmp.release(); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 409 | } else { |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 410 | error = errors::kManifestUnreadable; |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | if (!extension) { |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 414 | ReportExtensionLoadError(info.extension_path, |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 415 | error, |
| 416 | NotificationType::EXTENSION_INSTALL_ERROR, |
| 417 | false); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 418 | return; |
| 419 | } |
| 420 | |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 421 | extension->set_location(info.extension_location); |
| 422 | |
| 423 | if (write_to_prefs) |
| 424 | extension_prefs_->UpdateManifest(extension); |
| 425 | |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 426 | OnExtensionLoaded(extension, true); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 427 | |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 428 | if (info.extension_location == Extension::EXTERNAL_PREF || |
| 429 | info.extension_location == Extension::EXTERNAL_REGISTRY) { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 430 | ChromeThread::PostTask( |
| 431 | ChromeThread::FILE, FROM_HERE, |
| 432 | NewRunnableMethod( |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 433 | backend_.get(), |
| 434 | &ExtensionsServiceBackend::CheckExternalUninstall, |
| 435 | scoped_refptr<ExtensionsService>(this), |
| 436 | info.extension_id, |
| 437 | info.extension_location)); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 438 | } |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 439 | } |
| 440 | |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 441 | void ExtensionsService::NotifyExtensionLoaded(Extension* extension) { |
| 442 | LOG(INFO) << "Sending EXTENSION_LOADED"; |
| 443 | |
| 444 | // The ChromeURLRequestContext needs to be first to know that the extension |
| 445 | // was loaded, otherwise a race can arise where a renderer that is created |
| 446 | // for the extension may try to load an extension URL with an extension id |
| 447 | // that the request context doesn't yet know about. |
| 448 | if (profile_ && !profile_->IsOffTheRecord()) { |
[email protected] | be180c80 | 2009-10-23 06:33:31 | [diff] [blame] | 449 | ChromeURLRequestContextGetter* context_getter = |
| 450 | static_cast<ChromeURLRequestContextGetter*>( |
| 451 | profile_->GetRequestContext()); |
| 452 | if (context_getter) { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 453 | ChromeThread::PostTask( |
| 454 | ChromeThread::IO, FROM_HERE, |
| 455 | NewRunnableMethod( |
| 456 | context_getter, |
| 457 | &ChromeURLRequestContextGetter::OnNewExtensions, |
| 458 | extension->id(), |
[email protected] | 0ce3f598 | 2010-01-28 23:04:27 | [diff] [blame] | 459 | new ChromeURLRequestContext::ExtensionInfo( |
| 460 | extension->path(), |
| 461 | extension->default_locale(), |
[email protected] | b30e0dd | 2010-01-29 23:33:21 | [diff] [blame] | 462 | extension->app_extent(), |
[email protected] | 0ce3f598 | 2010-01-28 23:04:27 | [diff] [blame] | 463 | extension->api_permissions()))); |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
| 467 | NotificationService::current()->Notify( |
| 468 | NotificationType::EXTENSION_LOADED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 469 | Source<Profile>(profile_), |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 470 | Details<Extension>(extension)); |
| 471 | } |
| 472 | |
| 473 | void ExtensionsService::NotifyExtensionUnloaded(Extension* extension) { |
| 474 | LOG(INFO) << "Sending EXTENSION_UNLOADED"; |
| 475 | |
| 476 | NotificationService::current()->Notify( |
| 477 | NotificationType::EXTENSION_UNLOADED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 478 | Source<Profile>(profile_), |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 479 | Details<Extension>(extension)); |
| 480 | |
| 481 | if (profile_ && !profile_->IsOffTheRecord()) { |
[email protected] | be180c80 | 2009-10-23 06:33:31 | [diff] [blame] | 482 | ChromeURLRequestContextGetter* context_getter = |
| 483 | static_cast<ChromeURLRequestContextGetter*>( |
| 484 | profile_->GetRequestContext()); |
| 485 | if (context_getter) { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 486 | ChromeThread::PostTask( |
| 487 | ChromeThread::IO, FROM_HERE, |
[email protected] | be180c80 | 2009-10-23 06:33:31 | [diff] [blame] | 488 | NewRunnableMethod( |
| 489 | context_getter, |
| 490 | &ChromeURLRequestContextGetter::OnUnloadedExtension, |
| 491 | extension->id())); |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
[email protected] | 6b75ec3 | 2009-08-14 06:37:18 | [diff] [blame] | 496 | void ExtensionsService::UpdateExtensionBlacklist( |
| 497 | const std::vector<std::string>& blacklist) { |
| 498 | // Use this set to indicate if an extension in the blacklist has been used. |
| 499 | std::set<std::string> blacklist_set; |
| 500 | for (unsigned int i = 0; i < blacklist.size(); ++i) { |
| 501 | if (Extension::IdIsValid(blacklist[i])) { |
| 502 | blacklist_set.insert(blacklist[i]); |
| 503 | } |
| 504 | } |
| 505 | extension_prefs_->UpdateBlacklist(blacklist_set); |
| 506 | std::vector<std::string> to_be_removed; |
| 507 | // Loop current extensions, unload installed extensions. |
| 508 | for (ExtensionList::const_iterator iter = extensions_.begin(); |
| 509 | iter != extensions_.end(); ++iter) { |
| 510 | Extension* extension = (*iter); |
| 511 | if (blacklist_set.find(extension->id()) != blacklist_set.end()) { |
| 512 | to_be_removed.push_back(extension->id()); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | // UnloadExtension will change the extensions_ list. So, we should |
| 517 | // call it outside the iterator loop. |
| 518 | for (unsigned int i = 0; i < to_be_removed.size(); ++i) { |
| 519 | UnloadExtension(to_be_removed[i]); |
| 520 | } |
| 521 | } |
| 522 | |
[email protected] | 5ef47ec | 2010-01-28 05:58:05 | [diff] [blame] | 523 | void ExtensionsService::SetLastPingDay(const std::string& extension_id, |
| 524 | const base::Time& time) { |
| 525 | extension_prefs_->SetLastPingDay(extension_id, time); |
| 526 | } |
| 527 | |
| 528 | base::Time ExtensionsService::LastPingDay(const std::string& extension_id) { |
| 529 | return extension_prefs_->LastPingDay(extension_id); |
| 530 | } |
| 531 | |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 532 | void ExtensionsService::CheckForExternalUpdates() { |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 533 | // This installs or updates externally provided extensions. |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 534 | // TODO(aa): Why pass this list into the provider, why not just filter it |
| 535 | // later? |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 536 | std::set<std::string> killed_extensions; |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 537 | extension_prefs_->GetKilledExtensionIds(&killed_extensions); |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 538 | ChromeThread::PostTask( |
| 539 | ChromeThread::FILE, FROM_HERE, |
| 540 | NewRunnableMethod( |
| 541 | backend_.get(), &ExtensionsServiceBackend::CheckForExternalUpdates, |
| 542 | killed_extensions, scoped_refptr<ExtensionsService>(this))); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | void ExtensionsService::UnloadExtension(const std::string& extension_id) { |
[email protected] | 27e469a | 2010-01-11 20:35:09 | [diff] [blame] | 546 | // Make sure the extension gets deleted after we return from this function. |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 547 | scoped_ptr<Extension> extension( |
| 548 | GetExtensionByIdInternal(extension_id, true, true)); |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 549 | |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 550 | // Callers should not send us nonexistant extensions. |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 551 | CHECK(extension.get()); |
| 552 | |
[email protected] | 1eb17508 | 2010-02-10 09:26:16 | [diff] [blame^] | 553 | // Keep information about the extension so that we can reload it later |
| 554 | // even if it's not permanently installed. |
| 555 | unloaded_extension_paths_[extension->id()] = extension->path(); |
| 556 | |
[email protected] | 86c008e8 | 2009-08-28 20:26:05 | [diff] [blame] | 557 | ExtensionDOMUI::UnregisterChromeURLOverrides(profile_, |
| 558 | extension->GetChromeURLOverrides()); |
| 559 | |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 560 | ExtensionList::iterator iter = std::find(disabled_extensions_.begin(), |
| 561 | disabled_extensions_.end(), |
| 562 | extension.get()); |
| 563 | if (iter != disabled_extensions_.end()) { |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 564 | disabled_extensions_.erase(iter); |
[email protected] | 86693068 | 2009-08-18 22:53:47 | [diff] [blame] | 565 | NotificationService::current()->Notify( |
| 566 | NotificationType::EXTENSION_UNLOADED_DISABLED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 567 | Source<Profile>(profile_), |
[email protected] | 86693068 | 2009-08-18 22:53:47 | [diff] [blame] | 568 | Details<Extension>(extension.get())); |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 569 | return; |
| 570 | } |
| 571 | |
| 572 | iter = std::find(extensions_.begin(), extensions_.end(), extension.get()); |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 573 | |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 574 | // Remove the extension from our list. |
| 575 | extensions_.erase(iter); |
| 576 | |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 577 | NotifyExtensionUnloaded(extension.get()); |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 578 | UpdateActiveExtensionsInCrashReporter(); |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 579 | } |
| 580 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 581 | void ExtensionsService::UnloadAllExtensions() { |
| 582 | ExtensionList::iterator iter; |
[email protected] | c6e4a341 | 2009-06-24 15:45:29 | [diff] [blame] | 583 | for (iter = extensions_.begin(); iter != extensions_.end(); ++iter) |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 584 | delete *iter; |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 585 | extensions_.clear(); |
[email protected] | c6e4a341 | 2009-06-24 15:45:29 | [diff] [blame] | 586 | |
| 587 | // TODO(erikkay) should there be a notification for this? We can't use |
| 588 | // EXTENSION_UNLOADED since that implies that the extension has been disabled |
| 589 | // or uninstalled, and UnloadAll is just part of shutdown. |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | void ExtensionsService::ReloadExtensions() { |
| 593 | UnloadAllExtensions(); |
| 594 | LoadAllExtensions(); |
| 595 | } |
| 596 | |
| 597 | void ExtensionsService::GarbageCollectExtensions() { |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 598 | InstalledExtensionSet installed(extension_prefs_.get()); |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 599 | ChromeThread::PostTask( |
| 600 | ChromeThread::FILE, FROM_HERE, |
| 601 | NewRunnableFunction( |
| 602 | &extension_file_util::GarbageCollectExtensions, install_directory_, |
[email protected] | 4559a7d | 2009-12-02 01:42:41 | [diff] [blame] | 603 | installed.extensions(), installed.versions())); |
[email protected] | 3cf4f099 | 2009-02-03 23:00:30 | [diff] [blame] | 604 | } |
| 605 | |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 606 | void ExtensionsService::OnLoadedInstalledExtensions() { |
[email protected] | e81dba3 | 2009-06-19 20:19:13 | [diff] [blame] | 607 | ready_ = true; |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 608 | if (updater_.get()) { |
| 609 | updater_->Start(); |
| 610 | } |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 611 | NotificationService::current()->Notify( |
| 612 | NotificationType::EXTENSIONS_READY, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 613 | Source<Profile>(profile_), |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 614 | NotificationService::NoDetails()); |
| 615 | } |
| 616 | |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 617 | void ExtensionsService::OnExtensionLoaded(Extension* extension, |
| 618 | bool allow_privilege_increase) { |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 619 | // Ensure extension is deleted unless we transfer ownership. |
| 620 | scoped_ptr<Extension> scoped_extension(extension); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 621 | |
[email protected] | 1eb17508 | 2010-02-10 09:26:16 | [diff] [blame^] | 622 | // The extension is now loaded, remove its data from unloaded extension map. |
| 623 | unloaded_extension_paths_.erase(extension->id()); |
| 624 | |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 625 | if (extensions_enabled() || |
| 626 | extension->IsTheme() || |
| 627 | extension->location() == Extension::LOAD || |
| 628 | Extension::IsExternalLocation(extension->location())) { |
| 629 | Extension* old = GetExtensionByIdInternal(extension->id(), true, true); |
| 630 | if (old) { |
| 631 | if (extension->version()->CompareTo(*(old->version())) > 0) { |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 632 | bool allow_silent_upgrade = |
| 633 | allow_privilege_increase || !Extension::IsPrivilegeIncrease( |
| 634 | old, extension); |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 635 | |
[email protected] | 1e8c93f | 2010-02-08 22:58:31 | [diff] [blame] | 636 | // Extensions get upgraded if silent upgrades are allowed, otherwise |
| 637 | // they get disabled. |
| 638 | if (allow_silent_upgrade) { |
| 639 | old->set_being_upgraded(true); |
| 640 | extension->set_being_upgraded(true); |
| 641 | } |
| 642 | |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 643 | // To upgrade an extension in place, unload the old one and |
| 644 | // then load the new one. |
| 645 | UnloadExtension(old->id()); |
| 646 | old = NULL; |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 647 | |
[email protected] | b24d831 | 2009-08-27 06:47:46 | [diff] [blame] | 648 | if (!allow_silent_upgrade) { |
[email protected] | 6d27a7b | 2009-12-18 23:25:45 | [diff] [blame] | 649 | // Extension has changed permissions significantly. Disable it. We |
| 650 | // send a notification below. |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 651 | extension_prefs_->SetExtensionState(extension, Extension::DISABLED); |
[email protected] | 7d84586 | 2010-01-04 21:28:57 | [diff] [blame] | 652 | extension_prefs_->SetShowInstallWarningOnEnable(extension, true); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 653 | } |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 654 | } else { |
| 655 | // We already have the extension of the same or older version. |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 656 | std::string error_message("Duplicate extension load attempt: "); |
| 657 | error_message += extension->id(); |
| 658 | LOG(WARNING) << error_message; |
| 659 | ReportExtensionLoadError(extension->path(), |
| 660 | error_message, |
| 661 | NotificationType::EXTENSION_OVERINSTALL_ERROR, |
| 662 | false); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 663 | return; |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 664 | } |
[email protected] | ba74f35 | 2009-06-11 18:54:45 | [diff] [blame] | 665 | } |
[email protected] | 86a27407 | 2009-06-11 02:06:45 | [diff] [blame] | 666 | |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 667 | switch (extension_prefs_->GetExtensionState(extension->id())) { |
| 668 | case Extension::ENABLED: |
| 669 | extensions_.push_back(scoped_extension.release()); |
| 670 | |
[email protected] | aeb53b3 | 2009-10-29 07:34:45 | [diff] [blame] | 671 | // We delay starting up the browser event router until at least one |
| 672 | // extension that needs it is loaded. |
| 673 | if (extension->HasApiPermission(Extension::kTabPermission)) { |
| 674 | ExtensionBrowserEventRouter::GetInstance()->Init(); |
| 675 | } |
[email protected] | 840b0db | 2009-11-20 03:00:38 | [diff] [blame] | 676 | if (extension->HasApiPermission(Extension::kBookmarkPermission)) { |
| 677 | ExtensionBookmarkEventRouter::GetSingleton()->Observe( |
| 678 | profile_->GetBookmarkModel()); |
| 679 | } |
[email protected] | aeb53b3 | 2009-10-29 07:34:45 | [diff] [blame] | 680 | |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 681 | NotifyExtensionLoaded(extension); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 682 | |
| 683 | if (extension->IsTheme() && extension->location() == Extension::LOAD) { |
| 684 | NotificationService::current()->Notify( |
| 685 | NotificationType::THEME_INSTALLED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 686 | Source<Profile>(profile_), |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 687 | Details<Extension>(extension)); |
[email protected] | 86c008e8 | 2009-08-28 20:26:05 | [diff] [blame] | 688 | } else { |
| 689 | ExtensionDOMUI::RegisterChromeURLOverrides(profile_, |
| 690 | extension->GetChromeURLOverrides()); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 691 | } |
| 692 | break; |
| 693 | case Extension::DISABLED: |
[email protected] | 6d27a7b | 2009-12-18 23:25:45 | [diff] [blame] | 694 | disabled_extensions_.push_back(scoped_extension.release()); |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 695 | NotificationService::current()->Notify( |
| 696 | NotificationType::EXTENSION_UPDATE_DISABLED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 697 | Source<Profile>(profile_), |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 698 | Details<Extension>(extension)); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 699 | break; |
| 700 | default: |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 701 | NOTREACHED(); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 702 | break; |
[email protected] | 811f343 | 2009-07-25 19:38:21 | [diff] [blame] | 703 | } |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 704 | } |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 705 | |
[email protected] | 1e8c93f | 2010-02-08 22:58:31 | [diff] [blame] | 706 | extension->set_being_upgraded(false); |
| 707 | |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 708 | UpdateActiveExtensionsInCrashReporter(); |
| 709 | } |
| 710 | |
| 711 | void ExtensionsService::UpdateActiveExtensionsInCrashReporter() { |
[email protected] | c886596 | 2009-12-16 07:47:39 | [diff] [blame] | 712 | std::set<std::string> extension_ids; |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 713 | for (size_t i = 0; i < extensions_.size(); ++i) { |
| 714 | if (!extensions_[i]->IsTheme()) |
[email protected] | c886596 | 2009-12-16 07:47:39 | [diff] [blame] | 715 | extension_ids.insert(extensions_[i]->id()); |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | child_process_logging::SetActiveExtensions(extension_ids); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 719 | } |
| 720 | |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 721 | void ExtensionsService::OnExtensionInstalled(Extension* extension, |
| 722 | bool allow_privilege_increase) { |
[email protected] | b6ab96d | 2009-08-20 18:58:19 | [diff] [blame] | 723 | extension_prefs_->OnExtensionInstalled(extension); |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 724 | |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 725 | // If the extension is a theme, tell the profile (and therefore ThemeProvider) |
| 726 | // to apply it. |
| 727 | if (extension->IsTheme()) { |
[email protected] | 9ceb0734 | 2009-07-26 04:09:23 | [diff] [blame] | 728 | NotificationService::current()->Notify( |
| 729 | NotificationType::THEME_INSTALLED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 730 | Source<Profile>(profile_), |
[email protected] | 9ceb0734 | 2009-07-26 04:09:23 | [diff] [blame] | 731 | Details<Extension>(extension)); |
[email protected] | 9197f3b | 2009-06-02 00:49:27 | [diff] [blame] | 732 | } else { |
| 733 | NotificationService::current()->Notify( |
| 734 | NotificationType::EXTENSION_INSTALLED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 735 | Source<Profile>(profile_), |
[email protected] | 9197f3b | 2009-06-02 00:49:27 | [diff] [blame] | 736 | Details<Extension>(extension)); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 737 | } |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 738 | |
| 739 | // Also load the extension. |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 740 | OnExtensionLoaded(extension, allow_privilege_increase); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 741 | } |
| 742 | |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 743 | void ExtensionsService::OnExtensionOverinstallAttempted(const std::string& id) { |
[email protected] | 61b41161 | 2009-11-10 23:17:41 | [diff] [blame] | 744 | Extension* extension = GetExtensionById(id, false); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 745 | if (extension && extension->IsTheme()) { |
[email protected] | 9ceb0734 | 2009-07-26 04:09:23 | [diff] [blame] | 746 | NotificationService::current()->Notify( |
| 747 | NotificationType::THEME_INSTALLED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 748 | Source<Profile>(profile_), |
[email protected] | 9ceb0734 | 2009-07-26 04:09:23 | [diff] [blame] | 749 | Details<Extension>(extension)); |
[email protected] | 91e1bd8 | 2009-09-03 22:04:40 | [diff] [blame] | 750 | } else { |
| 751 | NotificationService::current()->Notify( |
| 752 | NotificationType::NO_THEME_DETECTED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 753 | Source<Profile>(profile_), |
[email protected] | 91e1bd8 | 2009-09-03 22:04:40 | [diff] [blame] | 754 | NotificationService::NoDetails()); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 755 | } |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 756 | } |
| 757 | |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 758 | Extension* ExtensionsService::GetExtensionByIdInternal(const std::string& id, |
| 759 | bool include_enabled, |
| 760 | bool include_disabled) { |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 761 | std::string lowercase_id = StringToLowerASCII(id); |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 762 | if (include_enabled) { |
| 763 | for (ExtensionList::const_iterator iter = extensions_.begin(); |
| 764 | iter != extensions_.end(); ++iter) { |
| 765 | if ((*iter)->id() == lowercase_id) |
| 766 | return *iter; |
| 767 | } |
| 768 | } |
| 769 | if (include_disabled) { |
| 770 | for (ExtensionList::const_iterator iter = disabled_extensions_.begin(); |
| 771 | iter != disabled_extensions_.end(); ++iter) { |
| 772 | if ((*iter)->id() == lowercase_id) |
| 773 | return *iter; |
| 774 | } |
[email protected] | ce5c450 | 2009-05-06 16:46:11 | [diff] [blame] | 775 | } |
| 776 | return NULL; |
| 777 | } |
| 778 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 779 | Extension* ExtensionsService::GetExtensionByURL(const GURL& url) { |
| 780 | std::string host = url.host(); |
[email protected] | 61b41161 | 2009-11-10 23:17:41 | [diff] [blame] | 781 | return GetExtensionById(host, false); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 782 | } |
| 783 | |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 784 | void ExtensionsService::ClearProvidersForTesting() { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 785 | ChromeThread::PostTask( |
| 786 | ChromeThread::FILE, FROM_HERE, |
| 787 | NewRunnableMethod( |
| 788 | backend_.get(), &ExtensionsServiceBackend::ClearProvidersForTesting)); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | void ExtensionsService::SetProviderForTesting( |
| 792 | Extension::Location location, ExternalExtensionProvider* test_provider) { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 793 | ChromeThread::PostTask( |
| 794 | ChromeThread::FILE, FROM_HERE, |
| 795 | NewRunnableMethod( |
| 796 | backend_.get(), &ExtensionsServiceBackend::SetProviderForTesting, |
| 797 | location, test_provider)); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 798 | } |
| 799 | |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 800 | void ExtensionsService::OnExternalExtensionFound(const std::string& id, |
| 801 | const std::string& version, |
| 802 | const FilePath& path, |
| 803 | Extension::Location location) { |
| 804 | // Before even bothering to unpack, check and see if we already have this |
[email protected] | 4c96793 | 2009-07-31 01:15:49 | [diff] [blame] | 805 | // version. This is important because these extensions are going to get |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 806 | // installed on every startup. |
[email protected] | 61b41161 | 2009-11-10 23:17:41 | [diff] [blame] | 807 | Extension* existing = GetExtensionById(id, true); |
[email protected] | a3a63ff8 | 2009-08-04 06:44:11 | [diff] [blame] | 808 | scoped_ptr<Version> other(Version::GetVersionFromString(version)); |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 809 | if (existing) { |
[email protected] | a3a63ff8 | 2009-08-04 06:44:11 | [diff] [blame] | 810 | switch (existing->version()->CompareTo(*other)) { |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 811 | case -1: // existing version is older, we should upgrade |
| 812 | break; |
| 813 | case 0: // existing version is same, do nothing |
| 814 | return; |
| 815 | case 1: // existing version is newer, uh-oh |
| 816 | LOG(WARNING) << "Found external version of extension " << id |
| 817 | << "that is older than current version. Current version " |
| 818 | << "is: " << existing->VersionString() << ". New version " |
| 819 | << "is: " << version << ". Keeping current version."; |
| 820 | return; |
| 821 | } |
| 822 | } |
| 823 | |
[email protected] | 2a464a9 | 2009-08-01 17:58:35 | [diff] [blame] | 824 | CrxInstaller::Start(path, install_directory_, location, id, |
| 825 | false, // don't delete crx when complete |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 826 | true, // allow privilege increase |
[email protected] | 2a464a9 | 2009-08-01 17:58:35 | [diff] [blame] | 827 | this, |
| 828 | NULL); // no client (silent install) |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 829 | } |
| 830 | |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 831 | void ExtensionsService::ReportExtensionLoadError( |
| 832 | const FilePath& extension_path, |
| 833 | const std::string &error, |
| 834 | NotificationType type, |
| 835 | bool be_noisy) { |
| 836 | NotificationService* service = NotificationService::current(); |
| 837 | service->Notify(type, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 838 | Source<Profile>(profile_), |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 839 | Details<const std::string>(&error)); |
| 840 | |
| 841 | // TODO(port): note that this isn't guaranteed to work properly on Linux. |
[email protected] | 99efb7b1 | 2009-12-18 02:39:16 | [diff] [blame] | 842 | std::string path_str = WideToUTF8(extension_path.ToWStringHack()); |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 843 | std::string message = StringPrintf("Could not load extension from '%s'. %s", |
| 844 | path_str.c_str(), error.c_str()); |
| 845 | ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy); |
| 846 | } |
| 847 | |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 848 | void ExtensionsService::Observe(NotificationType type, |
| 849 | const NotificationSource& source, |
| 850 | const NotificationDetails& details) { |
| 851 | switch (type.value) { |
| 852 | case NotificationType::EXTENSION_HOST_DID_STOP_LOADING: { |
| 853 | ExtensionHost* host = Details<ExtensionHost>(details).ptr(); |
| 854 | OrphanedDevTools::iterator iter = |
| 855 | orphaned_dev_tools_.find(host->extension()->id()); |
| 856 | if (iter == orphaned_dev_tools_.end()) |
| 857 | return; |
| 858 | |
| 859 | DevToolsManager::GetInstance()->AttachClientHost( |
| 860 | iter->second, host->render_view_host()); |
| 861 | orphaned_dev_tools_.erase(iter); |
| 862 | break; |
| 863 | } |
| 864 | |
[email protected] | a4ed628 | 2009-12-14 20:51:16 | [diff] [blame] | 865 | case NotificationType::EXTENSION_PROCESS_TERMINATED: { |
[email protected] | 31f7726 | 2009-12-02 20:48:53 | [diff] [blame] | 866 | DCHECK_EQ(profile_, Source<Profile>(source).ptr()); |
[email protected] | a4ed628 | 2009-12-14 20:51:16 | [diff] [blame] | 867 | |
[email protected] | 31f7726 | 2009-12-02 20:48:53 | [diff] [blame] | 868 | ExtensionHost* host = Details<ExtensionHost>(details).ptr(); |
[email protected] | 27e469a | 2010-01-11 20:35:09 | [diff] [blame] | 869 | // TODO(phajdan.jr): Change to DCHECK after fixing http://crbug.com/30405. |
| 870 | CHECK(profile_->GetExtensionProcessManager()->HasExtensionHost(host)); |
| 871 | |
| 872 | // If we hit one of these assertions it means that the host's |
[email protected] | b0b567bf | 2010-01-22 19:22:53 | [diff] [blame] | 873 | // Extension pointer became invalid. http://crbug.com/30405 |
[email protected] | 27e469a | 2010-01-11 20:35:09 | [diff] [blame] | 874 | // TODO(phajdan.jr): Remove excessive debugging after fixing bug 30405. |
| 875 | std::string extension_id(host->extension()->id()); |
| 876 | CHECK(extension_id.length() == 32U); |
| 877 | Extension* extension = GetExtensionById(extension_id, true); |
[email protected] | b0b567bf | 2010-01-22 19:22:53 | [diff] [blame] | 878 | CHECK(extension == host->extension()); |
[email protected] | 6f39aee9 | 2010-01-26 22:33:58 | [diff] [blame] | 879 | if (!extension) { |
| 880 | NOTREACHED(); |
| 881 | return; |
| 882 | } |
[email protected] | 31f7726 | 2009-12-02 20:48:53 | [diff] [blame] | 883 | |
| 884 | // Unload the entire extension. We want it to be in a consistent state: |
| 885 | // either fully working or not loaded at all, but never half-crashed. |
[email protected] | 27e469a | 2010-01-11 20:35:09 | [diff] [blame] | 886 | UnloadExtension(extension_id); |
[email protected] | 31f7726 | 2009-12-02 20:48:53 | [diff] [blame] | 887 | break; |
| 888 | } |
| 889 | |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 890 | default: |
| 891 | NOTREACHED() << "Unexpected notification type."; |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 896 | // ExtensionsServicesBackend |
| 897 | |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 898 | ExtensionsServiceBackend::ExtensionsServiceBackend( |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 899 | const FilePath& install_directory) |
[email protected] | 0c7bc4b | 2009-05-30 01:47:08 | [diff] [blame] | 900 | : frontend_(NULL), |
| 901 | install_directory_(install_directory), |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 902 | alert_on_error_(false) { |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 903 | // TODO(aa): This ends up doing blocking IO on the UI thread because it reads |
| 904 | // pref data in the ctor and that is called on the UI thread. Would be better |
| 905 | // to re-read data each time we list external extensions, anyway. |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 906 | external_extension_providers_[Extension::EXTERNAL_PREF] = |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 907 | linked_ptr<ExternalExtensionProvider>( |
[email protected] | 27b985d | 2009-06-25 17:53:15 | [diff] [blame] | 908 | new ExternalPrefExtensionProvider()); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 909 | #if defined(OS_WIN) |
| 910 | external_extension_providers_[Extension::EXTERNAL_REGISTRY] = |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 911 | linked_ptr<ExternalExtensionProvider>( |
| 912 | new ExternalRegistryExtensionProvider()); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 913 | #endif |
| 914 | } |
| 915 | |
| 916 | ExtensionsServiceBackend::~ExtensionsServiceBackend() { |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 917 | } |
| 918 | |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 919 | void ExtensionsServiceBackend::LoadSingleExtension( |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 920 | const FilePath& path_in, scoped_refptr<ExtensionsService> frontend) { |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 921 | frontend_ = frontend; |
| 922 | |
| 923 | // Explicit UI loads are always noisy. |
| 924 | alert_on_error_ = true; |
| 925 | |
[email protected] | cc5da33 | 2009-03-04 08:02:51 | [diff] [blame] | 926 | FilePath extension_path = path_in; |
[email protected] | f36fa4fb | 2009-06-19 18:23:50 | [diff] [blame] | 927 | file_util::AbsolutePath(&extension_path); |
[email protected] | bf24d2c | 2009-02-24 23:07:45 | [diff] [blame] | 928 | |
| 929 | LOG(INFO) << "Loading single extension from " << |
[email protected] | 99efb7b1 | 2009-12-18 02:39:16 | [diff] [blame] | 930 | extension_path.BaseName().value(); |
[email protected] | bf24d2c | 2009-02-24 23:07:45 | [diff] [blame] | 931 | |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame] | 932 | std::string error; |
| 933 | Extension* extension = extension_file_util::LoadExtension( |
| 934 | extension_path, |
| 935 | false, // Don't require id |
| 936 | &error); |
| 937 | |
| 938 | if (!extension) { |
| 939 | ReportExtensionLoadError(extension_path, error); |
| 940 | return; |
[email protected] | 0877fd9 | 2009-02-03 16:34:06 | [diff] [blame] | 941 | } |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame] | 942 | |
| 943 | extension->set_location(Extension::LOAD); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 944 | ReportExtensionLoaded(extension); |
[email protected] | 0877fd9 | 2009-02-03 16:34:06 | [diff] [blame] | 945 | } |
| 946 | |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 947 | void ExtensionsServiceBackend::ReportExtensionLoadError( |
[email protected] | cc5da33 | 2009-03-04 08:02:51 | [diff] [blame] | 948 | const FilePath& extension_path, const std::string &error) { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 949 | ChromeThread::PostTask( |
| 950 | ChromeThread::UI, FROM_HERE, |
| 951 | NewRunnableMethod( |
| 952 | frontend_, |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 953 | &ExtensionsService::ReportExtensionLoadError, extension_path, |
| 954 | error, NotificationType::EXTENSION_INSTALL_ERROR, alert_on_error_)); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 955 | } |
| 956 | |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 957 | void ExtensionsServiceBackend::ReportExtensionLoaded(Extension* extension) { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 958 | ChromeThread::PostTask( |
| 959 | ChromeThread::UI, FROM_HERE, |
| 960 | NewRunnableMethod( |
| 961 | frontend_, &ExtensionsService::OnExtensionLoaded, extension, true)); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 962 | } |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 963 | |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 964 | bool ExtensionsServiceBackend::LookupExternalExtension( |
| 965 | const std::string& id, Version** version, Extension::Location* location) { |
| 966 | scoped_ptr<Version> extension_version; |
| 967 | for (ProviderMap::const_iterator i = external_extension_providers_.begin(); |
| 968 | i != external_extension_providers_.end(); ++i) { |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 969 | const ExternalExtensionProvider* provider = i->second.get(); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 970 | extension_version.reset(provider->RegisteredVersion(id, location)); |
| 971 | if (extension_version.get()) { |
| 972 | if (version) |
| 973 | *version = extension_version.release(); |
| 974 | return true; |
| 975 | } |
| 976 | } |
| 977 | return false; |
| 978 | } |
| 979 | |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 980 | // Some extensions will autoupdate themselves externally from Chrome. These |
| 981 | // are typically part of some larger client application package. To support |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 982 | // these, the extension will register its location in the the preferences file |
| 983 | // (and also, on Windows, in the registry) and this code will periodically |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 984 | // check that location for a .crx file, which it will then install locally if |
| 985 | // a new version is available. |
| 986 | void ExtensionsServiceBackend::CheckForExternalUpdates( |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 987 | std::set<std::string> ids_to_ignore, |
| 988 | scoped_refptr<ExtensionsService> frontend) { |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 989 | // Note that this installation is intentionally silent (since it didn't |
| 990 | // go through the front-end). Extensions that are registered in this |
| 991 | // way are effectively considered 'pre-bundled', and so implicitly |
| 992 | // trusted. In general, if something has HKLM or filesystem access, |
| 993 | // they could install an extension manually themselves anyway. |
| 994 | alert_on_error_ = false; |
| 995 | frontend_ = frontend; |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 996 | |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 997 | // Ask each external extension provider to give us a call back for each |
| 998 | // extension they know about. See OnExternalExtensionFound. |
| 999 | for (ProviderMap::const_iterator i = external_extension_providers_.begin(); |
| 1000 | i != external_extension_providers_.end(); ++i) { |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 1001 | ExternalExtensionProvider* provider = i->second.get(); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1002 | provider->VisitRegisteredExtension(this, ids_to_ignore); |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 1003 | } |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 1004 | } |
| 1005 | |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 1006 | void ExtensionsServiceBackend::CheckExternalUninstall( |
| 1007 | scoped_refptr<ExtensionsService> frontend, const std::string& id, |
| 1008 | Extension::Location location) { |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1009 | // Check if the providers know about this extension. |
| 1010 | ProviderMap::const_iterator i = external_extension_providers_.find(location); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 1011 | if (i == external_extension_providers_.end()) { |
| 1012 | NOTREACHED() << "CheckExternalUninstall called for non-external extension " |
| 1013 | << location; |
| 1014 | return; |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 1015 | } |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 1016 | |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 1017 | scoped_ptr<Version> version; |
| 1018 | version.reset(i->second->RegisteredVersion(id, NULL)); |
| 1019 | if (version.get()) |
| 1020 | return; // Yup, known extension, don't uninstall. |
| 1021 | |
| 1022 | // This is an external extension that we don't have registered. Uninstall. |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 1023 | ChromeThread::PostTask( |
| 1024 | ChromeThread::UI, FROM_HERE, |
| 1025 | NewRunnableMethod( |
| 1026 | frontend.get(), &ExtensionsService::UninstallExtension, id, true)); |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 1027 | } |
| 1028 | |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1029 | void ExtensionsServiceBackend::ClearProvidersForTesting() { |
| 1030 | external_extension_providers_.clear(); |
| 1031 | } |
| 1032 | |
| 1033 | void ExtensionsServiceBackend::SetProviderForTesting( |
| 1034 | Extension::Location location, |
| 1035 | ExternalExtensionProvider* test_provider) { |
| 1036 | DCHECK(test_provider); |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 1037 | external_extension_providers_[location] = |
| 1038 | linked_ptr<ExternalExtensionProvider>(test_provider); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | void ExtensionsServiceBackend::OnExternalExtensionFound( |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 1042 | const std::string& id, const Version* version, const FilePath& path, |
| 1043 | Extension::Location location) { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 1044 | ChromeThread::PostTask( |
| 1045 | ChromeThread::UI, FROM_HERE, |
| 1046 | NewRunnableMethod( |
| 1047 | frontend_, &ExtensionsService::OnExternalExtensionFound, id, |
| 1048 | version->GetString(), path, location)); |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 1049 | } |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 1050 | |
| 1051 | void ExtensionsServiceBackend::ReloadExtensionManifestsForLocaleChanged( |
| 1052 | ExtensionPrefs::ExtensionsInfo* extensions_to_reload, |
| 1053 | base::TimeTicks start_time, |
| 1054 | scoped_refptr<ExtensionsService> frontend) { |
| 1055 | frontend_ = frontend; |
| 1056 | |
| 1057 | for (size_t i = 0; i < extensions_to_reload->size(); ++i) { |
| 1058 | ExtensionInfo* info = extensions_to_reload->at(i).get(); |
| 1059 | if (!info->extension_manifest.get()) |
| 1060 | continue; |
| 1061 | |
| 1062 | if (!extension_l10n_util::ShouldRelocalizeManifest(*info)) |
| 1063 | continue; |
| 1064 | |
| 1065 | // We need to reload original manifest in order to localize properly. |
| 1066 | std::string error; |
| 1067 | scoped_ptr<Extension> extension(extension_file_util::LoadExtension( |
| 1068 | info->extension_path, false, &error)); |
| 1069 | |
| 1070 | if (extension.get()) |
| 1071 | extensions_to_reload->at(i)->extension_manifest.reset( |
| 1072 | static_cast<DictionaryValue*>( |
| 1073 | extension->manifest_value()->DeepCopy())); |
| 1074 | } |
| 1075 | |
| 1076 | // Finish installing on UI thread. |
| 1077 | ChromeThread::PostTask( |
| 1078 | ChromeThread::UI, FROM_HERE, |
| 1079 | NewRunnableMethod( |
| 1080 | frontend_, |
| 1081 | &ExtensionsService::ContinueLoadAllExtensions, |
| 1082 | extensions_to_reload, |
| 1083 | start_time, |
| 1084 | true)); |
| 1085 | } |