OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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 #include "sync/internal_api/public/http_bridge.h" | 5 #include "sync/internal_api/public/http_bridge.h" |
6 | 6 |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/metrics/field_trial.h" | 8 #include "base/metrics/field_trial.h" |
9 #include "base/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 // init. It would be impossible for a shutdown to have been requested at this | 154 // init. It would be impossible for a shutdown to have been requested at this |
155 // point. | 155 // point. |
156 bool result = cancelation_signal_->TryRegisterHandler(this); | 156 bool result = cancelation_signal_->TryRegisterHandler(this); |
157 DCHECK(result); | 157 DCHECK(result); |
158 } | 158 } |
159 | 159 |
160 HttpBridgeFactory::~HttpBridgeFactory() { | 160 HttpBridgeFactory::~HttpBridgeFactory() { |
161 cancelation_signal_->UnregisterHandler(this); | 161 cancelation_signal_->UnregisterHandler(this); |
162 } | 162 } |
163 | 163 |
164 void HttpBridgeFactory::Init( | 164 void HttpBridgeFactory::Init(const std::string& user_agent) { |
165 const std::string& user_agent, | |
166 const BindToTrackerCallback& bind_to_tracker_callback) { | |
167 user_agent_ = user_agent; | 165 user_agent_ = user_agent; |
168 bind_to_tracker_callback_ = bind_to_tracker_callback; | |
169 } | 166 } |
170 | 167 |
171 HttpPostProviderInterface* HttpBridgeFactory::Create() { | 168 HttpPostProviderInterface* HttpBridgeFactory::Create() { |
172 base::AutoLock lock(request_context_getter_lock_); | 169 base::AutoLock lock(request_context_getter_lock_); |
173 | 170 |
174 // If we've been asked to shut down (something which may happen asynchronously | 171 // If we've been asked to shut down (something which may happen asynchronously |
175 // and at pretty much any time), then we won't have a request_context_getter_. | 172 // and at pretty much any time), then we won't have a request_context_getter_. |
176 // Some external mechanism must ensure that this function is not called after | 173 // Some external mechanism must ensure that this function is not called after |
177 // we've been asked to shut down. | 174 // we've been asked to shut down. |
178 CHECK(request_context_getter_.get()); | 175 CHECK(request_context_getter_.get()); |
179 | 176 |
180 scoped_refptr<HttpBridge> http = | 177 scoped_refptr<HttpBridge> http = new HttpBridge( |
181 new HttpBridge(user_agent_, request_context_getter_, | 178 user_agent_, request_context_getter_, network_time_update_callback_); |
182 network_time_update_callback_, bind_to_tracker_callback_); | |
183 http->AddRef(); | 179 http->AddRef(); |
184 return http.get(); | 180 return http.get(); |
185 } | 181 } |
186 | 182 |
187 void HttpBridgeFactory::Destroy(HttpPostProviderInterface* http) { | 183 void HttpBridgeFactory::Destroy(HttpPostProviderInterface* http) { |
188 static_cast<HttpBridge*>(http)->Release(); | 184 static_cast<HttpBridge*>(http)->Release(); |
189 } | 185 } |
190 | 186 |
191 void HttpBridgeFactory::OnSignalReceived() { | 187 void HttpBridgeFactory::OnSignalReceived() { |
192 base::AutoLock lock(request_context_getter_lock_); | 188 base::AutoLock lock(request_context_getter_lock_); |
193 // Release |request_context_getter_| as soon as possible so that it | 189 // Release |request_context_getter_| as soon as possible so that it |
194 // is destroyed in the right order on its network task runner. | 190 // is destroyed in the right order on its network task runner. |
195 request_context_getter_ = NULL; | 191 request_context_getter_ = NULL; |
196 } | 192 } |
197 | 193 |
198 HttpBridge::URLFetchState::URLFetchState() | 194 HttpBridge::URLFetchState::URLFetchState() |
199 : url_poster(NULL), | 195 : url_poster(NULL), |
200 aborted(false), | 196 aborted(false), |
201 request_completed(false), | 197 request_completed(false), |
202 request_succeeded(false), | 198 request_succeeded(false), |
203 http_response_code(-1), | 199 http_response_code(-1), |
204 error_code(-1) { | 200 error_code(-1) { |
205 } | 201 } |
206 HttpBridge::URLFetchState::~URLFetchState() {} | 202 HttpBridge::URLFetchState::~URLFetchState() {} |
207 | 203 |
208 HttpBridge::HttpBridge( | 204 HttpBridge::HttpBridge( |
209 const std::string& user_agent, | 205 const std::string& user_agent, |
210 const scoped_refptr<net::URLRequestContextGetter>& context_getter, | 206 const scoped_refptr<net::URLRequestContextGetter>& context_getter, |
211 const NetworkTimeUpdateCallback& network_time_update_callback, | 207 const NetworkTimeUpdateCallback& network_time_update_callback) |
212 const BindToTrackerCallback& bind_to_tracker_callback) | |
213 : created_on_loop_(base::MessageLoop::current()), | 208 : created_on_loop_(base::MessageLoop::current()), |
214 user_agent_(user_agent), | 209 user_agent_(user_agent), |
215 http_post_completed_(false, false), | 210 http_post_completed_(false, false), |
216 request_context_getter_(context_getter), | 211 request_context_getter_(context_getter), |
217 network_task_runner_(request_context_getter_->GetNetworkTaskRunner()), | 212 network_task_runner_(request_context_getter_->GetNetworkTaskRunner()), |
218 network_time_update_callback_(network_time_update_callback), | 213 network_time_update_callback_(network_time_update_callback) { |
219 bind_to_tracker_callback_(bind_to_tracker_callback) {} | 214 } |
220 | 215 |
221 HttpBridge::~HttpBridge() { | 216 HttpBridge::~HttpBridge() { |
222 } | 217 } |
223 | 218 |
224 void HttpBridge::SetExtraRequestHeaders(const char * headers) { | 219 void HttpBridge::SetExtraRequestHeaders(const char * headers) { |
225 DCHECK(extra_headers_.empty()) | 220 DCHECK(extra_headers_.empty()) |
226 << "HttpBridge::SetExtraRequestHeaders called twice."; | 221 << "HttpBridge::SetExtraRequestHeaders called twice."; |
227 extra_headers_.assign(headers); | 222 extra_headers_.assign(headers); |
228 } | 223 } |
229 | 224 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 fetch_state_.http_request_timeout_timer.reset(new base::Timer(false, false)); | 306 fetch_state_.http_request_timeout_timer.reset(new base::Timer(false, false)); |
312 fetch_state_.http_request_timeout_timer->Start( | 307 fetch_state_.http_request_timeout_timer->Start( |
313 FROM_HERE, base::TimeDelta::FromSeconds(kMaxHttpRequestTimeSeconds), | 308 FROM_HERE, base::TimeDelta::FromSeconds(kMaxHttpRequestTimeSeconds), |
314 base::Bind(&HttpBridge::OnURLFetchTimedOut, this)); | 309 base::Bind(&HttpBridge::OnURLFetchTimedOut, this)); |
315 | 310 |
316 DCHECK(request_context_getter_.get()); | 311 DCHECK(request_context_getter_.get()); |
317 fetch_state_.start_time = base::Time::Now(); | 312 fetch_state_.start_time = base::Time::Now(); |
318 fetch_state_.url_poster = | 313 fetch_state_.url_poster = |
319 net::URLFetcher::Create(url_for_request_, net::URLFetcher::POST, this) | 314 net::URLFetcher::Create(url_for_request_, net::URLFetcher::POST, this) |
320 .release(); | 315 .release(); |
321 if (!bind_to_tracker_callback_.is_null()) | |
322 bind_to_tracker_callback_.Run(fetch_state_.url_poster); | |
323 fetch_state_.url_poster->SetRequestContext(request_context_getter_.get()); | 316 fetch_state_.url_poster->SetRequestContext(request_context_getter_.get()); |
324 fetch_state_.url_poster->SetExtraRequestHeaders(extra_headers_); | 317 fetch_state_.url_poster->SetExtraRequestHeaders(extra_headers_); |
325 | 318 |
326 int64 compressed_content_size = 0; | 319 int64 compressed_content_size = 0; |
327 if (IsSyncHttpContentCompressionEnabled()) { | 320 if (IsSyncHttpContentCompressionEnabled()) { |
328 std::string compressed_request_content; | 321 std::string compressed_request_content; |
329 GzipCompress(request_content_, &compressed_request_content); | 322 GzipCompress(request_content_, &compressed_request_content); |
330 compressed_content_size = compressed_request_content.size(); | 323 compressed_content_size = compressed_request_content.size(); |
331 fetch_state_.url_poster->SetUploadData(content_type_, | 324 fetch_state_.url_poster->SetUploadData(content_type_, |
332 compressed_request_content); | 325 compressed_request_content); |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
533 int64 sane_time_ms = 0; | 526 int64 sane_time_ms = 0; |
534 if (base::StringToInt64(sane_time_str, &sane_time_ms)) { | 527 if (base::StringToInt64(sane_time_str, &sane_time_ms)) { |
535 network_time_update_callback_.Run( | 528 network_time_update_callback_.Run( |
536 base::Time::FromJsTime(sane_time_ms), | 529 base::Time::FromJsTime(sane_time_ms), |
537 base::TimeDelta::FromMilliseconds(1), | 530 base::TimeDelta::FromMilliseconds(1), |
538 fetch_state_.end_time - fetch_state_.start_time); | 531 fetch_state_.end_time - fetch_state_.start_time); |
539 } | 532 } |
540 } | 533 } |
541 | 534 |
542 } // namespace syncer | 535 } // namespace syncer |
OLD | NEW |