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. 4 5 #ifndef CONTENT_CHILD_CROSS_MESSAGE_FILTER_H_ 6 #define CONTENT_CHILD_CROSS_MESSAGE_FILTER_H_ 7 8 #include "base/memory/ref_counted.h" 9 #include "ipc/ipc_sender.h" 10 11 namespace base { 12 class TaskRunner; 13 } 14 15 namespace IPC { 16 class MessageFilter; 17 } 18 19 namespace content { 20 21 class ThreadSafeSender; 22 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>, 27 public IPC::Sender { 28 public: 29 // IPC::Sender implementation. Can be called on any threads. 30 virtual bool Send(IPC::Message* message) OVERRIDE; 31 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; 37 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; 41 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) {} 47 48 protected: 49 ChildMessageFilter(); 50 virtual ~ChildMessageFilter(); 51 52 private: 53 class Internal; 54 friend class ChildThread; 55 friend class RenderThreadImpl; 56 friend class WorkerThread; 57 58 friend class base::RefCountedThreadSafe<ChildMessageFilter>; 59 60 IPC::MessageFilter* GetFilter(); 61 62 // This implements IPC::MessageFilter to hide the actual filter methods from 63 // child classes. 64 Internal* internal_; 65 66 scoped_refptr<ThreadSafeSender> thread_safe_sender_; 67 68 DISALLOW_COPY_AND_ASSIGN(ChildMessageFilter); 69 }; 70 71 } // namespace content 72 73 #endif // CONTENT_CHILD_CROSS_MESSAGE_FILTER_H_ 74