| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/cronet/ios/cronet_environment.h" | 5 #include "components/cronet/ios/cronet_environment.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/at_exit.h" | 9 #include "base/at_exit.h" |
| 10 #include "base/atomicops.h" | 10 #include "base/atomicops.h" |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 DCHECK(!base::MessageLoop::current()); | 128 DCHECK(!base::MessageLoop::current()); |
| 129 DCHECK(!g_main_message_loop); | 129 DCHECK(!g_main_message_loop); |
| 130 g_main_message_loop = new base::MessageLoopForUI(); | 130 g_main_message_loop = new base::MessageLoopForUI(); |
| 131 base::MessageLoopForUI::current()->Attach(); | 131 base::MessageLoopForUI::current()->Attach(); |
| 132 // The network change notifier must be initialized so that registered | 132 // The network change notifier must be initialized so that registered |
| 133 // delegates will receive callbacks. | 133 // delegates will receive callbacks. |
| 134 DCHECK(!g_network_change_notifier); | 134 DCHECK(!g_network_change_notifier); |
| 135 g_network_change_notifier = net::NetworkChangeNotifier::Create(); | 135 g_network_change_notifier = net::NetworkChangeNotifier::Create(); |
| 136 } | 136 } |
| 137 | 137 |
| 138 void CronetEnvironment::StartNetLog(base::FilePath::StringType file_name, | 138 bool CronetEnvironment::StartNetLog(base::FilePath::StringType file_name, |
| 139 bool log_bytes) { | 139 bool log_bytes) { |
| 140 DCHECK(file_name.length()); | 140 if (!file_name.length()) |
| 141 PostToNetworkThread(FROM_HERE, | 141 return false; |
| 142 base::Bind(&CronetEnvironment::StartNetLogOnNetworkThread, | 142 |
| 143 base::Unretained(this), file_name, log_bytes)); | 143 base::FilePath path(file_name); |
| 144 |
| 145 base::ScopedFILE file(base::OpenFile(path, "w")); |
| 146 if (!file) { |
| 147 LOG(ERROR) << "Can not start NetLog to " << path.value() << ": " |
| 148 << strerror(errno); |
| 149 return false; |
| 150 } |
| 151 |
| 152 LOG(WARNING) << "Starting NetLog to " << path.value(); |
| 153 PostToNetworkThread( |
| 154 FROM_HERE, |
| 155 base::Bind(&CronetEnvironment::StartNetLogOnNetworkThread, |
| 156 base::Unretained(this), base::Passed(&file), log_bytes)); |
| 157 |
| 158 return true; |
| 144 } | 159 } |
| 145 | 160 |
| 146 void CronetEnvironment::StartNetLogOnNetworkThread( | 161 void CronetEnvironment::StartNetLogOnNetworkThread(base::ScopedFILE file, |
| 147 const base::FilePath::StringType& file_name, | 162 bool log_bytes) { |
| 148 bool log_bytes) { | |
| 149 DCHECK(file_name.length()); | |
| 150 DCHECK(net_log_); | 163 DCHECK(net_log_); |
| 151 | 164 |
| 152 if (net_log_observer_) | 165 if (net_log_observer_) |
| 153 return; | 166 return; |
| 154 | 167 |
| 155 base::FilePath files_root; | |
| 156 if (!PathService::Get(base::DIR_HOME, &files_root)) | |
| 157 return; | |
| 158 | |
| 159 base::FilePath full_path = files_root.Append(file_name); | |
| 160 base::ScopedFILE file(base::OpenFile(full_path, "w")); | |
| 161 if (!file) { | |
| 162 LOG(ERROR) << "Can not start NetLog to " << full_path.value(); | |
| 163 return; | |
| 164 } | |
| 165 | |
| 166 net::NetLogCaptureMode capture_mode = | 168 net::NetLogCaptureMode capture_mode = |
| 167 log_bytes ? net::NetLogCaptureMode::IncludeSocketBytes() | 169 log_bytes ? net::NetLogCaptureMode::IncludeSocketBytes() |
| 168 : net::NetLogCaptureMode::Default(); | 170 : net::NetLogCaptureMode::Default(); |
| 169 | 171 |
| 170 net_log_observer_.reset(new net::WriteToFileNetLogObserver()); | 172 net_log_observer_.reset(new net::WriteToFileNetLogObserver()); |
| 171 net_log_observer_->set_capture_mode(capture_mode); | 173 net_log_observer_->set_capture_mode(capture_mode); |
| 172 net_log_observer_->StartObserving(main_context_->net_log(), std::move(file), | 174 net_log_observer_->StartObserving(main_context_->net_log(), std::move(file), |
| 173 nullptr, main_context_.get()); | 175 nullptr, main_context_.get()); |
| 174 LOG(WARNING) << "Started NetLog to " << full_path.value(); | 176 LOG(WARNING) << "Started NetLog"; |
| 175 } | 177 } |
| 176 | 178 |
| 177 void CronetEnvironment::StopNetLog() { | 179 void CronetEnvironment::StopNetLog() { |
| 178 base::WaitableEvent log_stopped_event( | 180 base::WaitableEvent log_stopped_event( |
| 179 base::WaitableEvent::ResetPolicy::MANUAL, | 181 base::WaitableEvent::ResetPolicy::MANUAL, |
| 180 base::WaitableEvent::InitialState::NOT_SIGNALED); | 182 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 181 PostToNetworkThread(FROM_HERE, | 183 PostToNetworkThread(FROM_HERE, |
| 182 base::Bind(&CronetEnvironment::StopNetLogOnNetworkThread, | 184 base::Bind(&CronetEnvironment::StopNetLogOnNetworkThread, |
| 183 base::Unretained(this), &log_stopped_event)); | 185 base::Unretained(this), &log_stopped_event)); |
| 184 log_stopped_event.Wait(); | 186 log_stopped_event.Wait(); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 network_io_thread_->StartWithOptions( | 229 network_io_thread_->StartWithOptions( |
| 228 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); | 230 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
| 229 file_thread_.reset(new base::Thread("Chrome File Thread")); | 231 file_thread_.reset(new base::Thread("Chrome File Thread")); |
| 230 file_thread_->StartWithOptions( | 232 file_thread_->StartWithOptions( |
| 231 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); | 233 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
| 232 file_user_blocking_thread_.reset( | 234 file_user_blocking_thread_.reset( |
| 233 new base::Thread("Chrome File User Blocking Thread")); | 235 new base::Thread("Chrome File User Blocking Thread")); |
| 234 file_user_blocking_thread_->StartWithOptions( | 236 file_user_blocking_thread_->StartWithOptions( |
| 235 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); | 237 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
| 236 | 238 |
| 237 static bool ssl_key_log_file_set = false; | |
| 238 if (!ssl_key_log_file_set && !ssl_key_log_file_name_.empty()) { | |
| 239 ssl_key_log_file_set = true; | |
| 240 base::FilePath ssl_key_log_file; | |
| 241 if (!PathService::Get(base::DIR_HOME, &ssl_key_log_file)) | |
| 242 return; | |
| 243 net::SSLClientSocket::SetSSLKeyLogFile( | |
| 244 ssl_key_log_file.Append(ssl_key_log_file_name_), | |
| 245 file_thread_->task_runner()); | |
| 246 } | |
| 247 | |
| 248 main_context_getter_ = new CronetURLRequestContextGetter( | 239 main_context_getter_ = new CronetURLRequestContextGetter( |
| 249 this, network_io_thread_->task_runner()); | 240 this, network_io_thread_->task_runner()); |
| 250 base::subtle::MemoryBarrier(); | 241 base::subtle::MemoryBarrier(); |
| 251 PostToNetworkThread(FROM_HERE, | 242 PostToNetworkThread(FROM_HERE, |
| 252 base::Bind(&CronetEnvironment::InitializeOnNetworkThread, | 243 base::Bind(&CronetEnvironment::InitializeOnNetworkThread, |
| 253 base::Unretained(this))); | 244 base::Unretained(this))); |
| 254 } | 245 } |
| 255 | 246 |
| 256 CronetEnvironment::~CronetEnvironment() { | 247 CronetEnvironment::~CronetEnvironment() { |
| 257 // net::HTTPProtocolHandlerDelegate::SetInstance(nullptr); | 248 // net::HTTPProtocolHandlerDelegate::SetInstance(nullptr); |
| 258 } | 249 } |
| 259 | 250 |
| 260 void CronetEnvironment::InitializeOnNetworkThread() { | 251 void CronetEnvironment::InitializeOnNetworkThread() { |
| 261 DCHECK(network_io_thread_->task_runner()->BelongsToCurrentThread()); | 252 DCHECK(network_io_thread_->task_runner()->BelongsToCurrentThread()); |
| 262 base::FeatureList::InitializeInstance(std::string(), std::string()); | 253 base::FeatureList::InitializeInstance(std::string(), std::string()); |
| 263 | 254 |
| 255 static bool ssl_key_log_file_set = false; |
| 256 if (!ssl_key_log_file_set && !ssl_key_log_file_name_.empty()) { |
| 257 ssl_key_log_file_set = true; |
| 258 base::FilePath ssl_key_log_file; |
| 259 if (!PathService::Get(base::DIR_HOME, &ssl_key_log_file)) |
| 260 return; |
| 261 net::SSLClientSocket::SetSSLKeyLogFile( |
| 262 ssl_key_log_file.Append(ssl_key_log_file_name_), |
| 263 file_thread_->task_runner()); |
| 264 } |
| 265 |
| 264 if (user_agent_partial_) | 266 if (user_agent_partial_) |
| 265 user_agent_ = web::BuildUserAgentFromProduct(user_agent_); | 267 user_agent_ = web::BuildUserAgentFromProduct(user_agent_); |
| 266 | 268 |
| 267 // Cache | 269 // Cache |
| 268 base::FilePath cache_path; | 270 base::FilePath cache_path; |
| 269 if (!PathService::Get(base::DIR_CACHE, &cache_path)) | 271 if (!PathService::Get(base::DIR_CACHE, &cache_path)) |
| 270 return; | 272 return; |
| 271 cache_path = cache_path.Append(FILE_PATH_LITERAL("cronet")); | 273 cache_path = cache_path.Append(FILE_PATH_LITERAL("cronet")); |
| 272 | 274 |
| 273 std::unique_ptr<URLRequestContextConfig> config(new URLRequestContextConfig( | 275 std::unique_ptr<URLRequestContextConfig> config(new URLRequestContextConfig( |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 event->Signal(); | 368 event->Signal(); |
| 367 } | 369 } |
| 368 | 370 |
| 369 std::string CronetEnvironment::getDefaultQuicUserAgentId() const { | 371 std::string CronetEnvironment::getDefaultQuicUserAgentId() const { |
| 370 return base::SysNSStringToUTF8([[NSBundle mainBundle] | 372 return base::SysNSStringToUTF8([[NSBundle mainBundle] |
| 371 objectForInfoDictionaryKey:@"CFBundleDisplayName"]) + | 373 objectForInfoDictionaryKey:@"CFBundleDisplayName"]) + |
| 372 " Cronet/" + CRONET_VERSION; | 374 " Cronet/" + CRONET_VERSION; |
| 373 } | 375 } |
| 374 | 376 |
| 375 } // namespace cronet | 377 } // namespace cronet |
| OLD | NEW |