Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 #pragma once | |
| 8 | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/callback.h" | |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/values.h" | |
| 15 #include "content/public/browser/download_save_info.h" | |
| 16 #include "googleurl/src/gurl.h" | |
| 17 #include "net/base/net_errors.h" | |
| 18 | |
| 19 namespace content { | |
| 20 | |
| 21 class ResourceContext; | |
| 22 class ResourceDispatcherHostImpl; | |
| 23 | |
| 24 // Pass an instance of DownloadUrlParameters to DownloadManager::DownloadUrl() | |
| 25 // to download the content at |url|. All parameters with setters are optional. | |
| 26 // |referrer| and |referrer_encoding| are the referrer for the download. If | |
| 27 // |prefer_cache| is true, then if the response to |url| is in the HTTP cache it | |
| 28 // will be used without revalidation. If |post_id| is non-negative, then it | |
| 29 // identifies the post transaction used to originally retrieve the |url| | |
| 30 // resource - it also requires |prefer_cache| to be |true| since re-post'ing is | |
| 31 // not done. |save_info| specifies where the downloaded file should be saved, | |
| 32 // and whether the user should be prompted about the download. If not null, | |
| 33 // |callback| will be called when the download starts, or if an error occurs | |
| 34 // that prevents a download item from being created. We send a pointer to | |
| 35 // content::ResourceContext instead of the usual reference so that a copy of the | |
| 36 // object isn't made. | |
| 37 | |
| 38 class CONTENT_EXPORT DownloadUrlParameters { | |
| 39 public: | |
| 40 // If there is an error, the DownloadId will be invalid. | |
| 41 typedef base::Callback<void(DownloadId, net::Error)> OnStartedCallback; | |
| 42 | |
| 43 typedef std::pair<std::string, std::string> RequestHeadersNameValuePair; | |
| 44 typedef std::vector<RequestHeadersNameValuePair> RequestHeadersType; | |
| 45 | |
| 46 static DownloadUrlParameters* FromWebContents( | |
| 47 content::WebContents* web_contents, | |
| 48 const GURL& url, | |
| 49 const content::DownloadSaveInfo& save_info); | |
| 50 | |
| 51 DownloadUrlParameters( | |
| 52 const GURL& url, | |
| 53 int render_process_host_id, | |
| 54 int render_view_host_routing_id, | |
| 55 content::ResourceContext* resource_context, | |
| 56 const content::DownloadSaveInfo& save_info); | |
| 57 | |
| 58 ~DownloadUrlParameters(); | |
| 59 | |
| 60 void set_request_header(const std::string& name, const std::string& value) { | |
|
Randy Smith (Not in Mondays)
2012/05/02 18:36:22
nit, suggestion: I think of "set" as implying "ove
benjhayden
2012/05/02 20:01:26
Done.
| |
| 61 request_headers_.push_back(make_pair(name, value)); | |
| 62 } | |
| 63 void set_referrer(const GURL& referrer) { referrer_ = referrer; } | |
| 64 void set_referrer_encoding(const std::string& referrer_encoding) { | |
| 65 referrer_encoding_ = referrer_encoding; | |
| 66 } | |
| 67 void set_load_flags(int load_flags) { load_flags_ |= load_flags; } | |
| 68 void set_method(const std::string& method) { | |
| 69 method_ = method; | |
| 70 } | |
| 71 void set_post_body(const std::string& post_body) { | |
| 72 post_body_ = post_body; | |
| 73 } | |
| 74 void set_prefer_cache(bool prefer_cache) { | |
| 75 prefer_cache_ = prefer_cache; | |
| 76 } | |
| 77 void set_post_id(int64 post_id) { | |
| 78 post_id_ = post_id; | |
| 79 // If post_id_ >= 0, then prefer_cache() must always return true regardless | |
| 80 // of set_prefer_cache() and method() must always return "POST" regardless | |
| 81 // of set_method(). | |
| 82 } | |
| 83 void set_callback(const OnStartedCallback& callback) { | |
| 84 callback_ = callback; | |
| 85 } | |
| 86 | |
| 87 const OnStartedCallback& callback() const { return callback_; } | |
| 88 int load_flags() const { return load_flags_; } | |
| 89 const std::string& method() const { | |
| 90 return post_id_set() ? method_post_ : method_; | |
| 91 } | |
| 92 const std::string& post_body() const { return post_body_; } | |
| 93 int64 post_id() const { return post_id_; } | |
| 94 bool post_id_set() const { return post_id() >= 0; } | |
| 95 bool prefer_cache() const { | |
| 96 return post_id_set() || prefer_cache_; | |
|
Randy Smith (Not in Mondays)
2012/05/02 18:36:22
Actually, my apologies, but I'd rather not do it t
benjhayden
2012/05/02 20:01:26
Done.
| |
| 97 } | |
| 98 const GURL& referrer() const { return referrer_; } | |
| 99 const std::string& referrer_encoding() const { return referrer_encoding_; } | |
| 100 int render_process_host_id() const { return render_process_host_id_; } | |
| 101 int render_view_host_routing_id() const { | |
| 102 return render_view_host_routing_id_; | |
| 103 } | |
| 104 RequestHeadersType::const_iterator request_headers_begin() const { | |
| 105 return request_headers_.begin(); | |
| 106 } | |
| 107 RequestHeadersType::const_iterator request_headers_end() const { | |
| 108 return request_headers_.end(); | |
| 109 } | |
| 110 content::ResourceContext* resource_context() const { | |
| 111 return resource_context_; | |
| 112 } | |
| 113 ResourceDispatcherHostImpl* resource_dispatcher_host() const { | |
| 114 return resource_dispatcher_host_; | |
| 115 } | |
| 116 const content::DownloadSaveInfo& save_info() const { return save_info_; } | |
| 117 const GURL& url() const { return url_; } | |
| 118 | |
| 119 private: | |
| 120 OnStartedCallback callback_; | |
| 121 RequestHeadersType request_headers_; | |
| 122 int load_flags_; | |
| 123 std::string method_; | |
| 124 const std::string method_post_; | |
| 125 std::string post_body_; | |
| 126 int64 post_id_; | |
| 127 bool prefer_cache_; | |
| 128 GURL referrer_; | |
| 129 std::string referrer_encoding_; | |
| 130 int render_process_host_id_; | |
| 131 int render_view_host_routing_id_; | |
| 132 ResourceContext* resource_context_; | |
| 133 ResourceDispatcherHostImpl* resource_dispatcher_host_; | |
| 134 DownloadSaveInfo save_info_; | |
| 135 GURL url_; | |
| 136 | |
| 137 DISALLOW_COPY_AND_ASSIGN(DownloadUrlParameters); | |
| 138 }; | |
| 139 | |
| 140 } // namespace content | |
| 141 | |
| 142 #endif // CONTENT_PUBLIC_BROWSER_DOWNLOAD_URL_PARAMETERS_H_ | |
| OLD | NEW |