Chromium Code Reviews| 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/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
| 9 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 11 #include "net/base/load_flags.h" | 12 #include "net/base/load_flags.h" |
| 12 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
| 13 #include "net/http/http_cache.h" | 14 #include "net/http/http_cache.h" |
| 14 #include "net/http/http_network_layer.h" | 15 #include "net/http/http_network_layer.h" |
| 15 #include "net/http/http_request_headers.h" | 16 #include "net/http/http_request_headers.h" |
| 16 #include "net/http/http_response_headers.h" | 17 #include "net/http/http_response_headers.h" |
| 17 #include "net/url_request/static_http_user_agent_settings.h" | 18 #include "net/url_request/static_http_user_agent_settings.h" |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 30 // treated as a transient failure. | 31 // treated as a transient failure. |
| 31 const int kMaxHttpRequestTimeSeconds = 60 * 5; // 5 minutes. | 32 const int kMaxHttpRequestTimeSeconds = 60 * 5; // 5 minutes. |
| 32 | 33 |
| 33 // Helper method for logging timeouts via UMA. | 34 // Helper method for logging timeouts via UMA. |
| 34 void LogTimeout(bool timed_out) { | 35 void LogTimeout(bool timed_out) { |
| 35 UMA_HISTOGRAM_BOOLEAN("Sync.URLFetchTimedOut", timed_out); | 36 UMA_HISTOGRAM_BOOLEAN("Sync.URLFetchTimedOut", timed_out); |
| 36 } | 37 } |
| 37 | 38 |
| 38 } // namespace | 39 } // namespace |
| 39 | 40 |
| 41 bool IsSyncHttpContentCompressionEnabled() { | |
| 42 const std::string group_name = | |
| 43 base::FieldTrialList::FindFullName("SyncHttpContentCompression"); | |
| 44 return group_name == "Enabled"; | |
| 45 } | |
| 46 | |
| 47 bool GzipCompress(std::string source, std::string* dest) { | |
| 48 z_stream stream = {0}; | |
| 49 int result = deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, | |
| 50 // 16 is added to produce a gzip header + trailer. | |
| 51 MAX_WBITS + 16, | |
| 52 8, // memLevel = 8 is default. | |
| 53 Z_DEFAULT_STRATEGY); | |
| 54 DCHECK_EQ(Z_OK, result); | |
| 55 size_t out_size = deflateBound(&stream, source.size()); | |
| 56 scoped_ptr<char[]> out(new char[out_size]); | |
| 57 | |
| 58 stream.next_in = reinterpret_cast<uint8*>(&source[0]); | |
| 59 stream.avail_in = source.size(); | |
| 60 stream.next_out = reinterpret_cast<uint8*>(out.get()); | |
| 61 stream.avail_out = out_size; | |
| 62 | |
| 63 result = deflate(&stream, Z_FINISH); | |
| 64 if (result != Z_STREAM_END) { | |
| 65 return false; | |
| 66 } | |
| 67 out_size = out_size - stream.avail_out; | |
| 68 | |
| 69 result = deflateEnd(&stream); | |
| 70 DCHECK_EQ(Z_OK, result); | |
| 71 | |
| 72 dest->assign(out.get(), out_size); | |
| 73 return true; | |
| 74 } | |
| 75 | |
| 76 void RecordSyncContentLengthHistograms(int64 this_content_length, | |
|
Nicolas Zea
2015/07/28 00:27:19
nit: I think these variables could be named cleare
Gang Wu
2015/07/29 21:09:15
Done.
| |
| 77 int64 this_original_content_length) { | |
| 78 // Upload UMA data | |
|
Nicolas Zea
2015/07/28 00:27:19
no need for this comment (all UMA histograms resul
Gang Wu
2015/07/29 21:09:15
Done.
| |
| 79 UMA_HISTOGRAM_COUNTS("Sync.ContentLengthCompressed", this_content_length); | |
| 80 UMA_HISTOGRAM_COUNTS("Sync.ContentLengthOriginal", | |
| 81 this_original_content_length); | |
| 82 } | |
| 83 | |
| 40 HttpBridgeFactory::HttpBridgeFactory( | 84 HttpBridgeFactory::HttpBridgeFactory( |
| 41 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, | 85 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, |
| 42 const NetworkTimeUpdateCallback& network_time_update_callback, | 86 const NetworkTimeUpdateCallback& network_time_update_callback, |
| 43 CancelationSignal* cancelation_signal) | 87 CancelationSignal* cancelation_signal) |
| 44 : request_context_getter_(request_context_getter), | 88 : request_context_getter_(request_context_getter), |
| 45 network_time_update_callback_(network_time_update_callback), | 89 network_time_update_callback_(network_time_update_callback), |
| 46 cancelation_signal_(cancelation_signal) { | 90 cancelation_signal_(cancelation_signal) { |
| 47 // Registration should never fail. This should happen on the UI thread during | 91 // Registration should never fail. This should happen on the UI thread during |
| 48 // init. It would be impossible for a shutdown to have been requested at this | 92 // init. It would be impossible for a shutdown to have been requested at this |
| 49 // point. | 93 // point. |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 196 | 240 |
| 197 // Start the timer on the network thread (the same thread progress is made | 241 // Start the timer on the network thread (the same thread progress is made |
| 198 // on, and on which the url fetcher lives). | 242 // on, and on which the url fetcher lives). |
| 199 DCHECK(!fetch_state_.http_request_timeout_timer.get()); | 243 DCHECK(!fetch_state_.http_request_timeout_timer.get()); |
| 200 fetch_state_.http_request_timeout_timer.reset(new base::Timer(false, false)); | 244 fetch_state_.http_request_timeout_timer.reset(new base::Timer(false, false)); |
| 201 fetch_state_.http_request_timeout_timer->Start( | 245 fetch_state_.http_request_timeout_timer->Start( |
| 202 FROM_HERE, base::TimeDelta::FromSeconds(kMaxHttpRequestTimeSeconds), | 246 FROM_HERE, base::TimeDelta::FromSeconds(kMaxHttpRequestTimeSeconds), |
| 203 base::Bind(&HttpBridge::OnURLFetchTimedOut, this)); | 247 base::Bind(&HttpBridge::OnURLFetchTimedOut, this)); |
| 204 | 248 |
| 205 DCHECK(request_context_getter_.get()); | 249 DCHECK(request_context_getter_.get()); |
| 250 fetch_state_.start_time = base::Time::Now(); | |
| 206 fetch_state_.url_poster = | 251 fetch_state_.url_poster = |
| 207 net::URLFetcher::Create(url_for_request_, net::URLFetcher::POST, this) | 252 net::URLFetcher::Create(url_for_request_, net::URLFetcher::POST, this) |
| 208 .release(); | 253 .release(); |
| 209 fetch_state_.url_poster->SetRequestContext(request_context_getter_.get()); | 254 fetch_state_.url_poster->SetRequestContext(request_context_getter_.get()); |
| 210 fetch_state_.url_poster->SetUploadData(content_type_, request_content_); | |
| 211 fetch_state_.url_poster->SetExtraRequestHeaders(extra_headers_); | 255 fetch_state_.url_poster->SetExtraRequestHeaders(extra_headers_); |
| 256 | |
| 257 std::string content_to_be_sent = request_content_; | |
|
Nicolas Zea
2015/07/28 00:27:19
I'd rather not pay the cost of a copy on all reque
Gang Wu
2015/07/29 21:09:15
Done.
| |
| 258 if (IsSyncHttpContentCompressionEnabled()) { | |
| 259 GzipCompress(request_content_, &content_to_be_sent); | |
| 260 fetch_state_.url_poster->AddExtraRequestHeader("Content-Encoding: gzip"); | |
| 261 } else { | |
| 262 fetch_state_.url_poster->AddExtraRequestHeader("Accept-Encoding: deflate"); | |
| 263 } | |
| 264 fetch_state_.url_poster->SetUploadData(content_type_, content_to_be_sent); | |
| 265 RecordSyncContentLengthHistograms(content_to_be_sent.size(), | |
| 266 request_content_.size()); | |
| 267 | |
| 212 fetch_state_.url_poster->AddExtraRequestHeader(base::StringPrintf( | 268 fetch_state_.url_poster->AddExtraRequestHeader(base::StringPrintf( |
| 213 "%s: %s", net::HttpRequestHeaders::kUserAgent, user_agent_.c_str())); | 269 "%s: %s", net::HttpRequestHeaders::kUserAgent, user_agent_.c_str())); |
| 214 fetch_state_.url_poster->SetLoadFlags(net::LOAD_BYPASS_CACHE | | 270 fetch_state_.url_poster->SetLoadFlags(net::LOAD_BYPASS_CACHE | |
| 215 net::LOAD_DISABLE_CACHE | | 271 net::LOAD_DISABLE_CACHE | |
| 216 net::LOAD_DO_NOT_SAVE_COOKIES | | 272 net::LOAD_DO_NOT_SAVE_COOKIES | |
| 217 net::LOAD_DO_NOT_SEND_COOKIES); | 273 net::LOAD_DO_NOT_SEND_COOKIES); |
| 218 fetch_state_.start_time = base::Time::Now(); | |
| 219 | 274 |
| 220 fetch_state_.url_poster->Start(); | 275 fetch_state_.url_poster->Start(); |
| 221 } | 276 } |
| 222 | 277 |
| 223 int HttpBridge::GetResponseContentLength() const { | 278 int HttpBridge::GetResponseContentLength() const { |
| 224 DCHECK_EQ(base::MessageLoop::current(), created_on_loop_); | 279 DCHECK_EQ(base::MessageLoop::current(), created_on_loop_); |
| 225 base::AutoLock lock(fetch_state_lock_); | 280 base::AutoLock lock(fetch_state_lock_); |
| 226 DCHECK(fetch_state_.request_completed); | 281 DCHECK(fetch_state_.request_completed); |
| 227 return fetch_state_.response_content.size(); | 282 return fetch_state_.response_content.size(); |
| 228 } | 283 } |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 308 // Use a real (non-debug) log to facilitate troubleshooting in the wild. | 363 // Use a real (non-debug) log to facilitate troubleshooting in the wild. |
| 309 VLOG(2) << "HttpBridge::OnURLFetchComplete for: " | 364 VLOG(2) << "HttpBridge::OnURLFetchComplete for: " |
| 310 << fetch_state_.url_poster->GetURL().spec(); | 365 << fetch_state_.url_poster->GetURL().spec(); |
| 311 VLOG(1) << "HttpBridge received response code: " | 366 VLOG(1) << "HttpBridge received response code: " |
| 312 << fetch_state_.http_response_code; | 367 << fetch_state_.http_response_code; |
| 313 | 368 |
| 314 source->GetResponseAsString(&fetch_state_.response_content); | 369 source->GetResponseAsString(&fetch_state_.response_content); |
| 315 fetch_state_.response_headers = source->GetResponseHeaders(); | 370 fetch_state_.response_headers = source->GetResponseHeaders(); |
| 316 UpdateNetworkTime(); | 371 UpdateNetworkTime(); |
| 317 | 372 |
| 373 int64 compressed_content_length = fetch_state_.response_content.size(); | |
| 374 int64 original_content_length = compressed_content_length; | |
| 375 if (fetch_state_.response_headers && | |
| 376 fetch_state_.response_headers->HasHeaderValue("content-encoding", | |
| 377 "gzip")) { | |
| 378 compressed_content_length = | |
| 379 fetch_state_.response_headers->GetContentLength(); | |
| 380 } | |
| 381 RecordSyncContentLengthHistograms(compressed_content_length, | |
|
Nicolas Zea
2015/07/28 00:27:19
Does it make sense to record the request separatel
Gang Wu
2015/07/29 21:09:15
Yes, since request is smaller than response, so se
| |
| 382 original_content_length); | |
| 383 | |
| 318 // End of the line for url_poster_. It lives only on the IO loop. | 384 // End of the line for url_poster_. It lives only on the IO loop. |
| 319 // We defer deletion because we're inside a callback from a component of the | 385 // We defer deletion because we're inside a callback from a component of the |
| 320 // URLFetcher, so it seems most natural / "polite" to let the stack unwind. | 386 // URLFetcher, so it seems most natural / "polite" to let the stack unwind. |
| 321 base::MessageLoop::current()->DeleteSoon(FROM_HERE, fetch_state_.url_poster); | 387 base::MessageLoop::current()->DeleteSoon(FROM_HERE, fetch_state_.url_poster); |
| 322 fetch_state_.url_poster = NULL; | 388 fetch_state_.url_poster = NULL; |
| 323 | 389 |
| 324 // Wake the blocked syncer thread in MakeSynchronousPost. | 390 // Wake the blocked syncer thread in MakeSynchronousPost. |
| 325 // WARNING: DONT DO ANYTHING AFTER THIS CALL! |this| may be deleted! | 391 // WARNING: DONT DO ANYTHING AFTER THIS CALL! |this| may be deleted! |
| 326 http_post_completed_.Signal(); | 392 http_post_completed_.Signal(); |
| 327 } | 393 } |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 392 int64 sane_time_ms = 0; | 458 int64 sane_time_ms = 0; |
| 393 if (base::StringToInt64(sane_time_str, &sane_time_ms)) { | 459 if (base::StringToInt64(sane_time_str, &sane_time_ms)) { |
| 394 network_time_update_callback_.Run( | 460 network_time_update_callback_.Run( |
| 395 base::Time::FromJsTime(sane_time_ms), | 461 base::Time::FromJsTime(sane_time_ms), |
| 396 base::TimeDelta::FromMilliseconds(1), | 462 base::TimeDelta::FromMilliseconds(1), |
| 397 fetch_state_.end_time - fetch_state_.start_time); | 463 fetch_state_.end_time - fetch_state_.start_time); |
| 398 } | 464 } |
| 399 } | 465 } |
| 400 | 466 |
| 401 } // namespace syncer | 467 } // namespace syncer |
| OLD | NEW |