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 <memory> |
| 6 |
5 #include "base/bind.h" | 7 #include "base/bind.h" |
6 #include "base/guid.h" | 8 #include "base/guid.h" |
7 #include "base/macros.h" | 9 #include "base/macros.h" |
8 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
9 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
10 #include "mojo/public/c/system/main.h" | 12 #include "mojo/public/c/system/main.h" |
11 #include "mojo/public/cpp/bindings/binding_set.h" | 13 #include "mojo/public/cpp/bindings/binding_set.h" |
12 #include "services/shell/public/cpp/application_runner.h" | 14 #include "services/shell/public/cpp/application_runner.h" |
13 #include "services/shell/public/cpp/connector.h" | 15 #include "services/shell/public/cpp/connector.h" |
14 #include "services/shell/public/cpp/interface_factory.h" | 16 #include "services/shell/public/cpp/interface_factory.h" |
15 #include "services/shell/public/cpp/shell_client.h" | 17 #include "services/shell/public/cpp/shell_client.h" |
16 #include "services/shell/public/interfaces/connector.mojom.h" | 18 #include "services/shell/public/interfaces/connector.mojom.h" |
17 #include "services/shell/tests/connect/connect_test.mojom.h" | 19 #include "services/shell/tests/connect/connect_test.mojom.h" |
18 | 20 |
19 namespace mojo { | |
20 namespace shell { | 21 namespace shell { |
| 22 |
21 namespace { | 23 namespace { |
| 24 |
22 void QuitLoop(base::RunLoop* loop) { | 25 void QuitLoop(base::RunLoop* loop) { |
23 loop->Quit(); | 26 loop->Quit(); |
24 } | 27 } |
25 | 28 |
26 void ReceiveString(std::string* string, base::RunLoop* loop, | 29 void ReceiveString(std::string* string, base::RunLoop* loop, |
27 const std::string& response) { | 30 const std::string& response) { |
28 *string = response; | 31 *string = response; |
29 loop->Quit(); | 32 loop->Quit(); |
30 } | 33 } |
31 } | 34 |
| 35 } // namespace |
32 | 36 |
33 using GetTitleCallback = test::mojom::ConnectTestService::GetTitleCallback; | 37 using GetTitleCallback = test::mojom::ConnectTestService::GetTitleCallback; |
34 | 38 |
35 class ConnectTestApp : public ShellClient, | 39 class ConnectTestApp : public ShellClient, |
36 public InterfaceFactory<test::mojom::ConnectTestService>, | 40 public InterfaceFactory<test::mojom::ConnectTestService>, |
37 public InterfaceFactory<test::mojom::StandaloneApp>, | 41 public InterfaceFactory<test::mojom::StandaloneApp>, |
38 public InterfaceFactory<test::mojom::BlockedInterface>, | 42 public InterfaceFactory<test::mojom::BlockedInterface>, |
39 public InterfaceFactory<test::mojom::UserIdTest>, | 43 public InterfaceFactory<test::mojom::UserIdTest>, |
40 public test::mojom::ConnectTestService, | 44 public test::mojom::ConnectTestService, |
41 public test::mojom::StandaloneApp, | 45 public test::mojom::StandaloneApp, |
42 public test::mojom::BlockedInterface, | 46 public test::mojom::BlockedInterface, |
43 public test::mojom::UserIdTest { | 47 public test::mojom::UserIdTest { |
44 public: | 48 public: |
45 ConnectTestApp() {} | 49 ConnectTestApp() {} |
46 ~ConnectTestApp() override {} | 50 ~ConnectTestApp() override {} |
47 | 51 |
48 private: | 52 private: |
49 // mojo::ShellClient: | 53 // shell::ShellClient: |
50 void Initialize(Connector* connector, const Identity& identity, | 54 void Initialize(Connector* connector, const Identity& identity, |
51 uint32_t id) override { | 55 uint32_t id) override { |
52 connector_ = connector; | 56 connector_ = connector; |
53 identity_ = identity; | 57 identity_ = identity; |
54 id_ = id; | 58 id_ = id; |
55 bindings_.set_connection_error_handler( | 59 bindings_.set_connection_error_handler( |
56 base::Bind(&ConnectTestApp::OnConnectionError, | 60 base::Bind(&ConnectTestApp::OnConnectionError, |
57 base::Unretained(this))); | 61 base::Unretained(this))); |
58 standalone_bindings_.set_connection_error_handler( | 62 standalone_bindings_.set_connection_error_handler( |
59 base::Bind(&ConnectTestApp::OnConnectionError, | 63 base::Bind(&ConnectTestApp::OnConnectionError, |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 callback.Run("APP"); | 113 callback.Run("APP"); |
110 } | 114 } |
111 void GetInstance(const GetInstanceCallback& callback) override { | 115 void GetInstance(const GetInstanceCallback& callback) override { |
112 callback.Run(identity_.instance()); | 116 callback.Run(identity_.instance()); |
113 } | 117 } |
114 | 118 |
115 // test::mojom::StandaloneApp: | 119 // test::mojom::StandaloneApp: |
116 void ConnectToAllowedAppInBlockedPackage( | 120 void ConnectToAllowedAppInBlockedPackage( |
117 const ConnectToAllowedAppInBlockedPackageCallback& callback) override { | 121 const ConnectToAllowedAppInBlockedPackageCallback& callback) override { |
118 base::RunLoop run_loop; | 122 base::RunLoop run_loop; |
119 scoped_ptr<Connection> connection = | 123 std::unique_ptr<Connection> connection = |
120 connector_->Connect("mojo:connect_test_a"); | 124 connector_->Connect("mojo:connect_test_a"); |
121 connection->SetConnectionLostClosure( | 125 connection->SetConnectionLostClosure( |
122 base::Bind(&ConnectTestApp::OnConnectionBlocked, | 126 base::Bind(&ConnectTestApp::OnConnectionBlocked, |
123 base::Unretained(this), callback, &run_loop)); | 127 base::Unretained(this), callback, &run_loop)); |
124 test::mojom::ConnectTestServicePtr test_service; | 128 test::mojom::ConnectTestServicePtr test_service; |
125 connection->GetInterface(&test_service); | 129 connection->GetInterface(&test_service); |
126 test_service->GetTitle( | 130 test_service->GetTitle( |
127 base::Bind(&ConnectTestApp::OnGotTitle, base::Unretained(this), | 131 base::Bind(&ConnectTestApp::OnGotTitle, base::Unretained(this), |
128 callback, &run_loop)); | 132 callback, &run_loop)); |
129 { | 133 { |
130 // This message is dispatched as a task on the same run loop, so we need | 134 // This message is dispatched as a task on the same run loop, so we need |
131 // to allow nesting in order to pump additional signals. | 135 // to allow nesting in order to pump additional signals. |
132 base::MessageLoop::ScopedNestableTaskAllower allow( | 136 base::MessageLoop::ScopedNestableTaskAllower allow( |
133 base::MessageLoop::current()); | 137 base::MessageLoop::current()); |
134 run_loop.Run(); | 138 run_loop.Run(); |
135 } | 139 } |
136 } | 140 } |
137 void ConnectToClassInterface( | 141 void ConnectToClassInterface( |
138 const ConnectToClassInterfaceCallback& callback) override { | 142 const ConnectToClassInterfaceCallback& callback) override { |
139 scoped_ptr<Connection> connection = | 143 std::unique_ptr<Connection> connection = |
140 connector_->Connect("mojo:connect_test_class_app"); | 144 connector_->Connect("mojo:connect_test_class_app"); |
141 test::mojom::ClassInterfacePtr class_interface; | 145 test::mojom::ClassInterfacePtr class_interface; |
142 connection->GetInterface(&class_interface); | 146 connection->GetInterface(&class_interface); |
143 std::string ping_response; | 147 std::string ping_response; |
144 { | 148 { |
145 base::RunLoop loop; | 149 base::RunLoop loop; |
146 class_interface->Ping(base::Bind(&ReceiveString, &ping_response, &loop)); | 150 class_interface->Ping(base::Bind(&ReceiveString, &ping_response, &loop)); |
147 base::MessageLoop::ScopedNestableTaskAllower allow( | 151 base::MessageLoop::ScopedNestableTaskAllower allow( |
148 base::MessageLoop::current()); | 152 base::MessageLoop::current()); |
149 loop.Run(); | 153 loop.Run(); |
(...skipping 14 matching lines...) Expand all Loading... |
164 // test::mojom::BlockedInterface: | 168 // test::mojom::BlockedInterface: |
165 void GetTitleBlocked(const GetTitleBlockedCallback& callback) override { | 169 void GetTitleBlocked(const GetTitleBlockedCallback& callback) override { |
166 callback.Run("Called Blocked Interface!"); | 170 callback.Run("Called Blocked Interface!"); |
167 } | 171 } |
168 | 172 |
169 // test::mojom::UserIdTest: | 173 // test::mojom::UserIdTest: |
170 void ConnectToClassAppAsDifferentUser( | 174 void ConnectToClassAppAsDifferentUser( |
171 mojom::IdentityPtr target, | 175 mojom::IdentityPtr target, |
172 const ConnectToClassAppAsDifferentUserCallback& callback) override { | 176 const ConnectToClassAppAsDifferentUserCallback& callback) override { |
173 Connector::ConnectParams params(target.To<Identity>()); | 177 Connector::ConnectParams params(target.To<Identity>()); |
174 scoped_ptr<Connection> connection = connector_->Connect(¶ms); | 178 std::unique_ptr<Connection> connection = connector_->Connect(¶ms); |
175 { | 179 { |
176 base::RunLoop loop; | 180 base::RunLoop loop; |
177 connection->AddConnectionCompletedClosure(base::Bind(&QuitLoop, &loop)); | 181 connection->AddConnectionCompletedClosure(base::Bind(&QuitLoop, &loop)); |
178 base::MessageLoop::ScopedNestableTaskAllower allow( | 182 base::MessageLoop::ScopedNestableTaskAllower allow( |
179 base::MessageLoop::current()); | 183 base::MessageLoop::current()); |
180 loop.Run(); | 184 loop.Run(); |
181 } | 185 } |
182 callback.Run(static_cast<int32_t>(connection->GetResult()), | 186 callback.Run(static_cast<int32_t>(connection->GetResult()), |
183 mojom::Identity::From(connection->GetRemoteIdentity())); | 187 mojom::Identity::From(connection->GetRemoteIdentity())); |
184 } | 188 } |
(...skipping 13 matching lines...) Expand all Loading... |
198 run_loop->Quit(); | 202 run_loop->Quit(); |
199 } | 203 } |
200 | 204 |
201 void OnConnectionError() { | 205 void OnConnectionError() { |
202 if (bindings_.empty() && standalone_bindings_.empty()) | 206 if (bindings_.empty() && standalone_bindings_.empty()) |
203 base::MessageLoop::current()->QuitWhenIdle(); | 207 base::MessageLoop::current()->QuitWhenIdle(); |
204 } | 208 } |
205 | 209 |
206 Connector* connector_ = nullptr; | 210 Connector* connector_ = nullptr; |
207 Identity identity_; | 211 Identity identity_; |
208 uint32_t id_ = shell::mojom::kInvalidInstanceID; | 212 uint32_t id_ = mojom::kInvalidInstanceID; |
209 BindingSet<test::mojom::ConnectTestService> bindings_; | 213 mojo::BindingSet<test::mojom::ConnectTestService> bindings_; |
210 BindingSet<test::mojom::StandaloneApp> standalone_bindings_; | 214 mojo::BindingSet<test::mojom::StandaloneApp> standalone_bindings_; |
211 BindingSet<test::mojom::BlockedInterface> blocked_bindings_; | 215 mojo::BindingSet<test::mojom::BlockedInterface> blocked_bindings_; |
212 BindingSet<test::mojom::UserIdTest> user_id_test_bindings_; | 216 mojo::BindingSet<test::mojom::UserIdTest> user_id_test_bindings_; |
213 test::mojom::ExposedInterfacePtr caller_; | 217 test::mojom::ExposedInterfacePtr caller_; |
214 | 218 |
215 DISALLOW_COPY_AND_ASSIGN(ConnectTestApp); | 219 DISALLOW_COPY_AND_ASSIGN(ConnectTestApp); |
216 }; | 220 }; |
217 | 221 |
218 } // namespace shell | 222 } // namespace shell |
219 } // namespace mojo | |
220 | |
221 | 223 |
222 MojoResult MojoMain(MojoHandle shell_handle) { | 224 MojoResult MojoMain(MojoHandle shell_handle) { |
223 MojoResult rv = mojo::ApplicationRunner( | 225 MojoResult rv = |
224 new mojo::shell::ConnectTestApp).Run(shell_handle); | 226 shell::ApplicationRunner(new shell::ConnectTestApp).Run(shell_handle); |
225 return rv; | 227 return rv; |
226 } | 228 } |
OLD | NEW |