| 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/macros.h" | |
| 7 #include "base/message_loop/message_loop.h" | |
| 8 #include "base/run_loop.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/connector.h" | |
| 13 #include "mojo/shell/public/cpp/interface_factory.h" | |
| 14 #include "mojo/shell/public/cpp/shell_client.h" | |
| 15 #include "mojo/shell/public/interfaces/connector.mojom.h" | |
| 16 #include "mojo/shell/tests/connect/connect_test.mojom.h" | |
| 17 | |
| 18 namespace mojo { | |
| 19 namespace shell { | |
| 20 | |
| 21 using GetTitleCallback = test::mojom::ConnectTestService::GetTitleCallback; | |
| 22 | |
| 23 class ConnectTestClassApp | |
| 24 : public ShellClient, | |
| 25 public InterfaceFactory<test::mojom::ConnectTestService>, | |
| 26 public InterfaceFactory<test::mojom::ClassInterface>, | |
| 27 public test::mojom::ConnectTestService, | |
| 28 public test::mojom::ClassInterface { | |
| 29 public: | |
| 30 ConnectTestClassApp() {} | |
| 31 ~ConnectTestClassApp() override {} | |
| 32 | |
| 33 private: | |
| 34 // mojo::ShellClient: | |
| 35 void Initialize(Connector* connector, const Identity& identity, | |
| 36 uint32_t id) override { | |
| 37 connector_ = connector; | |
| 38 identity_ = identity; | |
| 39 } | |
| 40 bool AcceptConnection(Connection* connection) override { | |
| 41 CHECK(connection->HasCapabilityClass("class")); | |
| 42 connection->AddInterface<test::mojom::ConnectTestService>(this); | |
| 43 connection->AddInterface<test::mojom::ClassInterface>(this); | |
| 44 inbound_connections_.insert(connection); | |
| 45 connection->SetConnectionLostClosure( | |
| 46 base::Bind(&ConnectTestClassApp::OnConnectionError, | |
| 47 base::Unretained(this), connection)); | |
| 48 return true; | |
| 49 } | |
| 50 | |
| 51 // InterfaceFactory<test::mojom::ConnectTestService>: | |
| 52 void Create(Connection* connection, | |
| 53 test::mojom::ConnectTestServiceRequest request) override { | |
| 54 bindings_.AddBinding(this, std::move(request)); | |
| 55 } | |
| 56 | |
| 57 // InterfaceFactory<test::mojom::ClassInterface>: | |
| 58 void Create(Connection* connection, | |
| 59 test::mojom::ClassInterfaceRequest request) override { | |
| 60 class_interface_bindings_.AddBinding(this, std::move(request)); | |
| 61 } | |
| 62 | |
| 63 // test::mojom::ConnectTestService: | |
| 64 void GetTitle(const GetTitleCallback& callback) override { | |
| 65 callback.Run("CLASS APP"); | |
| 66 } | |
| 67 void GetInstance(const GetInstanceCallback& callback) override { | |
| 68 callback.Run(identity_.instance()); | |
| 69 } | |
| 70 | |
| 71 // test::mojom::ClassInterface: | |
| 72 void Ping(const PingCallback& callback) override { | |
| 73 callback.Run("PONG"); | |
| 74 } | |
| 75 | |
| 76 void OnConnectionError(Connection* connection) { | |
| 77 auto it = inbound_connections_.find(connection); | |
| 78 DCHECK(it != inbound_connections_.end()); | |
| 79 inbound_connections_.erase(it); | |
| 80 if (inbound_connections_.empty()) | |
| 81 base::MessageLoop::current()->QuitWhenIdle(); | |
| 82 } | |
| 83 | |
| 84 Connector* connector_ = nullptr; | |
| 85 Identity identity_; | |
| 86 std::set<Connection*> inbound_connections_; | |
| 87 BindingSet<test::mojom::ConnectTestService> bindings_; | |
| 88 BindingSet<test::mojom::ClassInterface> class_interface_bindings_; | |
| 89 | |
| 90 DISALLOW_COPY_AND_ASSIGN(ConnectTestClassApp); | |
| 91 }; | |
| 92 | |
| 93 } // namespace shell | |
| 94 } // namespace mojo | |
| 95 | |
| 96 | |
| 97 MojoResult MojoMain(MojoHandle shell_handle) { | |
| 98 MojoResult rv = mojo::ApplicationRunner( | |
| 99 new mojo::shell::ConnectTestClassApp).Run(shell_handle); | |
| 100 return rv; | |
| 101 } | |
| OLD | NEW |