summaryrefslogtreecommitdiffstats
path: root/mojo/shell/public/cpp/lib/interface_registry.cc
blob: c8f9703b6c077ce5792333ff398ec5fbf681c153 (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
// Copyright 2016 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/interface_registry.h"

#include "mojo/shell/public/cpp/connection.h"

namespace mojo {

InterfaceRegistry::InterfaceRegistry(Connection* connection)
    : InterfaceRegistry(GetProxy(&client_handle_), connection) {}

InterfaceRegistry::InterfaceRegistry(
    shell::mojom::InterfaceProviderRequest request,
    Connection* connection)
    : binding_(this),
      connection_(connection),
      default_binder_(nullptr) {
  if (request.is_pending())
    binding_.Bind(std::move(request));
}

InterfaceRegistry::~InterfaceRegistry() {
  for (auto& i : name_to_binder_)
    delete i.second;
  name_to_binder_.clear();
}

shell::mojom::InterfaceProviderPtr InterfaceRegistry::TakeClientHandle() {
  return std::move(client_handle_);
}

// shell::mojom::InterfaceProvider:
void InterfaceRegistry::GetInterface(const String& interface_name,
                                     ScopedMessagePipeHandle handle) {
  auto iter = name_to_binder_.find(interface_name);
  InterfaceBinder* binder = iter != name_to_binder_.end() ? iter->second :
      default_binder_;
  if (binder)
    binder->BindInterface(connection_, interface_name, std::move(handle));
}

bool InterfaceRegistry::SetInterfaceBinderForName(
    InterfaceBinder* binder,
    const std::string& interface_name) {
  if (!connection_ ||
      (connection_ && connection_->AllowsInterface(interface_name))) {
    RemoveInterfaceBinderForName(interface_name);
    name_to_binder_[interface_name] = binder;
    return true;
  }
  LOG(WARNING) << "Connection CapabilityFilter prevented binding to interface: "
               << interface_name << " connection_name:"
               << connection_->GetConnectionName() << " remote_name:"
               << connection_->GetRemoteIdentity().name();
  return false;
}

void InterfaceRegistry::RemoveInterfaceBinderForName(
    const std::string& interface_name) {
  NameToInterfaceBinderMap::iterator it = name_to_binder_.find(interface_name);
  if (it == name_to_binder_.end())
    return;
  delete it->second;
  name_to_binder_.erase(it);
}

}  // namespace mojo