Index: ppapi/host/background_thread_message_filter.h |
diff --git a/ppapi/host/background_thread_message_filter.h b/ppapi/host/background_thread_message_filter.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..34807be82ce79f99c1603f2f24bbb197b769d5ac |
--- /dev/null |
+++ b/ppapi/host/background_thread_message_filter.h |
@@ -0,0 +1,139 @@ |
+// 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_BACKGROUND_THREAD_MESSAGE_FILTER_H_ |
+#define PPAPI_HOST_BACKGROUND_THREAD_MESSAGE_FILTER_H_ |
+ |
+#include "base/memory/ref_counted.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "ppapi/c/pp_instance.h" |
+#include "ppapi/c/pp_stdint.h" |
+#include "ppapi/host/ppapi_host_export.h" |
+#include "ppapi/host/resource_host_message_filter.h" |
+ |
+namespace base { |
+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 BackgroundThreadMessageFilter { |
+// protected: |
+// virtual bool OverrideThreadForMessage( |
+// const IPC::Message& message, |
+// content::BrowserThread::ID* thread) OVERRIDE { |
+// if (message.type() == MyMessage::ID) { |
+// *thread = content::BrowserThread::UI; |
+// return true; |
+// } |
+// return false; |
+// } |
+// |
+// 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(new MyMessageFilter); |
+class PPAPI_HOST_EXPORT BackgroundThreadMessageFilter |
+ : public ResourceHostMessageFilter, |
+ public base::RefCountedThreadSafe<BackgroundThreadMessageFilter> { |
+ public: |
+ BackgroundThreadMessageFilter(); |
+ |
+ // 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<BackgroundThreadMessageFilter>; |
+ virtual ~BackgroundThreadMessageFilter(); |
+ |
+ // If you want the message to be dispatched on a particular well-known |
+ // browser thread, change |thread| to the id of the target thread. If the |
+ // given message is handled on on a different thread then true should be |
+ // returned. |
+ virtual bool OverrideThreadForMessage( |
+ const IPC::Message& message, |
+ content::BrowserThread::ID* thread); |
+ |
+ // If you want the message to be dispatched via the SequencedWorkerPool, |
+ // return a non-null task runner which will target tasks accordingly. |
+ virtual base::TaskRunner* OverrideTaskRunnerForMessage( |
+ const IPC::Message& message); |
+ |
+private: |
+ // This method is posted to the target thread and runs the message handler. |
+ void DispatchMessage(const IPC::Message& msg, |
+ HostMessageContext context); |
+ |
+ // Non-owning pointer to the resource host owning this filter. |
yzshen1
2012/11/13 21:51:48
Please comment that it should only be accessed on
raymes
2012/11/16 18:56:35
Done.
|
+ ResourceHost* resource_host_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(BackgroundThreadMessageFilter); |
+}; |
+ |
+// This class is a BackgroundThreadMessageFilter which provides easy access to |
+// a RenderViewHost on a background thread. |
+class PPAPI_HOST_EXPORT RenderViewHostMessageFilter |
+ : public BackgroundThreadMessageFilter { |
+ public: |
+ RenderViewHostMessageFilter(content::BrowserPpapiHost* host, |
+ PP_Instance instance); |
+ |
+ // Returns the RenderViewHost for the instance passed in the constructor. |
+ // Safe to call from other threads. |
+ content::RenderViewHost* GetRenderViewHost(); |
+ |
+ private: |
+ int render_process_id_ = 0; |
+ int render_view_id_ = 0; |
+}; |
+ |
+} // namespace host |
+} // namespace ppapi |
+ |
+#endif // PPAPI_HOST_BACKGROUND_THREAD_MESSAGE_FILTER_H_ |