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 3 // found in the LICENSE file. 4 5 #ifndef CONTENT_PUBLIC_BROWSER_DOWNLOAD_URL_PARAMETERS_H_ 6 #define CONTENT_PUBLIC_BROWSER_DOWNLOAD_URL_PARAMETERS_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/basictypes.h" 12 #include "base/callback.h" 13 #include "content/public/browser/download_interrupt_reasons.h" 14 #include "content/public/browser/download_save_info.h" 15 #include "content/public/common/referrer.h" 16 #include "url/gurl.h" 17 18 namespace content { 19 20 class DownloadItem; 21 class ResourceContext; 22 class ResourceDispatcherHost; 23 class WebContents; 24 25 // Pass an instance of DownloadUrlParameters to DownloadManager::DownloadUrl() 26 // to download the content at |url|. All parameters with setters are optional. 27 // |referrer| and |referrer_encoding| are the referrer for the download. If 28 // |prefer_cache| is true, then if the response to |url| is in the HTTP cache it 29 // will be used without revalidation. If |post_id| is non-negative, then it 30 // identifies the post transaction used to originally retrieve the |url| 31 // resource - it also requires |prefer_cache| to be |true| since re-post'ing is 32 // not done. |save_info| specifies where the downloaded file should be saved, 33 // and whether the user should be prompted about the download. If not null, 34 // |callback| will be called when the download starts, or if an error occurs 35 // that prevents a download item from being created. We send a pointer to 36 // content::ResourceContext instead of the usual reference so that a copy of the 37 // object isn't made. 38 39 class CONTENT_EXPORT DownloadUrlParameters { 40 public: 41 // If there is an error, then |item| will be NULL. 42 typedef base::Callback<void(DownloadItem*, DownloadInterruptReason)> 43 OnStartedCallback; 44 45 typedef std::pair<std::string, std::string> RequestHeadersNameValuePair; 46 typedef std::vector<RequestHeadersNameValuePair> RequestHeadersType; 47 48 static DownloadUrlParameters* FromWebContents( 49 WebContents* web_contents, 50 const GURL& url); 51 52 DownloadUrlParameters( 53 const GURL& url, 54 int render_process_host_id, 55 int render_view_host_routing_id, 56 content::ResourceContext* resource_context); 57 58 ~DownloadUrlParameters(); 59 set_content_initiated(bool content_initiated)60 void set_content_initiated(bool content_initiated) { 61 content_initiated_ = content_initiated; 62 } add_request_header(const std::string & name,const std::string & value)63 void add_request_header(const std::string& name, const std::string& value) { 64 request_headers_.push_back(make_pair(name, value)); 65 } set_referrer(const Referrer & referrer)66 void set_referrer(const Referrer& referrer) { referrer_ = referrer; } set_referrer_encoding(const std::string & referrer_encoding)67 void set_referrer_encoding(const std::string& referrer_encoding) { 68 referrer_encoding_ = referrer_encoding; 69 } set_load_flags(int load_flags)70 void set_load_flags(int load_flags) { load_flags_ |= load_flags; } set_last_modified(const std::string & last_modified)71 void set_last_modified(const std::string& last_modified) { 72 last_modified_ = last_modified; 73 } set_etag(const std::string & etag)74 void set_etag(const std::string& etag) { 75 etag_ = etag; 76 } set_method(const std::string & method)77 void set_method(const std::string& method) { 78 method_ = method; 79 } set_post_body(const std::string & post_body)80 void set_post_body(const std::string& post_body) { 81 post_body_ = post_body; 82 } set_prefer_cache(bool prefer_cache)83 void set_prefer_cache(bool prefer_cache) { 84 prefer_cache_ = prefer_cache; 85 } set_post_id(int64 post_id)86 void set_post_id(int64 post_id) { post_id_ = post_id; } set_callback(const OnStartedCallback & callback)87 void set_callback(const OnStartedCallback& callback) { 88 callback_ = callback; 89 } set_file_path(const base::FilePath & file_path)90 void set_file_path(const base::FilePath& file_path) { 91 save_info_.file_path = file_path; 92 } set_suggested_name(const base::string16 & suggested_name)93 void set_suggested_name(const base::string16& suggested_name) { 94 save_info_.suggested_name = suggested_name; 95 } set_offset(int64 offset)96 void set_offset(int64 offset) { save_info_.offset = offset; } set_hash_state(std::string hash_state)97 void set_hash_state(std::string hash_state) { 98 save_info_.hash_state = hash_state; 99 } set_prompt(bool prompt)100 void set_prompt(bool prompt) { save_info_.prompt_for_save_location = prompt; } set_file(base::File file)101 void set_file(base::File file) { 102 save_info_.file = file.Pass(); 103 } 104 callback()105 const OnStartedCallback& callback() const { return callback_; } content_initiated()106 bool content_initiated() const { return content_initiated_; } load_flags()107 int load_flags() const { return load_flags_; } last_modified()108 const std::string& last_modified() const { return last_modified_; } etag()109 const std::string& etag() const { return etag_; } method()110 const std::string& method() const { return method_; } post_body()111 const std::string& post_body() const { return post_body_; } post_id()112 int64 post_id() const { return post_id_; } prefer_cache()113 bool prefer_cache() const { return prefer_cache_; } referrer()114 const Referrer& referrer() const { return referrer_; } referrer_encoding()115 const std::string& referrer_encoding() const { return referrer_encoding_; } render_process_host_id()116 int render_process_host_id() const { return render_process_host_id_; } render_view_host_routing_id()117 int render_view_host_routing_id() const { 118 return render_view_host_routing_id_; 119 } request_headers_begin()120 RequestHeadersType::const_iterator request_headers_begin() const { 121 return request_headers_.begin(); 122 } request_headers_end()123 RequestHeadersType::const_iterator request_headers_end() const { 124 return request_headers_.end(); 125 } resource_context()126 content::ResourceContext* resource_context() const { 127 return resource_context_; 128 } file_path()129 const base::FilePath& file_path() const { return save_info_.file_path; } suggested_name()130 const base::string16& suggested_name() const { 131 return save_info_.suggested_name; 132 } offset()133 int64 offset() const { return save_info_.offset; } hash_state()134 const std::string& hash_state() const { return save_info_.hash_state; } prompt()135 bool prompt() const { return save_info_.prompt_for_save_location; } url()136 const GURL& url() const { return url_; } 137 138 // Note that this is state changing--the DownloadUrlParameters object 139 // will not have a file attached to it after this call. GetFile()140 base::File GetFile() { return save_info_.file.Pass(); } 141 142 private: 143 OnStartedCallback callback_; 144 bool content_initiated_; 145 RequestHeadersType request_headers_; 146 int load_flags_; 147 std::string last_modified_; 148 std::string etag_; 149 std::string method_; 150 std::string post_body_; 151 int64 post_id_; 152 bool prefer_cache_; 153 Referrer referrer_; 154 std::string referrer_encoding_; 155 int render_process_host_id_; 156 int render_view_host_routing_id_; 157 ResourceContext* resource_context_; 158 DownloadSaveInfo save_info_; 159 GURL url_; 160 161 DISALLOW_COPY_AND_ASSIGN(DownloadUrlParameters); 162 }; 163 164 } // namespace content 165 166 #endif // CONTENT_PUBLIC_BROWSER_DOWNLOAD_URL_PARAMETERS_H_ 167