[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 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 | |
[email protected] | fa5f474 | 2014-08-08 21:36:19 | [diff] [blame] | 5 | #include "components/cronet/android/url_request_context_adapter.h" |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 6 | |
xunjieli | 926338d | 2014-10-15 15:38:40 | [diff] [blame] | 7 | #include <limits> |
| 8 | |
[email protected] | 94de3e0 | 2014-06-17 00:09:51 | [diff] [blame] | 9 | #include "base/bind.h" |
thestig | 819adcc8 | 2014-09-10 22:24:53 | [diff] [blame] | 10 | #include "base/files/file_util.h" |
mmenke | 853ed15d | 2015-03-16 17:38:49 | [diff] [blame] | 11 | #include "base/files/scoped_file.h" |
xunjieli | 0b21f8f | 2014-10-17 12:58:28 | [diff] [blame] | 12 | #include "base/message_loop/message_loop.h" |
[email protected] | 94de3e0 | 2014-06-17 00:09:51 | [diff] [blame] | 13 | #include "base/single_thread_task_runner.h" |
| 14 | #include "components/cronet/url_request_context_config.h" |
xunjieli | 071779c | 2014-10-28 20:07:51 | [diff] [blame] | 15 | #include "net/android/network_change_notifier_factory_android.h" |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 16 | #include "net/base/net_errors.h" |
mef | f937604 | 2014-10-20 22:42:12 | [diff] [blame] | 17 | #include "net/base/net_util.h" |
xunjieli | 071779c | 2014-10-28 20:07:51 | [diff] [blame] | 18 | #include "net/base/network_change_notifier.h" |
megjablon | c175145 | 2014-12-09 19:46:47 | [diff] [blame] | 19 | #include "net/base/network_delegate_impl.h" |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 20 | #include "net/cert/cert_verifier.h" |
| 21 | #include "net/http/http_auth_handler_factory.h" |
| 22 | #include "net/http/http_network_layer.h" |
mef | c71361c | 2014-09-16 14:48:56 | [diff] [blame] | 23 | #include "net/http/http_server_properties.h" |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 24 | #include "net/log/write_to_file_net_log_observer.h" |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 25 | #include "net/proxy/proxy_service.h" |
xunjieli | b8a6d56f | 2015-04-29 17:36:14 | [diff] [blame] | 26 | #include "net/sdch/sdch_owner.h" |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 27 | #include "net/ssl/ssl_config_service_defaults.h" |
| 28 | #include "net/url_request/static_http_user_agent_settings.h" |
[email protected] | c7b1ddc7 | 2014-04-18 19:09:43 | [diff] [blame] | 29 | #include "net/url_request/url_request_context_builder.h" |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 30 | #include "net/url_request/url_request_context_storage.h" |
| 31 | #include "net/url_request/url_request_job_factory_impl.h" |
| 32 | |
| 33 | namespace { |
| 34 | |
megjablon | c175145 | 2014-12-09 19:46:47 | [diff] [blame] | 35 | class BasicNetworkDelegate : public net::NetworkDelegateImpl { |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 36 | public: |
| 37 | BasicNetworkDelegate() {} |
dcheng | e27b1ef | 2015-02-04 23:34:11 | [diff] [blame] | 38 | ~BasicNetworkDelegate() override {} |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 39 | |
| 40 | private: |
| 41 | // net::NetworkDelegate implementation. |
xunjieli | 926338d | 2014-10-15 15:38:40 | [diff] [blame] | 42 | int OnBeforeURLRequest(net::URLRequest* request, |
| 43 | const net::CompletionCallback& callback, |
| 44 | GURL* new_url) override { |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 45 | return net::OK; |
| 46 | } |
| 47 | |
xunjieli | 926338d | 2014-10-15 15:38:40 | [diff] [blame] | 48 | int OnBeforeSendHeaders(net::URLRequest* request, |
| 49 | const net::CompletionCallback& callback, |
| 50 | net::HttpRequestHeaders* headers) override { |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 51 | return net::OK; |
| 52 | } |
| 53 | |
xunjieli | 926338d | 2014-10-15 15:38:40 | [diff] [blame] | 54 | void OnSendHeaders(net::URLRequest* request, |
| 55 | const net::HttpRequestHeaders& headers) override {} |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 56 | |
xunjieli | 926338d | 2014-10-15 15:38:40 | [diff] [blame] | 57 | int OnHeadersReceived( |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 58 | net::URLRequest* request, |
| 59 | const net::CompletionCallback& callback, |
| 60 | const net::HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 61 | scoped_refptr<net::HttpResponseHeaders>* _response_headers, |
mostynb | fe59f48 | 2014-10-06 15:04:46 | [diff] [blame] | 62 | GURL* allowed_unsafe_redirect_url) override { |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 63 | return net::OK; |
| 64 | } |
| 65 | |
xunjieli | 926338d | 2014-10-15 15:38:40 | [diff] [blame] | 66 | void OnBeforeRedirect(net::URLRequest* request, |
| 67 | const GURL& new_location) override {} |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 68 | |
xunjieli | 926338d | 2014-10-15 15:38:40 | [diff] [blame] | 69 | void OnResponseStarted(net::URLRequest* request) override {} |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 70 | |
xunjieli | 926338d | 2014-10-15 15:38:40 | [diff] [blame] | 71 | void OnRawBytesRead(const net::URLRequest& request, |
| 72 | int bytes_read) override {} |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 73 | |
xunjieli | 926338d | 2014-10-15 15:38:40 | [diff] [blame] | 74 | void OnCompleted(net::URLRequest* request, bool started) override {} |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 75 | |
xunjieli | 926338d | 2014-10-15 15:38:40 | [diff] [blame] | 76 | void OnURLRequestDestroyed(net::URLRequest* request) override {} |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 77 | |
xunjieli | 926338d | 2014-10-15 15:38:40 | [diff] [blame] | 78 | void OnPACScriptError(int line_number, |
| 79 | const base::string16& error) override {} |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 80 | |
xunjieli | 926338d | 2014-10-15 15:38:40 | [diff] [blame] | 81 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 82 | net::URLRequest* request, |
| 83 | const net::AuthChallengeInfo& auth_info, |
| 84 | const AuthCallback& callback, |
mostynb | fe59f48 | 2014-10-06 15:04:46 | [diff] [blame] | 85 | net::AuthCredentials* credentials) override { |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 86 | return net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION; |
| 87 | } |
| 88 | |
xunjieli | 926338d | 2014-10-15 15:38:40 | [diff] [blame] | 89 | bool OnCanGetCookies(const net::URLRequest& request, |
| 90 | const net::CookieList& cookie_list) override { |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 91 | return false; |
| 92 | } |
| 93 | |
xunjieli | 926338d | 2014-10-15 15:38:40 | [diff] [blame] | 94 | bool OnCanSetCookie(const net::URLRequest& request, |
| 95 | const std::string& cookie_line, |
| 96 | net::CookieOptions* options) override { |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 97 | return false; |
| 98 | } |
| 99 | |
xunjieli | 926338d | 2014-10-15 15:38:40 | [diff] [blame] | 100 | bool OnCanAccessFile(const net::URLRequest& request, |
| 101 | const base::FilePath& path) const override { |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 102 | return false; |
| 103 | } |
| 104 | |
xunjieli | 926338d | 2014-10-15 15:38:40 | [diff] [blame] | 105 | bool OnCanThrottleRequest( |
mostynb | fe59f48 | 2014-10-06 15:04:46 | [diff] [blame] | 106 | const net::URLRequest& request) const override { |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 107 | return false; |
| 108 | } |
| 109 | |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 110 | DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate); |
| 111 | }; |
| 112 | |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 113 | } // namespace |
| 114 | |
[email protected] | f478a960 | 2014-04-23 01:48:08 | [diff] [blame] | 115 | namespace cronet { |
| 116 | |
[email protected] | fa5f474 | 2014-08-08 21:36:19 | [diff] [blame] | 117 | URLRequestContextAdapter::URLRequestContextAdapter( |
| 118 | URLRequestContextAdapterDelegate* delegate, |
mef | bb4f45c | 2015-01-12 18:03:25 | [diff] [blame] | 119 | std::string user_agent) |
| 120 | : load_disable_cache_(true), |
| 121 | is_context_initialized_(false) { |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 122 | delegate_ = delegate; |
| 123 | user_agent_ = user_agent; |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 124 | } |
| 125 | |
[email protected] | fa5f474 | 2014-08-08 21:36:19 | [diff] [blame] | 126 | void URLRequestContextAdapter::Initialize( |
[email protected] | 94de3e0 | 2014-06-17 00:09:51 | [diff] [blame] | 127 | scoped_ptr<URLRequestContextConfig> config) { |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 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); |
xunjieli | 0b21f8f | 2014-10-17 12:58:28 | [diff] [blame] | 132 | config_ = config.Pass(); |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 133 | } |
| 134 | |
xunjieli | 0b21f8f | 2014-10-17 12:58:28 | [diff] [blame] | 135 | void URLRequestContextAdapter::InitRequestContextOnMainThread() { |
xunjieli | 0b21f8f | 2014-10-17 12:58:28 | [diff] [blame] | 136 | proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService( |
| 137 | GetNetworkTaskRunner(), NULL)); |
| 138 | GetNetworkTaskRunner()->PostTask( |
| 139 | FROM_HERE, |
| 140 | base::Bind(&URLRequestContextAdapter::InitRequestContextOnNetworkThread, |
| 141 | this)); |
| 142 | } |
| 143 | |
| 144 | void URLRequestContextAdapter::InitRequestContextOnNetworkThread() { |
xunjieli | be3c72f | 2014-10-10 18:29:07 | [diff] [blame] | 145 | DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
xunjieli | 0b21f8f | 2014-10-17 12:58:28 | [diff] [blame] | 146 | DCHECK(config_); |
[email protected] | d759912 | 2014-05-24 03:37:23 | [diff] [blame] | 147 | // TODO(mmenke): Add method to have the builder enable SPDY. |
[email protected] | c7b1ddc7 | 2014-04-18 19:09:43 | [diff] [blame] | 148 | net::URLRequestContextBuilder context_builder; |
| 149 | context_builder.set_network_delegate(new BasicNetworkDelegate()); |
xunjieli | 0b21f8f | 2014-10-17 12:58:28 | [diff] [blame] | 150 | context_builder.set_proxy_config_service(proxy_config_service_.get()); |
| 151 | config_->ConfigureURLRequestContextBuilder(&context_builder); |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 152 | |
[email protected] | c7b1ddc7 | 2014-04-18 19:09:43 | [diff] [blame] | 153 | context_.reset(context_builder.Build()); |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 154 | |
xunjieli | b8a6d56f | 2015-04-29 17:36:14 | [diff] [blame] | 155 | if (config_->enable_sdch) { |
| 156 | DCHECK(context_->sdch_manager()); |
| 157 | sdch_owner_.reset( |
| 158 | new net::SdchOwner(context_->sdch_manager(), context_.get())); |
| 159 | } |
| 160 | |
mef | c71361c | 2014-09-16 14:48:56 | [diff] [blame] | 161 | // Currently (circa M39) enabling QUIC requires setting probability threshold. |
xunjieli | 0b21f8f | 2014-10-17 12:58:28 | [diff] [blame] | 162 | if (config_->enable_quic) { |
mef | c71361c | 2014-09-16 14:48:56 | [diff] [blame] | 163 | context_->http_server_properties() |
bnc | 62891a5 | 2015-04-27 14:14:12 | [diff] [blame] | 164 | ->SetAlternativeServiceProbabilityThreshold(0.0f); |
xunjieli | 0b21f8f | 2014-10-17 12:58:28 | [diff] [blame] | 165 | for (size_t hint = 0; hint < config_->quic_hints.size(); ++hint) { |
mef | c71361c | 2014-09-16 14:48:56 | [diff] [blame] | 166 | const URLRequestContextConfig::QuicHint& quic_hint = |
xunjieli | 0b21f8f | 2014-10-17 12:58:28 | [diff] [blame] | 167 | *config_->quic_hints[hint]; |
mef | c71361c | 2014-09-16 14:48:56 | [diff] [blame] | 168 | if (quic_hint.host.empty()) { |
| 169 | LOG(ERROR) << "Empty QUIC hint host: " << quic_hint.host; |
| 170 | continue; |
| 171 | } |
| 172 | |
mef | f937604 | 2014-10-20 22:42:12 | [diff] [blame] | 173 | url::CanonHostInfo host_info; |
| 174 | std::string canon_host(net::CanonicalizeHost(quic_hint.host, &host_info)); |
| 175 | if (!host_info.IsIPAddress() && |
| 176 | !net::IsCanonicalizedHostCompliant(canon_host)) { |
| 177 | LOG(ERROR) << "Invalid QUIC hint host: " << quic_hint.host; |
| 178 | continue; |
| 179 | } |
| 180 | |
mef | c71361c | 2014-09-16 14:48:56 | [diff] [blame] | 181 | if (quic_hint.port <= std::numeric_limits<uint16>::min() || |
| 182 | quic_hint.port > std::numeric_limits<uint16>::max()) { |
| 183 | LOG(ERROR) << "Invalid QUIC hint port: " |
| 184 | << quic_hint.port; |
| 185 | continue; |
| 186 | } |
| 187 | |
| 188 | if (quic_hint.alternate_port <= std::numeric_limits<uint16>::min() || |
| 189 | quic_hint.alternate_port > std::numeric_limits<uint16>::max()) { |
| 190 | LOG(ERROR) << "Invalid QUIC hint alternate port: " |
| 191 | << quic_hint.alternate_port; |
| 192 | continue; |
| 193 | } |
| 194 | |
mef | f937604 | 2014-10-20 22:42:12 | [diff] [blame] | 195 | net::HostPortPair quic_hint_host_port_pair(canon_host, |
mef | c71361c | 2014-09-16 14:48:56 | [diff] [blame] | 196 | quic_hint.port); |
bnc | cacc099 | 2015-03-20 20:22:22 | [diff] [blame] | 197 | net::AlternativeService alternative_service( |
| 198 | net::AlternateProtocol::QUIC, "", |
| 199 | static_cast<uint16>(quic_hint.alternate_port)); |
| 200 | context_->http_server_properties()->SetAlternativeService( |
| 201 | quic_hint_host_port_pair, alternative_service, 1.0f); |
mef | c71361c | 2014-09-16 14:48:56 | [diff] [blame] | 202 | } |
| 203 | } |
mef | bb4f45c | 2015-01-12 18:03:25 | [diff] [blame] | 204 | load_disable_cache_ = config_->load_disable_cache; |
xunjieli | 0b21f8f | 2014-10-17 12:58:28 | [diff] [blame] | 205 | config_.reset(NULL); |
mef | c71361c | 2014-09-16 14:48:56 | [diff] [blame] | 206 | |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 207 | if (VLOG_IS_ON(2)) { |
[email protected] | f3b1c76 | 2014-08-07 17:07:20 | [diff] [blame] | 208 | net_log_observer_.reset(new NetLogObserver()); |
eroman | 001c374 | 2015-04-23 03:11:17 | [diff] [blame] | 209 | context_->net_log()->DeprecatedAddObserver( |
| 210 | net_log_observer_.get(), |
| 211 | net::NetLogCaptureMode::IncludeCookiesAndCredentials()); |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 212 | } |
| 213 | |
xunjieli | 0b21f8f | 2014-10-17 12:58:28 | [diff] [blame] | 214 | is_context_initialized_ = true; |
| 215 | while (!tasks_waiting_for_context_.empty()) { |
| 216 | tasks_waiting_for_context_.front().Run(); |
| 217 | tasks_waiting_for_context_.pop(); |
| 218 | } |
| 219 | |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 220 | delegate_->OnContextInitialized(this); |
| 221 | } |
| 222 | |
xunjieli | be3c72f | 2014-10-10 18:29:07 | [diff] [blame] | 223 | void URLRequestContextAdapter::PostTaskToNetworkThread( |
| 224 | const tracked_objects::Location& posted_from, |
| 225 | const RunAfterContextInitTask& callback) { |
| 226 | GetNetworkTaskRunner()->PostTask( |
| 227 | posted_from, |
| 228 | base::Bind( |
| 229 | &URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread, |
| 230 | this, |
| 231 | callback)); |
| 232 | } |
| 233 | |
| 234 | void URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread( |
| 235 | const RunAfterContextInitTask& callback) { |
| 236 | DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| 237 | if (is_context_initialized_) { |
| 238 | callback.Run(); |
| 239 | return; |
| 240 | } |
| 241 | tasks_waiting_for_context_.push(callback); |
| 242 | } |
| 243 | |
[email protected] | fa5f474 | 2014-08-08 21:36:19 | [diff] [blame] | 244 | URLRequestContextAdapter::~URLRequestContextAdapter() { |
xunjieli | be3c72f | 2014-10-10 18:29:07 | [diff] [blame] | 245 | DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
[email protected] | 9afe891 | 2014-04-09 16:57:25 | [diff] [blame] | 246 | if (net_log_observer_) { |
eroman | bc4e5fe | 2015-04-03 17:41:34 | [diff] [blame] | 247 | context_->net_log()->DeprecatedRemoveObserver(net_log_observer_.get()); |
[email protected] | 9afe891 | 2014-04-09 16:57:25 | [diff] [blame] | 248 | net_log_observer_.reset(); |
| 249 | } |
xunjieli | be3c72f | 2014-10-10 18:29:07 | [diff] [blame] | 250 | StopNetLogHelper(); |
[email protected] | 94de3e0 | 2014-06-17 00:09:51 | [diff] [blame] | 251 | // TODO(mef): Ensure that |network_thread_| is destroyed properly. |
[email protected] | 9afe891 | 2014-04-09 16:57:25 | [diff] [blame] | 252 | } |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 253 | |
[email protected] | fa5f474 | 2014-08-08 21:36:19 | [diff] [blame] | 254 | const std::string& URLRequestContextAdapter::GetUserAgent( |
| 255 | const GURL& url) const { |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 256 | return user_agent_; |
| 257 | } |
| 258 | |
[email protected] | fa5f474 | 2014-08-08 21:36:19 | [diff] [blame] | 259 | net::URLRequestContext* URLRequestContextAdapter::GetURLRequestContext() { |
xunjieli | be3c72f | 2014-10-10 18:29:07 | [diff] [blame] | 260 | DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 261 | if (!context_) { |
| 262 | LOG(ERROR) << "URLRequestContext is not set up"; |
| 263 | } |
| 264 | return context_.get(); |
| 265 | } |
| 266 | |
| 267 | scoped_refptr<base::SingleThreadTaskRunner> |
[email protected] | fa5f474 | 2014-08-08 21:36:19 | [diff] [blame] | 268 | URLRequestContextAdapter::GetNetworkTaskRunner() const { |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 269 | return network_thread_->task_runner(); |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 270 | } |
| 271 | |
kallasjoki | 9f27db6 | 2015-05-07 16:06:00 | [diff] [blame] | 272 | void URLRequestContextAdapter::StartNetLogToFile(const std::string& file_name, |
| 273 | bool log_all) { |
xunjieli | be3c72f | 2014-10-10 18:29:07 | [diff] [blame] | 274 | PostTaskToNetworkThread( |
| 275 | FROM_HERE, |
kallasjoki | 9f27db6 | 2015-05-07 16:06:00 | [diff] [blame] | 276 | base::Bind(&URLRequestContextAdapter::StartNetLogToFileHelper, this, |
| 277 | file_name, log_all)); |
xunjieli | be3c72f | 2014-10-10 18:29:07 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | void URLRequestContextAdapter::StopNetLog() { |
| 281 | PostTaskToNetworkThread( |
| 282 | FROM_HERE, base::Bind(&URLRequestContextAdapter::StopNetLogHelper, this)); |
| 283 | } |
| 284 | |
| 285 | void URLRequestContextAdapter::StartNetLogToFileHelper( |
kallasjoki | 9f27db6 | 2015-05-07 16:06:00 | [diff] [blame] | 286 | const std::string& file_name, bool log_all) { |
xunjieli | be3c72f | 2014-10-10 18:29:07 | [diff] [blame] | 287 | DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
[email protected] | 9afe891 | 2014-04-09 16:57:25 | [diff] [blame] | 288 | // Do nothing if already logging to a file. |
vishal.b | 94fed10 | 2015-04-23 13:10:41 | [diff] [blame] | 289 | if (write_to_file_observer_) |
[email protected] | 9afe891 | 2014-04-09 16:57:25 | [diff] [blame] | 290 | return; |
| 291 | |
| 292 | base::FilePath file_path(file_name); |
mmenke | 853ed15d | 2015-03-16 17:38:49 | [diff] [blame] | 293 | base::ScopedFILE file(base::OpenFile(file_path, "w")); |
[email protected] | 9afe891 | 2014-04-09 16:57:25 | [diff] [blame] | 294 | if (!file) |
| 295 | return; |
| 296 | |
vishal.b | 94fed10 | 2015-04-23 13:10:41 | [diff] [blame] | 297 | write_to_file_observer_.reset(new net::WriteToFileNetLogObserver()); |
kallasjoki | 9f27db6 | 2015-05-07 16:06:00 | [diff] [blame] | 298 | if (log_all) { |
| 299 | write_to_file_observer_->set_capture_mode( |
| 300 | net::NetLogCaptureMode::IncludeSocketBytes()); |
| 301 | } |
vishal.b | 94fed10 | 2015-04-23 13:10:41 | [diff] [blame] | 302 | write_to_file_observer_->StartObserving(context_->net_log(), file.Pass(), |
| 303 | nullptr, context_.get()); |
[email protected] | 9afe891 | 2014-04-09 16:57:25 | [diff] [blame] | 304 | } |
| 305 | |
xunjieli | be3c72f | 2014-10-10 18:29:07 | [diff] [blame] | 306 | void URLRequestContextAdapter::StopNetLogHelper() { |
| 307 | DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
vishal.b | 94fed10 | 2015-04-23 13:10:41 | [diff] [blame] | 308 | if (write_to_file_observer_) { |
| 309 | write_to_file_observer_->StopObserving(context_.get()); |
| 310 | write_to_file_observer_.reset(); |
[email protected] | 9afe891 | 2014-04-09 16:57:25 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 314 | void NetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) { |
[email protected] | f3b1c76 | 2014-08-07 17:07:20 | [diff] [blame] | 315 | VLOG(2) << "Net log entry: type=" << entry.type() |
[email protected] | fa5f474 | 2014-08-08 21:36:19 | [diff] [blame] | 316 | << ", source=" << entry.source().type << ", phase=" << entry.phase(); |
[email protected] | 9105e26 | 2014-03-14 16:45:32 | [diff] [blame] | 317 | } |
[email protected] | f478a960 | 2014-04-23 01:48:08 | [diff] [blame] | 318 | |
| 319 | } // namespace cronet |