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_peer.h"
7 #include "base/file_util.h"
8 #include "net/base/net_errors.h"
9 #include "net/base/net_log_logger.h"
10 #include "net/cert/cert_verifier.h"
11 #include "net/http/http_auth_handler_factory.h"
12 #include "net/http/http_network_layer.h"
13 #include "net/http/http_server_properties_impl.h"
14 #include "net/proxy/proxy_config_service_fixed.h"
15 #include "net/proxy/proxy_service.h"
16 #include "net/ssl/ssl_config_service_defaults.h"
17 #include "net/url_request/static_http_user_agent_settings.h"
18 #include "net/url_request/url_request_context_builder.h"
19 #include "net/url_request/url_request_context_storage.h"
20 #include "net/url_request/url_request_job_factory_impl.h"
24 class BasicNetworkDelegate
: public net::NetworkDelegate
{
26 BasicNetworkDelegate() {}
27 virtual ~BasicNetworkDelegate() {}
30 // net::NetworkDelegate implementation.
31 virtual int OnBeforeURLRequest(net::URLRequest
* request
,
32 const net::CompletionCallback
& callback
,
33 GURL
* new_url
) OVERRIDE
{
37 virtual int OnBeforeSendHeaders(net::URLRequest
* request
,
38 const net::CompletionCallback
& callback
,
39 net::HttpRequestHeaders
* headers
) OVERRIDE
{
43 virtual void OnSendHeaders(net::URLRequest
* request
,
44 const net::HttpRequestHeaders
& headers
) OVERRIDE
{}
46 virtual int OnHeadersReceived(
47 net::URLRequest
* request
,
48 const net::CompletionCallback
& callback
,
49 const net::HttpResponseHeaders
* original_response_headers
,
50 scoped_refptr
<net::HttpResponseHeaders
>* _response_headers
,
51 GURL
* allowed_unsafe_redirect_url
) OVERRIDE
{
55 virtual void OnBeforeRedirect(net::URLRequest
* request
,
56 const GURL
& new_location
) OVERRIDE
{}
58 virtual void OnResponseStarted(net::URLRequest
* request
) OVERRIDE
{}
60 virtual void OnRawBytesRead(const net::URLRequest
& request
,
61 int bytes_read
) OVERRIDE
{}
63 virtual void OnCompleted(net::URLRequest
* request
, bool started
) OVERRIDE
{}
65 virtual void OnURLRequestDestroyed(net::URLRequest
* request
) OVERRIDE
{}
67 virtual void OnPACScriptError(int line_number
,
68 const base::string16
& error
) OVERRIDE
{}
70 virtual NetworkDelegate::AuthRequiredResponse
OnAuthRequired(
71 net::URLRequest
* request
,
72 const net::AuthChallengeInfo
& auth_info
,
73 const AuthCallback
& callback
,
74 net::AuthCredentials
* credentials
) OVERRIDE
{
75 return net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION
;
78 virtual bool OnCanGetCookies(const net::URLRequest
& request
,
79 const net::CookieList
& cookie_list
) OVERRIDE
{
83 virtual bool OnCanSetCookie(const net::URLRequest
& request
,
84 const std::string
& cookie_line
,
85 net::CookieOptions
* options
) OVERRIDE
{
89 virtual bool OnCanAccessFile(const net::URLRequest
& request
,
90 const base::FilePath
& path
) const OVERRIDE
{
94 virtual bool OnCanThrottleRequest(const net::URLRequest
& request
)
99 virtual int OnBeforeSocketStreamConnect(
100 net::SocketStream
* stream
,
101 const net::CompletionCallback
& callback
) OVERRIDE
{
105 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate
);
112 URLRequestContextPeer::URLRequestContextPeer(
113 URLRequestContextPeerDelegate
* delegate
,
114 std::string user_agent
,
116 const char* version
) {
117 delegate_
= delegate
;
118 user_agent_
= user_agent
;
119 logging_level_
= logging_level
;
123 void URLRequestContextPeer::Initialize() {
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(
131 base::Bind(&URLRequestContextPeer::InitializeURLRequestContext
, this));
134 void URLRequestContextPeer::InitializeURLRequestContext() {
135 net::URLRequestContextBuilder context_builder
;
136 context_builder
.set_network_delegate(new BasicNetworkDelegate());
137 context_builder
.set_proxy_config_service(
138 new net::ProxyConfigServiceFixed(net::ProxyConfig()));
139 context_builder
.DisableHttpCache();
141 context_
.reset(context_builder
.Build());
144 net_log_observer_
.reset(new NetLogObserver(logging_level_
));
145 context_
->net_log()->AddThreadSafeObserver(net_log_observer_
.get(),
146 net::NetLog::LOG_ALL_BUT_BYTES
);
149 net::HttpStreamFactory::EnableNpnSpdy31();
151 delegate_
->OnContextInitialized(this);
154 URLRequestContextPeer::~URLRequestContextPeer() {
155 if (net_log_observer_
) {
156 context_
->net_log()->RemoveThreadSafeObserver(net_log_observer_
.get());
157 net_log_observer_
.reset();
162 const std::string
& URLRequestContextPeer::GetUserAgent(const GURL
& url
) const {
166 net::URLRequestContext
* URLRequestContextPeer::GetURLRequestContext() {
168 LOG(ERROR
) << "URLRequestContext is not set up";
170 return context_
.get();
173 scoped_refptr
<base::SingleThreadTaskRunner
>
174 URLRequestContextPeer::GetNetworkTaskRunner() const {
175 return network_thread_
->message_loop_proxy();
178 void URLRequestContextPeer::StartNetLogToFile(const std::string
& file_name
) {
179 // Do nothing if already logging to a file.
183 base::FilePath
file_path(file_name
);
184 FILE* file
= base::OpenFile(file_path
, "w");
188 scoped_ptr
<base::Value
> constants(net::NetLogLogger::GetConstants());
189 net_log_logger_
.reset(new net::NetLogLogger(file
, *constants
));
190 net_log_logger_
->StartObserving(context_
->net_log());
193 void URLRequestContextPeer::StopNetLog() {
194 if (net_log_logger_
) {
195 net_log_logger_
->StopObserving();
196 net_log_logger_
.reset();
200 void NetLogObserver::OnAddEntry(const net::NetLog::Entry
& entry
) {
202 VLOG(2) << "Net log entry: type=" << entry
.type()
203 << ", source=" << entry
.source().type
204 << ", phase=" << entry
.phase();
208 } // namespace cronet