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/content_handler.mojom.h" |
| 22 |
| 23 // Tests that multiple applications can be packaged in a single Mojo application |
| 24 // implementing ContentHandler; 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 const Callback<void()>& destruct_callback) |
| 41 : base::SimpleThread(name), |
| 42 name_(name), |
| 43 request_(std::move(request)), |
| 44 destruct_callback_(destruct_callback), |
| 45 shell_(nullptr) { |
| 46 Start(); |
| 47 } |
| 48 ~ProvidedShellClient() override { |
| 49 Join(); |
| 50 destruct_callback_.Run(); |
| 51 } |
| 52 |
| 53 private: |
| 54 // mojo::ShellClient: |
| 55 void Initialize(Shell* shell, const std::string& url, uint32_t id) override { |
| 56 shell_ = shell; |
| 57 bindings_.set_connection_error_handler( |
| 58 base::Bind(&ProvidedShellClient::OnConnectionError, |
| 59 base::Unretained(this))); |
| 60 } |
| 61 bool AcceptConnection(Connection* connection) override { |
| 62 connection->AddInterface<test::mojom::PackageTestService>( |
| 63 this); |
| 64 return true; |
| 65 } |
| 66 |
| 67 // InterfaceFactory<test::mojom::PackageTestService>: |
| 68 void Create(Connection* connection, |
| 69 test::mojom::PackageTestServiceRequest request) override { |
| 70 bindings_.AddBinding(this, std::move(request)); |
| 71 } |
| 72 |
| 73 // test::mojom::PackageTestService: |
| 74 void GetName(const GetNameCallback& callback) override { |
| 75 callback.Run(name_); |
| 76 } |
| 77 |
| 78 // base::SimpleThread: |
| 79 void Run() override { |
| 80 ApplicationRunner(this).Run(request_.PassMessagePipe().release().value(), |
| 81 false); |
| 82 delete this; |
| 83 } |
| 84 |
| 85 void OnConnectionError() { |
| 86 if (bindings_.empty()) |
| 87 shell_->Quit(); |
| 88 } |
| 89 |
| 90 const std::string name_; |
| 91 mojom::ShellClientRequest request_; |
| 92 const Callback<void()> destruct_callback_; |
| 93 Shell* shell_; |
| 94 WeakBindingSet<test::mojom::PackageTestService> bindings_; |
| 95 |
| 96 DISALLOW_COPY_AND_ASSIGN(ProvidedShellClient); |
| 97 }; |
| 98 |
| 99 class PackageTestShellClient |
| 100 : public ShellClient, |
| 101 public InterfaceFactory<mojom::ContentHandler>, |
| 102 public InterfaceFactory<test::mojom::PackageTestService>, |
| 103 public mojom::ContentHandler, |
| 104 public test::mojom::PackageTestService { |
| 105 public: |
| 106 PackageTestShellClient() : shell_(nullptr) {} |
| 107 ~PackageTestShellClient() override {} |
| 108 |
| 109 private: |
| 110 // mojo::ShellClient: |
| 111 void Initialize(Shell* shell, const std::string& url, uint32_t id) override { |
| 112 shell_ = shell; |
| 113 bindings_.set_connection_error_handler( |
| 114 base::Bind(&PackageTestShellClient::OnConnectionError, |
| 115 base::Unretained(this))); |
| 116 } |
| 117 bool AcceptConnection(Connection* connection) override { |
| 118 connection->AddInterface<ContentHandler>(this); |
| 119 connection->AddInterface<test::mojom::PackageTestService>( |
| 120 this); |
| 121 return true; |
| 122 } |
| 123 |
| 124 // InterfaceFactory<mojom::ContentHandler>: |
| 125 void Create(Connection* connection, |
| 126 mojom::ContentHandlerRequest request) override { |
| 127 content_handler_bindings_.AddBinding(this, std::move(request)); |
| 128 } |
| 129 |
| 130 // InterfaceFactory<test::mojom::PackageTestService>: |
| 131 void Create(Connection* connection, |
| 132 test::mojom::PackageTestServiceRequest request) override { |
| 133 bindings_.AddBinding(this, std::move(request)); |
| 134 } |
| 135 |
| 136 // mojom::ContentHandler: |
| 137 void StartApplication(mojom::ShellClientRequest request, |
| 138 URLResponsePtr response, |
| 139 const Callback<void()>& destruct_callback) override { |
| 140 const std::string url = response->url; |
| 141 if (url == "mojo://package_test_a/") |
| 142 new ProvidedShellClient("A", std::move(request), destruct_callback); |
| 143 else if (url == "mojo://package_test_b/") |
| 144 new ProvidedShellClient("B", std::move(request), destruct_callback); |
| 145 } |
| 146 |
| 147 // test::mojom::PackageTestService: |
| 148 void GetName(const GetNameCallback& callback) override { |
| 149 callback.Run("ROOT"); |
| 150 } |
| 151 |
| 152 void OnConnectionError() { |
| 153 if (bindings_.empty()) |
| 154 shell_->Quit(); |
| 155 } |
| 156 |
| 157 Shell* shell_; |
| 158 std::vector<scoped_ptr<ShellClient>> delegates_; |
| 159 WeakBindingSet<mojom::ContentHandler> content_handler_bindings_; |
| 160 WeakBindingSet<test::mojom::PackageTestService> bindings_; |
| 161 |
| 162 DISALLOW_COPY_AND_ASSIGN(PackageTestShellClient); |
| 163 }; |
| 164 |
| 165 } // namespace shell |
| 166 } // namespace mojo |
| 167 |
| 168 |
| 169 MojoResult MojoMain(MojoHandle shell_handle) { |
| 170 MojoResult rv = mojo::ApplicationRunner( |
| 171 new mojo::shell::PackageTestShellClient).Run(shell_handle); |
| 172 return rv; |
| 173 } |
OLD | NEW |