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