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/message_loop/message_loop_proxy.h" | 8 #include "base/message_loop/message_loop_proxy.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "net/base/load_flags.h" | 10 #include "net/base/load_flags.h" |
11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
12 #include "net/cookies/cookie_monster.h" | 12 #include "net/cookies/cookie_monster.h" |
13 #include "net/dns/host_resolver.h" | 13 #include "net/dns/host_resolver.h" |
14 #include "net/http/http_cache.h" | 14 #include "net/http/http_cache.h" |
15 #include "net/http/http_network_layer.h" | 15 #include "net/http/http_network_layer.h" |
16 #include "net/http/http_response_headers.h" | 16 #include "net/http/http_response_headers.h" |
17 #include "net/proxy/proxy_service.h" | 17 #include "net/proxy/proxy_service.h" |
18 #include "net/url_request/static_http_user_agent_settings.h" | 18 #include "net/url_request/static_http_user_agent_settings.h" |
19 #include "net/url_request/url_fetcher.h" | 19 #include "net/url_request/url_fetcher.h" |
20 #include "net/url_request/url_request_context.h" | 20 #include "net/url_request/url_request_context.h" |
21 #include "net/url_request/url_request_status.h" | 21 #include "net/url_request/url_request_status.h" |
| 22 #include "sync/internal_api/public/base/cancelation_signal.h" |
22 | 23 |
23 namespace syncer { | 24 namespace syncer { |
24 | 25 |
25 HttpBridge::RequestContextGetter::RequestContextGetter( | 26 HttpBridge::RequestContextGetter::RequestContextGetter( |
26 net::URLRequestContextGetter* baseline_context_getter, | 27 net::URLRequestContextGetter* baseline_context_getter, |
27 const std::string& user_agent) | 28 const std::string& user_agent) |
28 : baseline_context_getter_(baseline_context_getter), | 29 : baseline_context_getter_(baseline_context_getter), |
29 network_task_runner_( | 30 network_task_runner_( |
30 baseline_context_getter_->GetNetworkTaskRunner()), | 31 baseline_context_getter_->GetNetworkTaskRunner()), |
31 user_agent_(user_agent) { | 32 user_agent_(user_agent) { |
(...skipping 19 matching lines...) Expand all Loading... |
51 return context_.get(); | 52 return context_.get(); |
52 } | 53 } |
53 | 54 |
54 scoped_refptr<base::SingleThreadTaskRunner> | 55 scoped_refptr<base::SingleThreadTaskRunner> |
55 HttpBridge::RequestContextGetter::GetNetworkTaskRunner() const { | 56 HttpBridge::RequestContextGetter::GetNetworkTaskRunner() const { |
56 return network_task_runner_; | 57 return network_task_runner_; |
57 } | 58 } |
58 | 59 |
59 HttpBridgeFactory::HttpBridgeFactory( | 60 HttpBridgeFactory::HttpBridgeFactory( |
60 net::URLRequestContextGetter* baseline_context_getter, | 61 net::URLRequestContextGetter* baseline_context_getter, |
61 const std::string& user_agent, | 62 const NetworkTimeUpdateCallback& network_time_update_callback, |
62 const NetworkTimeUpdateCallback& network_time_update_callback) | 63 CancelationSignal* cancelation_signal) |
63 : request_context_getter_( | 64 : baseline_request_context_getter_(baseline_context_getter), |
64 new HttpBridge::RequestContextGetter( | 65 network_time_update_callback_(network_time_update_callback), |
65 baseline_context_getter, user_agent)), | 66 cancelation_signal_(cancelation_signal) { |
66 network_time_update_callback_(network_time_update_callback) { | 67 // Registration should never fail. This should happen on the UI thread during |
| 68 // init. It would be impossible for a shutdown to have been requested at this |
| 69 // point. |
| 70 bool result = cancelation_signal_->TryRegisterHandler(this); |
| 71 DCHECK(result); |
67 } | 72 } |
68 | 73 |
69 HttpBridgeFactory::~HttpBridgeFactory() { | 74 HttpBridgeFactory::~HttpBridgeFactory() { |
| 75 cancelation_signal_->UnregisterHandler(this); |
| 76 } |
| 77 |
| 78 void HttpBridgeFactory::Init(const std::string& user_agent) { |
| 79 base::AutoLock lock(context_getter_lock_); |
| 80 |
| 81 if (!baseline_request_context_getter_.get()) { |
| 82 // Uh oh. We've been aborted before we finsihed initializing. |
| 83 // There's no point in initializating further; let's just return |
| 84 // right away. |
| 85 } |
| 86 |
| 87 request_context_getter_ = |
| 88 new HttpBridge::RequestContextGetter( |
| 89 baseline_request_context_getter_, user_agent); |
70 } | 90 } |
71 | 91 |
72 HttpPostProviderInterface* HttpBridgeFactory::Create() { | 92 HttpPostProviderInterface* HttpBridgeFactory::Create() { |
73 base::AutoLock lock(context_getter_lock_); | 93 base::AutoLock lock(context_getter_lock_); |
| 94 |
| 95 // If we've been asked to shut down (something which may happen asynchronously |
| 96 // and at pretty much any time), then we won't have a request_context_getter_. |
| 97 // Some external mechanism must ensure that this function is not called after |
| 98 // we've been asked to shut down. |
74 CHECK(request_context_getter_.get()); | 99 CHECK(request_context_getter_.get()); |
| 100 |
75 HttpBridge* http = new HttpBridge(request_context_getter_.get(), | 101 HttpBridge* http = new HttpBridge(request_context_getter_.get(), |
76 network_time_update_callback_); | 102 network_time_update_callback_); |
77 http->AddRef(); | 103 http->AddRef(); |
78 return http; | 104 return http; |
79 } | 105 } |
80 | 106 |
81 void HttpBridgeFactory::Destroy(HttpPostProviderInterface* http) { | 107 void HttpBridgeFactory::Destroy(HttpPostProviderInterface* http) { |
82 static_cast<HttpBridge*>(http)->Release(); | 108 static_cast<HttpBridge*>(http)->Release(); |
83 } | 109 } |
84 | 110 |
85 void HttpBridgeFactory::Shutdown() { | 111 void HttpBridgeFactory::OnSignalReceived() { |
86 base::AutoLock lock(context_getter_lock_); | 112 base::AutoLock lock(context_getter_lock_); |
87 // Release |request_context_getter_| as soon as possible so that it is | 113 // Release |baseline_request_context_getter_| as soon as possible so that it |
88 // destroyed in the right order on its network task runner. | 114 // is destroyed in the right order on its network task runner. The |
| 115 // |request_context_getter_| has a reference to the baseline, so we must |
| 116 // drop our reference to it, too. |
| 117 baseline_request_context_getter_ = NULL; |
89 request_context_getter_ = NULL; | 118 request_context_getter_ = NULL; |
90 } | 119 } |
91 | 120 |
92 HttpBridge::RequestContext::RequestContext( | 121 HttpBridge::RequestContext::RequestContext( |
93 net::URLRequestContext* baseline_context, | 122 net::URLRequestContext* baseline_context, |
94 const scoped_refptr<base::SingleThreadTaskRunner>& | 123 const scoped_refptr<base::SingleThreadTaskRunner>& |
95 network_task_runner, | 124 network_task_runner, |
96 const std::string& user_agent) | 125 const std::string& user_agent) |
97 : baseline_context_(baseline_context), | 126 : baseline_context_(baseline_context), |
98 network_task_runner_(network_task_runner) { | 127 network_task_runner_(network_task_runner) { |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 int64 sane_time_ms = 0; | 383 int64 sane_time_ms = 0; |
355 if (base::StringToInt64(sane_time_str, &sane_time_ms)) { | 384 if (base::StringToInt64(sane_time_str, &sane_time_ms)) { |
356 network_time_update_callback_.Run( | 385 network_time_update_callback_.Run( |
357 base::Time::FromJsTime(sane_time_ms), | 386 base::Time::FromJsTime(sane_time_ms), |
358 base::TimeDelta::FromMilliseconds(1), | 387 base::TimeDelta::FromMilliseconds(1), |
359 fetch_state_.end_time - fetch_state_.start_time); | 388 fetch_state_.end_time - fetch_state_.start_time); |
360 } | 389 } |
361 } | 390 } |
362 | 391 |
363 } // namespace syncer | 392 } // namespace syncer |
OLD | NEW |