[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 "app/l10n_util.h" |
| 8 | #include "base/command_line.h" |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 9 | #include "base/file_util.h" |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 10 | #include "base/string_util.h" |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 11 | #include "base/values.h" |
[email protected] | a5720987 | 2009-05-04 22:53:14 | [diff] [blame] | 12 | #include "chrome/browser/browser.h" |
| 13 | #include "chrome/browser/browser_list.h" |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 14 | #include "chrome/browser/browser_process.h" |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 15 | #include "chrome/browser/chrome_thread.h" |
[email protected] | b68d5ed | 2009-04-16 02:41:28 | [diff] [blame] | 16 | #include "chrome/browser/extensions/extension_browser_event_router.h" |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame^] | 17 | #include "chrome/browser/extensions/extension_file_util.h" |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 18 | #include "chrome/browser/extensions/extension_process_manager.h" |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 19 | #include "chrome/browser/extensions/extension_updater.h" |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 20 | #include "chrome/browser/extensions/external_extension_provider.h" |
| 21 | #include "chrome/browser/extensions/external_pref_extension_provider.h" |
[email protected] | 6ef635e4 | 2009-07-26 06:16:12 | [diff] [blame] | 22 | #include "chrome/browser/extensions/theme_preview_infobar_delegate.h" |
[email protected] | 81e6378 | 2009-02-27 19:35:09 | [diff] [blame] | 23 | #include "chrome/browser/profile.h" |
[email protected] | 6ef635e4 | 2009-07-26 06:16:12 | [diff] [blame] | 24 | #include "chrome/browser/tab_contents/tab_contents.h" |
[email protected] | e2eb4311 | 2009-05-29 21:19:54 | [diff] [blame] | 25 | #include "chrome/common/chrome_switches.h" |
[email protected] | 5b1a0e2 | 2009-05-26 19:00:58 | [diff] [blame] | 26 | #include "chrome/common/extensions/extension.h" |
[email protected] | 5b1a0e2 | 2009-05-26 19:00:58 | [diff] [blame] | 27 | #include "chrome/common/extensions/extension_error_reporter.h" |
[email protected] | 8289126 | 2008-12-24 00:21:26 | [diff] [blame] | 28 | #include "chrome/common/notification_service.h" |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 29 | #include "chrome/common/pref_names.h" |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 30 | #include "chrome/common/pref_service.h" |
[email protected] | a5720987 | 2009-05-04 22:53:14 | [diff] [blame] | 31 | #include "chrome/common/url_constants.h" |
[email protected] | e2eb4311 | 2009-05-29 21:19:54 | [diff] [blame] | 32 | #include "grit/chromium_strings.h" |
| 33 | #include "grit/generated_resources.h" |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 34 | |
[email protected] | 79db623 | 2009-02-13 20:51:20 | [diff] [blame] | 35 | #if defined(OS_WIN) |
[email protected] | e2eb4311 | 2009-05-29 21:19:54 | [diff] [blame] | 36 | #include "app/win_util.h" |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 37 | #include "chrome/browser/extensions/external_registry_extension_provider_win.h" |
[email protected] | 638d352 | 2009-06-25 14:52:01 | [diff] [blame] | 38 | #elif defined(OS_MACOSX) |
| 39 | #include "base/scoped_cftyperef.h" |
| 40 | #include "base/sys_string_conversions.h" |
| 41 | #include <CoreFoundation/CFUserNotification.h> |
[email protected] | 79db623 | 2009-02-13 20:51:20 | [diff] [blame] | 42 | #endif |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 43 | |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 44 | // ExtensionsService. |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 45 | |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 46 | const char* ExtensionsService::kInstallDirectoryName = "Extensions"; |
| 47 | const char* ExtensionsService::kCurrentVersionFileName = "Current Version"; |
[email protected] | 494c06e | 2009-07-25 01:06:42 | [diff] [blame] | 48 | |
| 49 | const char* ExtensionsService::kGalleryDownloadURLPrefix = |
| 50 | "https://dl-ssl.google.com/chrome/"; |
[email protected] | 75a2567 | 2009-07-24 17:41:39 | [diff] [blame] | 51 | const char* ExtensionsService::kGalleryURLPrefix = |
| 52 | "https://tools.google.com/chrome/"; |
| 53 | |
[email protected] | 4289d9b | 2009-07-25 21:17:34 | [diff] [blame] | 54 | // static |
| 55 | bool ExtensionsService::IsDownloadFromGallery(const GURL& download_url, |
| 56 | const GURL& referrer_url) { |
| 57 | if (StartsWithASCII(download_url.spec(), kGalleryDownloadURLPrefix, false) && |
| 58 | StartsWithASCII(referrer_url.spec(), kGalleryURLPrefix, false)) { |
| 59 | return true; |
| 60 | } else { |
| 61 | return false; |
| 62 | } |
| 63 | } |
| 64 | |
[email protected] | af1277b | 2009-07-28 00:47:53 | [diff] [blame] | 65 | // This class hosts a SandboxedExtensionUnpacker task and routes the results |
| 66 | // back to ExtensionsService. The unpack process is started immediately on |
| 67 | // construction of this object. |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 68 | class ExtensionsServiceBackend::UnpackerClient |
[email protected] | af1277b | 2009-07-28 00:47:53 | [diff] [blame] | 69 | : public SandboxedExtensionUnpackerClient { |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 70 | public: |
| 71 | UnpackerClient(ExtensionsServiceBackend* backend, |
| 72 | const FilePath& extension_path, |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 73 | const std::string& expected_id, |
[email protected] | c1e432a | 2009-07-22 21:21:48 | [diff] [blame] | 74 | bool silent, bool from_gallery) |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 75 | : backend_(backend), extension_path_(extension_path), |
[email protected] | af1277b | 2009-07-28 00:47:53 | [diff] [blame] | 76 | expected_id_(expected_id), silent_(silent), from_gallery_(from_gallery) { |
| 77 | unpacker_ = new SandboxedExtensionUnpacker(extension_path, |
| 78 | backend->resource_dispatcher_host_, this); |
| 79 | unpacker_->Start(); |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | private: |
[email protected] | af1277b | 2009-07-28 00:47:53 | [diff] [blame] | 83 | // SandboxedExtensionUnpackerClient |
| 84 | virtual void OnUnpackSuccess(const FilePath& temp_dir, |
| 85 | const FilePath& extension_dir, |
| 86 | Extension* extension) { |
| 87 | backend_->OnExtensionUnpacked(extension_path_, extension_dir, extension, |
| 88 | expected_id_, silent_, from_gallery_); |
| 89 | file_util::Delete(temp_dir, true); |
| 90 | delete this; |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 91 | } |
| 92 | |
[email protected] | af1277b | 2009-07-28 00:47:53 | [diff] [blame] | 93 | virtual void OnUnpackFailure(const std::string& error_message) { |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 94 | backend_->ReportExtensionInstallError(extension_path_, error_message); |
[email protected] | af1277b | 2009-07-28 00:47:53 | [diff] [blame] | 95 | delete this; |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 96 | } |
| 97 | |
[email protected] | af1277b | 2009-07-28 00:47:53 | [diff] [blame] | 98 | scoped_refptr<SandboxedExtensionUnpacker> unpacker_; |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 99 | |
| 100 | scoped_refptr<ExtensionsServiceBackend> backend_; |
| 101 | |
| 102 | // The path to the crx file that we're installing. |
| 103 | FilePath extension_path_; |
| 104 | |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 105 | // The path to the copy of the crx file in the temporary directory where we're |
| 106 | // unpacking it. |
| 107 | FilePath temp_extension_path_; |
| 108 | |
| 109 | // The ID we expect this extension to have, if any. |
| 110 | std::string expected_id_; |
| 111 | |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 112 | // True if the install should be done with no confirmation dialog. |
| 113 | bool silent_; |
[email protected] | c1e432a | 2009-07-22 21:21:48 | [diff] [blame] | 114 | |
| 115 | // True if the install is from the gallery (and therefore should not get an |
| 116 | // alert UI if it turns out to also be a theme). |
| 117 | bool from_gallery_; |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 118 | }; |
| 119 | |
[email protected] | 81e6378 | 2009-02-27 19:35:09 | [diff] [blame] | 120 | ExtensionsService::ExtensionsService(Profile* profile, |
[email protected] | 36a784c | 2009-06-23 06:21:08 | [diff] [blame] | 121 | const CommandLine* command_line, |
[email protected] | a9b00ac | 2009-06-25 21:03:23 | [diff] [blame] | 122 | PrefService* prefs, |
| 123 | const FilePath& install_directory, |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 124 | MessageLoop* frontend_loop, |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 125 | MessageLoop* backend_loop, |
| 126 | bool autoupdate_enabled) |
[email protected] | 6ef635e4 | 2009-07-26 06:16:12 | [diff] [blame] | 127 | : profile_(profile), |
| 128 | extension_prefs_(new ExtensionPrefs(prefs, install_directory)), |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 129 | backend_loop_(backend_loop), |
[email protected] | a9b00ac | 2009-06-25 21:03:23 | [diff] [blame] | 130 | install_directory_(install_directory), |
[email protected] | abe7a894 | 2009-06-23 05:14:29 | [diff] [blame] | 131 | extensions_enabled_(false), |
[email protected] | e81dba3 | 2009-06-19 20:19:13 | [diff] [blame] | 132 | show_extensions_prompts_(true), |
| 133 | ready_(false) { |
[email protected] | 36a784c | 2009-06-23 06:21:08 | [diff] [blame] | 134 | // Figure out if extension installation should be enabled. |
| 135 | if (command_line->HasSwitch(switches::kEnableExtensions)) |
| 136 | extensions_enabled_ = true; |
| 137 | else if (profile->GetPrefs()->GetBoolean(prefs::kEnableExtensions)) |
| 138 | extensions_enabled_ = true; |
| 139 | |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 140 | // Set up the ExtensionUpdater |
| 141 | if (autoupdate_enabled) { |
| 142 | int update_frequency = kDefaultUpdateFrequencySeconds; |
| 143 | if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) { |
| 144 | update_frequency = StringToInt(WideToASCII(command_line->GetSwitchValue( |
| 145 | switches::kExtensionsUpdateFrequency))); |
| 146 | } |
| 147 | updater_ = new ExtensionUpdater(this, update_frequency, backend_loop_); |
| 148 | } |
| 149 | |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 150 | backend_ = new ExtensionsServiceBackend( |
| 151 | install_directory_, g_browser_process->resource_dispatcher_host(), |
[email protected] | 27b985d | 2009-06-25 17:53:15 | [diff] [blame] | 152 | frontend_loop, extensions_enabled()); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | ExtensionsService::~ExtensionsService() { |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 156 | UnloadAllExtensions(); |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 157 | if (updater_.get()) { |
| 158 | updater_->Stop(); |
| 159 | } |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 160 | } |
| 161 | |
[email protected] | abe7a894 | 2009-06-23 05:14:29 | [diff] [blame] | 162 | void ExtensionsService::SetExtensionsEnabled(bool enabled) { |
| 163 | extensions_enabled_ = true; |
| 164 | backend_loop_->PostTask(FROM_HERE, NewRunnableMethod(backend_.get(), |
| 165 | &ExtensionsServiceBackend::set_extensions_enabled, enabled)); |
| 166 | } |
| 167 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 168 | void ExtensionsService::Init() { |
[email protected] | c6e4a341 | 2009-06-24 15:45:29 | [diff] [blame] | 169 | DCHECK(!ready_); |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 170 | DCHECK_EQ(extensions_.size(), 0u); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 171 | |
[email protected] | b68d5ed | 2009-04-16 02:41:28 | [diff] [blame] | 172 | // Start up the extension event routers. |
| 173 | ExtensionBrowserEventRouter::GetInstance()->Init(); |
| 174 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 175 | LoadAllExtensions(); |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 176 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 177 | // TODO(erikkay) this should probably be deferred to a future point |
| 178 | // rather than running immediately at startup. |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 179 | CheckForExternalUpdates(); |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 180 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 181 | // TODO(erikkay) this should probably be deferred as well. |
| 182 | GarbageCollectExtensions(); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 183 | } |
| 184 | |
[email protected] | 3cf4f099 | 2009-02-03 23:00:30 | [diff] [blame] | 185 | void ExtensionsService::InstallExtension(const FilePath& extension_path) { |
[email protected] | 494c06e | 2009-07-25 01:06:42 | [diff] [blame] | 186 | InstallExtension(extension_path, GURL(), GURL()); |
[email protected] | c1e432a | 2009-07-22 21:21:48 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | void ExtensionsService::InstallExtension(const FilePath& extension_path, |
[email protected] | 494c06e | 2009-07-25 01:06:42 | [diff] [blame] | 190 | const GURL& download_url, |
[email protected] | 75a2567 | 2009-07-24 17:41:39 | [diff] [blame] | 191 | const GURL& referrer_url) { |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 192 | backend_loop_->PostTask(FROM_HERE, NewRunnableMethod(backend_.get(), |
[email protected] | 4289d9b | 2009-07-25 21:17:34 | [diff] [blame] | 193 | &ExtensionsServiceBackend::InstallExtension, extension_path, |
| 194 | IsDownloadFromGallery(download_url, referrer_url), |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 195 | scoped_refptr<ExtensionsService>(this))); |
[email protected] | 4289d9b | 2009-07-25 21:17:34 | [diff] [blame] | 196 | |
[email protected] | 3cf4f099 | 2009-02-03 23:00:30 | [diff] [blame] | 197 | } |
| 198 | |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 199 | void ExtensionsService::UpdateExtension(const std::string& id, |
| 200 | const FilePath& extension_path, |
| 201 | bool alert_on_error, |
[email protected] | d1ca0ed1 | 2009-07-01 18:24:32 | [diff] [blame] | 202 | ExtensionInstallCallback* callback) { |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 203 | if (callback) { |
| 204 | if (install_callbacks_.find(extension_path) != install_callbacks_.end()) { |
| 205 | // We can't have multiple outstanding install requests for the same |
| 206 | // path, so immediately indicate error via the callback here. |
| 207 | LOG(WARNING) << "Dropping update request for '" << |
| 208 | extension_path.value() << "' (already in progress)'"; |
| 209 | callback->Run(extension_path, static_cast<Extension*>(NULL)); |
| 210 | delete callback; |
| 211 | return; |
| 212 | } |
[email protected] | d1ca0ed1 | 2009-07-01 18:24:32 | [diff] [blame] | 213 | install_callbacks_[extension_path] = |
| 214 | linked_ptr<ExtensionInstallCallback>(callback); |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | if (!GetExtensionById(id)) { |
| 218 | LOG(WARNING) << "Will not update extension " << id << " because it is not " |
| 219 | << "installed"; |
| 220 | FireInstallCallback(extension_path, NULL); |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | backend_loop_->PostTask(FROM_HERE, NewRunnableMethod(backend_.get(), |
| 225 | &ExtensionsServiceBackend::UpdateExtension, id, extension_path, |
| 226 | alert_on_error, scoped_refptr<ExtensionsService>(this))); |
| 227 | } |
| 228 | |
[email protected] | 9cddd470 | 2009-07-27 22:09:40 | [diff] [blame] | 229 | void ExtensionsService::ReloadExtension(const std::string& extension_id) { |
| 230 | Extension* extension = GetExtensionById(extension_id); |
| 231 | FilePath extension_path = extension->path(); |
| 232 | |
| 233 | UnloadExtension(extension_id); |
| 234 | LoadExtension(extension_path); |
| 235 | } |
| 236 | |
[email protected] | 27b985d | 2009-06-25 17:53:15 | [diff] [blame] | 237 | void ExtensionsService::UninstallExtension(const std::string& extension_id, |
| 238 | bool external_uninstall) { |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 239 | Extension* extension = GetExtensionById(extension_id); |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 240 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 241 | // Callers should not send us nonexistant extensions. |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 242 | DCHECK(extension); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 243 | |
[email protected] | 27b985d | 2009-06-25 17:53:15 | [diff] [blame] | 244 | extension_prefs_->OnExtensionUninstalled(extension, external_uninstall); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 245 | |
| 246 | // Tell the backend to start deleting installed extensions on the file thread. |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 247 | if (Extension::LOAD != extension->location()) { |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame^] | 248 | backend_loop_->PostTask(FROM_HERE, NewRunnableFunction( |
| 249 | &extension_file_util::UninstallExtension, extension_id, |
| 250 | install_directory_)); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | UnloadExtension(extension_id); |
| 254 | } |
| 255 | |
| 256 | void ExtensionsService::LoadExtension(const FilePath& extension_path) { |
| 257 | backend_loop_->PostTask(FROM_HERE, NewRunnableMethod(backend_.get(), |
| 258 | &ExtensionsServiceBackend::LoadSingleExtension, |
| 259 | extension_path, scoped_refptr<ExtensionsService>(this))); |
| 260 | } |
| 261 | |
| 262 | void ExtensionsService::LoadAllExtensions() { |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 263 | // Load the previously installed extensions. |
| 264 | backend_loop_->PostTask(FROM_HERE, NewRunnableMethod(backend_.get(), |
| 265 | &ExtensionsServiceBackend::LoadInstalledExtensions, |
| 266 | scoped_refptr<ExtensionsService>(this), |
| 267 | new InstalledExtensions(extension_prefs_.get()))); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 268 | } |
| 269 | |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 270 | void ExtensionsService::CheckForExternalUpdates() { |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 271 | // This installs or updates externally provided extensions. |
| 272 | std::set<std::string> killed_extensions; |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 273 | extension_prefs_->GetKilledExtensionIds(&killed_extensions); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 274 | backend_loop_->PostTask(FROM_HERE, NewRunnableMethod(backend_.get(), |
| 275 | &ExtensionsServiceBackend::CheckForExternalUpdates, |
| 276 | killed_extensions, |
| 277 | scoped_refptr<ExtensionsService>(this))); |
| 278 | } |
| 279 | |
| 280 | void ExtensionsService::UnloadExtension(const std::string& extension_id) { |
| 281 | Extension* extension = NULL; |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 282 | ExtensionList::iterator iter; |
| 283 | for (iter = extensions_.begin(); iter != extensions_.end(); ++iter) { |
| 284 | if ((*iter)->id() == extension_id) { |
| 285 | extension = *iter; |
| 286 | break; |
| 287 | } |
| 288 | } |
| 289 | |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 290 | // Callers should not send us nonexistant extensions. |
| 291 | CHECK(extension); |
| 292 | |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 293 | // Remove the extension from our list. |
| 294 | extensions_.erase(iter); |
| 295 | |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 296 | // Tell other services the extension is gone. |
| 297 | NotificationService::current()->Notify(NotificationType::EXTENSION_UNLOADED, |
[email protected] | c6e4a341 | 2009-06-24 15:45:29 | [diff] [blame] | 298 | Source<ExtensionsService>(this), |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 299 | Details<Extension>(extension)); |
| 300 | |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 301 | delete extension; |
| 302 | } |
| 303 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 304 | void ExtensionsService::UnloadAllExtensions() { |
| 305 | ExtensionList::iterator iter; |
[email protected] | c6e4a341 | 2009-06-24 15:45:29 | [diff] [blame] | 306 | for (iter = extensions_.begin(); iter != extensions_.end(); ++iter) |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 307 | delete *iter; |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 308 | extensions_.clear(); |
[email protected] | c6e4a341 | 2009-06-24 15:45:29 | [diff] [blame] | 309 | |
| 310 | // TODO(erikkay) should there be a notification for this? We can't use |
| 311 | // EXTENSION_UNLOADED since that implies that the extension has been disabled |
| 312 | // or uninstalled, and UnloadAll is just part of shutdown. |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | void ExtensionsService::ReloadExtensions() { |
| 316 | UnloadAllExtensions(); |
| 317 | LoadAllExtensions(); |
| 318 | } |
| 319 | |
| 320 | void ExtensionsService::GarbageCollectExtensions() { |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame^] | 321 | backend_loop_->PostTask(FROM_HERE, NewRunnableFunction( |
| 322 | &extension_file_util::GarbageCollectExtensions, install_directory_)); |
[email protected] | 3cf4f099 | 2009-02-03 23:00:30 | [diff] [blame] | 323 | } |
| 324 | |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 325 | void ExtensionsService::OnLoadedInstalledExtensions() { |
[email protected] | e81dba3 | 2009-06-19 20:19:13 | [diff] [blame] | 326 | ready_ = true; |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 327 | if (updater_.get()) { |
| 328 | updater_->Start(); |
| 329 | } |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 330 | NotificationService::current()->Notify( |
| 331 | NotificationType::EXTENSIONS_READY, |
| 332 | Source<ExtensionsService>(this), |
| 333 | NotificationService::NoDetails()); |
| 334 | } |
| 335 | |
[email protected] | 4a8d327 | 2009-03-10 19:15:08 | [diff] [blame] | 336 | void ExtensionsService::OnExtensionsLoaded(ExtensionList* new_extensions) { |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 337 | scoped_ptr<ExtensionList> cleanup(new_extensions); |
| 338 | |
[email protected] | 75a2567 | 2009-07-24 17:41:39 | [diff] [blame] | 339 | // Filter out any extensions that shouldn't be loaded. If extensions are |
| 340 | // enabled, load everything. Otherwise load only: |
| 341 | // - themes |
| 342 | // - --load-extension |
| 343 | // - externally installed extensions |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 344 | ExtensionList enabled_extensions; |
[email protected] | 86a27407 | 2009-06-11 02:06:45 | [diff] [blame] | 345 | for (ExtensionList::iterator iter = new_extensions->begin(); |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 346 | iter != new_extensions->end(); ++iter) { |
[email protected] | 75a2567 | 2009-07-24 17:41:39 | [diff] [blame] | 347 | if (extensions_enabled() || |
| 348 | (*iter)->IsTheme() || |
[email protected] | 919ddc8 | 2009-07-15 04:30:12 | [diff] [blame] | 349 | (*iter)->location() == Extension::LOAD || |
[email protected] | 75a2567 | 2009-07-24 17:41:39 | [diff] [blame] | 350 | Extension::IsExternalLocation((*iter)->location())) { |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 351 | Extension* old = GetExtensionById((*iter)->id()); |
| 352 | if (old) { |
| 353 | if ((*iter)->version()->CompareTo(*(old->version())) > 0) { |
| 354 | // To upgrade an extension in place, unload the old one and |
| 355 | // then load the new one. |
| 356 | // TODO(erikkay) issue 12399 |
| 357 | UnloadExtension(old->id()); |
| 358 | } else { |
| 359 | // We already have the extension of the same or older version. |
| 360 | LOG(WARNING) << "Duplicate extension load attempt: " << (*iter)->id(); |
| 361 | delete *iter; |
| 362 | continue; |
| 363 | } |
| 364 | } |
[email protected] | 86a27407 | 2009-06-11 02:06:45 | [diff] [blame] | 365 | enabled_extensions.push_back(*iter); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 366 | extensions_.push_back(*iter); |
[email protected] | ba74f35 | 2009-06-11 18:54:45 | [diff] [blame] | 367 | } else { |
| 368 | // Extensions that get enabled get added to extensions_ and deleted later. |
| 369 | // Anything skipped must be deleted now so we don't leak. |
| 370 | delete *iter; |
| 371 | } |
[email protected] | 86a27407 | 2009-06-11 02:06:45 | [diff] [blame] | 372 | } |
| 373 | |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 374 | if (enabled_extensions.size()) { |
| 375 | NotificationService::current()->Notify( |
| 376 | NotificationType::EXTENSIONS_LOADED, |
[email protected] | c6e4a341 | 2009-06-24 15:45:29 | [diff] [blame] | 377 | Source<ExtensionsService>(this), |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 378 | Details<ExtensionList>(&enabled_extensions)); |
[email protected] | 811f343 | 2009-07-25 19:38:21 | [diff] [blame] | 379 | for (ExtensionList::iterator iter = enabled_extensions.begin(); |
| 380 | iter != enabled_extensions.end(); ++iter) { |
| 381 | if ((*iter)->IsTheme() && (*iter)->location() == Extension::LOAD) { |
| 382 | NotificationService::current()->Notify( |
| 383 | NotificationType::THEME_INSTALLED, |
| 384 | Source<ExtensionsService>(this), |
| 385 | Details<Extension>(*iter)); |
| 386 | } |
| 387 | } |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 388 | } |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 389 | } |
| 390 | |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 391 | void ExtensionsService::OnExtensionInstalled(const FilePath& path, |
| 392 | Extension* extension, Extension::InstallType install_type) { |
| 393 | FireInstallCallback(path, extension); |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 394 | extension_prefs_->OnExtensionInstalled(extension); |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 395 | |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 396 | // If the extension is a theme, tell the profile (and therefore ThemeProvider) |
| 397 | // to apply it. |
| 398 | if (extension->IsTheme()) { |
[email protected] | 6ef635e4 | 2009-07-26 06:16:12 | [diff] [blame] | 399 | ShowThemePreviewInfobar(extension); |
[email protected] | 9ceb0734 | 2009-07-26 04:09:23 | [diff] [blame] | 400 | NotificationService::current()->Notify( |
| 401 | NotificationType::THEME_INSTALLED, |
| 402 | Source<ExtensionsService>(this), |
| 403 | Details<Extension>(extension)); |
[email protected] | 9197f3b | 2009-06-02 00:49:27 | [diff] [blame] | 404 | } else { |
| 405 | NotificationService::current()->Notify( |
| 406 | NotificationType::EXTENSION_INSTALLED, |
[email protected] | c6e4a341 | 2009-06-24 15:45:29 | [diff] [blame] | 407 | Source<ExtensionsService>(this), |
[email protected] | 9197f3b | 2009-06-02 00:49:27 | [diff] [blame] | 408 | Details<Extension>(extension)); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 412 | |
| 413 | void ExtensionsService::OnExtenionInstallError(const FilePath& path) { |
| 414 | FireInstallCallback(path, NULL); |
| 415 | } |
| 416 | |
| 417 | void ExtensionsService::FireInstallCallback(const FilePath& path, |
| 418 | Extension* extension) { |
| 419 | CallbackMap::iterator iter = install_callbacks_.find(path); |
| 420 | if (iter != install_callbacks_.end()) { |
| 421 | iter->second->Run(path, extension); |
| 422 | install_callbacks_.erase(iter); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | void ExtensionsService::OnExtensionOverinstallAttempted(const std::string& id, |
| 427 | const FilePath& path) { |
| 428 | FireInstallCallback(path, NULL); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 429 | Extension* extension = GetExtensionById(id); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 430 | if (extension && extension->IsTheme()) { |
[email protected] | 6ef635e4 | 2009-07-26 06:16:12 | [diff] [blame] | 431 | ShowThemePreviewInfobar(extension); |
[email protected] | 9ceb0734 | 2009-07-26 04:09:23 | [diff] [blame] | 432 | NotificationService::current()->Notify( |
| 433 | NotificationType::THEME_INSTALLED, |
| 434 | Source<ExtensionsService>(this), |
| 435 | Details<Extension>(extension)); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 436 | } |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 437 | } |
| 438 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 439 | Extension* ExtensionsService::GetExtensionById(const std::string& id) { |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 440 | std::string lowercase_id = StringToLowerASCII(id); |
[email protected] | ce5c450 | 2009-05-06 16:46:11 | [diff] [blame] | 441 | for (ExtensionList::const_iterator iter = extensions_.begin(); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 442 | iter != extensions_.end(); ++iter) { |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 443 | if ((*iter)->id() == lowercase_id) |
[email protected] | ce5c450 | 2009-05-06 16:46:11 | [diff] [blame] | 444 | return *iter; |
| 445 | } |
| 446 | return NULL; |
| 447 | } |
| 448 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 449 | Extension* ExtensionsService::GetExtensionByURL(const GURL& url) { |
| 450 | std::string host = url.host(); |
| 451 | return GetExtensionById(host); |
| 452 | } |
| 453 | |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 454 | void ExtensionsService::ClearProvidersForTesting() { |
| 455 | backend_loop_->PostTask(FROM_HERE, NewRunnableMethod(backend_.get(), |
| 456 | &ExtensionsServiceBackend::ClearProvidersForTesting)); |
| 457 | } |
| 458 | |
| 459 | void ExtensionsService::SetProviderForTesting( |
| 460 | Extension::Location location, ExternalExtensionProvider* test_provider) { |
| 461 | backend_loop_->PostTask(FROM_HERE, NewRunnableMethod(backend_.get(), |
| 462 | &ExtensionsServiceBackend::SetProviderForTesting, |
| 463 | location, test_provider)); |
| 464 | } |
| 465 | |
[email protected] | 6ef635e4 | 2009-07-26 06:16:12 | [diff] [blame] | 466 | bool ExtensionsService::ShowThemePreviewInfobar(Extension* extension) { |
| 467 | if (!profile_) |
| 468 | return false; |
| 469 | |
| 470 | Browser* browser = BrowserList::GetLastActiveWithProfile(profile_); |
| 471 | if (!browser) |
| 472 | return false; |
| 473 | |
| 474 | TabContents* tab_contents = browser->GetSelectedTabContents(); |
| 475 | if (!tab_contents) |
| 476 | return false; |
| 477 | |
| 478 | tab_contents->AddInfoBar(new ThemePreviewInfobarDelegate(tab_contents, |
| 479 | extension->name())); |
| 480 | return true; |
| 481 | } |
| 482 | |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 483 | // ExtensionsServicesBackend |
| 484 | |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 485 | ExtensionsServiceBackend::ExtensionsServiceBackend( |
| 486 | const FilePath& install_directory, ResourceDispatcherHost* rdh, |
[email protected] | 27b985d | 2009-06-25 17:53:15 | [diff] [blame] | 487 | MessageLoop* frontend_loop, bool extensions_enabled) |
[email protected] | 0c7bc4b | 2009-05-30 01:47:08 | [diff] [blame] | 488 | : frontend_(NULL), |
| 489 | install_directory_(install_directory), |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 490 | resource_dispatcher_host_(rdh), |
[email protected] | 0c7bc4b | 2009-05-30 01:47:08 | [diff] [blame] | 491 | alert_on_error_(false), |
[email protected] | abe7a894 | 2009-06-23 05:14:29 | [diff] [blame] | 492 | frontend_loop_(frontend_loop), |
[email protected] | 36a784c | 2009-06-23 06:21:08 | [diff] [blame] | 493 | extensions_enabled_(extensions_enabled) { |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 494 | external_extension_providers_[Extension::EXTERNAL_PREF] = |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 495 | linked_ptr<ExternalExtensionProvider>( |
[email protected] | 27b985d | 2009-06-25 17:53:15 | [diff] [blame] | 496 | new ExternalPrefExtensionProvider()); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 497 | #if defined(OS_WIN) |
| 498 | external_extension_providers_[Extension::EXTERNAL_REGISTRY] = |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 499 | linked_ptr<ExternalExtensionProvider>( |
| 500 | new ExternalRegistryExtensionProvider()); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 501 | #endif |
| 502 | } |
| 503 | |
| 504 | ExtensionsServiceBackend::~ExtensionsServiceBackend() { |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 505 | } |
| 506 | |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 507 | void ExtensionsServiceBackend::LoadInstalledExtensions( |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 508 | scoped_refptr<ExtensionsService> frontend, |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 509 | InstalledExtensions* installed) { |
| 510 | scoped_ptr<InstalledExtensions> cleanup(installed); |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 511 | frontend_ = frontend; |
| 512 | alert_on_error_ = false; |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 513 | |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 514 | // Call LoadInstalledExtension for each extension |installed| knows about. |
| 515 | scoped_ptr<InstalledExtensions::Callback> callback( |
| 516 | NewCallback(this, &ExtensionsServiceBackend::LoadInstalledExtension)); |
| 517 | installed->VisitInstalledExtensions(callback.get()); |
| 518 | |
| 519 | frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod( |
| 520 | frontend_, &ExtensionsService::OnLoadedInstalledExtensions)); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 521 | } |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 522 | |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 523 | void ExtensionsServiceBackend::LoadSingleExtension( |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 524 | const FilePath& path_in, scoped_refptr<ExtensionsService> frontend) { |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 525 | frontend_ = frontend; |
| 526 | |
| 527 | // Explicit UI loads are always noisy. |
| 528 | alert_on_error_ = true; |
| 529 | |
[email protected] | cc5da33 | 2009-03-04 08:02:51 | [diff] [blame] | 530 | FilePath extension_path = path_in; |
[email protected] | f36fa4fb | 2009-06-19 18:23:50 | [diff] [blame] | 531 | file_util::AbsolutePath(&extension_path); |
[email protected] | bf24d2c | 2009-02-24 23:07:45 | [diff] [blame] | 532 | |
| 533 | LOG(INFO) << "Loading single extension from " << |
[email protected] | cc5da33 | 2009-03-04 08:02:51 | [diff] [blame] | 534 | WideToASCII(extension_path.BaseName().ToWStringHack()); |
[email protected] | bf24d2c | 2009-02-24 23:07:45 | [diff] [blame] | 535 | |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame^] | 536 | std::string error; |
| 537 | Extension* extension = extension_file_util::LoadExtension( |
| 538 | extension_path, |
| 539 | false, // Don't require id |
| 540 | &error); |
| 541 | |
| 542 | if (!extension) { |
| 543 | ReportExtensionLoadError(extension_path, error); |
| 544 | return; |
[email protected] | 0877fd9 | 2009-02-03 16:34:06 | [diff] [blame] | 545 | } |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame^] | 546 | |
| 547 | extension->set_location(Extension::LOAD); |
| 548 | ExtensionList* extensions = new ExtensionList; |
| 549 | extensions->push_back(extension); |
| 550 | ReportExtensionsLoaded(extensions); |
[email protected] | 0877fd9 | 2009-02-03 16:34:06 | [diff] [blame] | 551 | } |
| 552 | |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 553 | void ExtensionsServiceBackend::LoadInstalledExtension( |
| 554 | const std::string& id, const FilePath& path, Extension::Location location) { |
| 555 | if (CheckExternalUninstall(id, location)) { |
[email protected] | 27b985d | 2009-06-25 17:53:15 | [diff] [blame] | 556 | frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod( |
| 557 | frontend_, |
| 558 | &ExtensionsService::UninstallExtension, |
| 559 | id, true)); |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 560 | |
| 561 | // No error needs to be reported. The extension effectively doesn't exist. |
| 562 | return; |
| 563 | } |
| 564 | |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame^] | 565 | std::string error; |
| 566 | Extension* extension = extension_file_util::LoadExtension( |
| 567 | path, |
| 568 | true, // Require id |
| 569 | &error); |
| 570 | |
| 571 | if (!extension) { |
| 572 | ReportExtensionLoadError(path, error); |
| 573 | return; |
| 574 | } |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 575 | |
| 576 | // TODO(erikkay) now we only report a single extension loaded at a time. |
| 577 | // Perhaps we should change the notifications to remove ExtensionList. |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame^] | 578 | extension->set_location(location); |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 579 | ExtensionList* extensions = new ExtensionList; |
| 580 | if (extension) |
| 581 | extensions->push_back(extension); |
| 582 | ReportExtensionsLoaded(extensions); |
| 583 | } |
| 584 | |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 585 | void ExtensionsServiceBackend::ReportExtensionLoadError( |
[email protected] | cc5da33 | 2009-03-04 08:02:51 | [diff] [blame] | 586 | const FilePath& extension_path, const std::string &error) { |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 587 | // TODO(port): note that this isn't guaranteed to work properly on Linux. |
[email protected] | cc5da33 | 2009-03-04 08:02:51 | [diff] [blame] | 588 | std::string path_str = WideToASCII(extension_path.ToWStringHack()); |
[email protected] | 3acbd42 | 2008-12-08 18:25:00 | [diff] [blame] | 589 | std::string message = StringPrintf("Could not load extension from '%s'. %s", |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 590 | path_str.c_str(), error.c_str()); |
[email protected] | bb28e06 | 2009-02-27 17:19:18 | [diff] [blame] | 591 | ExtensionErrorReporter::GetInstance()->ReportError(message, alert_on_error_); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | void ExtensionsServiceBackend::ReportExtensionsLoaded( |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 595 | ExtensionList* extensions) { |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 596 | frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod( |
| 597 | frontend_, &ExtensionsService::OnExtensionsLoaded, extensions)); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 598 | } |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 599 | |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 600 | void ExtensionsServiceBackend::InstallExtension( |
[email protected] | c1e432a | 2009-07-22 21:21:48 | [diff] [blame] | 601 | const FilePath& extension_path, bool from_gallery, |
| 602 | scoped_refptr<ExtensionsService> frontend) { |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 603 | LOG(INFO) << "Installing extension " << extension_path.value(); |
| 604 | |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 605 | frontend_ = frontend; |
[email protected] | 0e34d789 | 2009-06-05 19:17:40 | [diff] [blame] | 606 | alert_on_error_ = true; |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 607 | |
[email protected] | c1e432a | 2009-07-22 21:21:48 | [diff] [blame] | 608 | InstallOrUpdateExtension(extension_path, from_gallery, std::string(), false); |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 609 | } |
| 610 | |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 611 | void ExtensionsServiceBackend::UpdateExtension(const std::string& id, |
| 612 | const FilePath& extension_path, bool alert_on_error, |
| 613 | scoped_refptr<ExtensionsService> frontend) { |
| 614 | LOG(INFO) << "Updating extension " << id << " " << extension_path.value(); |
| 615 | |
| 616 | frontend_ = frontend; |
| 617 | alert_on_error_ = alert_on_error; |
| 618 | |
[email protected] | c1e432a | 2009-07-22 21:21:48 | [diff] [blame] | 619 | InstallOrUpdateExtension(extension_path, false, id, true); |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 620 | } |
| 621 | |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 622 | void ExtensionsServiceBackend::InstallOrUpdateExtension( |
[email protected] | c1e432a | 2009-07-22 21:21:48 | [diff] [blame] | 623 | const FilePath& extension_path, bool from_gallery, |
| 624 | const std::string& expected_id, bool silent) { |
[email protected] | af1277b | 2009-07-28 00:47:53 | [diff] [blame] | 625 | // NOTE: We don't need to keep a reference to this, it deletes itself when it |
| 626 | // is done. |
| 627 | new UnpackerClient(this, extension_path, expected_id, silent, from_gallery); |
[email protected] | fbcc4030 | 2009-06-12 20:45:45 | [diff] [blame] | 628 | } |
| 629 | |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 630 | void ExtensionsServiceBackend::OnExtensionUnpacked( |
[email protected] | af1277b | 2009-07-28 00:47:53 | [diff] [blame] | 631 | const FilePath& crx_path, const FilePath& unpacked_path, |
| 632 | Extension* extension, const std::string expected_id, bool silent, |
| 633 | bool from_gallery) { |
| 634 | // Take ownership of the extension object. |
| 635 | scoped_ptr<Extension> extension_deleter(extension); |
[email protected] | 0b34496 | 2009-03-31 04:21:45 | [diff] [blame] | 636 | |
[email protected] | abe7a894 | 2009-06-23 05:14:29 | [diff] [blame] | 637 | Extension::Location location = Extension::INTERNAL; |
[email protected] | af1277b | 2009-07-28 00:47:53 | [diff] [blame] | 638 | LookupExternalExtension(extension->id(), NULL, &location); |
| 639 | extension->set_location(location); |
[email protected] | abe7a894 | 2009-06-23 05:14:29 | [diff] [blame] | 640 | |
[email protected] | 75a2567 | 2009-07-24 17:41:39 | [diff] [blame] | 641 | bool allow_install = false; |
| 642 | if (extensions_enabled_) |
| 643 | allow_install = true; |
| 644 | |
[email protected] | 6ef635e4 | 2009-07-26 06:16:12 | [diff] [blame] | 645 | // Always allow themes. |
[email protected] | af1277b | 2009-07-28 00:47:53 | [diff] [blame] | 646 | if (extension->IsTheme()) |
[email protected] | 75a2567 | 2009-07-24 17:41:39 | [diff] [blame] | 647 | allow_install = true; |
| 648 | |
[email protected] | 6ef635e4 | 2009-07-26 06:16:12 | [diff] [blame] | 649 | // Always allow externally installed extensions (partners use this). |
[email protected] | 75a2567 | 2009-07-24 17:41:39 | [diff] [blame] | 650 | if (Extension::IsExternalLocation(location)) |
| 651 | allow_install = true; |
| 652 | |
| 653 | if (!allow_install) { |
[email protected] | af1277b | 2009-07-28 00:47:53 | [diff] [blame] | 654 | ReportExtensionInstallError(crx_path, "Extensions are not enabled."); |
[email protected] | e2eb4311 | 2009-05-29 21:19:54 | [diff] [blame] | 655 | return; |
| 656 | } |
| 657 | |
[email protected] | 638d352 | 2009-06-25 14:52:01 | [diff] [blame] | 658 | // TODO(extensions): Make better extensions UI. http://crbug.com/12116 |
| 659 | |
[email protected] | 75a2567 | 2009-07-24 17:41:39 | [diff] [blame] | 660 | // We also skip the dialog for a few special cases: |
[email protected] | 6ef635e4 | 2009-07-26 06:16:12 | [diff] [blame] | 661 | // - themes (because we show the preview infobar for them) |
[email protected] | c1e432a | 2009-07-22 21:21:48 | [diff] [blame] | 662 | // - externally registered extensions |
[email protected] | 75a2567 | 2009-07-24 17:41:39 | [diff] [blame] | 663 | // - during tests (!frontend->show_extension_prompts()) |
| 664 | // - autoupdate (silent). |
[email protected] | c1e432a | 2009-07-22 21:21:48 | [diff] [blame] | 665 | bool show_dialog = true; |
[email protected] | af1277b | 2009-07-28 00:47:53 | [diff] [blame] | 666 | if (extension->IsTheme()) |
[email protected] | c1e432a | 2009-07-22 21:21:48 | [diff] [blame] | 667 | show_dialog = false; |
| 668 | |
| 669 | if (Extension::IsExternalLocation(location)) |
| 670 | show_dialog = false; |
| 671 | |
| 672 | if (silent || !frontend_->show_extensions_prompts()) |
| 673 | show_dialog = false; |
| 674 | |
| 675 | if (show_dialog) { |
[email protected] | 638d352 | 2009-06-25 14:52:01 | [diff] [blame] | 676 | #if defined(OS_WIN) |
[email protected] | 6987f24 | 2009-07-23 08:00:35 | [diff] [blame] | 677 | if (win_util::MessageBox(GetForegroundWindow(), |
[email protected] | 638d352 | 2009-06-25 14:52:01 | [diff] [blame] | 678 | L"Are you sure you want to install this extension?\n\n" |
[email protected] | 75a2567 | 2009-07-24 17:41:39 | [diff] [blame] | 679 | L"You should only install extensions from sources you trust.", |
[email protected] | 638d352 | 2009-06-25 14:52:01 | [diff] [blame] | 680 | l10n_util::GetString(IDS_PRODUCT_NAME).c_str(), |
| 681 | MB_OKCANCEL) != IDOK) { |
[email protected] | 638d352 | 2009-06-25 14:52:01 | [diff] [blame] | 682 | return; |
| 683 | } |
| 684 | #elif defined(OS_MACOSX) |
| 685 | // Using CoreFoundation to do this dialog is unimaginably lame but will do |
| 686 | // until the UI is redone. |
| 687 | scoped_cftyperef<CFStringRef> product_name( |
| 688 | base::SysWideToCFStringRef(l10n_util::GetString(IDS_PRODUCT_NAME))); |
| 689 | CFOptionFlags response; |
| 690 | CFUserNotificationDisplayAlert( |
| 691 | 0, kCFUserNotificationCautionAlertLevel, NULL, NULL, NULL, |
| 692 | product_name, |
| 693 | CFSTR("Are you sure you want to install this extension?\n\n" |
| 694 | "This is a temporary message and it will be removed when " |
| 695 | "extensions UI is finalized."), |
| 696 | NULL, CFSTR("Cancel"), NULL, &response); |
| 697 | |
| 698 | if (response == kCFUserNotificationAlternateResponse) { |
[email protected] | af1277b | 2009-07-28 00:47:53 | [diff] [blame] | 699 | ReportExtensionInstallError(crx_path, |
[email protected] | 638d352 | 2009-06-25 14:52:01 | [diff] [blame] | 700 | "User did not allow extension to be installed."); |
| 701 | return; |
| 702 | } |
| 703 | #endif // OS_* |
[email protected] | 0e34d789 | 2009-06-05 19:17:40 | [diff] [blame] | 704 | } |
[email protected] | 0e34d789 | 2009-06-05 19:17:40 | [diff] [blame] | 705 | |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 706 | // If an expected id was provided, make sure it matches. |
[email protected] | af1277b | 2009-07-28 00:47:53 | [diff] [blame] | 707 | if (!expected_id.empty() && expected_id != extension->id()) { |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame^] | 708 | ReportExtensionInstallError(crx_path, |
| 709 | StringPrintf("ID in new extension manifest (%s) does not match " |
| 710 | "expected id (%s)", extension->id().c_str(), |
| 711 | expected_id.c_str())); |
| 712 | return; |
| 713 | } |
| 714 | |
| 715 | FilePath version_dir; |
| 716 | Extension::InstallType install_type = Extension::INSTALL_ERROR; |
| 717 | std::string error_msg; |
| 718 | if (!extension_file_util::InstallExtension(unpacked_path, install_directory_, |
| 719 | extension->id(), |
| 720 | extension->VersionString(), |
| 721 | &version_dir, |
| 722 | &install_type, &error_msg)) { |
[email protected] | af1277b | 2009-07-28 00:47:53 | [diff] [blame] | 723 | ReportExtensionInstallError(crx_path, error_msg); |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 724 | return; |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 725 | } |
| 726 | |
[email protected] | fbcc4030 | 2009-06-12 20:45:45 | [diff] [blame] | 727 | if (install_type == Extension::DOWNGRADE) { |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame^] | 728 | ReportExtensionInstallError(crx_path, "Attempted to downgrade extension."); |
[email protected] | fbcc4030 | 2009-06-12 20:45:45 | [diff] [blame] | 729 | return; |
| 730 | } |
| 731 | |
[email protected] | af1277b | 2009-07-28 00:47:53 | [diff] [blame] | 732 | extension->set_path(version_dir); |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 733 | |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame^] | 734 | if (install_type == Extension::REINSTALL) { |
| 735 | // The client may use this as a signal (to switch themes, for instance). |
| 736 | ReportExtensionOverinstallAttempted(extension->id(), crx_path); |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 737 | return; |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame^] | 738 | } |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 739 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 740 | frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod( |
[email protected] | af1277b | 2009-07-28 00:47:53 | [diff] [blame] | 741 | frontend_, &ExtensionsService::OnExtensionInstalled, crx_path, |
| 742 | extension, install_type)); |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 743 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 744 | // Only one extension, but ReportExtensionsLoaded can handle multiple, |
| 745 | // so we need to construct a list. |
[email protected] | af1277b | 2009-07-28 00:47:53 | [diff] [blame] | 746 | ExtensionList* extensions = new ExtensionList; |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 747 | |
[email protected] | af1277b | 2009-07-28 00:47:53 | [diff] [blame] | 748 | // Hand off ownership of the extension to the frontend. |
| 749 | extensions->push_back(extension_deleter.release()); |
| 750 | ReportExtensionsLoaded(extensions); |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | void ExtensionsServiceBackend::ReportExtensionInstallError( |
[email protected] | cc5da33 | 2009-03-04 08:02:51 | [diff] [blame] | 754 | const FilePath& extension_path, const std::string &error) { |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 755 | |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 756 | // TODO(erikkay): note that this isn't guaranteed to work properly on Linux. |
[email protected] | cc5da33 | 2009-03-04 08:02:51 | [diff] [blame] | 757 | std::string path_str = WideToASCII(extension_path.ToWStringHack()); |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 758 | std::string message = |
| 759 | StringPrintf("Could not install extension from '%s'. %s", |
| 760 | path_str.c_str(), error.c_str()); |
[email protected] | bb28e06 | 2009-02-27 17:19:18 | [diff] [blame] | 761 | ExtensionErrorReporter::GetInstance()->ReportError(message, alert_on_error_); |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 762 | |
| 763 | frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod( |
| 764 | frontend_, |
| 765 | &ExtensionsService::OnExtenionInstallError, |
| 766 | extension_path)); |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 767 | } |
| 768 | |
[email protected] | fbcc4030 | 2009-06-12 20:45:45 | [diff] [blame] | 769 | void ExtensionsServiceBackend::ReportExtensionOverinstallAttempted( |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 770 | const std::string& id, const FilePath& path) { |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 771 | frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod( |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 772 | frontend_, &ExtensionsService::OnExtensionOverinstallAttempted, id, |
| 773 | path)); |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 774 | } |
| 775 | |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 776 | bool ExtensionsServiceBackend::LookupExternalExtension( |
| 777 | const std::string& id, Version** version, Extension::Location* location) { |
| 778 | scoped_ptr<Version> extension_version; |
| 779 | for (ProviderMap::const_iterator i = external_extension_providers_.begin(); |
| 780 | i != external_extension_providers_.end(); ++i) { |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 781 | const ExternalExtensionProvider* provider = i->second.get(); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 782 | extension_version.reset(provider->RegisteredVersion(id, location)); |
| 783 | if (extension_version.get()) { |
| 784 | if (version) |
| 785 | *version = extension_version.release(); |
| 786 | return true; |
| 787 | } |
| 788 | } |
| 789 | return false; |
| 790 | } |
| 791 | |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 792 | // Some extensions will autoupdate themselves externally from Chrome. These |
| 793 | // are typically part of some larger client application package. To support |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 794 | // these, the extension will register its location in the the preferences file |
| 795 | // (and also, on Windows, in the registry) and this code will periodically |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 796 | // check that location for a .crx file, which it will then install locally if |
| 797 | // a new version is available. |
| 798 | void ExtensionsServiceBackend::CheckForExternalUpdates( |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 799 | std::set<std::string> ids_to_ignore, |
| 800 | scoped_refptr<ExtensionsService> frontend) { |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 801 | // Note that this installation is intentionally silent (since it didn't |
| 802 | // go through the front-end). Extensions that are registered in this |
| 803 | // way are effectively considered 'pre-bundled', and so implicitly |
| 804 | // trusted. In general, if something has HKLM or filesystem access, |
| 805 | // they could install an extension manually themselves anyway. |
| 806 | alert_on_error_ = false; |
| 807 | frontend_ = frontend; |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 808 | |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 809 | // Ask each external extension provider to give us a call back for each |
| 810 | // extension they know about. See OnExternalExtensionFound. |
| 811 | for (ProviderMap::const_iterator i = external_extension_providers_.begin(); |
| 812 | i != external_extension_providers_.end(); ++i) { |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 813 | ExternalExtensionProvider* provider = i->second.get(); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 814 | provider->VisitRegisteredExtension(this, ids_to_ignore); |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 815 | } |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 816 | } |
| 817 | |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 818 | bool ExtensionsServiceBackend::CheckExternalUninstall( |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 819 | const std::string& id, Extension::Location location) { |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 820 | // Check if the providers know about this extension. |
| 821 | ProviderMap::const_iterator i = external_extension_providers_.find(location); |
| 822 | if (i != external_extension_providers_.end()) { |
| 823 | scoped_ptr<Version> version; |
| 824 | version.reset(i->second->RegisteredVersion(id, NULL)); |
| 825 | if (version.get()) |
| 826 | return false; // Yup, known extension, don't uninstall. |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 827 | } else { |
| 828 | // Not from an external provider, so it's fine. |
| 829 | return false; |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 830 | } |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 831 | |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 832 | return true; // This is not a known extension, uninstall. |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 833 | } |
| 834 | |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 835 | void ExtensionsServiceBackend::ClearProvidersForTesting() { |
| 836 | external_extension_providers_.clear(); |
| 837 | } |
| 838 | |
| 839 | void ExtensionsServiceBackend::SetProviderForTesting( |
| 840 | Extension::Location location, |
| 841 | ExternalExtensionProvider* test_provider) { |
| 842 | DCHECK(test_provider); |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 843 | external_extension_providers_[location] = |
| 844 | linked_ptr<ExternalExtensionProvider>(test_provider); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | void ExtensionsServiceBackend::OnExternalExtensionFound( |
| 848 | const std::string& id, const Version* version, const FilePath& path) { |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame^] | 849 | InstallOrUpdateExtension(path, |
| 850 | false, // not from gallery |
| 851 | id, // expected id |
| 852 | true); // silent |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 853 | } |