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 "components/cronet/android/url_request_context_peer.h" 6 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" 24 25 namespace { 26 27 class BasicNetworkDelegate : public net::NetworkDelegate { 28 public: 29 BasicNetworkDelegate() {} 30 virtual ~BasicNetworkDelegate() {} 31 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; 38 } 39 40 virtual int OnBeforeSendHeaders(net::URLRequest* request, 41 const net::CompletionCallback& callback, 42 net::HttpRequestHeaders* headers) OVERRIDE { 43 return net::OK; 44 } 45 46 virtual void OnSendHeaders(net::URLRequest* request, 47 const net::HttpRequestHeaders& headers) OVERRIDE {} 48 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; 56 } 57 58 virtual void OnBeforeRedirect(net::URLRequest* request, 59 const GURL& new_location) OVERRIDE {} 60 61 virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE {} 62 63 virtual void OnRawBytesRead(const net::URLRequest& request, 64 int bytes_read) OVERRIDE {} 65 66 virtual void OnCompleted(net::URLRequest* request, bool started) OVERRIDE {} 67 68 virtual void OnURLRequestDestroyed(net::URLRequest* request) OVERRIDE {} 69 70 virtual void OnPACScriptError(int line_number, 71 const base::string16& error) OVERRIDE {} 72 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; 79 } 80 81 virtual bool OnCanGetCookies(const net::URLRequest& request, 82 const net::CookieList& cookie_list) OVERRIDE { 83 return false; 84 } 85 86 virtual bool OnCanSetCookie(const net::URLRequest& request, 87 const std::string& cookie_line, 88 net::CookieOptions* options) OVERRIDE { 89 return false; 90 } 91 92 virtual bool OnCanAccessFile(const net::URLRequest& request, 93 const base::FilePath& path) const OVERRIDE { 94 return false; 95 } 96 97 virtual bool OnCanThrottleRequest(const net::URLRequest& request) 98 const OVERRIDE { 99 return false; 100 } 101 102 virtual int OnBeforeSocketStreamConnect( 103 net::SocketStream* stream, 104 const net::CompletionCallback& callback) OVERRIDE { 105 return net::OK; 106 } 107 108 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate); 109 }; 110 111 } // namespace 112 113 namespace cronet { 114 115 URLRequestContextPeer::URLRequestContextPeer( 116 URLRequestContextPeerDelegate* delegate, 117 std::string user_agent, 118 int logging_level, 119 const char* version) { 120 delegate_ = delegate; 121 user_agent_ = user_agent; 122 logging_level_ = logging_level; 123 version_ = version; 124 } 125 126 void URLRequestContextPeer::Initialize( 127 scoped_ptr<URLRequestContextConfig> config) { 128 network_thread_ = new base::Thread("network"); 129 base::Thread::Options options; 130 options.message_loop_type = base::MessageLoop::TYPE_IO; 131 network_thread_->StartWithOptions(options); 132 133 GetNetworkTaskRunner()->PostTask( 134 FROM_HERE, 135 base::Bind(&URLRequestContextPeer::InitializeURLRequestContext, 136 this, 137 Passed(&config))); 138 } 139 140 void URLRequestContextPeer::InitializeURLRequestContext( 141 scoped_ptr<URLRequestContextConfig> config) { 142 // TODO(mmenke): Add method to have the builder enable SPDY. 143 net::URLRequestContextBuilder context_builder; 144 context_builder.set_network_delegate(new BasicNetworkDelegate()); 145 context_builder.set_proxy_config_service( 146 new net::ProxyConfigServiceFixed(net::ProxyConfig())); 147 config->ConfigureURLRequestContextBuilder(&context_builder); 148 149 context_.reset(context_builder.Build()); 150 151 if (VLOG_IS_ON(2)) { 152 net_log_observer_.reset(new NetLogObserver(logging_level_)); 153 context_->net_log()->AddThreadSafeObserver(net_log_observer_.get(), 154 net::NetLog::LOG_ALL_BUT_BYTES); 155 } 156 157 delegate_->OnContextInitialized(this); 158 } 159 160 URLRequestContextPeer::~URLRequestContextPeer() { 161 if (net_log_observer_) { 162 context_->net_log()->RemoveThreadSafeObserver(net_log_observer_.get()); 163 net_log_observer_.reset(); 164 } 165 StopNetLog(); 166 // TODO(mef): Ensure that |network_thread_| is destroyed properly. 167 } 168 169 const std::string& URLRequestContextPeer::GetUserAgent(const GURL& url) const { 170 return user_agent_; 171 } 172 173 net::URLRequestContext* URLRequestContextPeer::GetURLRequestContext() { 174 if (!context_) { 175 LOG(ERROR) << "URLRequestContext is not set up"; 176 } 177 return context_.get(); 178 } 179 180 scoped_refptr<base::SingleThreadTaskRunner> 181 URLRequestContextPeer::GetNetworkTaskRunner() const { 182 return network_thread_->message_loop_proxy(); 183 } 184 185 void URLRequestContextPeer::StartNetLogToFile(const std::string& file_name) { 186 // Do nothing if already logging to a file. 187 if (net_log_logger_) 188 return; 189 190 base::FilePath file_path(file_name); 191 FILE* file = base::OpenFile(file_path, "w"); 192 if (!file) 193 return; 194 195 scoped_ptr<base::Value> constants(net::NetLogLogger::GetConstants()); 196 net_log_logger_.reset(new net::NetLogLogger(file, *constants)); 197 net_log_logger_->StartObserving(context_->net_log()); 198 } 199 200 void URLRequestContextPeer::StopNetLog() { 201 if (net_log_logger_) { 202 net_log_logger_->StopObserving(); 203 net_log_logger_.reset(); 204 } 205 } 206 207 void NetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) { 208 if (VLOG_IS_ON(2)) { 209 VLOG(2) << "Net log entry: type=" << entry.type() 210 << ", source=" << entry.source().type 211 << ", phase=" << entry.phase(); 212 } 213 } 214 215 } // namespace cronet 216