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