| OLD | NEW |
| (Empty) |
| 1 // Copyright 2017 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 "content/network/network_service.h" | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/memory/ptr_util.h" | |
| 10 #include "content/network/network_context.h" | |
| 11 #include "content/public/common/content_switches.h" | |
| 12 #include "mojo/public/cpp/bindings/strong_binding.h" | |
| 13 #include "net/log/net_log_util.h" | |
| 14 #include "net/log/write_to_file_net_log_observer.h" | |
| 15 #include "services/service_manager/public/cpp/bind_source_info.h" | |
| 16 | |
| 17 namespace content { | |
| 18 | |
| 19 class NetworkService::MojoNetLog : public net::NetLog { | |
| 20 public: | |
| 21 MojoNetLog() { | |
| 22 const base::CommandLine* command_line = | |
| 23 base::CommandLine::ForCurrentProcess(); | |
| 24 if (!command_line->HasSwitch(switches::kLogNetLog)) | |
| 25 return; | |
| 26 base::FilePath log_path = | |
| 27 command_line->GetSwitchValuePath(switches::kLogNetLog); | |
| 28 base::ScopedFILE file; | |
| 29 #if defined(OS_WIN) | |
| 30 file.reset(_wfopen(log_path.value().c_str(), L"w")); | |
| 31 #elif defined(OS_POSIX) | |
| 32 file.reset(fopen(log_path.value().c_str(), "w")); | |
| 33 #endif | |
| 34 if (!file) { | |
| 35 LOG(ERROR) << "Could not open file " << log_path.value() | |
| 36 << " for net logging"; | |
| 37 } else { | |
| 38 write_to_file_observer_.reset(new net::WriteToFileNetLogObserver()); | |
| 39 write_to_file_observer_->set_capture_mode( | |
| 40 net::NetLogCaptureMode::IncludeCookiesAndCredentials()); | |
| 41 write_to_file_observer_->StartObserving(this, std::move(file), nullptr, | |
| 42 nullptr); | |
| 43 } | |
| 44 } | |
| 45 ~MojoNetLog() override { | |
| 46 if (write_to_file_observer_) | |
| 47 write_to_file_observer_->StopObserving(nullptr); | |
| 48 } | |
| 49 | |
| 50 private: | |
| 51 std::unique_ptr<net::WriteToFileNetLogObserver> write_to_file_observer_; | |
| 52 DISALLOW_COPY_AND_ASSIGN(MojoNetLog); | |
| 53 }; | |
| 54 | |
| 55 NetworkService::NetworkService( | |
| 56 std::unique_ptr<service_manager::BinderRegistry> registry) | |
| 57 : net_log_(new MojoNetLog), registry_(std::move(registry)), binding_(this) { | |
| 58 // |registry_| may be nullptr in tests. | |
| 59 if (registry_) { | |
| 60 registry_->AddInterface<mojom::NetworkService>( | |
| 61 base::Bind(&NetworkService::Create, base::Unretained(this))); | |
| 62 } | |
| 63 } | |
| 64 | |
| 65 NetworkService::~NetworkService() { | |
| 66 // Call each Network and ask it to release its net::URLRequestContext, as they | |
| 67 // may have references to shared objects owned by the NetworkService. The | |
| 68 // NetworkContexts deregister themselves in Cleanup(), so have to be careful. | |
| 69 while (!network_contexts_.empty()) | |
| 70 (*network_contexts_.begin())->Cleanup(); | |
| 71 } | |
| 72 | |
| 73 std::unique_ptr<NetworkService> NetworkService::CreateForTesting() { | |
| 74 return base::WrapUnique(new NetworkService()); | |
| 75 } | |
| 76 | |
| 77 void NetworkService::RegisterNetworkContext(NetworkContext* network_context) { | |
| 78 DCHECK_EQ(0u, network_contexts_.count(network_context)); | |
| 79 network_contexts_.insert(network_context); | |
| 80 } | |
| 81 | |
| 82 void NetworkService::DeregisterNetworkContext(NetworkContext* network_context) { | |
| 83 DCHECK_EQ(1u, network_contexts_.count(network_context)); | |
| 84 network_contexts_.erase(network_context); | |
| 85 } | |
| 86 | |
| 87 void NetworkService::CreateNetworkContext( | |
| 88 mojom::NetworkContextRequest request, | |
| 89 mojom::NetworkContextParamsPtr params) { | |
| 90 // The NetworkContext will destroy itself on connection error, or when the | |
| 91 // service is destroyed. | |
| 92 new NetworkContext(this, std::move(request), std::move(params)); | |
| 93 } | |
| 94 | |
| 95 NetworkService::NetworkService() : NetworkService(nullptr) {} | |
| 96 | |
| 97 void NetworkService::OnBindInterface( | |
| 98 const service_manager::BindSourceInfo& source_info, | |
| 99 const std::string& interface_name, | |
| 100 mojo::ScopedMessagePipeHandle interface_pipe) { | |
| 101 registry_->BindInterface(source_info, interface_name, | |
| 102 std::move(interface_pipe)); | |
| 103 } | |
| 104 | |
| 105 void NetworkService::Create(const service_manager::BindSourceInfo& source_info, | |
| 106 mojom::NetworkServiceRequest request) { | |
| 107 DCHECK(!binding_.is_bound()); | |
| 108 binding_.Bind(std::move(request)); | |
| 109 } | |
| 110 | |
| 111 } // namespace content | |
| OLD | NEW |