| 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 <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/threading/simple_thread.h" | 13 #include "base/threading/simple_thread.h" |
| 14 #include "mojo/public/c/system/main.h" | 14 #include "mojo/public/c/system/main.h" |
| 15 #include "mojo/public/cpp/bindings/weak_binding_set.h" | 15 #include "mojo/public/cpp/bindings/weak_binding_set.h" |
| 16 #include "mojo/shell/package_test.mojom.h" | 16 #include "mojo/shell/package_test.mojom.h" |
| 17 #include "mojo/shell/public/cpp/application_runner.h" | 17 #include "mojo/shell/public/cpp/application_runner.h" |
| 18 #include "mojo/shell/public/cpp/interface_factory.h" | 18 #include "mojo/shell/public/cpp/interface_factory.h" |
| 19 #include "mojo/shell/public/cpp/shell.h" | 19 #include "mojo/shell/public/cpp/shell.h" |
| 20 #include "mojo/shell/public/cpp/shell_client.h" | 20 #include "mojo/shell/public/cpp/shell_client.h" |
| 21 #include "mojo/shell/public/interfaces/content_handler.mojom.h" | 21 #include "mojo/shell/public/interfaces/shell_client_factory.mojom.h" |
| 22 | 22 |
| 23 // Tests that multiple applications can be packaged in a single Mojo application | 23 // Tests that multiple applications can be packaged in a single Mojo application |
| 24 // implementing ContentHandler; that these applications can be specified by | 24 // implementing ShellClientFactory; that these applications can be specified by |
| 25 // the package's manifest and are thus registered with the PackageManager. | 25 // the package's manifest and are thus registered with the PackageManager. |
| 26 | 26 |
| 27 namespace mojo { | 27 namespace mojo { |
| 28 namespace shell { | 28 namespace shell { |
| 29 | 29 |
| 30 using GetNameCallback = test::mojom::PackageTestService::GetNameCallback; | 30 using GetNameCallback = test::mojom::PackageTestService::GetNameCallback; |
| 31 | 31 |
| 32 class ProvidedShellClient | 32 class ProvidedShellClient |
| 33 : public ShellClient, | 33 : public ShellClient, |
| 34 public InterfaceFactory<test::mojom::PackageTestService>, | 34 public InterfaceFactory<test::mojom::PackageTestService>, |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 mojom::ShellClientRequest request_; | 91 mojom::ShellClientRequest request_; |
| 92 const Callback<void()> destruct_callback_; | 92 const Callback<void()> destruct_callback_; |
| 93 Shell* shell_; | 93 Shell* shell_; |
| 94 WeakBindingSet<test::mojom::PackageTestService> bindings_; | 94 WeakBindingSet<test::mojom::PackageTestService> bindings_; |
| 95 | 95 |
| 96 DISALLOW_COPY_AND_ASSIGN(ProvidedShellClient); | 96 DISALLOW_COPY_AND_ASSIGN(ProvidedShellClient); |
| 97 }; | 97 }; |
| 98 | 98 |
| 99 class PackageTestShellClient | 99 class PackageTestShellClient |
| 100 : public ShellClient, | 100 : public ShellClient, |
| 101 public InterfaceFactory<mojom::ContentHandler>, | 101 public InterfaceFactory<mojom::ShellClientFactory>, |
| 102 public InterfaceFactory<test::mojom::PackageTestService>, | 102 public InterfaceFactory<test::mojom::PackageTestService>, |
| 103 public mojom::ContentHandler, | 103 public mojom::ShellClientFactory, |
| 104 public test::mojom::PackageTestService { | 104 public test::mojom::PackageTestService { |
| 105 public: | 105 public: |
| 106 PackageTestShellClient() : shell_(nullptr) {} | 106 PackageTestShellClient() : shell_(nullptr) {} |
| 107 ~PackageTestShellClient() override {} | 107 ~PackageTestShellClient() override {} |
| 108 | 108 |
| 109 private: | 109 private: |
| 110 // mojo::ShellClient: | 110 // mojo::ShellClient: |
| 111 void Initialize(Shell* shell, const std::string& url, uint32_t id) override { | 111 void Initialize(Shell* shell, const std::string& url, uint32_t id) override { |
| 112 shell_ = shell; | 112 shell_ = shell; |
| 113 bindings_.set_connection_error_handler( | 113 bindings_.set_connection_error_handler( |
| 114 base::Bind(&PackageTestShellClient::OnConnectionError, | 114 base::Bind(&PackageTestShellClient::OnConnectionError, |
| 115 base::Unretained(this))); | 115 base::Unretained(this))); |
| 116 } | 116 } |
| 117 bool AcceptConnection(Connection* connection) override { | 117 bool AcceptConnection(Connection* connection) override { |
| 118 connection->AddInterface<ContentHandler>(this); | 118 connection->AddInterface<ShellClientFactory>(this); |
| 119 connection->AddInterface<test::mojom::PackageTestService>( | 119 connection->AddInterface<test::mojom::PackageTestService>(this); |
| 120 this); | |
| 121 return true; | 120 return true; |
| 122 } | 121 } |
| 123 | 122 |
| 124 // InterfaceFactory<mojom::ContentHandler>: | 123 // InterfaceFactory<mojom::ShellClientFactory>: |
| 125 void Create(Connection* connection, | 124 void Create(Connection* connection, |
| 126 mojom::ContentHandlerRequest request) override { | 125 mojom::ShellClientFactoryRequest request) override { |
| 127 content_handler_bindings_.AddBinding(this, std::move(request)); | 126 shell_client_factory_bindings_.AddBinding(this, std::move(request)); |
| 128 } | 127 } |
| 129 | 128 |
| 130 // InterfaceFactory<test::mojom::PackageTestService>: | 129 // InterfaceFactory<test::mojom::PackageTestService>: |
| 131 void Create(Connection* connection, | 130 void Create(Connection* connection, |
| 132 test::mojom::PackageTestServiceRequest request) override { | 131 test::mojom::PackageTestServiceRequest request) override { |
| 133 bindings_.AddBinding(this, std::move(request)); | 132 bindings_.AddBinding(this, std::move(request)); |
| 134 } | 133 } |
| 135 | 134 |
| 136 // mojom::ContentHandler: | 135 // mojom::ShellClientFactory: |
| 137 void StartApplication(mojom::ShellClientRequest request, | 136 void CreateShellClient(mojom::ShellClientRequest request, |
| 138 URLResponsePtr response, | 137 const String& url, |
| 139 const Callback<void()>& destruct_callback) override { | 138 const Callback<void()>& destruct_callback) override { |
| 140 const std::string url = response->url; | |
| 141 if (url == "mojo://package_test_a/") | 139 if (url == "mojo://package_test_a/") |
| 142 new ProvidedShellClient("A", std::move(request), destruct_callback); | 140 new ProvidedShellClient("A", std::move(request), destruct_callback); |
| 143 else if (url == "mojo://package_test_b/") | 141 else if (url == "mojo://package_test_b/") |
| 144 new ProvidedShellClient("B", std::move(request), destruct_callback); | 142 new ProvidedShellClient("B", std::move(request), destruct_callback); |
| 145 } | 143 } |
| 146 | 144 |
| 147 // test::mojom::PackageTestService: | 145 // test::mojom::PackageTestService: |
| 148 void GetName(const GetNameCallback& callback) override { | 146 void GetName(const GetNameCallback& callback) override { |
| 149 callback.Run("ROOT"); | 147 callback.Run("ROOT"); |
| 150 } | 148 } |
| 151 | 149 |
| 152 void OnConnectionError() { | 150 void OnConnectionError() { |
| 153 if (bindings_.empty()) | 151 if (bindings_.empty()) |
| 154 shell_->Quit(); | 152 shell_->Quit(); |
| 155 } | 153 } |
| 156 | 154 |
| 157 Shell* shell_; | 155 Shell* shell_; |
| 158 std::vector<scoped_ptr<ShellClient>> delegates_; | 156 std::vector<scoped_ptr<ShellClient>> delegates_; |
| 159 WeakBindingSet<mojom::ContentHandler> content_handler_bindings_; | 157 WeakBindingSet<mojom::ShellClientFactory> shell_client_factory_bindings_; |
| 160 WeakBindingSet<test::mojom::PackageTestService> bindings_; | 158 WeakBindingSet<test::mojom::PackageTestService> bindings_; |
| 161 | 159 |
| 162 DISALLOW_COPY_AND_ASSIGN(PackageTestShellClient); | 160 DISALLOW_COPY_AND_ASSIGN(PackageTestShellClient); |
| 163 }; | 161 }; |
| 164 | 162 |
| 165 } // namespace shell | 163 } // namespace shell |
| 166 } // namespace mojo | 164 } // namespace mojo |
| 167 | 165 |
| 168 | 166 |
| 169 MojoResult MojoMain(MojoHandle shell_handle) { | 167 MojoResult MojoMain(MojoHandle shell_handle) { |
| 170 MojoResult rv = mojo::ApplicationRunner( | 168 MojoResult rv = mojo::ApplicationRunner( |
| 171 new mojo::shell::PackageTestShellClient).Run(shell_handle); | 169 new mojo::shell::PackageTestShellClient).Run(shell_handle); |
| 172 return rv; | 170 return rv; |
| 173 } | 171 } |
| OLD | NEW |