ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / components / cronet / android / url_request_context_adapter.cc
blob00daf654752fdce46f6342fee84aaef1d74d82ed
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
3 // found in the LICENSE file.
5 #include "components/cronet/android/url_request_context_adapter.h"
7 #include <limits>
9 #include "base/bind.h"
10 #include "base/files/file_util.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/single_thread_task_runner.h"
13 #include "components/cronet/url_request_context_config.h"
14 #include "net/android/network_change_notifier_factory_android.h"
15 #include "net/base/net_errors.h"
16 #include "net/base/net_log_logger.h"
17 #include "net/base/net_util.h"
18 #include "net/base/network_change_notifier.h"
19 #include "net/base/network_delegate_impl.h"
20 #include "net/cert/cert_verifier.h"
21 #include "net/http/http_auth_handler_factory.h"
22 #include "net/http/http_network_layer.h"
23 #include "net/http/http_server_properties.h"
24 #include "net/proxy/proxy_service.h"
25 #include "net/ssl/ssl_config_service_defaults.h"
26 #include "net/url_request/static_http_user_agent_settings.h"
27 #include "net/url_request/url_request_context_builder.h"
28 #include "net/url_request/url_request_context_storage.h"
29 #include "net/url_request/url_request_job_factory_impl.h"
31 namespace {
33 class BasicNetworkDelegate : public net::NetworkDelegateImpl {
34 public:
35 BasicNetworkDelegate() {}
36 ~BasicNetworkDelegate() override {}
38 private:
39 // net::NetworkDelegate implementation.
40 int OnBeforeURLRequest(net::URLRequest* request,
41 const net::CompletionCallback& callback,
42 GURL* new_url) override {
43 return net::OK;
46 int OnBeforeSendHeaders(net::URLRequest* request,
47 const net::CompletionCallback& callback,
48 net::HttpRequestHeaders* headers) override {
49 return net::OK;
52 void OnSendHeaders(net::URLRequest* request,
53 const net::HttpRequestHeaders& headers) override {}
55 int OnHeadersReceived(
56 net::URLRequest* request,
57 const net::CompletionCallback& callback,
58 const net::HttpResponseHeaders* original_response_headers,
59 scoped_refptr<net::HttpResponseHeaders>* _response_headers,
60 GURL* allowed_unsafe_redirect_url) override {
61 return net::OK;
64 void OnBeforeRedirect(net::URLRequest* request,
65 const GURL& new_location) override {}
67 void OnResponseStarted(net::URLRequest* request) override {}
69 void OnRawBytesRead(const net::URLRequest& request,
70 int bytes_read) override {}
72 void OnCompleted(net::URLRequest* request, bool started) override {}
74 void OnURLRequestDestroyed(net::URLRequest* request) override {}
76 void OnPACScriptError(int line_number,
77 const base::string16& error) override {}
79 NetworkDelegate::AuthRequiredResponse OnAuthRequired(
80 net::URLRequest* request,
81 const net::AuthChallengeInfo& auth_info,
82 const AuthCallback& callback,
83 net::AuthCredentials* credentials) override {
84 return net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
87 bool OnCanGetCookies(const net::URLRequest& request,
88 const net::CookieList& cookie_list) override {
89 return false;
92 bool OnCanSetCookie(const net::URLRequest& request,
93 const std::string& cookie_line,
94 net::CookieOptions* options) override {
95 return false;
98 bool OnCanAccessFile(const net::URLRequest& request,
99 const base::FilePath& path) const override {
100 return false;
103 bool OnCanThrottleRequest(
104 const net::URLRequest& request) const override {
105 return false;
108 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate);
111 } // namespace
113 namespace cronet {
115 URLRequestContextAdapter::URLRequestContextAdapter(
116 URLRequestContextAdapterDelegate* delegate,
117 std::string user_agent)
118 : load_disable_cache_(true),
119 is_context_initialized_(false) {
120 delegate_ = delegate;
121 user_agent_ = user_agent;
124 void URLRequestContextAdapter::Initialize(
125 scoped_ptr<URLRequestContextConfig> config) {
126 network_thread_ = new base::Thread("network");
127 base::Thread::Options options;
128 options.message_loop_type = base::MessageLoop::TYPE_IO;
129 network_thread_->StartWithOptions(options);
130 config_ = config.Pass();
133 void URLRequestContextAdapter::InitRequestContextOnMainThread() {
134 proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService(
135 GetNetworkTaskRunner(), NULL));
136 GetNetworkTaskRunner()->PostTask(
137 FROM_HERE,
138 base::Bind(&URLRequestContextAdapter::InitRequestContextOnNetworkThread,
139 this));
142 void URLRequestContextAdapter::InitRequestContextOnNetworkThread() {
143 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
144 DCHECK(config_);
145 // TODO(mmenke): Add method to have the builder enable SPDY.
146 net::URLRequestContextBuilder context_builder;
147 context_builder.set_network_delegate(new BasicNetworkDelegate());
148 context_builder.set_proxy_config_service(proxy_config_service_.get());
149 config_->ConfigureURLRequestContextBuilder(&context_builder);
151 context_.reset(context_builder.Build());
153 // Currently (circa M39) enabling QUIC requires setting probability threshold.
154 if (config_->enable_quic) {
155 context_->http_server_properties()
156 ->SetAlternateProtocolProbabilityThreshold(0.0f);
157 for (size_t hint = 0; hint < config_->quic_hints.size(); ++hint) {
158 const URLRequestContextConfig::QuicHint& quic_hint =
159 *config_->quic_hints[hint];
160 if (quic_hint.host.empty()) {
161 LOG(ERROR) << "Empty QUIC hint host: " << quic_hint.host;
162 continue;
165 url::CanonHostInfo host_info;
166 std::string canon_host(net::CanonicalizeHost(quic_hint.host, &host_info));
167 if (!host_info.IsIPAddress() &&
168 !net::IsCanonicalizedHostCompliant(canon_host)) {
169 LOG(ERROR) << "Invalid QUIC hint host: " << quic_hint.host;
170 continue;
173 if (quic_hint.port <= std::numeric_limits<uint16>::min() ||
174 quic_hint.port > std::numeric_limits<uint16>::max()) {
175 LOG(ERROR) << "Invalid QUIC hint port: "
176 << quic_hint.port;
177 continue;
180 if (quic_hint.alternate_port <= std::numeric_limits<uint16>::min() ||
181 quic_hint.alternate_port > std::numeric_limits<uint16>::max()) {
182 LOG(ERROR) << "Invalid QUIC hint alternate port: "
183 << quic_hint.alternate_port;
184 continue;
187 net::HostPortPair quic_hint_host_port_pair(canon_host,
188 quic_hint.port);
189 context_->http_server_properties()->SetAlternateProtocol(
190 quic_hint_host_port_pair,
191 static_cast<uint16>(quic_hint.alternate_port),
192 net::AlternateProtocol::QUIC,
193 1.0f);
196 load_disable_cache_ = config_->load_disable_cache;
197 config_.reset(NULL);
199 if (VLOG_IS_ON(2)) {
200 net_log_observer_.reset(new NetLogObserver());
201 context_->net_log()->AddThreadSafeObserver(net_log_observer_.get(),
202 net::NetLog::LOG_ALL_BUT_BYTES);
205 is_context_initialized_ = true;
206 while (!tasks_waiting_for_context_.empty()) {
207 tasks_waiting_for_context_.front().Run();
208 tasks_waiting_for_context_.pop();
211 delegate_->OnContextInitialized(this);
214 void URLRequestContextAdapter::PostTaskToNetworkThread(
215 const tracked_objects::Location& posted_from,
216 const RunAfterContextInitTask& callback) {
217 GetNetworkTaskRunner()->PostTask(
218 posted_from,
219 base::Bind(
220 &URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread,
221 this,
222 callback));
225 void URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread(
226 const RunAfterContextInitTask& callback) {
227 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
228 if (is_context_initialized_) {
229 callback.Run();
230 return;
232 tasks_waiting_for_context_.push(callback);
235 URLRequestContextAdapter::~URLRequestContextAdapter() {
236 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
237 if (net_log_observer_) {
238 context_->net_log()->RemoveThreadSafeObserver(net_log_observer_.get());
239 net_log_observer_.reset();
241 StopNetLogHelper();
242 // TODO(mef): Ensure that |network_thread_| is destroyed properly.
245 const std::string& URLRequestContextAdapter::GetUserAgent(
246 const GURL& url) const {
247 return user_agent_;
250 net::URLRequestContext* URLRequestContextAdapter::GetURLRequestContext() {
251 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
252 if (!context_) {
253 LOG(ERROR) << "URLRequestContext is not set up";
255 return context_.get();
258 scoped_refptr<base::SingleThreadTaskRunner>
259 URLRequestContextAdapter::GetNetworkTaskRunner() const {
260 return network_thread_->message_loop_proxy();
263 void URLRequestContextAdapter::StartNetLogToFile(const std::string& file_name) {
264 PostTaskToNetworkThread(
265 FROM_HERE,
266 base::Bind(
267 &URLRequestContextAdapter::StartNetLogToFileHelper, this, file_name));
270 void URLRequestContextAdapter::StopNetLog() {
271 PostTaskToNetworkThread(
272 FROM_HERE, base::Bind(&URLRequestContextAdapter::StopNetLogHelper, this));
275 void URLRequestContextAdapter::StartNetLogToFileHelper(
276 const std::string& file_name) {
277 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
278 // Do nothing if already logging to a file.
279 if (net_log_logger_)
280 return;
282 base::FilePath file_path(file_name);
283 FILE* file = base::OpenFile(file_path, "w");
284 if (!file)
285 return;
287 scoped_ptr<base::Value> constants(net::NetLogLogger::GetConstants());
288 net_log_logger_.reset(new net::NetLogLogger(file, *constants));
289 net_log_logger_->StartObserving(context_->net_log());
292 void URLRequestContextAdapter::StopNetLogHelper() {
293 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
294 if (net_log_logger_) {
295 net_log_logger_->StopObserving();
296 net_log_logger_.reset();
300 void NetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) {
301 VLOG(2) << "Net log entry: type=" << entry.type()
302 << ", source=" << entry.source().type << ", phase=" << entry.phase();
305 } // namespace cronet