| 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 "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "mojo/public/cpp/bindings/binding_set.h" | 11 #include "mojo/public/cpp/bindings/binding_set.h" |
| 12 #include "services/service_manager/public/c/main.h" | 12 #include "services/service_manager/public/c/main.h" |
| 13 #include "services/service_manager/public/cpp/binder_registry.h" |
| 13 #include "services/service_manager/public/cpp/interface_factory.h" | 14 #include "services/service_manager/public/cpp/interface_factory.h" |
| 14 #include "services/service_manager/public/cpp/interface_registry.h" | |
| 15 #include "services/service_manager/public/cpp/service_context.h" | 15 #include "services/service_manager/public/cpp/service_context.h" |
| 16 #include "services/service_manager/public/cpp/service_runner.h" | 16 #include "services/service_manager/public/cpp/service_runner.h" |
| 17 #include "services/service_manager/public/interfaces/service_factory.mojom.h" | 17 #include "services/service_manager/public/interfaces/service_factory.mojom.h" |
| 18 #include "services/service_manager/tests/lifecycle/app_client.h" | 18 #include "services/service_manager/tests/lifecycle/app_client.h" |
| 19 #include "services/service_manager/tests/lifecycle/lifecycle_unittest.mojom.h" | 19 #include "services/service_manager/tests/lifecycle/lifecycle_unittest.mojom.h" |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 class PackagedApp | 23 class PackagedApp |
| 24 : public service_manager::Service, | 24 : public service_manager::Service, |
| 25 public service_manager::InterfaceFactory< | 25 public service_manager::InterfaceFactory< |
| 26 service_manager::test::mojom::LifecycleControl>, | 26 service_manager::test::mojom::LifecycleControl>, |
| 27 public service_manager::test::mojom::LifecycleControl { | 27 public service_manager::test::mojom::LifecycleControl { |
| 28 public: | 28 public: |
| 29 PackagedApp( | 29 PackagedApp( |
| 30 const base::Closure& service_manager_connection_closed_callback, | 30 const base::Closure& service_manager_connection_closed_callback, |
| 31 const base::Closure& destruct_callback) | 31 const base::Closure& destruct_callback) |
| 32 : service_manager_connection_closed_callback_( | 32 : service_manager_connection_closed_callback_( |
| 33 service_manager_connection_closed_callback), | 33 service_manager_connection_closed_callback), |
| 34 destruct_callback_(destruct_callback) { | 34 destruct_callback_(destruct_callback) { |
| 35 bindings_.set_connection_error_handler(base::Bind(&PackagedApp::BindingLost, | 35 bindings_.set_connection_error_handler(base::Bind(&PackagedApp::BindingLost, |
| 36 base::Unretained(this))); | 36 base::Unretained(this))); |
| 37 registry_.AddInterface<service_manager::test::mojom::LifecycleControl>( |
| 38 this); |
| 37 } | 39 } |
| 38 | 40 |
| 39 ~PackagedApp() override {} | 41 ~PackagedApp() override {} |
| 40 | 42 |
| 41 private: | 43 private: |
| 42 // service_manager::Service: | 44 // service_manager::Service: |
| 43 bool OnConnect(const service_manager::ServiceInfo& remote_info, | 45 void OnBindInterface(const service_manager::ServiceInfo& source_info, |
| 44 service_manager::InterfaceRegistry* registry) override { | 46 const std::string& interface_name, |
| 45 registry->AddInterface<service_manager::test::mojom::LifecycleControl>( | 47 mojo::ScopedMessagePipeHandle interface_pipe) override { |
| 46 this); | 48 registry_.BindInterface(source_info.identity, interface_name, |
| 47 return true; | 49 std::move(interface_pipe)); |
| 48 } | 50 } |
| 49 | 51 |
| 50 // service_manager::InterfaceFactory<LifecycleControl> | 52 // service_manager::InterfaceFactory<LifecycleControl> |
| 51 void Create( | 53 void Create( |
| 52 const service_manager::Identity& remote_identity, | 54 const service_manager::Identity& remote_identity, |
| 53 service_manager::test::mojom::LifecycleControlRequest request) override { | 55 service_manager::test::mojom::LifecycleControlRequest request) override { |
| 54 bindings_.AddBinding(this, std::move(request)); | 56 bindings_.AddBinding(this, std::move(request)); |
| 55 } | 57 } |
| 56 | 58 |
| 57 // LifecycleControl: | 59 // LifecycleControl: |
| (...skipping 22 matching lines...) Expand all Loading... |
| 80 // remain active. | 82 // remain active. |
| 81 } | 83 } |
| 82 | 84 |
| 83 void BindingLost() { | 85 void BindingLost() { |
| 84 if (bindings_.empty()) { | 86 if (bindings_.empty()) { |
| 85 // Deletes |this|. | 87 // Deletes |this|. |
| 86 destruct_callback_.Run(); | 88 destruct_callback_.Run(); |
| 87 } | 89 } |
| 88 } | 90 } |
| 89 | 91 |
| 92 service_manager::BinderRegistry registry_; |
| 90 mojo::BindingSet<service_manager::test::mojom::LifecycleControl> bindings_; | 93 mojo::BindingSet<service_manager::test::mojom::LifecycleControl> bindings_; |
| 91 | 94 |
| 92 // Run when this object's connection to the service manager is closed. | 95 // Run when this object's connection to the service manager is closed. |
| 93 base::Closure service_manager_connection_closed_callback_; | 96 base::Closure service_manager_connection_closed_callback_; |
| 94 // Run when this object is destructed. | 97 // Run when this object is destructed. |
| 95 base::Closure destruct_callback_; | 98 base::Closure destruct_callback_; |
| 96 | 99 |
| 97 DISALLOW_COPY_AND_ASSIGN(PackagedApp); | 100 DISALLOW_COPY_AND_ASSIGN(PackagedApp); |
| 98 }; | 101 }; |
| 99 | 102 |
| 100 class Package : public service_manager::ForwardingService, | 103 class Package : public service_manager::ForwardingService, |
| 101 public service_manager::InterfaceFactory< | 104 public service_manager::InterfaceFactory< |
| 102 service_manager::mojom::ServiceFactory>, | 105 service_manager::mojom::ServiceFactory>, |
| 103 public service_manager::mojom::ServiceFactory { | 106 public service_manager::mojom::ServiceFactory { |
| 104 public: | 107 public: |
| 105 Package() : ForwardingService(&app_client_) {} | 108 Package() : ForwardingService(&app_client_) { |
| 109 registry_.AddInterface<service_manager::mojom::ServiceFactory>(this); |
| 110 } |
| 106 ~Package() override {} | 111 ~Package() override {} |
| 107 | 112 |
| 108 private: | 113 private: |
| 109 // ForwardingService: | 114 // ForwardingService: |
| 110 bool OnConnect(const service_manager::ServiceInfo& remote_info, | 115 void OnBindInterface(const service_manager::ServiceInfo& source_info, |
| 111 service_manager::InterfaceRegistry* registry) override { | 116 const std::string& interface_name, |
| 112 registry->AddInterface<service_manager::mojom::ServiceFactory>(this); | 117 mojo::ScopedMessagePipeHandle interface_pipe) override { |
| 113 return ForwardingService::OnConnect(remote_info, registry); | 118 if (registry_.CanBindInterface(interface_name)) { |
| 119 registry_.BindInterface(source_info.identity, interface_name, |
| 120 std::move(interface_pipe)); |
| 121 } else { |
| 122 ForwardingService::OnBindInterface(source_info, interface_name, |
| 123 std::move(interface_pipe)); |
| 124 } |
| 114 } | 125 } |
| 115 | 126 |
| 116 // service_manager::InterfaceFactory<service_manager::mojom::ServiceFactory>: | 127 // service_manager::InterfaceFactory<service_manager::mojom::ServiceFactory>: |
| 117 void Create(const service_manager::Identity& remote_identity, | 128 void Create(const service_manager::Identity& remote_identity, |
| 118 service_manager::mojom::ServiceFactoryRequest request) override { | 129 service_manager::mojom::ServiceFactoryRequest request) override { |
| 119 bindings_.AddBinding(this, std::move(request)); | 130 bindings_.AddBinding(this, std::move(request)); |
| 120 } | 131 } |
| 121 | 132 |
| 122 // service_manager::mojom::ServiceFactory: | 133 // service_manager::mojom::ServiceFactory: |
| 123 void CreateService(service_manager::mojom::ServiceRequest request, | 134 void CreateService(service_manager::mojom::ServiceRequest request, |
| (...skipping 25 matching lines...) Expand all Loading... |
| 149 auto it = contexts_.find(id_it->second); | 160 auto it = contexts_.find(id_it->second); |
| 150 DCHECK(it != contexts_.end()); | 161 DCHECK(it != contexts_.end()); |
| 151 contexts_.erase(it); | 162 contexts_.erase(it); |
| 152 id_to_context_.erase(id_it); | 163 id_to_context_.erase(id_it); |
| 153 if (contexts_.empty() && base::MessageLoop::current()->is_running()) | 164 if (contexts_.empty() && base::MessageLoop::current()->is_running()) |
| 154 base::MessageLoop::current()->QuitWhenIdle(); | 165 base::MessageLoop::current()->QuitWhenIdle(); |
| 155 } | 166 } |
| 156 | 167 |
| 157 service_manager::test::AppClient app_client_; | 168 service_manager::test::AppClient app_client_; |
| 158 int service_manager_connection_refcount_ = 0; | 169 int service_manager_connection_refcount_ = 0; |
| 170 service_manager::BinderRegistry registry_; |
| 159 mojo::BindingSet<service_manager::mojom::ServiceFactory> bindings_; | 171 mojo::BindingSet<service_manager::mojom::ServiceFactory> bindings_; |
| 160 | 172 |
| 161 using ServiceContextMap = | 173 using ServiceContextMap = |
| 162 std::map<service_manager::ServiceContext*, | 174 std::map<service_manager::ServiceContext*, |
| 163 std::unique_ptr<service_manager::ServiceContext>>; | 175 std::unique_ptr<service_manager::ServiceContext>>; |
| 164 ServiceContextMap contexts_; | 176 ServiceContextMap contexts_; |
| 165 | 177 |
| 166 int next_id_ = 0; | 178 int next_id_ = 0; |
| 167 std::map<int, service_manager::ServiceContext*> id_to_context_; | 179 std::map<int, service_manager::ServiceContext*> id_to_context_; |
| 168 | 180 |
| 169 DISALLOW_COPY_AND_ASSIGN(Package); | 181 DISALLOW_COPY_AND_ASSIGN(Package); |
| 170 }; | 182 }; |
| 171 | 183 |
| 172 } // namespace | 184 } // namespace |
| 173 | 185 |
| 174 MojoResult ServiceMain(MojoHandle service_request_handle) { | 186 MojoResult ServiceMain(MojoHandle service_request_handle) { |
| 175 service_manager::ServiceRunner runner(new Package); | 187 service_manager::ServiceRunner runner(new Package); |
| 176 return runner.Run(service_request_handle); | 188 return runner.Run(service_request_handle); |
| 177 } | 189 } |
| OLD | NEW |