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. | 64 // |registry_| is nullptr in tests and when an in-process NetworkService is |
| 65 // created directly. The latter is done in concert with using |
| 66 // CreateNetworkContextWithBuilder to ease the transition to using the network |
| 67 // service. |
59 if (registry_) { | 68 if (registry_) { |
60 registry_->AddInterface<mojom::NetworkService>( | 69 registry_->AddInterface<mojom::NetworkService>( |
61 base::Bind(&NetworkService::Create, base::Unretained(this))); | 70 base::Bind(&NetworkServiceImpl::Create, base::Unretained(this))); |
62 } | 71 } |
63 } | 72 } |
64 | 73 |
65 NetworkService::~NetworkService() { | 74 NetworkServiceImpl::~NetworkServiceImpl() { |
66 // Call each Network and ask it to release its net::URLRequestContext, as they | 75 // 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 | 76 // may have references to shared objects owned by the NetworkService. The |
68 // NetworkContexts deregister themselves in Cleanup(), so have to be careful. | 77 // NetworkContexts deregister themselves in Cleanup(), so have to be careful. |
69 while (!network_contexts_.empty()) | 78 while (!network_contexts_.empty()) |
70 (*network_contexts_.begin())->Cleanup(); | 79 (*network_contexts_.begin())->Cleanup(); |
71 } | 80 } |
72 | 81 |
73 std::unique_ptr<NetworkService> NetworkService::CreateForTesting() { | 82 std::unique_ptr<mojom::NetworkContext> |
74 return base::WrapUnique(new NetworkService()); | 83 NetworkServiceImpl::CreateNetworkContextWithBuilder( |
| 84 content::mojom::NetworkContextRequest request, |
| 85 content::mojom::NetworkContextParamsPtr params, |
| 86 std::unique_ptr<net::URLRequestContextBuilder> builder, |
| 87 net::URLRequestContext** url_request_context) { |
| 88 std::unique_ptr<NetworkContext> network_context = |
| 89 base::MakeUnique<NetworkContext>(std::move(request), std::move(params), |
| 90 std::move(builder)); |
| 91 *url_request_context = network_context->url_request_context(); |
| 92 return network_context; |
75 } | 93 } |
76 | 94 |
77 void NetworkService::RegisterNetworkContext(NetworkContext* network_context) { | 95 std::unique_ptr<NetworkService> NetworkServiceImpl::CreateForTesting() { |
| 96 return base::WrapUnique(new NetworkServiceImpl(nullptr)); |
| 97 } |
| 98 |
| 99 void NetworkServiceImpl::RegisterNetworkContext( |
| 100 NetworkContext* network_context) { |
78 DCHECK_EQ(0u, network_contexts_.count(network_context)); | 101 DCHECK_EQ(0u, network_contexts_.count(network_context)); |
79 network_contexts_.insert(network_context); | 102 network_contexts_.insert(network_context); |
80 } | 103 } |
81 | 104 |
82 void NetworkService::DeregisterNetworkContext(NetworkContext* network_context) { | 105 void NetworkServiceImpl::DeregisterNetworkContext( |
| 106 NetworkContext* network_context) { |
83 DCHECK_EQ(1u, network_contexts_.count(network_context)); | 107 DCHECK_EQ(1u, network_contexts_.count(network_context)); |
84 network_contexts_.erase(network_context); | 108 network_contexts_.erase(network_context); |
85 } | 109 } |
86 | 110 |
87 void NetworkService::CreateNetworkContext( | 111 void NetworkServiceImpl::CreateNetworkContext( |
88 mojom::NetworkContextRequest request, | 112 mojom::NetworkContextRequest request, |
89 mojom::NetworkContextParamsPtr params) { | 113 mojom::NetworkContextParamsPtr params) { |
90 // The NetworkContext will destroy itself on connection error, or when the | 114 // The NetworkContext will destroy itself on connection error, or when the |
91 // service is destroyed. | 115 // service is destroyed. |
92 new NetworkContext(this, std::move(request), std::move(params)); | 116 new NetworkContext(this, std::move(request), std::move(params)); |
93 } | 117 } |
94 | 118 |
95 NetworkService::NetworkService() : NetworkService(nullptr) {} | 119 void NetworkServiceImpl::OnBindInterface( |
96 | |
97 void NetworkService::OnBindInterface( | |
98 const service_manager::BindSourceInfo& source_info, | 120 const service_manager::BindSourceInfo& source_info, |
99 const std::string& interface_name, | 121 const std::string& interface_name, |
100 mojo::ScopedMessagePipeHandle interface_pipe) { | 122 mojo::ScopedMessagePipeHandle interface_pipe) { |
101 registry_->BindInterface(source_info, interface_name, | 123 registry_->BindInterface(source_info, interface_name, |
102 std::move(interface_pipe)); | 124 std::move(interface_pipe)); |
103 } | 125 } |
104 | 126 |
105 void NetworkService::Create(const service_manager::BindSourceInfo& source_info, | 127 void NetworkServiceImpl::Create( |
106 mojom::NetworkServiceRequest request) { | 128 const service_manager::BindSourceInfo& source_info, |
| 129 mojom::NetworkServiceRequest request) { |
107 DCHECK(!binding_.is_bound()); | 130 DCHECK(!binding_.is_bound()); |
108 binding_.Bind(std::move(request)); | 131 binding_.Bind(std::move(request)); |
109 } | 132 } |
110 | 133 |
111 } // namespace content | 134 } // namespace content |
OLD | NEW |