Delete historical data usage from store when user clears data usage.
[chromium-blink-merge.git] / mojo / application / public / cpp / lib / service_registry.cc
blob7edf8d6c1c821dd9b1b9c60d2e9cdd5d773e8f36
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"
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "mojo/application/public/cpp/application_connection.h"
10 #include "mojo/application/public/cpp/service_connector.h"
12 namespace mojo {
13 namespace internal {
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),
23 local_binding_(this),
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),
30 weak_factory_(this) {
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),
38 weak_factory_(this) {
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,
60 interface_name);
61 return true;
63 LOG(WARNING) << "CapabilityFilter prevented connection to interface: " <<
64 interface_name;
65 return false;
68 ServiceProvider* ServiceRegistry::GetLocalServiceProvider() {
69 return this;
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_)
79 return false;
81 *content_handler_id = content_handler_id_;
82 return true;
85 void ServiceRegistry::AddContentHandlerIDCallback(const Closure& callback) {
86 if (is_content_handler_id_valid_) {
87 callback.Run();
88 return;
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() {
109 return remote_url_;
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)
123 callback.Run();
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
133 } // namespace mojo