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 "third_party/mojo/src/mojo/edk/embedder/scoped_platform_handle.h"
16 class AttachmentBroker
;
18 // MojoBootstrap establishes a bootstrap pipe between two processes in
19 // Chrome. It creates a native IPC::Channel first, then sends one
20 // side of a newly created pipe to peer process. The pipe is intended
21 // to be wrapped by Mojo MessagePipe.
23 // Clients should implement MojoBootstrapDelegate to get the pipe
24 // from MojoBootstrap object. It should also tell the client process handle
25 // using OnClientLaunched().
27 // This lives on IO thread other than Create(), which can be called from
28 // UI thread as Channel::Create() can be.
29 class IPC_MOJO_EXPORT MojoBootstrap
: public Listener
{
33 virtual void OnPipeAvailable(
34 mojo::embedder::ScopedPlatformHandle handle
) = 0;
35 virtual void OnBootstrapError() = 0;
38 // Create the MojoBootstrap instance.
39 // Instead of creating IPC::Channel, passs its ChannelHandle as |handle|,
40 // mode as |mode|. The result is notified to passed |delegate|.
41 static scoped_ptr
<MojoBootstrap
> Create(ChannelHandle handle
,
44 AttachmentBroker
* broker
);
47 ~MojoBootstrap() override
;
49 // Start the handshake over the underlying platform channel.
52 // GetSelfPID returns the PID associated with |channel_|.
53 base::ProcessId
GetSelfPID() const;
55 // Each client should call this once the process handle becomes known.
56 virtual void OnClientLaunched(base::ProcessHandle process
) = 0;
58 #if defined(OS_POSIX) && !defined(OS_NACL)
59 int GetClientFileDescriptor() const;
60 base::ScopedFD
TakeClientFileDescriptor();
61 #endif // defined(OS_POSIX) && !defined(OS_NACL)
64 // On MojoServerBootstrap: INITIALIZED -> WAITING_ACK -> READY
65 // On MojoClientBootstrap: INITIALIZED -> READY
66 // STATE_ERROR is a catch-all state that captures any observed error.
67 enum State
{ STATE_INITIALIZED
, STATE_WAITING_ACK
, STATE_READY
, STATE_ERROR
};
69 Delegate
* delegate() const { return delegate_
; }
70 bool Send(Message
* message
);
72 bool HasFailed() const;
74 State
state() const { return state_
; }
75 void set_state(State state
) { state_
= state
; }
78 void Init(scoped_ptr
<Channel
> channel
, Delegate
* delegate
);
80 // Listener implementations
81 void OnBadMessageReceived(const Message
& message
) override
;
82 void OnChannelError() override
;
84 scoped_ptr
<Channel
> channel_
;
88 DISALLOW_COPY_AND_ASSIGN(MojoBootstrap
);
93 #endif // IPC_MOJO_IPC_MOJO_BOOTSTRAP_H_