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_IPC_CHANNEL_MOJO_H_ | |
6 #define IPC_IPC_CHANNEL_MOJO_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <memory> | |
11 #include <vector> | |
12 | |
13 #include "base/macros.h" | |
14 #include "base/memory/ref_counted.h" | |
15 #include "base/memory/scoped_vector.h" | |
16 #include "base/memory/weak_ptr.h" | |
17 #include "base/synchronization/lock.h" | |
18 #include "base/task_runner.h" | |
19 #include "build/build_config.h" | |
20 #include "ipc/ipc_channel.h" | |
21 #include "ipc/ipc_channel_factory.h" | |
22 #include "ipc/ipc_export.h" | |
23 #include "ipc/mojo/ipc_message_pipe_reader.h" | |
24 #include "ipc/mojo/ipc_mojo_bootstrap.h" | |
25 #include "mojo/public/cpp/system/core.h" | |
26 | |
27 namespace IPC { | |
28 | |
29 // Mojo-based IPC::Channel implementation over a Mojo message pipe. | |
30 // | |
31 // ChannelMojo builds a Mojo MessagePipe using the provided message pipe | |
32 // |handle| and builds an associated interface for each direction on the | |
33 // channel. | |
34 // | |
35 // TODO(morrita): Add APIs to create extra MessagePipes to let | |
36 // Mojo-based objects talk over this Channel. | |
37 // | |
38 class IPC_MOJO_EXPORT ChannelMojo | |
39 : public Channel, | |
40 public MojoBootstrap::Delegate, | |
41 public NON_EXPORTED_BASE(internal::MessagePipeReader::Delegate) { | |
42 public: | |
43 // Creates a ChannelMojo. | |
44 static std::unique_ptr<ChannelMojo> | |
45 Create(mojo::ScopedMessagePipeHandle handle, Mode mode, Listener* listener); | |
46 | |
47 // Create a factory object for ChannelMojo. | |
48 // The factory is used to create Mojo-based ChannelProxy family. | |
49 // |host| must not be null. | |
50 static std::unique_ptr<ChannelFactory> CreateServerFactory( | |
51 mojo::ScopedMessagePipeHandle handle); | |
52 | |
53 static std::unique_ptr<ChannelFactory> CreateClientFactory( | |
54 mojo::ScopedMessagePipeHandle handle); | |
55 | |
56 ~ChannelMojo() override; | |
57 | |
58 // Channel implementation | |
59 bool Connect() override; | |
60 void Close() override; | |
61 bool Send(Message* message) override; | |
62 bool IsSendThreadSafe() const override; | |
63 base::ProcessId GetPeerPID() const override; | |
64 base::ProcessId GetSelfPID() const override; | |
65 | |
66 #if defined(OS_POSIX) && !defined(OS_NACL_SFI) | |
67 int GetClientFileDescriptor() const override; | |
68 base::ScopedFD TakeClientFileDescriptor() override; | |
69 #endif // defined(OS_POSIX) && !defined(OS_NACL_SFI) | |
70 | |
71 // These access protected API of IPC::Message, which has ChannelMojo | |
72 // as a friend class. | |
73 static MojoResult WriteToMessageAttachmentSet( | |
74 mojo::Array<mojom::SerializedHandlePtr> handle_buffer, | |
75 Message* message); | |
76 static MojoResult ReadFromMessageAttachmentSet( | |
77 Message* message, | |
78 mojo::Array<mojom::SerializedHandlePtr>* handles); | |
79 | |
80 // MojoBootstrapDelegate implementation | |
81 void OnPipesAvailable(mojom::ChannelAssociatedPtrInfo send_channel, | |
82 mojom::ChannelAssociatedRequest receive_channel, | |
83 int32_t peer_pid) override; | |
84 void OnBootstrapError() override; | |
85 | |
86 // MessagePipeReader::Delegate | |
87 void OnMessageReceived(const Message& message) override; | |
88 void OnPipeError() override; | |
89 | |
90 private: | |
91 ChannelMojo(mojo::ScopedMessagePipeHandle handle, | |
92 Mode mode, | |
93 Listener* listener); | |
94 | |
95 void InitMessageReader(mojom::ChannelAssociatedPtrInfo sender, | |
96 mojom::ChannelAssociatedRequest receiver, | |
97 base::ProcessId peer_pid); | |
98 | |
99 // ChannelMojo needs to kill its MessagePipeReader in delayed manner | |
100 // because the channel wants to kill these readers during the | |
101 // notifications invoked by them. | |
102 typedef internal::MessagePipeReader::DelayedDeleter ReaderDeleter; | |
103 | |
104 // A TaskRunner which runs tasks on the ChannelMojo's owning thread. | |
105 scoped_refptr<base::TaskRunner> task_runner_; | |
106 | |
107 const mojo::MessagePipeHandle pipe_; | |
108 std::unique_ptr<MojoBootstrap> bootstrap_; | |
109 Listener* listener_; | |
110 | |
111 // Guards access to the fields below. | |
112 mutable base::Lock lock_; | |
113 std::unique_ptr<internal::MessagePipeReader, ReaderDeleter> message_reader_; | |
114 std::vector<std::unique_ptr<Message>> pending_messages_; | |
115 bool waiting_connect_; | |
116 | |
117 base::WeakPtrFactory<ChannelMojo> weak_factory_; | |
118 | |
119 DISALLOW_COPY_AND_ASSIGN(ChannelMojo); | |
120 }; | |
121 | |
122 } // namespace IPC | |
123 | |
124 #endif // IPC_IPC_CHANNEL_MOJO_H_ | |
OLD | NEW |