blob: 8e85ba2e44cf551d8a84bd99e6093b34c5a043d2 [file] [log] [blame]
[email protected]00d320a2012-02-14 00:27:041// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]1054c072010-11-05 22:52:482// 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
avie029c4132015-12-23 06:45:227#include <stddef.h>
8
[email protected]a76295972013-07-18 00:42:329#include "base/message_loop/message_loop.h"
fdoray095043f2016-06-22 13:20:4310#include "base/run_loop.h"
[email protected]7310b162013-11-25 22:39:1511#include "ppapi/c/private/ppb_testing_private.h"
[email protected]ed33e2e2011-11-20 21:18:0712#include "ppapi/proxy/enter_proxy.h"
[email protected]1054c072010-11-05 22:52:4813#include "ppapi/proxy/plugin_dispatcher.h"
14#include "ppapi/proxy/ppapi_messages.h"
[email protected]794d83cd2011-10-20 19:09:2015#include "ppapi/shared_impl/ppapi_globals.h"
[email protected]4c41d3f2012-02-15 01:44:4716#include "ppapi/shared_impl/proxy_lock.h"
[email protected]7f8b26b2011-08-18 15:41:0117#include "ppapi/shared_impl/resource.h"
[email protected]794d83cd2011-10-20 19:09:2018#include "ppapi/shared_impl/resource_tracker.h"
[email protected]ed33e2e2011-11-20 21:18:0719#include "ppapi/thunk/enter.h"
[email protected]08e65c12012-11-29 18:53:2920#include "ppapi/thunk/ppb_graphics_2d_api.h"
[email protected]ed33e2e2011-11-20 21:18:0721#include "ppapi/thunk/ppb_input_event_api.h"
22
[email protected]961e8972011-12-15 15:29:1923using ppapi::thunk::EnterInstance;
[email protected]08e65c12012-11-29 18:53:2924using ppapi::thunk::EnterResource;
[email protected]4c41d3f2012-02-15 01:44:4725using ppapi::thunk::EnterResourceNoLock;
[email protected]08e65c12012-11-29 18:53:2926using ppapi::thunk::PPB_Graphics2D_API;
[email protected]ed33e2e2011-11-20 21:18:0727using ppapi::thunk::PPB_InputEvent_API;
[email protected]1054c072010-11-05 22:52:4828
[email protected]4d2efd22011-08-18 21:58:0229namespace ppapi {
[email protected]1054c072010-11-05 22:52:4830namespace proxy {
31
32namespace {
33
[email protected]f24448db2011-01-27 20:40:3934PP_Bool ReadImageData(PP_Resource graphics_2d,
[email protected]19d2b012010-11-08 16:32:1835 PP_Resource image,
36 const PP_Point* top_left) {
[email protected]4c41d3f2012-02-15 01:44:4737 ProxyAutoLock lock;
[email protected]794d83cd2011-10-20 19:09:2038 Resource* image_object =
39 PpapiGlobals::Get()->GetResourceTracker()->GetResource(image);
[email protected]4614f192011-01-21 00:26:4340 if (!image_object)
41 return PP_FALSE;
[email protected]7f8b26b2011-08-18 15:41:0142 Resource* graphics_2d_object =
[email protected]794d83cd2011-10-20 19:09:2043 PpapiGlobals::Get()->GetResourceTracker()->GetResource(graphics_2d);
[email protected]f24448db2011-01-27 20:40:3944 if (!graphics_2d_object ||
[email protected]7f8b26b2011-08-18 15:41:0145 image_object->pp_instance() != graphics_2d_object->pp_instance())
[email protected]f24448db2011-01-27 20:40:3946 return PP_FALSE;
47
[email protected]08e65c12012-11-29 18:53:2948 EnterResourceNoLock<PPB_Graphics2D_API> enter(graphics_2d, true);
49 if (enter.failed())
[email protected]4614f192011-01-21 00:26:4350 return PP_FALSE;
[email protected]08e65c12012-11-29 18:53:2951 const HostResource& host_image = image_object->host_resource();
52 return enter.object()->ReadImageData(host_image.host_resource(), top_left) ?
53 PP_TRUE : PP_FALSE;
[email protected]1054c072010-11-05 22:52:4854}
55
[email protected]7358d572011-02-15 18:44:4056void RunMessageLoop(PP_Instance instance) {
[email protected]d2881d82013-05-06 19:23:0857 base::MessageLoop::ScopedNestableTaskAllower allow(
58 base::MessageLoop::current());
[email protected]77c34172012-11-08 18:55:1659 CHECK(PpapiGlobals::Get()->GetMainThreadMessageLoop()->
[email protected]d2881d82013-05-06 19:23:0860 BelongsToCurrentThread());
fdoray095043f2016-06-22 13:20:4361 base::RunLoop().Run();
[email protected]1054c072010-11-05 22:52:4862}
63
[email protected]7358d572011-02-15 18:44:4064void QuitMessageLoop(PP_Instance instance) {
[email protected]77c34172012-11-08 18:55:1665 CHECK(PpapiGlobals::Get()->GetMainThreadMessageLoop()->
66 BelongsToCurrentThread());
Gabriel Charette53a9ef812017-07-26 12:36:2367 base::RunLoop::QuitCurrentDeprecated();
[email protected]1054c072010-11-05 22:52:4868}
69
[email protected]4614f192011-01-21 00:26:4370uint32_t GetLiveObjectsForInstance(PP_Instance instance_id) {
[email protected]4c41d3f2012-02-15 01:44:4771 ProxyAutoLock lock;
[email protected]4614f192011-01-21 00:26:4372 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance_id);
73 if (!dispatcher)
74 return static_cast<uint32_t>(-1);
75
[email protected]1054c072010-11-05 22:52:4876 uint32_t result = 0;
[email protected]4614f192011-01-21 00:26:4377 dispatcher->Send(new PpapiHostMsg_PPBTesting_GetLiveObjectsForInstance(
[email protected]ac4b54d2011-10-20 23:09:2878 API_ID_PPB_TESTING, instance_id, &result));
[email protected]1054c072010-11-05 22:52:4879 return result;
80}
81
[email protected]5d904b9e2011-08-30 19:38:3182PP_Bool IsOutOfProcess() {
83 return PP_TRUE;
84}
85
tommycliee0a49b2015-05-05 19:40:4986void PostPowerSaverStatus(PP_Instance instance_id) {
tommycli0dd13012015-04-24 20:27:2287 ProxyAutoLock lock;
88 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance_id);
89 if (!dispatcher)
tommycliee0a49b2015-05-05 19:40:4990 return;
tommycli0dd13012015-04-24 20:27:2291
tommycliee0a49b2015-05-05 19:40:4992 dispatcher->Send(new PpapiHostMsg_PPBTesting_PostPowerSaverStatus(
93 API_ID_PPB_TESTING, instance_id));
94}
95
96void SubscribeToPowerSaverNotifications(PP_Instance instance_id) {
97 ProxyAutoLock lock;
98 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance_id);
99 if (!dispatcher)
100 return;
101
102 dispatcher->Send(
103 new PpapiHostMsg_PPBTesting_SubscribeToPowerSaverNotifications(
104 API_ID_PPB_TESTING, instance_id));
tommycli0dd13012015-04-24 20:27:22105}
106
[email protected]ed33e2e2011-11-20 21:18:07107void SimulateInputEvent(PP_Instance instance_id, PP_Resource input_event) {
[email protected]4c41d3f2012-02-15 01:44:47108 ProxyAutoLock lock;
[email protected]ed33e2e2011-11-20 21:18:07109 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance_id);
110 if (!dispatcher)
111 return;
[email protected]4c41d3f2012-02-15 01:44:47112 EnterResourceNoLock<PPB_InputEvent_API> enter(input_event, false);
[email protected]ed33e2e2011-11-20 21:18:07113 if (enter.failed())
114 return;
115
116 const InputEventData& input_event_data = enter.object()->GetInputEventData();
117 dispatcher->Send(new PpapiHostMsg_PPBTesting_SimulateInputEvent(
118 API_ID_PPB_TESTING, instance_id, input_event_data));
119}
120
[email protected]961e8972011-12-15 15:29:19121PP_Var GetDocumentURL(PP_Instance instance, PP_URLComponents_Dev* components) {
122 EnterInstance enter(instance);
123 if (enter.failed())
124 return PP_MakeUndefined();
125 return enter.functions()->GetDocumentURL(instance, components);
126}
127
[email protected]a732cec2011-12-22 08:35:52128// TODO(dmichael): Ideally we could get a way to check the number of vars in the
129// host-side tracker when running out-of-process, to make sure the proxy does
130// not leak host-side vars.
131uint32_t GetLiveVars(PP_Var live_vars[], uint32_t array_size) {
[email protected]4c41d3f2012-02-15 01:44:47132 ProxyAutoLock lock;
[email protected]a732cec2011-12-22 08:35:52133 std::vector<PP_Var> vars =
134 PpapiGlobals::Get()->GetVarTracker()->GetLiveVars();
135 for (size_t i = 0u;
136 i < std::min(static_cast<size_t>(array_size), vars.size());
137 ++i)
138 live_vars[i] = vars[i];
brettw669d47b12015-02-13 21:17:38139 return static_cast<uint32_t>(vars.size());
[email protected]a732cec2011-12-22 08:35:52140}
141
[email protected]38f428f12013-04-19 14:46:05142void SetMinimumArrayBufferSizeForShmem(PP_Instance instance,
143 uint32_t threshold) {
144 ProxyAutoLock lock;
145 RawVarDataGraph::SetMinimumArrayBufferSizeForShmemForTest(threshold);
146 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
147 if (!dispatcher)
148 return;
149 dispatcher->Send(
150 new PpapiHostMsg_PPBTesting_SetMinimumArrayBufferSizeForShmem(
151 API_ID_PPB_TESTING, threshold));
152}
153
[email protected]45c897d2014-08-11 04:12:46154void RunV8GC(PP_Instance instance) {
155 // TODO(raymes): Implement this if we need it.
156 NOTIMPLEMENTED();
157}
158
[email protected]7310b162013-11-25 22:39:15159const PPB_Testing_Private testing_interface = {
tommycli0dd13012015-04-24 20:27:22160 &ReadImageData,
161 &RunMessageLoop,
162 &QuitMessageLoop,
163 &GetLiveObjectsForInstance,
164 &IsOutOfProcess,
tommycliee0a49b2015-05-05 19:40:49165 &PostPowerSaverStatus,
166 &SubscribeToPowerSaverNotifications,
tommycli0dd13012015-04-24 20:27:22167 &SimulateInputEvent,
168 &GetDocumentURL,
169 &GetLiveVars,
170 &SetMinimumArrayBufferSizeForShmem,
171 &RunV8GC};
[email protected]1054c072010-11-05 22:52:48172
173} // namespace
174
[email protected]5c966022011-09-13 18:09:37175PPB_Testing_Proxy::PPB_Testing_Proxy(Dispatcher* dispatcher)
176 : InterfaceProxy(dispatcher),
177 ppb_testing_impl_(NULL) {
178 if (!dispatcher->IsPlugin()) {
[email protected]7310b162013-11-25 22:39:15179 ppb_testing_impl_ = static_cast<const PPB_Testing_Private*>(
180 dispatcher->local_get_interface()(PPB_TESTING_PRIVATE_INTERFACE));
[email protected]5c966022011-09-13 18:09:37181 }
[email protected]1054c072010-11-05 22:52:48182}
183
184PPB_Testing_Proxy::~PPB_Testing_Proxy() {
185}
186
[email protected]465faa22011-02-08 16:31:46187// static
[email protected]637fc4cd2013-12-06 22:08:15188const PPB_Testing_Private* PPB_Testing_Proxy::GetProxyInterface() {
189 return &testing_interface;
[email protected]1054c072010-11-05 22:52:48190}
191
[email protected]a95986a82010-12-24 06:19:28192bool PPB_Testing_Proxy::OnMessageReceived(const IPC::Message& msg) {
[email protected]27b04df2012-12-10 23:43:10193 if (!dispatcher()->permissions().HasPermission(PERMISSION_TESTING))
194 return false;
195
[email protected]a95986a82010-12-24 06:19:28196 bool handled = true;
[email protected]1054c072010-11-05 22:52:48197 IPC_BEGIN_MESSAGE_MAP(PPB_Testing_Proxy, msg)
198 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBTesting_ReadImageData,
199 OnMsgReadImageData)
[email protected]4614f192011-01-21 00:26:43200 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBTesting_GetLiveObjectsForInstance,
201 OnMsgGetLiveObjectsForInstance)
tommycliee0a49b2015-05-05 19:40:49202 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBTesting_PostPowerSaverStatus,
203 OnMsgPostPowerSaverStatus)
204 IPC_MESSAGE_HANDLER(
205 PpapiHostMsg_PPBTesting_SubscribeToPowerSaverNotifications,
206 OnMsgSubscribeToPowerSaverNotifications)
[email protected]ed33e2e2011-11-20 21:18:07207 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBTesting_SimulateInputEvent,
208 OnMsgSimulateInputEvent)
[email protected]38f428f12013-04-19 14:46:05209 IPC_MESSAGE_HANDLER(
210 PpapiHostMsg_PPBTesting_SetMinimumArrayBufferSizeForShmem,
211 OnMsgSetMinimumArrayBufferSizeForShmem)
[email protected]a95986a82010-12-24 06:19:28212 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]1054c072010-11-05 22:52:48213 IPC_END_MESSAGE_MAP()
[email protected]a95986a82010-12-24 06:19:28214 return handled;
[email protected]1054c072010-11-05 22:52:48215}
216
[email protected]f24448db2011-01-27 20:40:39217void PPB_Testing_Proxy::OnMsgReadImageData(
218 const HostResource& device_context_2d,
219 const HostResource& image,
220 const PP_Point& top_left,
221 PP_Bool* result) {
[email protected]5c966022011-09-13 18:09:37222 *result = ppb_testing_impl_->ReadImageData(
[email protected]f24448db2011-01-27 20:40:39223 device_context_2d.host_resource(), image.host_resource(), &top_left);
[email protected]1054c072010-11-05 22:52:48224}
225
[email protected]7358d572011-02-15 18:44:40226void PPB_Testing_Proxy::OnMsgRunMessageLoop(PP_Instance instance) {
[email protected]5c966022011-09-13 18:09:37227 ppb_testing_impl_->RunMessageLoop(instance);
[email protected]1054c072010-11-05 22:52:48228}
229
[email protected]7358d572011-02-15 18:44:40230void PPB_Testing_Proxy::OnMsgQuitMessageLoop(PP_Instance instance) {
[email protected]5c966022011-09-13 18:09:37231 ppb_testing_impl_->QuitMessageLoop(instance);
[email protected]1054c072010-11-05 22:52:48232}
233
[email protected]4614f192011-01-21 00:26:43234void PPB_Testing_Proxy::OnMsgGetLiveObjectsForInstance(PP_Instance instance,
[email protected]f24448db2011-01-27 20:40:39235 uint32_t* result) {
[email protected]5c966022011-09-13 18:09:37236 *result = ppb_testing_impl_->GetLiveObjectsForInstance(instance);
[email protected]1054c072010-11-05 22:52:48237}
238
tommycliee0a49b2015-05-05 19:40:49239void PPB_Testing_Proxy::OnMsgPostPowerSaverStatus(PP_Instance instance) {
240 ppb_testing_impl_->PostPowerSaverStatus(instance);
241}
242
243void PPB_Testing_Proxy::OnMsgSubscribeToPowerSaverNotifications(
244 PP_Instance instance) {
245 ppb_testing_impl_->SubscribeToPowerSaverNotifications(instance);
tommycli0dd13012015-04-24 20:27:22246}
247
[email protected]ed33e2e2011-11-20 21:18:07248void PPB_Testing_Proxy::OnMsgSimulateInputEvent(
249 PP_Instance instance,
250 const InputEventData& input_event) {
[email protected]9a578392011-12-07 18:59:27251 scoped_refptr<PPB_InputEvent_Shared> input_event_impl(
[email protected]00d320a2012-02-14 00:27:04252 new PPB_InputEvent_Shared(OBJECT_IS_PROXY, instance, input_event));
[email protected]ed33e2e2011-11-20 21:18:07253 ppb_testing_impl_->SimulateInputEvent(instance,
254 input_event_impl->pp_resource());
255}
256
[email protected]38f428f12013-04-19 14:46:05257void PPB_Testing_Proxy::OnMsgSetMinimumArrayBufferSizeForShmem(
258 uint32_t threshold) {
259 RawVarDataGraph::SetMinimumArrayBufferSizeForShmemForTest(threshold);
260}
261
[email protected]1054c072010-11-05 22:52:48262} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:02263} // namespace ppapi