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 #ifndef CONTENT_PUBLIC_BROWSER_DOWNLOAD_URL_PARAMETERS_H_ | 5 #ifndef CONTENT_PUBLIC_BROWSER_DOWNLOAD_URL_PARAMETERS_H_ |
6 #define CONTENT_PUBLIC_BROWSER_DOWNLOAD_URL_PARAMETERS_H_ | 6 #define CONTENT_PUBLIC_BROWSER_DOWNLOAD_URL_PARAMETERS_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
(...skipping 23 matching lines...) Expand all Loading... |
34 // resource - it also requires |prefer_cache| to be |true| since re-post'ing is | 34 // resource - it also requires |prefer_cache| to be |true| since re-post'ing is |
35 // not done. |save_info| specifies where the downloaded file should be saved, | 35 // not done. |save_info| specifies where the downloaded file should be saved, |
36 // and whether the user should be prompted about the download. If not null, | 36 // and whether the user should be prompted about the download. If not null, |
37 // |callback| will be called when the download starts, or if an error occurs | 37 // |callback| will be called when the download starts, or if an error occurs |
38 // that prevents a download item from being created. We send a pointer to | 38 // that prevents a download item from being created. We send a pointer to |
39 // content::ResourceContext instead of the usual reference so that a copy of the | 39 // content::ResourceContext instead of the usual reference so that a copy of the |
40 // object isn't made. | 40 // object isn't made. |
41 | 41 |
42 class CONTENT_EXPORT DownloadUrlParameters { | 42 class CONTENT_EXPORT DownloadUrlParameters { |
43 public: | 43 public: |
44 // If there is an error, then |item| will be nullptr. | 44 // An OnStartedCallback is invoked when a response is available for the |
| 45 // downlaod request. For new downloads, this callback is invoked after the |
| 46 // OnDownloadCreated notification is issued by the DownloadManager. If the |
| 47 // download fails, then the DownloadInterruptReason parameter will indicate |
| 48 // the failure. |
| 49 // |
| 50 // DownloadItem* may be nullptr if no DownloadItem was created. DownloadItems |
| 51 // are not created when a resource throttle or a resource handler blocks the |
| 52 // download request. I.e. the download triggered a warning of some sort and |
| 53 // the user chose to not to proceed with the download as a result. |
45 typedef base::Callback<void(DownloadItem*, DownloadInterruptReason)> | 54 typedef base::Callback<void(DownloadItem*, DownloadInterruptReason)> |
46 OnStartedCallback; | 55 OnStartedCallback; |
47 | 56 |
48 typedef std::pair<std::string, std::string> RequestHeadersNameValuePair; | 57 typedef std::pair<std::string, std::string> RequestHeadersNameValuePair; |
49 typedef std::vector<RequestHeadersNameValuePair> RequestHeadersType; | 58 typedef std::vector<RequestHeadersNameValuePair> RequestHeadersType; |
50 | 59 |
| 60 // Construct DownloadUrlParameters for downloading the resource at |url| and |
| 61 // associating the download with |web_contents|. |
51 static scoped_ptr<DownloadUrlParameters> FromWebContents( | 62 static scoped_ptr<DownloadUrlParameters> FromWebContents( |
52 WebContents* web_contents, | 63 WebContents* web_contents, |
53 const GURL& url); | 64 const GURL& url); |
54 | 65 |
55 DownloadUrlParameters( | 66 // Construct DownloadUrlParameters for downloading the resource at |url| and |
56 const GURL& url, | 67 // associating the download with the WebContents identified by |
57 int render_process_host_id, | 68 // |render_process_host_id| and |render_view_host_routing_id|. |
58 int render_view_host_routing_id, | 69 // |
59 int render_frame_host_routing_id, | 70 // If the download is not associated with a WebContents, then set the IDs to |
60 content::ResourceContext* resource_context); | 71 // -1. |
| 72 // NOTE: This is not safe and should only be done in a limited set of cases |
| 73 // where the download URL has been previously vetted. A download that's |
| 74 // initiated without associating it with a WebContents don't receive the same |
| 75 // security checks as a request that's associated with one. |
| 76 DownloadUrlParameters(const GURL& url, |
| 77 int render_process_host_id, |
| 78 int render_view_host_routing_id, |
| 79 int render_frame_host_routing_id, |
| 80 ResourceContext* resource_context); |
61 | 81 |
62 ~DownloadUrlParameters(); | 82 ~DownloadUrlParameters(); |
63 | 83 |
| 84 // Should be set to true if the download was initiated by a script or a web |
| 85 // page. I.e. if the download request cannot be attributed to an explicit user |
| 86 // request for a download, then set this value to true. |
64 void set_content_initiated(bool content_initiated) { | 87 void set_content_initiated(bool content_initiated) { |
65 content_initiated_ = content_initiated; | 88 content_initiated_ = content_initiated; |
66 } | 89 } |
67 void add_request_header(const std::string& name, const std::string& value) { | 90 void add_request_header(const std::string& name, const std::string& value) { |
68 request_headers_.push_back(make_pair(name, value)); | 91 request_headers_.push_back(make_pair(name, value)); |
69 } | 92 } |
| 93 |
| 94 // HTTP Referrer and referrer encoding. |
70 void set_referrer(const Referrer& referrer) { referrer_ = referrer; } | 95 void set_referrer(const Referrer& referrer) { referrer_ = referrer; } |
71 void set_referrer_encoding(const std::string& referrer_encoding) { | 96 void set_referrer_encoding(const std::string& referrer_encoding) { |
72 referrer_encoding_ = referrer_encoding; | 97 referrer_encoding_ = referrer_encoding; |
73 } | 98 } |
| 99 |
| 100 // If this is a request for resuming an HTTP/S download, |last_modified| |
| 101 // should be the value of the last seen Last-Modified response header. |
74 void set_last_modified(const std::string& last_modified) { | 102 void set_last_modified(const std::string& last_modified) { |
75 last_modified_ = last_modified; | 103 last_modified_ = last_modified; |
76 } | 104 } |
| 105 |
| 106 // If this is a request for resuming an HTTP/S download, |etag| should be the |
| 107 // last seen Etag response header. |
77 void set_etag(const std::string& etag) { | 108 void set_etag(const std::string& etag) { |
78 etag_ = etag; | 109 etag_ = etag; |
79 } | 110 } |
| 111 |
| 112 // HTTP method to use. |
80 void set_method(const std::string& method) { | 113 void set_method(const std::string& method) { |
81 method_ = method; | 114 method_ = method; |
82 } | 115 } |
| 116 |
| 117 // Body of the HTTP POST request. |
83 void set_post_body(const std::string& post_body) { | 118 void set_post_body(const std::string& post_body) { |
84 post_body_ = post_body; | 119 post_body_ = post_body; |
85 } | 120 } |
| 121 |
| 122 // If |prefer_cache| is true and the response to |url| is in the HTTP cache, |
| 123 // it will be used without validation. If |method| is POST, then |post_id_| |
| 124 // shoud be set via |set_post_id()| below to the identifier of the POST |
| 125 // transaction used to originally retrieve the resource. |
86 void set_prefer_cache(bool prefer_cache) { | 126 void set_prefer_cache(bool prefer_cache) { |
87 prefer_cache_ = prefer_cache; | 127 prefer_cache_ = prefer_cache; |
88 } | 128 } |
| 129 |
| 130 // See set_prefer_cache() above. |
89 void set_post_id(int64_t post_id) { post_id_ = post_id; } | 131 void set_post_id(int64_t post_id) { post_id_ = post_id; } |
| 132 |
| 133 // See OnStartedCallback above. |
90 void set_callback(const OnStartedCallback& callback) { | 134 void set_callback(const OnStartedCallback& callback) { |
91 callback_ = callback; | 135 callback_ = callback; |
92 } | 136 } |
| 137 |
| 138 // If not empty, specifies the full target path for the download. This value |
| 139 // overrides the filename suggested by a Content-Disposition headers. It |
| 140 // should only be set for programmatic downloads where the caller can verify |
| 141 // the safety of the filename and the resulting download. |
93 void set_file_path(const base::FilePath& file_path) { | 142 void set_file_path(const base::FilePath& file_path) { |
94 save_info_.file_path = file_path; | 143 save_info_.file_path = file_path; |
95 } | 144 } |
| 145 |
| 146 // Suggessted filename for the download. The suggestion can be overridden by |
| 147 // either a Content-Disposition response header or a |file_path|. |
96 void set_suggested_name(const base::string16& suggested_name) { | 148 void set_suggested_name(const base::string16& suggested_name) { |
97 save_info_.suggested_name = suggested_name; | 149 save_info_.suggested_name = suggested_name; |
98 } | 150 } |
| 151 |
| 152 // If |offset| is non-zero, then a byte range request will be issued to fetch |
| 153 // the range of bytes starting at |offset| through to the end of thedownload. |
99 void set_offset(int64_t offset) { save_info_.offset = offset; } | 154 void set_offset(int64_t offset) { save_info_.offset = offset; } |
100 void set_hash_state(const std::string& hash_state) { | 155 void set_hash_state(const std::string& hash_state) { |
101 save_info_.hash_state = hash_state; | 156 save_info_.hash_state = hash_state; |
102 } | 157 } |
| 158 |
| 159 // If |prompt| is true, then the user will be prompted for a filename. Ignored |
| 160 // if |file_path| is non-empty. |
103 void set_prompt(bool prompt) { save_info_.prompt_for_save_location = prompt; } | 161 void set_prompt(bool prompt) { save_info_.prompt_for_save_location = prompt; } |
104 void set_file(base::File file) { save_info_.file = std::move(file); } | 162 void set_file(base::File file) { save_info_.file = std::move(file); } |
105 void set_do_not_prompt_for_login(bool do_not_prompt) { | 163 void set_do_not_prompt_for_login(bool do_not_prompt) { |
106 do_not_prompt_for_login_ = do_not_prompt; | 164 do_not_prompt_for_login_ = do_not_prompt; |
107 } | 165 } |
108 | 166 |
109 const OnStartedCallback& callback() const { return callback_; } | 167 const OnStartedCallback& callback() const { return callback_; } |
110 bool content_initiated() const { return content_initiated_; } | 168 bool content_initiated() const { return content_initiated_; } |
111 const std::string& last_modified() const { return last_modified_; } | 169 const std::string& last_modified() const { return last_modified_; } |
112 const std::string& etag() const { return etag_; } | 170 const std::string& etag() const { return etag_; } |
113 const std::string& method() const { return method_; } | 171 const std::string& method() const { return method_; } |
114 const std::string& post_body() const { return post_body_; } | 172 const std::string& post_body() const { return post_body_; } |
115 int64_t post_id() const { return post_id_; } | 173 int64_t post_id() const { return post_id_; } |
116 bool prefer_cache() const { return prefer_cache_; } | 174 bool prefer_cache() const { return prefer_cache_; } |
117 const Referrer& referrer() const { return referrer_; } | 175 const Referrer& referrer() const { return referrer_; } |
118 const std::string& referrer_encoding() const { return referrer_encoding_; } | 176 const std::string& referrer_encoding() const { return referrer_encoding_; } |
119 int render_process_host_id() const { return render_process_host_id_; } | 177 int render_process_host_id() const { return render_process_host_id_; } |
120 int render_view_host_routing_id() const { | 178 int render_view_host_routing_id() const { |
121 return render_view_host_routing_id_; | 179 return render_view_host_routing_id_; |
122 } | 180 } |
123 int render_frame_host_routing_id() const { | 181 int render_frame_host_routing_id() const { |
124 return render_frame_host_routing_id_; | 182 return render_frame_host_routing_id_; |
125 } | 183 } |
126 RequestHeadersType::const_iterator request_headers_begin() const { | 184 const RequestHeadersType& request_headers() const { return request_headers_; } |
127 return request_headers_.begin(); | 185 ResourceContext* resource_context() const { return resource_context_; } |
128 } | |
129 RequestHeadersType::const_iterator request_headers_end() const { | |
130 return request_headers_.end(); | |
131 } | |
132 content::ResourceContext* resource_context() const { | |
133 return resource_context_; | |
134 } | |
135 const base::FilePath& file_path() const { return save_info_.file_path; } | 186 const base::FilePath& file_path() const { return save_info_.file_path; } |
136 const base::string16& suggested_name() const { | 187 const base::string16& suggested_name() const { |
137 return save_info_.suggested_name; | 188 return save_info_.suggested_name; |
138 } | 189 } |
139 int64_t offset() const { return save_info_.offset; } | 190 int64_t offset() const { return save_info_.offset; } |
140 const std::string& hash_state() const { return save_info_.hash_state; } | 191 const std::string& hash_state() const { return save_info_.hash_state; } |
141 bool prompt() const { return save_info_.prompt_for_save_location; } | 192 bool prompt() const { return save_info_.prompt_for_save_location; } |
142 const GURL& url() const { return url_; } | 193 const GURL& url() const { return url_; } |
143 bool do_not_prompt_for_login() const { return do_not_prompt_for_login_; } | 194 bool do_not_prompt_for_login() const { return do_not_prompt_for_login_; } |
144 | 195 |
(...skipping 20 matching lines...) Expand all Loading... |
165 DownloadSaveInfo save_info_; | 216 DownloadSaveInfo save_info_; |
166 GURL url_; | 217 GURL url_; |
167 bool do_not_prompt_for_login_; | 218 bool do_not_prompt_for_login_; |
168 | 219 |
169 DISALLOW_COPY_AND_ASSIGN(DownloadUrlParameters); | 220 DISALLOW_COPY_AND_ASSIGN(DownloadUrlParameters); |
170 }; | 221 }; |
171 | 222 |
172 } // namespace content | 223 } // namespace content |
173 | 224 |
174 #endif // CONTENT_PUBLIC_BROWSER_DOWNLOAD_URL_PARAMETERS_H_ | 225 #endif // CONTENT_PUBLIC_BROWSER_DOWNLOAD_URL_PARAMETERS_H_ |
OLD | NEW |