summaryrefslogtreecommitdiffstats
path: root/mojo/shell/public/cpp/lib/connection_impl.h
blob: 86ca3a73c4dfbee3f1585b1e7e150cb7c267f56c (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
// 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.

#ifndef MOJO_SHELL_PUBLIC_CPP_LIB_CONNECTION_IMPL_H_
#define MOJO_SHELL_PUBLIC_CPP_LIB_CONNECTION_IMPL_H_

#include <stdint.h>

#include <set>
#include <string>

#include "mojo/public/cpp/bindings/binding.h"
#include "mojo/shell/public/cpp/connection.h"
#include "mojo/shell/public/interfaces/interface_provider.mojom.h"
#include "mojo/shell/public/interfaces/shell.mojom.h"

namespace mojo {
namespace internal {

// A ConnectionImpl represents each half of a connection between two
// applications, allowing customization of which interfaces are published to the
// other.
class ConnectionImpl : public Connection,
                       public shell::mojom::InterfaceProvider {
 public:
  class TestApi {
  public:
    explicit TestApi(ConnectionImpl* impl) : impl_(impl) {}
    ~TestApi() {}

    void SetInterfaceBinderForName(InterfaceBinder* binder,
                                   const std::string& interface_name) {
      impl_->SetInterfaceBinderForName(binder, interface_name);
    }
    void RemoveInterfaceBinderForName(const std::string& interface_name) {
      impl_->RemoveInterfaceBinderForName(interface_name);
    }

  private:
    ConnectionImpl* impl_;
    DISALLOW_COPY_AND_ASSIGN(TestApi);
  };

  ConnectionImpl();
  // |allowed_interfaces| are the set of interfaces that the shell has allowed
  // an application to expose to another application. If this set contains only
  // the string value "*" all interfaces may be exposed.
  ConnectionImpl(const std::string& connection_url,
                 const std::string& remote_url,
                 uint32_t remote_id,
                 shell::mojom::InterfaceProviderPtr remote_interfaces,
                 shell::mojom::InterfaceProviderRequest local_interfaces,
                 const std::set<std::string>& allowed_interfaces);
  ~ConnectionImpl() override;

  shell::mojom::Shell::ConnectToApplicationCallback
      GetConnectToApplicationCallback();

 private:
  using NameToInterfaceBinderMap = std::map<std::string, InterfaceBinder*>;

  // Connection:
  void SetDefaultInterfaceBinder(InterfaceBinder* binder) override;
  bool SetInterfaceBinderForName(InterfaceBinder* binder,
                                 const std::string& interface_name) override;
  const std::string& GetConnectionURL() override;
  const std::string& GetRemoteApplicationURL() override;
  shell::mojom::InterfaceProvider* GetRemoteInterfaces() override;
  shell::mojom::InterfaceProvider* GetLocalInterfaces() override;
  void SetRemoteInterfaceProviderConnectionErrorHandler(
      const Closure& handler) override;
  bool GetRemoteApplicationID(uint32_t* remote_id) const override;
  bool GetRemoteContentHandlerID(uint32_t* content_handler_id) const override;
  void AddRemoteIDCallback(const Closure& callback) override;
  base::WeakPtr<Connection> GetWeakPtr() override;

  // InterfaceProvider:
  void GetInterface(const mojo::String& interface_name,
                    ScopedMessagePipeHandle handle) override;

  void RemoveInterfaceBinderForName(const std::string& interface_name);
  void OnGotRemoteIDs(uint32_t target_application_id,
                      uint32_t content_handler_id);

  const std::string connection_url_;
  const std::string remote_url_;

  uint32_t remote_id_;
  // The id of the content_handler is only available once the callback from
  // establishing the connection is made.
  uint32_t content_handler_id_;
  bool remote_ids_valid_;
  std::vector<Closure> remote_id_callbacks_;

  Binding<shell::mojom::InterfaceProvider> local_binding_;
  shell::mojom::InterfaceProviderPtr remote_interfaces_;

  const std::set<std::string> allowed_interfaces_;
  const bool allow_all_interfaces_;

  InterfaceBinder* default_binder_;
  NameToInterfaceBinderMap name_to_binder_;

  base::WeakPtrFactory<ConnectionImpl> weak_factory_;

  MOJO_DISALLOW_COPY_AND_ASSIGN(ConnectionImpl);
};

}  // namespace internal
}  // namespace mojo

#endif  // MOJO_SHELL_PUBLIC_CPP_LIB_CONNECTION_IMPL_H_