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