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/cronet_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/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
10 #include "components/cronet/url_request_context_config.h" | 10 #include "components/cronet/url_request_context_config.h" |
11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
12 #include "net/base/net_log_logger.h" | 12 #include "net/base/net_log_logger.h" |
13 #include "net/cert/cert_verifier.h" | 13 #include "net/cert/cert_verifier.h" |
14 #include "net/http/http_auth_handler_factory.h" | 14 #include "net/http/http_auth_handler_factory.h" |
15 #include "net/http/http_network_layer.h" | 15 #include "net/http/http_network_layer.h" |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
98 const net::URLRequest& request) const OVERRIDE { | 98 const net::URLRequest& request) const OVERRIDE { |
99 return false; | 99 return false; |
100 } | 100 } |
101 | 101 |
102 virtual int OnBeforeSocketStreamConnect( | 102 virtual int OnBeforeSocketStreamConnect( |
103 net::SocketStream* stream, | 103 net::SocketStream* stream, |
104 const net::CompletionCallback& callback) OVERRIDE { | 104 const net::CompletionCallback& callback) OVERRIDE { |
105 return net::OK; | 105 return net::OK; |
106 } | 106 } |
107 | 107 |
108 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate); | 108 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate); |
xunjieli
2014/10/01 14:40:37
nit: include "base/macros.h".
mef
2014/10/01 20:41:31
Hmm, I think it is not necessary as long as it is
| |
109 }; | 109 }; |
110 | 110 |
111 } // namespace | 111 } // namespace |
112 | 112 |
113 namespace cronet { | 113 namespace cronet { |
114 | 114 |
115 URLRequestContextAdapter::URLRequestContextAdapter( | 115 CronetURLRequestContextAdapter::CronetURLRequestContextAdapter( |
116 URLRequestContextAdapterDelegate* delegate, | 116 CronetURLRequestContextAdapterDelegate* delegate) { |
117 std::string user_agent) { | |
118 delegate_ = delegate; | 117 delegate_ = delegate; |
119 user_agent_ = user_agent; | |
120 } | 118 } |
121 | 119 |
122 void URLRequestContextAdapter::Initialize( | 120 void CronetURLRequestContextAdapter::Initialize( |
123 scoped_ptr<URLRequestContextConfig> config) { | 121 scoped_ptr<URLRequestContextConfig> config) { |
124 network_thread_ = new base::Thread("network"); | 122 network_thread_ = new base::Thread("network"); |
125 base::Thread::Options options; | 123 base::Thread::Options options; |
126 options.message_loop_type = base::MessageLoop::TYPE_IO; | 124 options.message_loop_type = base::MessageLoop::TYPE_IO; |
127 network_thread_->StartWithOptions(options); | 125 network_thread_->StartWithOptions(options); |
128 | 126 |
129 GetNetworkTaskRunner()->PostTask( | 127 GetNetworkTaskRunner()->PostTask( |
130 FROM_HERE, | 128 FROM_HERE, |
131 base::Bind(&URLRequestContextAdapter::InitializeURLRequestContext, | 129 base::Bind(&CronetURLRequestContextAdapter::InitializeURLRequestContext, |
132 this, | 130 this, |
133 Passed(&config))); | 131 Passed(&config))); |
134 } | 132 } |
135 | 133 |
136 void URLRequestContextAdapter::InitializeURLRequestContext( | 134 void CronetURLRequestContextAdapter::InitializeURLRequestContext( |
137 scoped_ptr<URLRequestContextConfig> config) { | 135 scoped_ptr<URLRequestContextConfig> config) { |
138 // TODO(mmenke): Add method to have the builder enable SPDY. | 136 // TODO(mmenke): Add method to have the builder enable SPDY. |
139 net::URLRequestContextBuilder context_builder; | 137 net::URLRequestContextBuilder context_builder; |
140 context_builder.set_network_delegate(new BasicNetworkDelegate()); | 138 context_builder.set_network_delegate(new BasicNetworkDelegate()); |
141 context_builder.set_proxy_config_service( | 139 context_builder.set_proxy_config_service( |
142 new net::ProxyConfigServiceFixed(net::ProxyConfig())); | 140 new net::ProxyConfigServiceFixed(net::ProxyConfig())); |
143 config->ConfigureURLRequestContextBuilder(&context_builder); | 141 config->ConfigureURLRequestContextBuilder(&context_builder); |
144 | 142 |
145 context_.reset(context_builder.Build()); | 143 context_.reset(context_builder.Build()); |
146 | 144 |
147 // Currently (circa M39) enabling QUIC requires setting probability threshold. | 145 // Currently (circa M39) enabling QUIC requires setting probability threshold. |
148 if (config->enable_quic) { | 146 if (config->enable_quic) { |
149 context_->http_server_properties() | 147 context_->http_server_properties() |
150 ->SetAlternateProtocolProbabilityThreshold(0.0f); | 148 ->SetAlternateProtocolProbabilityThreshold(0.0f); |
151 for (size_t hint = 0; hint < config->quic_hints.size(); ++hint) { | 149 for (size_t hint = 0; hint < config->quic_hints.size(); ++hint) { |
152 const URLRequestContextConfig::QuicHint& quic_hint = | 150 const URLRequestContextConfig::QuicHint& quic_hint = |
153 *config->quic_hints[hint]; | 151 *config->quic_hints[hint]; |
154 if (quic_hint.host.empty()) { | 152 if (quic_hint.host.empty()) { |
155 LOG(ERROR) << "Empty QUIC hint host: " << quic_hint.host; | 153 LOG(ERROR) << "Empty QUIC hint host: " << quic_hint.host; |
156 continue; | 154 continue; |
157 } | 155 } |
158 | 156 |
159 if (quic_hint.port <= std::numeric_limits<uint16>::min() || | 157 if (quic_hint.port <= std::numeric_limits<uint16>::min() || |
160 quic_hint.port > std::numeric_limits<uint16>::max()) { | 158 quic_hint.port > std::numeric_limits<uint16>::max()) { |
161 LOG(ERROR) << "Invalid QUIC hint port: " | 159 LOG(ERROR) << "Invalid QUIC hint port: " << quic_hint.port; |
162 << quic_hint.port; | |
163 continue; | 160 continue; |
164 } | 161 } |
165 | 162 |
166 if (quic_hint.alternate_port <= std::numeric_limits<uint16>::min() || | 163 if (quic_hint.alternate_port <= std::numeric_limits<uint16>::min() || |
167 quic_hint.alternate_port > std::numeric_limits<uint16>::max()) { | 164 quic_hint.alternate_port > std::numeric_limits<uint16>::max()) { |
168 LOG(ERROR) << "Invalid QUIC hint alternate port: " | 165 LOG(ERROR) << "Invalid QUIC hint alternate port: " |
169 << quic_hint.alternate_port; | 166 << quic_hint.alternate_port; |
170 continue; | 167 continue; |
171 } | 168 } |
172 | 169 |
173 net::HostPortPair quic_hint_host_port_pair(quic_hint.host, | 170 net::HostPortPair quic_hint_host_port_pair(quic_hint.host, |
174 quic_hint.port); | 171 quic_hint.port); |
175 context_->http_server_properties()->SetAlternateProtocol( | 172 context_->http_server_properties()->SetAlternateProtocol( |
176 quic_hint_host_port_pair, | 173 quic_hint_host_port_pair, |
177 static_cast<uint16>(quic_hint.alternate_port), | 174 static_cast<uint16>(quic_hint.alternate_port), |
178 net::AlternateProtocol::QUIC, | 175 net::AlternateProtocol::QUIC, |
179 1.0f); | 176 1.0f); |
180 } | 177 } |
181 } | 178 } |
182 | 179 user_agent_ = config->user_agent; |
183 if (VLOG_IS_ON(2)) { | |
184 net_log_observer_.reset(new NetLogObserver()); | |
185 context_->net_log()->AddThreadSafeObserver(net_log_observer_.get(), | |
186 net::NetLog::LOG_ALL_BUT_BYTES); | |
187 } | |
188 | 180 |
189 delegate_->OnContextInitialized(this); | 181 delegate_->OnContextInitialized(this); |
190 } | 182 } |
191 | 183 |
192 URLRequestContextAdapter::~URLRequestContextAdapter() { | 184 CronetURLRequestContextAdapter::~CronetURLRequestContextAdapter() { |
193 if (net_log_observer_) { | |
194 context_->net_log()->RemoveThreadSafeObserver(net_log_observer_.get()); | |
195 net_log_observer_.reset(); | |
196 } | |
197 StopNetLog(); | 185 StopNetLog(); |
198 // TODO(mef): Ensure that |network_thread_| is destroyed properly. | 186 // TODO(mef): Ensure that |network_thread_| is destroyed properly. |
199 } | 187 } |
200 | 188 |
201 const std::string& URLRequestContextAdapter::GetUserAgent( | 189 const std::string& CronetURLRequestContextAdapter::GetUserAgent( |
202 const GURL& url) const { | 190 const GURL& url) const { |
203 return user_agent_; | 191 return user_agent_; |
204 } | 192 } |
205 | 193 |
206 net::URLRequestContext* URLRequestContextAdapter::GetURLRequestContext() { | 194 net::URLRequestContext* CronetURLRequestContextAdapter::GetURLRequestContext() { |
207 if (!context_) { | 195 if (!context_) { |
208 LOG(ERROR) << "URLRequestContext is not set up"; | 196 LOG(ERROR) << "URLRequestContext is not set up"; |
209 } | 197 } |
210 return context_.get(); | 198 return context_.get(); |
211 } | 199 } |
212 | 200 |
213 scoped_refptr<base::SingleThreadTaskRunner> | 201 scoped_refptr<base::SingleThreadTaskRunner> |
214 URLRequestContextAdapter::GetNetworkTaskRunner() const { | 202 CronetURLRequestContextAdapter::GetNetworkTaskRunner() const { |
215 return network_thread_->message_loop_proxy(); | 203 return network_thread_->message_loop_proxy(); |
216 } | 204 } |
217 | 205 |
218 void URLRequestContextAdapter::StartNetLogToFile(const std::string& file_name) { | 206 void CronetURLRequestContextAdapter::StartNetLogToFile( |
207 const std::string& file_name) { | |
219 // Do nothing if already logging to a file. | 208 // Do nothing if already logging to a file. |
220 if (net_log_logger_) | 209 if (net_log_logger_) |
221 return; | 210 return; |
222 | 211 |
223 base::FilePath file_path(file_name); | 212 base::FilePath file_path(file_name); |
224 FILE* file = base::OpenFile(file_path, "w"); | 213 FILE* file = base::OpenFile(file_path, "w"); |
225 if (!file) | 214 if (!file) |
226 return; | 215 return; |
227 | 216 |
228 scoped_ptr<base::Value> constants(net::NetLogLogger::GetConstants()); | 217 scoped_ptr<base::Value> constants(net::NetLogLogger::GetConstants()); |
229 net_log_logger_.reset(new net::NetLogLogger(file, *constants)); | 218 net_log_logger_.reset(new net::NetLogLogger(file, *constants)); |
230 net_log_logger_->StartObserving(context_->net_log()); | 219 net_log_logger_->StartObserving(context_->net_log()); |
231 } | 220 } |
232 | 221 |
233 void URLRequestContextAdapter::StopNetLog() { | 222 void CronetURLRequestContextAdapter::StopNetLog() { |
234 if (net_log_logger_) { | 223 if (net_log_logger_) { |
235 net_log_logger_->StopObserving(); | 224 net_log_logger_->StopObserving(); |
236 net_log_logger_.reset(); | 225 net_log_logger_.reset(); |
237 } | 226 } |
238 } | 227 } |
239 | |
240 void NetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) { | |
241 VLOG(2) << "Net log entry: type=" << entry.type() | |
242 << ", source=" << entry.source().type << ", phase=" << entry.phase(); | |
243 } | |
244 | |
245 } // namespace cronet | 228 } // namespace cronet |
OLD | NEW |