Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / components / cronet / android / url_request_context_adapter.cc
blob0feb0f13e4ad825768ca041f4665061cfcafeba8
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 "base/bind.h"
8 #include "base/file_util.h"
9 #include "base/single_thread_task_runner.h"
10 #include "components/cronet/url_request_context_config.h"
11 #include "net/base/net_errors.h"
12 #include "net/base/net_log_logger.h"
13 #include "net/cert/cert_verifier.h"
14 #include "net/http/http_auth_handler_factory.h"
15 #include "net/http/http_network_layer.h"
16 #include "net/http/http_server_properties_impl.h"
17 #include "net/proxy/proxy_config_service_fixed.h"
18 #include "net/proxy/proxy_service.h"
19 #include "net/ssl/ssl_config_service_defaults.h"
20 #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_storage.h"
23 #include "net/url_request/url_request_job_factory_impl.h"
25 namespace {
27 class BasicNetworkDelegate : public net::NetworkDelegate {
28 public:
29 BasicNetworkDelegate() {}
30 virtual ~BasicNetworkDelegate() {}
32 private:
33 // net::NetworkDelegate implementation.
34 virtual int OnBeforeURLRequest(net::URLRequest* request,
35 const net::CompletionCallback& callback,
36 GURL* new_url) OVERRIDE {
37 return net::OK;
40 virtual int OnBeforeSendHeaders(net::URLRequest* request,
41 const net::CompletionCallback& callback,
42 net::HttpRequestHeaders* headers) OVERRIDE {
43 return net::OK;
46 virtual void OnSendHeaders(net::URLRequest* request,
47 const net::HttpRequestHeaders& headers) OVERRIDE {}
49 virtual int OnHeadersReceived(
50 net::URLRequest* request,
51 const net::CompletionCallback& callback,
52 const net::HttpResponseHeaders* original_response_headers,
53 scoped_refptr<net::HttpResponseHeaders>* _response_headers,
54 GURL* allowed_unsafe_redirect_url) OVERRIDE {
55 return net::OK;
58 virtual void OnBeforeRedirect(net::URLRequest* request,
59 const GURL& new_location) OVERRIDE {}
61 virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE {}
63 virtual void OnRawBytesRead(const net::URLRequest& request,
64 int bytes_read) OVERRIDE {}
66 virtual void OnCompleted(net::URLRequest* request, bool started) OVERRIDE {}
68 virtual void OnURLRequestDestroyed(net::URLRequest* request) OVERRIDE {}
70 virtual void OnPACScriptError(int line_number,
71 const base::string16& error) OVERRIDE {}
73 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
74 net::URLRequest* request,
75 const net::AuthChallengeInfo& auth_info,
76 const AuthCallback& callback,
77 net::AuthCredentials* credentials) OVERRIDE {
78 return net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
81 virtual bool OnCanGetCookies(const net::URLRequest& request,
82 const net::CookieList& cookie_list) OVERRIDE {
83 return false;
86 virtual bool OnCanSetCookie(const net::URLRequest& request,
87 const std::string& cookie_line,
88 net::CookieOptions* options) OVERRIDE {
89 return false;
92 virtual bool OnCanAccessFile(const net::URLRequest& request,
93 const base::FilePath& path) const OVERRIDE {
94 return false;
97 virtual bool OnCanThrottleRequest(
98 const net::URLRequest& request) const OVERRIDE {
99 return false;
102 virtual int OnBeforeSocketStreamConnect(
103 net::SocketStream* stream,
104 const net::CompletionCallback& callback) OVERRIDE {
105 return net::OK;
108 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate);
111 } // namespace
113 namespace cronet {
115 URLRequestContextAdapter::URLRequestContextAdapter(
116 URLRequestContextAdapterDelegate* delegate,
117 std::string user_agent) {
118 delegate_ = delegate;
119 user_agent_ = user_agent;
122 void URLRequestContextAdapter::Initialize(
123 scoped_ptr<URLRequestContextConfig> config) {
124 network_thread_ = new base::Thread("network");
125 base::Thread::Options options;
126 options.message_loop_type = base::MessageLoop::TYPE_IO;
127 network_thread_->StartWithOptions(options);
129 GetNetworkTaskRunner()->PostTask(
130 FROM_HERE,
131 base::Bind(&URLRequestContextAdapter::InitializeURLRequestContext,
132 this,
133 Passed(&config)));
136 void URLRequestContextAdapter::InitializeURLRequestContext(
137 scoped_ptr<URLRequestContextConfig> config) {
138 // TODO(mmenke): Add method to have the builder enable SPDY.
139 net::URLRequestContextBuilder context_builder;
140 context_builder.set_network_delegate(new BasicNetworkDelegate());
141 context_builder.set_proxy_config_service(
142 new net::ProxyConfigServiceFixed(net::ProxyConfig()));
143 config->ConfigureURLRequestContextBuilder(&context_builder);
145 context_.reset(context_builder.Build());
147 if (VLOG_IS_ON(2)) {
148 net_log_observer_.reset(new NetLogObserver());
149 context_->net_log()->AddThreadSafeObserver(net_log_observer_.get(),
150 net::NetLog::LOG_ALL_BUT_BYTES);
153 delegate_->OnContextInitialized(this);
156 URLRequestContextAdapter::~URLRequestContextAdapter() {
157 if (net_log_observer_) {
158 context_->net_log()->RemoveThreadSafeObserver(net_log_observer_.get());
159 net_log_observer_.reset();
161 StopNetLog();
162 // TODO(mef): Ensure that |network_thread_| is destroyed properly.
165 const std::string& URLRequestContextAdapter::GetUserAgent(
166 const GURL& url) const {
167 return user_agent_;
170 net::URLRequestContext* URLRequestContextAdapter::GetURLRequestContext() {
171 if (!context_) {
172 LOG(ERROR) << "URLRequestContext is not set up";
174 return context_.get();
177 scoped_refptr<base::SingleThreadTaskRunner>
178 URLRequestContextAdapter::GetNetworkTaskRunner() const {
179 return network_thread_->message_loop_proxy();
182 void URLRequestContextAdapter::StartNetLogToFile(const std::string& file_name) {
183 // Do nothing if already logging to a file.
184 if (net_log_logger_)
185 return;
187 base::FilePath file_path(file_name);
188 FILE* file = base::OpenFile(file_path, "w");
189 if (!file)
190 return;
192 scoped_ptr<base::Value> constants(net::NetLogLogger::GetConstants());
193 net_log_logger_.reset(new net::NetLogLogger(file, *constants));
194 net_log_logger_->StartObserving(context_->net_log());
197 void URLRequestContextAdapter::StopNetLog() {
198 if (net_log_logger_) {
199 net_log_logger_->StopObserving();
200 net_log_logger_.reset();
204 void NetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) {
205 VLOG(2) << "Net log entry: type=" << entry.type()
206 << ", source=" << entry.source().type << ", phase=" << entry.phase();
209 } // namespace cronet