Ignore title parameter for navigator.registerProtocolHandler
[chromium-blink-merge.git] / components / cronet / android / url_request_context_peer.cc
bloba66edb9fe440d455799fbd9652c9d966eca4ea0f
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"
22 namespace {
24 class BasicNetworkDelegate : public net::NetworkDelegate {
25 public:
26 BasicNetworkDelegate() {}
27 virtual ~BasicNetworkDelegate() {}
29 private:
30 // net::NetworkDelegate implementation.
31 virtual int OnBeforeURLRequest(net::URLRequest* request,
32 const net::CompletionCallback& callback,
33 GURL* new_url) OVERRIDE {
34 return net::OK;
37 virtual int OnBeforeSendHeaders(net::URLRequest* request,
38 const net::CompletionCallback& callback,
39 net::HttpRequestHeaders* headers) OVERRIDE {
40 return net::OK;
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 {
52 return net::OK;
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 {
80 return false;
83 virtual bool OnCanSetCookie(const net::URLRequest& request,
84 const std::string& cookie_line,
85 net::CookieOptions* options) OVERRIDE {
86 return false;
89 virtual bool OnCanAccessFile(const net::URLRequest& request,
90 const base::FilePath& path) const OVERRIDE {
91 return false;
94 virtual bool OnCanThrottleRequest(const net::URLRequest& request)
95 const OVERRIDE {
96 return false;
99 virtual int OnBeforeSocketStreamConnect(
100 net::SocketStream* stream,
101 const net::CompletionCallback& callback) OVERRIDE {
102 return net::OK;
105 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate);
108 } // namespace
110 namespace cronet {
112 URLRequestContextPeer::URLRequestContextPeer(
113 URLRequestContextPeerDelegate* delegate,
114 std::string user_agent,
115 int logging_level,
116 const char* version) {
117 delegate_ = delegate;
118 user_agent_ = user_agent;
119 logging_level_ = logging_level;
120 version_ = version;
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(
130 FROM_HERE,
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());
143 if (VLOG_IS_ON(2)) {
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();
159 StopNetLog();
162 const std::string& URLRequestContextPeer::GetUserAgent(const GURL& url) const {
163 return user_agent_;
166 net::URLRequestContext* URLRequestContextPeer::GetURLRequestContext() {
167 if (!context_) {
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.
180 if (net_log_logger_)
181 return;
183 base::FilePath file_path(file_name);
184 FILE* file = base::OpenFile(file_path, "w");
185 if (!file)
186 return;
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) {
201 if (VLOG_IS_ON(2)) {
202 VLOG(2) << "Net log entry: type=" << entry.type()
203 << ", source=" << entry.source().type
204 << ", phase=" << entry.phase();
208 } // namespace cronet