Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(738)

Side by Side Diff: content/shell/utility/shell_content_utility_client.cc

Issue 2837813004: Sync the browser test host resolver with the network process before a test runs. (Closed)
Patch Set: review comments Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/shell/utility/shell_content_utility_client.h ('k') | content/test/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/shell/utility/shell_content_utility_client.h" 5 #include "content/shell/utility/shell_content_utility_client.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/files/scoped_temp_dir.h" 11 #include "base/files/scoped_temp_dir.h"
12 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
13 #include "base/process/process.h" 13 #include "base/process/process.h"
14 #include "content/public/child/child_thread.h" 14 #include "content/public/child/child_thread.h"
15 #include "content/public/common/service_manager_connection.h" 15 #include "content/public/common/service_manager_connection.h"
16 #include "content/public/common/simple_connection_filter.h" 16 #include "content/public/common/simple_connection_filter.h"
17 #include "content/public/test/test_host_resolver.h"
17 #include "content/public/test/test_service.h" 18 #include "content/public/test/test_service.h"
18 #include "content/public/test/test_service.mojom.h" 19 #include "content/public/test/test_service.mojom.h"
19 #include "mojo/public/cpp/bindings/strong_binding.h" 20 #include "mojo/public/cpp/bindings/strong_binding.h"
20 #include "mojo/public/cpp/system/buffer.h" 21 #include "mojo/public/cpp/system/buffer.h"
22 #include "net/base/net_errors.h"
23 #include "net/base/network_interfaces.h"
24 #include "net/dns/mock_host_resolver.h"
21 #include "services/service_manager/public/cpp/binder_registry.h" 25 #include "services/service_manager/public/cpp/binder_registry.h"
22 26
23 namespace content { 27 namespace content {
24 28
25 namespace { 29 namespace {
26 30
27 class TestServiceImpl : public mojom::TestService { 31 class TestServiceImpl : public mojom::TestService {
28 public: 32 public:
29 static void Create(mojom::TestServiceRequest request) { 33 static void Create(mojom::TestServiceRequest request) {
30 mojo::MakeStrongBinding(base::WrapUnique(new TestServiceImpl), 34 mojo::MakeStrongBinding(base::WrapUnique(new TestServiceImpl),
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 private: 71 private:
68 explicit TestServiceImpl() {} 72 explicit TestServiceImpl() {}
69 73
70 DISALLOW_COPY_AND_ASSIGN(TestServiceImpl); 74 DISALLOW_COPY_AND_ASSIGN(TestServiceImpl);
71 }; 75 };
72 76
73 std::unique_ptr<service_manager::Service> CreateTestService() { 77 std::unique_ptr<service_manager::Service> CreateTestService() {
74 return std::unique_ptr<service_manager::Service>(new TestService); 78 return std::unique_ptr<service_manager::Service>(new TestService);
75 } 79 }
76 80
81 class NetworkServiceTestImpl : public mojom::NetworkServiceTest {
82 public:
83 static void Create(mojom::NetworkServiceTestRequest request) {
84 // Leak this.
85 new NetworkServiceTestImpl(std::move(request));
86 }
87 explicit NetworkServiceTestImpl(mojom::NetworkServiceTestRequest request)
88 : binding_(this, std::move(request)) {}
89 ~NetworkServiceTestImpl() override = default;
90
91 // mojom::NetworkServiceTest implementation.
92 void AddRules(std::vector<mojom::RulePtr> rules) override {
93 for (const auto& rule : rules) {
94 test_host_resolver_.host_resolver()->AddRule(rule->host_pattern,
95 rule->replacement);
96 }
97 }
98
99 private:
100 mojo::Binding<mojom::NetworkServiceTest> binding_;
101
102 TestHostResolver test_host_resolver_;
103
104 DISALLOW_COPY_AND_ASSIGN(NetworkServiceTestImpl);
105 };
106
77 } // namespace 107 } // namespace
78 108
109 ShellContentUtilityClient::ShellContentUtilityClient() {}
110
79 ShellContentUtilityClient::~ShellContentUtilityClient() { 111 ShellContentUtilityClient::~ShellContentUtilityClient() {
80 } 112 }
81 113
82 void ShellContentUtilityClient::UtilityThreadStarted() { 114 void ShellContentUtilityClient::UtilityThreadStarted() {
83 auto registry = base::MakeUnique<service_manager::BinderRegistry>(); 115 auto registry = base::MakeUnique<service_manager::BinderRegistry>();
84 registry->AddInterface(base::Bind(&TestServiceImpl::Create), 116 registry->AddInterface(base::Bind(&TestServiceImpl::Create),
85 base::ThreadTaskRunnerHandle::Get()); 117 base::ThreadTaskRunnerHandle::Get());
86 content::ChildThread::Get() 118 content::ChildThread::Get()
87 ->GetServiceManagerConnection() 119 ->GetServiceManagerConnection()
88 ->AddConnectionFilter( 120 ->AddConnectionFilter(
89 base::MakeUnique<SimpleConnectionFilter>(std::move(registry))); 121 base::MakeUnique<SimpleConnectionFilter>(std::move(registry)));
90 } 122 }
91 123
92 void ShellContentUtilityClient::RegisterServices(StaticServiceMap* services) { 124 void ShellContentUtilityClient::RegisterServices(StaticServiceMap* services) {
93 ServiceInfo info; 125 ServiceInfo info;
94 info.factory = base::Bind(&CreateTestService); 126 info.factory = base::Bind(&CreateTestService);
95 services->insert(std::make_pair(kTestServiceUrl, info)); 127 services->insert(std::make_pair(kTestServiceUrl, info));
96 } 128 }
97 129
130 void ShellContentUtilityClient::RegisterNetworkBinders(
131 service_manager::BinderRegistry* registry) {
132 registry->AddInterface<mojom::NetworkServiceTest>(
133 base::Bind(&ShellContentUtilityClient::Create, base::Unretained(this)));
134 }
135
136 void ShellContentUtilityClient::Create(
137 mojom::NetworkServiceTestRequest request) {
138 DCHECK(!network_service_test_);
139 network_service_test_ =
140 base::MakeUnique<NetworkServiceTestImpl>(std::move(request));
141 }
142
98 } // namespace content 143 } // namespace content
OLDNEW
« no previous file with comments | « content/shell/utility/shell_content_utility_client.h ('k') | content/test/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698