Use UintToString() for unsigned values.
[chromium-blink-merge.git] / ipc / mojo / ipc_mojo_bootstrap.h
blobf22cedce766799334183b6f0f2929c29dc84c159
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"
14 namespace IPC {
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.
26 // This lives on IO thread other than Create(), which can be called from
27 // UI thread as Channel::Create() can be.
28 class IPC_MOJO_EXPORT MojoBootstrap : public Listener {
29 public:
30 class Delegate {
31 public:
32 virtual void OnPipeAvailable(
33 mojo::embedder::ScopedPlatformHandle handle) = 0;
34 virtual void OnBootstrapError() = 0;
37 // Create the MojoBootstrap instance.
38 // Instead of creating IPC::Channel, passs its ChannelHandle as |handle|,
39 // mode as |mode|. The result is notified to passed |delegate|.
40 static scoped_ptr<MojoBootstrap> Create(ChannelHandle handle,
41 Channel::Mode mode,
42 Delegate* delegate,
43 AttachmentBroker* broker);
45 MojoBootstrap();
46 ~MojoBootstrap() override;
48 // Start the handshake over the underlying platform channel.
49 bool Connect();
51 // GetSelfPID returns the PID associated with |channel_|.
52 base::ProcessId GetSelfPID() const;
54 #if defined(OS_POSIX) && !defined(OS_NACL)
55 int GetClientFileDescriptor() const;
56 base::ScopedFD TakeClientFileDescriptor();
57 #endif // defined(OS_POSIX) && !defined(OS_NACL)
59 protected:
60 // On MojoServerBootstrap: INITIALIZED -> WAITING_ACK -> READY
61 // On MojoClientBootstrap: INITIALIZED -> READY
62 // STATE_ERROR is a catch-all state that captures any observed error.
63 enum State { STATE_INITIALIZED, STATE_WAITING_ACK, STATE_READY, STATE_ERROR };
65 Delegate* delegate() const { return delegate_; }
66 bool Send(Message* message);
67 void Fail();
68 bool HasFailed() const;
70 State state() const { return state_; }
71 void set_state(State state) { state_ = state; }
73 private:
74 void Init(scoped_ptr<Channel> channel, Delegate* delegate);
76 // Listener implementations
77 void OnBadMessageReceived(const Message& message) override;
78 void OnChannelError() override;
80 scoped_ptr<Channel> channel_;
81 Delegate* delegate_;
82 State state_;
84 DISALLOW_COPY_AND_ASSIGN(MojoBootstrap);
87 } // namespace IPC
89 #endif // IPC_MOJO_IPC_MOJO_BOOTSTRAP_H_