| 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/download/chrome_download_manager_delegate.h" | 5 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 | 102 |
| 103 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile) | 103 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile) |
| 104 : profile_(profile), | 104 : profile_(profile), |
| 105 next_download_id_(0), | 105 next_download_id_(0), |
| 106 download_prefs_(new DownloadPrefs(profile->GetPrefs())) { | 106 download_prefs_(new DownloadPrefs(profile->GetPrefs())) { |
| 107 } | 107 } |
| 108 | 108 |
| 109 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { | 109 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { |
| 110 } | 110 } |
| 111 | 111 |
| 112 bool ChromeDownloadManagerDelegate::IsExtensionDownload( | |
| 113 const DownloadItem* item) { | |
| 114 if (item->PromptUserForSaveLocation()) | |
| 115 return false; | |
| 116 | |
| 117 if (item->GetMimeType() == extensions::Extension::kMimeType || | |
| 118 UserScript::IsURLUserScript(item->GetURL(), item->GetMimeType())) { | |
| 119 return true; | |
| 120 } else { | |
| 121 return false; | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 void ChromeDownloadManagerDelegate::SetDownloadManager(DownloadManager* dm) { | 112 void ChromeDownloadManagerDelegate::SetDownloadManager(DownloadManager* dm) { |
| 126 download_manager_ = dm; | 113 download_manager_ = dm; |
| 127 download_history_.reset(new DownloadHistory(profile_)); | 114 download_history_.reset(new DownloadHistory(profile_)); |
| 128 download_history_->Load( | 115 download_history_->Load( |
| 129 base::Bind(&DownloadManager::OnPersistentStoreQueryComplete, | 116 base::Bind(&DownloadManager::OnPersistentStoreQueryComplete, |
| 130 base::Unretained(dm))); | 117 base::Unretained(dm))); |
| 131 } | 118 } |
| 132 | 119 |
| 133 void ChromeDownloadManagerDelegate::Shutdown() { | 120 void ChromeDownloadManagerDelegate::Shutdown() { |
| 134 download_history_.reset(); | 121 download_history_.reset(); |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 | 280 |
| 294 bool ChromeDownloadManagerDelegate::ShouldCompleteDownload( | 281 bool ChromeDownloadManagerDelegate::ShouldCompleteDownload( |
| 295 DownloadItem* item, | 282 DownloadItem* item, |
| 296 const base::Closure& user_complete_callback) { | 283 const base::Closure& user_complete_callback) { |
| 297 return IsDownloadReadyForCompletion(item, base::Bind( | 284 return IsDownloadReadyForCompletion(item, base::Bind( |
| 298 &ChromeDownloadManagerDelegate::ShouldCompleteDownloadInternal, this, | 285 &ChromeDownloadManagerDelegate::ShouldCompleteDownloadInternal, this, |
| 299 item->GetId(), user_complete_callback)); | 286 item->GetId(), user_complete_callback)); |
| 300 } | 287 } |
| 301 | 288 |
| 302 bool ChromeDownloadManagerDelegate::ShouldOpenDownload(DownloadItem* item) { | 289 bool ChromeDownloadManagerDelegate::ShouldOpenDownload(DownloadItem* item) { |
| 303 if (IsExtensionDownload(item)) { | 290 if (download_crx_util::IsExtensionDownload(*item)) { |
| 304 // We can open extensions if either they came from the store, or | 291 scoped_refptr<CrxInstaller> crx_installer = |
| 305 // off-store-install is enabled. | 292 download_crx_util::OpenChromeExtension(profile_, *item); |
| 306 if (extensions::switch_utils::IsOffStoreInstallEnabled() || | |
| 307 WebstoreInstaller::GetAssociatedApproval(*item)) { | |
| 308 scoped_refptr<CrxInstaller> crx_installer = | |
| 309 download_crx_util::OpenChromeExtension(profile_, *item); | |
| 310 | 293 |
| 311 // CRX_INSTALLER_DONE will fire when the install completes. Observe() | 294 // CRX_INSTALLER_DONE will fire when the install completes. Observe() |
| 312 // will call DelayedDownloadOpened() on this item. If this DownloadItem | 295 // will call DelayedDownloadOpened() on this item. If this DownloadItem |
| 313 // is not around when CRX_INSTALLER_DONE fires, Complete() will not be | 296 // is not around when CRX_INSTALLER_DONE fires, Complete() will not be |
| 314 // called. | 297 // called. |
| 315 registrar_.Add(this, | 298 registrar_.Add(this, |
| 316 chrome::NOTIFICATION_CRX_INSTALLER_DONE, | 299 chrome::NOTIFICATION_CRX_INSTALLER_DONE, |
| 317 content::Source<CrxInstaller>(crx_installer.get())); | 300 content::Source<CrxInstaller>(crx_installer.get())); |
| 318 | 301 |
| 319 crx_installers_[crx_installer.get()] = item->GetId(); | 302 crx_installers_[crx_installer.get()] = item->GetId(); |
| 320 // The status text and percent complete indicator will change now | 303 // The status text and percent complete indicator will change now |
| 321 // that we are installing a CRX. Update observers so that they pick | 304 // that we are installing a CRX. Update observers so that they pick |
| 322 // up the change. | 305 // up the change. |
| 323 item->UpdateObservers(); | 306 item->UpdateObservers(); |
| 324 return false; | 307 return false; |
| 325 } | |
| 326 } | 308 } |
| 327 | 309 |
| 328 if (ShouldOpenWithWebIntents(item)) { | 310 if (ShouldOpenWithWebIntents(item)) { |
| 329 OpenWithWebIntent(item); | 311 OpenWithWebIntent(item); |
| 330 item->DelayedDownloadOpened(); | 312 item->DelayedDownloadOpened(); |
| 331 return false; | 313 return false; |
| 332 } | 314 } |
| 333 | 315 |
| 334 return true; | 316 return true; |
| 335 } | 317 } |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 565 // Freeze the user's preference for showing a Save As dialog. We're going | 547 // Freeze the user's preference for showing a Save As dialog. We're going |
| 566 // to bounce around a bunch of threads and we don't want to worry about race | 548 // to bounce around a bunch of threads and we don't want to worry about race |
| 567 // conditions where the user changes this pref out from under us. | 549 // conditions where the user changes this pref out from under us. |
| 568 if (download_prefs_->PromptForDownload()) { | 550 if (download_prefs_->PromptForDownload()) { |
| 569 // But ignore the user's preference for the following scenarios: | 551 // But ignore the user's preference for the following scenarios: |
| 570 // 1) Extension installation. Note that we only care here about the case | 552 // 1) Extension installation. Note that we only care here about the case |
| 571 // where an extension is installed, not when one is downloaded with | 553 // where an extension is installed, not when one is downloaded with |
| 572 // "save as...". | 554 // "save as...". |
| 573 // 2) Filetypes marked "always open." If the user just wants this file | 555 // 2) Filetypes marked "always open." If the user just wants this file |
| 574 // opened, don't bother asking where to keep it. | 556 // opened, don't bother asking where to keep it. |
| 575 if (!IsExtensionDownload(download) && | 557 if (!download_crx_util::IsExtensionDownload(*download) && |
| 576 !ShouldOpenFileBasedOnExtension(generated_name)) | 558 !ShouldOpenFileBasedOnExtension(generated_name)) |
| 577 state.prompt_user_for_save_location = true; | 559 state.prompt_user_for_save_location = true; |
| 578 } | 560 } |
| 579 if (download_prefs_->IsDownloadPathManaged()) { | 561 if (download_prefs_->IsDownloadPathManaged()) { |
| 580 state.prompt_user_for_save_location = false; | 562 state.prompt_user_for_save_location = false; |
| 581 } | 563 } |
| 582 | 564 |
| 583 // Determine the proper path for a download, by either one of the following: | 565 // Determine the proper path for a download, by either one of the following: |
| 584 // 1) using the default download directory. | 566 // 1) using the default download directory. |
| 585 // 2) prompting the user. | 567 // 2) prompting the user. |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 736 const DownloadStateInfo& state, | 718 const DownloadStateInfo& state, |
| 737 bool visited_referrer_before) { | 719 bool visited_referrer_before) { |
| 738 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 720 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 739 | 721 |
| 740 // Anything loaded directly from the address bar is OK. | 722 // Anything loaded directly from the address bar is OK. |
| 741 if (state.transition_type & content::PAGE_TRANSITION_FROM_ADDRESS_BAR) | 723 if (state.transition_type & content::PAGE_TRANSITION_FROM_ADDRESS_BAR) |
| 742 return false; | 724 return false; |
| 743 | 725 |
| 744 // Extensions that are not from the gallery are considered dangerous. | 726 // Extensions that are not from the gallery are considered dangerous. |
| 745 // When off-store install is disabled we skip this, since in this case, we | 727 // When off-store install is disabled we skip this, since in this case, we |
| 746 // will not offer to install the extension. | 728 // will cancel the install later. |
| 747 if (extensions::switch_utils::IsOffStoreInstallEnabled() && | 729 if (extensions::switch_utils::IsOffStoreInstallEnabled() && |
| 748 IsExtensionDownload(&download) && | 730 download_crx_util::IsExtensionDownload(download) && |
| 749 !WebstoreInstaller::GetAssociatedApproval(download)) { | 731 !WebstoreInstaller::GetAssociatedApproval(download)) { |
| 750 return true; | 732 return true; |
| 751 } | 733 } |
| 752 | 734 |
| 753 // Anything the user has marked auto-open is OK if it's user-initiated. | 735 // Anything the user has marked auto-open is OK if it's user-initiated. |
| 754 if (ShouldOpenFileBasedOnExtension(state.suggested_path) && | 736 if (ShouldOpenFileBasedOnExtension(state.suggested_path) && |
| 755 state.has_user_gesture) | 737 state.has_user_gesture) |
| 756 return false; | 738 return false; |
| 757 | 739 |
| 758 // "Allow on user gesture" is OK when we have a user gesture and the hosting | 740 // "Allow on user gesture" is OK when we have a user gesture and the hosting |
| (...skipping 10 matching lines...) Expand all Loading... |
| 769 int32 download_id, int64 db_handle) { | 751 int32 download_id, int64 db_handle) { |
| 770 // It's not immediately obvious, but HistoryBackend::CreateDownload() can | 752 // It's not immediately obvious, but HistoryBackend::CreateDownload() can |
| 771 // call this function with an invalid |db_handle|. For instance, this can | 753 // call this function with an invalid |db_handle|. For instance, this can |
| 772 // happen when the history database is offline. We cannot have multiple | 754 // happen when the history database is offline. We cannot have multiple |
| 773 // DownloadItems with the same invalid db_handle, so we need to assign a | 755 // DownloadItems with the same invalid db_handle, so we need to assign a |
| 774 // unique |db_handle| here. | 756 // unique |db_handle| here. |
| 775 if (db_handle == DownloadItem::kUninitializedHandle) | 757 if (db_handle == DownloadItem::kUninitializedHandle) |
| 776 db_handle = download_history_->GetNextFakeDbHandle(); | 758 db_handle = download_history_->GetNextFakeDbHandle(); |
| 777 download_manager_->OnItemAddedToPersistentStore(download_id, db_handle); | 759 download_manager_->OnItemAddedToPersistentStore(download_id, db_handle); |
| 778 } | 760 } |
| OLD | NEW |