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