OLD | NEW |
| (Empty) |
1 // Copyright 2007-2010 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 // | |
16 // BitsRequest provides http transactions using BITS, with an optional | |
17 // number of retries using a specified network configuration. | |
18 // | |
19 // BITS is sending the following string as user agent: | |
20 // User-Agent: Microsoft BITS/6.6 | |
21 // where the version seems to be the version of %windir%\System32\QMgr.dll. | |
22 // | |
23 // TODO(omaha): the class interface is not stable yet, as a few more | |
24 // getters and setters are still needed. | |
25 | |
26 #ifndef OMAHA_NET_BITS_REQUEST_H__ | |
27 #define OMAHA_NET_BITS_REQUEST_H__ | |
28 | |
29 #include <windows.h> | |
30 #include <bits.h> | |
31 #include <vector> | |
32 #include "base/basictypes.h" | |
33 #include "base/scoped_ptr.h" | |
34 #include "omaha/base/synchronized.h" | |
35 #include "omaha/base/utils.h" | |
36 #include "omaha/net/http_request.h" | |
37 | |
38 namespace omaha { | |
39 | |
40 class BitsJobCallback; | |
41 | |
42 class BitsRequest : public HttpRequestInterface { | |
43 public: | |
44 BitsRequest(); | |
45 virtual ~BitsRequest(); | |
46 | |
47 virtual HRESULT Close(); | |
48 | |
49 virtual HRESULT Send(); | |
50 | |
51 virtual HRESULT Cancel(); | |
52 | |
53 virtual HRESULT Pause(); | |
54 | |
55 virtual HRESULT Resume(); | |
56 | |
57 virtual std::vector<uint8> GetResponse() const { | |
58 return std::vector<uint8>(); | |
59 } | |
60 | |
61 // TODO(omaha): BITS provides access to headers on Windows Vista. | |
62 virtual HRESULT QueryHeadersString(uint32 info_level, | |
63 const TCHAR* name, | |
64 CString* value) const; | |
65 virtual CString GetResponseHeaders() const; | |
66 | |
67 // Returns the http status code in case of errors or 200 when the file is | |
68 // successfully transferred. BITS does not provide access to the status code | |
69 // directly; in some conditions the status code can be deduced from the error. | |
70 virtual int GetHttpStatusCode() const { | |
71 return request_state_.get() ? request_state_->http_status_code : 0; | |
72 } | |
73 | |
74 virtual CString ToString() const { return _T("BITS"); } | |
75 | |
76 virtual void set_session_handle(HINTERNET session_handle) { | |
77 session_handle_ = session_handle; | |
78 } | |
79 | |
80 virtual void set_url(const CString& url) { url_ = url; } | |
81 | |
82 virtual void set_request_buffer(const void* buffer, size_t buffer_length) { | |
83 request_buffer_ = buffer; | |
84 request_buffer_length_ = buffer_length; | |
85 } | |
86 | |
87 virtual void set_proxy_configuration(const ProxyConfig& proxy_config) { | |
88 proxy_config_ = proxy_config; | |
89 } | |
90 | |
91 // Sets the filename to receive the response instead of the memory buffer. | |
92 virtual void set_filename(const CString& filename) { filename_ = filename; } | |
93 | |
94 virtual void set_low_priority(bool low_priority) { | |
95 low_priority_ = low_priority; | |
96 } | |
97 | |
98 virtual void set_callback(NetworkRequestCallback* callback) { | |
99 callback_ = callback; | |
100 } | |
101 | |
102 virtual void set_additional_headers(const CString& additional_headers) { | |
103 additional_headers_ = additional_headers; | |
104 } | |
105 | |
106 // This request always uses the specified protocol so it is fine to ignore | |
107 // this attribute. | |
108 virtual void set_preserve_protocol(bool preserve_protocol) { | |
109 UNREFERENCED_PARAMETER(preserve_protocol); | |
110 } | |
111 | |
112 virtual CString user_agent() const { return user_agent_; } | |
113 | |
114 virtual void set_user_agent(const CString& user_agent) { | |
115 user_agent_ = user_agent; | |
116 } | |
117 | |
118 virtual void set_proxy_auth_config(const ProxyAuthConfig& proxy_auth_config) { | |
119 proxy_auth_config_ = proxy_auth_config; | |
120 } | |
121 | |
122 // Sets the minimum length of time that BITS waits after encountering a | |
123 // transient error condition before trying to transfer the file. | |
124 // The default value is 600 seconds. | |
125 void set_minimum_retry_delay(int minimum_retry_delay) { | |
126 minimum_retry_delay_ = minimum_retry_delay; | |
127 } | |
128 | |
129 // Sets the length of time that BITS tries to transfer the file after a | |
130 // transient error condition occurs. If BITS does not make progress during | |
131 // the retry period, it moves the state of the job from transient error | |
132 // to the error state. The default value is 14 days. | |
133 void set_no_progress_timeout(int no_progress_timeout) { | |
134 no_progress_timeout_ = no_progress_timeout; | |
135 } | |
136 | |
137 // Handles that BITS job state has changed. | |
138 void OnBitsJobStateChanged(); | |
139 | |
140 private: | |
141 // Sets invariant job properties, such as the filename and the description. | |
142 // These parameters can't change over the job life time. | |
143 HRESULT SetInvariantJobProperties(); | |
144 | |
145 // Sets non-invariant job properties. | |
146 HRESULT SetJobProperties(); | |
147 | |
148 // Sets additional_headers_ on the Job if IBackgroundCopyJobHttpOptions is | |
149 // supported. | |
150 HRESULT SetJobCustomHeaders(); | |
151 | |
152 // Uses the SimpleRequest HttpClient to detect the proxy for the current | |
153 // request. | |
154 HRESULT DetectManualProxy(); | |
155 | |
156 // Specifies how a job connects to the Internet. | |
157 HRESULT SetJobProxyUsage(); | |
158 | |
159 // Runs a polling loop waiting for the job to transition in one of its | |
160 // final states. | |
161 HRESULT DoSend(); | |
162 | |
163 // Handles the BG_JOB_STATE_ERROR. It returns S_OK when the error has | |
164 // been handled, otherwise, it returns the job error code and it makes the | |
165 // control return to the caller of Send. | |
166 HRESULT OnStateError(); | |
167 | |
168 // Handles the BG_JOB_STATE_TRANSFERRING. | |
169 HRESULT OnStateTransferring(); | |
170 | |
171 // Gets username and password through NetworkConfig. If successful, sets the | |
172 // credentials on the BITS job. | |
173 HRESULT GetProxyCredentials(); | |
174 | |
175 // Handles 407 errors. Tries autologon schemes. | |
176 HRESULT HandleProxyAuthenticationError(); | |
177 | |
178 // Handles 407 errors by cycling through the auth schemes, when credentials | |
179 // are already set on the BITS job. | |
180 HRESULT HandleProxyAuthenticationErrorCredsSet(); | |
181 | |
182 // Calls back with progress information if available. | |
183 HRESULT NotifyProgress(); | |
184 | |
185 int WinHttpToBitsProxyAuthScheme(uint32 winhttp_scheme); | |
186 uint32 BitsToWinhttpProxyAuthScheme(int bits_scheme); | |
187 | |
188 // Sets up BITS callback so BITS can send job status changes to this class. | |
189 HRESULT SetupBitsCallback(); | |
190 | |
191 // Stops BITS callback to send further job change notifications to this class. | |
192 // It is important to do this before the object goes out of scope since BITS | |
193 // callback needs to reference this object. | |
194 void RemoveBitsCallback(); | |
195 | |
196 // Creates or opens an existing job. | |
197 // 'is_created' is true if the job has been created or false if the job | |
198 // has been opened. | |
199 static HRESULT CreateOrOpenJob(const TCHAR* display_name, | |
200 IBackgroundCopyJob** bits_job, | |
201 bool* is_created); | |
202 | |
203 // Returns major.minor.0.0 BITS version. | |
204 static ULONGLONG GetBitsVersion(); | |
205 | |
206 // Holds the transient state corresponding to a BITS request. | |
207 struct TransientRequestState { | |
208 TransientRequestState() : http_status_code(0) { | |
209 SetZero(bits_job_id); | |
210 } | |
211 | |
212 int http_status_code; | |
213 CComPtr<IBackgroundCopyJob> bits_job; | |
214 GUID bits_job_id; | |
215 }; | |
216 | |
217 LLock lock_; | |
218 CString url_; | |
219 CString filename_; | |
220 const void* request_buffer_; // Contains the request body for POST. | |
221 size_t request_buffer_length_; // Length of the request body. | |
222 CString additional_headers_; | |
223 CString user_agent_; | |
224 ProxyAuthConfig proxy_auth_config_; | |
225 ProxyConfig proxy_config_; | |
226 bool low_priority_; | |
227 bool is_canceled_; | |
228 HINTERNET session_handle_; // Not owned by this class. | |
229 NetworkRequestCallback* callback_; | |
230 int minimum_retry_delay_; | |
231 int no_progress_timeout_; | |
232 int current_auth_scheme_; | |
233 | |
234 // For manual proxy authentication, if we do not know the auth scheme that the | |
235 // proxy is using, we set the username/password on all the schemes and try | |
236 // them out in sequence. | |
237 bool creds_set_scheme_unknown_; | |
238 | |
239 // Event that is set when the BITS job state is changed. | |
240 scoped_event bits_job_status_changed_event_; | |
241 | |
242 BitsJobCallback* bits_request_callback_; | |
243 uint32 last_progress_report_tick_; | |
244 | |
245 scoped_ptr<TransientRequestState> request_state_; | |
246 | |
247 // See http://b/1189928 | |
248 CComPtr<IBackgroundCopyManager> bits_manager_; | |
249 | |
250 // BITS could call JobModification() callback very often during job transfer. | |
251 // This minumum interval is to prevent reporting job progress too often to | |
252 // BitsRequest. | |
253 static const int kJobProgressReportMinimumIntervalMs = 200; | |
254 | |
255 DISALLOW_EVIL_CONSTRUCTORS(BitsRequest); | |
256 }; | |
257 | |
258 } // namespace omaha | |
259 | |
260 #endif // OMAHA_NET_BITS_REQUEST_H__ | |
261 | |
262 | |
OLD | NEW |