summaryrefslogtreecommitdiffstats
path: root/mojo/shell/public/cpp/lib/service_registry.cc
blob: 1425276dc16f75efaf889fc8bc5c0ac971822dac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "mojo/shell/public/cpp/lib/service_registry.h"

#include <stdint.h>

#include <utility>

#include "base/bind.h"
#include "base/logging.h"
#include "mojo/shell/public/cpp/application_connection.h"
#include "mojo/shell/public/cpp/service_connector.h"

namespace mojo {
namespace internal {

ServiceRegistry::ServiceRegistry(
    const std::string& connection_url,
    const std::string& remote_url,
    uint32_t remote_id,
    ServiceProviderPtr remote_services,
    InterfaceRequest<ServiceProvider> local_services,
    const std::set<std::string>& allowed_interfaces)
    : connection_url_(connection_url),
      remote_url_(remote_url),
      local_binding_(this),
      remote_service_provider_(std::move(remote_services)),
      allowed_interfaces_(allowed_interfaces),
      allow_all_interfaces_(allowed_interfaces_.size() == 1 &&
                            allowed_interfaces_.count("*") == 1),
      remote_id_(remote_id),
      content_handler_id_(0u),
      remote_ids_valid_(false),
      weak_factory_(this) {
  if (local_services.is_pending())
    local_binding_.Bind(std::move(local_services));
}

ServiceRegistry::ServiceRegistry()
    : local_binding_(this),
      allow_all_interfaces_(true),
      weak_factory_(this) {
}

ServiceRegistry::~ServiceRegistry() {
}

shell::mojom::Shell::ConnectToApplicationCallback
ServiceRegistry::GetConnectToApplicationCallback() {
  return base::Bind(&ServiceRegistry::OnGotRemoteIDs,
                    weak_factory_.GetWeakPtr());
}

void ServiceRegistry::SetServiceConnector(ServiceConnector* connector) {
  service_connector_registry_.set_service_connector(connector);
}

bool ServiceRegistry::SetServiceConnectorForName(
    ServiceConnector* service_connector,
    const std::string& interface_name) {
  if (allow_all_interfaces_ ||
      allowed_interfaces_.count(interface_name)) {
    service_connector_registry_.SetServiceConnectorForName(service_connector,
                                                           interface_name);
    return true;
  }
  LOG(WARNING) << "CapabilityFilter prevented connection to interface: "
               << interface_name << " connection_url:" << connection_url_
               << " remote_url:" << remote_url_;
  return false;
}

ServiceProvider* ServiceRegistry::GetLocalServiceProvider() {
  return this;
}

void ServiceRegistry::SetRemoteServiceProviderConnectionErrorHandler(
    const Closure& handler) {
  remote_service_provider_.set_connection_error_handler(handler);
}

bool ServiceRegistry::GetRemoteApplicationID(uint32_t* remote_id) const {
  if (!remote_ids_valid_)
    return false;

  *remote_id = remote_id_;
  return true;
}

bool ServiceRegistry::GetRemoteContentHandlerID(
    uint32_t* content_handler_id) const {
  if (!remote_ids_valid_)
    return false;

  *content_handler_id = content_handler_id_;
  return true;
}

void ServiceRegistry::AddRemoteIDCallback(const Closure& callback) {
  if (remote_ids_valid_) {
    callback.Run();
    return;
  }
  remote_id_callbacks_.push_back(callback);
}

base::WeakPtr<ApplicationConnection> ServiceRegistry::GetWeakPtr() {
  return weak_factory_.GetWeakPtr();
}

void ServiceRegistry::RemoveServiceConnectorForName(
    const std::string& interface_name) {
  service_connector_registry_.RemoveServiceConnectorForName(interface_name);
  if (service_connector_registry_.empty())
    remote_service_provider_.reset();
}

const std::string& ServiceRegistry::GetConnectionURL() {
  return connection_url_;
}

const std::string& ServiceRegistry::GetRemoteApplicationURL() {
  return remote_url_;
}

ServiceProvider* ServiceRegistry::GetServiceProvider() {
  return remote_service_provider_.get();
}

void ServiceRegistry::OnGotRemoteIDs(uint32_t target_application_id,
                                     uint32_t content_handler_id) {
  DCHECK(!remote_ids_valid_);
  remote_ids_valid_ = true;

  remote_id_ = target_application_id;
  content_handler_id_ = content_handler_id;
  std::vector<Closure> callbacks;
  callbacks.swap(remote_id_callbacks_);
  for (auto callback : callbacks)
    callback.Run();
}

void ServiceRegistry::ConnectToService(const mojo::String& service_name,
                                       ScopedMessagePipeHandle client_handle) {
  service_connector_registry_.ConnectToService(this, service_name,
                                               std::move(client_handle));
}

}  // namespace internal
}  // namespace mojo