Chromium Code Reviews| Index: ppapi/host/resource_message_filter.h |
| diff --git a/ppapi/host/resource_message_filter.h b/ppapi/host/resource_message_filter.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..14d84ea5bf77dcb4b75fbc68723390a2b39991db |
| --- /dev/null |
| +++ b/ppapi/host/resource_message_filter.h |
| @@ -0,0 +1,118 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef PPAPI_HOST_RESOURCE_MESSAGE_FILTER_H_ |
| +#define PPAPI_HOST_RESOURCE_MESSAGE_FILTER_H_ |
| + |
| +#include "base/memory/ref_counted.h" |
| +#include "ppapi/c/pp_stdint.h" |
| +#include "ppapi/host/ppapi_host_export.h" |
| +#include "ppapi/host/resource_message_handler.h" |
| + |
| +namespace base { |
| +class MessageLoopProxy; |
| +class TaskRunner; |
| +} |
| + |
| +namespace content { |
| +class BrowserPpapiHost; |
| +class RenderViewHost; |
| +} |
| + |
| +namespace IPC { |
| +class Message; |
| +} |
| + |
| +namespace ppapi { |
| +namespace host { |
| + |
| +struct HostMessageContext; |
| +struct ReplyMessageContext; |
| +class ResourceHost; |
| + |
| +// This is the base class of resource message filters that can handle resource |
| +// messages on another thread. ResourceHosts can handle most messages |
| +// directly, but if they need to handle something on a different thread it is |
| +// inconvenient. This class makes handling that case easier. This class is |
| +// similar to a BrowserMessageFilter but for resource messages. |
| +// |
| +// To handle a resource message on another thread you should implement a |
| +// subclass as follows: |
| +// class MyMessageFilter : public ResourceMessageFilter { |
| +// protected: |
| +// virtual scoped_refptr<base::TaskRunner> OverrideTaskRunnerForMessage( |
| +// const IPC::Message& message) OVERRIDE { |
| +// if (message.type() == MyMessage::ID) |
| +// return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); |
| +// return NULL; |
| +// } |
| +// |
| +// virtual int32_t OnResourceMessageReceived( |
| +// const IPC::Message& msg, |
| +// HostMessageContext* context) OVERRIDE { |
| +// IPC_BEGIN_MESSAGE_MAP(MyMessageFilter, msg) |
| +// PPAPI_DISPATCH_HOST_RESOURCE_CALL(MyMessage, OnMyMessage) |
| +// IPC_END_MESSAGE_MAP() |
| +// return PP_ERROR_FAILED; |
| +// } |
| +// |
| +// private: |
| +// int32_t OnMyMessage(ppapi::host::HostMessageContext* context, ...) { |
| +// // Will be run on the UI thread. |
| +// } |
| +// } |
| +// |
| +// The filter should then be added in the resource host using: |
| +// AddFilter(make_scoped_ptr(new MyMessageFilter)); |
|
brettw
2012/11/16 19:27:54
I think it's worth calling out explicitly in this
raymes
2012/11/16 19:42:49
Done.
|
| +class PPAPI_HOST_EXPORT ResourceMessageFilter |
| + : public ResourceMessageHandler, |
| + public base::RefCountedThreadSafe<ResourceMessageFilter> { |
| + public: |
| + ResourceMessageFilter(); |
| + // Test constructor. Allows you to specify the IO message loop (which will be |
| + // used to dispatch replies on). |
| + ResourceMessageFilter( |
| + scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy); |
| + |
| + // Called when a filter is added to a ResourceHost. |
| + void OnFilterAdded(ResourceHost* resource_host); |
| + // Called when a filter is removed from a ResourceHost. |
| + void OnFilterDestroyed(); |
| + |
| + // This will dispatch the message handler on the target thread. It returns |
| + // true if the message was handled by this filter and false otherwise. |
| + virtual bool HandleMessage(const IPC::Message& msg, |
| + HostMessageContext* context) OVERRIDE; |
| + |
| + virtual void SendReply(const ReplyMessageContext& context, |
| + const IPC::Message& msg) OVERRIDE; |
| + |
| + protected: |
| + friend class base::RefCountedThreadSafe<ResourceMessageFilter>; |
| + virtual ~ResourceMessageFilter(); |
| + |
| + // If you want the message to be dispatched via the TaskRunner, |
| + // return a non-null task runner which will target tasks accordingly. |
| + virtual scoped_refptr<base::TaskRunner> OverrideTaskRunnerForMessage( |
| + const IPC::Message& message); |
| + |
| +private: |
|
brettw
2012/11/16 19:27:54
Nit: one more space.
raymes
2012/11/16 19:42:49
Done.
|
| + // This method is posted to the target thread and runs the message handler. |
| + void DispatchMessage(const IPC::Message& msg, |
| + HostMessageContext context); |
| + |
| + // Message loop to send resource message replies on. |
| + scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; |
| + |
| + // Non-owning pointer to the resource host owning this filter. Should only be |
| + // accessed from the IO thread. |
| + ResourceHost* resource_host_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ResourceMessageFilter); |
| +}; |
| + |
| +} // namespace host |
| +} // namespace ppapi |
| + |
| +#endif // PPAPI_HOST_RESOURCE_MESSAGE_FILTER_H_ |