[email protected] | 00d320a | 2012-02-14 00:27:04 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "ppapi/proxy/ppb_testing_proxy.h" |
| 6 | |
avi | e029c413 | 2015-12-23 06:45:22 | [diff] [blame^] | 7 | #include <stddef.h> |
| 8 | |
[email protected] | a7629597 | 2013-07-18 00:42:32 | [diff] [blame] | 9 | #include "base/message_loop/message_loop.h" |
[email protected] | 7310b16 | 2013-11-25 22:39:15 | [diff] [blame] | 10 | #include "ppapi/c/private/ppb_testing_private.h" |
[email protected] | ed33e2e | 2011-11-20 21:18:07 | [diff] [blame] | 11 | #include "ppapi/proxy/enter_proxy.h" |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 12 | #include "ppapi/proxy/plugin_dispatcher.h" |
| 13 | #include "ppapi/proxy/ppapi_messages.h" |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 14 | #include "ppapi/shared_impl/ppapi_globals.h" |
[email protected] | 4c41d3f | 2012-02-15 01:44:47 | [diff] [blame] | 15 | #include "ppapi/shared_impl/proxy_lock.h" |
[email protected] | 7f8b26b | 2011-08-18 15:41:01 | [diff] [blame] | 16 | #include "ppapi/shared_impl/resource.h" |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 17 | #include "ppapi/shared_impl/resource_tracker.h" |
[email protected] | ed33e2e | 2011-11-20 21:18:07 | [diff] [blame] | 18 | #include "ppapi/thunk/enter.h" |
[email protected] | 08e65c1 | 2012-11-29 18:53:29 | [diff] [blame] | 19 | #include "ppapi/thunk/ppb_graphics_2d_api.h" |
[email protected] | ed33e2e | 2011-11-20 21:18:07 | [diff] [blame] | 20 | #include "ppapi/thunk/ppb_input_event_api.h" |
| 21 | |
[email protected] | 961e897 | 2011-12-15 15:29:19 | [diff] [blame] | 22 | using ppapi::thunk::EnterInstance; |
[email protected] | 08e65c1 | 2012-11-29 18:53:29 | [diff] [blame] | 23 | using ppapi::thunk::EnterResource; |
[email protected] | 4c41d3f | 2012-02-15 01:44:47 | [diff] [blame] | 24 | using ppapi::thunk::EnterResourceNoLock; |
[email protected] | 08e65c1 | 2012-11-29 18:53:29 | [diff] [blame] | 25 | using ppapi::thunk::PPB_Graphics2D_API; |
[email protected] | ed33e2e | 2011-11-20 21:18:07 | [diff] [blame] | 26 | using ppapi::thunk::PPB_InputEvent_API; |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 27 | |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 28 | namespace ppapi { |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 29 | namespace proxy { |
| 30 | |
| 31 | namespace { |
| 32 | |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 33 | PP_Bool ReadImageData(PP_Resource graphics_2d, |
[email protected] | 19d2b01 | 2010-11-08 16:32:18 | [diff] [blame] | 34 | PP_Resource image, |
| 35 | const PP_Point* top_left) { |
[email protected] | 4c41d3f | 2012-02-15 01:44:47 | [diff] [blame] | 36 | ProxyAutoLock lock; |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 37 | Resource* image_object = |
| 38 | PpapiGlobals::Get()->GetResourceTracker()->GetResource(image); |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 39 | if (!image_object) |
| 40 | return PP_FALSE; |
[email protected] | 7f8b26b | 2011-08-18 15:41:01 | [diff] [blame] | 41 | Resource* graphics_2d_object = |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 42 | PpapiGlobals::Get()->GetResourceTracker()->GetResource(graphics_2d); |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 43 | if (!graphics_2d_object || |
[email protected] | 7f8b26b | 2011-08-18 15:41:01 | [diff] [blame] | 44 | image_object->pp_instance() != graphics_2d_object->pp_instance()) |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 45 | return PP_FALSE; |
| 46 | |
[email protected] | 08e65c1 | 2012-11-29 18:53:29 | [diff] [blame] | 47 | EnterResourceNoLock<PPB_Graphics2D_API> enter(graphics_2d, true); |
| 48 | if (enter.failed()) |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 49 | return PP_FALSE; |
[email protected] | 08e65c1 | 2012-11-29 18:53:29 | [diff] [blame] | 50 | const HostResource& host_image = image_object->host_resource(); |
| 51 | return enter.object()->ReadImageData(host_image.host_resource(), top_left) ? |
| 52 | PP_TRUE : PP_FALSE; |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 53 | } |
| 54 | |
[email protected] | 7358d57 | 2011-02-15 18:44:40 | [diff] [blame] | 55 | void RunMessageLoop(PP_Instance instance) { |
[email protected] | d2881d8 | 2013-05-06 19:23:08 | [diff] [blame] | 56 | base::MessageLoop::ScopedNestableTaskAllower allow( |
| 57 | base::MessageLoop::current()); |
[email protected] | 77c3417 | 2012-11-08 18:55:16 | [diff] [blame] | 58 | CHECK(PpapiGlobals::Get()->GetMainThreadMessageLoop()-> |
[email protected] | d2881d8 | 2013-05-06 19:23:08 | [diff] [blame] | 59 | BelongsToCurrentThread()); |
| 60 | base::MessageLoop::current()->Run(); |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 61 | } |
| 62 | |
[email protected] | 7358d57 | 2011-02-15 18:44:40 | [diff] [blame] | 63 | void QuitMessageLoop(PP_Instance instance) { |
[email protected] | 77c3417 | 2012-11-08 18:55:16 | [diff] [blame] | 64 | CHECK(PpapiGlobals::Get()->GetMainThreadMessageLoop()-> |
| 65 | BelongsToCurrentThread()); |
[email protected] | d2881d8 | 2013-05-06 19:23:08 | [diff] [blame] | 66 | base::MessageLoop::current()->QuitNow(); |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 67 | } |
| 68 | |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 69 | uint32_t GetLiveObjectsForInstance(PP_Instance instance_id) { |
[email protected] | 4c41d3f | 2012-02-15 01:44:47 | [diff] [blame] | 70 | ProxyAutoLock lock; |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 71 | PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance_id); |
| 72 | if (!dispatcher) |
| 73 | return static_cast<uint32_t>(-1); |
| 74 | |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 75 | uint32_t result = 0; |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 76 | dispatcher->Send(new PpapiHostMsg_PPBTesting_GetLiveObjectsForInstance( |
[email protected] | ac4b54d | 2011-10-20 23:09:28 | [diff] [blame] | 77 | API_ID_PPB_TESTING, instance_id, &result)); |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 78 | return result; |
| 79 | } |
| 80 | |
[email protected] | 5d904b9e | 2011-08-30 19:38:31 | [diff] [blame] | 81 | PP_Bool IsOutOfProcess() { |
| 82 | return PP_TRUE; |
| 83 | } |
| 84 | |
tommycli | ee0a49b | 2015-05-05 19:40:49 | [diff] [blame] | 85 | void PostPowerSaverStatus(PP_Instance instance_id) { |
tommycli | 0dd1301 | 2015-04-24 20:27:22 | [diff] [blame] | 86 | ProxyAutoLock lock; |
| 87 | PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance_id); |
| 88 | if (!dispatcher) |
tommycli | ee0a49b | 2015-05-05 19:40:49 | [diff] [blame] | 89 | return; |
tommycli | 0dd1301 | 2015-04-24 20:27:22 | [diff] [blame] | 90 | |
tommycli | ee0a49b | 2015-05-05 19:40:49 | [diff] [blame] | 91 | dispatcher->Send(new PpapiHostMsg_PPBTesting_PostPowerSaverStatus( |
| 92 | API_ID_PPB_TESTING, instance_id)); |
| 93 | } |
| 94 | |
| 95 | void SubscribeToPowerSaverNotifications(PP_Instance instance_id) { |
| 96 | ProxyAutoLock lock; |
| 97 | PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance_id); |
| 98 | if (!dispatcher) |
| 99 | return; |
| 100 | |
| 101 | dispatcher->Send( |
| 102 | new PpapiHostMsg_PPBTesting_SubscribeToPowerSaverNotifications( |
| 103 | API_ID_PPB_TESTING, instance_id)); |
tommycli | 0dd1301 | 2015-04-24 20:27:22 | [diff] [blame] | 104 | } |
| 105 | |
[email protected] | ed33e2e | 2011-11-20 21:18:07 | [diff] [blame] | 106 | void SimulateInputEvent(PP_Instance instance_id, PP_Resource input_event) { |
[email protected] | 4c41d3f | 2012-02-15 01:44:47 | [diff] [blame] | 107 | ProxyAutoLock lock; |
[email protected] | ed33e2e | 2011-11-20 21:18:07 | [diff] [blame] | 108 | PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance_id); |
| 109 | if (!dispatcher) |
| 110 | return; |
[email protected] | 4c41d3f | 2012-02-15 01:44:47 | [diff] [blame] | 111 | EnterResourceNoLock<PPB_InputEvent_API> enter(input_event, false); |
[email protected] | ed33e2e | 2011-11-20 21:18:07 | [diff] [blame] | 112 | if (enter.failed()) |
| 113 | return; |
| 114 | |
| 115 | const InputEventData& input_event_data = enter.object()->GetInputEventData(); |
| 116 | dispatcher->Send(new PpapiHostMsg_PPBTesting_SimulateInputEvent( |
| 117 | API_ID_PPB_TESTING, instance_id, input_event_data)); |
| 118 | } |
| 119 | |
[email protected] | 961e897 | 2011-12-15 15:29:19 | [diff] [blame] | 120 | PP_Var GetDocumentURL(PP_Instance instance, PP_URLComponents_Dev* components) { |
| 121 | EnterInstance enter(instance); |
| 122 | if (enter.failed()) |
| 123 | return PP_MakeUndefined(); |
| 124 | return enter.functions()->GetDocumentURL(instance, components); |
| 125 | } |
| 126 | |
[email protected] | a732cec | 2011-12-22 08:35:52 | [diff] [blame] | 127 | // TODO(dmichael): Ideally we could get a way to check the number of vars in the |
| 128 | // host-side tracker when running out-of-process, to make sure the proxy does |
| 129 | // not leak host-side vars. |
| 130 | uint32_t GetLiveVars(PP_Var live_vars[], uint32_t array_size) { |
[email protected] | 4c41d3f | 2012-02-15 01:44:47 | [diff] [blame] | 131 | ProxyAutoLock lock; |
[email protected] | a732cec | 2011-12-22 08:35:52 | [diff] [blame] | 132 | std::vector<PP_Var> vars = |
| 133 | PpapiGlobals::Get()->GetVarTracker()->GetLiveVars(); |
| 134 | for (size_t i = 0u; |
| 135 | i < std::min(static_cast<size_t>(array_size), vars.size()); |
| 136 | ++i) |
| 137 | live_vars[i] = vars[i]; |
brettw | 669d47b1 | 2015-02-13 21:17:38 | [diff] [blame] | 138 | return static_cast<uint32_t>(vars.size()); |
[email protected] | a732cec | 2011-12-22 08:35:52 | [diff] [blame] | 139 | } |
| 140 | |
[email protected] | 38f428f1 | 2013-04-19 14:46:05 | [diff] [blame] | 141 | void SetMinimumArrayBufferSizeForShmem(PP_Instance instance, |
| 142 | uint32_t threshold) { |
| 143 | ProxyAutoLock lock; |
| 144 | RawVarDataGraph::SetMinimumArrayBufferSizeForShmemForTest(threshold); |
| 145 | PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); |
| 146 | if (!dispatcher) |
| 147 | return; |
| 148 | dispatcher->Send( |
| 149 | new PpapiHostMsg_PPBTesting_SetMinimumArrayBufferSizeForShmem( |
| 150 | API_ID_PPB_TESTING, threshold)); |
| 151 | } |
| 152 | |
[email protected] | 45c897d | 2014-08-11 04:12:46 | [diff] [blame] | 153 | void RunV8GC(PP_Instance instance) { |
| 154 | // TODO(raymes): Implement this if we need it. |
| 155 | NOTIMPLEMENTED(); |
| 156 | } |
| 157 | |
[email protected] | 7310b16 | 2013-11-25 22:39:15 | [diff] [blame] | 158 | const PPB_Testing_Private testing_interface = { |
tommycli | 0dd1301 | 2015-04-24 20:27:22 | [diff] [blame] | 159 | &ReadImageData, |
| 160 | &RunMessageLoop, |
| 161 | &QuitMessageLoop, |
| 162 | &GetLiveObjectsForInstance, |
| 163 | &IsOutOfProcess, |
tommycli | ee0a49b | 2015-05-05 19:40:49 | [diff] [blame] | 164 | &PostPowerSaverStatus, |
| 165 | &SubscribeToPowerSaverNotifications, |
tommycli | 0dd1301 | 2015-04-24 20:27:22 | [diff] [blame] | 166 | &SimulateInputEvent, |
| 167 | &GetDocumentURL, |
| 168 | &GetLiveVars, |
| 169 | &SetMinimumArrayBufferSizeForShmem, |
| 170 | &RunV8GC}; |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 171 | |
| 172 | } // namespace |
| 173 | |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 174 | PPB_Testing_Proxy::PPB_Testing_Proxy(Dispatcher* dispatcher) |
| 175 | : InterfaceProxy(dispatcher), |
| 176 | ppb_testing_impl_(NULL) { |
| 177 | if (!dispatcher->IsPlugin()) { |
[email protected] | 7310b16 | 2013-11-25 22:39:15 | [diff] [blame] | 178 | ppb_testing_impl_ = static_cast<const PPB_Testing_Private*>( |
| 179 | dispatcher->local_get_interface()(PPB_TESTING_PRIVATE_INTERFACE)); |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 180 | } |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | PPB_Testing_Proxy::~PPB_Testing_Proxy() { |
| 184 | } |
| 185 | |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 186 | // static |
[email protected] | 637fc4cd | 2013-12-06 22:08:15 | [diff] [blame] | 187 | const PPB_Testing_Private* PPB_Testing_Proxy::GetProxyInterface() { |
| 188 | return &testing_interface; |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 189 | } |
| 190 | |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 191 | bool PPB_Testing_Proxy::OnMessageReceived(const IPC::Message& msg) { |
[email protected] | 27b04df | 2012-12-10 23:43:10 | [diff] [blame] | 192 | if (!dispatcher()->permissions().HasPermission(PERMISSION_TESTING)) |
| 193 | return false; |
| 194 | |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 195 | bool handled = true; |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 196 | IPC_BEGIN_MESSAGE_MAP(PPB_Testing_Proxy, msg) |
| 197 | IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBTesting_ReadImageData, |
| 198 | OnMsgReadImageData) |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 199 | IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBTesting_GetLiveObjectsForInstance, |
| 200 | OnMsgGetLiveObjectsForInstance) |
tommycli | ee0a49b | 2015-05-05 19:40:49 | [diff] [blame] | 201 | IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBTesting_PostPowerSaverStatus, |
| 202 | OnMsgPostPowerSaverStatus) |
| 203 | IPC_MESSAGE_HANDLER( |
| 204 | PpapiHostMsg_PPBTesting_SubscribeToPowerSaverNotifications, |
| 205 | OnMsgSubscribeToPowerSaverNotifications) |
[email protected] | ed33e2e | 2011-11-20 21:18:07 | [diff] [blame] | 206 | IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBTesting_SimulateInputEvent, |
| 207 | OnMsgSimulateInputEvent) |
[email protected] | 38f428f1 | 2013-04-19 14:46:05 | [diff] [blame] | 208 | IPC_MESSAGE_HANDLER( |
| 209 | PpapiHostMsg_PPBTesting_SetMinimumArrayBufferSizeForShmem, |
| 210 | OnMsgSetMinimumArrayBufferSizeForShmem) |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 211 | IPC_MESSAGE_UNHANDLED(handled = false) |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 212 | IPC_END_MESSAGE_MAP() |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 213 | return handled; |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 214 | } |
| 215 | |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 216 | void PPB_Testing_Proxy::OnMsgReadImageData( |
| 217 | const HostResource& device_context_2d, |
| 218 | const HostResource& image, |
| 219 | const PP_Point& top_left, |
| 220 | PP_Bool* result) { |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 221 | *result = ppb_testing_impl_->ReadImageData( |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 222 | device_context_2d.host_resource(), image.host_resource(), &top_left); |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 223 | } |
| 224 | |
[email protected] | 7358d57 | 2011-02-15 18:44:40 | [diff] [blame] | 225 | void PPB_Testing_Proxy::OnMsgRunMessageLoop(PP_Instance instance) { |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 226 | ppb_testing_impl_->RunMessageLoop(instance); |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 227 | } |
| 228 | |
[email protected] | 7358d57 | 2011-02-15 18:44:40 | [diff] [blame] | 229 | void PPB_Testing_Proxy::OnMsgQuitMessageLoop(PP_Instance instance) { |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 230 | ppb_testing_impl_->QuitMessageLoop(instance); |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 231 | } |
| 232 | |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 233 | void PPB_Testing_Proxy::OnMsgGetLiveObjectsForInstance(PP_Instance instance, |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 234 | uint32_t* result) { |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 235 | *result = ppb_testing_impl_->GetLiveObjectsForInstance(instance); |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 236 | } |
| 237 | |
tommycli | ee0a49b | 2015-05-05 19:40:49 | [diff] [blame] | 238 | void PPB_Testing_Proxy::OnMsgPostPowerSaverStatus(PP_Instance instance) { |
| 239 | ppb_testing_impl_->PostPowerSaverStatus(instance); |
| 240 | } |
| 241 | |
| 242 | void PPB_Testing_Proxy::OnMsgSubscribeToPowerSaverNotifications( |
| 243 | PP_Instance instance) { |
| 244 | ppb_testing_impl_->SubscribeToPowerSaverNotifications(instance); |
tommycli | 0dd1301 | 2015-04-24 20:27:22 | [diff] [blame] | 245 | } |
| 246 | |
[email protected] | ed33e2e | 2011-11-20 21:18:07 | [diff] [blame] | 247 | void PPB_Testing_Proxy::OnMsgSimulateInputEvent( |
| 248 | PP_Instance instance, |
| 249 | const InputEventData& input_event) { |
[email protected] | 9a57839 | 2011-12-07 18:59:27 | [diff] [blame] | 250 | scoped_refptr<PPB_InputEvent_Shared> input_event_impl( |
[email protected] | 00d320a | 2012-02-14 00:27:04 | [diff] [blame] | 251 | new PPB_InputEvent_Shared(OBJECT_IS_PROXY, instance, input_event)); |
[email protected] | ed33e2e | 2011-11-20 21:18:07 | [diff] [blame] | 252 | ppb_testing_impl_->SimulateInputEvent(instance, |
| 253 | input_event_impl->pp_resource()); |
| 254 | } |
| 255 | |
[email protected] | 38f428f1 | 2013-04-19 14:46:05 | [diff] [blame] | 256 | void PPB_Testing_Proxy::OnMsgSetMinimumArrayBufferSizeForShmem( |
| 257 | uint32_t threshold) { |
| 258 | RawVarDataGraph::SetMinimumArrayBufferSizeForShmemForTest(threshold); |
| 259 | } |
| 260 | |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 261 | } // namespace proxy |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 262 | } // namespace ppapi |