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> |
| 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/logging.h" | 10 #include "base/logging.h" |
9 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
10 #include "mojo/public/cpp/bindings/interface_ptr.h" | 12 #include "mojo/public/cpp/bindings/interface_ptr.h" |
11 #include "mojo/public/cpp/bindings/interface_request.h" | 13 #include "mojo/public/cpp/bindings/interface_request.h" |
12 #include "services/service_manager/public/cpp/interface_provider_spec.h" | 14 #include "services/service_manager/public/cpp/interface_provider_spec.h" |
13 #include "services/service_manager/public/cpp/interface_registry.h" | 15 #include "services/service_manager/public/cpp/interface_registry.h" |
14 #include "services/service_manager/public/cpp/lib/connector_impl.h" | 16 #include "services/service_manager/public/cpp/lib/connector_impl.h" |
15 #include "services/service_manager/public/cpp/service.h" | 17 #include "services/service_manager/public/cpp/service.h" |
16 | 18 |
17 namespace service_manager { | 19 namespace service_manager { |
18 | 20 |
19 //////////////////////////////////////////////////////////////////////////////// | 21 //////////////////////////////////////////////////////////////////////////////// |
20 // ServiceContext, public: | 22 // ServiceContext, public: |
21 | 23 |
22 ServiceContext::ServiceContext(service_manager::Service* service, | 24 ServiceContext::ServiceContext(service_manager::Service* service, |
23 mojom::ServiceRequest request, | 25 mojom::ServiceRequest request, |
24 std::unique_ptr<Connector> connector, | 26 std::unique_ptr<Connector> connector, |
25 mojom::ConnectorRequest connector_request) | 27 mojom::ConnectorRequest connector_request) |
26 : pending_connector_request_(std::move(connector_request)), | 28 : pending_connector_request_(std::move(connector_request)), |
27 service_(service), | 29 service_(service), |
28 binding_(this, std::move(request)), | 30 binding_(this, std::move(request)), |
29 connector_(std::move(connector)) { | 31 connector_(std::move(connector)), |
| 32 weak_factory_(this) { |
30 DCHECK(binding_.is_bound()); | 33 DCHECK(binding_.is_bound()); |
31 binding_.set_connection_error_handler( | 34 binding_.set_connection_error_handler( |
32 base::Bind(&ServiceContext::OnConnectionError, base::Unretained(this))); | 35 base::Bind(&ServiceContext::OnConnectionError, base::Unretained(this))); |
33 if (!connector_) { | 36 if (!connector_) { |
34 connector_ = Connector::Create(&pending_connector_request_); | 37 connector_ = Connector::Create(&pending_connector_request_); |
35 } else { | 38 } else { |
36 DCHECK(pending_connector_request_.is_pending()); | 39 DCHECK(pending_connector_request_.is_pending()); |
37 } | 40 } |
38 } | 41 } |
39 | 42 |
(...skipping 29 matching lines...) Expand all Loading... |
69 GetInterfaceProviderSpec(mojom::kServiceManager_ConnectorSpec, | 72 GetInterfaceProviderSpec(mojom::kServiceManager_ConnectorSpec, |
70 source_info.interface_provider_specs, &source_spec); | 73 source_info.interface_provider_specs, &source_spec); |
71 auto registry = | 74 auto registry = |
72 base::MakeUnique<InterfaceRegistry>(mojom::kServiceManager_ConnectorSpec); | 75 base::MakeUnique<InterfaceRegistry>(mojom::kServiceManager_ConnectorSpec); |
73 registry->Bind(std::move(interfaces), local_info_.identity, target_spec, | 76 registry->Bind(std::move(interfaces), local_info_.identity, target_spec, |
74 source_info.identity, source_spec); | 77 source_info.identity, source_spec); |
75 | 78 |
76 if (!service_->OnConnect(source_info, registry.get())) | 79 if (!service_->OnConnect(source_info, registry.get())) |
77 return; | 80 return; |
78 | 81 |
79 // TODO(beng): it appears we never prune this list. We should, when the | 82 InterfaceRegistry* raw_registry = registry.get(); |
80 // registry's remote interface provider pipe breaks. | 83 registry->AddConnectionLostClosure(base::Bind( |
81 incoming_connections_.push_back(std::move(registry)); | 84 &ServiceContext::OnRegistryConnectionError, base::Unretained(this), |
| 85 raw_registry)); |
| 86 connection_interface_registries_.insert( |
| 87 std::make_pair(raw_registry, std::move(registry))); |
82 } | 88 } |
83 | 89 |
84 //////////////////////////////////////////////////////////////////////////////// | 90 //////////////////////////////////////////////////////////////////////////////// |
85 // ServiceContext, private: | 91 // ServiceContext, private: |
86 | 92 |
87 void ServiceContext::OnConnectionError() { | 93 void ServiceContext::OnConnectionError() { |
88 // Note that the Service doesn't technically have to quit now, it may live | 94 // Note that the Service doesn't technically have to quit now, it may live |
89 // on to service existing connections. All existing Connectors however are | 95 // on to service existing connections. All existing Connectors however are |
90 // invalid. | 96 // invalid. |
91 should_run_connection_lost_closure_ = service_->OnStop(); | 97 should_run_connection_lost_closure_ = service_->OnStop(); |
92 if (should_run_connection_lost_closure_ && | 98 if (should_run_connection_lost_closure_ && |
93 !connection_lost_closure_.is_null()) | 99 !connection_lost_closure_.is_null()) |
94 connection_lost_closure_.Run(); | 100 connection_lost_closure_.Run(); |
95 // We don't reset the connector as clients may have taken a raw pointer to it. | 101 // We don't reset the connector as clients may have taken a raw pointer to it. |
96 // Connect() will return nullptr if they try to connect to anything. | 102 // Connect() will return nullptr if they try to connect to anything. |
97 } | 103 } |
98 | 104 |
| 105 void ServiceContext::OnRegistryConnectionError(InterfaceRegistry* registry) { |
| 106 // NOTE: We destroy the InterfaceRegistry asynchronously since it's calling |
| 107 // into us from its own connection error handler which may continue to access |
| 108 // the InterfaceRegistry's own state after we return. |
| 109 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 110 FROM_HERE, |
| 111 base::Bind(&ServiceContext::DestroyConnectionInterfaceRegistry, |
| 112 weak_factory_.GetWeakPtr(), registry)); |
| 113 } |
| 114 |
| 115 void ServiceContext::DestroyConnectionInterfaceRegistry( |
| 116 InterfaceRegistry* registry) { |
| 117 auto it = connection_interface_registries_.find(registry); |
| 118 CHECK(it != connection_interface_registries_.end()); |
| 119 connection_interface_registries_.erase(it); |
| 120 } |
| 121 |
99 } // namespace service_manager | 122 } // namespace service_manager |
OLD | NEW |