diff options
author | benjhayden@chromium.org <benjhayden@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-05-04 20:05:14 +0000 |
---|---|---|
committer | benjhayden@chromium.org <benjhayden@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-05-04 20:05:14 +0000 |
commit | c5a5c084acc5c25f14920e559f69dd6ae3267fa5 (patch) | |
tree | 2ba655fefd77c2ea419559a4d8b53e13434f4795 | |
parent | 6ac091784670a4e1391053beae4bf50f0fb1612b (diff) | |
download | chromium_src-c5a5c084acc5c25f14920e559f69dd6ae3267fa5.zip chromium_src-c5a5c084acc5c25f14920e559f69dd6ae3267fa5.tar.gz chromium_src-c5a5c084acc5c25f14920e559f69dd6ae3267fa5.tar.bz2 |
Refactor DownloadManager::DownloadUrl() to allow it to take many parameters.
Review URL: http://codereview.chromium.org/10232010
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@135403 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/download/download_browsertest.cc | 21 | ||||
-rw-r--r-- | chrome/browser/download/download_test_observer.cc | 3 | ||||
-rw-r--r-- | chrome/browser/download/download_test_observer.h | 3 | ||||
-rw-r--r-- | chrome/browser/extensions/webstore_installer.cc | 18 | ||||
-rw-r--r-- | chrome/browser/tab_contents/render_view_context_menu.cc | 43 | ||||
-rw-r--r-- | content/browser/download/download_manager_impl.cc | 109 | ||||
-rw-r--r-- | content/browser/download/download_manager_impl.h | 10 | ||||
-rw-r--r-- | content/browser/download/download_resource_handler.h | 5 | ||||
-rw-r--r-- | content/browser/download/drag_download_file.cc | 15 | ||||
-rw-r--r-- | content/browser/web_contents/web_contents_impl.cc | 20 | ||||
-rw-r--r-- | content/content_browser.gypi | 2 | ||||
-rw-r--r-- | content/public/browser/download_manager.h | 33 | ||||
-rw-r--r-- | content/public/browser/download_url_parameters.cc | 53 | ||||
-rw-r--r-- | content/public/browser/download_url_parameters.h | 132 | ||||
-rw-r--r-- | content/test/mock_download_manager.h | 14 |
15 files changed, 313 insertions, 168 deletions
diff --git a/chrome/browser/download/download_browsertest.cc b/chrome/browser/download/download_browsertest.cc index 9f33bfd..5dd298f 100644 --- a/chrome/browser/download/download_browsertest.cc +++ b/chrome/browser/download/download_browsertest.cc @@ -48,6 +48,7 @@ #include "content/public/browser/download_manager.h" #include "content/public/browser/download_persistent_store_info.h" #include "content/public/browser/download_save_info.h" +#include "content/public/browser/download_url_parameters.h" #include "content/public/browser/notification_source.h" #include "content/public/browser/render_view_host.h" #include "content/public/browser/resource_context.h" @@ -66,6 +67,7 @@ using content::BrowserThread; using content::DownloadItem; using content::DownloadManager; using content::DownloadPersistentStoreInfo; +using content::DownloadUrlParameters; using content::WebContents; namespace { @@ -667,9 +669,10 @@ class DownloadTest : public InProcessBrowserTest { scoped_refptr<DownloadTestItemCreationObserver> creation_observer( new DownloadTestItemCreationObserver); - DownloadManagerForBrowser(browser())->DownloadUrl( - url, GURL(""), "", false, -1, save_info, web_contents, - creation_observer->callback()); + scoped_ptr<DownloadUrlParameters> params( + DownloadUrlParameters::FromWebContents(web_contents, url, save_info)); + params->set_callback(creation_observer->callback()); + DownloadManagerForBrowser(browser())->DownloadUrl(params.Pass()); // Wait until the item is created, or we have determined that it // won't be. @@ -1935,9 +1938,9 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadUrl) { DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL)); content::DownloadSaveInfo save_info; save_info.prompt_for_save_location = true; - DownloadManagerForBrowser(browser())->DownloadUrl( - url, GURL(""), "", false, -1, save_info, web_contents, - DownloadManager::OnStartedCallback()); + scoped_ptr<DownloadUrlParameters> params( + DownloadUrlParameters::FromWebContents(web_contents, url, save_info)); + DownloadManagerForBrowser(browser())->DownloadUrl(params.Pass()); observer->WaitForFinished(); EXPECT_EQ(1u, observer->NumDownloadsSeenInState(DownloadItem::COMPLETE)); CheckDownloadStates(1, DownloadItem::COMPLETE); @@ -1964,9 +1967,9 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadUrlToPath) { save_info.file_path = target_file_full_path; DownloadTestObserver* observer(CreateWaiter(browser(), 1)); - DownloadManagerForBrowser(browser())->DownloadUrl( - url, GURL(""), "", false, -1, save_info, web_contents, - DownloadManager::OnStartedCallback()); + scoped_ptr<DownloadUrlParameters> params( + DownloadUrlParameters::FromWebContents(web_contents, url, save_info)); + DownloadManagerForBrowser(browser())->DownloadUrl(params.Pass()); observer->WaitForFinished(); EXPECT_EQ(1u, observer->NumDownloadsSeenInState(DownloadItem::COMPLETE)); diff --git a/chrome/browser/download/download_test_observer.cc b/chrome/browser/download/download_test_observer.cc index 5624128..13d6791 100644 --- a/chrome/browser/download/download_test_observer.cc +++ b/chrome/browser/download/download_test_observer.cc @@ -11,6 +11,7 @@ #include "chrome/browser/download/download_test_observer.h" #include "chrome/test/base/ui_test_utils.h" #include "content/public/browser/browser_thread.h" +#include "content/public/browser/download_url_parameters.h" using content::BrowserThread; using content::DownloadItem; @@ -394,7 +395,7 @@ void DownloadTestItemCreationObserver::DownloadItemCreationCallback( MessageLoopForUI::current()->Quit(); } -const content::DownloadManager::OnStartedCallback +const content::DownloadUrlParameters::OnStartedCallback DownloadTestItemCreationObserver::callback() { return base::Bind( &DownloadTestItemCreationObserver::DownloadItemCreationCallback, this); diff --git a/chrome/browser/download/download_test_observer.h b/chrome/browser/download/download_test_observer.h index 1be27be..d8ea685 100644 --- a/chrome/browser/download/download_test_observer.h +++ b/chrome/browser/download/download_test_observer.h @@ -13,6 +13,7 @@ #include "base/memory/ref_counted.h" #include "content/public/browser/download_item.h" #include "content/public/browser/download_manager.h" +#include "content/public/browser/download_url_parameters.h" #include "net/base/net_errors.h" // Detects changes to the downloads after construction. @@ -255,7 +256,7 @@ class DownloadTestItemCreationObserver bool started() const { return called_back_count_ > 0; } bool succeeded() const { return started() && (error_ == net::OK); } - const content::DownloadManager::OnStartedCallback callback(); + const content::DownloadUrlParameters::OnStartedCallback callback(); private: friend class base::RefCountedThreadSafe<DownloadTestItemCreationObserver>; diff --git a/chrome/browser/extensions/webstore_installer.cc b/chrome/browser/extensions/webstore_installer.cc index da62be7..f86eba4 100644 --- a/chrome/browser/extensions/webstore_installer.cc +++ b/chrome/browser/extensions/webstore_installer.cc @@ -9,9 +9,9 @@ #include "base/command_line.h" #include "base/file_util.h" #include "base/rand_util.h" -#include "base/stringprintf.h" -#include "base/string_util.h" #include "base/string_number_conversions.h" +#include "base/string_util.h" +#include "base/stringprintf.h" #include "base/utf_string_conversions.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/download/chrome_download_manager_delegate.h" @@ -29,6 +29,7 @@ #include "content/public/browser/browser_thread.h" #include "content/public/browser/download_manager.h" #include "content/public/browser/download_save_info.h" +#include "content/public/browser/download_url_parameters.h" #include "content/public/browser/navigation_controller.h" #include "content/public/browser/navigation_entry.h" #include "content/public/browser/notification_details.h" @@ -41,6 +42,7 @@ using content::BrowserThread; using content::DownloadId; using content::DownloadItem; using content::NavigationController; +using content::DownloadUrlParameters; namespace { @@ -299,11 +301,13 @@ void WebstoreInstaller::StartDownload(const FilePath& file) { // We will navigate the current tab to this url to start the download. The // download system will then pass the crx to the CrxInstaller. download_util::RecordDownloadSource( - download_util::INITIATED_BY_WEBSTORE_INSTALLER); - profile_->GetDownloadManager()->DownloadUrl( - download_url_, referrer, "", - false, -1, save_info, controller_->GetWebContents(), - base::Bind(&WebstoreInstaller::OnDownloadStarted, this)); + download_util::INITIATED_BY_WEBSTORE_INSTALLER); + scoped_ptr<DownloadUrlParameters> params( + DownloadUrlParameters::FromWebContents( + controller_->GetWebContents(), download_url_, save_info)); + params->set_referrer(referrer); + params->set_callback(base::Bind(&WebstoreInstaller::OnDownloadStarted, this)); + profile_->GetDownloadManager()->DownloadUrl(params.Pass()); } void WebstoreInstaller::ReportFailure(const std::string& error) { diff --git a/chrome/browser/tab_contents/render_view_context_menu.cc b/chrome/browser/tab_contents/render_view_context_menu.cc index 9bf97a0..de05fcf 100644 --- a/chrome/browser/tab_contents/render_view_context_menu.cc +++ b/chrome/browser/tab_contents/render_view_context_menu.cc @@ -62,6 +62,7 @@ #include "content/public/browser/child_process_security_policy.h" #include "content/public/browser/download_manager.h" #include "content/public/browser/download_save_info.h" +#include "content/public/browser/download_url_parameters.h" #include "content/public/browser/navigation_details.h" #include "content/public/browser/navigation_entry.h" #include "content/public/browser/notification_service.h" @@ -86,8 +87,14 @@ #include "chrome/browser/chromeos/extensions/file_manager_util.h" #endif +using WebKit::WebContextMenuData; +using WebKit::WebMediaPlayerAction; +using WebKit::WebPluginAction; +using WebKit::WebString; +using WebKit::WebURL; using content::ChildProcessSecurityPolicy; using content::DownloadManager; +using content::DownloadUrlParameters; using content::NavigationController; using content::NavigationEntry; using content::OpenURLParams; @@ -95,11 +102,6 @@ using content::RenderViewHost; using content::SSLStatus; using content::UserMetricsAction; using content::WebContents; -using WebKit::WebContextMenuData; -using WebKit::WebMediaPlayerAction; -using WebKit::WebPluginAction; -using WebKit::WebURL; -using WebKit::WebString; namespace { @@ -1517,14 +1519,12 @@ void RenderViewContextMenu::ExecuteCommand(int id, int event_flags) { save_info.prompt_for_save_location = true; DownloadManager* dlm = DownloadServiceFactory::GetForProfile(profile_)->GetDownloadManager(); - dlm->DownloadUrl(url, - referrer, - params_.frame_charset, - false, // Don't prefer_cache - -1, // No POST id - save_info, - source_web_contents_, - DownloadManager::OnStartedCallback()); + scoped_ptr<DownloadUrlParameters> dl_params( + DownloadUrlParameters::FromWebContents( + source_web_contents_, url, save_info)); + dl_params->set_referrer(referrer); + dl_params->set_referrer_encoding(params_.frame_charset); + dlm->DownloadUrl(dl_params.Pass()); break; } @@ -1546,14 +1546,15 @@ void RenderViewContextMenu::ExecuteCommand(int id, int event_flags) { } DownloadManager* dlm = DownloadServiceFactory::GetForProfile(profile_)->GetDownloadManager(); - dlm->DownloadUrl(url, - referrer, - "", - true, // prefer_cache - post_id, - save_info, - source_web_contents_, - DownloadManager::OnStartedCallback()); + scoped_ptr<DownloadUrlParameters> dl_params( + DownloadUrlParameters::FromWebContents( + source_web_contents_, url, save_info)); + dl_params->set_referrer(referrer); + dl_params->set_post_id(post_id); + dl_params->set_prefer_cache(true); + if (post_id >= 0) + dl_params->set_method("POST"); + dlm->DownloadUrl(dl_params.Pass()); break; } diff --git a/content/browser/download/download_manager_impl.cc b/content/browser/download/download_manager_impl.cc index afea786..f677092 100644 --- a/content/browser/download/download_manager_impl.cc +++ b/content/browser/download/download_manager_impl.cc @@ -30,10 +30,12 @@ #include "content/public/browser/download_interrupt_reasons.h" #include "content/public/browser/download_manager_delegate.h" #include "content/public/browser/download_persistent_store_info.h" +#include "content/public/browser/download_url_parameters.h" #include "content/public/browser/notification_service.h" #include "content/public/browser/notification_types.h" #include "content/public/browser/render_process_host.h" #include "content/public/browser/web_contents_delegate.h" +#include "net/base/load_flags.h" #include "net/base/upload_data.h" using content::BrowserThread; @@ -45,52 +47,48 @@ using content::WebContents; namespace { -// Param structs exist because base::Bind can only handle 6 args. -struct URLParams { - URLParams(const GURL& url, const GURL& referrer, int64 post_id, bool cache) - : url_(url), referrer_(referrer), post_id_(post_id), prefer_cache_(cache) {} - GURL url_; - GURL referrer_; - int64 post_id_; - bool prefer_cache_; -}; - -struct RenderParams { - RenderParams(int rpi, int rvi) - : render_process_id_(rpi), render_view_id_(rvi) {} - int render_process_id_; - int render_view_id_; -}; - -void BeginDownload( - const URLParams& url_params, - const content::DownloadSaveInfo& save_info, - ResourceDispatcherHostImpl* resource_dispatcher_host, - const RenderParams& render_params, - content::ResourceContext* context, - const content::DownloadManager::OnStartedCallback& callback) { - scoped_ptr<net::URLRequest> request( - new net::URLRequest(url_params.url_, resource_dispatcher_host)); - request->set_referrer(url_params.referrer_.spec()); - if (url_params.post_id_ >= 0) { +void BeginDownload(content::DownloadUrlParameters* params) { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); + // ResourceDispatcherHost{Base} is-not-a URLRequest::Delegate, and + // DownloadUrlParameters can-not include resource_dispatcher_host_impl.h, so + // we must down cast. RDHI is the only subclass of RDH as of 2012 May 4. + content::ResourceDispatcherHostImpl* resource_dispatcher_host = + static_cast<content::ResourceDispatcherHostImpl*>( + params->resource_dispatcher_host()); + scoped_ptr<net::URLRequest> request(new net::URLRequest( + params->url(), resource_dispatcher_host)); + request->set_referrer(params->referrer().spec()); + request->set_load_flags(request->load_flags() | params->load_flags()); + request->set_method(params->method()); + if (!params->post_body().empty()) + request->AppendBytesToUpload(params->post_body().data(), + params->post_body().size()); + if (params->post_id() >= 0) { // The POST in this case does not have an actual body, and only works // when retrieving data from cache. This is done because we don't want // to do a re-POST without user consent, and currently don't have a good // plan on how to display the UI for that. - DCHECK(url_params.prefer_cache_); - request->set_method("POST"); + DCHECK(params->prefer_cache()); + DCHECK(params->method() == "POST"); scoped_refptr<net::UploadData> upload_data = new net::UploadData(); - upload_data->set_identifier(url_params.post_id_); + upload_data->set_identifier(params->post_id()); request->set_upload(upload_data); } + for (content::DownloadUrlParameters::RequestHeadersType::const_iterator iter + = params->request_headers_begin(); + iter != params->request_headers_end(); + ++iter) { + request->SetExtraRequestHeaderByName( + iter->first, iter->second, false/*overwrite*/); + } resource_dispatcher_host->BeginDownload( request.Pass(), - context, - render_params.render_process_id_, - render_params.render_view_id_, - url_params.prefer_cache_, - save_info, - callback); + params->resource_context(), + params->render_process_host_id(), + params->render_view_host_routing_id(), + params->prefer_cache(), + params->save_info(), + params->callback()); } class MapValueIteratorAdapter { @@ -855,36 +853,15 @@ int DownloadManagerImpl::RemoveAllDownloads() { return RemoveDownloadsBetween(base::Time(), base::Time()); } -// Initiate a download of a specific URL. We send the request to the -// ResourceDispatcherHost, and let it send us responses like a regular -// download. void DownloadManagerImpl::DownloadUrl( - const GURL& url, - const GURL& referrer, - const std::string& referrer_charset, - bool prefer_cache, - int64 post_id, - const content::DownloadSaveInfo& save_info, - WebContents* web_contents, - const OnStartedCallback& callback) { - ResourceDispatcherHostImpl* resource_dispatcher_host = - ResourceDispatcherHostImpl::Get(); - DCHECK(resource_dispatcher_host); - - // We send a pointer to content::ResourceContext, instead of the usual - // reference, so that a copy of the object isn't made. - // base::Bind can't handle 7 args, so we use URLParams and RenderParams. - BrowserThread::PostTask( - BrowserThread::IO, FROM_HERE, - base::Bind( - &BeginDownload, - URLParams(url, referrer, post_id, prefer_cache), - save_info, - resource_dispatcher_host, - RenderParams(web_contents->GetRenderProcessHost()->GetID(), - web_contents->GetRenderViewHost()->GetRoutingID()), - web_contents->GetBrowserContext()->GetResourceContext(), - callback)); + scoped_ptr<content::DownloadUrlParameters> params) { + if (params->post_id() >= 0) { + // Check this here so that the traceback is more useful. + DCHECK(params->prefer_cache()); + DCHECK(params->method() == "POST"); + } + BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( + &BeginDownload, base::Owned(params.release()))); } void DownloadManagerImpl::AddObserver(Observer* observer) { diff --git a/content/browser/download/download_manager_impl.h b/content/browser/download/download_manager_impl.h index 16d6914..bc1cc1b 100644 --- a/content/browser/download/download_manager_impl.h +++ b/content/browser/download/download_manager_impl.h @@ -57,14 +57,8 @@ class CONTENT_EXPORT DownloadManagerImpl base::Time remove_end) OVERRIDE; virtual int RemoveDownloads(base::Time remove_begin) OVERRIDE; virtual int RemoveAllDownloads() OVERRIDE; - virtual void DownloadUrl(const GURL& url, - const GURL& referrer, - const std::string& referrer_encoding, - bool prefer_cache, - int64 post_id, - const content::DownloadSaveInfo& save_info, - content::WebContents* web_contents, - const OnStartedCallback& callback) OVERRIDE; + virtual void DownloadUrl( + scoped_ptr<content::DownloadUrlParameters> params) OVERRIDE; virtual void AddObserver(Observer* observer) OVERRIDE; virtual void RemoveObserver(Observer* observer) OVERRIDE; virtual void OnPersistentStoreQueryComplete( diff --git a/content/browser/download/download_resource_handler.h b/content/browser/download/download_resource_handler.h index 3a9a5c7..ea14626 100644 --- a/content/browser/download/download_resource_handler.h +++ b/content/browser/download/download_resource_handler.h @@ -12,9 +12,10 @@ #include "base/memory/scoped_ptr.h" #include "base/timer.h" #include "content/browser/renderer_host/resource_handler.h" -#include "content/public/browser/download_manager.h" #include "content/public/browser/download_id.h" +#include "content/public/browser/download_manager.h" #include "content/public/browser/download_save_info.h" +#include "content/public/browser/download_url_parameters.h" #include "content/public/browser/global_request_id.h" #include "net/base/net_errors.h" @@ -33,7 +34,7 @@ class URLRequest; // Forwards data to the download thread. class DownloadResourceHandler : public ResourceHandler { public: - typedef content::DownloadManager::OnStartedCallback OnStartedCallback; + typedef content::DownloadUrlParameters::OnStartedCallback OnStartedCallback; static const size_t kLoadsToWrite = 100; // number of data buffers queued diff --git a/content/browser/download/drag_download_file.cc b/content/browser/download/drag_download_file.cc index cb9a2fd..fe9281e 100644 --- a/content/browser/download/drag_download_file.cc +++ b/content/browser/download/drag_download_file.cc @@ -13,11 +13,13 @@ #include "content/public/browser/browser_thread.h" #include "content/public/browser/download_item.h" #include "content/public/browser/download_save_info.h" +#include "content/public/browser/download_url_parameters.h" #include "net/base/file_stream.h" using content::BrowserThread; using content::DownloadItem; using content::DownloadManager; +using content::DownloadUrlParameters; using content::WebContents; DragDownloadFile::DragDownloadFile( @@ -135,14 +137,11 @@ void DragDownloadFile::InitiateDownload() { download_stats::RecordDownloadSource( download_stats::INITIATED_BY_DRAG_N_DROP); - download_manager_->DownloadUrl(url_, - referrer_, - referrer_encoding_, - false, - -1, - save_info, - web_contents_, - DownloadManager::OnStartedCallback()); + scoped_ptr<DownloadUrlParameters> params( + DownloadUrlParameters::FromWebContents(web_contents_, url_, save_info)); + params->set_referrer(referrer_); + params->set_referrer_encoding(referrer_encoding_); + download_manager_->DownloadUrl(params.Pass()); } void DragDownloadFile::DownloadCompleted(bool is_successful) { diff --git a/content/browser/web_contents/web_contents_impl.cc b/content/browser/web_contents/web_contents_impl.cc index 8faf0f8..a8abbc6f1 100644 --- a/content/browser/web_contents/web_contents_impl.cc +++ b/content/browser/web_contents/web_contents_impl.cc @@ -42,6 +42,7 @@ #include "content/public/browser/content_browser_client.h" #include "content/public/browser/devtools_agent_host_registry.h" #include "content/public/browser/download_manager.h" +#include "content/public/browser/download_url_parameters.h" #include "content/public/browser/invalidate_type.h" #include "content/public/browser/javascript_dialogs.h" #include "content/public/browser/load_notification_details.h" @@ -129,6 +130,7 @@ using content::DevToolsAgentHostRegistry; using content::DevToolsManagerImpl; using content::DownloadItem; using content::DownloadManager; +using content::DownloadUrlParameters; using content::GlobalRequestID; using content::HostZoomMap; using content::InterstitialPage; @@ -144,9 +146,9 @@ using content::RenderWidgetHost; using content::RenderWidgetHostView; using content::RenderWidgetHostViewPort; using content::ResourceDispatcherHostImpl; +using content::SSLStatus; using content::SessionStorageNamespace; using content::SiteInstance; -using content::SSLStatus; using content::UserMetricsAction; using content::WebContents; using content::WebContentsObserver; @@ -2694,14 +2696,14 @@ void WebContentsImpl::SaveURL(const GURL& url, } content::DownloadSaveInfo save_info; save_info.prompt_for_save_location = true; - dlm->DownloadUrl(url, - referrer, - "", - true, // prefer_cache - post_id, - save_info, - this, - DownloadManager::OnStartedCallback()); + scoped_ptr<DownloadUrlParameters> params( + DownloadUrlParameters::FromWebContents(this, url, save_info)); + params->set_referrer(referrer); + params->set_post_id(post_id); + params->set_prefer_cache(true); + if (post_id >= 0) + params->set_method("POST"); + dlm->DownloadUrl(params.Pass()); } void WebContentsImpl::CreateViewAndSetSizeForRVH(RenderViewHost* rvh) { diff --git a/content/content_browser.gypi b/content/content_browser.gypi index 0fe3caa..62df23b 100644 --- a/content/content_browser.gypi +++ b/content/content_browser.gypi @@ -71,6 +71,8 @@ 'public/browser/download_persistent_store_info.h', 'public/browser/download_save_info.cc', 'public/browser/download_save_info.h', + 'public/browser/download_url_parameters.cc', + 'public/browser/download_url_parameters.h', 'public/browser/favicon_status.cc', 'public/browser/favicon_status.h', 'public/browser/font_list_async.h', diff --git a/content/public/browser/download_manager.h b/content/public/browser/download_manager.h index a7a3999..b0039bd 100644 --- a/content/public/browser/download_manager.h +++ b/content/public/browser/download_manager.h @@ -37,12 +37,12 @@ #include "base/gtest_prod_util.h" #include "base/message_loop_helpers.h" #include "base/time.h" +#include "content/public/browser/browser_thread.h" #include "content/public/browser/download_id.h" #include "content/public/browser/download_interrupt_reasons.h" #include "content/public/browser/download_item.h" -#include "content/public/browser/browser_thread.h" -#include "net/base/net_log.h" #include "net/base/net_errors.h" +#include "net/base/net_log.h" class DownloadRequestHandle; class GURL; @@ -50,19 +50,16 @@ struct DownloadCreateInfo; struct DownloadRetrieveInfo; namespace content { + class BrowserContext; class DownloadManagerDelegate; class DownloadQuery; -class WebContents; -struct DownloadSaveInfo; +class DownloadUrlParameters; // Browser's download manager: manages all downloads and destination view. class CONTENT_EXPORT DownloadManager : public base::RefCountedThreadSafe<DownloadManager> { public: - // NOTE: If there is an error, the DownloadId will be invalid. - typedef base::Callback<void(DownloadId, net::Error)> OnStartedCallback; - virtual ~DownloadManager() {} static DownloadManager* Create( @@ -172,26 +169,8 @@ class CONTENT_EXPORT DownloadManager // deleted is returned back to the caller. virtual int RemoveAllDownloads() = 0; - // Downloads the content at |url|. |referrer| and |referrer_encoding| are the - // referrer for the download, and may be empty. If |prefer_cache| is true, - // then if the response to |url| is in the HTTP cache it will be used without - // revalidation. If |post_id| is non-negative, then it identifies the post - // transaction used to originally retrieve the |url| resource - it also - // requires |prefer_cache| to be |true| since re-post'ing is not done. - // |save_info| specifies where the downloaded file should be - // saved, and whether the user should be prompted about the download. - // |web_contents| is the web page that the download is done in context of, - // and must be non-NULL. - // |callback| will be called when the download starts, or if an error - // occurs that prevents a download item from being created. - virtual void DownloadUrl(const GURL& url, - const GURL& referrer, - const std::string& referrer_encoding, - bool prefer_cache, - int64 post_id, - const DownloadSaveInfo& save_info, - WebContents* web_contents, - const OnStartedCallback& callback) = 0; + // See DownloadUrlParameters for details about controlling the download. + virtual void DownloadUrl(scoped_ptr<DownloadUrlParameters> parameters) = 0; // Allow objects to observe the download creation process. virtual void AddObserver(Observer* observer) = 0; diff --git a/content/public/browser/download_url_parameters.cc b/content/public/browser/download_url_parameters.cc new file mode 100644 index 0000000..64102fa --- /dev/null +++ b/content/public/browser/download_url_parameters.cc @@ -0,0 +1,53 @@ +// Copyright (c) 2012 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 "content/public/browser/download_url_parameters.h" + +#include "base/callback.h" +#include "content/public/browser/browser_context.h" +#include "content/public/browser/download_save_info.h" +#include "content/public/browser/render_process_host.h" +#include "content/public/browser/render_view_host.h" +#include "content/public/browser/resource_dispatcher_host.h" +#include "content/public/browser/web_contents.h" +#include "googleurl/src/gurl.h" + +namespace content { + +DownloadUrlParameters::DownloadUrlParameters( + const GURL& url, + int render_process_host_id, + int render_view_host_routing_id, + ResourceContext* resource_context, + const DownloadSaveInfo& save_info) + : load_flags_(0), + method_("GET"), + post_id_(-1), + prefer_cache_(false), + render_process_host_id_(render_process_host_id), + render_view_host_routing_id_(render_view_host_routing_id), + resource_context_(resource_context), + resource_dispatcher_host_(ResourceDispatcherHost::Get()), + save_info_(save_info), + url_(url) { + DCHECK(resource_dispatcher_host_); +} + +DownloadUrlParameters::~DownloadUrlParameters() { +} + +// static +DownloadUrlParameters* DownloadUrlParameters::FromWebContents( + WebContents* web_contents, + const GURL& url, + const DownloadSaveInfo& save_info) { + return new DownloadUrlParameters( + url, + web_contents->GetRenderProcessHost()->GetID(), + web_contents->GetRenderViewHost()->GetRoutingID(), + web_contents->GetBrowserContext()->GetResourceContext(), + save_info); +} + +} // namespace content diff --git a/content/public/browser/download_url_parameters.h b/content/public/browser/download_url_parameters.h new file mode 100644 index 0000000..40cec1c --- /dev/null +++ b/content/public/browser/download_url_parameters.h @@ -0,0 +1,132 @@ +// Copyright (c) 2012 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 CONTENT_PUBLIC_BROWSER_DOWNLOAD_URL_PARAMETERS_H_ +#define CONTENT_PUBLIC_BROWSER_DOWNLOAD_URL_PARAMETERS_H_ +#pragma once + +#include <string> +#include <vector> + +#include "base/basictypes.h" +#include "base/callback.h" +#include "content/public/browser/download_id.h" +#include "content/public/browser/download_save_info.h" +#include "googleurl/src/gurl.h" +#include "net/base/net_errors.h" + +namespace content { + +class ResourceContext; +class ResourceDispatcherHost; +class WebContents; + +// Pass an instance of DownloadUrlParameters to DownloadManager::DownloadUrl() +// to download the content at |url|. All parameters with setters are optional. +// |referrer| and |referrer_encoding| are the referrer for the download. If +// |prefer_cache| is true, then if the response to |url| is in the HTTP cache it +// will be used without revalidation. If |post_id| is non-negative, then it +// identifies the post transaction used to originally retrieve the |url| +// resource - it also requires |prefer_cache| to be |true| since re-post'ing is +// not done. |save_info| specifies where the downloaded file should be saved, +// and whether the user should be prompted about the download. If not null, +// |callback| will be called when the download starts, or if an error occurs +// that prevents a download item from being created. We send a pointer to +// content::ResourceContext instead of the usual reference so that a copy of the +// object isn't made. + +class CONTENT_EXPORT DownloadUrlParameters { + public: + // If there is an error, the DownloadId will be invalid. + typedef base::Callback<void(DownloadId, net::Error)> OnStartedCallback; + + typedef std::pair<std::string, std::string> RequestHeadersNameValuePair; + typedef std::vector<RequestHeadersNameValuePair> RequestHeadersType; + + static DownloadUrlParameters* FromWebContents( + WebContents* web_contents, + const GURL& url, + const DownloadSaveInfo& save_info); + + DownloadUrlParameters( + const GURL& url, + int render_process_host_id, + int render_view_host_routing_id, + content::ResourceContext* resource_context, + const DownloadSaveInfo& save_info); + + ~DownloadUrlParameters(); + + void add_request_header(const std::string& name, const std::string& value) { + request_headers_.push_back(make_pair(name, value)); + } + void set_referrer(const GURL& referrer) { referrer_ = referrer; } + void set_referrer_encoding(const std::string& referrer_encoding) { + referrer_encoding_ = referrer_encoding; + } + void set_load_flags(int load_flags) { load_flags_ |= load_flags; } + void set_method(const std::string& method) { + method_ = method; + } + void set_post_body(const std::string& post_body) { + post_body_ = post_body; + } + void set_prefer_cache(bool prefer_cache) { + prefer_cache_ = prefer_cache; + } + void set_post_id(int64 post_id) { post_id_ = post_id; } + void set_callback(const OnStartedCallback& callback) { + callback_ = callback; + } + + const OnStartedCallback& callback() const { return callback_; } + int load_flags() const { return load_flags_; } + const std::string& method() const { return method_; } + const std::string& post_body() const { return post_body_; } + int64 post_id() const { return post_id_; } + bool prefer_cache() const { return prefer_cache_; } + const GURL& referrer() const { return referrer_; } + const std::string& referrer_encoding() const { return referrer_encoding_; } + int render_process_host_id() const { return render_process_host_id_; } + int render_view_host_routing_id() const { + return render_view_host_routing_id_; + } + RequestHeadersType::const_iterator request_headers_begin() const { + return request_headers_.begin(); + } + RequestHeadersType::const_iterator request_headers_end() const { + return request_headers_.end(); + } + content::ResourceContext* resource_context() const { + return resource_context_; + } + ResourceDispatcherHost* resource_dispatcher_host() const { + return resource_dispatcher_host_; + } + const DownloadSaveInfo& save_info() const { return save_info_; } + const GURL& url() const { return url_; } + + private: + OnStartedCallback callback_; + RequestHeadersType request_headers_; + int load_flags_; + std::string method_; + std::string post_body_; + int64 post_id_; + bool prefer_cache_; + GURL referrer_; + std::string referrer_encoding_; + int render_process_host_id_; + int render_view_host_routing_id_; + ResourceContext* resource_context_; + ResourceDispatcherHost* resource_dispatcher_host_; + DownloadSaveInfo save_info_; + GURL url_; + + DISALLOW_COPY_AND_ASSIGN(DownloadUrlParameters); +}; + +} // namespace content + +#endif // CONTENT_PUBLIC_BROWSER_DOWNLOAD_URL_PARAMETERS_H_ diff --git a/content/test/mock_download_manager.h b/content/test/mock_download_manager.h index e1c5143..4fb8512 100644 --- a/content/test/mock_download_manager.h +++ b/content/test/mock_download_manager.h @@ -11,6 +11,7 @@ #include "content/public/browser/download_item.h" #include "content/public/browser/download_manager.h" #include "content/public/browser/download_save_info.h" +#include "content/public/browser/download_url_parameters.h" #include "googleurl/src/gurl.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" @@ -52,15 +53,10 @@ class MockDownloadManager : public content::DownloadManager { base::Time remove_end)); MOCK_METHOD1(RemoveDownloads, int(base::Time remove_begin)); MOCK_METHOD0(RemoveAllDownloads, int()); - MOCK_METHOD8(DownloadUrl, - void(const GURL& url, - const GURL& referrer, - const std::string& referrer_encoding, - bool prefer_cache, - int64 post_id, - const DownloadSaveInfo& save_info, - content::WebContents* web_contents, - const DownloadManager::OnStartedCallback& callback)); + MOCK_METHOD1(DownloadUrlMock, void(DownloadUrlParameters*)); + virtual void DownloadUrl(scoped_ptr<DownloadUrlParameters> params) OVERRIDE { + DownloadUrlMock(params.get()); + } MOCK_METHOD1(AddObserver, void(Observer* observer)); MOCK_METHOD1(RemoveObserver, void(Observer* observer)); MOCK_METHOD1(OnPersistentStoreQueryComplete, void( |