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

Side by Side Diff: services/service_manager/tests/lifecycle/parent.cc

Issue 2795883002: Eliminate OnConnect usage (Closed)
Patch Set: . Created 3 years, 8 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
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 <memory> 5 #include <memory>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "mojo/public/cpp/bindings/binding_set.h" 10 #include "mojo/public/cpp/bindings/binding_set.h"
11 #include "services/service_manager/public/c/main.h" 11 #include "services/service_manager/public/c/main.h"
12 #include "services/service_manager/public/cpp/binder_registry.h"
12 #include "services/service_manager/public/cpp/connector.h" 13 #include "services/service_manager/public/cpp/connector.h"
13 #include "services/service_manager/public/cpp/interface_factory.h" 14 #include "services/service_manager/public/cpp/interface_factory.h"
14 #include "services/service_manager/public/cpp/interface_registry.h"
15 #include "services/service_manager/public/cpp/service.h" 15 #include "services/service_manager/public/cpp/service.h"
16 #include "services/service_manager/public/cpp/service_context.h" 16 #include "services/service_manager/public/cpp/service_context.h"
17 #include "services/service_manager/public/cpp/service_runner.h" 17 #include "services/service_manager/public/cpp/service_runner.h"
18 #include "services/service_manager/tests/lifecycle/lifecycle_unittest.mojom.h" 18 #include "services/service_manager/tests/lifecycle/lifecycle_unittest.mojom.h"
19 19
20 namespace { 20 namespace {
21 21
22 void QuitLoop(base::RunLoop* loop) { 22 void QuitLoop(base::RunLoop* loop) {
23 loop->Quit(); 23 loop->Quit();
24 } 24 }
25 25
26 class Parent : public service_manager::Service, 26 class Parent : public service_manager::Service,
27 public service_manager::InterfaceFactory< 27 public service_manager::InterfaceFactory<
28 service_manager::test::mojom::Parent>, 28 service_manager::test::mojom::Parent>,
29 public service_manager::test::mojom::Parent { 29 public service_manager::test::mojom::Parent {
30 public: 30 public:
31 Parent() {} 31 Parent() {
32 registry_.AddInterface<service_manager::test::mojom::Parent>(this);
33 }
32 ~Parent() override { 34 ~Parent() override {
33 child_connection_.reset();
34 parent_bindings_.CloseAllBindings(); 35 parent_bindings_.CloseAllBindings();
35 } 36 }
36 37
37 private: 38 private:
38 // Service: 39 // Service:
39 bool OnConnect(const service_manager::ServiceInfo& remote_info, 40 void OnBindInterface(const service_manager::ServiceInfo& source_info,
40 service_manager::InterfaceRegistry* registry) override { 41 const std::string& interface_name,
41 registry->AddInterface<service_manager::test::mojom::Parent>(this); 42 mojo::ScopedMessagePipeHandle interface_pipe) override {
42 return true; 43 registry_.BindInterface(source_info.identity, interface_name,
44 std::move(interface_pipe));
43 } 45 }
44 46
45 // InterfaceFactory<service_manager::test::mojom::Parent>: 47 // InterfaceFactory<service_manager::test::mojom::Parent>:
46 void Create(const service_manager::Identity& remote_identity, 48 void Create(const service_manager::Identity& remote_identity,
47 service_manager::test::mojom::ParentRequest request) override { 49 service_manager::test::mojom::ParentRequest request) override {
48 parent_bindings_.AddBinding(this, std::move(request)); 50 parent_bindings_.AddBinding(this, std::move(request));
49 } 51 }
50 52
51 // service_manager::test::mojom::Parent: 53 // service_manager::test::mojom::Parent:
52 void ConnectToChild(const ConnectToChildCallback& callback) override { 54 void ConnectToChild(const ConnectToChildCallback& callback) override {
53 child_connection_ =
54 context()->connector()->Connect("lifecycle_unittest_app");
55 service_manager::test::mojom::LifecycleControlPtr lifecycle; 55 service_manager::test::mojom::LifecycleControlPtr lifecycle;
56 child_connection_->GetInterface(&lifecycle); 56 context()->connector()->BindInterface("lifecycle_unittest_app", &lifecycle);
57 { 57 {
58 base::RunLoop loop; 58 base::RunLoop loop;
59 lifecycle->Ping(base::Bind(&QuitLoop, &loop)); 59 lifecycle->Ping(base::Bind(&QuitLoop, &loop));
60 base::MessageLoop::ScopedNestableTaskAllower allow( 60 base::MessageLoop::ScopedNestableTaskAllower allow(
61 base::MessageLoop::current()); 61 base::MessageLoop::current());
62 loop.Run(); 62 loop.Run();
63 } 63 }
64 callback.Run(); 64 callback.Run();
65 } 65 }
66 void Quit() override { 66 void Quit() override {
67 base::MessageLoop::current()->QuitWhenIdle(); 67 base::MessageLoop::current()->QuitWhenIdle();
68 } 68 }
69 69
70 std::unique_ptr<service_manager::Connection> child_connection_; 70 service_manager::BinderRegistry registry_;
71 mojo::BindingSet<service_manager::test::mojom::Parent> parent_bindings_; 71 mojo::BindingSet<service_manager::test::mojom::Parent> parent_bindings_;
72 72
73 DISALLOW_COPY_AND_ASSIGN(Parent); 73 DISALLOW_COPY_AND_ASSIGN(Parent);
74 }; 74 };
75 75
76 } // namespace 76 } // namespace
77 77
78 MojoResult ServiceMain(MojoHandle service_request_handle) { 78 MojoResult ServiceMain(MojoHandle service_request_handle) {
79 Parent* parent = new Parent; 79 Parent* parent = new Parent;
80 return service_manager::ServiceRunner(parent).Run(service_request_handle); 80 return service_manager::ServiceRunner(parent).Run(service_request_handle);
81 } 81 }
OLDNEW
« no previous file with comments | « services/service_manager/tests/lifecycle/package.cc ('k') | services/service_manager/tests/service_manager/embedder.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698