[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 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_ |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "base/basictypes.h" |
| 12 | #include "base/callback.h" |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 13 | #include "content/public/browser/download_save_info.h" |
[email protected] | f859eba | 2012-05-30 17:22:49 | [diff] [blame] | 14 | #include "content/public/common/referrer.h" |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 15 | #include "googleurl/src/gurl.h" |
| 16 | #include "net/base/net_errors.h" |
| 17 | |
| 18 | namespace content { |
| 19 | |
[email protected] | 3d43eef | 2012-10-09 23:17:56 | [diff] [blame] | 20 | class DownloadItem; |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 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: |
[email protected] | 3d43eef | 2012-10-09 23:17:56 | [diff] [blame] | 41 | // If there is an error, then |item| will be NULL. |
| 42 | typedef base::Callback<void(DownloadItem*, net::Error)> OnStartedCallback; |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 43 | |
| 44 | typedef std::pair<std::string, std::string> RequestHeadersNameValuePair; |
| 45 | typedef std::vector<RequestHeadersNameValuePair> RequestHeadersType; |
| 46 | |
| 47 | static DownloadUrlParameters* FromWebContents( |
| 48 | WebContents* web_contents, |
[email protected] | 49d812e | 2012-11-06 21:18:12 | [diff] [blame] | 49 | const GURL& url); |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 50 | |
| 51 | DownloadUrlParameters( |
| 52 | const GURL& url, |
| 53 | int render_process_host_id, |
| 54 | int render_view_host_routing_id, |
[email protected] | 49d812e | 2012-11-06 21:18:12 | [diff] [blame] | 55 | content::ResourceContext* resource_context); |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 56 | |
| 57 | ~DownloadUrlParameters(); |
| 58 | |
[email protected] | a53e2f9 | 2012-05-15 15:27:06 | [diff] [blame] | 59 | void set_content_initiated(bool content_initiated) { |
| 60 | content_initiated_ = content_initiated; |
| 61 | } |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 62 | void add_request_header(const std::string& name, const std::string& value) { |
| 63 | request_headers_.push_back(make_pair(name, value)); |
| 64 | } |
[email protected] | f859eba | 2012-05-30 17:22:49 | [diff] [blame] | 65 | void set_referrer(const Referrer& referrer) { referrer_ = referrer; } |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 66 | void set_referrer_encoding(const std::string& referrer_encoding) { |
| 67 | referrer_encoding_ = referrer_encoding; |
| 68 | } |
| 69 | void set_load_flags(int load_flags) { load_flags_ |= load_flags; } |
[email protected] | 8d68a3e0 | 2013-01-12 15:57:10 | [diff] [blame] | 70 | void set_last_modified(const std::string& last_modified) { |
| 71 | last_modified_ = last_modified; |
| 72 | } |
| 73 | void set_etag(const std::string& etag) { |
| 74 | etag_ = etag; |
| 75 | } |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 76 | void set_method(const std::string& method) { |
| 77 | method_ = method; |
| 78 | } |
| 79 | void set_post_body(const std::string& post_body) { |
| 80 | post_body_ = post_body; |
| 81 | } |
| 82 | void set_prefer_cache(bool prefer_cache) { |
| 83 | prefer_cache_ = prefer_cache; |
| 84 | } |
| 85 | void set_post_id(int64 post_id) { post_id_ = post_id; } |
| 86 | void set_callback(const OnStartedCallback& callback) { |
| 87 | callback_ = callback; |
| 88 | } |
[email protected] | c42de73 | 2013-02-16 06:26:31 | [diff] [blame] | 89 | void set_file_path(const base::FilePath& file_path) { |
[email protected] | 49d812e | 2012-11-06 21:18:12 | [diff] [blame] | 90 | save_info_.file_path = file_path; |
| 91 | } |
| 92 | void set_suggested_name(const string16& suggested_name) { |
| 93 | save_info_.suggested_name = suggested_name; |
| 94 | } |
| 95 | void set_offset(int64 offset) { save_info_.offset = offset; } |
| 96 | void set_hash_state(std::string hash_state) { |
| 97 | save_info_.hash_state = hash_state; |
| 98 | } |
| 99 | void set_prompt(bool prompt) { save_info_.prompt_for_save_location = prompt; } |
| 100 | void set_file_stream(scoped_ptr<net::FileStream> file_stream) { |
| 101 | save_info_.file_stream = file_stream.Pass(); |
| 102 | } |
[email protected] | c873c86 | 2012-10-17 15:32:12 | [diff] [blame] | 103 | |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 104 | const OnStartedCallback& callback() const { return callback_; } |
[email protected] | a53e2f9 | 2012-05-15 15:27:06 | [diff] [blame] | 105 | bool content_initiated() const { return content_initiated_; } |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 106 | int load_flags() const { return load_flags_; } |
[email protected] | 8d68a3e0 | 2013-01-12 15:57:10 | [diff] [blame] | 107 | const std::string& last_modified() { return last_modified_; } |
| 108 | const std::string& etag() { return etag_; } |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 109 | const std::string& method() const { return method_; } |
| 110 | const std::string& post_body() const { return post_body_; } |
| 111 | int64 post_id() const { return post_id_; } |
| 112 | bool prefer_cache() const { return prefer_cache_; } |
[email protected] | f859eba | 2012-05-30 17:22:49 | [diff] [blame] | 113 | const Referrer& referrer() const { return referrer_; } |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 114 | const std::string& referrer_encoding() const { return referrer_encoding_; } |
| 115 | int render_process_host_id() const { return render_process_host_id_; } |
| 116 | int render_view_host_routing_id() const { |
| 117 | return render_view_host_routing_id_; |
| 118 | } |
| 119 | RequestHeadersType::const_iterator request_headers_begin() const { |
| 120 | return request_headers_.begin(); |
| 121 | } |
| 122 | RequestHeadersType::const_iterator request_headers_end() const { |
| 123 | return request_headers_.end(); |
| 124 | } |
| 125 | content::ResourceContext* resource_context() const { |
| 126 | return resource_context_; |
| 127 | } |
| 128 | ResourceDispatcherHost* resource_dispatcher_host() const { |
| 129 | return resource_dispatcher_host_; |
| 130 | } |
[email protected] | c42de73 | 2013-02-16 06:26:31 | [diff] [blame] | 131 | const base::FilePath& file_path() const { return save_info_.file_path; } |
[email protected] | 49d812e | 2012-11-06 21:18:12 | [diff] [blame] | 132 | const string16& suggested_name() const { return save_info_.suggested_name; } |
| 133 | int64 offset() const { return save_info_.offset; } |
| 134 | const std::string& hash_state() const { return save_info_.hash_state; } |
| 135 | bool prompt() const { return save_info_.prompt_for_save_location; } |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 136 | const GURL& url() const { return url_; } |
| 137 | |
[email protected] | 49d812e | 2012-11-06 21:18:12 | [diff] [blame] | 138 | // Note that this is state changing--the DownloadUrlParameters object |
| 139 | // will not have a file_stream attached to it after this call. |
| 140 | scoped_ptr<net::FileStream> GetFileStream() { |
| 141 | return save_info_.file_stream.Pass(); |
| 142 | } |
| 143 | |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 144 | private: |
| 145 | OnStartedCallback callback_; |
[email protected] | a53e2f9 | 2012-05-15 15:27:06 | [diff] [blame] | 146 | bool content_initiated_; |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 147 | RequestHeadersType request_headers_; |
| 148 | int load_flags_; |
[email protected] | 8d68a3e0 | 2013-01-12 15:57:10 | [diff] [blame] | 149 | std::string last_modified_; |
| 150 | std::string etag_; |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 151 | std::string method_; |
| 152 | std::string post_body_; |
| 153 | int64 post_id_; |
| 154 | bool prefer_cache_; |
[email protected] | f859eba | 2012-05-30 17:22:49 | [diff] [blame] | 155 | Referrer referrer_; |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 156 | std::string referrer_encoding_; |
| 157 | int render_process_host_id_; |
| 158 | int render_view_host_routing_id_; |
| 159 | ResourceContext* resource_context_; |
| 160 | ResourceDispatcherHost* resource_dispatcher_host_; |
[email protected] | 49d812e | 2012-11-06 21:18:12 | [diff] [blame] | 161 | DownloadSaveInfo save_info_; |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 162 | GURL url_; |
| 163 | |
| 164 | DISALLOW_COPY_AND_ASSIGN(DownloadUrlParameters); |
| 165 | }; |
| 166 | |
| 167 | } // namespace content |
| 168 | |
| 169 | #endif // CONTENT_PUBLIC_BROWSER_DOWNLOAD_URL_PARAMETERS_H_ |