| 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 <algorithm> | 5 #include <algorithm> |
| 6 #include <memory> |
| 6 | 7 |
| 7 #include "base/bind.h" | 8 #include "base/bind.h" |
| 8 #include "base/macros.h" | 9 #include "base/macros.h" |
| 9 #include "mojo/public/c/system/main.h" | 10 #include "mojo/public/c/system/main.h" |
| 10 #include "mojo/public/cpp/bindings/binding_set.h" | 11 #include "mojo/public/cpp/bindings/binding_set.h" |
| 11 #include "services/shell/public/cpp/application_runner.h" | 12 #include "services/shell/public/cpp/application_runner.h" |
| 12 #include "services/shell/public/cpp/shell_connection.h" | 13 #include "services/shell/public/cpp/shell_connection.h" |
| 13 #include "services/shell/public/interfaces/shell_client_factory.mojom.h" | 14 #include "services/shell/public/interfaces/shell_client_factory.mojom.h" |
| 14 #include "services/shell/tests/lifecycle/app_client.h" | 15 #include "services/shell/tests/lifecycle/app_client.h" |
| 15 #include "services/shell/tests/lifecycle/lifecycle_unittest.mojom.h" | 16 #include "services/shell/tests/lifecycle/lifecycle_unittest.mojom.h" |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 class PackagedApp : public mojo::ShellClient, | 20 class PackagedApp : public shell::ShellClient, |
| 20 public mojo::InterfaceFactory<LifecycleControl>, | 21 public shell::InterfaceFactory<LifecycleControl>, |
| 21 public LifecycleControl { | 22 public LifecycleControl { |
| 22 public: | 23 public: |
| 23 using DestructCallback = base::Callback<void(PackagedApp*)>; | 24 using DestructCallback = base::Callback<void(PackagedApp*)>; |
| 24 | 25 |
| 25 PackagedApp(mojo::shell::mojom::ShellClientRequest request, | 26 PackagedApp(shell::mojom::ShellClientRequest request, |
| 26 const DestructCallback& shell_connection_closed_callback, | 27 const DestructCallback& shell_connection_closed_callback, |
| 27 const DestructCallback& destruct_callback) | 28 const DestructCallback& destruct_callback) |
| 28 : connection_(new mojo::ShellConnection(this, std::move(request))), | 29 : connection_(new shell::ShellConnection(this, std::move(request))), |
| 29 shell_connection_closed_callback_(shell_connection_closed_callback), | 30 shell_connection_closed_callback_(shell_connection_closed_callback), |
| 30 destruct_callback_(destruct_callback) { | 31 destruct_callback_(destruct_callback) { |
| 31 bindings_.set_connection_error_handler(base::Bind(&PackagedApp::BindingLost, | 32 bindings_.set_connection_error_handler(base::Bind(&PackagedApp::BindingLost, |
| 32 base::Unretained(this))); | 33 base::Unretained(this))); |
| 33 } | 34 } |
| 34 ~PackagedApp() override { | 35 ~PackagedApp() override { |
| 35 destruct_callback_.Run(this); | 36 destruct_callback_.Run(this); |
| 36 } | 37 } |
| 37 | 38 |
| 38 private: | 39 private: |
| 39 // mojo::ShellClient: | 40 // shell::ShellClient: |
| 40 bool AcceptConnection(mojo::Connection* connection) override { | 41 bool AcceptConnection(shell::Connection* connection) override { |
| 41 connection->AddInterface<LifecycleControl>(this); | 42 connection->AddInterface<LifecycleControl>(this); |
| 42 return true; | 43 return true; |
| 43 } | 44 } |
| 44 | 45 |
| 45 // mojo::InterfaceFactory<LifecycleControl> | 46 // shell::InterfaceFactory<LifecycleControl> |
| 46 void Create(mojo::Connection* connection, | 47 void Create(shell::Connection* connection, |
| 47 LifecycleControlRequest request) override { | 48 LifecycleControlRequest request) override { |
| 48 bindings_.AddBinding(this, std::move(request)); | 49 bindings_.AddBinding(this, std::move(request)); |
| 49 } | 50 } |
| 50 | 51 |
| 51 // LifecycleControl: | 52 // LifecycleControl: |
| 52 void Ping(const PingCallback& callback) override { | 53 void Ping(const PingCallback& callback) override { |
| 53 callback.Run(); | 54 callback.Run(); |
| 54 } | 55 } |
| 55 void GracefulQuit() override { | 56 void GracefulQuit() override { |
| 56 shell_connection_closed_callback_.Run(this); | 57 shell_connection_closed_callback_.Run(this); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 67 connection_.reset(); | 68 connection_.reset(); |
| 68 // This only closed our relationship with the shell, existing |bindings_| | 69 // This only closed our relationship with the shell, existing |bindings_| |
| 69 // remain active. | 70 // remain active. |
| 70 } | 71 } |
| 71 | 72 |
| 72 void BindingLost() { | 73 void BindingLost() { |
| 73 if (bindings_.empty()) | 74 if (bindings_.empty()) |
| 74 delete this; | 75 delete this; |
| 75 } | 76 } |
| 76 | 77 |
| 77 scoped_ptr<mojo::ShellConnection> connection_; | 78 std::unique_ptr<shell::ShellConnection> connection_; |
| 78 mojo::BindingSet<LifecycleControl> bindings_; | 79 mojo::BindingSet<LifecycleControl> bindings_; |
| 79 // Run when this object's connection to the shell is closed. | 80 // Run when this object's connection to the shell is closed. |
| 80 DestructCallback shell_connection_closed_callback_; | 81 DestructCallback shell_connection_closed_callback_; |
| 81 // Run when this object is destructed. | 82 // Run when this object is destructed. |
| 82 DestructCallback destruct_callback_; | 83 DestructCallback destruct_callback_; |
| 83 | 84 |
| 84 DISALLOW_COPY_AND_ASSIGN(PackagedApp); | 85 DISALLOW_COPY_AND_ASSIGN(PackagedApp); |
| 85 }; | 86 }; |
| 86 | 87 |
| 87 class Package | 88 class Package |
| 88 : public mojo::ShellClient, | 89 : public shell::ShellClient, |
| 89 public mojo::InterfaceFactory<mojo::shell::mojom::ShellClientFactory>, | 90 public shell::InterfaceFactory<shell::mojom::ShellClientFactory>, |
| 90 public mojo::shell::mojom::ShellClientFactory { | 91 public shell::mojom::ShellClientFactory { |
| 91 public: | 92 public: |
| 92 Package() {} | 93 Package() {} |
| 93 ~Package() override {} | 94 ~Package() override {} |
| 94 | 95 |
| 95 void set_runner(mojo::ApplicationRunner* runner) { | 96 void set_runner(shell::ApplicationRunner* runner) { |
| 96 app_client_.set_runner(runner); | 97 app_client_.set_runner(runner); |
| 97 } | 98 } |
| 98 | 99 |
| 99 private: | 100 private: |
| 100 // mojo::shell::test::AppClient: | 101 // shell::test::AppClient: |
| 101 bool AcceptConnection(mojo::Connection* connection) override { | 102 bool AcceptConnection(shell::Connection* connection) override { |
| 102 connection->AddInterface<mojo::shell::mojom::ShellClientFactory>(this); | 103 connection->AddInterface<shell::mojom::ShellClientFactory>(this); |
| 103 return app_client_.AcceptConnection(connection); | 104 return app_client_.AcceptConnection(connection); |
| 104 } | 105 } |
| 105 | 106 |
| 106 // mojo::InterfaceFactory<mojo::shell::mojom::ShellClientFactory>: | 107 // shell::InterfaceFactory<shell::mojom::ShellClientFactory>: |
| 107 void Create(mojo::Connection* connection, | 108 void Create(shell::Connection* connection, |
| 108 mojo::shell::mojom::ShellClientFactoryRequest request) override { | 109 shell::mojom::ShellClientFactoryRequest request) override { |
| 109 bindings_.AddBinding(this, std::move(request)); | 110 bindings_.AddBinding(this, std::move(request)); |
| 110 } | 111 } |
| 111 | 112 |
| 112 // mojo::shell::mojom::ShellClientFactory: | 113 // shell::mojom::ShellClientFactory: |
| 113 void CreateShellClient(mojo::shell::mojom::ShellClientRequest request, | 114 void CreateShellClient(shell::mojom::ShellClientRequest request, |
| 114 const mojo::String& name) override { | 115 const mojo::String& name) override { |
| 115 ++shell_connection_refcount_; | 116 ++shell_connection_refcount_; |
| 116 apps_.push_back( | 117 apps_.push_back( |
| 117 new PackagedApp(std::move(request), | 118 new PackagedApp(std::move(request), |
| 118 base::Bind(&Package::AppShellConnectionClosed, | 119 base::Bind(&Package::AppShellConnectionClosed, |
| 119 base::Unretained(this)), | 120 base::Unretained(this)), |
| 120 base::Bind(&Package::AppDestructed, | 121 base::Bind(&Package::AppDestructed, |
| 121 base::Unretained(this)))); | 122 base::Unretained(this)))); |
| 122 } | 123 } |
| 123 | 124 |
| 124 void AppShellConnectionClosed(PackagedApp* app) { | 125 void AppShellConnectionClosed(PackagedApp* app) { |
| 125 if (!--shell_connection_refcount_) | 126 if (!--shell_connection_refcount_) |
| 126 app_client_.CloseShellConnection(); | 127 app_client_.CloseShellConnection(); |
| 127 } | 128 } |
| 128 | 129 |
| 129 void AppDestructed(PackagedApp* app) { | 130 void AppDestructed(PackagedApp* app) { |
| 130 auto it = std::find(apps_.begin(), apps_.end(), app); | 131 auto it = std::find(apps_.begin(), apps_.end(), app); |
| 131 DCHECK(it != apps_.end()); | 132 DCHECK(it != apps_.end()); |
| 132 apps_.erase(it); | 133 apps_.erase(it); |
| 133 if (apps_.empty() && base::MessageLoop::current()->is_running()) | 134 if (apps_.empty() && base::MessageLoop::current()->is_running()) |
| 134 base::MessageLoop::current()->QuitWhenIdle(); | 135 base::MessageLoop::current()->QuitWhenIdle(); |
| 135 } | 136 } |
| 136 | 137 |
| 137 mojo::shell::test::AppClient app_client_; | 138 shell::test::AppClient app_client_; |
| 138 int shell_connection_refcount_ = 0; | 139 int shell_connection_refcount_ = 0; |
| 139 mojo::BindingSet<mojo::shell::mojom::ShellClientFactory> bindings_; | 140 mojo::BindingSet<shell::mojom::ShellClientFactory> bindings_; |
| 140 std::vector<PackagedApp*> apps_; | 141 std::vector<PackagedApp*> apps_; |
| 141 | 142 |
| 142 DISALLOW_COPY_AND_ASSIGN(Package); | 143 DISALLOW_COPY_AND_ASSIGN(Package); |
| 143 }; | 144 }; |
| 144 | 145 |
| 145 } // namespace | 146 } // namespace |
| 146 | 147 |
| 147 MojoResult MojoMain(MojoHandle shell_handle) { | 148 MojoResult MojoMain(MojoHandle shell_handle) { |
| 148 Package* package = new Package; | 149 Package* package = new Package; |
| 149 mojo::ApplicationRunner runner(package); | 150 shell::ApplicationRunner runner(package); |
| 150 package->set_runner(&runner); | 151 package->set_runner(&runner); |
| 151 return runner.Run(shell_handle); | 152 return runner.Run(shell_handle); |
| 152 } | 153 } |
| OLD | NEW |