Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(522)

Side by Side Diff: ipc/ipc_channel_proxy.h

Issue 6711024: Example of how to interpose yourself in a message stream. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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_PROXY_H__ 5 #ifndef IPC_IPC_CHANNEL_PROXY_H__
6 #define IPC_IPC_CHANNEL_PROXY_H__ 6 #define IPC_IPC_CHANNEL_PROXY_H__
7 #pragma once 7 #pragma once
8 8
9 #include <vector> 9 #include <vector>
10 10
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 // on etc. 92 // on etc.
93 virtual void OnDestruct() const; 93 virtual void OnDestruct() const;
94 }; 94 };
95 95
96 struct MessageFilterTraits { 96 struct MessageFilterTraits {
97 static void Destruct(const MessageFilter* filter) { 97 static void Destruct(const MessageFilter* filter) {
98 filter->OnDestruct(); 98 filter->OnDestruct();
99 } 99 }
100 }; 100 };
101 101
102 // Interface for a filter to be imposed on outgoing messages which can
103 // re-write the message. Used mainly for testing.
104 class OutgoingMessageFilter {
105 public:
106 // Returns a re-written message, freeing the original, or simply the
107 // original unchanged if no rewrite indicated.
108 virtual Message *Rewrite(Message *message) = 0;
109 };
110
102 // Initializes a channel proxy. The channel_handle and mode parameters are 111 // Initializes a channel proxy. The channel_handle and mode parameters are
103 // passed directly to the underlying IPC::Channel. The listener is called on 112 // passed directly to the underlying IPC::Channel. The listener is called on
104 // the thread that creates the ChannelProxy. The filter's OnMessageReceived 113 // the thread that creates the ChannelProxy. The filter's OnMessageReceived
105 // method is called on the thread where the IPC::Channel is running. The 114 // method is called on the thread where the IPC::Channel is running. The
106 // filter may be null if the consumer is not interested in handling messages 115 // filter may be null if the consumer is not interested in handling messages
107 // on the background thread. Any message not handled by the filter will be 116 // on the background thread. Any message not handled by the filter will be
108 // dispatched to the listener. The given message loop indicates where the 117 // dispatched to the listener. The given message loop indicates where the
109 // IPC::Channel should be created. 118 // IPC::Channel should be created.
110 ChannelProxy(const IPC::ChannelHandle& channel_handle, 119 ChannelProxy(const IPC::ChannelHandle& channel_handle,
111 Channel::Mode mode, 120 Channel::Mode mode,
(...skipping 21 matching lines...) Expand all
133 // Ordinarily, messages sent to the ChannelProxy are routed to the matching 142 // Ordinarily, messages sent to the ChannelProxy are routed to the matching
134 // listener on the worker thread. This API allows code to intercept messages 143 // listener on the worker thread. This API allows code to intercept messages
135 // before they are sent to the worker thread. 144 // before they are sent to the worker thread.
136 // If you call this before the target process is launched, then you're 145 // If you call this before the target process is launched, then you're
137 // guaranteed to not miss any messages. But if you call this anytime after, 146 // guaranteed to not miss any messages. But if you call this anytime after,
138 // then some messages might be missed since the filter is added internally on 147 // then some messages might be missed since the filter is added internally on
139 // the IO thread. 148 // the IO thread.
140 void AddFilter(MessageFilter* filter); 149 void AddFilter(MessageFilter* filter);
141 void RemoveFilter(MessageFilter* filter); 150 void RemoveFilter(MessageFilter* filter);
142 151
152 void set_outgoing_message_filter(OutgoingMessageFilter* filter) {
153 outgoing_message_filter_ = filter;
154 }
155
143 // Called to clear the pointer to the IPC message loop when it's going away. 156 // Called to clear the pointer to the IPC message loop when it's going away.
144 void ClearIPCMessageLoop(); 157 void ClearIPCMessageLoop();
145 158
146 #if defined(OS_POSIX) 159 #if defined(OS_POSIX)
147 // Calls through to the underlying channel's methods. 160 // Calls through to the underlying channel's methods.
148 int GetClientFileDescriptor() const; 161 int GetClientFileDescriptor() const;
149 bool GetClientEuid(uid_t* client_euid) const; 162 bool GetClientEuid(uid_t* client_euid) const;
150 #endif // defined(OS_POSIX) 163 #endif // defined(OS_POSIX)
151 164
152 protected: 165 protected:
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
228 241
229 // Holds filters between the AddFilter call on the listerner thread and the 242 // Holds filters between the AddFilter call on the listerner thread and the
230 // IPC thread when they're added to filters_. 243 // IPC thread when they're added to filters_.
231 std::vector<scoped_refptr<MessageFilter> > pending_filters_; 244 std::vector<scoped_refptr<MessageFilter> > pending_filters_;
232 // Lock for pending_filters_. 245 // Lock for pending_filters_.
233 base::Lock pending_filters_lock_; 246 base::Lock pending_filters_lock_;
234 }; 247 };
235 248
236 Context* context() { return context_; } 249 Context* context() { return context_; }
237 250
251 OutgoingMessageFilter* outgoing_message_filter() {
252 return outgoing_message_filter_;
253 }
254
238 private: 255 private:
239 friend class SendTask; 256 friend class SendTask;
240 257
241 void Init(const IPC::ChannelHandle& channel_handle, Channel::Mode mode, 258 void Init(const IPC::ChannelHandle& channel_handle, Channel::Mode mode,
242 MessageLoop* ipc_thread_loop, bool create_pipe_now); 259 MessageLoop* ipc_thread_loop, bool create_pipe_now);
243 260
244 // By maintaining this indirection (ref-counted) to our internal state, we 261 // By maintaining this indirection (ref-counted) to our internal state, we
245 // can safely be destroyed while the background thread continues to do stuff 262 // can safely be destroyed while the background thread continues to do stuff
246 // that involves this data. 263 // that involves this data.
247 scoped_refptr<Context> context_; 264 scoped_refptr<Context> context_;
265
266 OutgoingMessageFilter* outgoing_message_filter_;
248 }; 267 };
249 268
250 } // namespace IPC 269 } // namespace IPC
251 270
252 #endif // IPC_IPC_CHANNEL_PROXY_H__ 271 #endif // IPC_IPC_CHANNEL_PROXY_H__
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698