Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 PPAPI_HOST_RESOURCE_MESSAGE_FILTER_H_ | 5 #ifndef PPAPI_HOST_RESOURCE_MESSAGE_FILTER_H_ |
| 6 #define PPAPI_HOST_RESOURCE_MESSAGE_FILTER_H_ | 6 #define PPAPI_HOST_RESOURCE_MESSAGE_FILTER_H_ |
| 7 | 7 |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "ppapi/c/pp_stdint.h" | 9 #include "ppapi/c/pp_stdint.h" |
| 10 #include "ppapi/host/host_message_context.h" | 10 #include "ppapi/host/host_message_context.h" |
| 11 #include "ppapi/host/ppapi_host_export.h" | 11 #include "ppapi/host/ppapi_host_export.h" |
| 12 #include "ppapi/host/resource_message_handler.h" | 12 #include "ppapi/host/resource_message_handler.h" |
| 13 | 13 |
| 14 namespace base { | 14 namespace base { |
| 15 class MessageLoopProxy; | 15 class MessageLoopProxy; |
| 16 class TaskRunner; | 16 class TaskRunner; |
| 17 } | 17 } |
| 18 | 18 |
| 19 namespace IPC { | 19 namespace IPC { |
| 20 class Message; | 20 class Message; |
| 21 } | 21 } |
| 22 | 22 |
| 23 namespace ppapi { | 23 namespace ppapi { |
| 24 namespace host { | 24 namespace host { |
| 25 | 25 |
| 26 class ResourceHost; | 26 class ResourceHost; |
| 27 class ResourceMessageFilter; | |
| 28 | |
| 29 namespace internal { | |
| 30 | |
| 31 struct ResourceMessageFilterDeleteTraits { | |
| 32 static void Destruct(const ResourceMessageFilter* filter); | |
| 33 }; | |
| 34 | |
| 35 } // namespace internal | |
| 27 | 36 |
| 28 // This is the base class of resource message filters that can handle resource | 37 // This is the base class of resource message filters that can handle resource |
| 29 // messages on another thread. ResourceHosts can handle most messages | 38 // messages on another thread. ResourceHosts can handle most messages |
| 30 // directly, but if they need to handle something on a different thread it is | 39 // 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 | 40 // inconvenient. This class makes handling that case easier. This class is |
| 32 // similar to a BrowserMessageFilter but for resource messages. Note that the | 41 // 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 | 42 // 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. | 43 // 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 | 44 // If this is the case, the message handler will always be called but a reply |
| 36 // may not be sent back to the host. | 45 // may not be sent back to the host. |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 58 // private: | 67 // private: |
| 59 // int32_t OnMyMessage(ppapi::host::HostMessageContext* context, ...) { | 68 // int32_t OnMyMessage(ppapi::host::HostMessageContext* context, ...) { |
| 60 // // Will be run on the UI thread. | 69 // // Will be run on the UI thread. |
| 61 // } | 70 // } |
| 62 // } | 71 // } |
| 63 // | 72 // |
| 64 // The filter should then be added in the resource host using: | 73 // The filter should then be added in the resource host using: |
| 65 // AddFilter(make_scoped_refptr(new MyMessageFilter)); | 74 // AddFilter(make_scoped_refptr(new MyMessageFilter)); |
| 66 class PPAPI_HOST_EXPORT ResourceMessageFilter | 75 class PPAPI_HOST_EXPORT ResourceMessageFilter |
| 67 : public ResourceMessageHandler, | 76 : public ResourceMessageHandler, |
| 68 public base::RefCountedThreadSafe<ResourceMessageFilter> { | 77 public base::RefCountedThreadSafe< |
| 78 ResourceMessageFilter, internal::ResourceMessageFilterDeleteTraits> { | |
| 69 public: | 79 public: |
| 70 // This object must be constructed on the same thread that a reply message | 80 // This object must be constructed on the same thread that a reply message |
| 71 // should be sent, i.e. the IO thread when constructed in the browser process | 81 // should be sent, i.e. the IO thread when constructed in the browser process |
| 72 // or the main thread when constructed in the renderer process. Since | 82 // or the main thread when constructed in the renderer process. Since |
| 73 // ResourceMessageFilters are usually constructed in the constructor of the | 83 // ResourceMessageFilters are usually constructed in the constructor of the |
| 74 // owning ResourceHost, this will almost always be the case anyway. | 84 // owning ResourceHost, this will almost always be the case anyway. |
| 85 // The object will be deleted on the creation thread. | |
| 75 ResourceMessageFilter(); | 86 ResourceMessageFilter(); |
| 76 // Test constructor. Allows you to specify the message loop which will be used | 87 // Test constructor. Allows you to specify the message loop which will be used |
| 77 // to dispatch replies on. | 88 // to dispatch replies on. |
| 78 ResourceMessageFilter( | 89 ResourceMessageFilter( |
| 79 scoped_refptr<base::MessageLoopProxy> reply_thread_message_loop_proxy); | 90 scoped_refptr<base::MessageLoopProxy> reply_thread_message_loop_proxy); |
| 80 | 91 |
| 81 // Called when a filter is added to a ResourceHost. | 92 // Called when a filter is added to a ResourceHost. |
| 82 void OnFilterAdded(ResourceHost* resource_host); | 93 void OnFilterAdded(ResourceHost* resource_host); |
| 83 // Called when a filter is removed from a ResourceHost. | 94 // Called when a filter is removed from a ResourceHost. |
| 84 void OnFilterDestroyed(); | 95 void OnFilterDestroyed(); |
| 85 | 96 |
| 86 // This will dispatch the message handler on the target thread. It returns | 97 // This will dispatch the message handler on the target thread. It returns |
| 87 // true if the message was handled by this filter and false otherwise. | 98 // true if the message was handled by this filter and false otherwise. |
| 88 virtual bool HandleMessage(const IPC::Message& msg, | 99 virtual bool HandleMessage(const IPC::Message& msg, |
| 89 HostMessageContext* context) OVERRIDE; | 100 HostMessageContext* context) OVERRIDE; |
| 90 | 101 |
| 91 // This can be called from any thread. | 102 // This can be called from any thread. |
| 92 virtual void SendReply(const ReplyMessageContext& context, | 103 virtual void SendReply(const ReplyMessageContext& context, |
| 93 const IPC::Message& msg) OVERRIDE; | 104 const IPC::Message& msg) OVERRIDE; |
| 94 | 105 |
| 95 protected: | 106 protected: |
| 96 friend class base::RefCountedThreadSafe<ResourceMessageFilter>; | |
| 97 virtual ~ResourceMessageFilter(); | 107 virtual ~ResourceMessageFilter(); |
| 98 | 108 |
| 99 // If you want the message to be handled on another thread, return a non-null | 109 // If you want the message to be handled on another thread, return a non-null |
| 100 // task runner which will target tasks accordingly. | 110 // task runner which will target tasks accordingly. |
| 101 virtual scoped_refptr<base::TaskRunner> OverrideTaskRunnerForMessage( | 111 virtual scoped_refptr<base::TaskRunner> OverrideTaskRunnerForMessage( |
| 102 const IPC::Message& message); | 112 const IPC::Message& message); |
| 103 | 113 |
| 104 private: | 114 private: |
| 115 friend class base::DeleteHelper<ResourceMessageFilter>; | |
| 116 friend class base::RefCountedThreadSafe< | |
| 117 ResourceMessageFilter, internal::ResourceMessageFilterDeleteTraits>; | |
|
dmichael (off chromium)
2013/11/01 20:30:39
Are these friend decls both still necessary?
yzshen1
2013/11/01 20:43:37
Yeah, unfortunately all of them are necessary. :/
| |
| 118 friend struct internal::ResourceMessageFilterDeleteTraits; | |
|
dmichael (off chromium)
2013/11/01 20:30:39
Optional: strictly speaking, you could do the work
yzshen1
2013/11/01 20:43:37
Done. Thanks!
On 2013/11/01 20:30:39, dmichael wr
| |
| 119 | |
| 105 // This method is posted to the target thread and runs the message handler. | 120 // This method is posted to the target thread and runs the message handler. |
| 106 void DispatchMessage(const IPC::Message& msg, | 121 void DispatchMessage(const IPC::Message& msg, |
| 107 HostMessageContext context); | 122 HostMessageContext context); |
| 108 | 123 |
| 124 void InternalDestruct() const; | |
| 125 | |
| 126 scoped_refptr<base::MessageLoopProxy> deletion_message_loop_proxy_; | |
| 127 | |
| 109 // Message loop to send resource message replies on. This will be the message | 128 // Message loop to send resource message replies on. This will be the message |
| 110 // loop proxy of the IO thread for the browser process or the main thread for | 129 // loop proxy of the IO thread for the browser process or the main thread for |
| 111 // the renderer process. | 130 // the renderer process. |
| 112 scoped_refptr<base::MessageLoopProxy> reply_thread_message_loop_proxy_; | 131 scoped_refptr<base::MessageLoopProxy> reply_thread_message_loop_proxy_; |
| 113 | 132 |
| 114 // Non-owning pointer to the resource host owning this filter. Should only be | 133 // Non-owning pointer to the resource host owning this filter. Should only be |
| 115 // accessed from the thread which sends messages to the plugin resource (i.e. | 134 // accessed from the thread which sends messages to the plugin resource (i.e. |
| 116 // the IO thread for the browser process or the main thread for the renderer). | 135 // the IO thread for the browser process or the main thread for the renderer). |
| 117 // This will be NULL upon creation of the filter and is set to the owning | 136 // This will be NULL upon creation of the filter and is set to the owning |
| 118 // ResourceHost when |OnFilterAdded| is called. When the owning ResourceHost | 137 // ResourceHost when |OnFilterAdded| is called. When the owning ResourceHost |
| 119 // is destroyed, |OnFilterDestroyed| is called and this will be set to NULL. | 138 // is destroyed, |OnFilterDestroyed| is called and this will be set to NULL. |
| 120 ResourceHost* resource_host_; | 139 ResourceHost* resource_host_; |
| 121 | 140 |
| 122 DISALLOW_COPY_AND_ASSIGN(ResourceMessageFilter); | 141 DISALLOW_COPY_AND_ASSIGN(ResourceMessageFilter); |
| 123 }; | 142 }; |
| 124 | 143 |
| 125 } // namespace host | 144 } // namespace host |
| 126 } // namespace ppapi | 145 } // namespace ppapi |
| 127 | 146 |
| 128 #endif // PPAPI_HOST_RESOURCE_MESSAGE_FILTER_H_ | 147 #endif // PPAPI_HOST_RESOURCE_MESSAGE_FILTER_H_ |
| OLD | NEW |