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

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: Make UrlRequestFactory into a class that can create factories. 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"
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 return net::OK; 105 return net::OK;
106 } 106 }
107 107
108 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate); 108 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate);
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) { 117 std::string user_agent) {
118 delegate_ = delegate; 118 delegate_ = delegate;
119 user_agent_ = user_agent; 119 user_agent_ = user_agent;
120 } 120 }
121 121
122 void URLRequestContextAdapter::Initialize( 122 void CronetURLRequestContextAdapter::Initialize(
123 scoped_ptr<URLRequestContextConfig> config) { 123 scoped_ptr<URLRequestContextConfig> config) {
124 network_thread_ = new base::Thread("network"); 124 network_thread_ = new base::Thread("network");
125 base::Thread::Options options; 125 base::Thread::Options options;
126 options.message_loop_type = base::MessageLoop::TYPE_IO; 126 options.message_loop_type = base::MessageLoop::TYPE_IO;
127 network_thread_->StartWithOptions(options); 127 network_thread_->StartWithOptions(options);
128 128
129 GetNetworkTaskRunner()->PostTask( 129 GetNetworkTaskRunner()->PostTask(
130 FROM_HERE, 130 FROM_HERE,
131 base::Bind(&URLRequestContextAdapter::InitializeURLRequestContext, 131 base::Bind(&CronetURLRequestContextAdapter::InitializeURLRequestContext,
132 this, 132 this,
133 Passed(&config))); 133 Passed(&config)));
134 } 134 }
135 135
136 void URLRequestContextAdapter::InitializeURLRequestContext( 136 void CronetURLRequestContextAdapter::InitializeURLRequestContext(
137 scoped_ptr<URLRequestContextConfig> config) { 137 scoped_ptr<URLRequestContextConfig> config) {
138 // TODO(mmenke): Add method to have the builder enable SPDY. 138 // TODO(mmenke): Add method to have the builder enable SPDY.
139 net::URLRequestContextBuilder context_builder; 139 net::URLRequestContextBuilder context_builder;
140 context_builder.set_network_delegate(new BasicNetworkDelegate()); 140 context_builder.set_network_delegate(new BasicNetworkDelegate());
141 context_builder.set_proxy_config_service( 141 context_builder.set_proxy_config_service(
142 new net::ProxyConfigServiceFixed(net::ProxyConfig())); 142 new net::ProxyConfigServiceFixed(net::ProxyConfig()));
143 config->ConfigureURLRequestContextBuilder(&context_builder); 143 config->ConfigureURLRequestContextBuilder(&context_builder);
144 144
145 context_.reset(context_builder.Build()); 145 context_.reset(context_builder.Build());
146 146
147 // Currently (circa M39) enabling QUIC requires setting probability threshold. 147 // Currently (circa M39) enabling QUIC requires setting probability threshold.
148 if (config->enable_quic) { 148 if (config->enable_quic) {
149 context_->http_server_properties() 149 context_->http_server_properties()
150 ->SetAlternateProtocolProbabilityThreshold(0.0f); 150 ->SetAlternateProtocolProbabilityThreshold(0.0f);
151 for (size_t hint = 0; hint < config->quic_hints.size(); ++hint) { 151 for (size_t hint = 0; hint < config->quic_hints.size(); ++hint) {
152 const URLRequestContextConfig::QuicHint& quic_hint = 152 const URLRequestContextConfig::QuicHint& quic_hint =
153 *config->quic_hints[hint]; 153 *config->quic_hints[hint];
154 if (quic_hint.host.empty()) { 154 if (quic_hint.host.empty()) {
155 LOG(ERROR) << "Empty QUIC hint host: " << quic_hint.host; 155 LOG(ERROR) << "Empty QUIC hint host: " << quic_hint.host;
156 continue; 156 continue;
157 } 157 }
158 158
159 if (quic_hint.port <= std::numeric_limits<uint16>::min() || 159 if (quic_hint.port <= std::numeric_limits<uint16>::min() ||
160 quic_hint.port > std::numeric_limits<uint16>::max()) { 160 quic_hint.port > std::numeric_limits<uint16>::max()) {
161 LOG(ERROR) << "Invalid QUIC hint port: " 161 LOG(ERROR) << "Invalid QUIC hint port: " << quic_hint.port;
162 << quic_hint.port;
163 continue; 162 continue;
164 } 163 }
165 164
166 if (quic_hint.alternate_port <= std::numeric_limits<uint16>::min() || 165 if (quic_hint.alternate_port <= std::numeric_limits<uint16>::min() ||
167 quic_hint.alternate_port > std::numeric_limits<uint16>::max()) { 166 quic_hint.alternate_port > std::numeric_limits<uint16>::max()) {
168 LOG(ERROR) << "Invalid QUIC hint alternate port: " 167 LOG(ERROR) << "Invalid QUIC hint alternate port: "
169 << quic_hint.alternate_port; 168 << quic_hint.alternate_port;
170 continue; 169 continue;
171 } 170 }
172 171
173 net::HostPortPair quic_hint_host_port_pair(quic_hint.host, 172 net::HostPortPair quic_hint_host_port_pair(quic_hint.host,
174 quic_hint.port); 173 quic_hint.port);
175 context_->http_server_properties()->SetAlternateProtocol( 174 context_->http_server_properties()->SetAlternateProtocol(
176 quic_hint_host_port_pair, 175 quic_hint_host_port_pair,
177 static_cast<uint16>(quic_hint.alternate_port), 176 static_cast<uint16>(quic_hint.alternate_port),
178 net::AlternateProtocol::QUIC, 177 net::AlternateProtocol::QUIC,
179 1.0f); 178 1.0f);
180 } 179 }
181 } 180 }
182 181
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
189 delegate_->OnContextInitialized(this); 182 delegate_->OnContextInitialized(this);
190 } 183 }
191 184
192 URLRequestContextAdapter::~URLRequestContextAdapter() { 185 CronetURLRequestContextAdapter::~CronetURLRequestContextAdapter() {
193 if (net_log_observer_) {
194 context_->net_log()->RemoveThreadSafeObserver(net_log_observer_.get());
195 net_log_observer_.reset();
196 }
197 StopNetLog(); 186 StopNetLog();
198 // TODO(mef): Ensure that |network_thread_| is destroyed properly. 187 // TODO(mef): Ensure that |network_thread_| is destroyed properly.
199 } 188 }
200 189
201 const std::string& URLRequestContextAdapter::GetUserAgent( 190 const std::string& CronetURLRequestContextAdapter::GetUserAgent(
202 const GURL& url) const { 191 const GURL& url) const {
203 return user_agent_; 192 return user_agent_;
204 } 193 }
205 194
206 net::URLRequestContext* URLRequestContextAdapter::GetURLRequestContext() { 195 net::URLRequestContext* CronetURLRequestContextAdapter::GetURLRequestContext() {
207 if (!context_) { 196 if (!context_) {
208 LOG(ERROR) << "URLRequestContext is not set up"; 197 LOG(ERROR) << "URLRequestContext is not set up";
209 } 198 }
210 return context_.get(); 199 return context_.get();
211 } 200 }
212 201
213 scoped_refptr<base::SingleThreadTaskRunner> 202 scoped_refptr<base::SingleThreadTaskRunner>
214 URLRequestContextAdapter::GetNetworkTaskRunner() const { 203 CronetURLRequestContextAdapter::GetNetworkTaskRunner() const {
215 return network_thread_->message_loop_proxy(); 204 return network_thread_->message_loop_proxy();
216 } 205 }
217 206
218 void URLRequestContextAdapter::StartNetLogToFile(const std::string& file_name) { 207 void CronetURLRequestContextAdapter::StartNetLogToFile(
208 const std::string& file_name) {
219 // Do nothing if already logging to a file. 209 // Do nothing if already logging to a file.
220 if (net_log_logger_) 210 if (net_log_logger_)
221 return; 211 return;
222 212
223 base::FilePath file_path(file_name); 213 base::FilePath file_path(file_name);
224 FILE* file = base::OpenFile(file_path, "w"); 214 FILE* file = base::OpenFile(file_path, "w");
225 if (!file) 215 if (!file)
226 return; 216 return;
227 217
228 scoped_ptr<base::Value> constants(net::NetLogLogger::GetConstants()); 218 scoped_ptr<base::Value> constants(net::NetLogLogger::GetConstants());
229 net_log_logger_.reset(new net::NetLogLogger(file, *constants)); 219 net_log_logger_.reset(new net::NetLogLogger(file, *constants));
230 net_log_logger_->StartObserving(context_->net_log()); 220 net_log_logger_->StartObserving(context_->net_log());
231 } 221 }
232 222
233 void URLRequestContextAdapter::StopNetLog() { 223 void CronetURLRequestContextAdapter::StopNetLog() {
234 if (net_log_logger_) { 224 if (net_log_logger_) {
235 net_log_logger_->StopObserving(); 225 net_log_logger_->StopObserving();
236 net_log_logger_.reset(); 226 net_log_logger_.reset();
237 } 227 }
238 } 228 }
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 229 } // namespace cronet
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698