OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_SOCKET_UTILS_H_ |
| 6 #define CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_SOCKET_UTILS_H_ |
| 7 |
| 8 #include "base/callback.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/tracked_objects.h" |
| 11 #include "content/public/browser/browser_ppapi_host.h" |
| 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/browser/render_process_host.h" |
| 14 #include "content/public/browser/render_view_host.h" |
| 15 #include "content/public/common/process_type.h" |
| 16 #include "content/public/common/socket_permission_request.h" |
| 17 #include "ppapi/c/pp_instance.h" |
| 18 |
| 19 struct PP_NetAddress_Private; |
| 20 |
| 21 namespace content { |
| 22 |
| 23 class RenderViewHost; |
| 24 |
| 25 namespace pepper_socket_utils { |
| 26 |
| 27 SocketPermissionRequest CreateSocketPermissionRequest( |
| 28 SocketPermissionRequest::OperationType type, |
| 29 const PP_NetAddress_Private& net_addr); |
| 30 |
| 31 bool CanUseSocketAPIs(ProcessType plugin_process_type, |
| 32 const SocketPermissionRequest& params, |
| 33 RenderViewHost* render_view_host); |
| 34 |
| 35 // Backend for PostOnUIThreadWithRenderViewHostAndReply. This converts the IDs |
| 36 // to a RenderViewHost and calls the function, or returns a |
| 37 // default-constructed return value on error. |
| 38 template<typename ReturnType> |
| 39 ReturnType CallWithRenderViewHost( |
| 40 int render_process_id, |
| 41 int render_view_id, |
| 42 const base::Callback<ReturnType(RenderViewHost*)>& task) { |
| 43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 44 |
| 45 RenderViewHost* render_view_host = RenderViewHost::FromID(render_process_id, |
| 46 render_view_id); |
| 47 if (render_view_host) |
| 48 return task.Run(render_view_host); |
| 49 return ReturnType(); |
| 50 } |
| 51 |
| 52 // Schedules the given callback to execute on the UI thread of the browser, |
| 53 // passing the RenderViewHost associated with the given instance as a |
| 54 // parameter. |
| 55 // |
| 56 // Normally this would be called from a ResourceHost with the reply using |
| 57 // a weak pointer to itself. |
| 58 // |
| 59 // Importantly, the task will not be run if the RenderView is destroyed by |
| 60 // the time we get to the UI thread, or if the PP_Instance is invalid, but |
| 61 // the reply will still be run. The return type in this case will be a |
| 62 // default-constructed |ReturnType|. |
| 63 // |
| 64 // So you may want to make sure you don't do silly things in the reply |
| 65 // handler if the task on the UI thread is never run and you get a |
| 66 // default-constructed result. |
| 67 template<typename ReturnType> |
| 68 bool PostOnUIThreadWithRenderViewHostAndReply( |
| 69 const BrowserPpapiHost* browser_ppapi_host, |
| 70 const tracked_objects::Location& from_here, |
| 71 PP_Instance instance, |
| 72 const base::Callback<ReturnType(RenderViewHost*)>& task, |
| 73 const base::Callback<void(ReturnType)>& reply) { |
| 74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 75 |
| 76 int render_process_id, render_view_id; |
| 77 bool success = browser_ppapi_host->GetRenderViewIDsForInstance( |
| 78 instance, |
| 79 &render_process_id, |
| 80 &render_view_id); |
| 81 if (!success) |
| 82 return false; |
| 83 BrowserThread::PostTaskAndReplyWithResult( |
| 84 BrowserThread::UI, |
| 85 from_here, |
| 86 base::Bind(&CallWithRenderViewHost<ReturnType>, |
| 87 render_process_id, render_view_id, task), |
| 88 reply); |
| 89 return true; |
| 90 } |
| 91 |
| 92 } // namespace pepper_socket_utils |
| 93 |
| 94 } // namespace content |
| 95 |
| 96 #endif // CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_SOCKET_UTILS_H_ |
OLD | NEW |