Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(114)

Side by Side Diff: components/cronet/android/cronet_url_request_context_adapter.cc

Issue 586143002: Initial implementation of Cronet Async API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added ReportMockErrorFromQuery to URLRequestMockHTTPJob. Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
16 #include "net/http/http_server_properties.h" 16 #include "net/http/http_server_properties.h"
17 #include "net/proxy/proxy_config_service_fixed.h" 17 #include "net/proxy/proxy_config_service_fixed.h"
18 #include "net/proxy/proxy_service.h" 18 #include "net/proxy/proxy_service.h"
19 #include "net/ssl/ssl_config_service_defaults.h" 19 #include "net/ssl/ssl_config_service_defaults.h"
20 #include "net/url_request/static_http_user_agent_settings.h" 20 #include "net/url_request/static_http_user_agent_settings.h"
21 #include "net/url_request/url_request_context.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
27 class BasicNetworkDelegate : public net::NetworkDelegate { 28 class BasicNetworkDelegate : public net::NetworkDelegate {
28 public: 29 public:
29 BasicNetworkDelegate() {} 30 BasicNetworkDelegate() {}
30 virtual ~BasicNetworkDelegate() {} 31 virtual ~BasicNetworkDelegate() {}
31 32
32 private: 33 private:
33 // net::NetworkDelegate implementation. 34 // net::NetworkDelegate implementation.
34 virtual int OnBeforeURLRequest(net::URLRequest* request, 35 virtual int OnBeforeURLRequest(net::URLRequest* request,
35 const net::CompletionCallback& callback, 36 const net::CompletionCallback& callback,
36 GURL* new_url) override { 37 GURL* new_url) OVERRIDE {
37 return net::OK; 38 return net::OK;
38 } 39 }
39 40
40 virtual int OnBeforeSendHeaders(net::URLRequest* request, 41 virtual int OnBeforeSendHeaders(net::URLRequest* request,
41 const net::CompletionCallback& callback, 42 const net::CompletionCallback& callback,
42 net::HttpRequestHeaders* headers) override { 43 net::HttpRequestHeaders* headers) OVERRIDE {
43 return net::OK; 44 return net::OK;
44 } 45 }
45 46
46 virtual void OnSendHeaders(net::URLRequest* request, 47 virtual void OnSendHeaders(net::URLRequest* request,
47 const net::HttpRequestHeaders& headers) override {} 48 const net::HttpRequestHeaders& headers) OVERRIDE {}
48 49
49 virtual int OnHeadersReceived( 50 virtual int OnHeadersReceived(
50 net::URLRequest* request, 51 net::URLRequest* request,
51 const net::CompletionCallback& callback, 52 const net::CompletionCallback& callback,
52 const net::HttpResponseHeaders* original_response_headers, 53 const net::HttpResponseHeaders* original_response_headers,
53 scoped_refptr<net::HttpResponseHeaders>* _response_headers, 54 scoped_refptr<net::HttpResponseHeaders>* _response_headers,
54 GURL* allowed_unsafe_redirect_url) override { 55 GURL* allowed_unsafe_redirect_url) OVERRIDE {
55 return net::OK; 56 return net::OK;
56 } 57 }
57 58
58 virtual void OnBeforeRedirect(net::URLRequest* request, 59 virtual void OnBeforeRedirect(net::URLRequest* request,
59 const GURL& new_location) override {} 60 const GURL& new_location) OVERRIDE {}
60 61
61 virtual void OnResponseStarted(net::URLRequest* request) override {} 62 virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE {}
62 63
63 virtual void OnRawBytesRead(const net::URLRequest& request, 64 virtual void OnRawBytesRead(const net::URLRequest& request,
64 int bytes_read) override {} 65 int bytes_read) OVERRIDE {}
65 66
66 virtual void OnCompleted(net::URLRequest* request, bool started) override {} 67 virtual void OnCompleted(net::URLRequest* request, bool started) OVERRIDE {}
67 68
68 virtual void OnURLRequestDestroyed(net::URLRequest* request) override {} 69 virtual void OnURLRequestDestroyed(net::URLRequest* request) OVERRIDE {}
69 70
70 virtual void OnPACScriptError(int line_number, 71 virtual void OnPACScriptError(int line_number,
71 const base::string16& error) override {} 72 const base::string16& error) OVERRIDE {}
72 73
73 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired( 74 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
74 net::URLRequest* request, 75 net::URLRequest* request,
75 const net::AuthChallengeInfo& auth_info, 76 const net::AuthChallengeInfo& auth_info,
76 const AuthCallback& callback, 77 const AuthCallback& callback,
77 net::AuthCredentials* credentials) override { 78 net::AuthCredentials* credentials) OVERRIDE {
78 return net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION; 79 return net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
79 } 80 }
80 81
81 virtual bool OnCanGetCookies(const net::URLRequest& request, 82 virtual bool OnCanGetCookies(const net::URLRequest& request,
82 const net::CookieList& cookie_list) override { 83 const net::CookieList& cookie_list) OVERRIDE {
83 return false; 84 return false;
84 } 85 }
85 86
86 virtual bool OnCanSetCookie(const net::URLRequest& request, 87 virtual bool OnCanSetCookie(const net::URLRequest& request,
87 const std::string& cookie_line, 88 const std::string& cookie_line,
88 net::CookieOptions* options) override { 89 net::CookieOptions* options) OVERRIDE {
89 return false; 90 return false;
90 } 91 }
91 92
92 virtual bool OnCanAccessFile(const net::URLRequest& request, 93 virtual bool OnCanAccessFile(const net::URLRequest& request,
93 const base::FilePath& path) const override { 94 const base::FilePath& path) const OVERRIDE {
94 return false; 95 return false;
95 } 96 }
96 97
97 virtual bool OnCanThrottleRequest( 98 virtual bool OnCanThrottleRequest(
98 const net::URLRequest& request) const override { 99 const net::URLRequest& request) const OVERRIDE {
99 return false; 100 return false;
100 } 101 }
101 102
102 virtual int OnBeforeSocketStreamConnect( 103 virtual int OnBeforeSocketStreamConnect(
103 net::SocketStream* stream, 104 net::SocketStream* stream,
104 const net::CompletionCallback& callback) override { 105 const net::CompletionCallback& callback) OVERRIDE {
105 return net::OK; 106 return net::OK;
106 } 107 }
107 108
108 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate); 109 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate);
109 }; 110 };
110 111
111 } // namespace 112 } // namespace
112 113
113 namespace cronet { 114 namespace cronet {
114 115
115 URLRequestContextAdapter::URLRequestContextAdapter( 116 CronetURLRequestContextAdapter::CronetURLRequestContextAdapter(
116 URLRequestContextAdapterDelegate* delegate, 117 CronetURLRequestContextAdapterDelegate* delegate) {
117 std::string user_agent) {
118 delegate_ = delegate; 118 delegate_ = delegate;
119 user_agent_ = user_agent;
120 } 119 }
121 120
122 void URLRequestContextAdapter::Initialize( 121 void CronetURLRequestContextAdapter::Initialize(
123 scoped_ptr<URLRequestContextConfig> config) { 122 scoped_ptr<URLRequestContextConfig> config) {
124 network_thread_ = new base::Thread("network"); 123 network_thread_ = new base::Thread("network");
125 base::Thread::Options options; 124 base::Thread::Options options;
126 options.message_loop_type = base::MessageLoop::TYPE_IO; 125 options.message_loop_type = base::MessageLoop::TYPE_IO;
127 network_thread_->StartWithOptions(options); 126 network_thread_->StartWithOptions(options);
128 127
129 GetNetworkTaskRunner()->PostTask( 128 GetNetworkTaskRunner()->PostTask(
130 FROM_HERE, 129 FROM_HERE,
131 base::Bind(&URLRequestContextAdapter::InitializeURLRequestContext, 130 base::Bind(&CronetURLRequestContextAdapter::InitializeOnNetworkThread,
132 this, 131 this,
133 Passed(&config))); 132 Passed(&config)));
134 } 133 }
135 134
136 void URLRequestContextAdapter::InitializeURLRequestContext( 135 void CronetURLRequestContextAdapter::InitializeOnNetworkThread(
137 scoped_ptr<URLRequestContextConfig> config) { 136 scoped_ptr<URLRequestContextConfig> config) {
138 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
139 // TODO(mmenke): Add method to have the builder enable SPDY. 137 // TODO(mmenke): Add method to have the builder enable SPDY.
140 net::URLRequestContextBuilder context_builder; 138 net::URLRequestContextBuilder context_builder;
141 context_builder.set_network_delegate(new BasicNetworkDelegate()); 139 context_builder.set_network_delegate(new BasicNetworkDelegate());
142 context_builder.set_proxy_config_service( 140 context_builder.set_proxy_config_service(
143 new net::ProxyConfigServiceFixed(net::ProxyConfig())); 141 new net::ProxyConfigServiceFixed(net::ProxyConfig()));
144 config->ConfigureURLRequestContextBuilder(&context_builder); 142 config->ConfigureURLRequestContextBuilder(&context_builder);
145 143
146 context_.reset(context_builder.Build()); 144 context_.reset(context_builder.Build());
147 145
148 // Currently (circa M39) enabling QUIC requires setting probability threshold. 146 // Currently (circa M39) enabling QUIC requires setting probability threshold.
149 if (config->enable_quic) { 147 if (config->enable_quic) {
150 context_->http_server_properties() 148 context_->http_server_properties()
151 ->SetAlternateProtocolProbabilityThreshold(0.0f); 149 ->SetAlternateProtocolProbabilityThreshold(0.0f);
152 for (size_t hint = 0; hint < config->quic_hints.size(); ++hint) { 150 for (size_t hint = 0; hint < config->quic_hints.size(); ++hint) {
153 const URLRequestContextConfig::QuicHint& quic_hint = 151 const URLRequestContextConfig::QuicHint& quic_hint =
154 *config->quic_hints[hint]; 152 *config->quic_hints[hint];
155 if (quic_hint.host.empty()) { 153 if (quic_hint.host.empty()) {
156 LOG(ERROR) << "Empty QUIC hint host: " << quic_hint.host; 154 LOG(ERROR) << "Empty QUIC hint host: " << quic_hint.host;
157 continue; 155 continue;
158 } 156 }
159 157
160 if (quic_hint.port <= std::numeric_limits<uint16>::min() || 158 if (quic_hint.port <= std::numeric_limits<uint16>::min() ||
161 quic_hint.port > std::numeric_limits<uint16>::max()) { 159 quic_hint.port > std::numeric_limits<uint16>::max()) {
162 LOG(ERROR) << "Invalid QUIC hint port: " 160 LOG(ERROR) << "Invalid QUIC hint port: " << quic_hint.port;
163 << quic_hint.port;
164 continue; 161 continue;
165 } 162 }
166 163
167 if (quic_hint.alternate_port <= std::numeric_limits<uint16>::min() || 164 if (quic_hint.alternate_port <= std::numeric_limits<uint16>::min() ||
168 quic_hint.alternate_port > std::numeric_limits<uint16>::max()) { 165 quic_hint.alternate_port > std::numeric_limits<uint16>::max()) {
169 LOG(ERROR) << "Invalid QUIC hint alternate port: " 166 LOG(ERROR) << "Invalid QUIC hint alternate port: "
170 << quic_hint.alternate_port; 167 << quic_hint.alternate_port;
171 continue; 168 continue;
172 } 169 }
173 170
174 net::HostPortPair quic_hint_host_port_pair(quic_hint.host, 171 net::HostPortPair quic_hint_host_port_pair(quic_hint.host,
175 quic_hint.port); 172 quic_hint.port);
176 context_->http_server_properties()->SetAlternateProtocol( 173 context_->http_server_properties()->SetAlternateProtocol(
177 quic_hint_host_port_pair, 174 quic_hint_host_port_pair,
178 static_cast<uint16>(quic_hint.alternate_port), 175 static_cast<uint16>(quic_hint.alternate_port),
179 net::AlternateProtocol::QUIC, 176 net::AlternateProtocol::QUIC,
180 1.0f); 177 1.0f);
181 } 178 }
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 } 179 }
188 180 user_agent_ = config->user_agent;
189 if (VLOG_IS_ON(2)) {
190 net_log_observer_.reset(new NetLogObserver());
191 context_->net_log()->AddThreadSafeObserver(net_log_observer_.get(),
192 net::NetLog::LOG_ALL_BUT_BYTES);
193 }
194 181
195 delegate_->OnContextInitialized(this); 182 delegate_->OnContextInitialized(this);
196 } 183 }
197 184
198 void URLRequestContextAdapter::PostTaskToNetworkThread( 185 CronetURLRequestContextAdapter::~CronetURLRequestContextAdapter() {
199 const tracked_objects::Location& posted_from, 186 StopNetLog();
200 const RunAfterContextInitTask& callback) {
201 GetNetworkTaskRunner()->PostTask(
202 posted_from,
203 base::Bind(
204 &URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread,
205 this,
206 callback));
207 }
208
209 void URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread(
210 const RunAfterContextInitTask& callback) {
211 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
212 if (is_context_initialized_) {
213 callback.Run();
214 return;
215 }
216 tasks_waiting_for_context_.push(callback);
217 }
218
219 URLRequestContextAdapter::~URLRequestContextAdapter() {
220 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
221 if (net_log_observer_) {
222 context_->net_log()->RemoveThreadSafeObserver(net_log_observer_.get());
223 net_log_observer_.reset();
224 }
225 StopNetLogHelper();
226 // TODO(mef): Ensure that |network_thread_| is destroyed properly. 187 // TODO(mef): Ensure that |network_thread_| is destroyed properly.
227 } 188 }
228 189
229 const std::string& URLRequestContextAdapter::GetUserAgent( 190 const std::string& CronetURLRequestContextAdapter::GetUserAgent(
230 const GURL& url) const { 191 const GURL& url) const {
231 return user_agent_; 192 return user_agent_;
232 } 193 }
233 194
234 net::URLRequestContext* URLRequestContextAdapter::GetURLRequestContext() { 195 net::URLRequestContext* CronetURLRequestContextAdapter::GetURLRequestContext() {
235 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
236 if (!context_) { 196 if (!context_) {
237 LOG(ERROR) << "URLRequestContext is not set up"; 197 LOG(ERROR) << "URLRequestContext is not set up";
238 } 198 }
239 return context_.get(); 199 return context_.get();
240 } 200 }
241 201
242 scoped_refptr<base::SingleThreadTaskRunner> 202 scoped_refptr<base::SingleThreadTaskRunner>
243 URLRequestContextAdapter::GetNetworkTaskRunner() const { 203 CronetURLRequestContextAdapter::GetNetworkTaskRunner() const {
244 return network_thread_->message_loop_proxy(); 204 return network_thread_->message_loop_proxy();
245 } 205 }
246 206
247 void URLRequestContextAdapter::StartNetLogToFile(const std::string& file_name) { 207 void CronetURLRequestContextAdapter::StartNetLogToFile(
248 PostTaskToNetworkThread( 208 const std::string& file_name) {
209 GetNetworkTaskRunner()->PostTask(
249 FROM_HERE, 210 FROM_HERE,
250 base::Bind( 211 base::Bind(
251 &URLRequestContextAdapter::StartNetLogToFileHelper, this, file_name)); 212 &CronetURLRequestContextAdapter::StartNetLogToFileOnNetworkThread,
213 this,
214 file_name));
252 } 215 }
253 216
254 void URLRequestContextAdapter::StopNetLog() { 217 void CronetURLRequestContextAdapter::StopNetLog() {
255 PostTaskToNetworkThread( 218 GetNetworkTaskRunner()->PostTask(
256 FROM_HERE, base::Bind(&URLRequestContextAdapter::StopNetLogHelper, this)); 219 FROM_HERE,
220 base::Bind(&CronetURLRequestContextAdapter::StopNetLogOnNetworkThread,
221 this));
257 } 222 }
258 223
259 void URLRequestContextAdapter::StartNetLogToFileHelper( 224 void CronetURLRequestContextAdapter::StartNetLogToFileOnNetworkThread(
260 const std::string& file_name) { 225 const std::string& file_name) {
261 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
262 // Do nothing if already logging to a file. 226 // Do nothing if already logging to a file.
263 if (net_log_logger_) 227 if (net_log_logger_)
264 return; 228 return;
265 229
266 base::FilePath file_path(file_name); 230 base::FilePath file_path(file_name);
267 FILE* file = base::OpenFile(file_path, "w"); 231 FILE* file = base::OpenFile(file_path, "w");
268 if (!file) 232 if (!file)
269 return; 233 return;
270 234
271 scoped_ptr<base::Value> constants(net::NetLogLogger::GetConstants()); 235 scoped_ptr<base::Value> constants(net::NetLogLogger::GetConstants());
272 net_log_logger_.reset(new net::NetLogLogger(file, *constants)); 236 net_log_logger_.reset(new net::NetLogLogger(file, *constants));
273 net_log_logger_->StartObserving(context_->net_log()); 237 net_log_logger_->StartObserving(context_->net_log());
274 } 238 }
275 239
276 void URLRequestContextAdapter::StopNetLogHelper() { 240 void CronetURLRequestContextAdapter::StopNetLogOnNetworkThread() {
277 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
278 if (net_log_logger_) { 241 if (net_log_logger_) {
279 net_log_logger_->StopObserving(); 242 net_log_logger_->StopObserving();
280 net_log_logger_.reset(); 243 net_log_logger_.reset();
281 } 244 }
282 } 245 }
283 246
284 void NetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) {
285 VLOG(2) << "Net log entry: type=" << entry.type()
286 << ", source=" << entry.source().type << ", phase=" << entry.phase();
287 }
288
289 } // namespace cronet 247 } // namespace cronet
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698