• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <map>
11 #include <memory>
12 #include <string>
13 #include <vector>
14 
15 #include "base/component_export.h"
16 #include "base/macros.h"
17 #include "base/memory/ref_counted.h"
18 #include "base/memory/weak_ptr.h"
19 #include "base/single_thread_task_runner.h"
20 #include "base/synchronization/lock.h"
21 #include "base/task_runner.h"
22 #include "base/threading/thread_task_runner_handle.h"
23 #include "build/build_config.h"
24 #include "ipc/ipc.mojom.h"
25 #include "ipc/ipc_channel.h"
26 #include "ipc/ipc_channel_factory.h"
27 #include "ipc/ipc_message_pipe_reader.h"
28 #include "ipc/ipc_mojo_bootstrap.h"
29 #include "mojo/public/cpp/bindings/thread_safe_interface_ptr.h"
30 #include "mojo/public/cpp/system/core.h"
31 
32 namespace IPC {
33 
34 // Mojo-based IPC::Channel implementation over a Mojo message pipe.
35 //
36 // ChannelMojo builds a Mojo MessagePipe using the provided message pipe
37 // |handle| and builds an associated interface for each direction on the
38 // channel.
39 //
40 // TODO(morrita): Add APIs to create extra MessagePipes to let
41 //                Mojo-based objects talk over this Channel.
42 //
COMPONENT_EXPORT(IPC)43 class COMPONENT_EXPORT(IPC) ChannelMojo
44     : public Channel,
45       public Channel::AssociatedInterfaceSupport,
46       public internal::MessagePipeReader::Delegate {
47  public:
48   // Creates a ChannelMojo.
49   static std::unique_ptr<ChannelMojo> Create(
50       mojo::ScopedMessagePipeHandle handle,
51       Mode mode,
52       Listener* listener,
53       const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
54       const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner);
55 
56   // Create a factory object for ChannelMojo.
57   // The factory is used to create Mojo-based ChannelProxy family.
58   // |host| must not be null.
59   static std::unique_ptr<ChannelFactory> CreateServerFactory(
60       mojo::ScopedMessagePipeHandle handle,
61       const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
62       const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner);
63 
64   static std::unique_ptr<ChannelFactory> CreateClientFactory(
65       mojo::ScopedMessagePipeHandle handle,
66       const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
67       const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner);
68 
69   ~ChannelMojo() override;
70 
71   // Channel implementation
72   bool Connect() override;
73   void Pause() override;
74   void Unpause(bool flush) override;
75   void Flush() override;
76   void Close() override;
77   bool Send(Message* message) override;
78   Channel::AssociatedInterfaceSupport* GetAssociatedInterfaceSupport() override;
79 
80   // These access protected API of IPC::Message, which has ChannelMojo
81   // as a friend class.
82   static MojoResult WriteToMessageAttachmentSet(
83       base::Optional<std::vector<mojo::native::SerializedHandlePtr>> handles,
84       Message* message);
85   static MojoResult ReadFromMessageAttachmentSet(
86       Message* message,
87       base::Optional<std::vector<mojo::native::SerializedHandlePtr>>* handles);
88 
89   // MessagePipeReader::Delegate
90   void OnPeerPidReceived(int32_t peer_pid) override;
91   void OnMessageReceived(const Message& message) override;
92   void OnBrokenDataReceived() override;
93   void OnPipeError() override;
94   void OnAssociatedInterfaceRequest(
95       const std::string& name,
96       mojo::ScopedInterfaceEndpointHandle handle) override;
97 
98  private:
99   ChannelMojo(
100       mojo::ScopedMessagePipeHandle handle,
101       Mode mode,
102       Listener* listener,
103       const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
104       const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner);
105 
106   void ForwardMessageFromThreadSafePtr(mojo::Message message);
107   void ForwardMessageWithResponderFromThreadSafePtr(
108       mojo::Message message,
109       std::unique_ptr<mojo::MessageReceiver> responder);
110 
111   // Channel::AssociatedInterfaceSupport:
112   std::unique_ptr<mojo::ThreadSafeForwarder<mojom::Channel>>
113   CreateThreadSafeChannel() override;
114   void AddGenericAssociatedInterface(
115       const std::string& name,
116       const GenericAssociatedInterfaceFactory& factory) override;
117   void GetGenericRemoteAssociatedInterface(
118       const std::string& name,
119       mojo::ScopedInterfaceEndpointHandle handle) override;
120 
121   base::WeakPtr<ChannelMojo> weak_ptr_;
122 
123   // A TaskRunner which runs tasks on the ChannelMojo's owning thread.
124   scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
125 
126   const mojo::MessagePipeHandle pipe_;
127   std::unique_ptr<MojoBootstrap> bootstrap_;
128   Listener* listener_;
129 
130   std::unique_ptr<internal::MessagePipeReader> message_reader_;
131 
132   base::Lock associated_interface_lock_;
133   std::map<std::string, GenericAssociatedInterfaceFactory>
134       associated_interfaces_;
135 
136   base::WeakPtrFactory<ChannelMojo> weak_factory_;
137 
138   DISALLOW_COPY_AND_ASSIGN(ChannelMojo);
139 };
140 
141 }  // namespace IPC
142 
143 #endif  // IPC_IPC_CHANNEL_MOJO_H_
144