summaryrefslogtreecommitdiffstats
path: root/content/common/mojo/mojo_shell_connection_impl.h
blob: d85fe0e45a7d8471407f696fee7df0d2bf1a2a3d (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
// 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_COMMON_MOJO_SHELL_CONNECTION_IMPL_H_
#define CONTENT_COMMON_MOJO_SHELL_CONNECTION_IMPL_H_

#include <vector>

#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "content/public/common/mojo_shell_connection.h"
#include "mojo/public/cpp/system/message_pipe.h"
#include "mojo/shell/public/cpp/application_impl.h"
#include "mojo/shell/public/cpp/shell_client.h"

namespace mojo {
namespace shell {
class RunnerConnection;
}
}

namespace content {

// Returns true for processes launched from an external mojo shell.
bool IsRunningInMojoShell();

class MojoShellConnectionImpl : public MojoShellConnection,
                                public mojo::ShellClient {
 public:
  // Creates an instance of this class and stuffs it in TLS on the calling
  // thread. Retrieve it using MojoShellConnection::Get().
  static void Create();

  // 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 MojoShellConnectionImpl* Get();

  // Blocks the calling thread until calling GetApplication() will return an
  // Initialized() application with a bound ShellPtr. This call is a no-op
  // if the connection has already been initialized.
  void BindToCommandLinePlatformChannel();

  // Same as BindToCommandLinePlatformChannel(), but receives a |handle| instead
  // of looking for one on the command line.
  void BindToMessagePipe(mojo::ScopedMessagePipeHandle handle);

 private:
  MojoShellConnectionImpl();
  ~MojoShellConnectionImpl() override;

  // mojo::ShellClient:
  void Initialize(mojo::Shell* shell, const std::string& url,
                  uint32_t id) override;
  bool AcceptConnection(mojo::Connection* connection) override;

  // MojoShellConnection:
  mojo::Shell* GetShell() override;
  void AddListener(Listener* listener) override;
  void RemoveListener(Listener* listener) override;

  // Blocks the calling thread until a connection to the spawning shell is
  // established, an Application request from it is bound, and the Initialize()
  // method on that application is called.
  void WaitForShell(mojo::ScopedMessagePipeHandle handle);

  bool initialized_;
  scoped_ptr<mojo::shell::RunnerConnection> runner_connection_;
  scoped_ptr<mojo::ApplicationImpl> application_impl_;
  std::vector<Listener*> listeners_;

  DISALLOW_COPY_AND_ASSIGN(MojoShellConnectionImpl);
};

}  // namespace content

#endif  // CONTENT_COMMON_MOJO_SHELL_CONNECTION_IMPL_H_