OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef IPC_IPC_MOJO_BOOTSTRAP_H_ | 5 #ifndef IPC_IPC_MOJO_BOOTSTRAP_H_ |
6 #define IPC_IPC_MOJO_BOOTSTRAP_H_ | 6 #define IPC_IPC_MOJO_BOOTSTRAP_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 | 11 |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/process/process_handle.h" | |
15 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
16 #include "build/build_config.h" | 15 #include "build/build_config.h" |
17 #include "ipc/ipc.mojom.h" | 16 #include "ipc/ipc.mojom.h" |
18 #include "ipc/ipc_channel.h" | 17 #include "ipc/ipc_channel.h" |
19 #include "ipc/ipc_listener.h" | 18 #include "ipc/ipc_listener.h" |
20 #include "mojo/public/cpp/bindings/associated_group.h" | 19 #include "mojo/public/cpp/bindings/associated_group.h" |
21 #include "mojo/public/cpp/system/message_pipe.h" | 20 #include "mojo/public/cpp/system/message_pipe.h" |
22 | 21 |
23 namespace IPC { | 22 namespace IPC { |
24 | 23 |
25 // MojoBootstrap establishes a pair of associated interfaces between two | 24 // MojoBootstrap establishes a pair of associated interfaces between two |
26 // processes in Chrome. | 25 // processes in Chrome. |
27 // | 26 // |
28 // Clients should implement MojoBootstrap::Delegate to get the associated pipes | 27 // Clients should implement MojoBootstrap::Delegate to get the associated pipes |
29 // from MojoBootstrap object. | 28 // from MojoBootstrap object. |
30 // | 29 // |
31 // This lives on IO thread other than Create(), which can be called from | 30 // This lives on IO thread other than Create(), which can be called from |
32 // UI thread as Channel::Create() can be. | 31 // UI thread as Channel::Create() can be. |
33 class IPC_EXPORT MojoBootstrap { | 32 class IPC_EXPORT MojoBootstrap { |
34 public: | 33 public: |
35 class Delegate { | 34 class Delegate { |
36 public: | 35 public: |
37 virtual void OnPipesAvailable( | 36 virtual ~Delegate() {} |
38 mojom::ChannelAssociatedPtrInfo send_channel, | 37 |
39 mojom::ChannelAssociatedRequest receive_channel, | 38 virtual void OnPipesAvailable(mojom::ChannelAssociatedPtr sender, |
40 int32_t peer_pid) = 0; | 39 mojom::ChannelAssociatedRequest receiver) = 0; |
41 virtual void OnBootstrapError() = 0; | |
42 }; | 40 }; |
43 | 41 |
| 42 virtual ~MojoBootstrap() {} |
| 43 |
44 // Create the MojoBootstrap instance, using |handle| as the message pipe, in | 44 // Create the MojoBootstrap instance, using |handle| as the message pipe, in |
45 // mode as specified by |mode|. The result is passed to |delegate|. | 45 // mode as specified by |mode|. The result is passed to |delegate|. |
46 static std::unique_ptr<MojoBootstrap> Create( | 46 static std::unique_ptr<MojoBootstrap> Create( |
47 mojo::ScopedMessagePipeHandle handle, | 47 mojo::ScopedMessagePipeHandle handle, |
48 Channel::Mode mode, | 48 Channel::Mode mode, |
49 Delegate* delegate); | 49 Delegate* delegate, |
50 | 50 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner); |
51 MojoBootstrap(); | |
52 virtual ~MojoBootstrap(); | |
53 | 51 |
54 // Start the handshake over the underlying message pipe. | 52 // Start the handshake over the underlying message pipe. |
55 virtual void Connect() = 0; | 53 virtual void Connect() = 0; |
56 | 54 |
57 virtual mojo::AssociatedGroup* GetAssociatedGroup() = 0; | 55 virtual mojo::AssociatedGroup* GetAssociatedGroup() = 0; |
58 | |
59 virtual void SetProxyTaskRunner( | |
60 scoped_refptr<base::SingleThreadTaskRunner> task_runner) = 0; | |
61 | |
62 // GetSelfPID returns our PID. | |
63 base::ProcessId GetSelfPID() const; | |
64 | |
65 protected: | |
66 // On MojoServerBootstrap: INITIALIZED -> WAITING_ACK -> READY | |
67 // On MojoClientBootstrap: INITIALIZED -> READY | |
68 // STATE_ERROR is a catch-all state that captures any observed error. | |
69 enum State { STATE_INITIALIZED, STATE_WAITING_ACK, STATE_READY, STATE_ERROR }; | |
70 | |
71 Delegate* delegate() const { return delegate_; } | |
72 void Fail(); | |
73 bool HasFailed() const; | |
74 | |
75 State state() const { return state_; } | |
76 void set_state(State state) { state_ = state; } | |
77 | |
78 mojo::ScopedMessagePipeHandle TakeHandle(); | |
79 | |
80 private: | |
81 void Init(mojo::ScopedMessagePipeHandle, Delegate* delegate); | |
82 | |
83 mojo::ScopedMessagePipeHandle handle_; | |
84 Delegate* delegate_; | |
85 State state_; | |
86 | |
87 DISALLOW_COPY_AND_ASSIGN(MojoBootstrap); | |
88 }; | 56 }; |
89 | 57 |
90 } // namespace IPC | 58 } // namespace IPC |
91 | 59 |
92 #endif // IPC_IPC_MOJO_BOOTSTRAP_H_ | 60 #endif // IPC_IPC_MOJO_BOOTSTRAP_H_ |
OLD | NEW |