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