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

Side by Side Diff: services/shell/tests/connect/connect_test_package.cc

Issue 2419723002: Move services/shell to services/service_manager (Closed)
Patch Set: rebase Created 4 years, 2 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
OLDNEW
(Empty)
1 // Copyright 2016 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 <stddef.h>
6 #include <stdint.h>
7
8 #include <memory>
9 #include <utility>
10
11 #include "base/bind.h"
12 #include "base/macros.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/run_loop.h"
15 #include "base/threading/simple_thread.h"
16 #include "mojo/public/cpp/bindings/binding_set.h"
17 #include "services/shell/public/c/main.h"
18 #include "services/shell/public/cpp/connector.h"
19 #include "services/shell/public/cpp/interface_factory.h"
20 #include "services/shell/public/cpp/service.h"
21 #include "services/shell/public/cpp/service_runner.h"
22 #include "services/shell/public/interfaces/service_factory.mojom.h"
23 #include "services/shell/tests/connect/connect_test.mojom.h"
24
25 // Tests that multiple services can be packaged in a single service by
26 // implementing ServiceFactory; that these services can be specified by
27 // the package's manifest and are thus registered with the PackageManager.
28
29 namespace shell {
30
31 namespace {
32
33 void QuitLoop(base::RunLoop* loop) {
34 loop->Quit();
35 }
36
37 } // namespace
38
39 using GetTitleCallback = test::mojom::ConnectTestService::GetTitleCallback;
40
41 class ProvidedService
42 : public Service,
43 public InterfaceFactory<test::mojom::ConnectTestService>,
44 public InterfaceFactory<test::mojom::BlockedInterface>,
45 public InterfaceFactory<test::mojom::UserIdTest>,
46 public test::mojom::ConnectTestService,
47 public test::mojom::BlockedInterface,
48 public test::mojom::UserIdTest,
49 public base::SimpleThread {
50 public:
51 ProvidedService(const std::string& title,
52 mojom::ServiceRequest request)
53 : base::SimpleThread(title),
54 title_(title),
55 request_(std::move(request)) {
56 Start();
57 }
58 ~ProvidedService() override {
59 Join();
60 }
61
62 private:
63 // shell::Service:
64 void OnStart(const Identity& identity) override {
65 identity_ = identity;
66 bindings_.set_connection_error_handler(
67 base::Bind(&ProvidedService::OnConnectionError,
68 base::Unretained(this)));
69 }
70 bool OnConnect(const Identity& remote_identity,
71 InterfaceRegistry* registry) override {
72 registry->AddInterface<test::mojom::ConnectTestService>(this);
73 registry->AddInterface<test::mojom::BlockedInterface>(this);
74 registry->AddInterface<test::mojom::UserIdTest>(this);
75
76 test::mojom::ConnectionStatePtr state(test::mojom::ConnectionState::New());
77 state->connection_remote_name = remote_identity.name();
78 state->connection_remote_userid = remote_identity.user_id();
79 state->initialize_local_name = identity_.name();
80 state->initialize_userid = identity_.user_id();
81
82 connector()->ConnectToInterface(remote_identity, &caller_);
83 caller_->ConnectionAccepted(std::move(state));
84
85 return true;
86 }
87
88 // InterfaceFactory<test::mojom::ConnectTestService>:
89 void Create(const Identity& remote_identity,
90 test::mojom::ConnectTestServiceRequest request) override {
91 bindings_.AddBinding(this, std::move(request));
92 }
93
94 // InterfaceFactory<test::mojom::BlockedInterface>:
95 void Create(const Identity& remote_identity,
96 test::mojom::BlockedInterfaceRequest request) override {
97 blocked_bindings_.AddBinding(this, std::move(request));
98 }
99
100 // InterfaceFactory<test::mojom::UserIdTest>:
101 void Create(const Identity& remote_identity,
102 test::mojom::UserIdTestRequest request) override {
103 user_id_test_bindings_.AddBinding(this, std::move(request));
104 }
105
106 // test::mojom::ConnectTestService:
107 void GetTitle(const GetTitleCallback& callback) override {
108 callback.Run(title_);
109 }
110 void GetInstance(const GetInstanceCallback& callback) override {
111 callback.Run(identity_.instance());
112 }
113
114 // test::mojom::BlockedInterface:
115 void GetTitleBlocked(const GetTitleBlockedCallback& callback) override {
116 callback.Run("Called Blocked Interface!");
117 }
118
119 // test::mojom::UserIdTest:
120 void ConnectToClassAppAsDifferentUser(
121 const shell::Identity& target,
122 const ConnectToClassAppAsDifferentUserCallback& callback) override {
123 Connector::ConnectParams params(target);
124 std::unique_ptr<Connection> connection =
125 connector()->Connect(&params);
126 {
127 base::RunLoop loop;
128 connection->AddConnectionCompletedClosure(base::Bind(&QuitLoop, &loop));
129 base::MessageLoop::ScopedNestableTaskAllower allow(
130 base::MessageLoop::current());
131 loop.Run();
132 }
133 callback.Run(static_cast<int32_t>(connection->GetResult()),
134 connection->GetRemoteIdentity());
135 }
136
137 // base::SimpleThread:
138 void Run() override {
139 ServiceRunner(this).Run(request_.PassMessagePipe().release().value(),
140 false);
141 delete this;
142 }
143
144 void OnConnectionError() {
145 if (bindings_.empty())
146 base::MessageLoop::current()->QuitWhenIdle();
147 }
148
149 Identity identity_;
150 const std::string title_;
151 mojom::ServiceRequest request_;
152 test::mojom::ExposedInterfacePtr caller_;
153 mojo::BindingSet<test::mojom::ConnectTestService> bindings_;
154 mojo::BindingSet<test::mojom::BlockedInterface> blocked_bindings_;
155 mojo::BindingSet<test::mojom::UserIdTest> user_id_test_bindings_;
156
157 DISALLOW_COPY_AND_ASSIGN(ProvidedService);
158 };
159
160 class ConnectTestService
161 : public Service,
162 public InterfaceFactory<mojom::ServiceFactory>,
163 public InterfaceFactory<test::mojom::ConnectTestService>,
164 public mojom::ServiceFactory,
165 public test::mojom::ConnectTestService {
166 public:
167 ConnectTestService() {}
168 ~ConnectTestService() override {}
169
170 private:
171 // shell::Service:
172 void OnStart(const Identity& identity) override {
173 identity_ = identity;
174 bindings_.set_connection_error_handler(
175 base::Bind(&ConnectTestService::OnConnectionError,
176 base::Unretained(this)));
177 }
178 bool OnConnect(const Identity& remote_identity,
179 InterfaceRegistry* registry) override {
180 registry->AddInterface<ServiceFactory>(this);
181 registry->AddInterface<test::mojom::ConnectTestService>(this);
182 return true;
183 }
184
185 // InterfaceFactory<mojom::ServiceFactory>:
186 void Create(const Identity& remote_identity,
187 mojom::ServiceFactoryRequest request) override {
188 service_factory_bindings_.AddBinding(this, std::move(request));
189 }
190
191 // InterfaceFactory<test::mojom::ConnectTestService>:
192 void Create(const Identity& remote_identity,
193 test::mojom::ConnectTestServiceRequest request) override {
194 bindings_.AddBinding(this, std::move(request));
195 }
196
197 // mojom::ServiceFactory:
198 void CreateService(mojom::ServiceRequest request,
199 const std::string& name) override {
200 if (name == "service:connect_test_a")
201 new ProvidedService("A", std::move(request));
202 else if (name == "service:connect_test_b")
203 new ProvidedService("B", std::move(request));
204 }
205
206 // test::mojom::ConnectTestService:
207 void GetTitle(const GetTitleCallback& callback) override {
208 callback.Run("ROOT");
209 }
210 void GetInstance(const GetInstanceCallback& callback) override {
211 callback.Run(identity_.instance());
212 }
213
214 void OnConnectionError() {
215 if (bindings_.empty())
216 base::MessageLoop::current()->QuitWhenIdle();
217 }
218
219 Identity identity_;
220 std::vector<std::unique_ptr<Service>> delegates_;
221 mojo::BindingSet<mojom::ServiceFactory> service_factory_bindings_;
222 mojo::BindingSet<test::mojom::ConnectTestService> bindings_;
223
224 DISALLOW_COPY_AND_ASSIGN(ConnectTestService);
225 };
226
227 } // namespace shell
228
229 MojoResult ServiceMain(MojoHandle service_request_handle) {
230 shell::ServiceRunner runner(new shell::ConnectTestService);
231 return runner.Run(service_request_handle);
232 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698