OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ios/crnet/crnet_environment.h" | 5 #include "ios/crnet/crnet_environment.h" |
6 | 6 |
7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
8 | 8 |
9 #include "base/at_exit.h" | 9 #include "base/at_exit.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/files/file_path.h" |
| 12 #include "base/files/file_util.h" |
| 13 #include "base/files/scoped_file.h" |
11 #include "base/i18n/icu_util.h" | 14 #include "base/i18n/icu_util.h" |
12 #include "base/json/json_writer.h" | 15 #include "base/json/json_writer.h" |
13 #include "base/mac/bind_objc_block.h" | 16 #include "base/mac/bind_objc_block.h" |
14 #include "base/mac/foundation_util.h" | 17 #include "base/mac/foundation_util.h" |
15 #include "base/mac/scoped_block.h" | 18 #include "base/mac/scoped_block.h" |
16 #include "base/path_service.h" | 19 #include "base/path_service.h" |
17 #include "base/threading/worker_pool.h" | 20 #include "base/threading/worker_pool.h" |
18 #import "components/webp_transcode/webp_network_client_factory.h" | 21 #import "components/webp_transcode/webp_network_client_factory.h" |
19 #include "crypto/nss_util.h" | 22 #include "crypto/nss_util.h" |
20 #include "ios/net/cookies/cookie_store_ios.h" | 23 #include "ios/net/cookies/cookie_store_ios.h" |
21 #include "ios/net/crn_http_protocol_handler.h" | 24 #include "ios/net/crn_http_protocol_handler.h" |
22 #include "ios/net/request_tracker.h" | 25 #include "ios/net/request_tracker.h" |
23 #include "ios/web/public/user_agent.h" | 26 #include "ios/web/public/user_agent.h" |
24 #include "net/base/net_errors.h" | 27 #include "net/base/net_errors.h" |
25 #include "net/base/network_change_notifier.h" | 28 #include "net/base/network_change_notifier.h" |
26 #include "net/base/sdch_manager.h" | 29 #include "net/base/sdch_manager.h" |
27 #include "net/cert/cert_verifier.h" | 30 #include "net/cert/cert_verifier.h" |
28 #include "net/cert_net/nss_ocsp.h" | 31 #include "net/cert_net/nss_ocsp.h" |
29 #include "net/disk_cache/disk_cache.h" | 32 #include "net/disk_cache/disk_cache.h" |
30 #include "net/http/http_auth_handler_factory.h" | 33 #include "net/http/http_auth_handler_factory.h" |
31 #include "net/http/http_cache.h" | 34 #include "net/http/http_cache.h" |
32 #include "net/http/http_server_properties_impl.h" | 35 #include "net/http/http_server_properties_impl.h" |
33 #include "net/http/http_stream_factory.h" | 36 #include "net/http/http_stream_factory.h" |
34 #include "net/http/http_util.h" | 37 #include "net/http/http_util.h" |
| 38 #include "net/log/net_log.h" |
| 39 #include "net/log/write_to_file_net_log_observer.h" |
35 #include "net/proxy/proxy_service.h" | 40 #include "net/proxy/proxy_service.h" |
36 #include "net/socket/next_proto.h" | 41 #include "net/socket/next_proto.h" |
37 #include "net/ssl/channel_id_service.h" | 42 #include "net/ssl/channel_id_service.h" |
38 #include "net/ssl/default_channel_id_store.h" | 43 #include "net/ssl/default_channel_id_store.h" |
39 #include "net/ssl/ssl_config_service_defaults.h" | 44 #include "net/ssl/ssl_config_service_defaults.h" |
40 #include "net/url_request/data_protocol_handler.h" | 45 #include "net/url_request/data_protocol_handler.h" |
41 #include "net/url_request/file_protocol_handler.h" | 46 #include "net/url_request/file_protocol_handler.h" |
42 #include "net/url_request/sdch_dictionary_fetcher.h" | 47 #include "net/url_request/sdch_dictionary_fetcher.h" |
43 #include "net/url_request/static_http_user_agent_settings.h" | 48 #include "net/url_request/static_http_user_agent_settings.h" |
44 #include "net/url_request/url_request_context_getter.h" | 49 #include "net/url_request/url_request_context_getter.h" |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 // Create a message loop on the UI thread. | 152 // Create a message loop on the UI thread. |
148 base::MessageLoop* main_message_loop = | 153 base::MessageLoop* main_message_loop = |
149 new base::MessageLoop(base::MessageLoop::TYPE_UI); | 154 new base::MessageLoop(base::MessageLoop::TYPE_UI); |
150 #pragma unused(main_message_loop) | 155 #pragma unused(main_message_loop) |
151 base::MessageLoopForUI::current()->Attach(); | 156 base::MessageLoopForUI::current()->Attach(); |
152 } | 157 } |
153 | 158 |
154 void CrNetEnvironment::StartNetLog(base::FilePath::StringType file_name, | 159 void CrNetEnvironment::StartNetLog(base::FilePath::StringType file_name, |
155 bool log_bytes) { | 160 bool log_bytes) { |
156 DCHECK(file_name.length()); | 161 DCHECK(file_name.length()); |
157 base::AutoLock lock(net_log_lock_); | |
158 if (net_log_started_) { | |
159 return; | |
160 } | |
161 net_log_started_ = true; | |
162 PostToFileUserBlockingThread(FROM_HERE, | 162 PostToFileUserBlockingThread(FROM_HERE, |
163 base::Bind(&CrNetEnvironment::StartNetLogInternal, | 163 base::Bind(&CrNetEnvironment::StartNetLogInternal, |
164 base::Unretained(this), file_name, log_bytes)); | 164 base::Unretained(this), file_name, log_bytes)); |
165 } | 165 } |
166 | 166 |
167 void CrNetEnvironment::StartNetLogInternal( | 167 void CrNetEnvironment::StartNetLogInternal( |
168 base::FilePath::StringType file_name, bool log_bytes) { | 168 base::FilePath::StringType file_name, bool log_bytes) { |
169 DCHECK(base::MessageLoop::current() == | 169 DCHECK(base::MessageLoop::current() == |
170 file_user_blocking_thread_->message_loop()); | 170 file_user_blocking_thread_->message_loop()); |
171 DCHECK(file_name.length()); | 171 DCHECK(file_name.length()); |
172 if (!net_log_.get()) { | 172 DCHECK(net_log_); |
173 net_log_.reset(new CrNetNetLog()); | 173 |
174 main_context_.get()->set_net_log(net_log_.get()); | 174 if (net_log_observer_) |
175 } | 175 return; |
176 CrNetNetLog::Mode mode = log_bytes ? CrNetNetLog::LOG_ALL_BYTES : | 176 |
177 CrNetNetLog::LOG_STRIP_PRIVATE_DATA; | 177 base::FilePath temp_dir; |
178 net_log_->Start(base::FilePath(file_name), mode); | 178 if (!base::GetTempDir(&temp_dir)) |
| 179 return; |
| 180 |
| 181 base::FilePath full_path = temp_dir.Append(file_name); |
| 182 base::ScopedFILE file(base::OpenFile(full_path, "w")); |
| 183 if (!file) |
| 184 return; |
| 185 |
| 186 net::NetLogCaptureMode capture_mode = log_bytes ? |
| 187 net::NetLogCaptureMode::IncludeSocketBytes() : |
| 188 net::NetLogCaptureMode::Default(); |
| 189 |
| 190 net_log_observer_.reset(new net::WriteToFileNetLogObserver()); |
| 191 net_log_observer_->set_capture_mode(capture_mode); |
| 192 net_log_observer_->StartObserving(net_log_.get(), file.Pass(), nullptr, |
| 193 nullptr); |
179 } | 194 } |
180 | 195 |
181 void CrNetEnvironment::StopNetLog() { | 196 void CrNetEnvironment::StopNetLog() { |
182 base::AutoLock lock(net_log_lock_); | |
183 if (!net_log_started_) { | |
184 return; | |
185 } | |
186 net_log_started_ = false; | |
187 PostToFileUserBlockingThread(FROM_HERE, | 197 PostToFileUserBlockingThread(FROM_HERE, |
188 base::Bind(&CrNetEnvironment::StopNetLogInternal, | 198 base::Bind(&CrNetEnvironment::StopNetLogInternal, |
189 base::Unretained(this))); | 199 base::Unretained(this))); |
190 } | 200 } |
191 | 201 |
192 void CrNetEnvironment::StopNetLogInternal() { | 202 void CrNetEnvironment::StopNetLogInternal() { |
193 DCHECK(base::MessageLoop::current() == | 203 DCHECK(base::MessageLoop::current() == |
194 file_user_blocking_thread_->message_loop()); | 204 file_user_blocking_thread_->message_loop()); |
195 if (net_log_.get()) { | 205 if (net_log_observer_) { |
196 net_log_->Stop(); | 206 net_log_observer_->StopObserving(nullptr); |
| 207 net_log_observer_.reset(); |
197 } | 208 } |
198 } | 209 } |
199 | 210 |
200 void CrNetEnvironment::CloseAllSpdySessions() { | 211 void CrNetEnvironment::CloseAllSpdySessions() { |
201 PostToNetworkThread(FROM_HERE, | 212 PostToNetworkThread(FROM_HERE, |
202 base::Bind(&CrNetEnvironment::CloseAllSpdySessionsInternal, | 213 base::Bind(&CrNetEnvironment::CloseAllSpdySessionsInternal, |
203 base::Unretained(this))); | 214 base::Unretained(this))); |
204 } | 215 } |
205 | 216 |
206 void CrNetEnvironment::SetRequestFilterBlock(RequestFilterBlock block) { | 217 void CrNetEnvironment::SetRequestFilterBlock(RequestFilterBlock block) { |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 network_io_thread_->message_loop_proxy(), nullptr)); | 274 network_io_thread_->message_loop_proxy(), nullptr)); |
264 | 275 |
265 PostToNetworkThread(FROM_HERE, | 276 PostToNetworkThread(FROM_HERE, |
266 base::Bind(&CrNetEnvironment::InitializeOnNetworkThread, | 277 base::Bind(&CrNetEnvironment::InitializeOnNetworkThread, |
267 base::Unretained(this))); | 278 base::Unretained(this))); |
268 | 279 |
269 net::SetURLRequestContextForNSSHttpIO(main_context_.get()); | 280 net::SetURLRequestContextForNSSHttpIO(main_context_.get()); |
270 main_context_getter_ = new CrNetURLRequestContextGetter( | 281 main_context_getter_ = new CrNetURLRequestContextGetter( |
271 main_context_.get(), network_io_thread_->message_loop_proxy()); | 282 main_context_.get(), network_io_thread_->message_loop_proxy()); |
272 SetRequestFilterBlock(nil); | 283 SetRequestFilterBlock(nil); |
273 net_log_started_ = false; | |
274 } | 284 } |
275 | 285 |
276 void CrNetEnvironment::InstallIntoSessionConfiguration( | 286 void CrNetEnvironment::InstallIntoSessionConfiguration( |
277 NSURLSessionConfiguration* config) { | 287 NSURLSessionConfiguration* config) { |
278 config.protocolClasses = @[ [CRNHTTPProtocolHandler class] ]; | 288 config.protocolClasses = @[ [CRNHTTPProtocolHandler class] ]; |
279 } | 289 } |
280 | 290 |
281 CrNetEnvironment::~CrNetEnvironment() { | 291 CrNetEnvironment::~CrNetEnvironment() { |
282 net::HTTPProtocolHandlerDelegate::SetInstance(nullptr); | 292 net::HTTPProtocolHandlerDelegate::SetInstance(nullptr); |
283 net::SetURLRequestContextForNSSHttpIO(nullptr); | 293 net::SetURLRequestContextForNSSHttpIO(nullptr); |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
416 scoped_refptr<net::CookieStore> cookie_store = | 426 scoped_refptr<net::CookieStore> cookie_store = |
417 net::CookieStoreIOS::CreateCookieStoreFromNSHTTPCookieStorage(); | 427 net::CookieStoreIOS::CreateCookieStoreFromNSHTTPCookieStorage(); |
418 main_context_->set_cookie_store(cookie_store.get()); | 428 main_context_->set_cookie_store(cookie_store.get()); |
419 | 429 |
420 net::URLRequestJobFactoryImpl* job_factory = | 430 net::URLRequestJobFactoryImpl* job_factory = |
421 new net::URLRequestJobFactoryImpl; | 431 new net::URLRequestJobFactoryImpl; |
422 job_factory->SetProtocolHandler("data", new net::DataProtocolHandler); | 432 job_factory->SetProtocolHandler("data", new net::DataProtocolHandler); |
423 job_factory->SetProtocolHandler( | 433 job_factory->SetProtocolHandler( |
424 "file", new net::FileProtocolHandler(file_thread_->message_loop_proxy())); | 434 "file", new net::FileProtocolHandler(file_thread_->message_loop_proxy())); |
425 main_context_->set_job_factory(job_factory); | 435 main_context_->set_job_factory(job_factory); |
| 436 |
| 437 net_log_.reset(new net::NetLog()); |
| 438 main_context_->set_net_log(net_log_.get()); |
426 } | 439 } |
427 | 440 |
428 std::string CrNetEnvironment::user_agent() { | 441 std::string CrNetEnvironment::user_agent() { |
429 const net::HttpUserAgentSettings* user_agent_settings = | 442 const net::HttpUserAgentSettings* user_agent_settings = |
430 main_context_->http_user_agent_settings(); | 443 main_context_->http_user_agent_settings(); |
431 if (!user_agent_settings) { | 444 if (!user_agent_settings) { |
432 return nullptr; | 445 return nullptr; |
433 } | 446 } |
434 | 447 |
435 return user_agent_settings->GetUserAgent(); | 448 return user_agent_settings->GetUserAgent(); |
(...skipping 19 matching lines...) Expand all Loading... |
455 if (backend) | 468 if (backend) |
456 backend->DoomAllEntries(client_callback); | 469 backend->DoomAllEntries(client_callback); |
457 }); | 470 }); |
458 int rc = cache->GetBackend(&backend, doom_callback); | 471 int rc = cache->GetBackend(&backend, doom_callback); |
459 if (rc != net::ERR_IO_PENDING) { | 472 if (rc != net::ERR_IO_PENDING) { |
460 // GetBackend doesn't call the callback if it completes synchronously, so | 473 // GetBackend doesn't call the callback if it completes synchronously, so |
461 // call it directly here. | 474 // call it directly here. |
462 doom_callback.Run(rc); | 475 doom_callback.Run(rc); |
463 } | 476 } |
464 } | 477 } |
OLD | NEW |