OLD | NEW |
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 <algorithm> | 5 #include <algorithm> |
6 #include <memory> | 6 #include <memory> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/macros.h" | 9 #include "base/macros.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/service_context.h" | 12 #include "services/service_manager/public/cpp/service_context.h" |
13 #include "services/service_manager/public/cpp/service_runner.h" | 13 #include "services/service_manager/public/cpp/service_runner.h" |
14 #include "services/service_manager/public/interfaces/service_factory.mojom.h" | 14 #include "services/service_manager/public/interfaces/service_factory.mojom.h" |
15 #include "services/service_manager/tests/lifecycle/app_client.h" | 15 #include "services/service_manager/tests/lifecycle/app_client.h" |
16 #include "services/service_manager/tests/lifecycle/lifecycle_unittest.mojom.h" | 16 #include "services/service_manager/tests/lifecycle/lifecycle_unittest.mojom.h" |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 class PackagedApp : public shell::Service, | 20 class PackagedApp : public service_manager::Service, |
21 public shell::InterfaceFactory<LifecycleControl>, | 21 public service_manager::InterfaceFactory<LifecycleControl>, |
22 public LifecycleControl { | 22 public LifecycleControl { |
23 public: | 23 public: |
24 using DestructCallback = base::Callback<void(PackagedApp*)>; | 24 using DestructCallback = base::Callback<void(PackagedApp*)>; |
25 | 25 |
26 PackagedApp(shell::mojom::ServiceRequest request, | 26 PackagedApp( |
27 const DestructCallback& shell_connection_closed_callback, | 27 service_manager::mojom::ServiceRequest request, |
28 const DestructCallback& destruct_callback) | 28 const DestructCallback& service_manager_connection_closed_callback, |
29 : connection_(new shell::ServiceContext(this, std::move(request))), | 29 const DestructCallback& destruct_callback) |
30 shell_connection_closed_callback_(shell_connection_closed_callback), | 30 : connection_( |
| 31 new service_manager::ServiceContext(this, std::move(request))), |
| 32 service_manager_connection_closed_callback_( |
| 33 service_manager_connection_closed_callback), |
31 destruct_callback_(destruct_callback) { | 34 destruct_callback_(destruct_callback) { |
32 bindings_.set_connection_error_handler(base::Bind(&PackagedApp::BindingLost, | 35 bindings_.set_connection_error_handler(base::Bind(&PackagedApp::BindingLost, |
33 base::Unretained(this))); | 36 base::Unretained(this))); |
34 } | 37 } |
35 ~PackagedApp() override { | 38 ~PackagedApp() override { |
36 destruct_callback_.Run(this); | 39 destruct_callback_.Run(this); |
37 } | 40 } |
38 | 41 |
39 private: | 42 private: |
40 // shell::Service: | 43 // service_manager::Service: |
41 bool OnConnect(const shell::Identity& remote_identity, | 44 bool OnConnect(const service_manager::Identity& remote_identity, |
42 shell::InterfaceRegistry* registry) override { | 45 service_manager::InterfaceRegistry* registry) override { |
43 registry->AddInterface<LifecycleControl>(this); | 46 registry->AddInterface<LifecycleControl>(this); |
44 return true; | 47 return true; |
45 } | 48 } |
46 | 49 |
47 // shell::InterfaceFactory<LifecycleControl> | 50 // service_manager::InterfaceFactory<LifecycleControl> |
48 void Create(const shell::Identity& remote_identity, | 51 void Create(const service_manager::Identity& remote_identity, |
49 LifecycleControlRequest request) override { | 52 LifecycleControlRequest request) override { |
50 bindings_.AddBinding(this, std::move(request)); | 53 bindings_.AddBinding(this, std::move(request)); |
51 } | 54 } |
52 | 55 |
53 // LifecycleControl: | 56 // LifecycleControl: |
54 void Ping(const PingCallback& callback) override { | 57 void Ping(const PingCallback& callback) override { |
55 callback.Run(); | 58 callback.Run(); |
56 } | 59 } |
57 void GracefulQuit() override { | 60 void GracefulQuit() override { |
58 shell_connection_closed_callback_.Run(this); | 61 service_manager_connection_closed_callback_.Run(this); |
59 delete this; | 62 delete this; |
60 // This will have closed all |bindings_|. | 63 // This will have closed all |bindings_|. |
61 } | 64 } |
62 void Crash() override { | 65 void Crash() override { |
63 // When multiple instances are vended from the same package instance, this | 66 // When multiple instances are vended from the same package instance, this |
64 // will cause all instances to be quit. | 67 // will cause all instances to be quit. |
65 exit(1); | 68 exit(1); |
66 } | 69 } |
67 void CloseShellConnection() override { | 70 void CloseShellConnection() override { |
68 shell_connection_closed_callback_.Run(this); | 71 service_manager_connection_closed_callback_.Run(this); |
69 connection_.reset(); | 72 connection_.reset(); |
70 // This only closed our relationship with the shell, existing |bindings_| | 73 // This only closed our relationship with the shell, existing |bindings_| |
71 // remain active. | 74 // remain active. |
72 } | 75 } |
73 | 76 |
74 void BindingLost() { | 77 void BindingLost() { |
75 if (bindings_.empty()) | 78 if (bindings_.empty()) |
76 delete this; | 79 delete this; |
77 } | 80 } |
78 | 81 |
79 std::unique_ptr<shell::ServiceContext> connection_; | 82 std::unique_ptr<service_manager::ServiceContext> connection_; |
80 mojo::BindingSet<LifecycleControl> bindings_; | 83 mojo::BindingSet<LifecycleControl> bindings_; |
81 // Run when this object's connection to the shell is closed. | 84 // Run when this object's connection to the shell is closed. |
82 DestructCallback shell_connection_closed_callback_; | 85 DestructCallback service_manager_connection_closed_callback_; |
83 // Run when this object is destructed. | 86 // Run when this object is destructed. |
84 DestructCallback destruct_callback_; | 87 DestructCallback destruct_callback_; |
85 | 88 |
86 DISALLOW_COPY_AND_ASSIGN(PackagedApp); | 89 DISALLOW_COPY_AND_ASSIGN(PackagedApp); |
87 }; | 90 }; |
88 | 91 |
89 class Package | 92 class Package : public service_manager::Service, |
90 : public shell::Service, | 93 public service_manager::InterfaceFactory< |
91 public shell::InterfaceFactory<shell::mojom::ServiceFactory>, | 94 service_manager::mojom::ServiceFactory>, |
92 public shell::mojom::ServiceFactory { | 95 public service_manager::mojom::ServiceFactory { |
93 public: | 96 public: |
94 Package() {} | 97 Package() {} |
95 ~Package() override {} | 98 ~Package() override {} |
96 | 99 |
97 void set_runner(shell::ServiceRunner* runner) { | 100 void set_runner(service_manager::ServiceRunner* runner) { |
98 app_client_.set_runner(runner); | 101 app_client_.set_runner(runner); |
99 } | 102 } |
100 | 103 |
101 private: | 104 private: |
102 // shell::test::AppClient: | 105 // service_manager::test::AppClient: |
103 bool OnConnect(const shell::Identity& remote_identity, | 106 bool OnConnect(const service_manager::Identity& remote_identity, |
104 shell::InterfaceRegistry* registry) override { | 107 service_manager::InterfaceRegistry* registry) override { |
105 registry->AddInterface<shell::mojom::ServiceFactory>(this); | 108 registry->AddInterface<service_manager::mojom::ServiceFactory>(this); |
106 return app_client_.OnConnect(remote_identity, registry); | 109 return app_client_.OnConnect(remote_identity, registry); |
107 } | 110 } |
108 | 111 |
109 // shell::InterfaceFactory<shell::mojom::ServiceFactory>: | 112 // service_manager::InterfaceFactory<service_manager::mojom::ServiceFactory>: |
110 void Create(const shell::Identity& remote_identity, | 113 void Create(const service_manager::Identity& remote_identity, |
111 shell::mojom::ServiceFactoryRequest request) override { | 114 service_manager::mojom::ServiceFactoryRequest request) override { |
112 bindings_.AddBinding(this, std::move(request)); | 115 bindings_.AddBinding(this, std::move(request)); |
113 } | 116 } |
114 | 117 |
115 // shell::mojom::ServiceFactory: | 118 // service_manager::mojom::ServiceFactory: |
116 void CreateService(shell::mojom::ServiceRequest request, | 119 void CreateService(service_manager::mojom::ServiceRequest request, |
117 const std::string& name) override { | 120 const std::string& name) override { |
118 ++shell_connection_refcount_; | 121 ++service_manager_connection_refcount_; |
119 apps_.push_back( | 122 apps_.push_back( |
120 new PackagedApp(std::move(request), | 123 new PackagedApp(std::move(request), |
121 base::Bind(&Package::AppShellConnectionClosed, | 124 base::Bind(&Package::AppShellConnectionClosed, |
122 base::Unretained(this)), | 125 base::Unretained(this)), |
123 base::Bind(&Package::AppDestructed, | 126 base::Bind(&Package::AppDestructed, |
124 base::Unretained(this)))); | 127 base::Unretained(this)))); |
125 } | 128 } |
126 | 129 |
127 void AppShellConnectionClosed(PackagedApp* app) { | 130 void AppShellConnectionClosed(PackagedApp* app) { |
128 if (!--shell_connection_refcount_) | 131 if (!--service_manager_connection_refcount_) |
129 app_client_.CloseShellConnection(); | 132 app_client_.CloseShellConnection(); |
130 } | 133 } |
131 | 134 |
132 void AppDestructed(PackagedApp* app) { | 135 void AppDestructed(PackagedApp* app) { |
133 auto it = std::find(apps_.begin(), apps_.end(), app); | 136 auto it = std::find(apps_.begin(), apps_.end(), app); |
134 DCHECK(it != apps_.end()); | 137 DCHECK(it != apps_.end()); |
135 apps_.erase(it); | 138 apps_.erase(it); |
136 if (apps_.empty() && base::MessageLoop::current()->is_running()) | 139 if (apps_.empty() && base::MessageLoop::current()->is_running()) |
137 base::MessageLoop::current()->QuitWhenIdle(); | 140 base::MessageLoop::current()->QuitWhenIdle(); |
138 } | 141 } |
139 | 142 |
140 shell::test::AppClient app_client_; | 143 service_manager::test::AppClient app_client_; |
141 int shell_connection_refcount_ = 0; | 144 int service_manager_connection_refcount_ = 0; |
142 mojo::BindingSet<shell::mojom::ServiceFactory> bindings_; | 145 mojo::BindingSet<service_manager::mojom::ServiceFactory> bindings_; |
143 std::vector<PackagedApp*> apps_; | 146 std::vector<PackagedApp*> apps_; |
144 | 147 |
145 DISALLOW_COPY_AND_ASSIGN(Package); | 148 DISALLOW_COPY_AND_ASSIGN(Package); |
146 }; | 149 }; |
147 | 150 |
148 } // namespace | 151 } // namespace |
149 | 152 |
150 MojoResult ServiceMain(MojoHandle service_request_handle) { | 153 MojoResult ServiceMain(MojoHandle service_request_handle) { |
151 Package* package = new Package; | 154 Package* package = new Package; |
152 shell::ServiceRunner runner(package); | 155 service_manager::ServiceRunner runner(package); |
153 package->set_runner(&runner); | 156 package->set_runner(&runner); |
154 return runner.Run(service_request_handle); | 157 return runner.Run(service_request_handle); |
155 } | 158 } |
OLD | NEW |