diff options
author | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-29 23:16:33 +0000 |
---|---|---|
committer | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-29 23:16:33 +0000 |
commit | da1a27b8cd49172e6dbbaaaab48849ec29c526a0 (patch) | |
tree | 503e30a88e9fc948e85f4ccba8be784029616e4f | |
parent | 1d97be6e206ef861f45d5b0ea25c55dcc9993455 (diff) | |
download | chromium_src-da1a27b8cd49172e6dbbaaaab48849ec29c526a0.zip chromium_src-da1a27b8cd49172e6dbbaaaab48849ec29c526a0.tar.gz chromium_src-da1a27b8cd49172e6dbbaaaab48849ec29c526a0.tar.bz2 |
Create DownloadManagerDelegate class to group the embedder functions related to download.
BUG=82782
Review URL: http://codereview.chromium.org/7529014
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@94790 0039d316-1c4b-4281-b951-d872f2087c98
23 files changed, 299 insertions, 168 deletions
diff --git a/chrome/browser/chrome_content_browser_client.cc b/chrome/browser/chrome_content_browser_client.cc index c7f5713..5f5dd21 100644 --- a/chrome/browser/chrome_content_browser_client.cc +++ b/chrome/browser/chrome_content_browser_client.cc @@ -15,8 +15,6 @@ #include "chrome/browser/chrome_worker_message_filter.h" #include "chrome/browser/content_settings/host_content_settings_map.h" #include "chrome/browser/content_settings/tab_specific_content_settings.h" -#include "chrome/browser/download/download_file_picker.h" -#include "chrome/browser/download/save_package_file_picker.h" #include "chrome/browser/extensions/extension_info_map.h" #include "chrome/browser/extensions/extension_message_handler.h" #include "chrome/browser/extensions/extension_service.h" @@ -26,7 +24,6 @@ #include "chrome/browser/notifications/desktop_notification_service.h" #include "chrome/browser/notifications/desktop_notification_service_factory.h" #include "chrome/browser/platform_util.h" -#include "chrome/browser/prefs/pref_member.h" #include "chrome/browser/prefs/pref_service.h" #include "chrome/browser/prerender/prerender_manager.h" #include "chrome/browser/prerender/prerender_tracker.h" @@ -43,8 +40,6 @@ #include "chrome/browser/tab_contents/render_view_host_delegate_helper.h" #include "chrome/browser/tab_contents/tab_contents_ssl_helper.h" #include "chrome/browser/tab_contents/tab_util.h" -#include "chrome/browser/ui/browser.h" -#include "chrome/browser/ui/browser_list.h" #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" #include "chrome/browser/ui/webui/chrome_web_ui_factory.h" #include "chrome/common/child_process_logging.h" @@ -764,60 +759,6 @@ void ChromeContentBrowserClient::ClearCookies(RenderViewHost* rvh) { // BrowsingDataRemover takes care of deleting itself when done. } -void ChromeContentBrowserClient::GetSaveDir(TabContents* tab_contents, - FilePath* website_save_dir, - FilePath* download_save_dir) { - Profile* profile = - Profile::FromBrowserContext(tab_contents->browser_context()); - PrefService* prefs = profile->GetPrefs(); - - // Check whether the preference has the preferred directory for saving file. - // If not, initialize it with default directory. - if (!prefs->FindPreference(prefs::kSaveFileDefaultDirectory)) { - DCHECK(prefs->FindPreference(prefs::kDownloadDefaultDirectory)); - FilePath default_save_path = prefs->GetFilePath( - prefs::kDownloadDefaultDirectory); - prefs->RegisterFilePathPref(prefs::kSaveFileDefaultDirectory, - default_save_path, - PrefService::UNSYNCABLE_PREF); - } - - // Get the directory from preference. - *website_save_dir = prefs->GetFilePath(prefs::kSaveFileDefaultDirectory); - DCHECK(!website_save_dir->empty()); - - *download_save_dir = prefs->GetFilePath(prefs::kDownloadDefaultDirectory); -} - -void ChromeContentBrowserClient::ChooseSavePath( - const base::WeakPtr<SavePackage>& save_package, - const FilePath& suggested_path, - bool can_save_as_complete) { - // Deletes itself. - new SavePackageFilePicker( - save_package, suggested_path, can_save_as_complete); -} - -void ChromeContentBrowserClient::ChooseDownloadPath( - DownloadManager* download_manager, - TabContents* tab_contents, - const FilePath& suggested_path, - void* data) { - // Deletes itself. - new DownloadFilePicker( - download_manager, tab_contents, suggested_path, data); -} - -TabContents* - ChromeContentBrowserClient::GetAlternativeTabContentsToNotifyForDownload( - DownloadManager* download_manager) { - // Start the download in the last active browser. This is not ideal but better - // than fully hiding the download from the user. - Browser* last_active = BrowserList::GetLastActiveWithProfile( - download_manager->profile()); - return last_active ? last_active->GetSelectedTabContents() : NULL; -} - #if defined(OS_LINUX) int ChromeContentBrowserClient::GetCrashSignalFD( const std::string& process_type) { diff --git a/chrome/browser/chrome_content_browser_client.h b/chrome/browser/chrome_content_browser_client.h index 6a0bdaa..2f196cb 100644 --- a/chrome/browser/chrome_content_browser_client.h +++ b/chrome/browser/chrome_content_browser_client.h @@ -9,8 +9,6 @@ #include "base/compiler_specific.h" #include "content/browser/content_browser_client.h" -class QuotaPermissionContext; - namespace chrome { class ChromeContentBrowserClient : public content::ContentBrowserClient { @@ -107,18 +105,6 @@ class ChromeContentBrowserClient : public content::ContentBrowserClient { virtual void BrowserURLHandlerCreated(BrowserURLHandler* handler) OVERRIDE; virtual void ClearCache(RenderViewHost* rvh) OVERRIDE; virtual void ClearCookies(RenderViewHost* rvh) OVERRIDE; - virtual void GetSaveDir(TabContents* tab_contents, - FilePath* website_save_dir, - FilePath* download_save_dir) OVERRIDE; - virtual void ChooseSavePath(const base::WeakPtr<SavePackage>& save_package, - const FilePath& suggested_path, - bool can_save_as_complete) OVERRIDE; - virtual void ChooseDownloadPath(DownloadManager* download_manager, - TabContents* tab_contents, - const FilePath& suggested_path, - void* data) OVERRIDE; - virtual TabContents* GetAlternativeTabContentsToNotifyForDownload( - DownloadManager* download_manager) OVERRIDE; #if defined(OS_POSIX) && !defined(OS_MACOSX) // Can return an optional fd for crash handling, otherwise returns -1. diff --git a/chrome/browser/download/chrome_download_manager_delegate.cc b/chrome/browser/download/chrome_download_manager_delegate.cc new file mode 100644 index 0000000..32a39ff --- /dev/null +++ b/chrome/browser/download/chrome_download_manager_delegate.cc @@ -0,0 +1,73 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/download/chrome_download_manager_delegate.h" + +#include "chrome/browser/download/download_file_picker.h" +#include "chrome/browser/download/save_package_file_picker.h" +#include "chrome/browser/prefs/pref_member.h" +#include "chrome/browser/prefs/pref_service.h" +#include "chrome/browser/profiles/profile.h" +#include "chrome/browser/ui/browser.h" +#include "chrome/browser/ui/browser_list.h" +#include "chrome/common/pref_names.h" +#include "content/browser/tab_contents/tab_contents.h" + +ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate() + : download_manager_(NULL) { +} + +void ChromeDownloadManagerDelegate::GetSaveDir(TabContents* tab_contents, + FilePath* website_save_dir, + FilePath* download_save_dir) { + Profile* profile = + Profile::FromBrowserContext(tab_contents->browser_context()); + PrefService* prefs = profile->GetPrefs(); + + // Check whether the preference has the preferred directory for saving file. + // If not, initialize it with default directory. + if (!prefs->FindPreference(prefs::kSaveFileDefaultDirectory)) { + DCHECK(prefs->FindPreference(prefs::kDownloadDefaultDirectory)); + FilePath default_save_path = prefs->GetFilePath( + prefs::kDownloadDefaultDirectory); + prefs->RegisterFilePathPref(prefs::kSaveFileDefaultDirectory, + default_save_path, + PrefService::UNSYNCABLE_PREF); + } + + // Get the directory from preference. + *website_save_dir = prefs->GetFilePath(prefs::kSaveFileDefaultDirectory); + DCHECK(!website_save_dir->empty()); + + *download_save_dir = prefs->GetFilePath(prefs::kDownloadDefaultDirectory); +} + +void ChromeDownloadManagerDelegate::ChooseSavePath( + const base::WeakPtr<SavePackage>& save_package, + const FilePath& suggested_path, + bool can_save_as_complete) { + // Deletes itself. + new SavePackageFilePicker( + save_package, suggested_path, can_save_as_complete); +} + +void ChromeDownloadManagerDelegate::ChooseDownloadPath( + DownloadManager* download_manager, + TabContents* tab_contents, + const FilePath& suggested_path, + void* data) { + // Deletes itself. + new DownloadFilePicker( + download_manager, tab_contents, suggested_path, data); +} + +TabContents* + ChromeDownloadManagerDelegate::GetAlternativeTabContentsToNotifyForDownload( + DownloadManager* download_manager) { + // Start the download in the last active browser. This is not ideal but better + // than fully hiding the download from the user. + Browser* last_active = BrowserList::GetLastActiveWithProfile( + download_manager->profile()); + return last_active ? last_active->GetSelectedTabContents() : NULL; +} diff --git a/chrome/browser/download/chrome_download_manager_delegate.h b/chrome/browser/download/chrome_download_manager_delegate.h new file mode 100644 index 0000000..9372a44 --- /dev/null +++ b/chrome/browser/download/chrome_download_manager_delegate.h @@ -0,0 +1,38 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_DOWNLOAD_CHROME_DOWNLOAD_MANAGER_DELEGATE_H_ +#define CHROME_BROWSER_DOWNLOAD_CHROME_DOWNLOAD_MANAGER_DELEGATE_H_ +#pragma once + +#include "base/compiler_specific.h" +#include "chrome/browser/download/download_manager_delegate.h" + +// Browser's download manager: manages all downloads and destination view. +class ChromeDownloadManagerDelegate : public DownloadManagerDelegate { + public: + ChromeDownloadManagerDelegate(); + + virtual void GetSaveDir(TabContents* tab_contents, + FilePath* website_save_dir, + FilePath* download_save_dir) OVERRIDE; + virtual void ChooseSavePath(const base::WeakPtr<SavePackage>& save_package, + const FilePath& suggested_path, + bool can_save_as_complete) OVERRIDE; + virtual void ChooseDownloadPath(DownloadManager* download_manager, + TabContents* tab_contents, + const FilePath& suggested_path, + void* data) OVERRIDE; + virtual TabContents* GetAlternativeTabContentsToNotifyForDownload( + DownloadManager* download_manager) OVERRIDE; + + void set_download_manager(DownloadManager* dm) { download_manager_ = dm; } + + private: + DownloadManager* download_manager_; + + DISALLOW_COPY_AND_ASSIGN(ChromeDownloadManagerDelegate); +}; + +#endif // CHROME_BROWSER_DOWNLOAD_CHROME_DOWNLOAD_MANAGER_DELEGATE_H_ diff --git a/chrome/browser/download/download_file_unittest.cc b/chrome/browser/download/download_file_unittest.cc index f4a2a28..7ecf3a0a 100644 --- a/chrome/browser/download/download_file_unittest.cc +++ b/chrome/browser/download/download_file_unittest.cc @@ -13,6 +13,7 @@ #include "chrome/browser/download/download_status_updater.h" #include "chrome/browser/download/download_util.h" #include "chrome/browser/download/mock_download_manager.h" +#include "chrome/browser/download/mock_download_manager_delegate.h" #include "content/browser/browser_thread.h" #include "net/base/file_stream.h" #include "testing/gtest/include/gtest/gtest.h" @@ -42,7 +43,9 @@ class DownloadFileTest : public testing::Test { virtual void SetUp() { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); - download_manager_ = new MockDownloadManager(&download_status_updater_); + download_manager_delegate_.reset(new MockDownloadManagerDelegate()); + download_manager_ = new MockDownloadManager( + download_manager_delegate_.get(), &download_status_updater_); } virtual void TearDown() { @@ -95,6 +98,7 @@ class DownloadFileTest : public testing::Test { ScopedTempDir temp_dir_; DownloadStatusUpdater download_status_updater_; + scoped_ptr<MockDownloadManagerDelegate> download_manager_delegate_; scoped_refptr<DownloadManager> download_manager_; linked_ptr<net::FileStream> file_stream_; diff --git a/chrome/browser/download/download_item.h b/chrome/browser/download/download_item.h index 5596b53..d0749f2 100644 --- a/chrome/browser/download/download_item.h +++ b/chrome/browser/download/download_item.h @@ -293,7 +293,9 @@ class DownloadItem : public NotificationObserver { bool auto_opened() { return auto_opened_; } const FilePath& target_name() const { return state_info_.target_name; } - bool save_as() const { return state_info_.prompt_user_for_save_location; } + bool prompt_user_for_save_location() const { + return state_info_.prompt_user_for_save_location; + } bool is_otr() const { return is_otr_; } bool is_extension_install() const { return state_info_.is_extension_install; diff --git a/chrome/browser/download/download_manager.cc b/chrome/browser/download/download_manager.cc index 89c4eca..cb62310 100644 --- a/chrome/browser/download/download_manager.cc +++ b/chrome/browser/download/download_manager.cc @@ -22,6 +22,7 @@ #include "chrome/browser/download/download_file_manager.h" #include "chrome/browser/download/download_history.h" #include "chrome/browser/download/download_item.h" +#include "chrome/browser/download/download_manager_delegate.h" #include "chrome/browser/download/download_prefs.h" #include "chrome/browser/download/download_request_handle.h" #include "chrome/browser/download/download_safe_browsing_client.h" @@ -34,7 +35,6 @@ #include "chrome/common/chrome_paths.h" #include "chrome/common/pref_names.h" #include "content/browser/browser_thread.h" -#include "content/browser/content_browser_client.h" #include "content/browser/renderer_host/render_process_host.h" #include "content/browser/renderer_host/render_view_host.h" #include "content/browser/renderer_host/resource_dispatcher_host.h" @@ -48,11 +48,13 @@ #include "ui/base/l10n/l10n_util.h" #include "ui/base/resource/resource_bundle.h" -DownloadManager::DownloadManager(DownloadStatusUpdater* status_updater) +DownloadManager::DownloadManager(DownloadManagerDelegate* delegate, + DownloadStatusUpdater* status_updater) : shutdown_needed_(false), profile_(NULL), file_manager_(NULL), - status_updater_(status_updater->AsWeakPtr()) { + status_updater_(status_updater->AsWeakPtr()), + delegate_(delegate) { if (status_updater_) status_updater_->AddDelegate(this); } @@ -387,7 +389,7 @@ void DownloadManager::CheckVisitedReferrerBeforeDone( if (!state.prompt_user_for_save_location && state.force_file_name.empty()) { state.is_dangerous_file = - IsDangerous(*download, state, visited_referrer_before); + IsDangerousFile(*download, state, visited_referrer_before); } // We need to move over to the download thread because we don't want to stat @@ -508,7 +510,7 @@ void DownloadManager::OnPathExistenceAvailable( FilePath suggested_path = download->suggested_path(); - if (download->save_as()) { + if (download->prompt_user_for_save_location()) { // We must ask the user for the place to put the download. DownloadRequestHandle request_handle = download->request_handle(); TabContents* contents = request_handle.GetTabContents(); @@ -518,7 +520,7 @@ void DownloadManager::OnPathExistenceAvailable( int32* id_ptr = new int32; *id_ptr = download_id; - content::GetContentClient()->browser()->ChooseDownloadPath( + delegate_->ChooseDownloadPath( this, contents, suggested_path, reinterpret_cast<void*>(id_ptr)); FOR_EACH_OBSERVER(Observer, observers_, @@ -1071,7 +1073,7 @@ void DownloadManager::FileSelected(const FilePath& path, void* params) { VLOG(20) << __FUNCTION__ << "()" << " path = \"" << path.value() << "\"" << " download = " << download->DebugString(true); - if (download->save_as()) + if (download->prompt_user_for_save_location()) last_download_path_ = path.DirName(); // Make sure the initial file name is set only once. @@ -1098,9 +1100,9 @@ void DownloadManager::FileSelectionCanceled(void* params) { } // TODO(phajdan.jr): This is apparently not being exercised in tests. -bool DownloadManager::IsDangerous(const DownloadItem& download, - const DownloadStateInfo& state, - bool visited_referrer_before) { +bool DownloadManager::IsDangerousFile(const DownloadItem& download, + const DownloadStateInfo& state, + bool visited_referrer_before) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); bool auto_open = ShouldOpenFileBasedOnExtension(state.suggested_path); @@ -1215,10 +1217,8 @@ void DownloadManager::ShowDownloadInBrowser(DownloadItem* download) { // If the contents no longer exists, we ask the embedder to suggest another // tab. - if (!content) { - content = content::GetContentClient()->browser()-> - GetAlternativeTabContentsToNotifyForDownload(this); - } + if (!content) + content = delegate_->GetAlternativeTabContentsToNotifyForDownload(this); if (content) content->OnStartDownload(download); diff --git a/chrome/browser/download/download_manager.h b/chrome/browser/download/download_manager.h index d7d473ca..b1ba24b 100644 --- a/chrome/browser/download/download_manager.h +++ b/chrome/browser/download/download_manager.h @@ -49,6 +49,7 @@ class DownloadFileManager; class DownloadHistory; +class DownloadManagerDelegate; class DownloadPrefs; class DownloadStatusUpdater; class GURL; @@ -65,7 +66,8 @@ class DownloadManager BrowserThread::DeleteOnUIThread>, public DownloadStatusUpdaterDelegate { public: - explicit DownloadManager(DownloadStatusUpdater* status_updater); + DownloadManager(DownloadManagerDelegate* delegate, + DownloadStatusUpdater* status_updater); // Shutdown the download manager. Must be called before destruction. void Shutdown(); @@ -227,9 +229,9 @@ class DownloadManager // Various factors are considered, such as the type of the file, whether a // user action initiated the download, and whether the user has explicitly // marked the file type as "auto open". - bool IsDangerous(const DownloadItem& download, - const DownloadStateInfo& state, - bool visited_referrer_before); + bool IsDangerousFile(const DownloadItem& download, + const DownloadStateInfo& state, + bool visited_referrer_before); // Checks whether downloaded files still exist. Updates state of downloads // that refer to removed files. The check runs in the background and may @@ -261,6 +263,8 @@ class DownloadManager // been removed from the active map, or was retrieved from the history DB. DownloadItem* GetDownloadItem(int id); + DownloadManagerDelegate* delegate() const { return delegate_; } + private: // For testing. friend class DownloadManagerTest; @@ -420,6 +424,9 @@ class DownloadManager scoped_ptr<OtherDownloadManagerObserver> other_download_manager_observer_; + // Allows an embedder to control behavior. Guaranteed to outlive this object. + DownloadManagerDelegate* delegate_; + DISALLOW_COPY_AND_ASSIGN(DownloadManager); }; diff --git a/chrome/browser/download/download_manager_delegate.h b/chrome/browser/download/download_manager_delegate.h new file mode 100644 index 0000000..db1265b --- /dev/null +++ b/chrome/browser/download/download_manager_delegate.h @@ -0,0 +1,52 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_DELEGATE_H_ +#define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_DELEGATE_H_ +#pragma once + +#include "base/basictypes.h" +#include "base/memory/weak_ptr.h" + +class DownloadManager; +class FilePath; +class TabContents; +class SavePackage; + +// Browser's download manager: manages all downloads and destination view. +class DownloadManagerDelegate { + public: + // Retrieve the directories to save html pages and downloads to. + virtual void GetSaveDir(TabContents* tab_contents, + FilePath* website_save_dir, + FilePath* download_save_dir) = 0; + + // Asks the user for the path to save a page. The embedder calls + // SavePackage::OnPathPicked to give the answer. + virtual void ChooseSavePath(const base::WeakPtr<SavePackage>& save_package, + const FilePath& suggested_path, + bool can_save_as_complete) = 0; + + // Asks the user for the path for a download. The embedder calls + // DownloadManager::FileSelected or DownloadManager::FileSelectionCanceled to + // give the answer. + virtual void ChooseDownloadPath(DownloadManager* download_manager, + TabContents* tab_contents, + const FilePath& suggested_path, + void* data) = 0; + + // Called when the download system wants to alert a TabContents that a + // download has started, but the TabContents has gone away. This lets an + // embedder return an alternative TabContents. The embedder can return NULL. + virtual TabContents* GetAlternativeTabContentsToNotifyForDownload( + DownloadManager* download_manager) = 0; + + protected: + DownloadManagerDelegate() {} + virtual ~DownloadManagerDelegate() {} + + DISALLOW_COPY_AND_ASSIGN(DownloadManagerDelegate); +}; + +#endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_DELEGATE_H_ diff --git a/chrome/browser/download/download_manager_unittest.cc b/chrome/browser/download/download_manager_unittest.cc index e8cda1c..c722a0f 100644 --- a/chrome/browser/download/download_manager_unittest.cc +++ b/chrome/browser/download/download_manager_unittest.cc @@ -24,6 +24,7 @@ #include "chrome/browser/download/download_status_updater.h" #include "chrome/browser/download/download_util.h" #include "chrome/browser/download/mock_download_manager.h" +#include "chrome/browser/download/mock_download_manager_delegate.h" #include "chrome/browser/prefs/pref_service.h" #include "chrome/common/pref_names.h" #include "chrome/test/testing_profile.h" @@ -42,7 +43,9 @@ class DownloadManagerTest : public testing::Test { DownloadManagerTest() : profile_(new TestingProfile()), - download_manager_(new MockDownloadManager(&download_status_updater_)), + download_manager_delegate_(new MockDownloadManagerDelegate()), + download_manager_(new MockDownloadManager( + download_manager_delegate_.get(), &download_status_updater_)), ui_thread_(BrowserThread::UI, &message_loop_), file_thread_(BrowserThread::FILE, &message_loop_) { download_manager_->Init(profile_.get()); @@ -87,6 +90,7 @@ class DownloadManagerTest : public testing::Test { protected: DownloadStatusUpdater download_status_updater_; scoped_ptr<TestingProfile> profile_; + scoped_ptr<MockDownloadManagerDelegate> download_manager_delegate_; scoped_refptr<DownloadManager> download_manager_; scoped_refptr<DownloadFileManager> file_manager_; MessageLoopForUI message_loop_; diff --git a/chrome/browser/download/mock_download_manager.h b/chrome/browser/download/mock_download_manager.h index 410bdc30..87af624 100644 --- a/chrome/browser/download/mock_download_manager.h +++ b/chrome/browser/download/mock_download_manager.h @@ -13,8 +13,9 @@ class DownloadItem; class MockDownloadManager : public DownloadManager { public: - explicit MockDownloadManager(DownloadStatusUpdater* updater) - : DownloadManager(updater) { + explicit MockDownloadManager(DownloadManagerDelegate* delegate, + DownloadStatusUpdater* updater) + : DownloadManager(delegate, updater) { } // Override some functions. diff --git a/chrome/browser/download/mock_download_manager_delegate.cc b/chrome/browser/download/mock_download_manager_delegate.cc new file mode 100644 index 0000000..566a5d3 --- /dev/null +++ b/chrome/browser/download/mock_download_manager_delegate.cc @@ -0,0 +1,33 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/download/mock_download_manager_delegate.h" + +MockDownloadManagerDelegate::~MockDownloadManagerDelegate() { +} + +void MockDownloadManagerDelegate::GetSaveDir( + TabContents* tab_contents, + FilePath* website_save_dir, + FilePath* download_save_dir) { +} + +void MockDownloadManagerDelegate::ChooseSavePath( + const base::WeakPtr<SavePackage>& save_package, + const FilePath& suggested_path, + bool can_save_as_complete) { +} + +void MockDownloadManagerDelegate::ChooseDownloadPath( + DownloadManager* download_manager, + TabContents* tab_contents, + const FilePath& suggested_path, + void* data) { +} + +TabContents* + MockDownloadManagerDelegate::GetAlternativeTabContentsToNotifyForDownload( + DownloadManager* download_manager) { + return NULL; +} diff --git a/chrome/browser/download/mock_download_manager_delegate.h b/chrome/browser/download/mock_download_manager_delegate.h new file mode 100644 index 0000000..10eac6a --- /dev/null +++ b/chrome/browser/download/mock_download_manager_delegate.h @@ -0,0 +1,29 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_DOWNLOAD_MOCK_DOWNLOAD_MANAGER_DELEGATE_H_ +#define CHROME_BROWSER_DOWNLOAD_MOCK_DOWNLOAD_MANAGER_DELEGATE_H_ +#pragma once + +#include "base/compiler_specific.h" +#include "chrome/browser/download/download_manager_delegate.h" + +class MockDownloadManagerDelegate : public DownloadManagerDelegate { + public: + virtual ~MockDownloadManagerDelegate(); + virtual void GetSaveDir(TabContents* tab_contents, + FilePath* website_save_dir, + FilePath* download_save_dir) OVERRIDE; + virtual void ChooseSavePath(const base::WeakPtr<SavePackage>& save_package, + const FilePath& suggested_path, + bool can_save_as_complete) OVERRIDE; + virtual void ChooseDownloadPath(DownloadManager* download_manager, + TabContents* tab_contents, + const FilePath& suggested_path, + void* data) OVERRIDE; + virtual TabContents* GetAlternativeTabContentsToNotifyForDownload( + DownloadManager* download_manager) OVERRIDE; +}; + +#endif // CHROME_BROWSER_DOWNLOAD_MOCK_DOWNLOAD_MANAGER_DELEGATE_H_ diff --git a/chrome/browser/profiles/profile.cc b/chrome/browser/profiles/profile.cc index 933ffd1..2d2506f 100644 --- a/chrome/browser/profiles/profile.cc +++ b/chrome/browser/profiles/profile.cc @@ -17,6 +17,7 @@ #include "chrome/browser/background/background_contents_service_factory.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/content_settings/host_content_settings_map.h" +#include "chrome/browser/download/chrome_download_manager_delegate.h" #include "chrome/browser/download/download_manager.h" #include "chrome/browser/extensions/extension_info_map.h" #include "chrome/browser/extensions/extension_message_service.h" @@ -450,8 +451,11 @@ class OffTheRecordProfileImpl : public Profile, virtual DownloadManager* GetDownloadManager() { if (!download_manager_.get()) { + download_manager_delegate_.reset(new ChromeDownloadManagerDelegate()); scoped_refptr<DownloadManager> dlm( - new DownloadManager(g_browser_process->download_status_updater())); + new DownloadManager(download_manager_delegate_.get(), + g_browser_process->download_status_updater())); + download_manager_delegate_->set_download_manager(dlm); dlm->Init(this); download_manager_.swap(dlm); } @@ -780,6 +784,10 @@ class OffTheRecordProfileImpl : public Profile, OffTheRecordProfileIOData::Handle io_data_; + // Used so that Chrome code can influence how content module's DownloadManager + // functions. + scoped_ptr<ChromeDownloadManagerDelegate> download_manager_delegate_; + // The download manager that only stores downloaded items in memory. scoped_refptr<DownloadManager> download_manager_; diff --git a/chrome/browser/profiles/profile_impl.cc b/chrome/browser/profiles/profile_impl.cc index f0d13c7..44fc2cd 100644 --- a/chrome/browser/profiles/profile_impl.cc +++ b/chrome/browser/profiles/profile_impl.cc @@ -25,6 +25,7 @@ #include "chrome/browser/content_settings/host_content_settings_map.h" #include "chrome/browser/custom_handlers/protocol_handler_registry.h" #include "chrome/browser/defaults.h" +#include "chrome/browser/download/chrome_download_manager_delegate.h" #include "chrome/browser/download/download_manager.h" #include "chrome/browser/extensions/extension_devtools_manager.h" #include "chrome/browser/extensions/extension_error_reporter.h" @@ -1270,8 +1271,11 @@ void ProfileImpl::CreatePasswordStore() { DownloadManager* ProfileImpl::GetDownloadManager() { if (!created_download_manager_) { + download_manager_delegate_.reset(new ChromeDownloadManagerDelegate()); scoped_refptr<DownloadManager> dlm( - new DownloadManager(g_browser_process->download_status_updater())); + new DownloadManager(download_manager_delegate_.get(), + g_browser_process->download_status_updater())); + download_manager_delegate_->set_download_manager(dlm); dlm->Init(this); created_download_manager_ = true; download_manager_.swap(dlm); diff --git a/chrome/browser/profiles/profile_impl.h b/chrome/browser/profiles/profile_impl.h index af5fd0c..ed54252 100644 --- a/chrome/browser/profiles/profile_impl.h +++ b/chrome/browser/profiles/profile_impl.h @@ -19,6 +19,7 @@ #include "content/common/notification_observer.h" #include "content/common/notification_registrar.h" +class ChromeDownloadManagerDelegate; class ExtensionPrefs; class ExtensionPrefValueMap; class PrefService; @@ -234,6 +235,7 @@ class ProfileImpl : public Profile, geolocation_permission_context_; scoped_refptr<UserStyleSheetWatcher> user_style_sheet_watcher_; scoped_ptr<FindBarState> find_bar_state_; + scoped_ptr<ChromeDownloadManagerDelegate> download_manager_delegate_; scoped_refptr<DownloadManager> download_manager_; scoped_refptr<HistoryService> history_service_; scoped_refptr<FaviconService> favicon_service_; diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 0eb221f..8329c1e 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -821,6 +821,8 @@ 'browser/diagnostics/sqlite_diagnostics.cc', 'browser/diagnostics/sqlite_diagnostics.h', 'browser/dom_operation_notification_details.h', + 'browser/download/chrome_download_manager_delegate.cc', + 'browser/download/chrome_download_manager_delegate.h', 'browser/download/download_create_info.cc', 'browser/download/download_create_info.h', 'browser/download/download_crx_util.cc', @@ -841,6 +843,7 @@ 'browser/download/download_item_model.h', 'browser/download/download_manager.cc', 'browser/download/download_manager.h', + 'browser/download/download_manager_delegate.h', 'browser/download/download_prefs.cc', 'browser/download/download_prefs.h', 'browser/download/download_request_handle.cc', diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index 3d806af..be7f7e5 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -1403,6 +1403,8 @@ 'browser/download/download_status_updater_unittest.cc', 'browser/download/download_util_unittest.cc', 'browser/download/mock_download_manager.h', + 'browser/download/mock_download_manager_delegate.cc', + 'browser/download/mock_download_manager_delegate.h', 'browser/enumerate_modules_model_unittest_win.cc', 'browser/extensions/apps_promo_unittest.cc', 'browser/extensions/convert_user_script_unittest.cc', diff --git a/content/browser/content_browser_client.h b/content/browser/content_browser_client.h index 9983a21..fd5f4aa 100644 --- a/content/browser/content_browser_client.h +++ b/content/browser/content_browser_client.h @@ -9,7 +9,6 @@ #include <string> #include "base/callback_old.h" -#include "base/memory/weak_ptr.h" #include "content/common/content_client.h" #include "content/common/window_container_type.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebNotificationPresenter.h" @@ -18,7 +17,6 @@ class BrowserRenderProcessHost; class BrowserURLHandler; class CommandLine; class DevToolsManager; -class DownloadManager; class FilePath; class GURL; class MHTMLGenerationManager; @@ -28,7 +26,6 @@ class RenderViewHost; class ResourceDispatcherHost; class SSLCertErrorHandler; class SSLClientAuthHandler; -class SavePackage; class SkBitmap; class TabContents; class WorkerProcessHost; @@ -265,31 +262,6 @@ class ContentBrowserClient { // Clears browser cookies. virtual void ClearCookies(RenderViewHost* rvh) = 0; - // Retrieve the directories to save html pages and downloads to. - virtual void GetSaveDir(TabContents* tab_contents, - FilePath* website_save_dir, - FilePath* download_save_dir) = 0; - - // Asks the user for the path to save a page. The embedder calls - // SavePackage::OnPathPicked to give the answer. - virtual void ChooseSavePath(const base::WeakPtr<SavePackage>& save_package, - const FilePath& suggested_path, - bool can_save_as_complete) = 0; - - // Asks the user for the path for a download. The embedder calls - // DownloadManager::FileSelected or DownloadManager::FileSelectionCanceled to - // give the answer. - virtual void ChooseDownloadPath(DownloadManager* download_manager, - TabContents* tab_contents, - const FilePath& suggested_path, - void* data) = 0; - - // Called when the download system wants to alert a TabContents that a - // download has started, but the TabContents has gone away. This lets an - // embedder return an alternative TabContents. The embedder can return NULL. - virtual TabContents* GetAlternativeTabContentsToNotifyForDownload( - DownloadManager* download_manager) = 0; - #if defined(OS_POSIX) && !defined(OS_MACOSX) // Can return an optional fd for crash handling, otherwise returns -1. virtual int GetCrashSignalFD(const std::string& process_type) = 0; diff --git a/content/browser/download/save_package.cc b/content/browser/download/save_package.cc index 00cf6c6..f26d974 100644 --- a/content/browser/download/save_package.cc +++ b/content/browser/download/save_package.cc @@ -21,6 +21,7 @@ #include "chrome/browser/download/download_item.h" #include "chrome/browser/download/download_item_model.h" #include "chrome/browser/download/download_manager.h" +#include "chrome/browser/download/download_manager_delegate.h" #include "chrome/browser/download/download_util.h" #include "chrome/browser/profiles/profile.h" #include "content/browser/browser_thread.h" @@ -263,16 +264,14 @@ bool SavePackage::Init() { } // Create the fake DownloadItem and display the view. - DownloadManager* download_manager = - tab_contents()->browser_context()->GetDownloadManager(); - download_ = new DownloadItem(download_manager, + download_ = new DownloadItem(GetDownloadManager(), saved_main_file_path_, page_url_, browser_context->IsOffTheRecord()); // Transfer the ownership to the download manager. We need the DownloadItem // to be alive as long as the Profile is alive. - download_manager->SavePageAsDownloadStarted(download_); + GetDownloadManager()->SavePageAsDownloadStarted(download_); tab_contents()->OnStartDownload(download_); @@ -1156,7 +1155,7 @@ void SavePackage::GetSaveInfo() { // Can't use tab_contents_ in the file thread, so get the data that we need // before calling to it. FilePath website_save_dir, download_save_dir; - content::GetContentClient()->browser()->GetSaveDir( + GetDownloadManager()->delegate()->GetSaveDir( tab_contents(), &website_save_dir, &download_save_dir); std::string mime_type = tab_contents()->contents_mime_type(); std::string accept_languages = @@ -1219,7 +1218,7 @@ void SavePackage::ContinueGetSaveInfo(const FilePath& suggested_path, if (!tab_contents()) return; - content::GetContentClient()->browser()->ChooseSavePath( + GetDownloadManager()->delegate()->ChooseSavePath( AsWeakPtr(), suggested_path, can_save_as_complete); } @@ -1264,3 +1263,7 @@ bool SavePackage::IsSavableContents(const std::string& contents_mime_type) { contents_mime_type == "text/css" || net::IsSupportedJavascriptMimeType(contents_mime_type.c_str()); } + +DownloadManager* SavePackage::GetDownloadManager() { + return tab_contents()->browser_context()->GetDownloadManager();; +} diff --git a/content/browser/download/save_package.h b/content/browser/download/save_package.h index 2c828a3..09a44c0 100644 --- a/content/browser/download/save_package.h +++ b/content/browser/download/save_package.h @@ -211,6 +211,9 @@ class SavePackage : public base::RefCountedThreadSafe<SavePackage>, const std::string& data, int32 status); + // Convenience function. + DownloadManager* GetDownloadManager(); + typedef base::hash_map<std::string, SaveItem*> SaveUrlItemMap; // in_progress_items_ is map of all saving job in in-progress state. diff --git a/content/browser/mock_content_browser_client.cc b/content/browser/mock_content_browser_client.cc index a3fa95c..1c68a24 100644 --- a/content/browser/mock_content_browser_client.cc +++ b/content/browser/mock_content_browser_client.cc @@ -219,30 +219,6 @@ void MockContentBrowserClient::ClearCache(RenderViewHost* rvh) { void MockContentBrowserClient::ClearCookies(RenderViewHost* rvh) { } -void MockContentBrowserClient::GetSaveDir(TabContents* tab_contents, - FilePath* website_save_dir, - FilePath* download_save_dir) { -} - -void MockContentBrowserClient::ChooseSavePath( - const base::WeakPtr<SavePackage>& save_package, - const FilePath& suggested_path, - bool can_save_as_complete) { -} - -void MockContentBrowserClient::ChooseDownloadPath( - DownloadManager* download_manager, - TabContents* tab_contents, - const FilePath& suggested_path, - void* data) { -} - -TabContents* - MockContentBrowserClient::GetAlternativeTabContentsToNotifyForDownload( - DownloadManager* download_manager) { - return NULL; -} - #if defined(OS_POSIX) && !defined(OS_MACOSX) int MockContentBrowserClient::GetCrashSignalFD( const std::string& process_type) { diff --git a/content/browser/mock_content_browser_client.h b/content/browser/mock_content_browser_client.h index d31b9d8..b471552 100644 --- a/content/browser/mock_content_browser_client.h +++ b/content/browser/mock_content_browser_client.h @@ -106,19 +106,7 @@ class MockContentBrowserClient : public ContentBrowserClient { virtual void ClearInspectorSettings(RenderViewHost* rvh) OVERRIDE; virtual void BrowserURLHandlerCreated(BrowserURLHandler* handler) OVERRIDE; virtual void ClearCache(RenderViewHost* rvh) OVERRIDE; - virtual void GetSaveDir(TabContents* tab_contents, - FilePath* website_save_dir, - FilePath* download_save_dir) OVERRIDE; virtual void ClearCookies(RenderViewHost* rvh) OVERRIDE; - virtual void ChooseSavePath(const base::WeakPtr<SavePackage>& save_package, - const FilePath& suggested_path, - bool can_save_as_complete) OVERRIDE; - virtual void ChooseDownloadPath(DownloadManager* download_manager, - TabContents* tab_contents, - const FilePath& suggested_path, - void* data) OVERRIDE; - virtual TabContents* GetAlternativeTabContentsToNotifyForDownload( - DownloadManager* download_manager) OVERRIDE; #if defined(OS_POSIX) && !defined(OS_MACOSX) virtual int GetCrashSignalFD(const std::string& process_type) OVERRIDE; |