Index: content/public/browser/browser_ppapi_host.h |
diff --git a/content/public/browser/browser_ppapi_host.h b/content/public/browser/browser_ppapi_host.h |
index ac62a07b749be83a7dd4f0595cc1bce7074dcb0f..e3a4a07a00e4487969cb78c78f76270127e3b757 100644 |
--- a/content/public/browser/browser_ppapi_host.h |
+++ b/content/public/browser/browser_ppapi_host.h |
@@ -8,6 +8,8 @@ |
#include "base/callback_forward.h" |
#include "base/process.h" |
#include "content/common/content_export.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "content/public/browser/render_process_host.h" |
#include "content/public/browser/render_view_host.h" |
#include "googleurl/src/gurl.h" |
#include "ppapi/c/pp_instance.h" |
@@ -82,6 +84,58 @@ class CONTENT_EXPORT BrowserPpapiHost { |
// Get the Document/Plugin URLs for the given PP_Instance. |
virtual GURL GetDocumentURLForInstance(PP_Instance instance) = 0; |
virtual GURL GetPluginURLForInstance(PP_Instance instance) = 0; |
+ |
+ // Schedules the given callback to execute on the UI thread of the browser, |
+ // passing the RenderViewHost associated with the given instance as a |
+ // parameter. |
+ // |
+ // Normally this would be called from a ResourceHost with the reply using |
+ // a weak pointer to itself. |
+ // |
+ // Importantly, the task will not be run if the RenderView is destroyed by |
+ // the time we get to the UI thread, or if the PP_Instance is invalid, but |
+ // the reply will still be run. The return type in this case will be a |
+ // default-constructed |ReturnType|. |
+ // |
+ // So you may want to make sure you don't do silly things in the reply |
+ // handler if the task on the UI thread is never run and you get a |
+ // default-constructed result. |
+ template<typename ReturnType> |
+ bool PostOnUIThreadWithRenderViewHostAndReply( |
dmichael (off chromium)
2013/01/10 17:30:47
This is cool, but I think it probably doesn't belo
ygorshenin1
2013/01/11 11:42:45
OK, I'll move these methods to pepper_socket_utils
|
+ const tracked_objects::Location& from_here, |
+ PP_Instance instance, |
+ const base::Callback<ReturnType(RenderViewHost*)>& task, |
+ const base::Callback<void(ReturnType)>& reply) const { |
+ int render_process_id, render_view_id; |
+ bool success = GetRenderViewIDsForInstance(instance, |
+ &render_process_id, |
+ &render_view_id); |
+ if (!success) |
+ return false; |
+ BrowserThread::PostTaskAndReplyWithResult( |
+ BrowserThread::UI, |
+ from_here, |
+ base::Bind(&CallWithRenderViewHost<ReturnType>, |
+ render_process_id, render_view_id, task), |
+ reply); |
+ return true; |
+ } |
+ |
+ protected: |
+ // Backend for PostOnUIThreadWithRenderViewAndReply. This converts the IDs |
+ // to a RenderViewHost and calls the function, or returns a |
+ // default-constructed return value on error. |
+ template<typename ReturnType> |
+ static ReturnType CallWithRenderViewHost( |
+ int render_process_id, |
+ int render_view_id, |
+ const base::Callback<ReturnType(RenderViewHost*)>& task) { |
+ RenderViewHost* render_view_host = RenderViewHost::FromID(render_process_id, |
+ render_view_id); |
+ if (render_view_host) |
+ return task.Run(render_view_host); |
+ return ReturnType(); |
+ } |
}; |
} // namespace content |