Chromium Code Reviews| 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 "build/build_config.h" | |
| 10 #include "content/network/network_context.h" | 11 #include "content/network/network_context.h" |
| 11 #include "content/public/common/content_switches.h" | 12 #include "content/public/common/content_switches.h" |
| 12 #include "mojo/public/cpp/bindings/strong_binding.h" | 13 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 13 #include "net/log/net_log_util.h" | 14 #include "net/log/net_log_util.h" |
| 14 #include "net/log/write_to_file_net_log_observer.h" | 15 #include "net/log/write_to_file_net_log_observer.h" |
| 16 #include "net/url_request/url_request_context_builder.h" | |
| 15 #include "services/service_manager/public/cpp/bind_source_info.h" | 17 #include "services/service_manager/public/cpp/bind_source_info.h" |
| 16 | 18 |
| 17 namespace content { | 19 namespace content { |
| 18 | 20 |
| 19 class NetworkService::MojoNetLog : public net::NetLog { | 21 std::unique_ptr<NetworkService> NetworkService::Create() { |
| 22 return base::MakeUnique<NetworkServiceImpl>(nullptr); | |
| 23 } | |
| 24 | |
| 25 class NetworkServiceImpl::MojoNetLog : public net::NetLog { | |
| 20 public: | 26 public: |
| 21 MojoNetLog() { | 27 MojoNetLog() { |
| 22 const base::CommandLine* command_line = | 28 const base::CommandLine* command_line = |
| 23 base::CommandLine::ForCurrentProcess(); | 29 base::CommandLine::ForCurrentProcess(); |
| 24 if (!command_line->HasSwitch(switches::kLogNetLog)) | 30 if (!command_line->HasSwitch(switches::kLogNetLog)) |
| 25 return; | 31 return; |
| 26 base::FilePath log_path = | 32 base::FilePath log_path = |
| 27 command_line->GetSwitchValuePath(switches::kLogNetLog); | 33 command_line->GetSwitchValuePath(switches::kLogNetLog); |
| 28 base::ScopedFILE file; | 34 base::ScopedFILE file; |
| 29 #if defined(OS_WIN) | 35 #if defined(OS_WIN) |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 45 ~MojoNetLog() override { | 51 ~MojoNetLog() override { |
| 46 if (write_to_file_observer_) | 52 if (write_to_file_observer_) |
| 47 write_to_file_observer_->StopObserving(nullptr); | 53 write_to_file_observer_->StopObserving(nullptr); |
| 48 } | 54 } |
| 49 | 55 |
| 50 private: | 56 private: |
| 51 std::unique_ptr<net::WriteToFileNetLogObserver> write_to_file_observer_; | 57 std::unique_ptr<net::WriteToFileNetLogObserver> write_to_file_observer_; |
| 52 DISALLOW_COPY_AND_ASSIGN(MojoNetLog); | 58 DISALLOW_COPY_AND_ASSIGN(MojoNetLog); |
| 53 }; | 59 }; |
| 54 | 60 |
| 55 NetworkService::NetworkService( | 61 NetworkServiceImpl::NetworkServiceImpl( |
| 56 std::unique_ptr<service_manager::BinderRegistry> registry) | 62 std::unique_ptr<service_manager::BinderRegistry> registry) |
| 57 : net_log_(new MojoNetLog), registry_(std::move(registry)), binding_(this) { | 63 : net_log_(new MojoNetLog), registry_(std::move(registry)), binding_(this) { |
| 58 // |registry_| may be nullptr in tests. | |
| 59 if (registry_) { | 64 if (registry_) { |
|
kinuko
2017/07/10 08:10:33
nit: might be still helpful to note when registry_
mmenke
2017/07/10 15:52:22
Done.
| |
| 60 registry_->AddInterface<mojom::NetworkService>( | 65 registry_->AddInterface<mojom::NetworkService>( |
| 61 base::Bind(&NetworkService::Create, base::Unretained(this))); | 66 base::Bind(&NetworkServiceImpl::Create, base::Unretained(this))); |
| 62 } | 67 } |
| 63 } | 68 } |
| 64 | 69 |
| 65 NetworkService::~NetworkService() { | 70 NetworkServiceImpl::~NetworkServiceImpl() { |
| 66 // Call each Network and ask it to release its net::URLRequestContext, as they | 71 // 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 | 72 // may have references to shared objects owned by the NetworkService. The |
| 68 // NetworkContexts deregister themselves in Cleanup(), so have to be careful. | 73 // NetworkContexts deregister themselves in Cleanup(), so have to be careful. |
| 69 while (!network_contexts_.empty()) | 74 while (!network_contexts_.empty()) |
| 70 (*network_contexts_.begin())->Cleanup(); | 75 (*network_contexts_.begin())->Cleanup(); |
| 71 } | 76 } |
| 72 | 77 |
| 73 std::unique_ptr<NetworkService> NetworkService::CreateForTesting() { | 78 std::unique_ptr<mojom::NetworkContext> |
| 74 return base::WrapUnique(new NetworkService()); | 79 NetworkServiceImpl::CreateNetworkContextWithBuilder( |
| 80 content::mojom::NetworkContextRequest request, | |
| 81 content::mojom::NetworkContextParamsPtr params, | |
| 82 std::unique_ptr<net::URLRequestContextBuilder> builder, | |
| 83 net::URLRequestContext** url_request_context) { | |
| 84 std::unique_ptr<NetworkContext> network_context = | |
| 85 base::MakeUnique<NetworkContext>(std::move(request), std::move(params), | |
| 86 std::move(builder)); | |
| 87 *url_request_context = network_context->url_request_context(); | |
| 88 return network_context; | |
| 75 } | 89 } |
| 76 | 90 |
| 77 void NetworkService::RegisterNetworkContext(NetworkContext* network_context) { | 91 std::unique_ptr<NetworkService> NetworkServiceImpl::CreateForTesting() { |
| 92 return base::WrapUnique(new NetworkServiceImpl(nullptr)); | |
| 93 } | |
| 94 | |
| 95 void NetworkServiceImpl::RegisterNetworkContext( | |
| 96 NetworkContext* network_context) { | |
| 78 DCHECK_EQ(0u, network_contexts_.count(network_context)); | 97 DCHECK_EQ(0u, network_contexts_.count(network_context)); |
| 79 network_contexts_.insert(network_context); | 98 network_contexts_.insert(network_context); |
| 80 } | 99 } |
| 81 | 100 |
| 82 void NetworkService::DeregisterNetworkContext(NetworkContext* network_context) { | 101 void NetworkServiceImpl::DeregisterNetworkContext( |
| 102 NetworkContext* network_context) { | |
| 83 DCHECK_EQ(1u, network_contexts_.count(network_context)); | 103 DCHECK_EQ(1u, network_contexts_.count(network_context)); |
| 84 network_contexts_.erase(network_context); | 104 network_contexts_.erase(network_context); |
| 85 } | 105 } |
| 86 | 106 |
| 87 void NetworkService::CreateNetworkContext( | 107 void NetworkServiceImpl::CreateNetworkContext( |
| 88 mojom::NetworkContextRequest request, | 108 mojom::NetworkContextRequest request, |
| 89 mojom::NetworkContextParamsPtr params) { | 109 mojom::NetworkContextParamsPtr params) { |
| 90 // The NetworkContext will destroy itself on connection error, or when the | 110 // The NetworkContext will destroy itself on connection error, or when the |
| 91 // service is destroyed. | 111 // service is destroyed. |
| 92 new NetworkContext(this, std::move(request), std::move(params)); | 112 new NetworkContext(this, std::move(request), std::move(params)); |
| 93 } | 113 } |
| 94 | 114 |
| 95 NetworkService::NetworkService() : NetworkService(nullptr) {} | 115 void NetworkServiceImpl::OnBindInterface( |
| 96 | |
| 97 void NetworkService::OnBindInterface( | |
| 98 const service_manager::BindSourceInfo& source_info, | 116 const service_manager::BindSourceInfo& source_info, |
| 99 const std::string& interface_name, | 117 const std::string& interface_name, |
| 100 mojo::ScopedMessagePipeHandle interface_pipe) { | 118 mojo::ScopedMessagePipeHandle interface_pipe) { |
| 101 registry_->BindInterface(source_info, interface_name, | 119 registry_->BindInterface(source_info, interface_name, |
| 102 std::move(interface_pipe)); | 120 std::move(interface_pipe)); |
| 103 } | 121 } |
| 104 | 122 |
| 105 void NetworkService::Create(const service_manager::BindSourceInfo& source_info, | 123 void NetworkServiceImpl::Create( |
| 106 mojom::NetworkServiceRequest request) { | 124 const service_manager::BindSourceInfo& source_info, |
| 125 mojom::NetworkServiceRequest request) { | |
| 107 DCHECK(!binding_.is_bound()); | 126 DCHECK(!binding_.is_bound()); |
| 108 binding_.Bind(std::move(request)); | 127 binding_.Bind(std::move(request)); |
| 109 } | 128 } |
| 110 | 129 |
| 111 } // namespace content | 130 } // namespace content |
| OLD | NEW |