blob: f28c500190c9c7e07dcb97865753aca2dcd3b81f [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
7#include "base/message_loop.h"
8#include "ppapi/c/dev/ppb_testing_dev.h"
[email protected]ed33e2e2011-11-20 21:18:079#include "ppapi/proxy/enter_proxy.h"
[email protected]1054c072010-11-05 22:52:4810#include "ppapi/proxy/plugin_dispatcher.h"
11#include "ppapi/proxy/ppapi_messages.h"
[email protected]794d83cd2011-10-20 19:09:2012#include "ppapi/shared_impl/ppapi_globals.h"
[email protected]4c41d3f2012-02-15 01:44:4713#include "ppapi/shared_impl/proxy_lock.h"
[email protected]7f8b26b2011-08-18 15:41:0114#include "ppapi/shared_impl/resource.h"
[email protected]794d83cd2011-10-20 19:09:2015#include "ppapi/shared_impl/resource_tracker.h"
[email protected]ed33e2e2011-11-20 21:18:0716#include "ppapi/thunk/enter.h"
[email protected]08e65c12012-11-29 18:53:2917#include "ppapi/thunk/ppb_graphics_2d_api.h"
[email protected]ed33e2e2011-11-20 21:18:0718#include "ppapi/thunk/ppb_input_event_api.h"
19
[email protected]961e8972011-12-15 15:29:1920using ppapi::thunk::EnterInstance;
[email protected]08e65c12012-11-29 18:53:2921using ppapi::thunk::EnterResource;
[email protected]4c41d3f2012-02-15 01:44:4722using ppapi::thunk::EnterResourceNoLock;
[email protected]08e65c12012-11-29 18:53:2923using ppapi::thunk::PPB_Graphics2D_API;
[email protected]ed33e2e2011-11-20 21:18:0724using ppapi::thunk::PPB_InputEvent_API;
[email protected]1054c072010-11-05 22:52:4825
[email protected]4d2efd22011-08-18 21:58:0226namespace ppapi {
[email protected]1054c072010-11-05 22:52:4827namespace proxy {
28
29namespace {
30
[email protected]f24448db2011-01-27 20:40:3931PP_Bool ReadImageData(PP_Resource graphics_2d,
[email protected]19d2b012010-11-08 16:32:1832 PP_Resource image,
33 const PP_Point* top_left) {
[email protected]4c41d3f2012-02-15 01:44:4734 ProxyAutoLock lock;
[email protected]794d83cd2011-10-20 19:09:2035 Resource* image_object =
36 PpapiGlobals::Get()->GetResourceTracker()->GetResource(image);
[email protected]4614f192011-01-21 00:26:4337 if (!image_object)
38 return PP_FALSE;
[email protected]7f8b26b2011-08-18 15:41:0139 Resource* graphics_2d_object =
[email protected]794d83cd2011-10-20 19:09:2040 PpapiGlobals::Get()->GetResourceTracker()->GetResource(graphics_2d);
[email protected]f24448db2011-01-27 20:40:3941 if (!graphics_2d_object ||
[email protected]7f8b26b2011-08-18 15:41:0142 image_object->pp_instance() != graphics_2d_object->pp_instance())
[email protected]f24448db2011-01-27 20:40:3943 return PP_FALSE;
44
[email protected]08e65c12012-11-29 18:53:2945 EnterResourceNoLock<PPB_Graphics2D_API> enter(graphics_2d, true);
46 if (enter.failed())
[email protected]4614f192011-01-21 00:26:4347 return PP_FALSE;
[email protected]08e65c12012-11-29 18:53:2948 const HostResource& host_image = image_object->host_resource();
49 return enter.object()->ReadImageData(host_image.host_resource(), top_left) ?
50 PP_TRUE : PP_FALSE;
[email protected]1054c072010-11-05 22:52:4851}
52
[email protected]7358d572011-02-15 18:44:4053void RunMessageLoop(PP_Instance instance) {
[email protected]d2881d82013-05-06 19:23:0854 base::MessageLoop::ScopedNestableTaskAllower allow(
55 base::MessageLoop::current());
[email protected]77c34172012-11-08 18:55:1656 CHECK(PpapiGlobals::Get()->GetMainThreadMessageLoop()->
[email protected]d2881d82013-05-06 19:23:0857 BelongsToCurrentThread());
58 base::MessageLoop::current()->Run();
[email protected]1054c072010-11-05 22:52:4859}
60
[email protected]7358d572011-02-15 18:44:4061void QuitMessageLoop(PP_Instance instance) {
[email protected]77c34172012-11-08 18:55:1662 CHECK(PpapiGlobals::Get()->GetMainThreadMessageLoop()->
63 BelongsToCurrentThread());
[email protected]d2881d82013-05-06 19:23:0864 base::MessageLoop::current()->QuitNow();
[email protected]1054c072010-11-05 22:52:4865}
66
[email protected]4614f192011-01-21 00:26:4367uint32_t GetLiveObjectsForInstance(PP_Instance instance_id) {
[email protected]4c41d3f2012-02-15 01:44:4768 ProxyAutoLock lock;
[email protected]4614f192011-01-21 00:26:4369 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance_id);
70 if (!dispatcher)
71 return static_cast<uint32_t>(-1);
72
[email protected]1054c072010-11-05 22:52:4873 uint32_t result = 0;
[email protected]4614f192011-01-21 00:26:4374 dispatcher->Send(new PpapiHostMsg_PPBTesting_GetLiveObjectsForInstance(
[email protected]ac4b54d2011-10-20 23:09:2875 API_ID_PPB_TESTING, instance_id, &result));
[email protected]1054c072010-11-05 22:52:4876 return result;
77}
78
[email protected]5d904b9e2011-08-30 19:38:3179PP_Bool IsOutOfProcess() {
80 return PP_TRUE;
81}
82
[email protected]ed33e2e2011-11-20 21:18:0783void SimulateInputEvent(PP_Instance instance_id, PP_Resource input_event) {
[email protected]4c41d3f2012-02-15 01:44:4784 ProxyAutoLock lock;
[email protected]ed33e2e2011-11-20 21:18:0785 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance_id);
86 if (!dispatcher)
87 return;
[email protected]4c41d3f2012-02-15 01:44:4788 EnterResourceNoLock<PPB_InputEvent_API> enter(input_event, false);
[email protected]ed33e2e2011-11-20 21:18:0789 if (enter.failed())
90 return;
91
92 const InputEventData& input_event_data = enter.object()->GetInputEventData();
93 dispatcher->Send(new PpapiHostMsg_PPBTesting_SimulateInputEvent(
94 API_ID_PPB_TESTING, instance_id, input_event_data));
95}
96
[email protected]961e8972011-12-15 15:29:1997PP_Var GetDocumentURL(PP_Instance instance, PP_URLComponents_Dev* components) {
98 EnterInstance enter(instance);
99 if (enter.failed())
100 return PP_MakeUndefined();
101 return enter.functions()->GetDocumentURL(instance, components);
102}
103
[email protected]a732cec2011-12-22 08:35:52104// TODO(dmichael): Ideally we could get a way to check the number of vars in the
105// host-side tracker when running out-of-process, to make sure the proxy does
106// not leak host-side vars.
107uint32_t GetLiveVars(PP_Var live_vars[], uint32_t array_size) {
[email protected]4c41d3f2012-02-15 01:44:47108 ProxyAutoLock lock;
[email protected]a732cec2011-12-22 08:35:52109 std::vector<PP_Var> vars =
110 PpapiGlobals::Get()->GetVarTracker()->GetLiveVars();
111 for (size_t i = 0u;
112 i < std::min(static_cast<size_t>(array_size), vars.size());
113 ++i)
114 live_vars[i] = vars[i];
115 return vars.size();
116}
117
[email protected]38f428f12013-04-19 14:46:05118void SetMinimumArrayBufferSizeForShmem(PP_Instance instance,
119 uint32_t threshold) {
120 ProxyAutoLock lock;
121 RawVarDataGraph::SetMinimumArrayBufferSizeForShmemForTest(threshold);
122 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
123 if (!dispatcher)
124 return;
125 dispatcher->Send(
126 new PpapiHostMsg_PPBTesting_SetMinimumArrayBufferSizeForShmem(
127 API_ID_PPB_TESTING, threshold));
128}
129
[email protected]1054c072010-11-05 22:52:48130const PPB_Testing_Dev testing_interface = {
131 &ReadImageData,
132 &RunMessageLoop,
133 &QuitMessageLoop,
[email protected]5d904b9e2011-08-30 19:38:31134 &GetLiveObjectsForInstance,
[email protected]ed33e2e2011-11-20 21:18:07135 &IsOutOfProcess,
[email protected]961e8972011-12-15 15:29:19136 &SimulateInputEvent,
[email protected]a732cec2011-12-22 08:35:52137 &GetDocumentURL,
[email protected]38f428f12013-04-19 14:46:05138 &GetLiveVars,
139 &SetMinimumArrayBufferSizeForShmem
[email protected]1054c072010-11-05 22:52:48140};
141
[email protected]5c966022011-09-13 18:09:37142InterfaceProxy* CreateTestingProxy(Dispatcher* dispatcher) {
143 return new PPB_Testing_Proxy(dispatcher);
[email protected]465faa22011-02-08 16:31:46144}
145
[email protected]1054c072010-11-05 22:52:48146} // namespace
147
[email protected]5c966022011-09-13 18:09:37148PPB_Testing_Proxy::PPB_Testing_Proxy(Dispatcher* dispatcher)
149 : InterfaceProxy(dispatcher),
150 ppb_testing_impl_(NULL) {
151 if (!dispatcher->IsPlugin()) {
152 ppb_testing_impl_ = static_cast<const PPB_Testing_Dev*>(
153 dispatcher->local_get_interface()(PPB_TESTING_DEV_INTERFACE));
154 }
[email protected]1054c072010-11-05 22:52:48155}
156
157PPB_Testing_Proxy::~PPB_Testing_Proxy() {
158}
159
[email protected]465faa22011-02-08 16:31:46160// static
161const InterfaceProxy::Info* PPB_Testing_Proxy::GetInfo() {
162 static const Info info = {
163 &testing_interface,
164 PPB_TESTING_DEV_INTERFACE,
[email protected]ac4b54d2011-10-20 23:09:28165 API_ID_PPB_TESTING,
[email protected]465faa22011-02-08 16:31:46166 false,
167 &CreateTestingProxy,
168 };
169 return &info;
[email protected]1054c072010-11-05 22:52:48170}
171
[email protected]a95986a82010-12-24 06:19:28172bool PPB_Testing_Proxy::OnMessageReceived(const IPC::Message& msg) {
[email protected]27b04df2012-12-10 23:43:10173 if (!dispatcher()->permissions().HasPermission(PERMISSION_TESTING))
174 return false;
175
[email protected]a95986a82010-12-24 06:19:28176 bool handled = true;
[email protected]1054c072010-11-05 22:52:48177 IPC_BEGIN_MESSAGE_MAP(PPB_Testing_Proxy, msg)
178 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBTesting_ReadImageData,
179 OnMsgReadImageData)
[email protected]4614f192011-01-21 00:26:43180 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBTesting_GetLiveObjectsForInstance,
181 OnMsgGetLiveObjectsForInstance)
[email protected]ed33e2e2011-11-20 21:18:07182 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBTesting_SimulateInputEvent,
183 OnMsgSimulateInputEvent)
[email protected]38f428f12013-04-19 14:46:05184 IPC_MESSAGE_HANDLER(
185 PpapiHostMsg_PPBTesting_SetMinimumArrayBufferSizeForShmem,
186 OnMsgSetMinimumArrayBufferSizeForShmem)
[email protected]a95986a82010-12-24 06:19:28187 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]1054c072010-11-05 22:52:48188 IPC_END_MESSAGE_MAP()
[email protected]a95986a82010-12-24 06:19:28189 return handled;
[email protected]1054c072010-11-05 22:52:48190}
191
[email protected]f24448db2011-01-27 20:40:39192void PPB_Testing_Proxy::OnMsgReadImageData(
193 const HostResource& device_context_2d,
194 const HostResource& image,
195 const PP_Point& top_left,
196 PP_Bool* result) {
[email protected]5c966022011-09-13 18:09:37197 *result = ppb_testing_impl_->ReadImageData(
[email protected]f24448db2011-01-27 20:40:39198 device_context_2d.host_resource(), image.host_resource(), &top_left);
[email protected]1054c072010-11-05 22:52:48199}
200
[email protected]7358d572011-02-15 18:44:40201void PPB_Testing_Proxy::OnMsgRunMessageLoop(PP_Instance instance) {
[email protected]5c966022011-09-13 18:09:37202 ppb_testing_impl_->RunMessageLoop(instance);
[email protected]1054c072010-11-05 22:52:48203}
204
[email protected]7358d572011-02-15 18:44:40205void PPB_Testing_Proxy::OnMsgQuitMessageLoop(PP_Instance instance) {
[email protected]5c966022011-09-13 18:09:37206 ppb_testing_impl_->QuitMessageLoop(instance);
[email protected]1054c072010-11-05 22:52:48207}
208
[email protected]4614f192011-01-21 00:26:43209void PPB_Testing_Proxy::OnMsgGetLiveObjectsForInstance(PP_Instance instance,
[email protected]f24448db2011-01-27 20:40:39210 uint32_t* result) {
[email protected]5c966022011-09-13 18:09:37211 *result = ppb_testing_impl_->GetLiveObjectsForInstance(instance);
[email protected]1054c072010-11-05 22:52:48212}
213
[email protected]ed33e2e2011-11-20 21:18:07214void PPB_Testing_Proxy::OnMsgSimulateInputEvent(
215 PP_Instance instance,
216 const InputEventData& input_event) {
[email protected]9a578392011-12-07 18:59:27217 scoped_refptr<PPB_InputEvent_Shared> input_event_impl(
[email protected]00d320a2012-02-14 00:27:04218 new PPB_InputEvent_Shared(OBJECT_IS_PROXY, instance, input_event));
[email protected]ed33e2e2011-11-20 21:18:07219 ppb_testing_impl_->SimulateInputEvent(instance,
220 input_event_impl->pp_resource());
221}
222
[email protected]38f428f12013-04-19 14:46:05223void PPB_Testing_Proxy::OnMsgSetMinimumArrayBufferSizeForShmem(
224 uint32_t threshold) {
225 RawVarDataGraph::SetMinimumArrayBufferSizeForShmemForTest(threshold);
226}
227
[email protected]1054c072010-11-05 22:52:48228} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:02229} // namespace ppapi