1 // Copyright 2013 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.
5 #ifndef CONTENT_CHILD_CROSS_MESSAGE_FILTER_H_
6 #define CONTENT_CHILD_CROSS_MESSAGE_FILTER_H_
8 #include "base/memory/ref_counted.h"
9 #include "ipc/ipc_sender.h"
21 class ThreadSafeSender
;
23 // A base class for implementing IPC MessageFilter's that run on a different
24 // thread or TaskRunner than the main thread.
25 class ChildMessageFilter
26 : public base::RefCountedThreadSafe
<ChildMessageFilter
>,
29 // IPC::Sender implementation. Can be called on any threads.
30 virtual bool Send(IPC::Message
* message
) OVERRIDE
;
32 // If implementers want to run OnMessageReceived on a different task
33 // runner it should override this and return the TaskRunner for the message.
34 // Returning NULL runs OnMessageReceived() on the current IPC thread.
35 virtual base::TaskRunner
* OverrideTaskRunnerForMessage(
36 const IPC::Message
& msg
) = 0;
38 // If OverrideTaskRunnerForMessage is overriden and returns non-null
39 // this will be called on the returned TaskRunner.
40 virtual bool OnMessageReceived(const IPC::Message
& msg
) = 0;
42 // This method is called when WorkerTaskRunner::PostTask() returned false
43 // for the target thread. Note that there's still a little chance that
44 // PostTask() returns true but OnMessageReceived() is never called on the
45 // target thread. By default this does nothing.
46 virtual void OnStaleMessageReceived(const IPC::Message
& msg
) {}
50 virtual ~ChildMessageFilter();
54 friend class ChildThread
;
55 friend class RenderThreadImpl
;
56 friend class WorkerThread
;
58 friend class base::RefCountedThreadSafe
<ChildMessageFilter
>;
60 IPC::MessageFilter
* GetFilter();
62 // This implements IPC::MessageFilter to hide the actual filter methods from
66 scoped_refptr
<ThreadSafeSender
> thread_safe_sender_
;
68 DISALLOW_COPY_AND_ASSIGN(ChildMessageFilter
);
71 } // namespace content
73 #endif // CONTENT_CHILD_CROSS_MESSAGE_FILTER_H_