| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "services/service_manager/public/cpp/service_context.h" | 5 #include "services/service_manager/public/cpp/service_context.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback_helpers.h" |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 12 #include "mojo/public/cpp/bindings/interface_ptr.h" | 13 #include "mojo/public/cpp/bindings/interface_ptr.h" |
| 13 #include "mojo/public/cpp/bindings/interface_request.h" | 14 #include "mojo/public/cpp/bindings/interface_request.h" |
| 14 #include "services/service_manager/public/cpp/interface_provider_spec.h" | 15 #include "services/service_manager/public/cpp/interface_provider_spec.h" |
| 15 #include "services/service_manager/public/cpp/interface_registry.h" | 16 #include "services/service_manager/public/cpp/interface_registry.h" |
| 16 #include "services/service_manager/public/cpp/lib/connector_impl.h" | 17 #include "services/service_manager/public/cpp/lib/connector_impl.h" |
| 17 #include "services/service_manager/public/cpp/service.h" | 18 #include "services/service_manager/public/cpp/service.h" |
| 18 | 19 |
| 19 namespace service_manager { | 20 namespace service_manager { |
| 20 | 21 |
| 21 //////////////////////////////////////////////////////////////////////////////// | 22 //////////////////////////////////////////////////////////////////////////////// |
| 22 // ServiceContext, public: | 23 // ServiceContext, public: |
| 23 | 24 |
| 24 ServiceContext::ServiceContext(service_manager::Service* service, | 25 ServiceContext::ServiceContext( |
| 25 mojom::ServiceRequest request, | 26 std::unique_ptr<service_manager::Service> service, |
| 26 std::unique_ptr<Connector> connector, | 27 mojom::ServiceRequest request, |
| 27 mojom::ConnectorRequest connector_request) | 28 std::unique_ptr<Connector> connector, |
| 29 mojom::ConnectorRequest connector_request) |
| 28 : pending_connector_request_(std::move(connector_request)), | 30 : pending_connector_request_(std::move(connector_request)), |
| 29 service_(service), | 31 service_(std::move(service)), |
| 30 binding_(this, std::move(request)), | 32 binding_(this, std::move(request)), |
| 31 connector_(std::move(connector)), | 33 connector_(std::move(connector)), |
| 32 weak_factory_(this) { | 34 weak_factory_(this) { |
| 33 DCHECK(binding_.is_bound()); | 35 DCHECK(binding_.is_bound()); |
| 34 binding_.set_connection_error_handler( | 36 binding_.set_connection_error_handler( |
| 35 base::Bind(&ServiceContext::OnConnectionError, base::Unretained(this))); | 37 base::Bind(&ServiceContext::OnConnectionError, base::Unretained(this))); |
| 36 if (!connector_) { | 38 if (!connector_) { |
| 37 connector_ = Connector::Create(&pending_connector_request_); | 39 connector_ = Connector::Create(&pending_connector_request_); |
| 38 } else { | 40 } else { |
| 39 DCHECK(pending_connector_request_.is_pending()); | 41 DCHECK(pending_connector_request_.is_pending()); |
| 40 } | 42 } |
| 41 } | 43 } |
| 42 | 44 |
| 43 ServiceContext::~ServiceContext() {} | 45 ServiceContext::~ServiceContext() {} |
| 44 | 46 |
| 45 void ServiceContext::SetConnectionLostClosure(const base::Closure& closure) { | 47 void ServiceContext::SetConnectionLostClosure(const base::Closure& closure) { |
| 46 connection_lost_closure_ = closure; | 48 connection_lost_closure_ = closure; |
| 47 if (should_run_connection_lost_closure_ && | 49 if (service_quit_) |
| 48 !connection_lost_closure_.is_null()) | 50 QuitNow(); |
| 49 connection_lost_closure_.Run(); | 51 } |
| 52 |
| 53 void ServiceContext::RequestQuit() { |
| 54 // TODO(rockot): Implement this. |
| 55 } |
| 56 |
| 57 void ServiceContext::DisconnectFromServiceManager() { |
| 58 if (binding_.is_bound()) |
| 59 binding_.Close(); |
| 60 connector_.reset(); |
| 61 } |
| 62 |
| 63 void ServiceContext::QuitNow() { |
| 64 if (binding_.is_bound()) |
| 65 binding_.Close(); |
| 66 if (!connection_lost_closure_.is_null()) |
| 67 base::ResetAndReturn(&connection_lost_closure_).Run(); |
| 68 } |
| 69 |
| 70 void ServiceContext::DestroyService() { |
| 71 QuitNow(); |
| 72 service_.reset(); |
| 50 } | 73 } |
| 51 | 74 |
| 52 //////////////////////////////////////////////////////////////////////////////// | 75 //////////////////////////////////////////////////////////////////////////////// |
| 53 // ServiceContext, mojom::Service implementation: | 76 // ServiceContext, mojom::Service implementation: |
| 54 | 77 |
| 55 void ServiceContext::OnStart(const ServiceInfo& info, | 78 void ServiceContext::OnStart(const ServiceInfo& info, |
| 56 const OnStartCallback& callback) { | 79 const OnStartCallback& callback) { |
| 57 local_info_ = info; | 80 local_info_ = info; |
| 58 if (!initialize_handler_.is_null()) | |
| 59 initialize_handler_.Run(); | |
| 60 | |
| 61 callback.Run(std::move(pending_connector_request_)); | 81 callback.Run(std::move(pending_connector_request_)); |
| 62 | 82 service_->OnStart(this); |
| 63 service_->OnStart(info); | |
| 64 } | 83 } |
| 65 | 84 |
| 66 void ServiceContext::OnConnect( | 85 void ServiceContext::OnConnect( |
| 67 const ServiceInfo& source_info, | 86 const ServiceInfo& source_info, |
| 68 mojom::InterfaceProviderRequest interfaces) { | 87 mojom::InterfaceProviderRequest interfaces) { |
| 69 InterfaceProviderSpec source_spec, target_spec; | 88 InterfaceProviderSpec source_spec, target_spec; |
| 70 GetInterfaceProviderSpec(mojom::kServiceManager_ConnectorSpec, | 89 GetInterfaceProviderSpec(mojom::kServiceManager_ConnectorSpec, |
| 71 local_info_.interface_provider_specs, &target_spec); | 90 local_info_.interface_provider_specs, &target_spec); |
| 72 GetInterfaceProviderSpec(mojom::kServiceManager_ConnectorSpec, | 91 GetInterfaceProviderSpec(mojom::kServiceManager_ConnectorSpec, |
| 73 source_info.interface_provider_specs, &source_spec); | 92 source_info.interface_provider_specs, &source_spec); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 87 std::make_pair(raw_registry, std::move(registry))); | 106 std::make_pair(raw_registry, std::move(registry))); |
| 88 } | 107 } |
| 89 | 108 |
| 90 //////////////////////////////////////////////////////////////////////////////// | 109 //////////////////////////////////////////////////////////////////////////////// |
| 91 // ServiceContext, private: | 110 // ServiceContext, private: |
| 92 | 111 |
| 93 void ServiceContext::OnConnectionError() { | 112 void ServiceContext::OnConnectionError() { |
| 94 // Note that the Service doesn't technically have to quit now, it may live | 113 // Note that the Service doesn't technically have to quit now, it may live |
| 95 // on to service existing connections. All existing Connectors however are | 114 // on to service existing connections. All existing Connectors however are |
| 96 // invalid. | 115 // invalid. |
| 97 should_run_connection_lost_closure_ = service_->OnStop(); | 116 service_quit_ = service_->OnStop(); |
| 98 if (should_run_connection_lost_closure_ && | 117 if (service_quit_) { |
| 99 !connection_lost_closure_.is_null()) | 118 QuitNow(); |
| 100 connection_lost_closure_.Run(); | 119 // NOTE: This call may delete |this|, so don't access any ServiceContext |
| 120 // state beyond this point. |
| 121 return; |
| 122 } |
| 123 |
| 101 // We don't reset the connector as clients may have taken a raw pointer to it. | 124 // We don't reset the connector as clients may have taken a raw pointer to it. |
| 102 // Connect() will return nullptr if they try to connect to anything. | 125 // Connect() will return nullptr if they try to connect to anything. |
| 103 } | 126 } |
| 104 | 127 |
| 105 void ServiceContext::OnRegistryConnectionError(InterfaceRegistry* registry) { | 128 void ServiceContext::OnRegistryConnectionError(InterfaceRegistry* registry) { |
| 106 // NOTE: We destroy the InterfaceRegistry asynchronously since it's calling | 129 // NOTE: We destroy the InterfaceRegistry asynchronously since it's calling |
| 107 // into us from its own connection error handler which may continue to access | 130 // into us from its own connection error handler which may continue to access |
| 108 // the InterfaceRegistry's own state after we return. | 131 // the InterfaceRegistry's own state after we return. |
| 109 base::ThreadTaskRunnerHandle::Get()->PostTask( | 132 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 110 FROM_HERE, | 133 FROM_HERE, |
| 111 base::Bind(&ServiceContext::DestroyConnectionInterfaceRegistry, | 134 base::Bind(&ServiceContext::DestroyConnectionInterfaceRegistry, |
| 112 weak_factory_.GetWeakPtr(), registry)); | 135 weak_factory_.GetWeakPtr(), registry)); |
| 113 } | 136 } |
| 114 | 137 |
| 115 void ServiceContext::DestroyConnectionInterfaceRegistry( | 138 void ServiceContext::DestroyConnectionInterfaceRegistry( |
| 116 InterfaceRegistry* registry) { | 139 InterfaceRegistry* registry) { |
| 117 auto it = connection_interface_registries_.find(registry); | 140 auto it = connection_interface_registries_.find(registry); |
| 118 CHECK(it != connection_interface_registries_.end()); | 141 CHECK(it != connection_interface_registries_.end()); |
| 119 connection_interface_registries_.erase(it); | 142 connection_interface_registries_.erase(it); |
| 120 } | 143 } |
| 121 | 144 |
| 122 } // namespace service_manager | 145 } // namespace service_manager |
| OLD | NEW |