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
3 // found in the LICENSE file.
5 #include "mojo/application/public/cpp/lib/service_registry.h"
8 #include "base/logging.h"
9 #include "mojo/application/public/cpp/application_connection.h"
10 #include "mojo/application/public/cpp/service_connector.h"
15 ServiceRegistry::ServiceRegistry(
16 const std::string
& connection_url
,
17 const std::string
& remote_url
,
18 ServiceProviderPtr remote_services
,
19 InterfaceRequest
<ServiceProvider
> local_services
,
20 const std::set
<std::string
>& allowed_interfaces
)
21 : connection_url_(connection_url
),
22 remote_url_(remote_url
),
24 remote_service_provider_(remote_services
.Pass()),
25 allowed_interfaces_(allowed_interfaces
),
26 allow_all_interfaces_(allowed_interfaces_
.size() == 1 &&
27 allowed_interfaces_
.count("*") == 1),
28 content_handler_id_(0u),
29 is_content_handler_id_valid_(false),
31 if (local_services
.is_pending())
32 local_binding_
.Bind(local_services
.Pass());
35 ServiceRegistry::ServiceRegistry()
36 : local_binding_(this),
37 allow_all_interfaces_(true),
41 ServiceRegistry::~ServiceRegistry() {
44 Shell::ConnectToApplicationCallback
45 ServiceRegistry::GetConnectToApplicationCallback() {
46 return base::Bind(&ServiceRegistry::OnGotContentHandlerID
,
47 weak_factory_
.GetWeakPtr());
50 void ServiceRegistry::SetServiceConnector(ServiceConnector
* connector
) {
51 service_connector_registry_
.set_service_connector(connector
);
54 bool ServiceRegistry::SetServiceConnectorForName(
55 ServiceConnector
* service_connector
,
56 const std::string
& interface_name
) {
57 if (allow_all_interfaces_
||
58 allowed_interfaces_
.count(interface_name
)) {
59 service_connector_registry_
.SetServiceConnectorForName(service_connector
,
63 LOG(WARNING
) << "CapabilityFilter prevented connection to interface: " <<
68 ServiceProvider
* ServiceRegistry::GetLocalServiceProvider() {
72 void ServiceRegistry::SetRemoteServiceProviderConnectionErrorHandler(
73 const Closure
& handler
) {
74 remote_service_provider_
.set_connection_error_handler(handler
);
77 bool ServiceRegistry::GetContentHandlerID(uint32_t* content_handler_id
) {
78 if (!is_content_handler_id_valid_
)
81 *content_handler_id
= content_handler_id_
;
85 void ServiceRegistry::AddContentHandlerIDCallback(const Closure
& callback
) {
86 if (is_content_handler_id_valid_
) {
90 content_handler_id_callbacks_
.push_back(callback
);
93 base::WeakPtr
<ApplicationConnection
> ServiceRegistry::GetWeakPtr() {
94 return weak_factory_
.GetWeakPtr();
97 void ServiceRegistry::RemoveServiceConnectorForName(
98 const std::string
& interface_name
) {
99 service_connector_registry_
.RemoveServiceConnectorForName(interface_name
);
100 if (service_connector_registry_
.empty())
101 remote_service_provider_
.reset();
104 const std::string
& ServiceRegistry::GetConnectionURL() {
105 return connection_url_
;
108 const std::string
& ServiceRegistry::GetRemoteApplicationURL() {
112 ServiceProvider
* ServiceRegistry::GetServiceProvider() {
113 return remote_service_provider_
.get();
116 void ServiceRegistry::OnGotContentHandlerID(uint32_t content_handler_id
) {
117 DCHECK(!is_content_handler_id_valid_
);
118 is_content_handler_id_valid_
= true;
119 content_handler_id_
= content_handler_id
;
120 std::vector
<Closure
> callbacks
;
121 callbacks
.swap(content_handler_id_callbacks_
);
122 for (auto callback
: callbacks
)
126 void ServiceRegistry::ConnectToService(const mojo::String
& service_name
,
127 ScopedMessagePipeHandle client_handle
) {
128 service_connector_registry_
.ConnectToService(this, service_name
,
129 client_handle
.Pass());
132 } // namespace internal