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