Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 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 PPAPI_HOST_RESOURCE_MESSAGE_FILTER_H_ | |
| 6 #define PPAPI_HOST_RESOURCE_MESSAGE_FILTER_H_ | |
| 7 | |
| 8 #include "base/memory/ref_counted.h" | |
| 9 #include "ppapi/c/pp_stdint.h" | |
| 10 #include "ppapi/host/ppapi_host_export.h" | |
| 11 #include "ppapi/host/resource_message_handler.h" | |
| 12 | |
| 13 namespace base { | |
| 14 class MessageLoopProxy; | |
| 15 class TaskRunner; | |
| 16 } | |
| 17 | |
| 18 namespace content { | |
| 19 class BrowserPpapiHost; | |
| 20 class RenderViewHost; | |
| 21 } | |
| 22 | |
| 23 namespace IPC { | |
| 24 class Message; | |
| 25 } | |
| 26 | |
| 27 namespace ppapi { | |
| 28 namespace host { | |
| 29 | |
| 30 struct HostMessageContext; | |
| 31 struct ReplyMessageContext; | |
| 32 class ResourceHost; | |
| 33 | |
| 34 // This is the base class of resource message filters that can handle resource | |
| 35 // messages on another thread. ResourceHosts can handle most messages | |
| 36 // directly, but if they need to handle something on a different thread it is | |
| 37 // inconvenient. This class makes handling that case easier. This class is | |
| 38 // similar to a BrowserMessageFilter but for resource messages. | |
| 39 // | |
| 40 // To handle a resource message on another thread you should implement a | |
| 41 // subclass as follows: | |
| 42 // class MyMessageFilter : public ResourceMessageFilter { | |
| 43 // protected: | |
| 44 // virtual scoped_refptr<base::TaskRunner> OverrideTaskRunnerForMessage( | |
| 45 // const IPC::Message& message) OVERRIDE { | |
| 46 // if (message.type() == MyMessage::ID) | |
| 47 // return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); | |
| 48 // return NULL; | |
| 49 // } | |
| 50 // | |
| 51 // virtual int32_t OnResourceMessageReceived( | |
| 52 // const IPC::Message& msg, | |
| 53 // HostMessageContext* context) OVERRIDE { | |
| 54 // IPC_BEGIN_MESSAGE_MAP(MyMessageFilter, msg) | |
| 55 // PPAPI_DISPATCH_HOST_RESOURCE_CALL(MyMessage, OnMyMessage) | |
| 56 // IPC_END_MESSAGE_MAP() | |
| 57 // return PP_ERROR_FAILED; | |
| 58 // } | |
| 59 // | |
| 60 // private: | |
| 61 // int32_t OnMyMessage(ppapi::host::HostMessageContext* context, ...) { | |
| 62 // // Will be run on the UI thread. | |
| 63 // } | |
| 64 // } | |
| 65 // | |
| 66 // The filter should then be added in the resource host using: | |
| 67 // AddFilter(make_scoped_ptr(new MyMessageFilter)); | |
| 68 class PPAPI_HOST_EXPORT ResourceMessageFilter | |
| 69 : public ResourceMessageHandler, | |
| 70 public base::RefCountedThreadSafe<ResourceMessageFilter> { | |
| 71 public: | |
| 72 ResourceMessageFilter(); | |
| 73 // Test constructor. Allows you to specify the IO message loop (which will be | |
| 74 // used to dispatch replies on). | |
| 75 ResourceMessageFilter( | |
| 76 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy); | |
| 77 | |
| 78 // Called when a filter is added to a ResourceHost. | |
| 79 void OnFilterAdded(ResourceHost* resource_host); | |
| 80 // Called when a filter is removed from a ResourceHost. | |
| 81 void OnFilterDestroyed(); | |
| 82 | |
| 83 // This will dispatch the message handler on the target thread. It returns | |
| 84 // true if the message was handled by this filter and false otherwise. | |
| 85 virtual bool HandleMessage(const IPC::Message& msg, | |
| 86 HostMessageContext* context) OVERRIDE; | |
| 87 | |
| 88 virtual void SendReply(const ReplyMessageContext& context, | |
| 89 const IPC::Message& msg) OVERRIDE; | |
| 90 | |
| 91 protected: | |
| 92 friend class base::RefCountedThreadSafe<ResourceMessageFilter>; | |
| 93 virtual ~ResourceMessageFilter(); | |
| 94 | |
| 95 // If you want the message to be dispatched via the TaskRunner, | |
| 96 // return a non-null task runner which will target tasks accordingly. | |
| 97 virtual scoped_refptr<base::TaskRunner> OverrideTaskRunnerForMessage( | |
| 98 const IPC::Message& message); | |
| 99 | |
| 100 private: | |
| 101 // This method is posted to the target thread and runs the message handler. | |
| 102 void DispatchMessage(const IPC::Message& msg, | |
| 103 HostMessageContext context); | |
| 104 | |
| 105 // Message loop to send resource message replies on. | |
| 106 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | |
| 107 | |
| 108 // Non-owning pointer to the resource host owning this filter. Should only be | |
| 109 // accessed from the IO thread. | |
|
brettw
2012/11/16 19:27:54
Can you also clarify that this may be NULL and whe
raymes
2012/11/16 19:42:50
Done.
| |
| 110 ResourceHost* resource_host_; | |
| 111 | |
| 112 DISALLOW_COPY_AND_ASSIGN(ResourceMessageFilter); | |
| 113 }; | |
| 114 | |
| 115 } // namespace host | |
| 116 } // namespace ppapi | |
| 117 | |
| 118 #endif // PPAPI_HOST_RESOURCE_MESSAGE_FILTER_H_ | |
| OLD | NEW |