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 // Used with set_callback() below. |
45 typedef base::Callback<void(DownloadItem*, DownloadInterruptReason)> | 45 typedef base::Callback<void(DownloadItem*, DownloadInterruptReason)> |
46 OnStartedCallback; | 46 OnStartedCallback; |
47 | 47 |
48 typedef std::pair<std::string, std::string> RequestHeadersNameValuePair; | 48 typedef std::pair<std::string, std::string> RequestHeadersNameValuePair; |
49 typedef std::vector<RequestHeadersNameValuePair> RequestHeadersType; | 49 typedef std::vector<RequestHeadersNameValuePair> RequestHeadersType; |
50 | 50 |
51 // Construct DownloadUrlParameters for downloading the resource at |url| and | |
52 // associating the download with |web_contents|. | |
51 static scoped_ptr<DownloadUrlParameters> FromWebContents( | 53 static scoped_ptr<DownloadUrlParameters> FromWebContents( |
52 WebContents* web_contents, | 54 WebContents* web_contents, |
53 const GURL& url); | 55 const GURL& url); |
54 | 56 |
55 DownloadUrlParameters( | 57 // Construct DownloadUrlParameters for downloading the resource at |url| and |
56 const GURL& url, | 58 // associating the download with the WebContents identified by |
57 int render_process_host_id, | 59 // |render_process_host_id| and |render_view_host_routing_id|. |
58 int render_view_host_routing_id, | 60 // |
59 int render_frame_host_routing_id, | 61 // If the download is not associated with a WebContents, then set the IDs to |
60 content::ResourceContext* resource_context); | 62 // -1. |
63 // NOTE: This is not safe and should only be done in a limited set of cases | |
64 // where the download URL has been previously vetted. | |
65 DownloadUrlParameters(const GURL& url, | |
66 int render_process_host_id, | |
67 int render_view_host_routing_id, | |
68 int render_frame_host_routing_id, | |
69 ResourceContext* resource_context); | |
61 | 70 |
62 ~DownloadUrlParameters(); | 71 ~DownloadUrlParameters(); |
63 | 72 |
73 // Should be set to true if the download is being initiated by web content. | |
74 // Defaults to false. | |
64 void set_content_initiated(bool content_initiated) { | 75 void set_content_initiated(bool content_initiated) { |
65 content_initiated_ = content_initiated; | 76 content_initiated_ = content_initiated; |
66 } | 77 } |
67 void add_request_header(const std::string& name, const std::string& value) { | 78 void add_request_header(const std::string& name, const std::string& value) { |
68 request_headers_.push_back(make_pair(name, value)); | 79 request_headers_.push_back(make_pair(name, value)); |
69 } | 80 } |
81 | |
82 // HTTP Referrer and referrer encoding. | |
70 void set_referrer(const Referrer& referrer) { referrer_ = referrer; } | 83 void set_referrer(const Referrer& referrer) { referrer_ = referrer; } |
71 void set_referrer_encoding(const std::string& referrer_encoding) { | 84 void set_referrer_encoding(const std::string& referrer_encoding) { |
72 referrer_encoding_ = referrer_encoding; | 85 referrer_encoding_ = referrer_encoding; |
73 } | 86 } |
87 | |
88 // If this is a request for resuming an HTTP/S download, |last_modified| | |
89 // should be the value of the last seen Last-Modified response header. | |
74 void set_last_modified(const std::string& last_modified) { | 90 void set_last_modified(const std::string& last_modified) { |
75 last_modified_ = last_modified; | 91 last_modified_ = last_modified; |
76 } | 92 } |
93 | |
94 // If this is a request for resuming an HTTP/S download, |etag| should be the | |
95 // last seen Etag response header. | |
77 void set_etag(const std::string& etag) { | 96 void set_etag(const std::string& etag) { |
78 etag_ = etag; | 97 etag_ = etag; |
79 } | 98 } |
99 | |
100 // HTTP method to use. | |
80 void set_method(const std::string& method) { | 101 void set_method(const std::string& method) { |
81 method_ = method; | 102 method_ = method; |
82 } | 103 } |
104 | |
105 // Body of the HTTP POST request. | |
83 void set_post_body(const std::string& post_body) { | 106 void set_post_body(const std::string& post_body) { |
84 post_body_ = post_body; | 107 post_body_ = post_body; |
85 } | 108 } |
109 | |
110 // If |prefer_cache| is true and the response to |url| is in the HTTP cache, | |
111 // it will be used without validation. If |method| is POST, then |post_id_| | |
112 // shoud be set via |set_post_id()| below to the identifier of the POST | |
113 // transaction used to originally retrieve the resource. | |
86 void set_prefer_cache(bool prefer_cache) { | 114 void set_prefer_cache(bool prefer_cache) { |
87 prefer_cache_ = prefer_cache; | 115 prefer_cache_ = prefer_cache; |
88 } | 116 } |
117 | |
118 // See set_prefer_cache() above. | |
89 void set_post_id(int64_t post_id) { post_id_ = post_id; } | 119 void set_post_id(int64_t post_id) { post_id_ = post_id; } |
120 | |
121 // If |callback| is non-null it will be invoked when a response is received | |
122 // for |url|. For new downloads, this callback is invoked after the | |
123 // OnDownloadCreated notification is issued by the DownloadManager. If the | |
124 // download fails, then the DownloadInterruptReason parameter will indicate | |
125 // the failure. | |
Randy Smith (Not in Mondays)
2016/02/11 22:08:09
suggestion, nit: I'd personally put the documentat
Randy Smith (Not in Mondays)
2016/02/11 22:08:09
Suggestion: Is it reasonable to lay out the contex
asanka
2016/02/11 23:57:25
I added a brief comment explaining the distinction
asanka
2016/02/11 23:57:25
Done.
| |
90 void set_callback(const OnStartedCallback& callback) { | 126 void set_callback(const OnStartedCallback& callback) { |
91 callback_ = callback; | 127 callback_ = callback; |
92 } | 128 } |
129 | |
130 // If not empty, specifies the full target path for the download. This value | |
131 // overrides the filename suggested by a Content-Disposition headers. It | |
132 // should only be set for programmatic downloads where the caller can verify | |
133 // the safety of the filename and the resulting download. | |
93 void set_file_path(const base::FilePath& file_path) { | 134 void set_file_path(const base::FilePath& file_path) { |
94 save_info_.file_path = file_path; | 135 save_info_.file_path = file_path; |
95 } | 136 } |
137 | |
138 // Suggessted filename for the download. The suggestion can be overridden by | |
139 // either a Content-Disposition response header or a |file_path|. | |
96 void set_suggested_name(const base::string16& suggested_name) { | 140 void set_suggested_name(const base::string16& suggested_name) { |
97 save_info_.suggested_name = suggested_name; | 141 save_info_.suggested_name = suggested_name; |
98 } | 142 } |
143 | |
144 // If |offset| is non-zero, then a byte range request will be issued to fetch | |
145 // the range of bytes starting at |offset| through to the end of thedownload. | |
99 void set_offset(int64_t offset) { save_info_.offset = offset; } | 146 void set_offset(int64_t offset) { save_info_.offset = offset; } |
100 void set_hash_state(const std::string& hash_state) { | 147 void set_hash_state(const std::string& hash_state) { |
101 save_info_.hash_state = hash_state; | 148 save_info_.hash_state = hash_state; |
102 } | 149 } |
150 | |
151 // If |prompt| is true, then the user will be prompted for a filename. Not | |
152 // effective if |file_path| is non-empty. | |
103 void set_prompt(bool prompt) { save_info_.prompt_for_save_location = prompt; } | 153 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); } | 154 void set_file(base::File file) { save_info_.file = std::move(file); } |
105 void set_do_not_prompt_for_login(bool do_not_prompt) { | 155 void set_do_not_prompt_for_login(bool do_not_prompt) { |
106 do_not_prompt_for_login_ = do_not_prompt; | 156 do_not_prompt_for_login_ = do_not_prompt; |
107 } | 157 } |
108 | 158 |
109 const OnStartedCallback& callback() const { return callback_; } | 159 const OnStartedCallback& callback() const { return callback_; } |
110 bool content_initiated() const { return content_initiated_; } | 160 bool content_initiated() const { return content_initiated_; } |
111 const std::string& last_modified() const { return last_modified_; } | 161 const std::string& last_modified() const { return last_modified_; } |
112 const std::string& etag() const { return etag_; } | 162 const std::string& etag() const { return etag_; } |
113 const std::string& method() const { return method_; } | 163 const std::string& method() const { return method_; } |
114 const std::string& post_body() const { return post_body_; } | 164 const std::string& post_body() const { return post_body_; } |
115 int64_t post_id() const { return post_id_; } | 165 int64_t post_id() const { return post_id_; } |
116 bool prefer_cache() const { return prefer_cache_; } | 166 bool prefer_cache() const { return prefer_cache_; } |
117 const Referrer& referrer() const { return referrer_; } | 167 const Referrer& referrer() const { return referrer_; } |
118 const std::string& referrer_encoding() const { return referrer_encoding_; } | 168 const std::string& referrer_encoding() const { return referrer_encoding_; } |
119 int render_process_host_id() const { return render_process_host_id_; } | 169 int render_process_host_id() const { return render_process_host_id_; } |
120 int render_view_host_routing_id() const { | 170 int render_view_host_routing_id() const { |
121 return render_view_host_routing_id_; | 171 return render_view_host_routing_id_; |
122 } | 172 } |
123 int render_frame_host_routing_id() const { | 173 int render_frame_host_routing_id() const { |
124 return render_frame_host_routing_id_; | 174 return render_frame_host_routing_id_; |
125 } | 175 } |
126 RequestHeadersType::const_iterator request_headers_begin() const { | 176 const RequestHeadersType& request_headers() const { return request_headers_; } |
127 return request_headers_.begin(); | 177 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; } | 178 const base::FilePath& file_path() const { return save_info_.file_path; } |
136 const base::string16& suggested_name() const { | 179 const base::string16& suggested_name() const { |
137 return save_info_.suggested_name; | 180 return save_info_.suggested_name; |
138 } | 181 } |
139 int64_t offset() const { return save_info_.offset; } | 182 int64_t offset() const { return save_info_.offset; } |
140 const std::string& hash_state() const { return save_info_.hash_state; } | 183 const std::string& hash_state() const { return save_info_.hash_state; } |
141 bool prompt() const { return save_info_.prompt_for_save_location; } | 184 bool prompt() const { return save_info_.prompt_for_save_location; } |
142 const GURL& url() const { return url_; } | 185 const GURL& url() const { return url_; } |
143 bool do_not_prompt_for_login() const { return do_not_prompt_for_login_; } | 186 bool do_not_prompt_for_login() const { return do_not_prompt_for_login_; } |
144 | 187 |
(...skipping 20 matching lines...) Expand all Loading... | |
165 DownloadSaveInfo save_info_; | 208 DownloadSaveInfo save_info_; |
166 GURL url_; | 209 GURL url_; |
167 bool do_not_prompt_for_login_; | 210 bool do_not_prompt_for_login_; |
168 | 211 |
169 DISALLOW_COPY_AND_ASSIGN(DownloadUrlParameters); | 212 DISALLOW_COPY_AND_ASSIGN(DownloadUrlParameters); |
170 }; | 213 }; |
171 | 214 |
172 } // namespace content | 215 } // namespace content |
173 | 216 |
174 #endif // CONTENT_PUBLIC_BROWSER_DOWNLOAD_URL_PARAMETERS_H_ | 217 #endif // CONTENT_PUBLIC_BROWSER_DOWNLOAD_URL_PARAMETERS_H_ |
OLD | NEW |