| 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 <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/at_exit.h" | 11 #include "base/at_exit.h" |
| 12 #include "base/atomicops.h" | 12 #include "base/atomicops.h" |
| 13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 15 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
| 16 #include "base/files/scoped_file.h" | 16 #include "base/files/scoped_file.h" |
| 17 #include "base/i18n/icu_util.h" | 17 #include "base/i18n/icu_util.h" |
| 18 #include "base/json/json_writer.h" | 18 #include "base/json/json_writer.h" |
| 19 #include "base/mac/bind_objc_block.h" | 19 #include "base/mac/bind_objc_block.h" |
| 20 #include "base/mac/foundation_util.h" | 20 #include "base/mac/foundation_util.h" |
| 21 #include "base/mac/scoped_block.h" | 21 #include "base/mac/scoped_block.h" |
| 22 #include "base/macros.h" | 22 #include "base/macros.h" |
| 23 #include "base/memory/ptr_util.h" | 23 #include "base/memory/ptr_util.h" |
| 24 #include "base/metrics/statistics_recorder.h" | 24 #include "base/metrics/statistics_recorder.h" |
| 25 #include "base/path_service.h" | 25 #include "base/path_service.h" |
| 26 #include "base/single_thread_task_runner.h" |
| 26 #include "base/threading/worker_pool.h" | 27 #include "base/threading/worker_pool.h" |
| 27 #include "components/prefs/json_pref_store.h" | 28 #include "components/prefs/json_pref_store.h" |
| 28 #include "components/prefs/pref_filter.h" | 29 #include "components/prefs/pref_filter.h" |
| 29 #include "ios/crnet/sdch_owner_pref_storage.h" | 30 #include "ios/crnet/sdch_owner_pref_storage.h" |
| 30 #include "ios/net/cookies/cookie_store_ios.h" | 31 #include "ios/net/cookies/cookie_store_ios.h" |
| 31 #include "ios/net/crn_http_protocol_handler.h" | 32 #include "ios/net/crn_http_protocol_handler.h" |
| 32 #include "ios/net/empty_nsurlcache.h" | 33 #include "ios/net/empty_nsurlcache.h" |
| 33 #include "ios/net/http_cache_helper.h" | 34 #include "ios/net/http_cache_helper.h" |
| 34 #include "ios/net/request_tracker.h" | 35 #include "ios/net/request_tracker.h" |
| 35 #include "ios/web/public/user_agent.h" | 36 #include "ios/web/public/user_agent.h" |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 void CrNetEnvironment::StartNetLog(base::FilePath::StringType file_name, | 170 void CrNetEnvironment::StartNetLog(base::FilePath::StringType file_name, |
| 170 bool log_bytes) { | 171 bool log_bytes) { |
| 171 DCHECK(file_name.length()); | 172 DCHECK(file_name.length()); |
| 172 PostToFileUserBlockingThread(FROM_HERE, | 173 PostToFileUserBlockingThread(FROM_HERE, |
| 173 base::Bind(&CrNetEnvironment::StartNetLogInternal, | 174 base::Bind(&CrNetEnvironment::StartNetLogInternal, |
| 174 base::Unretained(this), file_name, log_bytes)); | 175 base::Unretained(this), file_name, log_bytes)); |
| 175 } | 176 } |
| 176 | 177 |
| 177 void CrNetEnvironment::StartNetLogInternal( | 178 void CrNetEnvironment::StartNetLogInternal( |
| 178 base::FilePath::StringType file_name, bool log_bytes) { | 179 base::FilePath::StringType file_name, bool log_bytes) { |
| 179 DCHECK(base::MessageLoop::current() == | 180 DCHECK(file_user_blocking_thread_->task_runner()->BelongsToCurrentThread()); |
| 180 file_user_blocking_thread_->message_loop()); | |
| 181 DCHECK(file_name.length()); | 181 DCHECK(file_name.length()); |
| 182 DCHECK(net_log_); | 182 DCHECK(net_log_); |
| 183 | 183 |
| 184 if (net_log_observer_) | 184 if (net_log_observer_) |
| 185 return; | 185 return; |
| 186 | 186 |
| 187 base::FilePath temp_dir; | 187 base::FilePath temp_dir; |
| 188 if (!base::GetTempDir(&temp_dir)) | 188 if (!base::GetTempDir(&temp_dir)) |
| 189 return; | 189 return; |
| 190 | 190 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 203 nullptr); | 203 nullptr); |
| 204 } | 204 } |
| 205 | 205 |
| 206 void CrNetEnvironment::StopNetLog() { | 206 void CrNetEnvironment::StopNetLog() { |
| 207 PostToFileUserBlockingThread(FROM_HERE, | 207 PostToFileUserBlockingThread(FROM_HERE, |
| 208 base::Bind(&CrNetEnvironment::StopNetLogInternal, | 208 base::Bind(&CrNetEnvironment::StopNetLogInternal, |
| 209 base::Unretained(this))); | 209 base::Unretained(this))); |
| 210 } | 210 } |
| 211 | 211 |
| 212 void CrNetEnvironment::StopNetLogInternal() { | 212 void CrNetEnvironment::StopNetLogInternal() { |
| 213 DCHECK(base::MessageLoop::current() == | 213 DCHECK(file_user_blocking_thread_->task_runner()->BelongsToCurrentThread()); |
| 214 file_user_blocking_thread_->message_loop()); | |
| 215 if (net_log_observer_) { | 214 if (net_log_observer_) { |
| 216 net_log_observer_->StopObserving(nullptr); | 215 net_log_observer_->StopObserving(nullptr); |
| 217 net_log_observer_.reset(); | 216 net_log_observer_.reset(); |
| 218 } | 217 } |
| 219 } | 218 } |
| 220 | 219 |
| 221 void CrNetEnvironment::CloseAllSpdySessions() { | 220 void CrNetEnvironment::CloseAllSpdySessions() { |
| 222 PostToNetworkThread(FROM_HERE, | 221 PostToNetworkThread(FROM_HERE, |
| 223 base::Bind(&CrNetEnvironment::CloseAllSpdySessionsInternal, | 222 base::Bind(&CrNetEnvironment::CloseAllSpdySessionsInternal, |
| 224 base::Unretained(this))); | 223 base::Unretained(this))); |
| 225 } | 224 } |
| 226 | 225 |
| 227 void CrNetEnvironment::SetRequestFilterBlock(RequestFilterBlock block) { | 226 void CrNetEnvironment::SetRequestFilterBlock(RequestFilterBlock block) { |
| 228 http_protocol_handler_delegate_.reset( | 227 http_protocol_handler_delegate_.reset( |
| 229 new CrNetHttpProtocolHandlerDelegate(main_context_getter_.get(), block)); | 228 new CrNetHttpProtocolHandlerDelegate(main_context_getter_.get(), block)); |
| 230 net::HTTPProtocolHandlerDelegate::SetInstance( | 229 net::HTTPProtocolHandlerDelegate::SetInstance( |
| 231 http_protocol_handler_delegate_.get()); | 230 http_protocol_handler_delegate_.get()); |
| 232 } | 231 } |
| 233 | 232 |
| 234 net::HttpNetworkSession* CrNetEnvironment::GetHttpNetworkSession( | 233 net::HttpNetworkSession* CrNetEnvironment::GetHttpNetworkSession( |
| 235 net::URLRequestContext* context) { | 234 net::URLRequestContext* context) { |
| 236 DCHECK(context); | 235 DCHECK(context); |
| 237 if (!context->http_transaction_factory()) | 236 if (!context->http_transaction_factory()) |
| 238 return nullptr; | 237 return nullptr; |
| 239 | 238 |
| 240 return context->http_transaction_factory()->GetSession(); | 239 return context->http_transaction_factory()->GetSession(); |
| 241 } | 240 } |
| 242 | 241 |
| 243 void CrNetEnvironment::CloseAllSpdySessionsInternal() { | 242 void CrNetEnvironment::CloseAllSpdySessionsInternal() { |
| 244 DCHECK(base::MessageLoop::current() == | 243 DCHECK(network_io_thread_->task_runner()->BelongsToCurrentThread()); |
| 245 network_io_thread_->message_loop()); | |
| 246 | 244 |
| 247 net::HttpNetworkSession* http_network_session = | 245 net::HttpNetworkSession* http_network_session = |
| 248 GetHttpNetworkSession(GetMainContextGetter()->GetURLRequestContext()); | 246 GetHttpNetworkSession(GetMainContextGetter()->GetURLRequestContext()); |
| 249 | 247 |
| 250 if (http_network_session) { | 248 if (http_network_session) { |
| 251 net::SpdySessionPool *spdy_session_pool = | 249 net::SpdySessionPool *spdy_session_pool = |
| 252 http_network_session->spdy_session_pool(); | 250 http_network_session->spdy_session_pool(); |
| 253 if (spdy_session_pool) | 251 if (spdy_session_pool) |
| 254 spdy_session_pool->CloseCurrentSessions(net::ERR_ABORTED); | 252 spdy_session_pool->CloseCurrentSessions(net::ERR_ABORTED); |
| 255 } | 253 } |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 } else { | 317 } else { |
| 320 // Set up an empty default cache, with default size. | 318 // Set up an empty default cache, with default size. |
| 321 // TODO(droger): If the NSURLCache is to be used, its size should most | 319 // TODO(droger): If the NSURLCache is to be used, its size should most |
| 322 // likely be changed. On an iPod2 with iOS4, the default size is 512k. | 320 // likely be changed. On an iPod2 with iOS4, the default size is 512k. |
| 323 [NSURLCache setSharedURLCache:[[[NSURLCache alloc] init] autorelease]]; | 321 [NSURLCache setSharedURLCache:[[[NSURLCache alloc] init] autorelease]]; |
| 324 [NSURLProtocol unregisterClass:[CRNPauseableHTTPProtocolHandler class]]; | 322 [NSURLProtocol unregisterClass:[CRNPauseableHTTPProtocolHandler class]]; |
| 325 } | 323 } |
| 326 } | 324 } |
| 327 | 325 |
| 328 void CrNetEnvironment::ConfigureSdchOnNetworkThread() { | 326 void CrNetEnvironment::ConfigureSdchOnNetworkThread() { |
| 329 DCHECK(base::MessageLoop::current() == network_io_thread_->message_loop()); | 327 DCHECK(network_io_thread_->task_runner()->BelongsToCurrentThread()); |
| 330 net::URLRequestContext* context = | 328 net::URLRequestContext* context = |
| 331 main_context_getter_->GetURLRequestContext(); | 329 main_context_getter_->GetURLRequestContext(); |
| 332 | 330 |
| 333 if (!sdch_enabled_) { | 331 if (!sdch_enabled_) { |
| 334 DCHECK_EQ(static_cast<net::SdchManager*>(nullptr), context->sdch_manager()); | 332 DCHECK_EQ(static_cast<net::SdchManager*>(nullptr), context->sdch_manager()); |
| 335 return; | 333 return; |
| 336 } | 334 } |
| 337 | 335 |
| 338 sdch_manager_.reset(new net::SdchManager()); | 336 sdch_manager_.reset(new net::SdchManager()); |
| 339 sdch_owner_.reset(new net::SdchOwner(sdch_manager_.get(), context)); | 337 sdch_owner_.reset(new net::SdchOwner(sdch_manager_.get(), context)); |
| 340 if (!sdch_pref_store_filename_.empty()) { | 338 if (!sdch_pref_store_filename_.empty()) { |
| 341 base::FilePath path(sdch_pref_store_filename_); | 339 base::FilePath path(sdch_pref_store_filename_); |
| 342 pref_store_worker_pool_ = file_user_blocking_thread_->task_runner(); | 340 pref_store_worker_pool_ = file_user_blocking_thread_->task_runner(); |
| 343 net_pref_store_ = new JsonPrefStore(path, pref_store_worker_pool_.get(), | 341 net_pref_store_ = new JsonPrefStore(path, pref_store_worker_pool_.get(), |
| 344 std::unique_ptr<PrefFilter>()); | 342 std::unique_ptr<PrefFilter>()); |
| 345 net_pref_store_->ReadPrefsAsync(nullptr); | 343 net_pref_store_->ReadPrefsAsync(nullptr); |
| 346 sdch_owner_->EnablePersistentStorage( | 344 sdch_owner_->EnablePersistentStorage( |
| 347 std::unique_ptr<net::SdchOwner::PrefStorage>( | 345 std::unique_ptr<net::SdchOwner::PrefStorage>( |
| 348 new SdchOwnerPrefStorage(net_pref_store_.get()))); | 346 new SdchOwnerPrefStorage(net_pref_store_.get()))); |
| 349 } | 347 } |
| 350 context->set_sdch_manager(sdch_manager_.get()); | 348 context->set_sdch_manager(sdch_manager_.get()); |
| 351 } | 349 } |
| 352 | 350 |
| 353 void CrNetEnvironment::InitializeOnNetworkThread() { | 351 void CrNetEnvironment::InitializeOnNetworkThread() { |
| 354 DCHECK(base::MessageLoop::current() == network_io_thread_->message_loop()); | 352 DCHECK(network_io_thread_->task_runner()->BelongsToCurrentThread()); |
| 355 | 353 |
| 356 ConfigureSdchOnNetworkThread(); | 354 ConfigureSdchOnNetworkThread(); |
| 357 | 355 |
| 358 NSString* bundlePath = | 356 NSString* bundlePath = |
| 359 [[NSBundle mainBundle] pathForResource:@"crnet_resources" | 357 [[NSBundle mainBundle] pathForResource:@"crnet_resources" |
| 360 ofType:@"bundle"]; | 358 ofType:@"bundle"]; |
| 361 NSBundle* bundle = [NSBundle bundleWithPath:bundlePath]; | 359 NSBundle* bundle = [NSBundle bundleWithPath:bundlePath]; |
| 362 NSString* acceptableLanguages = NSLocalizedStringWithDefaultValue( | 360 NSString* acceptableLanguages = NSLocalizedStringWithDefaultValue( |
| 363 @"IDS_ACCEPT_LANGUAGES", | 361 @"IDS_ACCEPT_LANGUAGES", |
| 364 @"Localizable", | 362 @"Localizable", |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 478 | 476 |
| 479 return user_agent_settings->GetUserAgent(); | 477 return user_agent_settings->GetUserAgent(); |
| 480 } | 478 } |
| 481 | 479 |
| 482 void CrNetEnvironment::ClearCache(ClearCacheCallback callback) { | 480 void CrNetEnvironment::ClearCache(ClearCacheCallback callback) { |
| 483 PostToNetworkThread( | 481 PostToNetworkThread( |
| 484 FROM_HERE, | 482 FROM_HERE, |
| 485 base::Bind(&net::ClearHttpCache, main_context_getter_, | 483 base::Bind(&net::ClearHttpCache, main_context_getter_, |
| 486 network_io_thread_->task_runner(), base::BindBlock(callback))); | 484 network_io_thread_->task_runner(), base::BindBlock(callback))); |
| 487 } | 485 } |
| OLD | NEW |