| 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/cpp/bindings/binding_set.h" | 10 #include "mojo/public/cpp/bindings/binding_set.h" |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 void GracefulQuit() override { | 60 void GracefulQuit() override { |
| 61 service_manager_connection_closed_callback_.Run(this); | 61 service_manager_connection_closed_callback_.Run(this); |
| 62 delete this; | 62 delete this; |
| 63 // This will have closed all |bindings_|. | 63 // This will have closed all |bindings_|. |
| 64 } | 64 } |
| 65 void Crash() override { | 65 void Crash() override { |
| 66 // When multiple instances are vended from the same package instance, this | 66 // When multiple instances are vended from the same package instance, this |
| 67 // will cause all instances to be quit. | 67 // will cause all instances to be quit. |
| 68 exit(1); | 68 exit(1); |
| 69 } | 69 } |
| 70 void CloseShellConnection() override { | 70 void CloseServiceManagerConnection() override { |
| 71 service_manager_connection_closed_callback_.Run(this); | 71 service_manager_connection_closed_callback_.Run(this); |
| 72 connection_.reset(); | 72 connection_.reset(); |
| 73 // This only closed our relationship with the shell, existing |bindings_| | 73 // This only closed our relationship with the service manager, existing |
| 74 // |bindings_| |
| 74 // remain active. | 75 // remain active. |
| 75 } | 76 } |
| 76 | 77 |
| 77 void BindingLost() { | 78 void BindingLost() { |
| 78 if (bindings_.empty()) | 79 if (bindings_.empty()) |
| 79 delete this; | 80 delete this; |
| 80 } | 81 } |
| 81 | 82 |
| 82 std::unique_ptr<service_manager::ServiceContext> connection_; | 83 std::unique_ptr<service_manager::ServiceContext> connection_; |
| 83 mojo::BindingSet<LifecycleControl> bindings_; | 84 mojo::BindingSet<LifecycleControl> bindings_; |
| 84 // Run when this object's connection to the shell is closed. | 85 // Run when this object's connection to the service manager is closed. |
| 85 DestructCallback service_manager_connection_closed_callback_; | 86 DestructCallback service_manager_connection_closed_callback_; |
| 86 // Run when this object is destructed. | 87 // Run when this object is destructed. |
| 87 DestructCallback destruct_callback_; | 88 DestructCallback destruct_callback_; |
| 88 | 89 |
| 89 DISALLOW_COPY_AND_ASSIGN(PackagedApp); | 90 DISALLOW_COPY_AND_ASSIGN(PackagedApp); |
| 90 }; | 91 }; |
| 91 | 92 |
| 92 class Package : public service_manager::Service, | 93 class Package : public service_manager::Service, |
| 93 public service_manager::InterfaceFactory< | 94 public service_manager::InterfaceFactory< |
| 94 service_manager::mojom::ServiceFactory>, | 95 service_manager::mojom::ServiceFactory>, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 112 // service_manager::InterfaceFactory<service_manager::mojom::ServiceFactory>: | 113 // service_manager::InterfaceFactory<service_manager::mojom::ServiceFactory>: |
| 113 void Create(const service_manager::Identity& remote_identity, | 114 void Create(const service_manager::Identity& remote_identity, |
| 114 service_manager::mojom::ServiceFactoryRequest request) override { | 115 service_manager::mojom::ServiceFactoryRequest request) override { |
| 115 bindings_.AddBinding(this, std::move(request)); | 116 bindings_.AddBinding(this, std::move(request)); |
| 116 } | 117 } |
| 117 | 118 |
| 118 // service_manager::mojom::ServiceFactory: | 119 // service_manager::mojom::ServiceFactory: |
| 119 void CreateService(service_manager::mojom::ServiceRequest request, | 120 void CreateService(service_manager::mojom::ServiceRequest request, |
| 120 const std::string& name) override { | 121 const std::string& name) override { |
| 121 ++service_manager_connection_refcount_; | 122 ++service_manager_connection_refcount_; |
| 122 apps_.push_back( | 123 apps_.push_back(new PackagedApp( |
| 123 new PackagedApp(std::move(request), | 124 std::move(request), |
| 124 base::Bind(&Package::AppShellConnectionClosed, | 125 base::Bind(&Package::AppServiceManagerConnectionClosed, |
| 125 base::Unretained(this)), | 126 base::Unretained(this)), |
| 126 base::Bind(&Package::AppDestructed, | 127 base::Bind(&Package::AppDestructed, base::Unretained(this)))); |
| 127 base::Unretained(this)))); | |
| 128 } | 128 } |
| 129 | 129 |
| 130 void AppShellConnectionClosed(PackagedApp* app) { | 130 void AppServiceManagerConnectionClosed(PackagedApp* app) { |
| 131 if (!--service_manager_connection_refcount_) | 131 if (!--service_manager_connection_refcount_) |
| 132 app_client_.CloseShellConnection(); | 132 app_client_.CloseServiceManagerConnection(); |
| 133 } | 133 } |
| 134 | 134 |
| 135 void AppDestructed(PackagedApp* app) { | 135 void AppDestructed(PackagedApp* app) { |
| 136 auto it = std::find(apps_.begin(), apps_.end(), app); | 136 auto it = std::find(apps_.begin(), apps_.end(), app); |
| 137 DCHECK(it != apps_.end()); | 137 DCHECK(it != apps_.end()); |
| 138 apps_.erase(it); | 138 apps_.erase(it); |
| 139 if (apps_.empty() && base::MessageLoop::current()->is_running()) | 139 if (apps_.empty() && base::MessageLoop::current()->is_running()) |
| 140 base::MessageLoop::current()->QuitWhenIdle(); | 140 base::MessageLoop::current()->QuitWhenIdle(); |
| 141 } | 141 } |
| 142 | 142 |
| 143 service_manager::test::AppClient app_client_; | 143 service_manager::test::AppClient app_client_; |
| 144 int service_manager_connection_refcount_ = 0; | 144 int service_manager_connection_refcount_ = 0; |
| 145 mojo::BindingSet<service_manager::mojom::ServiceFactory> bindings_; | 145 mojo::BindingSet<service_manager::mojom::ServiceFactory> bindings_; |
| 146 std::vector<PackagedApp*> apps_; | 146 std::vector<PackagedApp*> apps_; |
| 147 | 147 |
| 148 DISALLOW_COPY_AND_ASSIGN(Package); | 148 DISALLOW_COPY_AND_ASSIGN(Package); |
| 149 }; | 149 }; |
| 150 | 150 |
| 151 } // namespace | 151 } // namespace |
| 152 | 152 |
| 153 MojoResult ServiceMain(MojoHandle service_request_handle) { | 153 MojoResult ServiceMain(MojoHandle service_request_handle) { |
| 154 Package* package = new Package; | 154 Package* package = new Package; |
| 155 service_manager::ServiceRunner runner(package); | 155 service_manager::ServiceRunner runner(package); |
| 156 package->set_runner(&runner); | 156 package->set_runner(&runner); |
| 157 return runner.Run(service_request_handle); | 157 return runner.Run(service_request_handle); |
| 158 } | 158 } |
| OLD | NEW |