summaryrefslogtreecommitdiffstats
path: root/content/public/common/mojo_shell_connection.h
blob: 6ae92bbe45fa5d600e6d2c46db608d1b0a2d588f (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
// Copyright 2015 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 CONTENT_PUBLIC_COMMON_MOJO_SHELL_CONNECTION_H_
#define CONTENT_PUBLIC_COMMON_MOJO_SHELL_CONNECTION_H_

#include "base/callback_forward.h"
#include "content/common/content_export.h"
#include "mojo/shell/public/interfaces/shell_client.mojom.h"

namespace mojo {
class Connection;
class Connector;
}

namespace content {

// Encapsulates a connection to a spawning external Mojo shell.
// Access an instance by calling Get(), on the thread the Shell connection is
// bound. Clients can implement Listener, which allows them to register services
// to expose to inbound connections. Clients should call this any time after
// the main message loop is created but not yet run (e.g. in the browser process
// this object is created in PreMainMessageLoopRun(), so the BrowserMainParts
// impl can access this in its implementation of that same method.
class CONTENT_EXPORT MojoShellConnection {
 public:
  // Override to add additional services to inbound connections.
  // TODO(beng): This should just be ShellClient.
  class Listener {
   public:
    virtual bool AcceptConnection(mojo::Connection* connection) = 0;

    virtual ~Listener() {}
  };

  using Factory = base::Closure;
  // Sets the factory used to create the MojoShellConnection. This must be
  // called before the MojoShellConnection has been created.
  static void SetFactoryForTest(Factory* factory);

  // Will return null if no connection has been established (either because it
  // hasn't happened yet or the application was not spawned from the external
  // Mojo shell.
  static MojoShellConnection* Get();

  // Destroys the connection. Must be called on the thread the connection was
  // created on.
  static void Destroy();

  // Creates the appropriate MojoShellConnection from |request|. See
  // UsingExternalShell() for details of |is_external|.
  static void Create(mojo::shell::mojom::ShellClientRequest request,
                     bool is_external);

  virtual mojo::Connector* GetConnector() = 0;

  // Indicates whether the shell connection is to an external shell (true) or
  // a shell embedded in the browser process (false).
  virtual bool UsingExternalShell() const = 0;

  // Sets a closure that is called when the connection is lost.
  virtual void SetConnectionLostClosure(const base::Closure& closure) = 0;

  // [De]Register an impl of Listener that will be consulted when the wrapped
  // ShellConnection exposes services to inbound connections.
  // Registered listeners are owned by this MojoShellConnection.
  virtual void AddListener(Listener* listener) = 0;
  virtual void RemoveListener(Listener* listener) = 0;

 protected:
  virtual ~MojoShellConnection();
};

}  // namespace content

#endif  // CONTENT_PUBLIC_COMMON_MOJO_SHELL_CONNECTION_H_