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 "base/bind.h" | |
6 #include "base/guid.h" | |
7 #include "base/macros.h" | |
8 #include "base/message_loop/message_loop.h" | |
9 #include "base/run_loop.h" | |
10 #include "mojo/public/c/system/main.h" | |
11 #include "mojo/public/cpp/bindings/binding_set.h" | |
12 #include "mojo/shell/public/cpp/application_runner.h" | |
13 #include "mojo/shell/public/cpp/connector.h" | |
14 #include "mojo/shell/public/cpp/interface_factory.h" | |
15 #include "mojo/shell/public/cpp/shell_client.h" | |
16 #include "mojo/shell/public/interfaces/connector.mojom.h" | |
17 #include "mojo/shell/tests/connect/connect_test.mojom.h" | |
18 | |
19 namespace mojo { | |
20 namespace shell { | |
21 namespace { | |
22 void QuitLoop(base::RunLoop* loop) { | |
23 loop->Quit(); | |
24 } | |
25 | |
26 void ReceiveString(std::string* string, base::RunLoop* loop, | |
27 const std::string& response) { | |
28 *string = response; | |
29 loop->Quit(); | |
30 } | |
31 } | |
32 | |
33 using GetTitleCallback = test::mojom::ConnectTestService::GetTitleCallback; | |
34 | |
35 class ConnectTestApp : public ShellClient, | |
36 public InterfaceFactory<test::mojom::ConnectTestService>, | |
37 public InterfaceFactory<test::mojom::StandaloneApp>, | |
38 public InterfaceFactory<test::mojom::BlockedInterface>, | |
39 public InterfaceFactory<test::mojom::UserIdTest>, | |
40 public test::mojom::ConnectTestService, | |
41 public test::mojom::StandaloneApp, | |
42 public test::mojom::BlockedInterface, | |
43 public test::mojom::UserIdTest { | |
44 public: | |
45 ConnectTestApp() {} | |
46 ~ConnectTestApp() override {} | |
47 | |
48 private: | |
49 // mojo::ShellClient: | |
50 void Initialize(Connector* connector, const Identity& identity, | |
51 uint32_t id) override { | |
52 connector_ = connector; | |
53 identity_ = identity; | |
54 id_ = id; | |
55 bindings_.set_connection_error_handler( | |
56 base::Bind(&ConnectTestApp::OnConnectionError, | |
57 base::Unretained(this))); | |
58 standalone_bindings_.set_connection_error_handler( | |
59 base::Bind(&ConnectTestApp::OnConnectionError, | |
60 base::Unretained(this))); | |
61 } | |
62 bool AcceptConnection(Connection* connection) override { | |
63 connection->AddInterface<test::mojom::ConnectTestService>(this); | |
64 connection->AddInterface<test::mojom::StandaloneApp>(this); | |
65 connection->AddInterface<test::mojom::BlockedInterface>(this); | |
66 connection->AddInterface<test::mojom::UserIdTest>(this); | |
67 | |
68 uint32_t remote_id = connection->GetRemoteInstanceID(); | |
69 test::mojom::ConnectionStatePtr state(test::mojom::ConnectionState::New()); | |
70 state->connection_local_name = connection->GetConnectionName(); | |
71 state->connection_remote_name = connection->GetRemoteIdentity().name(); | |
72 state->connection_remote_userid = connection->GetRemoteIdentity().user_id(); | |
73 state->connection_remote_id = remote_id; | |
74 state->initialize_local_name = identity_.name(); | |
75 state->initialize_id = id_; | |
76 state->initialize_userid = identity_.user_id(); | |
77 connection->GetInterface(&caller_); | |
78 caller_->ConnectionAccepted(std::move(state)); | |
79 | |
80 return true; | |
81 } | |
82 | |
83 // InterfaceFactory<test::mojom::ConnectTestService>: | |
84 void Create(Connection* connection, | |
85 test::mojom::ConnectTestServiceRequest request) override { | |
86 bindings_.AddBinding(this, std::move(request)); | |
87 } | |
88 | |
89 // InterfaceFactory<test::mojom::StandaloneApp>: | |
90 void Create(Connection* connection, | |
91 test::mojom::StandaloneAppRequest request) override { | |
92 standalone_bindings_.AddBinding(this, std::move(request)); | |
93 } | |
94 | |
95 // InterfaceFactory<test::mojom::BlockedInterface>: | |
96 void Create(Connection* connection, | |
97 test::mojom::BlockedInterfaceRequest request) override { | |
98 blocked_bindings_.AddBinding(this, std::move(request)); | |
99 } | |
100 | |
101 // InterfaceFactory<test::mojom::UserIdTest>: | |
102 void Create(Connection* connection, | |
103 test::mojom::UserIdTestRequest request) override { | |
104 user_id_test_bindings_.AddBinding(this, std::move(request)); | |
105 } | |
106 | |
107 // test::mojom::ConnectTestService: | |
108 void GetTitle(const GetTitleCallback& callback) override { | |
109 callback.Run("APP"); | |
110 } | |
111 void GetInstance(const GetInstanceCallback& callback) override { | |
112 callback.Run(identity_.instance()); | |
113 } | |
114 | |
115 // test::mojom::StandaloneApp: | |
116 void ConnectToAllowedAppInBlockedPackage( | |
117 const ConnectToAllowedAppInBlockedPackageCallback& callback) override { | |
118 base::RunLoop run_loop; | |
119 scoped_ptr<Connection> connection = | |
120 connector_->Connect("mojo:connect_test_a"); | |
121 connection->SetConnectionLostClosure( | |
122 base::Bind(&ConnectTestApp::OnConnectionBlocked, | |
123 base::Unretained(this), callback, &run_loop)); | |
124 test::mojom::ConnectTestServicePtr test_service; | |
125 connection->GetInterface(&test_service); | |
126 test_service->GetTitle( | |
127 base::Bind(&ConnectTestApp::OnGotTitle, base::Unretained(this), | |
128 callback, &run_loop)); | |
129 { | |
130 // 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. | |
132 base::MessageLoop::ScopedNestableTaskAllower allow( | |
133 base::MessageLoop::current()); | |
134 run_loop.Run(); | |
135 } | |
136 } | |
137 void ConnectToClassInterface( | |
138 const ConnectToClassInterfaceCallback& callback) override { | |
139 scoped_ptr<Connection> connection = | |
140 connector_->Connect("mojo:connect_test_class_app"); | |
141 test::mojom::ClassInterfacePtr class_interface; | |
142 connection->GetInterface(&class_interface); | |
143 std::string ping_response; | |
144 { | |
145 base::RunLoop loop; | |
146 class_interface->Ping(base::Bind(&ReceiveString, &ping_response, &loop)); | |
147 base::MessageLoop::ScopedNestableTaskAllower allow( | |
148 base::MessageLoop::current()); | |
149 loop.Run(); | |
150 } | |
151 test::mojom::ConnectTestServicePtr service; | |
152 connection->GetInterface(&service); | |
153 std::string title_response; | |
154 { | |
155 base::RunLoop loop; | |
156 service->GetTitle(base::Bind(&ReceiveString, &title_response, &loop)); | |
157 base::MessageLoop::ScopedNestableTaskAllower allow( | |
158 base::MessageLoop::current()); | |
159 loop.Run(); | |
160 } | |
161 callback.Run(ping_response, title_response); | |
162 } | |
163 | |
164 // test::mojom::BlockedInterface: | |
165 void GetTitleBlocked(const GetTitleBlockedCallback& callback) override { | |
166 callback.Run("Called Blocked Interface!"); | |
167 } | |
168 | |
169 // test::mojom::UserIdTest: | |
170 void ConnectToClassAppAsDifferentUser( | |
171 mojom::IdentityPtr target, | |
172 const ConnectToClassAppAsDifferentUserCallback& callback) override { | |
173 Connector::ConnectParams params(target.To<Identity>()); | |
174 scoped_ptr<Connection> connection = connector_->Connect(¶ms); | |
175 { | |
176 base::RunLoop loop; | |
177 connection->AddConnectionCompletedClosure(base::Bind(&QuitLoop, &loop)); | |
178 base::MessageLoop::ScopedNestableTaskAllower allow( | |
179 base::MessageLoop::current()); | |
180 loop.Run(); | |
181 } | |
182 callback.Run(static_cast<int32_t>(connection->GetResult()), | |
183 mojom::Identity::From(connection->GetRemoteIdentity())); | |
184 } | |
185 | |
186 void OnConnectionBlocked( | |
187 const ConnectToAllowedAppInBlockedPackageCallback& callback, | |
188 base::RunLoop* run_loop) { | |
189 callback.Run("uninitialized"); | |
190 run_loop->Quit(); | |
191 } | |
192 | |
193 void OnGotTitle( | |
194 const ConnectToAllowedAppInBlockedPackageCallback& callback, | |
195 base::RunLoop* run_loop, | |
196 const mojo::String& title) { | |
197 callback.Run(title); | |
198 run_loop->Quit(); | |
199 } | |
200 | |
201 void OnConnectionError() { | |
202 if (bindings_.empty() && standalone_bindings_.empty()) | |
203 base::MessageLoop::current()->QuitWhenIdle(); | |
204 } | |
205 | |
206 Connector* connector_ = nullptr; | |
207 Identity identity_; | |
208 uint32_t id_ = shell::mojom::kInvalidInstanceID; | |
209 BindingSet<test::mojom::ConnectTestService> bindings_; | |
210 BindingSet<test::mojom::StandaloneApp> standalone_bindings_; | |
211 BindingSet<test::mojom::BlockedInterface> blocked_bindings_; | |
212 BindingSet<test::mojom::UserIdTest> user_id_test_bindings_; | |
213 test::mojom::ExposedInterfacePtr caller_; | |
214 | |
215 DISALLOW_COPY_AND_ASSIGN(ConnectTestApp); | |
216 }; | |
217 | |
218 } // namespace shell | |
219 } // namespace mojo | |
220 | |
221 | |
222 MojoResult MojoMain(MojoHandle shell_handle) { | |
223 MojoResult rv = mojo::ApplicationRunner( | |
224 new mojo::shell::ConnectTestApp).Run(shell_handle); | |
225 return rv; | |
226 } | |
OLD | NEW |