OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/cronet/android/url_request_context_adapter.h" | 5 #include "components/cronet/android/url_request_context_adapter.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/message_loop/message_loop.h" | |
9 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
10 #include "components/cronet/url_request_context_config.h" | 11 #include "components/cronet/url_request_context_config.h" |
11 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
12 #include "net/base/net_log_logger.h" | 13 #include "net/base/net_log_logger.h" |
13 #include "net/cert/cert_verifier.h" | 14 #include "net/cert/cert_verifier.h" |
14 #include "net/http/http_auth_handler_factory.h" | 15 #include "net/http/http_auth_handler_factory.h" |
15 #include "net/http/http_network_layer.h" | 16 #include "net/http/http_network_layer.h" |
16 #include "net/http/http_server_properties.h" | 17 #include "net/http/http_server_properties.h" |
17 #include "net/proxy/proxy_config_service_fixed.h" | 18 #include "net/proxy/proxy_config_service.h" |
18 #include "net/proxy/proxy_service.h" | 19 #include "net/proxy/proxy_service.h" |
19 #include "net/ssl/ssl_config_service_defaults.h" | 20 #include "net/ssl/ssl_config_service_defaults.h" |
20 #include "net/url_request/static_http_user_agent_settings.h" | 21 #include "net/url_request/static_http_user_agent_settings.h" |
21 #include "net/url_request/url_request_context_builder.h" | 22 #include "net/url_request/url_request_context_builder.h" |
22 #include "net/url_request/url_request_context_storage.h" | 23 #include "net/url_request/url_request_context_storage.h" |
23 #include "net/url_request/url_request_job_factory_impl.h" | 24 #include "net/url_request/url_request_job_factory_impl.h" |
24 | 25 |
25 namespace { | 26 namespace { |
26 | 27 |
28 // MessageLoop on the main thread, which is where objects that receive Java | |
29 // notifications generally live. | |
30 base::MessageLoop* g_main_message_loop = nullptr; | |
31 | |
27 class BasicNetworkDelegate : public net::NetworkDelegate { | 32 class BasicNetworkDelegate : public net::NetworkDelegate { |
28 public: | 33 public: |
29 BasicNetworkDelegate() {} | 34 BasicNetworkDelegate() {} |
30 virtual ~BasicNetworkDelegate() {} | 35 virtual ~BasicNetworkDelegate() {} |
31 | 36 |
32 private: | 37 private: |
33 // net::NetworkDelegate implementation. | 38 // net::NetworkDelegate implementation. |
34 virtual int OnBeforeURLRequest(net::URLRequest* request, | 39 virtual int OnBeforeURLRequest(net::URLRequest* request, |
35 const net::CompletionCallback& callback, | 40 const net::CompletionCallback& callback, |
36 GURL* new_url) override { | 41 GURL* new_url) override { |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
118 delegate_ = delegate; | 123 delegate_ = delegate; |
119 user_agent_ = user_agent; | 124 user_agent_ = user_agent; |
120 } | 125 } |
121 | 126 |
122 void URLRequestContextAdapter::Initialize( | 127 void URLRequestContextAdapter::Initialize( |
123 scoped_ptr<URLRequestContextConfig> config) { | 128 scoped_ptr<URLRequestContextConfig> config) { |
124 network_thread_ = new base::Thread("network"); | 129 network_thread_ = new base::Thread("network"); |
125 base::Thread::Options options; | 130 base::Thread::Options options; |
126 options.message_loop_type = base::MessageLoop::TYPE_IO; | 131 options.message_loop_type = base::MessageLoop::TYPE_IO; |
127 network_thread_->StartWithOptions(options); | 132 network_thread_->StartWithOptions(options); |
133 config_ = config.Pass(); | |
134 } | |
128 | 135 |
136 void URLRequestContextAdapter::InitRequestContextOnMainThread() { | |
137 if (!base::MessageLoop::current()) { | |
138 DCHECK(!g_main_message_loop); | |
139 g_main_message_loop = new base::MessageLoopForUI(); | |
140 base::MessageLoopForUI::current()->Start(); | |
141 } | |
142 DCHECK_EQ(g_main_message_loop, base::MessageLoop::current()); | |
mef
2014/10/15 20:04:55
what if base::MessageLoop::current() is NOT null o
xunjieli
2014/10/15 20:27:46
I guess we implicitly assume that Java main thread
| |
143 | |
144 proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService( | |
145 GetNetworkTaskRunner(), NULL)); | |
129 GetNetworkTaskRunner()->PostTask( | 146 GetNetworkTaskRunner()->PostTask( |
130 FROM_HERE, | 147 FROM_HERE, |
131 base::Bind(&URLRequestContextAdapter::InitializeURLRequestContext, | 148 base::Bind(&URLRequestContextAdapter::InitRequestContextOnNetworkThread, |
132 this, | 149 this)); |
133 Passed(&config))); | |
134 } | 150 } |
135 | 151 |
136 void URLRequestContextAdapter::InitializeURLRequestContext( | 152 void URLRequestContextAdapter::InitRequestContextOnNetworkThread() { |
137 scoped_ptr<URLRequestContextConfig> config) { | |
138 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 153 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
154 DCHECK(config_); | |
139 // TODO(mmenke): Add method to have the builder enable SPDY. | 155 // TODO(mmenke): Add method to have the builder enable SPDY. |
140 net::URLRequestContextBuilder context_builder; | 156 net::URLRequestContextBuilder context_builder; |
141 context_builder.set_network_delegate(new BasicNetworkDelegate()); | 157 context_builder.set_network_delegate(new BasicNetworkDelegate()); |
142 context_builder.set_proxy_config_service( | 158 context_builder.set_proxy_config_service(proxy_config_service_.get()); |
143 new net::ProxyConfigServiceFixed(net::ProxyConfig())); | 159 config_->ConfigureURLRequestContextBuilder(&context_builder); |
144 config->ConfigureURLRequestContextBuilder(&context_builder); | |
145 | 160 |
146 context_.reset(context_builder.Build()); | 161 context_.reset(context_builder.Build()); |
147 | 162 |
148 // Currently (circa M39) enabling QUIC requires setting probability threshold. | 163 // Currently (circa M39) enabling QUIC requires setting probability threshold. |
149 if (config->enable_quic) { | 164 if (config_->enable_quic) { |
150 context_->http_server_properties() | 165 context_->http_server_properties() |
151 ->SetAlternateProtocolProbabilityThreshold(0.0f); | 166 ->SetAlternateProtocolProbabilityThreshold(0.0f); |
152 for (size_t hint = 0; hint < config->quic_hints.size(); ++hint) { | 167 for (size_t hint = 0; hint < config_->quic_hints.size(); ++hint) { |
153 const URLRequestContextConfig::QuicHint& quic_hint = | 168 const URLRequestContextConfig::QuicHint& quic_hint = |
154 *config->quic_hints[hint]; | 169 *config_->quic_hints[hint]; |
155 if (quic_hint.host.empty()) { | 170 if (quic_hint.host.empty()) { |
156 LOG(ERROR) << "Empty QUIC hint host: " << quic_hint.host; | 171 LOG(ERROR) << "Empty QUIC hint host: " << quic_hint.host; |
157 continue; | 172 continue; |
158 } | 173 } |
159 | 174 |
160 if (quic_hint.port <= std::numeric_limits<uint16>::min() || | 175 if (quic_hint.port <= std::numeric_limits<uint16>::min() || |
161 quic_hint.port > std::numeric_limits<uint16>::max()) { | 176 quic_hint.port > std::numeric_limits<uint16>::max()) { |
162 LOG(ERROR) << "Invalid QUIC hint port: " | 177 LOG(ERROR) << "Invalid QUIC hint port: " |
163 << quic_hint.port; | 178 << quic_hint.port; |
164 continue; | 179 continue; |
165 } | 180 } |
166 | 181 |
167 if (quic_hint.alternate_port <= std::numeric_limits<uint16>::min() || | 182 if (quic_hint.alternate_port <= std::numeric_limits<uint16>::min() || |
168 quic_hint.alternate_port > std::numeric_limits<uint16>::max()) { | 183 quic_hint.alternate_port > std::numeric_limits<uint16>::max()) { |
169 LOG(ERROR) << "Invalid QUIC hint alternate port: " | 184 LOG(ERROR) << "Invalid QUIC hint alternate port: " |
170 << quic_hint.alternate_port; | 185 << quic_hint.alternate_port; |
171 continue; | 186 continue; |
172 } | 187 } |
173 | 188 |
174 net::HostPortPair quic_hint_host_port_pair(quic_hint.host, | 189 net::HostPortPair quic_hint_host_port_pair(quic_hint.host, |
175 quic_hint.port); | 190 quic_hint.port); |
176 context_->http_server_properties()->SetAlternateProtocol( | 191 context_->http_server_properties()->SetAlternateProtocol( |
177 quic_hint_host_port_pair, | 192 quic_hint_host_port_pair, |
178 static_cast<uint16>(quic_hint.alternate_port), | 193 static_cast<uint16>(quic_hint.alternate_port), |
179 net::AlternateProtocol::QUIC, | 194 net::AlternateProtocol::QUIC, |
180 1.0f); | 195 1.0f); |
181 } | 196 } |
182 is_context_initialized_ = true; | |
183 while (!tasks_waiting_for_context_.empty()) { | |
184 tasks_waiting_for_context_.front().Run(); | |
185 tasks_waiting_for_context_.pop(); | |
186 } | |
187 } | 197 } |
198 config_.reset(NULL); | |
188 | 199 |
189 if (VLOG_IS_ON(2)) { | 200 if (VLOG_IS_ON(2)) { |
190 net_log_observer_.reset(new NetLogObserver()); | 201 net_log_observer_.reset(new NetLogObserver()); |
191 context_->net_log()->AddThreadSafeObserver(net_log_observer_.get(), | 202 context_->net_log()->AddThreadSafeObserver(net_log_observer_.get(), |
192 net::NetLog::LOG_ALL_BUT_BYTES); | 203 net::NetLog::LOG_ALL_BUT_BYTES); |
193 } | 204 } |
194 | 205 |
206 is_context_initialized_ = true; | |
207 while (!tasks_waiting_for_context_.empty()) { | |
208 tasks_waiting_for_context_.front().Run(); | |
209 tasks_waiting_for_context_.pop(); | |
210 } | |
211 | |
195 delegate_->OnContextInitialized(this); | 212 delegate_->OnContextInitialized(this); |
196 } | 213 } |
197 | 214 |
198 void URLRequestContextAdapter::PostTaskToNetworkThread( | 215 void URLRequestContextAdapter::PostTaskToNetworkThread( |
199 const tracked_objects::Location& posted_from, | 216 const tracked_objects::Location& posted_from, |
200 const RunAfterContextInitTask& callback) { | 217 const RunAfterContextInitTask& callback) { |
201 GetNetworkTaskRunner()->PostTask( | 218 GetNetworkTaskRunner()->PostTask( |
202 posted_from, | 219 posted_from, |
203 base::Bind( | 220 base::Bind( |
204 &URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread, | 221 &URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread, |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
280 net_log_logger_.reset(); | 297 net_log_logger_.reset(); |
281 } | 298 } |
282 } | 299 } |
283 | 300 |
284 void NetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) { | 301 void NetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) { |
285 VLOG(2) << "Net log entry: type=" << entry.type() | 302 VLOG(2) << "Net log entry: type=" << entry.type() |
286 << ", source=" << entry.source().type << ", phase=" << entry.phase(); | 303 << ", source=" << entry.source().type << ", phase=" << entry.phase(); |
287 } | 304 } |
288 | 305 |
289 } // namespace cronet | 306 } // namespace cronet |
OLD | NEW |