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

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

Issue 2476063002: Service Manager: Rework Service and ServiceContext lifetime (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 <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/connector.h" 12 #include "services/service_manager/public/cpp/connector.h"
13 #include "services/service_manager/public/cpp/interface_factory.h" 13 #include "services/service_manager/public/cpp/interface_factory.h"
14 #include "services/service_manager/public/cpp/interface_registry.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_runner.h" 17 #include "services/service_manager/public/cpp/service_runner.h"
17 #include "services/service_manager/tests/lifecycle/lifecycle_unittest.mojom.h" 18 #include "services/service_manager/tests/lifecycle/lifecycle_unittest.mojom.h"
18 19
19 namespace { 20 namespace {
20 21
21 void QuitLoop(base::RunLoop* loop) { 22 void QuitLoop(base::RunLoop* loop) {
22 loop->Quit(); 23 loop->Quit();
23 } 24 }
24 25
25 class Parent : public service_manager::Service, 26 class Parent : public service_manager::Service,
26 public service_manager::InterfaceFactory< 27 public service_manager::InterfaceFactory<
27 service_manager::test::mojom::Parent>, 28 service_manager::test::mojom::Parent>,
28 public service_manager::test::mojom::Parent { 29 public service_manager::test::mojom::Parent {
29 public: 30 public:
30 Parent() {} 31 Parent() {}
31 ~Parent() override { 32 ~Parent() override {
32 child_connection_.reset(); 33 child_connection_.reset();
33 parent_bindings_.CloseAllBindings(); 34 parent_bindings_.CloseAllBindings();
34 } 35 }
35 36
36 private: 37 private:
37 // Service: 38 // Service:
39 void OnStart(service_manager::ServiceContext* context) override {
40 context_ = context;
41 }
42
38 bool OnConnect(const service_manager::ServiceInfo& remote_info, 43 bool OnConnect(const service_manager::ServiceInfo& remote_info,
39 service_manager::InterfaceRegistry* registry) override { 44 service_manager::InterfaceRegistry* registry) override {
40 registry->AddInterface<service_manager::test::mojom::Parent>(this); 45 registry->AddInterface<service_manager::test::mojom::Parent>(this);
41 return true; 46 return true;
42 } 47 }
43 48
44 // InterfaceFactory<service_manager::test::mojom::Parent>: 49 // InterfaceFactory<service_manager::test::mojom::Parent>:
45 void Create(const service_manager::Identity& remote_identity, 50 void Create(const service_manager::Identity& remote_identity,
46 service_manager::test::mojom::ParentRequest request) override { 51 service_manager::test::mojom::ParentRequest request) override {
47 parent_bindings_.AddBinding(this, std::move(request)); 52 parent_bindings_.AddBinding(this, std::move(request));
48 } 53 }
49 54
50 // Parent: 55 // service_manager::test::mojom::Parent:
51 void ConnectToChild(const ConnectToChildCallback& callback) override { 56 void ConnectToChild(const ConnectToChildCallback& callback) override {
52 child_connection_ = connector()->Connect("service:lifecycle_unittest_app"); 57 child_connection_ =
58 context_->connector()->Connect("service:lifecycle_unittest_app");
53 service_manager::test::mojom::LifecycleControlPtr lifecycle; 59 service_manager::test::mojom::LifecycleControlPtr lifecycle;
54 child_connection_->GetInterface(&lifecycle); 60 child_connection_->GetInterface(&lifecycle);
55 { 61 {
56 base::RunLoop loop; 62 base::RunLoop loop;
57 lifecycle->Ping(base::Bind(&QuitLoop, &loop)); 63 lifecycle->Ping(base::Bind(&QuitLoop, &loop));
58 base::MessageLoop::ScopedNestableTaskAllower allow( 64 base::MessageLoop::ScopedNestableTaskAllower allow(
59 base::MessageLoop::current()); 65 base::MessageLoop::current());
60 loop.Run(); 66 loop.Run();
61 } 67 }
62 callback.Run(); 68 callback.Run();
63 } 69 }
64 void Quit() override { 70 void Quit() override {
65 base::MessageLoop::current()->QuitWhenIdle(); 71 base::MessageLoop::current()->QuitWhenIdle();
66 } 72 }
67 73
74 service_manager::ServiceContext* context_ = nullptr;
68 std::unique_ptr<service_manager::Connection> child_connection_; 75 std::unique_ptr<service_manager::Connection> child_connection_;
69 mojo::BindingSet<service_manager::test::mojom::Parent> parent_bindings_; 76 mojo::BindingSet<service_manager::test::mojom::Parent> parent_bindings_;
70 77
71 DISALLOW_COPY_AND_ASSIGN(Parent); 78 DISALLOW_COPY_AND_ASSIGN(Parent);
72 }; 79 };
73 80
74 } // namespace 81 } // namespace
75 82
76 MojoResult ServiceMain(MojoHandle service_request_handle) { 83 MojoResult ServiceMain(MojoHandle service_request_handle) {
77 Parent* parent = new Parent; 84 Parent* parent = new Parent;
78 return service_manager::ServiceRunner(parent).Run(service_request_handle); 85 return service_manager::ServiceRunner(parent).Run(service_request_handle);
79 } 86 }
OLDNEW
« no previous file with comments | « services/service_manager/tests/lifecycle/package.cc ('k') | services/service_manager/tests/service_manager/driver.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698