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.
5 #ifndef PPAPI_HOST_RESOURCE_MESSAGE_FILTER_H_
6 #define PPAPI_HOST_RESOURCE_MESSAGE_FILTER_H_
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"
15 class MessageLoopProxy
;
27 class ResourceMessageFilter
;
31 struct PPAPI_HOST_EXPORT ResourceMessageFilterDeleteTraits
{
32 static void Destruct(const ResourceMessageFilter
* filter
);
35 } // namespace internal
37 // This is the base class of resource message filters that can handle resource
38 // messages on another thread. ResourceHosts can handle most messages
39 // directly, but if they need to handle something on a different thread it is
40 // inconvenient. This class makes handling that case easier. This class is
41 // similar to a BrowserMessageFilter but for resource messages. Note that the
42 // liftetime of a ResourceHost is managed by a PpapiHost and may be destroyed
43 // before or while your message is being processed on another thread.
44 // If this is the case, the message handler will always be called but a reply
45 // may not be sent back to the host.
47 // To handle a resource message on another thread you should implement a
48 // subclass as follows:
49 // class MyMessageFilter : public ResourceMessageFilter {
51 // virtual scoped_refptr<base::TaskRunner> OverrideTaskRunnerForMessage(
52 // const IPC::Message& message) OVERRIDE {
53 // if (message.type() == MyMessage::ID)
54 // return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI);
58 // virtual int32_t OnResourceMessageReceived(
59 // const IPC::Message& msg,
60 // HostMessageContext* context) OVERRIDE {
61 // IPC_BEGIN_MESSAGE_MAP(MyMessageFilter, msg)
62 // PPAPI_DISPATCH_HOST_RESOURCE_CALL(MyMessage, OnMyMessage)
63 // IPC_END_MESSAGE_MAP()
64 // return PP_ERROR_FAILED;
68 // int32_t OnMyMessage(ppapi::host::HostMessageContext* context, ...) {
69 // // Will be run on the UI thread.
73 // The filter should then be added in the resource host using:
74 // AddFilter(make_scoped_refptr(new MyMessageFilter));
75 class PPAPI_HOST_EXPORT ResourceMessageFilter
76 : public ResourceMessageHandler
,
77 public base::RefCountedThreadSafe
<
78 ResourceMessageFilter
, internal::ResourceMessageFilterDeleteTraits
> {
80 // This object must be constructed on the same thread that a reply message
81 // should be sent, i.e. the IO thread when constructed in the browser process
82 // or the main thread when constructed in the renderer process. Since
83 // ResourceMessageFilters are usually constructed in the constructor of the
84 // owning ResourceHost, this will almost always be the case anyway.
85 // The object will be deleted on the creation thread.
86 ResourceMessageFilter();
87 // Test constructor. Allows you to specify the message loop which will be used
88 // to dispatch replies on.
89 ResourceMessageFilter(
90 scoped_refptr
<base::MessageLoopProxy
> reply_thread_message_loop_proxy
);
92 // Called when a filter is added to a ResourceHost.
93 void OnFilterAdded(ResourceHost
* resource_host
);
94 // Called when a filter is removed from a ResourceHost.
95 void OnFilterDestroyed();
97 // This will dispatch the message handler on the target thread. It returns
98 // true if the message was handled by this filter and false otherwise.
99 virtual bool HandleMessage(const IPC::Message
& msg
,
100 HostMessageContext
* context
) OVERRIDE
;
102 // This can be called from any thread.
103 virtual void SendReply(const ReplyMessageContext
& context
,
104 const IPC::Message
& msg
) OVERRIDE
;
107 virtual ~ResourceMessageFilter();
109 // If you want the message to be handled on another thread, return a non-null
110 // task runner which will target tasks accordingly.
111 virtual scoped_refptr
<base::TaskRunner
> OverrideTaskRunnerForMessage(
112 const IPC::Message
& message
);
115 friend class base::DeleteHelper
<ResourceMessageFilter
>;
116 friend class base::RefCountedThreadSafe
<
117 ResourceMessageFilter
, internal::ResourceMessageFilterDeleteTraits
>;
118 friend struct internal::ResourceMessageFilterDeleteTraits
;
120 // This method is posted to the target thread and runs the message handler.
121 void DispatchMessage(const IPC::Message
& msg
,
122 HostMessageContext context
);
124 scoped_refptr
<base::MessageLoopProxy
> deletion_message_loop_proxy_
;
126 // Message loop to send resource message replies on. This will be the message
127 // loop proxy of the IO thread for the browser process or the main thread for
128 // the renderer process.
129 scoped_refptr
<base::MessageLoopProxy
> reply_thread_message_loop_proxy_
;
131 // Non-owning pointer to the resource host owning this filter. Should only be
132 // accessed from the thread which sends messages to the plugin resource (i.e.
133 // the IO thread for the browser process or the main thread for the renderer).
134 // This will be NULL upon creation of the filter and is set to the owning
135 // ResourceHost when |OnFilterAdded| is called. When the owning ResourceHost
136 // is destroyed, |OnFilterDestroyed| is called and this will be set to NULL.
137 ResourceHost
* resource_host_
;
139 DISALLOW_COPY_AND_ASSIGN(ResourceMessageFilter
);
145 #endif // PPAPI_HOST_RESOURCE_MESSAGE_FILTER_H_