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