| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/webstore_installer.h" | 5 #include "chrome/browser/extensions/webstore_installer.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 content::Source<Profile>(profile->GetOriginalProfile())); | 194 content::Source<Profile>(profile->GetOriginalProfile())); |
| 195 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR, | 195 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR, |
| 196 content::Source<CrxInstaller>(NULL)); | 196 content::Source<CrxInstaller>(NULL)); |
| 197 } | 197 } |
| 198 | 198 |
| 199 void WebstoreInstaller::Start() { | 199 void WebstoreInstaller::Start() { |
| 200 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 200 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 201 AddRef(); // Balanced in ReportSuccess and ReportFailure. | 201 AddRef(); // Balanced in ReportSuccess and ReportFailure. |
| 202 | 202 |
| 203 if (!Extension::IdIsValid(id_)) { | 203 if (!Extension::IdIsValid(id_)) { |
| 204 ReportFailure(kInvalidIdError); | 204 ReportFailure(kInvalidIdError, FAILURE_REASON_OTHER); |
| 205 return; | 205 return; |
| 206 } | 206 } |
| 207 | 207 |
| 208 FilePath download_path = DownloadPrefs::FromDownloadManager( | 208 FilePath download_path = DownloadPrefs::FromDownloadManager( |
| 209 BrowserContext::GetDownloadManager(profile_))->DownloadPath(); | 209 BrowserContext::GetDownloadManager(profile_))->DownloadPath(); |
| 210 BrowserThread::PostTask( | 210 BrowserThread::PostTask( |
| 211 BrowserThread::FILE, FROM_HERE, | 211 BrowserThread::FILE, FROM_HERE, |
| 212 base::Bind(&GetDownloadFilePath, download_path, id_, | 212 base::Bind(&GetDownloadFilePath, download_path, id_, |
| 213 base::Bind(&WebstoreInstaller::StartDownload, this))); | 213 base::Bind(&WebstoreInstaller::StartDownload, this))); |
| 214 } | 214 } |
| 215 | 215 |
| 216 void WebstoreInstaller::Observe(int type, | 216 void WebstoreInstaller::Observe(int type, |
| 217 const content::NotificationSource& source, | 217 const content::NotificationSource& source, |
| 218 const content::NotificationDetails& details) { | 218 const content::NotificationDetails& details) { |
| 219 switch (type) { | 219 switch (type) { |
| 220 case chrome::NOTIFICATION_CRX_INSTALLER_DONE: { | 220 case chrome::NOTIFICATION_CRX_INSTALLER_DONE: { |
| 221 const Extension* extension = | 221 const Extension* extension = |
| 222 content::Details<const Extension>(details).ptr(); | 222 content::Details<const Extension>(details).ptr(); |
| 223 CrxInstaller* installer = content::Source<CrxInstaller>(source).ptr(); | 223 CrxInstaller* installer = content::Source<CrxInstaller>(source).ptr(); |
| 224 if (extension == NULL && download_item_ != NULL && | 224 if (extension == NULL && download_item_ != NULL && |
| 225 installer->download_url() == download_item_->GetURL() && | 225 installer->download_url() == download_item_->GetURL() && |
| 226 installer->profile()->IsSameProfile(profile_)) { | 226 installer->profile()->IsSameProfile(profile_)) { |
| 227 ReportFailure(kInstallCanceledError); | 227 ReportFailure(kInstallCanceledError, FAILURE_REASON_CANCELLED); |
| 228 } | 228 } |
| 229 break; | 229 break; |
| 230 } | 230 } |
| 231 | 231 |
| 232 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { | 232 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { |
| 233 CHECK(profile_->IsSameProfile(content::Source<Profile>(source).ptr())); | 233 CHECK(profile_->IsSameProfile(content::Source<Profile>(source).ptr())); |
| 234 const Extension* extension = | 234 const Extension* extension = |
| 235 content::Details<const Extension>(details).ptr(); | 235 content::Details<const Extension>(details).ptr(); |
| 236 if (id_ == extension->id()) | 236 if (id_ == extension->id()) |
| 237 ReportSuccess(); | 237 ReportSuccess(); |
| 238 break; | 238 break; |
| 239 } | 239 } |
| 240 | 240 |
| 241 case chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR: { | 241 case chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR: { |
| 242 CrxInstaller* crx_installer = content::Source<CrxInstaller>(source).ptr(); | 242 CrxInstaller* crx_installer = content::Source<CrxInstaller>(source).ptr(); |
| 243 CHECK(crx_installer); | 243 CHECK(crx_installer); |
| 244 if (!profile_->IsSameProfile(crx_installer->profile())) | 244 if (!profile_->IsSameProfile(crx_installer->profile())) |
| 245 return; | 245 return; |
| 246 | 246 |
| 247 // TODO(rdevlin.cronin): Continue removing std::string errors and | 247 // TODO(rdevlin.cronin): Continue removing std::string errors and |
| 248 // replacing with string16 | 248 // replacing with string16 |
| 249 const string16* error = content::Details<const string16>(details).ptr(); | 249 const string16* error = content::Details<const string16>(details).ptr(); |
| 250 const std::string utf8_error = UTF16ToUTF8(*error); | 250 const std::string utf8_error = UTF16ToUTF8(*error); |
| 251 if (download_url_ == crx_installer->original_download_url()) | 251 if (download_url_ == crx_installer->original_download_url()) |
| 252 ReportFailure(utf8_error); | 252 ReportFailure(utf8_error, FAILURE_REASON_OTHER); |
| 253 break; | 253 break; |
| 254 } | 254 } |
| 255 | 255 |
| 256 default: | 256 default: |
| 257 NOTREACHED(); | 257 NOTREACHED(); |
| 258 } | 258 } |
| 259 } | 259 } |
| 260 | 260 |
| 261 void WebstoreInstaller::SetDownloadDirectoryForTests(FilePath* directory) { | 261 void WebstoreInstaller::SetDownloadDirectoryForTests(FilePath* directory) { |
| 262 g_download_directory_for_tests = directory; | 262 g_download_directory_for_tests = directory; |
| 263 } | 263 } |
| 264 | 264 |
| 265 WebstoreInstaller::~WebstoreInstaller() { | 265 WebstoreInstaller::~WebstoreInstaller() { |
| 266 if (download_item_) { | 266 if (download_item_) { |
| 267 download_item_->RemoveObserver(this); | 267 download_item_->RemoveObserver(this); |
| 268 download_item_ = NULL; | 268 download_item_ = NULL; |
| 269 } | 269 } |
| 270 } | 270 } |
| 271 | 271 |
| 272 void WebstoreInstaller::OnDownloadStarted(DownloadId id, net::Error error) { | 272 void WebstoreInstaller::OnDownloadStarted(DownloadId id, net::Error error) { |
| 273 if (error != net::OK) { | 273 if (error != net::OK) { |
| 274 ReportFailure(net::ErrorToString(error)); | 274 ReportFailure(net::ErrorToString(error), FAILURE_REASON_OTHER); |
| 275 return; | 275 return; |
| 276 } | 276 } |
| 277 | 277 |
| 278 CHECK(id.IsValid()); | 278 CHECK(id.IsValid()); |
| 279 | 279 |
| 280 DownloadManager* download_manager = | 280 DownloadManager* download_manager = |
| 281 BrowserContext::GetDownloadManager(profile_); | 281 BrowserContext::GetDownloadManager(profile_); |
| 282 if (!download_manager) | 282 if (!download_manager) |
| 283 return; | 283 return; |
| 284 download_item_ = download_manager->GetDownload(id.local()); | 284 download_item_ = download_manager->GetDownload(id.local()); |
| 285 // TODO(benjhayden): DCHECK(item && item->IsInProgress()) after investigating | 285 // TODO(benjhayden): DCHECK(item && item->IsInProgress()) after investigating |
| 286 // the relationship between net::OK and invalid id. | 286 // the relationship between net::OK and invalid id. |
| 287 if (download_item_) { | 287 if (download_item_) { |
| 288 download_item_->AddObserver(this); | 288 download_item_->AddObserver(this); |
| 289 if (approval_.get()) | 289 if (approval_.get()) |
| 290 download_item_->SetUserData(kApprovalKey, approval_.release()); | 290 download_item_->SetUserData(kApprovalKey, approval_.release()); |
| 291 if (delegate_) |
| 292 delegate_->OnExtensionDownloadStarted(id_, download_item_); |
| 291 } | 293 } |
| 292 } | 294 } |
| 293 | 295 |
| 294 void WebstoreInstaller::OnDownloadUpdated(DownloadItem* download) { | 296 void WebstoreInstaller::OnDownloadUpdated(DownloadItem* download) { |
| 295 CHECK_EQ(download_item_, download); | 297 CHECK_EQ(download_item_, download); |
| 296 | 298 |
| 297 switch (download->GetState()) { | 299 switch (download->GetState()) { |
| 298 case DownloadItem::CANCELLED: | 300 case DownloadItem::CANCELLED: |
| 299 ReportFailure(kDownloadCanceledError); | 301 ReportFailure(kDownloadCanceledError, FAILURE_REASON_CANCELLED); |
| 300 break; | 302 break; |
| 301 case DownloadItem::INTERRUPTED: | 303 case DownloadItem::INTERRUPTED: |
| 302 ReportFailure(kDownloadInterruptedError); | 304 ReportFailure(kDownloadInterruptedError, FAILURE_REASON_OTHER); |
| 303 break; | 305 break; |
| 304 case DownloadItem::COMPLETE: | 306 case DownloadItem::COMPLETE: |
| 305 // Wait for other notifications if the download is really an extension. | 307 // Wait for other notifications if the download is really an extension. |
| 306 if (!download_crx_util::IsExtensionDownload(*download)) | 308 if (!download_crx_util::IsExtensionDownload(*download)) |
| 307 ReportFailure(kInvalidDownloadError); | 309 ReportFailure(kInvalidDownloadError, FAILURE_REASON_OTHER); |
| 310 else if (delegate_) |
| 311 delegate_->OnExtensionDownloadProgress(id_, download); |
| 312 break; |
| 313 case DownloadItem::IN_PROGRESS: |
| 314 if (delegate_) |
| 315 delegate_->OnExtensionDownloadProgress(id_, download); |
| 308 break; | 316 break; |
| 309 default: | 317 default: |
| 310 // Continue listening if the download is not in one of the above states. | 318 // Continue listening if the download is not in one of the above states. |
| 311 break; | 319 break; |
| 312 } | 320 } |
| 313 } | 321 } |
| 314 | 322 |
| 315 void WebstoreInstaller::OnDownloadDestroyed(DownloadItem* download) { | 323 void WebstoreInstaller::OnDownloadDestroyed(DownloadItem* download) { |
| 316 CHECK_EQ(download_item_, download); | 324 CHECK_EQ(download_item_, download); |
| 317 download_item_->RemoveObserver(this); | 325 download_item_->RemoveObserver(this); |
| 318 download_item_ = NULL; | 326 download_item_ = NULL; |
| 319 } | 327 } |
| 320 | 328 |
| 321 void WebstoreInstaller::StartDownload(const FilePath& file) { | 329 void WebstoreInstaller::StartDownload(const FilePath& file) { |
| 322 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 330 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 323 | 331 |
| 324 DownloadManager* download_manager = | 332 DownloadManager* download_manager = |
| 325 BrowserContext::GetDownloadManager(profile_); | 333 BrowserContext::GetDownloadManager(profile_); |
| 326 if (file.empty() || | 334 if (file.empty() || |
| 327 !download_manager || | 335 !download_manager || |
| 328 !controller_->GetWebContents() || | 336 !controller_->GetWebContents() || |
| 329 !controller_->GetWebContents()->GetRenderProcessHost() || | 337 !controller_->GetWebContents()->GetRenderProcessHost() || |
| 330 !controller_->GetWebContents()->GetRenderViewHost() || | 338 !controller_->GetWebContents()->GetRenderViewHost() || |
| 331 !controller_->GetWebContents()->GetBrowserContext() || | 339 !controller_->GetWebContents()->GetBrowserContext() || |
| 332 !controller_->GetWebContents()->GetBrowserContext() | 340 !controller_->GetWebContents()->GetBrowserContext() |
| 333 ->GetResourceContext()) { | 341 ->GetResourceContext()) { |
| 334 ReportFailure(kDownloadDirectoryError); | 342 ReportFailure(kDownloadDirectoryError, FAILURE_REASON_OTHER); |
| 335 return; | 343 return; |
| 336 } | 344 } |
| 337 | 345 |
| 338 content::DownloadSaveInfo save_info; | 346 content::DownloadSaveInfo save_info; |
| 339 save_info.file_path = file; | 347 save_info.file_path = file; |
| 340 | 348 |
| 341 // The download url for the given extension is contained in |download_url_|. | 349 // The download url for the given extension is contained in |download_url_|. |
| 342 // We will navigate the current tab to this url to start the download. The | 350 // We will navigate the current tab to this url to start the download. The |
| 343 // download system will then pass the crx to the CrxInstaller. | 351 // download system will then pass the crx to the CrxInstaller. |
| 344 download_util::RecordDownloadSource( | 352 download_util::RecordDownloadSource( |
| 345 download_util::INITIATED_BY_WEBSTORE_INSTALLER); | 353 download_util::INITIATED_BY_WEBSTORE_INSTALLER); |
| 346 scoped_ptr<DownloadUrlParameters> params( | 354 scoped_ptr<DownloadUrlParameters> params( |
| 347 DownloadUrlParameters::FromWebContents( | 355 DownloadUrlParameters::FromWebContents( |
| 348 controller_->GetWebContents(), download_url_, save_info)); | 356 controller_->GetWebContents(), download_url_, save_info)); |
| 349 if (controller_->GetActiveEntry()) | 357 if (controller_->GetActiveEntry()) |
| 350 params->set_referrer( | 358 params->set_referrer( |
| 351 content::Referrer(controller_->GetActiveEntry()->GetURL(), | 359 content::Referrer(controller_->GetActiveEntry()->GetURL(), |
| 352 WebKit::WebReferrerPolicyDefault)); | 360 WebKit::WebReferrerPolicyDefault)); |
| 353 params->set_callback(base::Bind(&WebstoreInstaller::OnDownloadStarted, this)); | 361 params->set_callback(base::Bind(&WebstoreInstaller::OnDownloadStarted, this)); |
| 354 download_manager->DownloadUrl(params.Pass()); | 362 download_manager->DownloadUrl(params.Pass()); |
| 355 } | 363 } |
| 356 | 364 |
| 357 void WebstoreInstaller::ReportFailure(const std::string& error) { | 365 void WebstoreInstaller::ReportFailure(const std::string& error, |
| 366 FailureReason reason) { |
| 358 if (delegate_) { | 367 if (delegate_) { |
| 359 delegate_->OnExtensionInstallFailure(id_, error); | 368 delegate_->OnExtensionInstallFailure(id_, error, reason); |
| 360 delegate_ = NULL; | 369 delegate_ = NULL; |
| 361 } | 370 } |
| 362 | 371 |
| 363 Release(); // Balanced in Start(). | 372 Release(); // Balanced in Start(). |
| 364 } | 373 } |
| 365 | 374 |
| 366 void WebstoreInstaller::ReportSuccess() { | 375 void WebstoreInstaller::ReportSuccess() { |
| 367 if (delegate_) { | 376 if (delegate_) { |
| 368 delegate_->OnExtensionInstallSuccess(id_); | 377 delegate_->OnExtensionInstallSuccess(id_); |
| 369 delegate_ = NULL; | 378 delegate_ = NULL; |
| 370 } | 379 } |
| 371 | 380 |
| 372 Release(); // Balanced in Start(). | 381 Release(); // Balanced in Start(). |
| 373 } | 382 } |
| 374 | 383 |
| 375 } // namespace extensions | 384 } // namespace extensions |
| OLD | NEW |