summaryrefslogtreecommitdiffstats
path: root/mojo/shell/public/cpp/connection.h
blob: 1825d9b4ebceffb43b00ca2a044b1d8b267580b3 (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
// 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_CONNECTION_H_
#define MOJO_SHELL_PUBLIC_CPP_CONNECTION_H_

#include <stdint.h>

#include <string>
#include <utility>

#include "base/memory/weak_ptr.h"
#include "mojo/shell/public/cpp/connect.h"
#include "mojo/shell/public/cpp/interface_registry.h"
#include "mojo/shell/public/interfaces/interface_provider.mojom.h"

namespace mojo {

class InterfaceBinder;

// Represents a connection to another application. An instance of this class is
// returned from Shell's ConnectToApplication(), and passed to ShellClient's
// AcceptConnection() each time an incoming connection is received.
//
// Call AddService<T>(factory) to expose an interface to the remote application,
// and GetInterface(&interface_ptr) to consume an interface exposed by the
// remote application.
//
// Internally, this class wraps an InterfaceRegistry that accepts interfaces
// that may be exposed to a remote application. See documentation in
// interface_registry.h for more information.
//
// A Connection returned via Shell::ConnectToApplication() is owned by the
// caller.
// An Connection received via AcceptConnection is owned by the ShellConnection.
// To close a connection, call CloseConnection which will destroy this object.
class Connection {
 public:
  virtual ~Connection() {}

  class TestApi {
   public:
    explicit TestApi(Connection* connection) : connection_(connection) {}
    base::WeakPtr<Connection> GetWeakPtr() {
      return connection_->GetWeakPtr();
    }

   private:
    Connection* connection_;
  };

  // Allow the remote application to request instances of Interface.
  // |factory| will create implementations of Interface on demand.
  // Returns true if the interface was exposed, false if capability filtering
  // from the shell prevented the interface from being exposed.
  template <typename Interface>
  bool AddInterface(InterfaceFactory<Interface>* factory) {
    return GetLocalRegistry()->AddInterface<Interface>(factory);
  }

  // Binds |ptr| to an implemention of Interface in the remote application.
  // |ptr| can immediately be used to start sending requests to the remote
  // interface.
  template <typename Interface>
  void GetInterface(InterfacePtr<Interface>* ptr) {
    mojo::GetInterface(GetRemoteInterfaces(), ptr);
  }

  // Returns the name that was used by the source application to establish a
  // connection to the destination application.
  //
  // When Connection is representing and outgoing connection, this will be the
  // same as the value returned by GetRemoveApplicationName().
  virtual const std::string& GetConnectionName() = 0;

  // Returns the name identifying the remote application on this connection.
  virtual const std::string& GetRemoteApplicationName() = 0;

  // Returns the User ID for the remote application. Prior to the Connect()
  // callback being fired, this will return the value passed via Connect().
  // After the Connect() callback (call AddRemoteIDCallback to register one)
  // this will return the actual user id the shell ran the target as.
  virtual uint32_t GetRemoteUserID() const = 0;

  // Register a handler to receive an error notification on the pipe to the
  // remote application's InterfaceProvider.
  virtual void SetRemoteInterfaceProviderConnectionErrorHandler(
      const Closure& handler) = 0;

  // Returns the id of the remote application. For Connections created via
  // Shell::Connect(), this will not be determined until Connect()'s callback is
  // run, and this function will return false. Use AddRemoteIDCallback() to
  // schedule a callback to be run when the remote application id is available.
  // A value of Shell::kInvalidApplicationID indicates the connection has not
  // been established.
  virtual bool GetRemoteApplicationID(uint32_t* remote_id) const = 0;

  // See description in GetRemoteApplicationID()/
  // GetRemoteShellClientFactoryID(). If the ids are available, |callback| is
  // run immediately.
  virtual void AddRemoteIDCallback(const Closure& callback) = 0;

  // Returns true if the Shell allows |interface_name| to be exposed to the
  // remote application.
  virtual bool AllowsInterface(const std::string& interface_name) const = 0;

  // Returns the raw proxy to the remote application's InterfaceProvider
  // interface. Most applications will just use GetInterface() instead.
  // Caller does not take ownership.
  virtual shell::mojom::InterfaceProvider* GetRemoteInterfaces() = 0;

 protected:
  virtual InterfaceRegistry* GetLocalRegistry() = 0;

  virtual base::WeakPtr<Connection> GetWeakPtr() = 0;
};

}  // namespace mojo

#endif  // MOJO_SHELL_PUBLIC_CPP_CONNECTION_H_