Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #ifndef CHROME_COMMON_IPC_CHANNEL_PROXY_H__ |
michael@0 | 6 | #define CHROME_COMMON_IPC_CHANNEL_PROXY_H__ |
michael@0 | 7 | |
michael@0 | 8 | #include <vector> |
michael@0 | 9 | #include "base/lock.h" |
michael@0 | 10 | #include "base/ref_counted.h" |
michael@0 | 11 | #include "chrome/common/ipc_channel.h" |
michael@0 | 12 | |
michael@0 | 13 | class MessageLoop; |
michael@0 | 14 | |
michael@0 | 15 | namespace IPC { |
michael@0 | 16 | |
michael@0 | 17 | //----------------------------------------------------------------------------- |
michael@0 | 18 | // IPC::ChannelProxy |
michael@0 | 19 | // |
michael@0 | 20 | // This class is a helper class that is useful when you wish to run an IPC |
michael@0 | 21 | // channel on a background thread. It provides you with the option of either |
michael@0 | 22 | // handling IPC messages on that background thread or having them dispatched to |
michael@0 | 23 | // your main thread (the thread on which the IPC::ChannelProxy is created). |
michael@0 | 24 | // |
michael@0 | 25 | // The API for an IPC::ChannelProxy is very similar to that of an IPC::Channel. |
michael@0 | 26 | // When you send a message to an IPC::ChannelProxy, the message is routed to |
michael@0 | 27 | // the background thread, where it is then passed to the IPC::Channel's Send |
michael@0 | 28 | // method. This means that you can send a message from your thread and your |
michael@0 | 29 | // message will be sent over the IPC channel when possible instead of being |
michael@0 | 30 | // delayed until your thread returns to its message loop. (Often IPC messages |
michael@0 | 31 | // will queue up on the IPC::Channel when there is a lot of traffic, and the |
michael@0 | 32 | // channel will not get cycles to flush its message queue until the thread, on |
michael@0 | 33 | // which it is running, returns to its message loop.) |
michael@0 | 34 | // |
michael@0 | 35 | // An IPC::ChannelProxy can have a MessageFilter associated with it, which will |
michael@0 | 36 | // be notified of incoming messages on the IPC::Channel's thread. This gives |
michael@0 | 37 | // the consumer of IPC::ChannelProxy the ability to respond to incoming |
michael@0 | 38 | // messages on this background thread instead of on their own thread, which may |
michael@0 | 39 | // be bogged down with other processing. The result can be greatly improved |
michael@0 | 40 | // latency for messages that can be handled on a background thread. |
michael@0 | 41 | // |
michael@0 | 42 | // The consumer of IPC::ChannelProxy is responsible for allocating the Thread |
michael@0 | 43 | // instance where the IPC::Channel will be created and operated. |
michael@0 | 44 | // |
michael@0 | 45 | class ChannelProxy : public Message::Sender { |
michael@0 | 46 | public: |
michael@0 | 47 | // A class that receives messages on the thread where the IPC channel is |
michael@0 | 48 | // running. It can choose to prevent the default action for an IPC message. |
michael@0 | 49 | class MessageFilter : public base::RefCountedThreadSafe<MessageFilter> { |
michael@0 | 50 | public: |
michael@0 | 51 | virtual ~MessageFilter() {} |
michael@0 | 52 | |
michael@0 | 53 | // Called on the background thread to provide the filter with access to the |
michael@0 | 54 | // channel. Called when the IPC channel is initialized or when AddFilter |
michael@0 | 55 | // is called if the channel is already initialized. |
michael@0 | 56 | virtual void OnFilterAdded(Channel* channel) {} |
michael@0 | 57 | |
michael@0 | 58 | // Called on the background thread when the filter has been removed from |
michael@0 | 59 | // the ChannelProxy and when the Channel is closing. After a filter is |
michael@0 | 60 | // removed, it will not be called again. |
michael@0 | 61 | virtual void OnFilterRemoved() {} |
michael@0 | 62 | |
michael@0 | 63 | // Called to inform the filter that the IPC channel is connected and we |
michael@0 | 64 | // have received the internal Hello message from the peer. |
michael@0 | 65 | virtual void OnChannelConnected(int32_t peer_pid) {} |
michael@0 | 66 | |
michael@0 | 67 | // Called when there is an error on the channel, typically that the channel |
michael@0 | 68 | // has been closed. |
michael@0 | 69 | virtual void OnChannelError() {} |
michael@0 | 70 | |
michael@0 | 71 | // Called to inform the filter that the IPC channel will be destroyed. |
michael@0 | 72 | // OnFilterRemoved is called immediately after this. |
michael@0 | 73 | virtual void OnChannelClosing() {} |
michael@0 | 74 | |
michael@0 | 75 | // Return true to indicate that the message was handled, or false to let |
michael@0 | 76 | // the message be handled in the default way. |
michael@0 | 77 | virtual bool OnMessageReceived(const Message& message) { |
michael@0 | 78 | return false; |
michael@0 | 79 | } |
michael@0 | 80 | }; |
michael@0 | 81 | |
michael@0 | 82 | // Initializes a channel proxy. The channel_id and mode parameters are |
michael@0 | 83 | // passed directly to the underlying IPC::Channel. The listener is called on |
michael@0 | 84 | // the thread that creates the ChannelProxy. The filter's OnMessageReceived |
michael@0 | 85 | // method is called on the thread where the IPC::Channel is running. The |
michael@0 | 86 | // filter may be null if the consumer is not interested in handling messages |
michael@0 | 87 | // on the background thread. Any message not handled by the filter will be |
michael@0 | 88 | // dispatched to the listener. The given message loop indicates where the |
michael@0 | 89 | // IPC::Channel should be created. |
michael@0 | 90 | ChannelProxy(const std::wstring& channel_id, Channel::Mode mode, |
michael@0 | 91 | Channel::Listener* listener, MessageFilter* filter, |
michael@0 | 92 | MessageLoop* ipc_thread_loop); |
michael@0 | 93 | |
michael@0 | 94 | ~ChannelProxy() { |
michael@0 | 95 | Close(); |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | // Close the IPC::Channel. This operation completes asynchronously, once the |
michael@0 | 99 | // background thread processes the command to close the channel. It is ok to |
michael@0 | 100 | // call this method multiple times. Redundant calls are ignored. |
michael@0 | 101 | // |
michael@0 | 102 | // WARNING: The MessageFilter object held by the ChannelProxy is also |
michael@0 | 103 | // released asynchronously, and it may in fact have its final reference |
michael@0 | 104 | // released on the background thread. The caller should be careful to deal |
michael@0 | 105 | // with / allow for this possibility. |
michael@0 | 106 | void Close(); |
michael@0 | 107 | |
michael@0 | 108 | // Send a message asynchronously. The message is routed to the background |
michael@0 | 109 | // thread where it is passed to the IPC::Channel's Send method. |
michael@0 | 110 | virtual bool Send(Message* message); |
michael@0 | 111 | |
michael@0 | 112 | // Used to intercept messages as they are received on the background thread. |
michael@0 | 113 | // |
michael@0 | 114 | // Ordinarily, messages sent to the ChannelProxy are routed to the matching |
michael@0 | 115 | // listener on the worker thread. This API allows code to intercept messages |
michael@0 | 116 | // before they are sent to the worker thread. |
michael@0 | 117 | void AddFilter(MessageFilter* filter); |
michael@0 | 118 | void RemoveFilter(MessageFilter* filter); |
michael@0 | 119 | |
michael@0 | 120 | #if defined(OS_POSIX) |
michael@0 | 121 | // Calls through to the underlying channel's methods. |
michael@0 | 122 | // TODO(playmobil): For now this is only implemented in the case of |
michael@0 | 123 | // create_pipe_now = true, we need to figure this out for the latter case. |
michael@0 | 124 | void GetClientFileDescriptorMapping(int *src_fd, int *dest_fd) const; |
michael@0 | 125 | #endif // defined(OS_POSIX) |
michael@0 | 126 | |
michael@0 | 127 | protected: |
michael@0 | 128 | class Context; |
michael@0 | 129 | // A subclass uses this constructor if it needs to add more information |
michael@0 | 130 | // to the internal state. If create_pipe_now is true, the pipe is created |
michael@0 | 131 | // immediately. Otherwise it's created on the IO thread. |
michael@0 | 132 | ChannelProxy(const std::wstring& channel_id, Channel::Mode mode, |
michael@0 | 133 | MessageLoop* ipc_thread_loop, Context* context, |
michael@0 | 134 | bool create_pipe_now); |
michael@0 | 135 | |
michael@0 | 136 | // Used internally to hold state that is referenced on the IPC thread. |
michael@0 | 137 | class Context : public base::RefCountedThreadSafe<Context>, |
michael@0 | 138 | public Channel::Listener { |
michael@0 | 139 | public: |
michael@0 | 140 | Context(Channel::Listener* listener, MessageFilter* filter, |
michael@0 | 141 | MessageLoop* ipc_thread); |
michael@0 | 142 | virtual ~Context() { } |
michael@0 | 143 | MessageLoop* ipc_message_loop() const { return ipc_message_loop_; } |
michael@0 | 144 | const std::wstring& channel_id() const { return channel_id_; } |
michael@0 | 145 | |
michael@0 | 146 | // Dispatches a message on the listener thread. |
michael@0 | 147 | void OnDispatchMessage(const Message& message); |
michael@0 | 148 | |
michael@0 | 149 | protected: |
michael@0 | 150 | // IPC::Channel::Listener methods: |
michael@0 | 151 | virtual void OnMessageReceived(const Message& message); |
michael@0 | 152 | virtual void OnChannelConnected(int32_t peer_pid); |
michael@0 | 153 | virtual void OnChannelError(); |
michael@0 | 154 | |
michael@0 | 155 | // Like OnMessageReceived but doesn't try the filters. |
michael@0 | 156 | void OnMessageReceivedNoFilter(const Message& message); |
michael@0 | 157 | |
michael@0 | 158 | // Gives the filters a chance at processing |message|. |
michael@0 | 159 | // Returns true if the message was processed, false otherwise. |
michael@0 | 160 | bool TryFilters(const Message& message); |
michael@0 | 161 | |
michael@0 | 162 | // Like Open and Close, but called on the IPC thread. |
michael@0 | 163 | virtual void OnChannelOpened(); |
michael@0 | 164 | virtual void OnChannelClosed(); |
michael@0 | 165 | |
michael@0 | 166 | // Called on the consumers thread when the ChannelProxy is closed. At that |
michael@0 | 167 | // point the consumer is telling us that they don't want to receive any |
michael@0 | 168 | // more messages, so we honor that wish by forgetting them! |
michael@0 | 169 | virtual void Clear() { listener_ = NULL; } |
michael@0 | 170 | |
michael@0 | 171 | private: |
michael@0 | 172 | friend class ChannelProxy; |
michael@0 | 173 | // Create the Channel |
michael@0 | 174 | void CreateChannel(const std::wstring& id, const Channel::Mode& mode); |
michael@0 | 175 | |
michael@0 | 176 | // Methods called via InvokeLater: |
michael@0 | 177 | void OnSendMessage(Message* message_ptr); |
michael@0 | 178 | void OnAddFilter(MessageFilter* filter); |
michael@0 | 179 | void OnRemoveFilter(MessageFilter* filter); |
michael@0 | 180 | void OnDispatchConnected(); |
michael@0 | 181 | void OnDispatchError(); |
michael@0 | 182 | |
michael@0 | 183 | MessageLoop* listener_message_loop_; |
michael@0 | 184 | Channel::Listener* listener_; |
michael@0 | 185 | |
michael@0 | 186 | // List of filters. This is only accessed on the IPC thread. |
michael@0 | 187 | std::vector<scoped_refptr<MessageFilter> > filters_; |
michael@0 | 188 | MessageLoop* ipc_message_loop_; |
michael@0 | 189 | Channel* channel_; |
michael@0 | 190 | std::wstring channel_id_; |
michael@0 | 191 | int peer_pid_; |
michael@0 | 192 | bool channel_connected_called_; |
michael@0 | 193 | }; |
michael@0 | 194 | |
michael@0 | 195 | Context* context() { return context_; } |
michael@0 | 196 | |
michael@0 | 197 | private: |
michael@0 | 198 | void Init(const std::wstring& channel_id, Channel::Mode mode, |
michael@0 | 199 | MessageLoop* ipc_thread_loop, bool create_pipe_now); |
michael@0 | 200 | |
michael@0 | 201 | // By maintaining this indirection (ref-counted) to our internal state, we |
michael@0 | 202 | // can safely be destroyed while the background thread continues to do stuff |
michael@0 | 203 | // that involves this data. |
michael@0 | 204 | scoped_refptr<Context> context_; |
michael@0 | 205 | }; |
michael@0 | 206 | |
michael@0 | 207 | } // namespace IPC |
michael@0 | 208 | |
michael@0 | 209 | #endif // CHROME_COMMON_IPC_CHANNEL_PROXY_H__ |