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