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

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

Issue 2500683002: Revert of Service Manager: Remove ServiceContext* arg from Service::OnStart() (Closed)
Patch Set: Created 4 years, 1 month 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 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 <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 7
8 #include <memory> 8 #include <memory>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 title_(title), 47 title_(title),
48 request_(std::move(request)) { 48 request_(std::move(request)) {
49 Start(); 49 Start();
50 } 50 }
51 51
52 ~ProvidedService() override { 52 ~ProvidedService() override {
53 Join(); 53 Join();
54 } 54 }
55 55
56 private: 56 private:
57 class ForwardingServiceImpl : public Service {
58 public:
59 explicit ForwardingServiceImpl(Service* service)
60 : service_(service) {}
61 ~ForwardingServiceImpl() override {}
62
63 // Service:
64 void OnStart(ServiceContext* context) override {
65 service_->OnStart(context);
66 }
67
68 bool OnConnect(const ServiceInfo& remote_info,
69 InterfaceRegistry* registry) override {
70 return service_->OnConnect(remote_info, registry);
71 }
72
73 bool OnStop() override { return service_->OnStop(); }
74
75 private:
76 Service* const service_;
77
78 DISALLOW_COPY_AND_ASSIGN(ForwardingServiceImpl);
79 };
80
57 // service_manager::Service: 81 // service_manager::Service:
58 void OnStart() override { 82 void OnStart(ServiceContext* context) override {
83 context_ = context;
59 bindings_.set_connection_error_handler( 84 bindings_.set_connection_error_handler(
60 base::Bind(&ProvidedService::OnConnectionError, 85 base::Bind(&ProvidedService::OnConnectionError,
61 base::Unretained(this))); 86 base::Unretained(this)));
62 } 87 }
63 88
64 bool OnConnect(const ServiceInfo& remote_info, 89 bool OnConnect(const ServiceInfo& remote_info,
65 InterfaceRegistry* registry) override { 90 InterfaceRegistry* registry) override {
66 registry->AddInterface<test::mojom::ConnectTestService>(this); 91 registry->AddInterface<test::mojom::ConnectTestService>(this);
67 registry->AddInterface<test::mojom::BlockedInterface>(this); 92 registry->AddInterface<test::mojom::BlockedInterface>(this);
68 registry->AddInterface<test::mojom::UserIdTest>(this); 93 registry->AddInterface<test::mojom::UserIdTest>(this);
69 94
70 test::mojom::ConnectionStatePtr state(test::mojom::ConnectionState::New()); 95 test::mojom::ConnectionStatePtr state(test::mojom::ConnectionState::New());
71 state->connection_remote_name = remote_info.identity.name(); 96 state->connection_remote_name = remote_info.identity.name();
72 state->connection_remote_userid = remote_info.identity.user_id(); 97 state->connection_remote_userid = remote_info.identity.user_id();
73 state->initialize_local_name = context()->identity().name(); 98 state->initialize_local_name = context_->identity().name();
74 state->initialize_userid = context()->identity().user_id(); 99 state->initialize_userid = context_->identity().user_id();
75 100
76 context()->connector()->ConnectToInterface(remote_info.identity, &caller_); 101 context_->connector()->ConnectToInterface(remote_info.identity, &caller_);
77 caller_->ConnectionAccepted(std::move(state)); 102 caller_->ConnectionAccepted(std::move(state));
78 103
79 return true; 104 return true;
80 } 105 }
81 106
82 // InterfaceFactory<test::mojom::ConnectTestService>: 107 // InterfaceFactory<test::mojom::ConnectTestService>:
83 void Create(const Identity& remote_identity, 108 void Create(const Identity& remote_identity,
84 test::mojom::ConnectTestServiceRequest request) override { 109 test::mojom::ConnectTestServiceRequest request) override {
85 bindings_.AddBinding(this, std::move(request)); 110 bindings_.AddBinding(this, std::move(request));
86 } 111 }
87 112
88 // InterfaceFactory<test::mojom::BlockedInterface>: 113 // InterfaceFactory<test::mojom::BlockedInterface>:
89 void Create(const Identity& remote_identity, 114 void Create(const Identity& remote_identity,
90 test::mojom::BlockedInterfaceRequest request) override { 115 test::mojom::BlockedInterfaceRequest request) override {
91 blocked_bindings_.AddBinding(this, std::move(request)); 116 blocked_bindings_.AddBinding(this, std::move(request));
92 } 117 }
93 118
94 // InterfaceFactory<test::mojom::UserIdTest>: 119 // InterfaceFactory<test::mojom::UserIdTest>:
95 void Create(const Identity& remote_identity, 120 void Create(const Identity& remote_identity,
96 test::mojom::UserIdTestRequest request) override { 121 test::mojom::UserIdTestRequest request) override {
97 user_id_test_bindings_.AddBinding(this, std::move(request)); 122 user_id_test_bindings_.AddBinding(this, std::move(request));
98 } 123 }
99 124
100 // test::mojom::ConnectTestService: 125 // test::mojom::ConnectTestService:
101 void GetTitle(const GetTitleCallback& callback) override { 126 void GetTitle(const GetTitleCallback& callback) override {
102 callback.Run(title_); 127 callback.Run(title_);
103 } 128 }
104 129
105 void GetInstance(const GetInstanceCallback& callback) override { 130 void GetInstance(const GetInstanceCallback& callback) override {
106 callback.Run(context()->identity().instance()); 131 callback.Run(context_->identity().instance());
107 } 132 }
108 133
109 // test::mojom::BlockedInterface: 134 // test::mojom::BlockedInterface:
110 void GetTitleBlocked(const GetTitleBlockedCallback& callback) override { 135 void GetTitleBlocked(const GetTitleBlockedCallback& callback) override {
111 callback.Run("Called Blocked Interface!"); 136 callback.Run("Called Blocked Interface!");
112 } 137 }
113 138
114 // test::mojom::UserIdTest: 139 // test::mojom::UserIdTest:
115 void ConnectToClassAppAsDifferentUser( 140 void ConnectToClassAppAsDifferentUser(
116 const service_manager::Identity& target, 141 const service_manager::Identity& target,
117 const ConnectToClassAppAsDifferentUserCallback& callback) override { 142 const ConnectToClassAppAsDifferentUserCallback& callback) override {
118 Connector::ConnectParams params(target); 143 Connector::ConnectParams params(target);
119 std::unique_ptr<Connection> connection = 144 std::unique_ptr<Connection> connection =
120 context()->connector()->Connect(&params); 145 context_->connector()->Connect(&params);
121 { 146 {
122 base::RunLoop loop; 147 base::RunLoop loop;
123 connection->AddConnectionCompletedClosure(loop.QuitClosure()); 148 connection->AddConnectionCompletedClosure(loop.QuitClosure());
124 base::MessageLoop::ScopedNestableTaskAllower allow( 149 base::MessageLoop::ScopedNestableTaskAllower allow(
125 base::MessageLoop::current()); 150 base::MessageLoop::current());
126 loop.Run(); 151 loop.Run();
127 } 152 }
128 callback.Run(static_cast<int32_t>(connection->GetResult()), 153 callback.Run(static_cast<int32_t>(connection->GetResult()),
129 connection->GetRemoteIdentity()); 154 connection->GetRemoteIdentity());
130 } 155 }
131 156
132 // base::SimpleThread: 157 // base::SimpleThread:
133 void Run() override { 158 void Run() override {
134 ServiceRunner(new ForwardingService(this)).Run( 159 ServiceRunner(new ForwardingServiceImpl(this)).Run(
135 request_.PassMessagePipe().release().value(), false); 160 request_.PassMessagePipe().release().value(), false);
136 caller_.reset(); 161 caller_.reset();
137 bindings_.CloseAllBindings(); 162 bindings_.CloseAllBindings();
138 blocked_bindings_.CloseAllBindings(); 163 blocked_bindings_.CloseAllBindings();
139 user_id_test_bindings_.CloseAllBindings(); 164 user_id_test_bindings_.CloseAllBindings();
140 } 165 }
141 166
142 void OnConnectionError() { 167 void OnConnectionError() {
143 if (bindings_.empty()) 168 if (bindings_.empty())
144 base::MessageLoop::current()->QuitWhenIdle(); 169 base::MessageLoop::current()->QuitWhenIdle();
145 } 170 }
146 171
172 ServiceContext* context_ = nullptr;
147 const std::string title_; 173 const std::string title_;
148 mojom::ServiceRequest request_; 174 mojom::ServiceRequest request_;
149 test::mojom::ExposedInterfacePtr caller_; 175 test::mojom::ExposedInterfacePtr caller_;
150 mojo::BindingSet<test::mojom::ConnectTestService> bindings_; 176 mojo::BindingSet<test::mojom::ConnectTestService> bindings_;
151 mojo::BindingSet<test::mojom::BlockedInterface> blocked_bindings_; 177 mojo::BindingSet<test::mojom::BlockedInterface> blocked_bindings_;
152 mojo::BindingSet<test::mojom::UserIdTest> user_id_test_bindings_; 178 mojo::BindingSet<test::mojom::UserIdTest> user_id_test_bindings_;
153 179
154 DISALLOW_COPY_AND_ASSIGN(ProvidedService); 180 DISALLOW_COPY_AND_ASSIGN(ProvidedService);
155 }; 181 };
156 182
157 class ConnectTestService 183 class ConnectTestService
158 : public Service, 184 : public Service,
159 public InterfaceFactory<mojom::ServiceFactory>, 185 public InterfaceFactory<mojom::ServiceFactory>,
160 public InterfaceFactory<test::mojom::ConnectTestService>, 186 public InterfaceFactory<test::mojom::ConnectTestService>,
161 public mojom::ServiceFactory, 187 public mojom::ServiceFactory,
162 public test::mojom::ConnectTestService { 188 public test::mojom::ConnectTestService {
163 public: 189 public:
164 ConnectTestService() {} 190 ConnectTestService() {}
165 ~ConnectTestService() override {} 191 ~ConnectTestService() override {}
166 192
167 private: 193 private:
168 // service_manager::Service: 194 // service_manager::Service:
169 void OnStart() override { 195 void OnStart(ServiceContext* context) override {
196 context_ = context;
197
170 base::Closure error_handler = 198 base::Closure error_handler =
171 base::Bind(&ConnectTestService::OnConnectionError, 199 base::Bind(&ConnectTestService::OnConnectionError,
172 base::Unretained(this)); 200 base::Unretained(this));
173 bindings_.set_connection_error_handler(error_handler); 201 bindings_.set_connection_error_handler(error_handler);
174 service_factory_bindings_.set_connection_error_handler(error_handler); 202 service_factory_bindings_.set_connection_error_handler(error_handler);
175 } 203 }
176 204
177 bool OnConnect(const ServiceInfo& remote_info, 205 bool OnConnect(const ServiceInfo& remote_info,
178 InterfaceRegistry* registry) override { 206 InterfaceRegistry* registry) override {
179 registry->AddInterface<ServiceFactory>(this); 207 registry->AddInterface<ServiceFactory>(this);
(...skipping 29 matching lines...) Expand all
209 base::MakeUnique<ProvidedService>("B", std::move(request))); 237 base::MakeUnique<ProvidedService>("B", std::move(request)));
210 } 238 }
211 } 239 }
212 240
213 // test::mojom::ConnectTestService: 241 // test::mojom::ConnectTestService:
214 void GetTitle(const GetTitleCallback& callback) override { 242 void GetTitle(const GetTitleCallback& callback) override {
215 callback.Run("ROOT"); 243 callback.Run("ROOT");
216 } 244 }
217 245
218 void GetInstance(const GetInstanceCallback& callback) override { 246 void GetInstance(const GetInstanceCallback& callback) override {
219 callback.Run(context()->identity().instance()); 247 callback.Run(context_->identity().instance());
220 } 248 }
221 249
222 void OnConnectionError() { 250 void OnConnectionError() {
223 if (bindings_.empty() && service_factory_bindings_.empty()) 251 if (bindings_.empty() && service_factory_bindings_.empty())
224 context()->RequestQuit(); 252 context_->RequestQuit();
225 } 253 }
226 254
255 ServiceContext* context_ = nullptr;
227 std::vector<std::unique_ptr<Service>> delegates_; 256 std::vector<std::unique_ptr<Service>> delegates_;
228 mojo::BindingSet<mojom::ServiceFactory> service_factory_bindings_; 257 mojo::BindingSet<mojom::ServiceFactory> service_factory_bindings_;
229 mojo::BindingSet<test::mojom::ConnectTestService> bindings_; 258 mojo::BindingSet<test::mojom::ConnectTestService> bindings_;
230 std::list<std::unique_ptr<ProvidedService>> provided_services_; 259 std::list<std::unique_ptr<ProvidedService>> provided_services_;
231 260
232 DISALLOW_COPY_AND_ASSIGN(ConnectTestService); 261 DISALLOW_COPY_AND_ASSIGN(ConnectTestService);
233 }; 262 };
234 263
235 } // namespace service_manager 264 } // namespace service_manager
236 265
237 MojoResult ServiceMain(MojoHandle service_request_handle) { 266 MojoResult ServiceMain(MojoHandle service_request_handle) {
238 service_manager::ServiceRunner runner( 267 service_manager::ServiceRunner runner(
239 new service_manager::ConnectTestService); 268 new service_manager::ConnectTestService);
240 return runner.Run(service_request_handle); 269 return runner.Run(service_request_handle);
241 } 270 }
OLDNEW
« no previous file with comments | « services/service_manager/tests/connect/connect_test_exe.cc ('k') | services/service_manager/tests/lifecycle/app_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698