OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "net/cronet/android/url_request_context_peer.h" | |
6 | |
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_storage.h" | |
19 #include "net/url_request/url_request_job_factory_impl.h" | |
20 | |
21 namespace { | |
22 | |
23 class BasicNetworkDelegate : public net::NetworkDelegate { | |
24 public: | |
25 BasicNetworkDelegate() {} | |
26 virtual ~BasicNetworkDelegate() {} | |
27 | |
28 private: | |
29 // net::NetworkDelegate implementation. | |
30 virtual int OnBeforeURLRequest(net::URLRequest* request, | |
31 const net::CompletionCallback& callback, | |
32 GURL* new_url) OVERRIDE { | |
33 return net::OK; | |
34 } | |
35 | |
36 virtual int OnBeforeSendHeaders(net::URLRequest* request, | |
37 const net::CompletionCallback& callback, | |
38 net::HttpRequestHeaders* headers) OVERRIDE { | |
39 return net::OK; | |
40 } | |
41 | |
42 virtual void OnSendHeaders(net::URLRequest* request, | |
43 const net::HttpRequestHeaders& headers) OVERRIDE {} | |
44 | |
45 virtual int OnHeadersReceived( | |
46 net::URLRequest* request, | |
47 const net::CompletionCallback& callback, | |
48 const net::HttpResponseHeaders* original_response_headers, | |
49 scoped_refptr<net::HttpResponseHeaders>* _response_headers, | |
50 GURL* allowed_unsafe_redirect_url) OVERRIDE { | |
51 return net::OK; | |
52 } | |
53 | |
54 virtual void OnBeforeRedirect(net::URLRequest* request, | |
55 const GURL& new_location) OVERRIDE {} | |
56 | |
57 virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE {} | |
58 | |
59 virtual void OnRawBytesRead(const net::URLRequest& request, | |
60 int bytes_read) OVERRIDE {} | |
61 | |
62 virtual void OnCompleted(net::URLRequest* request, bool started) OVERRIDE {} | |
63 | |
64 virtual void OnURLRequestDestroyed(net::URLRequest* request) OVERRIDE {} | |
65 | |
66 virtual void OnPACScriptError(int line_number, | |
67 const base::string16& error) OVERRIDE {} | |
68 | |
69 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired( | |
70 net::URLRequest* request, | |
71 const net::AuthChallengeInfo& auth_info, | |
72 const AuthCallback& callback, | |
73 net::AuthCredentials* credentials) OVERRIDE { | |
74 return net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION; | |
75 } | |
76 | |
77 virtual bool OnCanGetCookies(const net::URLRequest& request, | |
78 const net::CookieList& cookie_list) OVERRIDE { | |
79 return false; | |
80 } | |
81 | |
82 virtual bool OnCanSetCookie(const net::URLRequest& request, | |
83 const std::string& cookie_line, | |
84 net::CookieOptions* options) OVERRIDE { | |
85 return false; | |
86 } | |
87 | |
88 virtual bool OnCanAccessFile(const net::URLRequest& request, | |
89 const base::FilePath& path) const OVERRIDE { | |
90 return false; | |
91 } | |
92 | |
93 virtual bool OnCanThrottleRequest(const net::URLRequest& request) | |
94 const OVERRIDE { | |
95 return false; | |
96 } | |
97 | |
98 virtual int OnBeforeSocketStreamConnect( | |
99 net::SocketStream* stream, | |
100 const net::CompletionCallback& callback) OVERRIDE { | |
101 return net::OK; | |
102 } | |
103 | |
104 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate); | |
105 }; | |
106 | |
107 class BasicURLRequestContext : public net::URLRequestContext { | |
108 public: | |
109 BasicURLRequestContext() : storage_(this) {} | |
110 | |
111 net::URLRequestContextStorage* storage() { return &storage_; } | |
112 | |
113 protected: | |
114 virtual ~BasicURLRequestContext() {} | |
115 | |
116 private: | |
117 net::URLRequestContextStorage storage_; | |
118 | |
119 DISALLOW_COPY_AND_ASSIGN(BasicURLRequestContext); | |
120 }; | |
121 | |
122 } // namespace | |
123 | |
124 URLRequestContextPeer::URLRequestContextPeer( | |
125 URLRequestContextPeerDelegate* delegate, | |
126 std::string user_agent, | |
127 int logging_level, | |
128 const char* version) { | |
129 delegate_ = delegate; | |
130 user_agent_ = user_agent; | |
131 logging_level_ = logging_level; | |
132 version_ = version; | |
133 } | |
134 | |
135 void URLRequestContextPeer::Initialize() { | |
136 network_thread_ = new base::Thread("network"); | |
137 base::Thread::Options options; | |
138 options.message_loop_type = base::MessageLoop::TYPE_IO; | |
139 network_thread_->StartWithOptions(options); | |
140 | |
141 GetNetworkTaskRunner()->PostTask( | |
142 FROM_HERE, | |
143 base::Bind(&URLRequestContextPeer::InitializeURLRequestContext, this)); | |
144 } | |
145 | |
146 void URLRequestContextPeer::InitializeURLRequestContext() { | |
147 BasicURLRequestContext* context = new BasicURLRequestContext; | |
148 net::URLRequestContextStorage* storage = context->storage(); | |
149 | |
150 net::NetworkDelegate* network_delegate = new BasicNetworkDelegate; | |
151 storage->set_network_delegate(network_delegate); | |
152 | |
153 storage->set_host_resolver(net::HostResolver::CreateDefaultResolver(NULL)); | |
154 storage->set_net_log(new net::NetLog); | |
155 | |
156 net::ProxyConfigService* proxy_config_service = | |
157 new net::ProxyConfigServiceFixed(net::ProxyConfig()); | |
158 storage->set_proxy_service(net::ProxyService::CreateUsingSystemProxyResolver( | |
159 proxy_config_service, | |
160 4, // TODO(willchan): Find a better constant somewhere. | |
161 context->net_log())); | |
162 storage->set_ssl_config_service(new net::SSLConfigServiceDefaults); | |
163 storage->set_http_auth_handler_factory( | |
164 net::HttpAuthHandlerRegistryFactory::CreateDefault( | |
165 context->host_resolver())); | |
166 storage->set_transport_security_state(new net::TransportSecurityState()); | |
167 storage->set_http_server_properties(scoped_ptr<net::HttpServerProperties>( | |
168 new net::HttpServerPropertiesImpl())); | |
169 storage->set_cert_verifier(net::CertVerifier::CreateDefault()); | |
170 | |
171 net::HttpNetworkSession::Params network_session_params; | |
172 network_session_params.host_resolver = context->host_resolver(); | |
173 network_session_params.cert_verifier = context->cert_verifier(); | |
174 network_session_params.transport_security_state = | |
175 context->transport_security_state(); | |
176 network_session_params.proxy_service = context->proxy_service(); | |
177 network_session_params.ssl_config_service = context->ssl_config_service(); | |
178 network_session_params.http_auth_handler_factory = | |
179 context->http_auth_handler_factory(); | |
180 network_session_params.network_delegate = network_delegate; | |
181 network_session_params.http_server_properties = | |
182 context->http_server_properties(); | |
183 network_session_params.net_log = context->net_log(); | |
184 | |
185 scoped_refptr<net::HttpNetworkSession> network_session( | |
186 new net::HttpNetworkSession(network_session_params)); | |
187 | |
188 net::HttpTransactionFactory* http_transaction_factory = | |
189 new net::HttpNetworkLayer(network_session.get()); | |
190 storage->set_http_transaction_factory(http_transaction_factory); | |
191 | |
192 net::URLRequestJobFactoryImpl* job_factory = | |
193 new net::URLRequestJobFactoryImpl; | |
194 storage->set_job_factory(job_factory); | |
195 | |
196 if (VLOG_IS_ON(2)) { | |
197 net_log_observer_.reset(new NetLogObserver(logging_level_)); | |
198 context->net_log()->AddThreadSafeObserver(net_log_observer_.get(), | |
199 net::NetLog::LOG_ALL_BUT_BYTES); | |
200 } | |
201 | |
202 context_.reset(context); | |
203 | |
204 net::HttpStreamFactory::EnableNpnSpdy31(); | |
205 | |
206 delegate_->OnContextInitialized(this); | |
207 } | |
208 | |
209 URLRequestContextPeer::~URLRequestContextPeer() { | |
210 if (net_log_observer_) { | |
211 context_->net_log()->RemoveThreadSafeObserver(net_log_observer_.get()); | |
212 net_log_observer_.reset(); | |
213 } | |
214 StopNetLog(); | |
215 } | |
216 | |
217 const std::string& URLRequestContextPeer::GetUserAgent(const GURL& url) const { | |
218 return user_agent_; | |
219 } | |
220 | |
221 net::URLRequestContext* URLRequestContextPeer::GetURLRequestContext() { | |
222 if (!context_) { | |
223 LOG(ERROR) << "URLRequestContext is not set up"; | |
224 } | |
225 return context_.get(); | |
226 } | |
227 | |
228 scoped_refptr<base::SingleThreadTaskRunner> | |
229 URLRequestContextPeer::GetNetworkTaskRunner() const { | |
230 return network_thread_->message_loop_proxy(); | |
231 } | |
232 | |
233 void URLRequestContextPeer::StartNetLogToFile(const std::string& file_name) { | |
234 // Do nothing if already logging to a file. | |
235 if (net_log_logger_) | |
236 return; | |
237 | |
238 base::FilePath file_path(file_name); | |
239 FILE* file = base::OpenFile(file_path, "w"); | |
240 if (!file) | |
241 return; | |
242 | |
243 scoped_ptr<base::Value> constants(net::NetLogLogger::GetConstants()); | |
244 net_log_logger_.reset(new net::NetLogLogger(file, *constants)); | |
245 net_log_logger_->StartObserving(context_->net_log()); | |
246 } | |
247 | |
248 void URLRequestContextPeer::StopNetLog() { | |
249 if (net_log_logger_) { | |
250 net_log_logger_->StopObserving(); | |
251 net_log_logger_.reset(); | |
252 } | |
253 } | |
254 | |
255 void NetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) { | |
256 if (VLOG_IS_ON(2)) { | |
257 VLOG(2) << "Net log entry: type=" << entry.type() | |
258 << ", source=" << entry.source().type | |
259 << ", phase=" << entry.phase(); | |
260 } | |
261 } | |
OLD | NEW |