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 "mojo/application/public/cpp/lib/service_registry.h" | 5 #include "mojo/application/public/cpp/lib/service_registry.h" |
6 | 6 |
| 7 #include "base/bind.h" |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
8 #include "mojo/application/public/cpp/application_connection.h" | 9 #include "mojo/application/public/cpp/application_connection.h" |
9 #include "mojo/application/public/cpp/service_connector.h" | 10 #include "mojo/application/public/cpp/service_connector.h" |
10 | 11 |
11 namespace mojo { | 12 namespace mojo { |
12 namespace internal { | 13 namespace internal { |
13 | 14 |
14 ServiceRegistry::ServiceRegistry( | 15 ServiceRegistry::ServiceRegistry( |
15 const std::string& connection_url, | 16 const std::string& connection_url, |
16 const std::string& remote_url, | 17 const std::string& remote_url, |
17 ServiceProviderPtr remote_services, | 18 ServiceProviderPtr remote_services, |
18 InterfaceRequest<ServiceProvider> local_services, | 19 InterfaceRequest<ServiceProvider> local_services, |
19 const std::set<std::string>& allowed_interfaces) | 20 const std::set<std::string>& allowed_interfaces) |
20 : connection_url_(connection_url), | 21 : connection_url_(connection_url), |
21 remote_url_(remote_url), | 22 remote_url_(remote_url), |
22 local_binding_(this), | 23 local_binding_(this), |
23 remote_service_provider_(remote_services.Pass()), | 24 remote_service_provider_(remote_services.Pass()), |
24 allowed_interfaces_(allowed_interfaces), | 25 allowed_interfaces_(allowed_interfaces), |
25 allow_all_interfaces_(allowed_interfaces_.size() == 1 && | 26 allow_all_interfaces_(allowed_interfaces_.size() == 1 && |
26 allowed_interfaces_.count("*") == 1), | 27 allowed_interfaces_.count("*") == 1), |
| 28 content_handler_id_(0u), |
| 29 is_content_handler_id_valid_(false), |
27 weak_factory_(this) { | 30 weak_factory_(this) { |
28 if (local_services.is_pending()) | 31 if (local_services.is_pending()) |
29 local_binding_.Bind(local_services.Pass()); | 32 local_binding_.Bind(local_services.Pass()); |
30 } | 33 } |
31 | 34 |
32 ServiceRegistry::ServiceRegistry() | 35 ServiceRegistry::ServiceRegistry() |
33 : local_binding_(this), | 36 : local_binding_(this), |
34 allow_all_interfaces_(true), | 37 allow_all_interfaces_(true), |
35 weak_factory_(this) { | 38 weak_factory_(this) { |
36 } | 39 } |
37 | 40 |
38 ServiceRegistry::~ServiceRegistry() { | 41 ServiceRegistry::~ServiceRegistry() { |
39 } | 42 } |
40 | 43 |
| 44 Shell::ConnectToApplicationCallback |
| 45 ServiceRegistry::GetConnectToApplicationCallback() { |
| 46 return base::Bind(&ServiceRegistry::OnGotContentHandlerID, |
| 47 weak_factory_.GetWeakPtr()); |
| 48 } |
| 49 |
41 void ServiceRegistry::SetServiceConnector(ServiceConnector* connector) { | 50 void ServiceRegistry::SetServiceConnector(ServiceConnector* connector) { |
42 service_connector_registry_.set_service_connector(connector); | 51 service_connector_registry_.set_service_connector(connector); |
43 } | 52 } |
44 | 53 |
45 bool ServiceRegistry::SetServiceConnectorForName( | 54 bool ServiceRegistry::SetServiceConnectorForName( |
46 ServiceConnector* service_connector, | 55 ServiceConnector* service_connector, |
47 const std::string& interface_name) { | 56 const std::string& interface_name) { |
48 if (allow_all_interfaces_ || | 57 if (allow_all_interfaces_ || |
49 allowed_interfaces_.count(interface_name)) { | 58 allowed_interfaces_.count(interface_name)) { |
50 service_connector_registry_.SetServiceConnectorForName(service_connector, | 59 service_connector_registry_.SetServiceConnectorForName(service_connector, |
51 interface_name); | 60 interface_name); |
52 return true; | 61 return true; |
53 } | 62 } |
54 LOG(WARNING) << "CapabilityFilter prevented connection to interface: " << | 63 LOG(WARNING) << "CapabilityFilter prevented connection to interface: " << |
55 interface_name; | 64 interface_name; |
56 return false; | 65 return false; |
57 } | 66 } |
58 | 67 |
59 ServiceProvider* ServiceRegistry::GetLocalServiceProvider() { | 68 ServiceProvider* ServiceRegistry::GetLocalServiceProvider() { |
60 return this; | 69 return this; |
61 } | 70 } |
62 | 71 |
63 void ServiceRegistry::SetRemoteServiceProviderConnectionErrorHandler( | 72 void ServiceRegistry::SetRemoteServiceProviderConnectionErrorHandler( |
64 const Closure& handler) { | 73 const Closure& handler) { |
65 remote_service_provider_.set_connection_error_handler(handler); | 74 remote_service_provider_.set_connection_error_handler(handler); |
66 } | 75 } |
67 | 76 |
| 77 bool ServiceRegistry::GetContentHandlerID(uint32_t* content_handler_id) { |
| 78 if (!is_content_handler_id_valid_) |
| 79 return false; |
| 80 |
| 81 *content_handler_id = content_handler_id_; |
| 82 return true; |
| 83 } |
| 84 |
| 85 void ServiceRegistry::AddContentHandlerIDCallback(const Closure& callback) { |
| 86 if (is_content_handler_id_valid_) { |
| 87 callback.Run(); |
| 88 return; |
| 89 } |
| 90 content_handler_id_callbacks_.push_back(callback); |
| 91 } |
| 92 |
68 base::WeakPtr<ApplicationConnection> ServiceRegistry::GetWeakPtr() { | 93 base::WeakPtr<ApplicationConnection> ServiceRegistry::GetWeakPtr() { |
69 return weak_factory_.GetWeakPtr(); | 94 return weak_factory_.GetWeakPtr(); |
70 } | 95 } |
71 | 96 |
72 void ServiceRegistry::RemoveServiceConnectorForName( | 97 void ServiceRegistry::RemoveServiceConnectorForName( |
73 const std::string& interface_name) { | 98 const std::string& interface_name) { |
74 service_connector_registry_.RemoveServiceConnectorForName(interface_name); | 99 service_connector_registry_.RemoveServiceConnectorForName(interface_name); |
75 if (service_connector_registry_.empty()) | 100 if (service_connector_registry_.empty()) |
76 remote_service_provider_.reset(); | 101 remote_service_provider_.reset(); |
77 } | 102 } |
78 | 103 |
79 const std::string& ServiceRegistry::GetConnectionURL() { | 104 const std::string& ServiceRegistry::GetConnectionURL() { |
80 return connection_url_; | 105 return connection_url_; |
81 } | 106 } |
82 | 107 |
83 const std::string& ServiceRegistry::GetRemoteApplicationURL() { | 108 const std::string& ServiceRegistry::GetRemoteApplicationURL() { |
84 return remote_url_; | 109 return remote_url_; |
85 } | 110 } |
86 | 111 |
87 ServiceProvider* ServiceRegistry::GetServiceProvider() { | 112 ServiceProvider* ServiceRegistry::GetServiceProvider() { |
88 return remote_service_provider_.get(); | 113 return remote_service_provider_.get(); |
89 } | 114 } |
90 | 115 |
| 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) |
| 123 callback.Run(); |
| 124 } |
| 125 |
91 void ServiceRegistry::ConnectToService(const mojo::String& service_name, | 126 void ServiceRegistry::ConnectToService(const mojo::String& service_name, |
92 ScopedMessagePipeHandle client_handle) { | 127 ScopedMessagePipeHandle client_handle) { |
93 service_connector_registry_.ConnectToService(this, service_name, | 128 service_connector_registry_.ConnectToService(this, service_name, |
94 client_handle.Pass()); | 129 client_handle.Pass()); |
95 } | 130 } |
96 | 131 |
97 } // namespace internal | 132 } // namespace internal |
98 } // namespace mojo | 133 } // namespace mojo |
OLD | NEW |