[email protected] | ce5c450 | 2009-05-06 16:46:11 | [diff] [blame] | 1 | // Copyright (c) 2009 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] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 9 | #include "base/string_util.h" |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 10 | #include "base/values.h" |
[email protected] | 15730c4 | 2009-09-03 00:03:20 | [diff] [blame] | 11 | #include "chrome/browser/browser_process.h" |
[email protected] | dbb92e0d | 2009-08-20 16:18:21 | [diff] [blame] | 12 | #include "chrome/browser/chrome_thread.h" |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 13 | #include "chrome/browser/extensions/crx_installer.h" |
[email protected] | b68d5ed | 2009-04-16 02:41:28 | [diff] [blame] | 14 | #include "chrome/browser/extensions/extension_browser_event_router.h" |
[email protected] | 86c008e8 | 2009-08-28 20:26:05 | [diff] [blame] | 15 | #include "chrome/browser/extensions/extension_dom_ui.h" |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame] | 16 | #include "chrome/browser/extensions/extension_file_util.h" |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 17 | #include "chrome/browser/extensions/extension_updater.h" |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 18 | #include "chrome/browser/extensions/external_extension_provider.h" |
| 19 | #include "chrome/browser/extensions/external_pref_extension_provider.h" |
[email protected] | 81e6378 | 2009-02-27 19:35:09 | [diff] [blame] | 20 | #include "chrome/browser/profile.h" |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 21 | #include "chrome/browser/net/chrome_url_request_context.h" |
[email protected] | e2eb4311 | 2009-05-29 21:19:54 | [diff] [blame] | 22 | #include "chrome/common/chrome_switches.h" |
[email protected] | 5b1a0e2 | 2009-05-26 19:00:58 | [diff] [blame] | 23 | #include "chrome/common/extensions/extension.h" |
| 24 | #include "chrome/common/extensions/extension_error_reporter.h" |
[email protected] | 8289126 | 2008-12-24 00:21:26 | [diff] [blame] | 25 | #include "chrome/common/notification_service.h" |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 26 | #include "chrome/common/pref_names.h" |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 27 | #include "chrome/common/pref_service.h" |
[email protected] | a5720987 | 2009-05-04 22:53:14 | [diff] [blame] | 28 | #include "chrome/common/url_constants.h" |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 29 | |
[email protected] | 79db623 | 2009-02-13 20:51:20 | [diff] [blame] | 30 | #if defined(OS_WIN) |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 31 | #include "chrome/browser/extensions/external_registry_extension_provider_win.h" |
[email protected] | 79db623 | 2009-02-13 20:51:20 | [diff] [blame] | 32 | #endif |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 33 | |
[email protected] | b6ab96d | 2009-08-20 18:58:19 | [diff] [blame] | 34 | namespace { |
| 35 | |
| 36 | // Helper class to collect the IDs of every extension listed in the prefs. |
| 37 | class InstalledExtensionSet { |
| 38 | public: |
[email protected] | 038d52e1 | 2009-10-14 16:53:41 | [diff] [blame] | 39 | explicit InstalledExtensionSet(InstalledExtensions* installed) { |
[email protected] | b6ab96d | 2009-08-20 18:58:19 | [diff] [blame] | 40 | scoped_ptr<InstalledExtensions> cleanup(installed); |
| 41 | installed->VisitInstalledExtensions( |
| 42 | NewCallback(this, &InstalledExtensionSet::ExtensionVisited)); |
| 43 | } |
| 44 | |
| 45 | const std::set<std::string>& extensions() { return extensions_; } |
| 46 | |
| 47 | private: |
| 48 | void ExtensionVisited( |
| 49 | DictionaryValue* manifest, const std::string& id, |
| 50 | const FilePath& path, Extension::Location location) { |
| 51 | extensions_.insert(id); |
| 52 | } |
| 53 | |
| 54 | std::set<std::string> extensions_; |
| 55 | }; |
| 56 | |
| 57 | } // namespace |
| 58 | |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 59 | // ExtensionsService. |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 60 | |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 61 | const char* ExtensionsService::kInstallDirectoryName = "Extensions"; |
| 62 | const char* ExtensionsService::kCurrentVersionFileName = "Current Version"; |
[email protected] | 494c06e | 2009-07-25 01:06:42 | [diff] [blame] | 63 | |
| 64 | const char* ExtensionsService::kGalleryDownloadURLPrefix = |
| 65 | "https://dl-ssl.google.com/chrome/"; |
[email protected] | 75a2567 | 2009-07-24 17:41:39 | [diff] [blame] | 66 | const char* ExtensionsService::kGalleryURLPrefix = |
| 67 | "https://tools.google.com/chrome/"; |
| 68 | |
[email protected] | 4289d9b | 2009-07-25 21:17:34 | [diff] [blame] | 69 | // static |
| 70 | bool ExtensionsService::IsDownloadFromGallery(const GURL& download_url, |
| 71 | const GURL& referrer_url) { |
| 72 | if (StartsWithASCII(download_url.spec(), kGalleryDownloadURLPrefix, false) && |
| 73 | StartsWithASCII(referrer_url.spec(), kGalleryURLPrefix, false)) { |
| 74 | return true; |
| 75 | } else { |
| 76 | return false; |
| 77 | } |
| 78 | } |
| 79 | |
[email protected] | 81e6378 | 2009-02-27 19:35:09 | [diff] [blame] | 80 | ExtensionsService::ExtensionsService(Profile* profile, |
[email protected] | 36a784c | 2009-06-23 06:21:08 | [diff] [blame] | 81 | const CommandLine* command_line, |
[email protected] | a9b00ac | 2009-06-25 21:03:23 | [diff] [blame] | 82 | PrefService* prefs, |
| 83 | const FilePath& install_directory, |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 84 | MessageLoop* frontend_loop, |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 85 | MessageLoop* backend_loop, |
| 86 | bool autoupdate_enabled) |
[email protected] | 6ef635e4 | 2009-07-26 06:16:12 | [diff] [blame] | 87 | : profile_(profile), |
| 88 | extension_prefs_(new ExtensionPrefs(prefs, install_directory)), |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 89 | backend_loop_(backend_loop), |
[email protected] | a9b00ac | 2009-06-25 21:03:23 | [diff] [blame] | 90 | install_directory_(install_directory), |
[email protected] | 6d60703b | 2009-08-29 01:29:23 | [diff] [blame] | 91 | extensions_enabled_(true), |
[email protected] | e81dba3 | 2009-06-19 20:19:13 | [diff] [blame] | 92 | show_extensions_prompts_(true), |
| 93 | ready_(false) { |
[email protected] | 36a784c | 2009-06-23 06:21:08 | [diff] [blame] | 94 | // Figure out if extension installation should be enabled. |
[email protected] | 6d60703b | 2009-08-29 01:29:23 | [diff] [blame] | 95 | if (command_line->HasSwitch(switches::kDisableExtensions)) { |
| 96 | extensions_enabled_ = false; |
| 97 | } else if (profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) { |
| 98 | extensions_enabled_ = false; |
[email protected] | 6b75ec3 | 2009-08-14 06:37:18 | [diff] [blame] | 99 | } |
[email protected] | 36a784c | 2009-06-23 06:21:08 | [diff] [blame] | 100 | |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 101 | // Set up the ExtensionUpdater |
| 102 | if (autoupdate_enabled) { |
| 103 | int update_frequency = kDefaultUpdateFrequencySeconds; |
| 104 | if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) { |
| 105 | update_frequency = StringToInt(WideToASCII(command_line->GetSwitchValue( |
| 106 | switches::kExtensionsUpdateFrequency))); |
| 107 | } |
[email protected] | 951073e | 2009-08-03 17:54:34 | [diff] [blame] | 108 | updater_ = new ExtensionUpdater(this, prefs, update_frequency, |
[email protected] | 15730c4 | 2009-09-03 00:03:20 | [diff] [blame] | 109 | backend_loop_, g_browser_process->io_thread()->message_loop()); |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 110 | } |
| 111 | |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 112 | backend_ = new ExtensionsServiceBackend(install_directory_, frontend_loop); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | ExtensionsService::~ExtensionsService() { |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 116 | UnloadAllExtensions(); |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 117 | if (updater_.get()) { |
| 118 | updater_->Stop(); |
| 119 | } |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 120 | } |
| 121 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 122 | void ExtensionsService::Init() { |
[email protected] | c6e4a341 | 2009-06-24 15:45:29 | [diff] [blame] | 123 | DCHECK(!ready_); |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 124 | DCHECK_EQ(extensions_.size(), 0u); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 125 | |
[email protected] | 95dd38f | 2009-10-20 20:09:15 | [diff] [blame] | 126 | // Hack: we need to ensure the ResourceDispatcherHost is ready before we load |
| 127 | // the first extension, because its members listen for loaded notifications. |
| 128 | g_browser_process->resource_dispatcher_host(); |
| 129 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 130 | LoadAllExtensions(); |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 131 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 132 | // TODO(erikkay) this should probably be deferred to a future point |
| 133 | // rather than running immediately at startup. |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 134 | CheckForExternalUpdates(); |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 135 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 136 | // TODO(erikkay) this should probably be deferred as well. |
| 137 | GarbageCollectExtensions(); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 138 | } |
| 139 | |
[email protected] | 3cf4f099 | 2009-02-03 23:00:30 | [diff] [blame] | 140 | void ExtensionsService::InstallExtension(const FilePath& extension_path) { |
[email protected] | 2a464a9 | 2009-08-01 17:58:35 | [diff] [blame] | 141 | CrxInstaller::Start(extension_path, install_directory_, Extension::INTERNAL, |
| 142 | "", // no expected id |
| 143 | false, // don't delete crx when complete |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 144 | true, // allow privilege increase |
[email protected] | 2a464a9 | 2009-08-01 17:58:35 | [diff] [blame] | 145 | backend_loop_, |
| 146 | this, |
| 147 | NULL); // no client (silent install) |
[email protected] | 3cf4f099 | 2009-02-03 23:00:30 | [diff] [blame] | 148 | } |
| 149 | |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 150 | void ExtensionsService::UpdateExtension(const std::string& id, |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 151 | const FilePath& extension_path) { |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 152 | if (!GetExtensionByIdInternal(id, true, true)) { |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 153 | LOG(WARNING) << "Will not update extension " << id << " because it is not " |
[email protected] | 4c96793 | 2009-07-31 01:15:49 | [diff] [blame] | 154 | << "installed"; |
| 155 | return; |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 156 | } |
| 157 | |
[email protected] | 2a464a9 | 2009-08-01 17:58:35 | [diff] [blame] | 158 | CrxInstaller::Start(extension_path, install_directory_, Extension::INTERNAL, |
| 159 | id, |
| 160 | true, // delete crx when complete |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 161 | false, // do not allow upgrade of privileges |
[email protected] | 2a464a9 | 2009-08-01 17:58:35 | [diff] [blame] | 162 | backend_loop_, |
| 163 | this, |
| 164 | NULL); // no client (silent install) |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 165 | } |
| 166 | |
[email protected] | 9cddd470 | 2009-07-27 22:09:40 | [diff] [blame] | 167 | void ExtensionsService::ReloadExtension(const std::string& extension_id) { |
[email protected] | b65272f | 2009-08-31 15:47:06 | [diff] [blame] | 168 | FilePath path; |
| 169 | Extension* current_extension = GetExtensionById(extension_id); |
[email protected] | 9cddd470 | 2009-07-27 22:09:40 | [diff] [blame] | 170 | |
[email protected] | b65272f | 2009-08-31 15:47:06 | [diff] [blame] | 171 | // Unload the extension if it's loaded. It might not be loaded if it crashed. |
| 172 | if (current_extension) { |
| 173 | path = current_extension->path(); |
| 174 | UnloadExtension(extension_id); |
| 175 | } |
| 176 | |
| 177 | if (path.empty()) { |
| 178 | // At this point we have to reconstruct the path from prefs, because |
| 179 | // we have no information about this extension in memory. |
| 180 | path = extension_prefs_->GetExtensionPath(extension_id); |
| 181 | } |
| 182 | |
| 183 | if (!path.empty()) |
| 184 | LoadExtension(path); |
[email protected] | 9cddd470 | 2009-07-27 22:09:40 | [diff] [blame] | 185 | } |
| 186 | |
[email protected] | 27b985d | 2009-06-25 17:53:15 | [diff] [blame] | 187 | void ExtensionsService::UninstallExtension(const std::string& extension_id, |
| 188 | bool external_uninstall) { |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 189 | Extension* extension = GetExtensionByIdInternal(extension_id, true, true); |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 190 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 191 | // Callers should not send us nonexistant extensions. |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 192 | DCHECK(extension); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 193 | |
[email protected] | 27b985d | 2009-06-25 17:53:15 | [diff] [blame] | 194 | extension_prefs_->OnExtensionUninstalled(extension, external_uninstall); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 195 | |
| 196 | // Tell the backend to start deleting installed extensions on the file thread. |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 197 | if (Extension::LOAD != extension->location()) { |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame] | 198 | backend_loop_->PostTask(FROM_HERE, NewRunnableFunction( |
| 199 | &extension_file_util::UninstallExtension, extension_id, |
| 200 | install_directory_)); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 201 | } |
| 202 | |
[email protected] | 86c008e8 | 2009-08-28 20:26:05 | [diff] [blame] | 203 | ExtensionDOMUI::UnregisterChromeURLOverrides(profile_, |
| 204 | extension->GetChromeURLOverrides()); |
| 205 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 206 | UnloadExtension(extension_id); |
| 207 | } |
| 208 | |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 209 | void ExtensionsService::EnableExtension(const std::string& extension_id) { |
| 210 | Extension* extension = GetExtensionByIdInternal(extension_id, false, true); |
| 211 | if (!extension) { |
| 212 | NOTREACHED() << "Trying to enable an extension that isn't disabled."; |
| 213 | return; |
| 214 | } |
| 215 | |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 216 | // Remember that we enabled it, unless it's temporary. |
| 217 | if (extension->location() != Extension::LOAD) |
| 218 | extension_prefs_->SetExtensionState(extension, Extension::ENABLED); |
| 219 | |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 220 | // Move it over to the enabled list. |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 221 | extensions_.push_back(extension); |
| 222 | ExtensionList::iterator iter = std::find(disabled_extensions_.begin(), |
| 223 | disabled_extensions_.end(), |
| 224 | extension); |
| 225 | disabled_extensions_.erase(iter); |
| 226 | |
[email protected] | 86c008e8 | 2009-08-28 20:26:05 | [diff] [blame] | 227 | ExtensionDOMUI::RegisterChromeURLOverrides(profile_, |
| 228 | extension->GetChromeURLOverrides()); |
| 229 | |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 230 | NotifyExtensionLoaded(extension); |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 231 | } |
| 232 | |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 233 | void ExtensionsService::DisableExtension(const std::string& extension_id) { |
| 234 | Extension* extension = GetExtensionByIdInternal(extension_id, true, false); |
| 235 | if (!extension) { |
| 236 | NOTREACHED() << "Trying to disable an extension that isn't enabled."; |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | // Remember that we disabled it, unless it's temporary. |
| 241 | if (extension->location() != Extension::LOAD) |
| 242 | extension_prefs_->SetExtensionState(extension, Extension::DISABLED); |
| 243 | |
| 244 | // Move it over to the disabled list. |
| 245 | disabled_extensions_.push_back(extension); |
| 246 | ExtensionList::iterator iter = std::find(extensions_.begin(), |
| 247 | extensions_.end(), |
| 248 | extension); |
| 249 | extensions_.erase(iter); |
| 250 | |
| 251 | ExtensionDOMUI::UnregisterChromeURLOverrides(profile_, |
| 252 | extension->GetChromeURLOverrides()); |
| 253 | |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 254 | NotifyExtensionUnloaded(extension); |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 255 | } |
| 256 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 257 | void ExtensionsService::LoadExtension(const FilePath& extension_path) { |
| 258 | backend_loop_->PostTask(FROM_HERE, NewRunnableMethod(backend_.get(), |
| 259 | &ExtensionsServiceBackend::LoadSingleExtension, |
| 260 | extension_path, scoped_refptr<ExtensionsService>(this))); |
| 261 | } |
| 262 | |
| 263 | void ExtensionsService::LoadAllExtensions() { |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 264 | // Load the previously installed extensions. |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 265 | scoped_ptr<InstalledExtensions> installed( |
| 266 | new InstalledExtensions(extension_prefs_.get())); |
| 267 | installed->VisitInstalledExtensions( |
| 268 | NewCallback(this, &ExtensionsService::LoadInstalledExtension)); |
| 269 | OnLoadedInstalledExtensions(); |
| 270 | } |
| 271 | |
| 272 | void ExtensionsService::LoadInstalledExtension( |
| 273 | DictionaryValue* manifest, const std::string& id, |
| 274 | const FilePath& path, Extension::Location location) { |
| 275 | std::string error; |
| 276 | Extension* extension = NULL; |
| 277 | if (manifest) { |
| 278 | scoped_ptr<Extension> tmp(new Extension(path)); |
| 279 | if (tmp->InitFromValue(*manifest, true, &error)) { |
| 280 | extension = tmp.release(); |
| 281 | } |
| 282 | } else { |
| 283 | // TODO(mpcomplete): obsolete. remove after migration period. |
| 284 | // http://code.google.com/p/chromium/issues/detail?id=19733 |
| 285 | extension = extension_file_util::LoadExtension(path, |
| 286 | true, // Require id |
| 287 | &error); |
| 288 | } |
| 289 | |
| 290 | if (!extension) { |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 291 | ReportExtensionLoadError(path, |
| 292 | error, |
| 293 | NotificationType::EXTENSION_INSTALL_ERROR, |
| 294 | false); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 295 | return; |
| 296 | } |
| 297 | |
| 298 | extension->set_location(location); |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 299 | OnExtensionLoaded(extension, true); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 300 | |
| 301 | if (location == Extension::EXTERNAL_PREF || |
| 302 | location == Extension::EXTERNAL_REGISTRY) { |
| 303 | backend_loop_->PostTask(FROM_HERE, NewRunnableMethod(backend_.get(), |
| 304 | &ExtensionsServiceBackend::CheckExternalUninstall, |
| 305 | scoped_refptr<ExtensionsService>(this), id, location)); |
| 306 | } |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 307 | } |
| 308 | |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 309 | void ExtensionsService::NotifyExtensionLoaded(Extension* extension) { |
| 310 | LOG(INFO) << "Sending EXTENSION_LOADED"; |
| 311 | |
| 312 | // The ChromeURLRequestContext needs to be first to know that the extension |
| 313 | // was loaded, otherwise a race can arise where a renderer that is created |
| 314 | // for the extension may try to load an extension URL with an extension id |
| 315 | // that the request context doesn't yet know about. |
| 316 | if (profile_ && !profile_->IsOffTheRecord()) { |
[email protected] | be180c80 | 2009-10-23 06:33:31 | [diff] [blame] | 317 | ChromeURLRequestContextGetter* context_getter = |
| 318 | static_cast<ChromeURLRequestContextGetter*>( |
| 319 | profile_->GetRequestContext()); |
| 320 | if (context_getter) { |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 321 | g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE, |
[email protected] | be180c80 | 2009-10-23 06:33:31 | [diff] [blame] | 322 | NewRunnableMethod(context_getter, |
| 323 | &ChromeURLRequestContextGetter::OnNewExtensions, |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 324 | extension->id(), |
| 325 | extension->path())); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | NotificationService::current()->Notify( |
| 330 | NotificationType::EXTENSION_LOADED, |
| 331 | Source<ExtensionsService>(this), |
| 332 | Details<Extension>(extension)); |
| 333 | } |
| 334 | |
| 335 | void ExtensionsService::NotifyExtensionUnloaded(Extension* extension) { |
| 336 | LOG(INFO) << "Sending EXTENSION_UNLOADED"; |
| 337 | |
| 338 | NotificationService::current()->Notify( |
| 339 | NotificationType::EXTENSION_UNLOADED, |
| 340 | Source<ExtensionsService>(this), |
| 341 | Details<Extension>(extension)); |
| 342 | |
| 343 | if (profile_ && !profile_->IsOffTheRecord()) { |
[email protected] | be180c80 | 2009-10-23 06:33:31 | [diff] [blame] | 344 | ChromeURLRequestContextGetter* context_getter = |
| 345 | static_cast<ChromeURLRequestContextGetter*>( |
| 346 | profile_->GetRequestContext()); |
| 347 | if (context_getter) { |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 348 | g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE, |
[email protected] | be180c80 | 2009-10-23 06:33:31 | [diff] [blame] | 349 | NewRunnableMethod( |
| 350 | context_getter, |
| 351 | &ChromeURLRequestContextGetter::OnUnloadedExtension, |
| 352 | extension->id())); |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
[email protected] | 6b75ec3 | 2009-08-14 06:37:18 | [diff] [blame] | 357 | void ExtensionsService::UpdateExtensionBlacklist( |
| 358 | const std::vector<std::string>& blacklist) { |
| 359 | // Use this set to indicate if an extension in the blacklist has been used. |
| 360 | std::set<std::string> blacklist_set; |
| 361 | for (unsigned int i = 0; i < blacklist.size(); ++i) { |
| 362 | if (Extension::IdIsValid(blacklist[i])) { |
| 363 | blacklist_set.insert(blacklist[i]); |
| 364 | } |
| 365 | } |
| 366 | extension_prefs_->UpdateBlacklist(blacklist_set); |
| 367 | std::vector<std::string> to_be_removed; |
| 368 | // Loop current extensions, unload installed extensions. |
| 369 | for (ExtensionList::const_iterator iter = extensions_.begin(); |
| 370 | iter != extensions_.end(); ++iter) { |
| 371 | Extension* extension = (*iter); |
| 372 | if (blacklist_set.find(extension->id()) != blacklist_set.end()) { |
| 373 | to_be_removed.push_back(extension->id()); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | // UnloadExtension will change the extensions_ list. So, we should |
| 378 | // call it outside the iterator loop. |
| 379 | for (unsigned int i = 0; i < to_be_removed.size(); ++i) { |
| 380 | UnloadExtension(to_be_removed[i]); |
| 381 | } |
| 382 | } |
| 383 | |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 384 | void ExtensionsService::CheckForExternalUpdates() { |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 385 | // This installs or updates externally provided extensions. |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 386 | // TODO(aa): Why pass this list into the provider, why not just filter it |
| 387 | // later? |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 388 | std::set<std::string> killed_extensions; |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 389 | extension_prefs_->GetKilledExtensionIds(&killed_extensions); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 390 | backend_loop_->PostTask(FROM_HERE, NewRunnableMethod(backend_.get(), |
| 391 | &ExtensionsServiceBackend::CheckForExternalUpdates, |
| 392 | killed_extensions, |
| 393 | scoped_refptr<ExtensionsService>(this))); |
| 394 | } |
| 395 | |
| 396 | void ExtensionsService::UnloadExtension(const std::string& extension_id) { |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 397 | scoped_ptr<Extension> extension( |
| 398 | GetExtensionByIdInternal(extension_id, true, true)); |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 399 | |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 400 | // Callers should not send us nonexistant extensions. |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 401 | CHECK(extension.get()); |
| 402 | |
[email protected] | 86c008e8 | 2009-08-28 20:26:05 | [diff] [blame] | 403 | ExtensionDOMUI::UnregisterChromeURLOverrides(profile_, |
| 404 | extension->GetChromeURLOverrides()); |
| 405 | |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 406 | ExtensionList::iterator iter = std::find(disabled_extensions_.begin(), |
| 407 | disabled_extensions_.end(), |
| 408 | extension.get()); |
| 409 | if (iter != disabled_extensions_.end()) { |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 410 | disabled_extensions_.erase(iter); |
[email protected] | 86693068 | 2009-08-18 22:53:47 | [diff] [blame] | 411 | NotificationService::current()->Notify( |
| 412 | NotificationType::EXTENSION_UNLOADED_DISABLED, |
| 413 | Source<ExtensionsService>(this), |
| 414 | Details<Extension>(extension.get())); |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 415 | return; |
| 416 | } |
| 417 | |
| 418 | iter = std::find(extensions_.begin(), extensions_.end(), extension.get()); |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 419 | |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 420 | // Remove the extension from our list. |
| 421 | extensions_.erase(iter); |
| 422 | |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 423 | NotifyExtensionUnloaded(extension.get()); |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 424 | } |
| 425 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 426 | void ExtensionsService::UnloadAllExtensions() { |
| 427 | ExtensionList::iterator iter; |
[email protected] | c6e4a341 | 2009-06-24 15:45:29 | [diff] [blame] | 428 | for (iter = extensions_.begin(); iter != extensions_.end(); ++iter) |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 429 | delete *iter; |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 430 | extensions_.clear(); |
[email protected] | c6e4a341 | 2009-06-24 15:45:29 | [diff] [blame] | 431 | |
| 432 | // TODO(erikkay) should there be a notification for this? We can't use |
| 433 | // EXTENSION_UNLOADED since that implies that the extension has been disabled |
| 434 | // or uninstalled, and UnloadAll is just part of shutdown. |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | void ExtensionsService::ReloadExtensions() { |
| 438 | UnloadAllExtensions(); |
| 439 | LoadAllExtensions(); |
| 440 | } |
| 441 | |
| 442 | void ExtensionsService::GarbageCollectExtensions() { |
[email protected] | b6ab96d | 2009-08-20 18:58:19 | [diff] [blame] | 443 | InstalledExtensionSet installed( |
| 444 | new InstalledExtensions(extension_prefs_.get())); |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame] | 445 | backend_loop_->PostTask(FROM_HERE, NewRunnableFunction( |
[email protected] | b6ab96d | 2009-08-20 18:58:19 | [diff] [blame] | 446 | &extension_file_util::GarbageCollectExtensions, install_directory_, |
| 447 | installed.extensions())); |
[email protected] | 3cf4f099 | 2009-02-03 23:00:30 | [diff] [blame] | 448 | } |
| 449 | |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 450 | void ExtensionsService::OnLoadedInstalledExtensions() { |
[email protected] | e81dba3 | 2009-06-19 20:19:13 | [diff] [blame] | 451 | ready_ = true; |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 452 | if (updater_.get()) { |
| 453 | updater_->Start(); |
| 454 | } |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 455 | NotificationService::current()->Notify( |
| 456 | NotificationType::EXTENSIONS_READY, |
| 457 | Source<ExtensionsService>(this), |
| 458 | NotificationService::NoDetails()); |
| 459 | } |
| 460 | |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 461 | void ExtensionsService::OnExtensionLoaded(Extension* extension, |
| 462 | bool allow_privilege_increase) { |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 463 | // Ensure extension is deleted unless we transfer ownership. |
| 464 | scoped_ptr<Extension> scoped_extension(extension); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 465 | |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 466 | if (extensions_enabled() || |
| 467 | extension->IsTheme() || |
| 468 | extension->location() == Extension::LOAD || |
| 469 | Extension::IsExternalLocation(extension->location())) { |
| 470 | Extension* old = GetExtensionByIdInternal(extension->id(), true, true); |
| 471 | if (old) { |
| 472 | if (extension->version()->CompareTo(*(old->version())) > 0) { |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 473 | bool allow_silent_upgrade = |
| 474 | allow_privilege_increase || !Extension::IsPrivilegeIncrease( |
| 475 | old, extension); |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 476 | |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 477 | // To upgrade an extension in place, unload the old one and |
| 478 | // then load the new one. |
| 479 | UnloadExtension(old->id()); |
| 480 | old = NULL; |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 481 | |
[email protected] | b24d831 | 2009-08-27 06:47:46 | [diff] [blame] | 482 | if (!allow_silent_upgrade) { |
| 483 | // Extension has changed permissions significantly. Disable it and |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 484 | // notify the user. |
| 485 | extension_prefs_->SetExtensionState(extension, Extension::DISABLED); |
| 486 | NotificationService::current()->Notify( |
| 487 | NotificationType::EXTENSION_UPDATE_DISABLED, |
| 488 | Source<ExtensionsService>(this), |
| 489 | Details<Extension>(extension)); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 490 | } |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 491 | } else { |
| 492 | // We already have the extension of the same or older version. |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 493 | std::string error_message("Duplicate extension load attempt: "); |
| 494 | error_message += extension->id(); |
| 495 | LOG(WARNING) << error_message; |
| 496 | ReportExtensionLoadError(extension->path(), |
| 497 | error_message, |
| 498 | NotificationType::EXTENSION_OVERINSTALL_ERROR, |
| 499 | false); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 500 | return; |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 501 | } |
[email protected] | ba74f35 | 2009-06-11 18:54:45 | [diff] [blame] | 502 | } |
[email protected] | 86a27407 | 2009-06-11 02:06:45 | [diff] [blame] | 503 | |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 504 | switch (extension_prefs_->GetExtensionState(extension->id())) { |
| 505 | case Extension::ENABLED: |
| 506 | extensions_.push_back(scoped_extension.release()); |
| 507 | |
[email protected] | aeb53b3 | 2009-10-29 07:34:45 | [diff] [blame^] | 508 | // We delay starting up the browser event router until at least one |
| 509 | // extension that needs it is loaded. |
| 510 | if (extension->HasApiPermission(Extension::kTabPermission)) { |
| 511 | ExtensionBrowserEventRouter::GetInstance()->Init(); |
| 512 | } |
| 513 | |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 514 | if (extension->location() != Extension::LOAD) |
| 515 | extension_prefs_->MigrateToPrefs(extension); |
| 516 | |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 517 | NotifyExtensionLoaded(extension); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 518 | |
| 519 | if (extension->IsTheme() && extension->location() == Extension::LOAD) { |
| 520 | NotificationService::current()->Notify( |
| 521 | NotificationType::THEME_INSTALLED, |
| 522 | Source<ExtensionsService>(this), |
| 523 | Details<Extension>(extension)); |
[email protected] | 86c008e8 | 2009-08-28 20:26:05 | [diff] [blame] | 524 | } else { |
| 525 | ExtensionDOMUI::RegisterChromeURLOverrides(profile_, |
| 526 | extension->GetChromeURLOverrides()); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 527 | } |
| 528 | break; |
| 529 | case Extension::DISABLED: |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 530 | NotificationService::current()->Notify( |
| 531 | NotificationType::EXTENSION_UPDATE_DISABLED, |
| 532 | Source<ExtensionsService>(this), |
| 533 | Details<Extension>(extension)); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 534 | disabled_extensions_.push_back(scoped_extension.release()); |
| 535 | break; |
| 536 | default: |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 537 | NOTREACHED(); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 538 | break; |
[email protected] | 811f343 | 2009-07-25 19:38:21 | [diff] [blame] | 539 | } |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 540 | } |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 541 | } |
| 542 | |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 543 | void ExtensionsService::OnExtensionInstalled(Extension* extension, |
| 544 | bool allow_privilege_increase) { |
[email protected] | b6ab96d | 2009-08-20 18:58:19 | [diff] [blame] | 545 | extension_prefs_->OnExtensionInstalled(extension); |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 546 | |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 547 | // If the extension is a theme, tell the profile (and therefore ThemeProvider) |
| 548 | // to apply it. |
| 549 | if (extension->IsTheme()) { |
[email protected] | 9ceb0734 | 2009-07-26 04:09:23 | [diff] [blame] | 550 | NotificationService::current()->Notify( |
| 551 | NotificationType::THEME_INSTALLED, |
| 552 | Source<ExtensionsService>(this), |
| 553 | Details<Extension>(extension)); |
[email protected] | 9197f3b | 2009-06-02 00:49:27 | [diff] [blame] | 554 | } else { |
| 555 | NotificationService::current()->Notify( |
| 556 | NotificationType::EXTENSION_INSTALLED, |
[email protected] | c6e4a341 | 2009-06-24 15:45:29 | [diff] [blame] | 557 | Source<ExtensionsService>(this), |
[email protected] | 9197f3b | 2009-06-02 00:49:27 | [diff] [blame] | 558 | Details<Extension>(extension)); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 559 | } |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 560 | |
| 561 | // Also load the extension. |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 562 | OnExtensionLoaded(extension, allow_privilege_increase); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 563 | } |
| 564 | |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 565 | void ExtensionsService::OnExtensionOverinstallAttempted(const std::string& id) { |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 566 | Extension* extension = GetExtensionById(id); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 567 | if (extension && extension->IsTheme()) { |
[email protected] | 9ceb0734 | 2009-07-26 04:09:23 | [diff] [blame] | 568 | NotificationService::current()->Notify( |
| 569 | NotificationType::THEME_INSTALLED, |
| 570 | Source<ExtensionsService>(this), |
| 571 | Details<Extension>(extension)); |
[email protected] | 91e1bd8 | 2009-09-03 22:04:40 | [diff] [blame] | 572 | } else { |
| 573 | NotificationService::current()->Notify( |
| 574 | NotificationType::NO_THEME_DETECTED, |
| 575 | Source<ExtensionsService>(this), |
| 576 | NotificationService::NoDetails()); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 577 | } |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 578 | } |
| 579 | |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 580 | Extension* ExtensionsService::GetExtensionByIdInternal(const std::string& id, |
| 581 | bool include_enabled, |
| 582 | bool include_disabled) { |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 583 | std::string lowercase_id = StringToLowerASCII(id); |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 584 | if (include_enabled) { |
| 585 | for (ExtensionList::const_iterator iter = extensions_.begin(); |
| 586 | iter != extensions_.end(); ++iter) { |
| 587 | if ((*iter)->id() == lowercase_id) |
| 588 | return *iter; |
| 589 | } |
| 590 | } |
| 591 | if (include_disabled) { |
| 592 | for (ExtensionList::const_iterator iter = disabled_extensions_.begin(); |
| 593 | iter != disabled_extensions_.end(); ++iter) { |
| 594 | if ((*iter)->id() == lowercase_id) |
| 595 | return *iter; |
| 596 | } |
[email protected] | ce5c450 | 2009-05-06 16:46:11 | [diff] [blame] | 597 | } |
| 598 | return NULL; |
| 599 | } |
| 600 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 601 | Extension* ExtensionsService::GetExtensionByURL(const GURL& url) { |
| 602 | std::string host = url.host(); |
| 603 | return GetExtensionById(host); |
| 604 | } |
| 605 | |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 606 | void ExtensionsService::ClearProvidersForTesting() { |
| 607 | backend_loop_->PostTask(FROM_HERE, NewRunnableMethod(backend_.get(), |
| 608 | &ExtensionsServiceBackend::ClearProvidersForTesting)); |
| 609 | } |
| 610 | |
| 611 | void ExtensionsService::SetProviderForTesting( |
| 612 | Extension::Location location, ExternalExtensionProvider* test_provider) { |
| 613 | backend_loop_->PostTask(FROM_HERE, NewRunnableMethod(backend_.get(), |
| 614 | &ExtensionsServiceBackend::SetProviderForTesting, |
| 615 | location, test_provider)); |
| 616 | } |
| 617 | |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 618 | void ExtensionsService::OnExternalExtensionFound(const std::string& id, |
| 619 | const std::string& version, |
| 620 | const FilePath& path, |
| 621 | Extension::Location location) { |
| 622 | // Before even bothering to unpack, check and see if we already have this |
[email protected] | 4c96793 | 2009-07-31 01:15:49 | [diff] [blame] | 623 | // version. This is important because these extensions are going to get |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 624 | // installed on every startup. |
| 625 | Extension* existing = GetExtensionById(id); |
[email protected] | a3a63ff8 | 2009-08-04 06:44:11 | [diff] [blame] | 626 | scoped_ptr<Version> other(Version::GetVersionFromString(version)); |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 627 | if (existing) { |
[email protected] | a3a63ff8 | 2009-08-04 06:44:11 | [diff] [blame] | 628 | switch (existing->version()->CompareTo(*other)) { |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 629 | case -1: // existing version is older, we should upgrade |
| 630 | break; |
| 631 | case 0: // existing version is same, do nothing |
| 632 | return; |
| 633 | case 1: // existing version is newer, uh-oh |
| 634 | LOG(WARNING) << "Found external version of extension " << id |
| 635 | << "that is older than current version. Current version " |
| 636 | << "is: " << existing->VersionString() << ". New version " |
| 637 | << "is: " << version << ". Keeping current version."; |
| 638 | return; |
| 639 | } |
| 640 | } |
| 641 | |
[email protected] | 2a464a9 | 2009-08-01 17:58:35 | [diff] [blame] | 642 | CrxInstaller::Start(path, install_directory_, location, id, |
| 643 | false, // don't delete crx when complete |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 644 | true, // allow privilege increase |
[email protected] | 2a464a9 | 2009-08-01 17:58:35 | [diff] [blame] | 645 | backend_loop_, |
| 646 | this, |
| 647 | NULL); // no client (silent install) |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 648 | } |
| 649 | |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 650 | void ExtensionsService::ReportExtensionLoadError( |
| 651 | const FilePath& extension_path, |
| 652 | const std::string &error, |
| 653 | NotificationType type, |
| 654 | bool be_noisy) { |
| 655 | NotificationService* service = NotificationService::current(); |
| 656 | service->Notify(type, |
| 657 | Source<ExtensionsService>(this), |
| 658 | Details<const std::string>(&error)); |
| 659 | |
| 660 | // TODO(port): note that this isn't guaranteed to work properly on Linux. |
| 661 | std::string path_str = WideToASCII(extension_path.ToWStringHack()); |
| 662 | std::string message = StringPrintf("Could not load extension from '%s'. %s", |
| 663 | path_str.c_str(), error.c_str()); |
| 664 | ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy); |
| 665 | } |
| 666 | |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 667 | // ExtensionsServicesBackend |
| 668 | |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 669 | ExtensionsServiceBackend::ExtensionsServiceBackend( |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 670 | const FilePath& install_directory, MessageLoop* frontend_loop) |
[email protected] | 0c7bc4b | 2009-05-30 01:47:08 | [diff] [blame] | 671 | : frontend_(NULL), |
| 672 | install_directory_(install_directory), |
[email protected] | 0c7bc4b | 2009-05-30 01:47:08 | [diff] [blame] | 673 | alert_on_error_(false), |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 674 | frontend_loop_(frontend_loop) { |
| 675 | // TODO(aa): This ends up doing blocking IO on the UI thread because it reads |
| 676 | // pref data in the ctor and that is called on the UI thread. Would be better |
| 677 | // to re-read data each time we list external extensions, anyway. |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 678 | external_extension_providers_[Extension::EXTERNAL_PREF] = |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 679 | linked_ptr<ExternalExtensionProvider>( |
[email protected] | 27b985d | 2009-06-25 17:53:15 | [diff] [blame] | 680 | new ExternalPrefExtensionProvider()); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 681 | #if defined(OS_WIN) |
| 682 | external_extension_providers_[Extension::EXTERNAL_REGISTRY] = |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 683 | linked_ptr<ExternalExtensionProvider>( |
| 684 | new ExternalRegistryExtensionProvider()); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 685 | #endif |
| 686 | } |
| 687 | |
| 688 | ExtensionsServiceBackend::~ExtensionsServiceBackend() { |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 689 | } |
| 690 | |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 691 | void ExtensionsServiceBackend::LoadSingleExtension( |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 692 | const FilePath& path_in, scoped_refptr<ExtensionsService> frontend) { |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 693 | frontend_ = frontend; |
| 694 | |
| 695 | // Explicit UI loads are always noisy. |
| 696 | alert_on_error_ = true; |
| 697 | |
[email protected] | cc5da33 | 2009-03-04 08:02:51 | [diff] [blame] | 698 | FilePath extension_path = path_in; |
[email protected] | f36fa4fb | 2009-06-19 18:23:50 | [diff] [blame] | 699 | file_util::AbsolutePath(&extension_path); |
[email protected] | bf24d2c | 2009-02-24 23:07:45 | [diff] [blame] | 700 | |
| 701 | LOG(INFO) << "Loading single extension from " << |
[email protected] | cc5da33 | 2009-03-04 08:02:51 | [diff] [blame] | 702 | WideToASCII(extension_path.BaseName().ToWStringHack()); |
[email protected] | bf24d2c | 2009-02-24 23:07:45 | [diff] [blame] | 703 | |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame] | 704 | std::string error; |
| 705 | Extension* extension = extension_file_util::LoadExtension( |
| 706 | extension_path, |
| 707 | false, // Don't require id |
| 708 | &error); |
| 709 | |
| 710 | if (!extension) { |
| 711 | ReportExtensionLoadError(extension_path, error); |
| 712 | return; |
[email protected] | 0877fd9 | 2009-02-03 16:34:06 | [diff] [blame] | 713 | } |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame] | 714 | |
| 715 | extension->set_location(Extension::LOAD); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 716 | ReportExtensionLoaded(extension); |
[email protected] | 0877fd9 | 2009-02-03 16:34:06 | [diff] [blame] | 717 | } |
| 718 | |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 719 | void ExtensionsServiceBackend::ReportExtensionLoadError( |
[email protected] | cc5da33 | 2009-03-04 08:02:51 | [diff] [blame] | 720 | const FilePath& extension_path, const std::string &error) { |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 721 | // In the unit tests, frontend_loop_ may be null. |
| 722 | if (frontend_loop_ == NULL) { |
| 723 | frontend_->ReportExtensionLoadError( |
| 724 | extension_path, |
| 725 | error, |
| 726 | NotificationType::EXTENSION_INSTALL_ERROR, |
| 727 | alert_on_error_); |
| 728 | return; |
| 729 | } |
| 730 | |
| 731 | frontend_loop_->PostTask(FROM_HERE, |
| 732 | NewRunnableMethod(frontend_, |
| 733 | &ExtensionsService::ReportExtensionLoadError, extension_path, |
| 734 | error, NotificationType::EXTENSION_INSTALL_ERROR, alert_on_error_)); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 735 | } |
| 736 | |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 737 | void ExtensionsServiceBackend::ReportExtensionLoaded(Extension* extension) { |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 738 | frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod( |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 739 | frontend_, &ExtensionsService::OnExtensionLoaded, extension, true)); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 740 | } |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 741 | |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 742 | bool ExtensionsServiceBackend::LookupExternalExtension( |
| 743 | const std::string& id, Version** version, Extension::Location* location) { |
| 744 | scoped_ptr<Version> extension_version; |
| 745 | for (ProviderMap::const_iterator i = external_extension_providers_.begin(); |
| 746 | i != external_extension_providers_.end(); ++i) { |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 747 | const ExternalExtensionProvider* provider = i->second.get(); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 748 | extension_version.reset(provider->RegisteredVersion(id, location)); |
| 749 | if (extension_version.get()) { |
| 750 | if (version) |
| 751 | *version = extension_version.release(); |
| 752 | return true; |
| 753 | } |
| 754 | } |
| 755 | return false; |
| 756 | } |
| 757 | |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 758 | // Some extensions will autoupdate themselves externally from Chrome. These |
| 759 | // are typically part of some larger client application package. To support |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 760 | // these, the extension will register its location in the the preferences file |
| 761 | // (and also, on Windows, in the registry) and this code will periodically |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 762 | // check that location for a .crx file, which it will then install locally if |
| 763 | // a new version is available. |
| 764 | void ExtensionsServiceBackend::CheckForExternalUpdates( |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 765 | std::set<std::string> ids_to_ignore, |
| 766 | scoped_refptr<ExtensionsService> frontend) { |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 767 | // Note that this installation is intentionally silent (since it didn't |
| 768 | // go through the front-end). Extensions that are registered in this |
| 769 | // way are effectively considered 'pre-bundled', and so implicitly |
| 770 | // trusted. In general, if something has HKLM or filesystem access, |
| 771 | // they could install an extension manually themselves anyway. |
| 772 | alert_on_error_ = false; |
| 773 | frontend_ = frontend; |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 774 | |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 775 | // Ask each external extension provider to give us a call back for each |
| 776 | // extension they know about. See OnExternalExtensionFound. |
| 777 | for (ProviderMap::const_iterator i = external_extension_providers_.begin(); |
| 778 | i != external_extension_providers_.end(); ++i) { |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 779 | ExternalExtensionProvider* provider = i->second.get(); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 780 | provider->VisitRegisteredExtension(this, ids_to_ignore); |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 781 | } |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 782 | } |
| 783 | |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 784 | void ExtensionsServiceBackend::CheckExternalUninstall( |
| 785 | scoped_refptr<ExtensionsService> frontend, const std::string& id, |
| 786 | Extension::Location location) { |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 787 | // Check if the providers know about this extension. |
| 788 | ProviderMap::const_iterator i = external_extension_providers_.find(location); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 789 | if (i == external_extension_providers_.end()) { |
| 790 | NOTREACHED() << "CheckExternalUninstall called for non-external extension " |
| 791 | << location; |
| 792 | return; |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 793 | } |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 794 | |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 795 | scoped_ptr<Version> version; |
| 796 | version.reset(i->second->RegisteredVersion(id, NULL)); |
| 797 | if (version.get()) |
| 798 | return; // Yup, known extension, don't uninstall. |
| 799 | |
| 800 | // This is an external extension that we don't have registered. Uninstall. |
| 801 | frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod( |
| 802 | frontend.get(), &ExtensionsService::UninstallExtension, |
| 803 | id, true)); |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 804 | } |
| 805 | |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 806 | void ExtensionsServiceBackend::ClearProvidersForTesting() { |
| 807 | external_extension_providers_.clear(); |
| 808 | } |
| 809 | |
| 810 | void ExtensionsServiceBackend::SetProviderForTesting( |
| 811 | Extension::Location location, |
| 812 | ExternalExtensionProvider* test_provider) { |
| 813 | DCHECK(test_provider); |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 814 | external_extension_providers_[location] = |
| 815 | linked_ptr<ExternalExtensionProvider>(test_provider); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | void ExtensionsServiceBackend::OnExternalExtensionFound( |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 819 | const std::string& id, const Version* version, const FilePath& path, |
| 820 | Extension::Location location) { |
| 821 | frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod(frontend_, |
| 822 | &ExtensionsService::OnExternalExtensionFound, id, version->GetString(), |
| 823 | path, location)); |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 824 | } |