summaryrefslogtreecommitdiffstats
path: root/ipc/mojo/ipc_mojo_bootstrap.h
blob: 726755386a84b20df647363eced6686ad9e6e000 (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
// 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 IPC_MOJO_IPC_MOJO_BOOTSTRAP_H_
#define IPC_MOJO_IPC_MOJO_BOOTSTRAP_H_

#include "base/memory/scoped_ptr.h"
#include "base/process/process_handle.h"
#include "ipc/ipc_channel.h"
#include "ipc/ipc_listener.h"
#include "mojo/edk/embedder/scoped_platform_handle.h"

namespace IPC {

// MojoBootstrap establishes a bootstrap pipe between two processes in
// Chrome. It creates a native IPC::Channel first, then sends one
// side of a newly created pipe to peer process. The pipe is intended
// to be wrapped by Mojo MessagePipe.
//
// Clients should implement MojoBootstrapDelegate to get the pipe
// from MojoBootstrap object. It should also tell the client process handle
// using OnClientLaunched().
//
// This lives on IO thread other than Create(), which can be called from
// UI thread as Channel::Create() can be.
class IPC_MOJO_EXPORT MojoBootstrap : public Listener {
 public:
  class Delegate {
   public:
    virtual void OnPipeAvailable(
        mojo::embedder::ScopedPlatformHandle handle) = 0;
    virtual void OnBootstrapError() = 0;
  };

  // Create the MojoBootstrap instance.
  // Instead of creating IPC::Channel, passs its ChannelHandle as |handle|,
  // mode as |mode|. The result is notified to passed |delegate|.
  static scoped_ptr<MojoBootstrap> Create(ChannelHandle handle,
                                          Channel::Mode mode,
                                          Delegate* delegate);

  MojoBootstrap();
  ~MojoBootstrap() override;

  // Start the handshake over the underlying platform channel.
  bool Connect();

  // Each client should call this once the process handle becomes known.
  virtual void OnClientLaunched(base::ProcessHandle process) = 0;

#if defined(OS_POSIX) && !defined(OS_NACL)
  int GetClientFileDescriptor() const;
  base::ScopedFD TakeClientFileDescriptor();
#endif  // defined(OS_POSIX) && !defined(OS_NACL)

 protected:
  // On MojoServerBootstrap: INITIALIZED -> WAITING_ACK -> READY
  // On MojoClientBootstrap: INITIALIZED -> READY
  // STATE_ERROR is a catch-all state that captures any observed error.
  enum State { STATE_INITIALIZED, STATE_WAITING_ACK, STATE_READY, STATE_ERROR };

  Delegate* delegate() const { return delegate_; }
  bool Send(Message* message);
  void Fail();
  bool HasFailed() const;

  State state() const { return state_; }
  void set_state(State state) { state_ = state; }

 private:
  void Init(scoped_ptr<Channel> channel, Delegate* delegate);

  // Listener implementations
  void OnBadMessageReceived(const Message& message) override;
  void OnChannelError() override;

  scoped_ptr<Channel> channel_;
  Delegate* delegate_;
  State state_;

  DISALLOW_COPY_AND_ASSIGN(MojoBootstrap);
};

}  // namespace IPC

#endif  // IPC_MOJO_IPC_MOJO_BOOTSTRAP_H_