Fix FullScreen crash in Webapp
[platform/framework/web/chromium-efl.git] / ipc / ipc_channel_mojo.h
1 // Copyright 2014 The Chromium Authors
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/memory/raw_ptr.h"
17 #include "base/memory/ref_counted.h"
18 #include "base/memory/weak_ptr.h"
19 #include "base/synchronization/lock.h"
20 #include "base/task/single_thread_task_runner.h"
21 #include "base/task/task_runner.h"
22 #include "build/build_config.h"
23 #include "ipc/ipc.mojom.h"
24 #include "ipc/ipc_channel.h"
25 #include "ipc/ipc_channel_factory.h"
26 #include "ipc/ipc_message_pipe_reader.h"
27 #include "ipc/ipc_mojo_bootstrap.h"
28
29 namespace IPC {
30
31 // Mojo-based IPC::Channel implementation over a Mojo message pipe.
32 //
33 // ChannelMojo builds a Mojo MessagePipe using the provided message pipe
34 // |handle| and builds an associated interface for each direction on the
35 // channel.
36 //
37 // TODO(morrita): Add APIs to create extra MessagePipes to let
38 //                Mojo-based objects talk over this Channel.
39 //
40 class COMPONENT_EXPORT(IPC) ChannelMojo
41     : public Channel,
42       public Channel::AssociatedInterfaceSupport,
43       public internal::MessagePipeReader::Delegate {
44  public:
45   // Creates a ChannelMojo.
46   static std::unique_ptr<ChannelMojo> Create(
47       mojo::ScopedMessagePipeHandle handle,
48       Mode mode,
49       Listener* listener,
50       const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
51       const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner);
52
53   // Create a factory object for ChannelMojo.
54   // The factory is used to create Mojo-based ChannelProxy family.
55   // |host| must not be null.
56   static std::unique_ptr<ChannelFactory> CreateServerFactory(
57       mojo::ScopedMessagePipeHandle handle,
58       const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
59       const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner);
60
61   static std::unique_ptr<ChannelFactory> CreateClientFactory(
62       mojo::ScopedMessagePipeHandle handle,
63       const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
64       const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner);
65
66   ChannelMojo(const ChannelMojo&) = delete;
67   ChannelMojo& operator=(const ChannelMojo&) = delete;
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       absl::optional<std::vector<mojo::native::SerializedHandlePtr>> handles,
84       Message* message);
85   static MojoResult ReadFromMessageAttachmentSet(
86       Message* message,
87       absl::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       mojo::GenericPendingAssociatedReceiver receiver) override;
96
97  private:
98   ChannelMojo(
99       mojo::ScopedMessagePipeHandle handle,
100       Mode mode,
101       Listener* listener,
102       const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
103       const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner);
104
105   void ForwardMessage(mojo::Message message);
106
107   // Channel::AssociatedInterfaceSupport:
108   std::unique_ptr<mojo::ThreadSafeForwarder<mojom::Channel>>
109   CreateThreadSafeChannel() override;
110   void AddGenericAssociatedInterface(
111       const std::string& name,
112       const GenericAssociatedInterfaceFactory& factory) override;
113   void GetRemoteAssociatedInterface(
114       mojo::GenericPendingAssociatedReceiver receiver) override;
115
116   void FinishConnectOnIOThread();
117
118   base::WeakPtr<ChannelMojo> weak_ptr_;
119
120   // A TaskRunner which runs tasks on the ChannelMojo's owning thread.
121   scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
122
123   const mojo::MessagePipeHandle pipe_;
124   std::unique_ptr<MojoBootstrap> bootstrap_;
125   raw_ptr<Listener, DanglingUntriaged> listener_;
126
127   std::unique_ptr<internal::MessagePipeReader> message_reader_;
128
129   base::Lock associated_interface_lock_;
130   std::map<std::string, GenericAssociatedInterfaceFactory>
131       associated_interfaces_;
132
133   base::WeakPtrFactory<ChannelMojo> weak_factory_{this};
134 };
135
136 }  // namespace IPC
137
138 #endif  // IPC_IPC_CHANNEL_MOJO_H_