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