| 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 <memory> |
| 8 #include <utility> | 9 #include <utility> |
| 9 | 10 |
| 10 #include "base/bind.h" | 11 #include "base/bind.h" |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 12 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 13 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 14 #include "base/threading/simple_thread.h" | 15 #include "base/threading/simple_thread.h" |
| 15 #include "mojo/public/c/system/main.h" | 16 #include "mojo/public/c/system/main.h" |
| 16 #include "mojo/public/cpp/bindings/binding_set.h" | 17 #include "mojo/public/cpp/bindings/binding_set.h" |
| 17 #include "services/shell/public/cpp/application_runner.h" | 18 #include "services/shell/public/cpp/application_runner.h" |
| 18 #include "services/shell/public/cpp/connector.h" | 19 #include "services/shell/public/cpp/connector.h" |
| 19 #include "services/shell/public/cpp/interface_factory.h" | 20 #include "services/shell/public/cpp/interface_factory.h" |
| 20 #include "services/shell/public/cpp/shell_client.h" | 21 #include "services/shell/public/cpp/shell_client.h" |
| 21 #include "services/shell/public/interfaces/shell_client_factory.mojom.h" | 22 #include "services/shell/public/interfaces/shell_client_factory.mojom.h" |
| 22 #include "services/shell/tests/connect/connect_test.mojom.h" | 23 #include "services/shell/tests/connect/connect_test.mojom.h" |
| 23 | 24 |
| 24 // Tests that multiple applications can be packaged in a single Mojo application | 25 // Tests that multiple applications can be packaged in a single Mojo application |
| 25 // implementing ShellClientFactory; that these applications can be specified by | 26 // implementing ShellClientFactory; that these applications can be specified by |
| 26 // the package's manifest and are thus registered with the PackageManager. | 27 // the package's manifest and are thus registered with the PackageManager. |
| 27 | 28 |
| 28 namespace mojo { | |
| 29 namespace shell { | 29 namespace shell { |
| 30 |
| 30 namespace { | 31 namespace { |
| 32 |
| 31 void QuitLoop(base::RunLoop* loop) { | 33 void QuitLoop(base::RunLoop* loop) { |
| 32 loop->Quit(); | 34 loop->Quit(); |
| 33 } | 35 } |
| 34 } | 36 |
| 37 } // namespace |
| 35 | 38 |
| 36 using GetTitleCallback = test::mojom::ConnectTestService::GetTitleCallback; | 39 using GetTitleCallback = test::mojom::ConnectTestService::GetTitleCallback; |
| 37 | 40 |
| 38 class ProvidedShellClient | 41 class ProvidedShellClient |
| 39 : public ShellClient, | 42 : public ShellClient, |
| 40 public InterfaceFactory<test::mojom::ConnectTestService>, | 43 public InterfaceFactory<test::mojom::ConnectTestService>, |
| 41 public InterfaceFactory<test::mojom::BlockedInterface>, | 44 public InterfaceFactory<test::mojom::BlockedInterface>, |
| 42 public InterfaceFactory<test::mojom::UserIdTest>, | 45 public InterfaceFactory<test::mojom::UserIdTest>, |
| 43 public test::mojom::ConnectTestService, | 46 public test::mojom::ConnectTestService, |
| 44 public test::mojom::BlockedInterface, | 47 public test::mojom::BlockedInterface, |
| 45 public test::mojom::UserIdTest, | 48 public test::mojom::UserIdTest, |
| 46 public base::SimpleThread { | 49 public base::SimpleThread { |
| 47 public: | 50 public: |
| 48 ProvidedShellClient(const std::string& title, | 51 ProvidedShellClient(const std::string& title, |
| 49 mojom::ShellClientRequest request) | 52 mojom::ShellClientRequest request) |
| 50 : base::SimpleThread(title), | 53 : base::SimpleThread(title), |
| 51 title_(title), | 54 title_(title), |
| 52 request_(std::move(request)) { | 55 request_(std::move(request)) { |
| 53 Start(); | 56 Start(); |
| 54 } | 57 } |
| 55 ~ProvidedShellClient() override { | 58 ~ProvidedShellClient() override { |
| 56 Join(); | 59 Join(); |
| 57 } | 60 } |
| 58 | 61 |
| 59 private: | 62 private: |
| 60 // mojo::ShellClient: | 63 // shell::ShellClient: |
| 61 void Initialize(Connector* connector, const Identity& identity, | 64 void Initialize(Connector* connector, const Identity& identity, |
| 62 uint32_t id) override { | 65 uint32_t id) override { |
| 63 connector_ = connector; | 66 connector_ = connector; |
| 64 identity_ = identity; | 67 identity_ = identity; |
| 65 id_ = id; | 68 id_ = id; |
| 66 bindings_.set_connection_error_handler( | 69 bindings_.set_connection_error_handler( |
| 67 base::Bind(&ProvidedShellClient::OnConnectionError, | 70 base::Bind(&ProvidedShellClient::OnConnectionError, |
| 68 base::Unretained(this))); | 71 base::Unretained(this))); |
| 69 } | 72 } |
| 70 bool AcceptConnection(Connection* connection) override { | 73 bool AcceptConnection(Connection* connection) override { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 // test::mojom::BlockedInterface: | 119 // test::mojom::BlockedInterface: |
| 117 void GetTitleBlocked(const GetTitleBlockedCallback& callback) override { | 120 void GetTitleBlocked(const GetTitleBlockedCallback& callback) override { |
| 118 callback.Run("Called Blocked Interface!"); | 121 callback.Run("Called Blocked Interface!"); |
| 119 } | 122 } |
| 120 | 123 |
| 121 // test::mojom::UserIdTest: | 124 // test::mojom::UserIdTest: |
| 122 void ConnectToClassAppAsDifferentUser( | 125 void ConnectToClassAppAsDifferentUser( |
| 123 mojom::IdentityPtr target, | 126 mojom::IdentityPtr target, |
| 124 const ConnectToClassAppAsDifferentUserCallback& callback) override { | 127 const ConnectToClassAppAsDifferentUserCallback& callback) override { |
| 125 Connector::ConnectParams params(target.To<Identity>()); | 128 Connector::ConnectParams params(target.To<Identity>()); |
| 126 scoped_ptr<Connection> connection = connector_->Connect(¶ms); | 129 std::unique_ptr<Connection> connection = connector_->Connect(¶ms); |
| 127 { | 130 { |
| 128 base::RunLoop loop; | 131 base::RunLoop loop; |
| 129 connection->AddConnectionCompletedClosure(base::Bind(&QuitLoop, &loop)); | 132 connection->AddConnectionCompletedClosure(base::Bind(&QuitLoop, &loop)); |
| 130 base::MessageLoop::ScopedNestableTaskAllower allow( | 133 base::MessageLoop::ScopedNestableTaskAllower allow( |
| 131 base::MessageLoop::current()); | 134 base::MessageLoop::current()); |
| 132 loop.Run(); | 135 loop.Run(); |
| 133 } | 136 } |
| 134 callback.Run(static_cast<int32_t>(connection->GetResult()), | 137 callback.Run(static_cast<int32_t>(connection->GetResult()), |
| 135 mojom::Identity::From(connection->GetRemoteIdentity())); | 138 mojom::Identity::From(connection->GetRemoteIdentity())); |
| 136 } | 139 } |
| 137 | 140 |
| 138 // base::SimpleThread: | 141 // base::SimpleThread: |
| 139 void Run() override { | 142 void Run() override { |
| 140 ApplicationRunner(this).Run(request_.PassMessagePipe().release().value(), | 143 ApplicationRunner(this).Run(request_.PassMessagePipe().release().value(), |
| 141 false); | 144 false); |
| 142 delete this; | 145 delete this; |
| 143 } | 146 } |
| 144 | 147 |
| 145 void OnConnectionError() { | 148 void OnConnectionError() { |
| 146 if (bindings_.empty()) | 149 if (bindings_.empty()) |
| 147 base::MessageLoop::current()->QuitWhenIdle(); | 150 base::MessageLoop::current()->QuitWhenIdle(); |
| 148 } | 151 } |
| 149 | 152 |
| 150 Connector* connector_ = nullptr; | 153 Connector* connector_ = nullptr; |
| 151 Identity identity_; | 154 Identity identity_; |
| 152 uint32_t id_ = shell::mojom::kInvalidInstanceID; | 155 uint32_t id_ = shell::mojom::kInvalidInstanceID; |
| 153 const std::string title_; | 156 const std::string title_; |
| 154 mojom::ShellClientRequest request_; | 157 mojom::ShellClientRequest request_; |
| 155 test::mojom::ExposedInterfacePtr caller_; | 158 test::mojom::ExposedInterfacePtr caller_; |
| 156 BindingSet<test::mojom::ConnectTestService> bindings_; | 159 mojo::BindingSet<test::mojom::ConnectTestService> bindings_; |
| 157 BindingSet<test::mojom::BlockedInterface> blocked_bindings_; | 160 mojo::BindingSet<test::mojom::BlockedInterface> blocked_bindings_; |
| 158 BindingSet<test::mojom::UserIdTest> user_id_test_bindings_; | 161 mojo::BindingSet<test::mojom::UserIdTest> user_id_test_bindings_; |
| 159 | 162 |
| 160 DISALLOW_COPY_AND_ASSIGN(ProvidedShellClient); | 163 DISALLOW_COPY_AND_ASSIGN(ProvidedShellClient); |
| 161 }; | 164 }; |
| 162 | 165 |
| 163 class ConnectTestShellClient | 166 class ConnectTestShellClient |
| 164 : public ShellClient, | 167 : public ShellClient, |
| 165 public InterfaceFactory<mojom::ShellClientFactory>, | 168 public InterfaceFactory<mojom::ShellClientFactory>, |
| 166 public InterfaceFactory<test::mojom::ConnectTestService>, | 169 public InterfaceFactory<test::mojom::ConnectTestService>, |
| 167 public mojom::ShellClientFactory, | 170 public mojom::ShellClientFactory, |
| 168 public test::mojom::ConnectTestService { | 171 public test::mojom::ConnectTestService { |
| 169 public: | 172 public: |
| 170 ConnectTestShellClient() {} | 173 ConnectTestShellClient() {} |
| 171 ~ConnectTestShellClient() override {} | 174 ~ConnectTestShellClient() override {} |
| 172 | 175 |
| 173 private: | 176 private: |
| 174 // mojo::ShellClient: | 177 // shell::ShellClient: |
| 175 void Initialize(Connector* connector, const Identity& identity, | 178 void Initialize(Connector* connector, const Identity& identity, |
| 176 uint32_t id) override { | 179 uint32_t id) override { |
| 177 identity_ = identity; | 180 identity_ = identity; |
| 178 bindings_.set_connection_error_handler( | 181 bindings_.set_connection_error_handler( |
| 179 base::Bind(&ConnectTestShellClient::OnConnectionError, | 182 base::Bind(&ConnectTestShellClient::OnConnectionError, |
| 180 base::Unretained(this))); | 183 base::Unretained(this))); |
| 181 } | 184 } |
| 182 bool AcceptConnection(Connection* connection) override { | 185 bool AcceptConnection(Connection* connection) override { |
| 183 connection->AddInterface<ShellClientFactory>(this); | 186 connection->AddInterface<ShellClientFactory>(this); |
| 184 connection->AddInterface<test::mojom::ConnectTestService>(this); | 187 connection->AddInterface<test::mojom::ConnectTestService>(this); |
| 185 return true; | 188 return true; |
| 186 } | 189 } |
| 187 | 190 |
| 188 // InterfaceFactory<mojom::ShellClientFactory>: | 191 // InterfaceFactory<mojom::ShellClientFactory>: |
| 189 void Create(Connection* connection, | 192 void Create(Connection* connection, |
| 190 mojom::ShellClientFactoryRequest request) override { | 193 mojom::ShellClientFactoryRequest request) override { |
| 191 shell_client_factory_bindings_.AddBinding(this, std::move(request)); | 194 shell_client_factory_bindings_.AddBinding(this, std::move(request)); |
| 192 } | 195 } |
| 193 | 196 |
| 194 // InterfaceFactory<test::mojom::ConnectTestService>: | 197 // InterfaceFactory<test::mojom::ConnectTestService>: |
| 195 void Create(Connection* connection, | 198 void Create(Connection* connection, |
| 196 test::mojom::ConnectTestServiceRequest request) override { | 199 test::mojom::ConnectTestServiceRequest request) override { |
| 197 bindings_.AddBinding(this, std::move(request)); | 200 bindings_.AddBinding(this, std::move(request)); |
| 198 } | 201 } |
| 199 | 202 |
| 200 // mojom::ShellClientFactory: | 203 // mojom::ShellClientFactory: |
| 201 void CreateShellClient(mojom::ShellClientRequest request, | 204 void CreateShellClient(mojom::ShellClientRequest request, |
| 202 const String& name) override { | 205 const mojo::String& name) override { |
| 203 if (name == "mojo:connect_test_a") | 206 if (name == "mojo:connect_test_a") |
| 204 new ProvidedShellClient("A", std::move(request)); | 207 new ProvidedShellClient("A", std::move(request)); |
| 205 else if (name == "mojo:connect_test_b") | 208 else if (name == "mojo:connect_test_b") |
| 206 new ProvidedShellClient("B", std::move(request)); | 209 new ProvidedShellClient("B", std::move(request)); |
| 207 } | 210 } |
| 208 | 211 |
| 209 // test::mojom::ConnectTestService: | 212 // test::mojom::ConnectTestService: |
| 210 void GetTitle(const GetTitleCallback& callback) override { | 213 void GetTitle(const GetTitleCallback& callback) override { |
| 211 callback.Run("ROOT"); | 214 callback.Run("ROOT"); |
| 212 } | 215 } |
| 213 void GetInstance(const GetInstanceCallback& callback) override { | 216 void GetInstance(const GetInstanceCallback& callback) override { |
| 214 callback.Run(identity_.instance()); | 217 callback.Run(identity_.instance()); |
| 215 } | 218 } |
| 216 | 219 |
| 217 void OnConnectionError() { | 220 void OnConnectionError() { |
| 218 if (bindings_.empty()) | 221 if (bindings_.empty()) |
| 219 base::MessageLoop::current()->QuitWhenIdle(); | 222 base::MessageLoop::current()->QuitWhenIdle(); |
| 220 } | 223 } |
| 221 | 224 |
| 222 Identity identity_; | 225 Identity identity_; |
| 223 std::vector<scoped_ptr<ShellClient>> delegates_; | 226 std::vector<std::unique_ptr<ShellClient>> delegates_; |
| 224 BindingSet<mojom::ShellClientFactory> shell_client_factory_bindings_; | 227 mojo::BindingSet<mojom::ShellClientFactory> shell_client_factory_bindings_; |
| 225 BindingSet<test::mojom::ConnectTestService> bindings_; | 228 mojo::BindingSet<test::mojom::ConnectTestService> bindings_; |
| 226 | 229 |
| 227 DISALLOW_COPY_AND_ASSIGN(ConnectTestShellClient); | 230 DISALLOW_COPY_AND_ASSIGN(ConnectTestShellClient); |
| 228 }; | 231 }; |
| 229 | 232 |
| 230 } // namespace shell | 233 } // namespace shell |
| 231 } // namespace mojo | |
| 232 | |
| 233 | 234 |
| 234 MojoResult MojoMain(MojoHandle shell_handle) { | 235 MojoResult MojoMain(MojoHandle shell_handle) { |
| 235 MojoResult rv = mojo::ApplicationRunner( | 236 MojoResult rv = shell::ApplicationRunner(new shell::ConnectTestShellClient) |
| 236 new mojo::shell::ConnectTestShellClient).Run(shell_handle); | 237 .Run(shell_handle); |
| 237 return rv; | 238 return rv; |
| 238 } | 239 } |
| OLD | NEW |