cc: Refactor RecordingSource
[chromium-blink-merge.git] / ipc / mojo / ipc_mojo_bootstrap.h
blob726755386a84b20df647363eced6686ad9e6e000
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef IPC_MOJO_IPC_MOJO_BOOTSTRAP_H_
6 #define IPC_MOJO_IPC_MOJO_BOOTSTRAP_H_
8 #include "base/memory/scoped_ptr.h"
9 #include "base/process/process_handle.h"
10 #include "ipc/ipc_channel.h"
11 #include "ipc/ipc_listener.h"
12 #include "mojo/edk/embedder/scoped_platform_handle.h"
14 namespace IPC {
16 // MojoBootstrap establishes a bootstrap pipe between two processes in
17 // Chrome. It creates a native IPC::Channel first, then sends one
18 // side of a newly created pipe to peer process. The pipe is intended
19 // to be wrapped by Mojo MessagePipe.
21 // Clients should implement MojoBootstrapDelegate to get the pipe
22 // from MojoBootstrap object. It should also tell the client process handle
23 // using OnClientLaunched().
25 // This lives on IO thread other than Create(), which can be called from
26 // UI thread as Channel::Create() can be.
27 class IPC_MOJO_EXPORT MojoBootstrap : public Listener {
28 public:
29 class Delegate {
30 public:
31 virtual void OnPipeAvailable(
32 mojo::embedder::ScopedPlatformHandle handle) = 0;
33 virtual void OnBootstrapError() = 0;
36 // Create the MojoBootstrap instance.
37 // Instead of creating IPC::Channel, passs its ChannelHandle as |handle|,
38 // mode as |mode|. The result is notified to passed |delegate|.
39 static scoped_ptr<MojoBootstrap> Create(ChannelHandle handle,
40 Channel::Mode mode,
41 Delegate* delegate);
43 MojoBootstrap();
44 ~MojoBootstrap() override;
46 // Start the handshake over the underlying platform channel.
47 bool Connect();
49 // Each client should call this once the process handle becomes known.
50 virtual void OnClientLaunched(base::ProcessHandle process) = 0;
52 #if defined(OS_POSIX) && !defined(OS_NACL)
53 int GetClientFileDescriptor() const;
54 base::ScopedFD TakeClientFileDescriptor();
55 #endif // defined(OS_POSIX) && !defined(OS_NACL)
57 protected:
58 // On MojoServerBootstrap: INITIALIZED -> WAITING_ACK -> READY
59 // On MojoClientBootstrap: INITIALIZED -> READY
60 // STATE_ERROR is a catch-all state that captures any observed error.
61 enum State { STATE_INITIALIZED, STATE_WAITING_ACK, STATE_READY, STATE_ERROR };
63 Delegate* delegate() const { return delegate_; }
64 bool Send(Message* message);
65 void Fail();
66 bool HasFailed() const;
68 State state() const { return state_; }
69 void set_state(State state) { state_ = state; }
71 private:
72 void Init(scoped_ptr<Channel> channel, Delegate* delegate);
74 // Listener implementations
75 void OnBadMessageReceived(const Message& message) override;
76 void OnChannelError() override;
78 scoped_ptr<Channel> channel_;
79 Delegate* delegate_;
80 State state_;
82 DISALLOW_COPY_AND_ASSIGN(MojoBootstrap);
85 } // namespace IPC
87 #endif // IPC_MOJO_IPC_MOJO_BOOTSTRAP_H_