OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #ifndef IPC_MOJO_IPC_MOJO_BOOTSTRAP_H_ | |
6 #define IPC_MOJO_IPC_MOJO_BOOTSTRAP_H_ | |
7 | |
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 "ipc/ipc_listener.h" | |
13 #include "ipc/mojo/ipc_mojo_bootstrap_delegate.h" | |
14 #include "mojo/embedder/scoped_platform_handle.h" | |
15 | |
16 namespace IPC { | |
17 | |
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. | |
22 // | |
23 // Clients should implement MojoBootstrapDelegate to get the pipe | |
24 // from MojoBootstrap object. It should also tell the client process handle | |
25 // using OnClientLaunched(). | |
26 class IPC_MOJO_EXPORT MojoBootstrap : public Listener { | |
27 public: | |
28 typedef MojoBootstrapDelegate Delegate; | |
29 | |
30 // Create the MojoBootstrap instance. | |
31 // Instead of creating IPC::Channel, passs its ChannelHandle as |handle|, | |
32 // mode as |mode|. The result is notified to passed |delegate|. | |
33 static scoped_ptr<MojoBootstrap> Create(ChannelHandle handle, | |
34 Channel::Mode mode, | |
35 Delegate* delegate); | |
36 | |
37 explicit MojoBootstrap(); | |
viettrungluu
2014/09/15 20:19:44
nit: no need for explicit
Hajime Morrita
2014/09/15 22:01:37
Done.
| |
38 virtual ~MojoBootstrap(); | |
39 | |
40 // Start the handshake over the underlying platform channel. | |
41 bool Connect(); | |
42 | |
43 // Each client should call this once the process handle becomes known. | |
44 virtual void OnClientLaunched(base::ProcessHandle process) = 0; | |
45 | |
46 #if defined(OS_POSIX) && !defined(OS_NACL) | |
47 int GetClientFileDescriptor() const; | |
48 int TakeClientFileDescriptor(); | |
49 #endif // defined(OS_POSIX) && !defined(OS_NACL) | |
50 | |
51 protected: | |
52 enum State { STATE_INITIALIZED, STATE_WAITING_ACK, STATE_READY }; | |
53 | |
54 Delegate* delegate() const { return delegate_; } | |
55 bool Send(Message* message); | |
56 | |
57 State state() const { return state_; } | |
58 void set_state(State state) { state_ = state; } | |
59 | |
60 private: | |
61 void Init(scoped_ptr<Channel> channel, Delegate* delegate); | |
62 | |
63 // Listener implementations | |
64 virtual void OnBadMessageReceived(const Message& message) OVERRIDE; | |
65 virtual void OnChannelError() OVERRIDE; | |
66 | |
67 scoped_ptr<Channel> channel_; | |
68 Delegate* delegate_; | |
69 State state_; | |
70 | |
71 DISALLOW_COPY_AND_ASSIGN(MojoBootstrap); | |
72 }; | |
73 | |
74 // MojoBootstrap for the server process. You should create the instance | |
viettrungluu
2014/09/15 20:19:44
Does this (and MojoClientBootstrap) need to be in
Hajime Morrita
2014/09/15 22:01:37
Good point. Moved to .cc file.
| |
75 // using MojoBootstrap::Create() | |
76 class IPC_MOJO_EXPORT MojoServerBootstrap : public MojoBootstrap { | |
77 public: | |
78 explicit MojoServerBootstrap(); | |
viettrungluu
2014/09/15 20:19:44
no explicit
Hajime Morrita
2014/09/15 22:01:37
Done.
| |
79 | |
80 virtual void OnClientLaunched(base::ProcessHandle process) OVERRIDE; | |
81 | |
82 private: | |
83 void SendClientPipe(); | |
84 void SendClientPipeIfReady(); | |
85 | |
86 // Listener implementations | |
87 virtual bool OnMessageReceived(const Message&) OVERRIDE; | |
viettrungluu
2014/09/15 20:19:44
nit: include the name of the argument ("message",
Hajime Morrita
2014/09/15 22:01:37
Done.
| |
88 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE; | |
89 | |
90 mojo::embedder::ScopedPlatformHandle server_pipe_; | |
91 base::ProcessHandle client_process_; | |
92 bool connected_; | |
93 }; | |
viettrungluu
2014/09/15 20:19:44
nit: DISALLOW_COPY_AND_ASSIGN
Hajime Morrita
2014/09/15 22:01:37
Done.
| |
94 | |
95 // MojoBootstrap for client processes. You should create the instance | |
96 // using MojoBootstrap::Create() | |
97 class IPC_MOJO_EXPORT MojoClientBootstrap : public MojoBootstrap { | |
98 public: | |
99 explicit MojoClientBootstrap(); | |
viettrungluu
2014/09/15 20:19:44
no explicit
Hajime Morrita
2014/09/15 22:01:37
Done.
| |
100 | |
101 virtual void OnClientLaunched(base::ProcessHandle process) OVERRIDE; | |
102 | |
103 private: | |
104 // Listener implementations | |
105 virtual bool OnMessageReceived(const Message&) OVERRIDE; | |
viettrungluu
2014/09/15 20:19:44
nit: include the name of the argument ("message",
Hajime Morrita
2014/09/15 22:01:37
Done.
| |
106 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE; | |
107 }; | |
viettrungluu
2014/09/15 20:19:44
nit: DISALLOW_COPY_AND_ASSIGN
Hajime Morrita
2014/09/15 22:01:37
Done.
| |
108 | |
109 } // namespace IPC | |
110 | |
111 #endif // IPC_MOJO_IPC_MOJO_BOOTSTRAP_H_ | |
OLD | NEW |