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 <stddef.h> | 5 #include <stddef.h> |
6 #include <stdint.h> | 6 #include <stdint.h> |
7 | 7 |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/message_loop/message_loop.h" |
12 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
13 #include "base/threading/simple_thread.h" | 14 #include "base/threading/simple_thread.h" |
14 #include "mojo/public/c/system/main.h" | 15 #include "mojo/public/c/system/main.h" |
15 #include "mojo/public/cpp/bindings/binding_set.h" | 16 #include "mojo/public/cpp/bindings/binding_set.h" |
16 #include "mojo/shell/public/cpp/application_runner.h" | 17 #include "mojo/shell/public/cpp/application_runner.h" |
17 #include "mojo/shell/public/cpp/interface_factory.h" | 18 #include "mojo/shell/public/cpp/interface_factory.h" |
18 #include "mojo/shell/public/cpp/shell.h" | |
19 #include "mojo/shell/public/cpp/shell_client.h" | 19 #include "mojo/shell/public/cpp/shell_client.h" |
20 #include "mojo/shell/public/interfaces/shell_client_factory.mojom.h" | 20 #include "mojo/shell/public/interfaces/shell_client_factory.mojom.h" |
21 #include "mojo/shell/tests/package_test.mojom.h" | 21 #include "mojo/shell/tests/package_test.mojom.h" |
22 | 22 |
23 // Tests that multiple applications can be packaged in a single Mojo application | 23 // Tests that multiple applications can be packaged in a single Mojo application |
24 // implementing ShellClientFactory; that these applications can be specified by | 24 // implementing ShellClientFactory; that these applications can be specified by |
25 // the package's manifest and are thus registered with the PackageManager. | 25 // the package's manifest and are thus registered with the PackageManager. |
26 | 26 |
27 namespace mojo { | 27 namespace mojo { |
28 namespace shell { | 28 namespace shell { |
29 | 29 |
30 using GetNameCallback = test::mojom::PackageTestService::GetNameCallback; | 30 using GetNameCallback = test::mojom::PackageTestService::GetNameCallback; |
31 | 31 |
32 class ProvidedShellClient | 32 class ProvidedShellClient |
33 : public ShellClient, | 33 : public ShellClient, |
34 public InterfaceFactory<test::mojom::PackageTestService>, | 34 public InterfaceFactory<test::mojom::PackageTestService>, |
35 public test::mojom::PackageTestService, | 35 public test::mojom::PackageTestService, |
36 public base::SimpleThread { | 36 public base::SimpleThread { |
37 public: | 37 public: |
38 ProvidedShellClient(const std::string& name, | 38 ProvidedShellClient(const std::string& name, |
39 mojom::ShellClientRequest request) | 39 mojom::ShellClientRequest request) |
40 : base::SimpleThread(name), | 40 : base::SimpleThread(name), |
41 name_(name), | 41 name_(name), |
42 request_(std::move(request)), | 42 request_(std::move(request)) { |
43 shell_(nullptr) { | |
44 Start(); | 43 Start(); |
45 } | 44 } |
46 ~ProvidedShellClient() override { | 45 ~ProvidedShellClient() override { |
47 Join(); | 46 Join(); |
48 } | 47 } |
49 | 48 |
50 private: | 49 private: |
51 // mojo::ShellClient: | 50 // mojo::ShellClient: |
52 void Initialize(Shell* shell, const std::string& url, | 51 void Initialize(Connector* connector, const std::string& url, |
53 uint32_t id, uint32_t user_id) override { | 52 uint32_t id, uint32_t user_id) override { |
54 shell_ = shell; | |
55 bindings_.set_connection_error_handler( | 53 bindings_.set_connection_error_handler( |
56 base::Bind(&ProvidedShellClient::OnConnectionError, | 54 base::Bind(&ProvidedShellClient::OnConnectionError, |
57 base::Unretained(this))); | 55 base::Unretained(this))); |
58 } | 56 } |
59 bool AcceptConnection(Connection* connection) override { | 57 bool AcceptConnection(Connection* connection) override { |
60 connection->AddInterface<test::mojom::PackageTestService>( | 58 connection->AddInterface<test::mojom::PackageTestService>( |
61 this); | 59 this); |
62 return true; | 60 return true; |
63 } | 61 } |
64 | 62 |
(...skipping 10 matching lines...) Expand all Loading... |
75 | 73 |
76 // base::SimpleThread: | 74 // base::SimpleThread: |
77 void Run() override { | 75 void Run() override { |
78 ApplicationRunner(this).Run(request_.PassMessagePipe().release().value(), | 76 ApplicationRunner(this).Run(request_.PassMessagePipe().release().value(), |
79 false); | 77 false); |
80 delete this; | 78 delete this; |
81 } | 79 } |
82 | 80 |
83 void OnConnectionError() { | 81 void OnConnectionError() { |
84 if (bindings_.empty()) | 82 if (bindings_.empty()) |
85 shell_->Quit(); | 83 base::MessageLoop::current()->QuitWhenIdle(); |
86 } | 84 } |
87 | 85 |
88 const std::string name_; | 86 const std::string name_; |
89 mojom::ShellClientRequest request_; | 87 mojom::ShellClientRequest request_; |
90 Shell* shell_; | |
91 BindingSet<test::mojom::PackageTestService> bindings_; | 88 BindingSet<test::mojom::PackageTestService> bindings_; |
92 | 89 |
93 DISALLOW_COPY_AND_ASSIGN(ProvidedShellClient); | 90 DISALLOW_COPY_AND_ASSIGN(ProvidedShellClient); |
94 }; | 91 }; |
95 | 92 |
96 class PackageTestShellClient | 93 class PackageTestShellClient |
97 : public ShellClient, | 94 : public ShellClient, |
98 public InterfaceFactory<mojom::ShellClientFactory>, | 95 public InterfaceFactory<mojom::ShellClientFactory>, |
99 public InterfaceFactory<test::mojom::PackageTestService>, | 96 public InterfaceFactory<test::mojom::PackageTestService>, |
100 public mojom::ShellClientFactory, | 97 public mojom::ShellClientFactory, |
101 public test::mojom::PackageTestService { | 98 public test::mojom::PackageTestService { |
102 public: | 99 public: |
103 PackageTestShellClient() : shell_(nullptr) {} | 100 PackageTestShellClient() {} |
104 ~PackageTestShellClient() override {} | 101 ~PackageTestShellClient() override {} |
105 | 102 |
106 private: | 103 private: |
107 // mojo::ShellClient: | 104 // mojo::ShellClient: |
108 void Initialize(Shell* shell, const std::string& url, | 105 void Initialize(Connector* connector, const std::string& url, |
109 uint32_t id, uint32_t user_id) override { | 106 uint32_t id, uint32_t user_id) override { |
110 shell_ = shell; | |
111 bindings_.set_connection_error_handler( | 107 bindings_.set_connection_error_handler( |
112 base::Bind(&PackageTestShellClient::OnConnectionError, | 108 base::Bind(&PackageTestShellClient::OnConnectionError, |
113 base::Unretained(this))); | 109 base::Unretained(this))); |
114 } | 110 } |
115 bool AcceptConnection(Connection* connection) override { | 111 bool AcceptConnection(Connection* connection) override { |
116 connection->AddInterface<ShellClientFactory>(this); | 112 connection->AddInterface<ShellClientFactory>(this); |
117 connection->AddInterface<test::mojom::PackageTestService>(this); | 113 connection->AddInterface<test::mojom::PackageTestService>(this); |
118 return true; | 114 return true; |
119 } | 115 } |
| 116 bool ShellConnectionLost() override { |
| 117 if (base::MessageLoop::current() && |
| 118 base::MessageLoop::current()->is_running()) { |
| 119 base::MessageLoop::current()->QuitWhenIdle(); |
| 120 } |
| 121 return true; |
| 122 } |
120 | 123 |
121 // InterfaceFactory<mojom::ShellClientFactory>: | 124 // InterfaceFactory<mojom::ShellClientFactory>: |
122 void Create(Connection* connection, | 125 void Create(Connection* connection, |
123 mojom::ShellClientFactoryRequest request) override { | 126 mojom::ShellClientFactoryRequest request) override { |
124 shell_client_factory_bindings_.AddBinding(this, std::move(request)); | 127 shell_client_factory_bindings_.AddBinding(this, std::move(request)); |
125 } | 128 } |
126 | 129 |
127 // InterfaceFactory<test::mojom::PackageTestService>: | 130 // InterfaceFactory<test::mojom::PackageTestService>: |
128 void Create(Connection* connection, | 131 void Create(Connection* connection, |
129 test::mojom::PackageTestServiceRequest request) override { | 132 test::mojom::PackageTestServiceRequest request) override { |
130 bindings_.AddBinding(this, std::move(request)); | 133 bindings_.AddBinding(this, std::move(request)); |
131 } | 134 } |
132 | 135 |
133 // mojom::ShellClientFactory: | 136 // mojom::ShellClientFactory: |
134 void CreateShellClient(mojom::ShellClientRequest request, | 137 void CreateShellClient(mojom::ShellClientRequest request, |
135 const String& url) override { | 138 const String& url) override { |
136 if (url == "mojo://package_test_a/") | 139 if (url == "mojo://package_test_a/") |
137 new ProvidedShellClient("A", std::move(request)); | 140 new ProvidedShellClient("A", std::move(request)); |
138 else if (url == "mojo://package_test_b/") | 141 else if (url == "mojo://package_test_b/") |
139 new ProvidedShellClient("B", std::move(request)); | 142 new ProvidedShellClient("B", std::move(request)); |
140 } | 143 } |
141 | 144 |
142 // test::mojom::PackageTestService: | 145 // test::mojom::PackageTestService: |
143 void GetName(const GetNameCallback& callback) override { | 146 void GetName(const GetNameCallback& callback) override { |
144 callback.Run("ROOT"); | 147 callback.Run("ROOT"); |
145 } | 148 } |
146 | 149 |
147 void OnConnectionError() { | 150 void OnConnectionError() { |
148 if (bindings_.empty()) | 151 if (bindings_.empty()) |
149 shell_->Quit(); | 152 base::MessageLoop::current()->QuitWhenIdle(); |
150 } | 153 } |
151 | 154 |
152 Shell* shell_; | |
153 std::vector<scoped_ptr<ShellClient>> delegates_; | 155 std::vector<scoped_ptr<ShellClient>> delegates_; |
154 BindingSet<mojom::ShellClientFactory> shell_client_factory_bindings_; | 156 BindingSet<mojom::ShellClientFactory> shell_client_factory_bindings_; |
155 BindingSet<test::mojom::PackageTestService> bindings_; | 157 BindingSet<test::mojom::PackageTestService> bindings_; |
156 | 158 |
157 DISALLOW_COPY_AND_ASSIGN(PackageTestShellClient); | 159 DISALLOW_COPY_AND_ASSIGN(PackageTestShellClient); |
158 }; | 160 }; |
159 | 161 |
160 } // namespace shell | 162 } // namespace shell |
161 } // namespace mojo | 163 } // namespace mojo |
162 | 164 |
163 | 165 |
164 MojoResult MojoMain(MojoHandle shell_handle) { | 166 MojoResult MojoMain(MojoHandle shell_handle) { |
165 MojoResult rv = mojo::ApplicationRunner( | 167 MojoResult rv = mojo::ApplicationRunner( |
166 new mojo::shell::PackageTestShellClient).Run(shell_handle); | 168 new mojo::shell::PackageTestShellClient).Run(shell_handle); |
167 return rv; | 169 return rv; |
168 } | 170 } |
OLD | NEW |