blob: b8c9ce07d3afb86a866fe26d4ccd89448197d25b [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"
17#include "ppapi/thunk/ppb_input_event_api.h"
18
[email protected]961e8972011-12-15 15:29:1919using ppapi::thunk::EnterInstance;
[email protected]4c41d3f2012-02-15 01:44:4720using ppapi::thunk::EnterResourceNoLock;
[email protected]ed33e2e2011-11-20 21:18:0721using ppapi::thunk::PPB_InputEvent_API;
[email protected]1054c072010-11-05 22:52:4822
[email protected]4d2efd22011-08-18 21:58:0223namespace ppapi {
[email protected]1054c072010-11-05 22:52:4824namespace proxy {
25
26namespace {
27
[email protected]f24448db2011-01-27 20:40:3928PP_Bool ReadImageData(PP_Resource graphics_2d,
[email protected]19d2b012010-11-08 16:32:1829 PP_Resource image,
30 const PP_Point* top_left) {
[email protected]4c41d3f2012-02-15 01:44:4731 ProxyAutoLock lock;
[email protected]794d83cd2011-10-20 19:09:2032 Resource* image_object =
33 PpapiGlobals::Get()->GetResourceTracker()->GetResource(image);
[email protected]4614f192011-01-21 00:26:4334 if (!image_object)
35 return PP_FALSE;
[email protected]7f8b26b2011-08-18 15:41:0136 Resource* graphics_2d_object =
[email protected]794d83cd2011-10-20 19:09:2037 PpapiGlobals::Get()->GetResourceTracker()->GetResource(graphics_2d);
[email protected]f24448db2011-01-27 20:40:3938 if (!graphics_2d_object ||
[email protected]7f8b26b2011-08-18 15:41:0139 image_object->pp_instance() != graphics_2d_object->pp_instance())
[email protected]f24448db2011-01-27 20:40:3940 return PP_FALSE;
41
[email protected]4614f192011-01-21 00:26:4342 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(
[email protected]7f8b26b2011-08-18 15:41:0143 image_object->pp_instance());
[email protected]4614f192011-01-21 00:26:4344 if (!dispatcher)
45 return PP_FALSE;
46
[email protected]19d2b012010-11-08 16:32:1847 PP_Bool result = PP_FALSE;
[email protected]4614f192011-01-21 00:26:4348 dispatcher->Send(new PpapiHostMsg_PPBTesting_ReadImageData(
[email protected]ac4b54d2011-10-20 23:09:2849 API_ID_PPB_TESTING, graphics_2d_object->host_resource(),
[email protected]f24448db2011-01-27 20:40:3950 image_object->host_resource(), *top_left, &result));
[email protected]1054c072010-11-05 22:52:4851 return result;
52}
53
[email protected]7358d572011-02-15 18:44:4054void RunMessageLoop(PP_Instance instance) {
[email protected]b5717a42012-02-14 19:33:5255 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current());
[email protected]4c41d3f2012-02-15 01:44:4756 // TODO(dmichael): We should probably assert that this is the main thread.
[email protected]b20df1c2011-08-03 14:38:2457 MessageLoop::current()->Run();
[email protected]1054c072010-11-05 22:52:4858}
59
[email protected]7358d572011-02-15 18:44:4060void QuitMessageLoop(PP_Instance instance) {
[email protected]4c41d3f2012-02-15 01:44:4761 // TODO(dmichael): We should probably assert that this is the main thread.
[email protected]b20df1c2011-08-03 14:38:2462 MessageLoop::current()->QuitNow();
[email protected]1054c072010-11-05 22:52:4863}
64
[email protected]4614f192011-01-21 00:26:4365uint32_t GetLiveObjectsForInstance(PP_Instance instance_id) {
[email protected]4c41d3f2012-02-15 01:44:4766 ProxyAutoLock lock;
[email protected]4614f192011-01-21 00:26:4367 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance_id);
68 if (!dispatcher)
69 return static_cast<uint32_t>(-1);
70
[email protected]1054c072010-11-05 22:52:4871 uint32_t result = 0;
[email protected]4614f192011-01-21 00:26:4372 dispatcher->Send(new PpapiHostMsg_PPBTesting_GetLiveObjectsForInstance(
[email protected]ac4b54d2011-10-20 23:09:2873 API_ID_PPB_TESTING, instance_id, &result));
[email protected]1054c072010-11-05 22:52:4874 return result;
75}
76
[email protected]5d904b9e2011-08-30 19:38:3177PP_Bool IsOutOfProcess() {
78 return PP_TRUE;
79}
80
[email protected]ed33e2e2011-11-20 21:18:0781void SimulateInputEvent(PP_Instance instance_id, PP_Resource input_event) {
[email protected]4c41d3f2012-02-15 01:44:4782 ProxyAutoLock lock;
[email protected]ed33e2e2011-11-20 21:18:0783 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance_id);
84 if (!dispatcher)
85 return;
[email protected]4c41d3f2012-02-15 01:44:4786 EnterResourceNoLock<PPB_InputEvent_API> enter(input_event, false);
[email protected]ed33e2e2011-11-20 21:18:0787 if (enter.failed())
88 return;
89
90 const InputEventData& input_event_data = enter.object()->GetInputEventData();
91 dispatcher->Send(new PpapiHostMsg_PPBTesting_SimulateInputEvent(
92 API_ID_PPB_TESTING, instance_id, input_event_data));
93}
94
[email protected]961e8972011-12-15 15:29:1995PP_Var GetDocumentURL(PP_Instance instance, PP_URLComponents_Dev* components) {
96 EnterInstance enter(instance);
97 if (enter.failed())
98 return PP_MakeUndefined();
99 return enter.functions()->GetDocumentURL(instance, components);
100}
101
[email protected]a732cec2011-12-22 08:35:52102// TODO(dmichael): Ideally we could get a way to check the number of vars in the
103// host-side tracker when running out-of-process, to make sure the proxy does
104// not leak host-side vars.
105uint32_t GetLiveVars(PP_Var live_vars[], uint32_t array_size) {
[email protected]4c41d3f2012-02-15 01:44:47106 ProxyAutoLock lock;
[email protected]a732cec2011-12-22 08:35:52107 std::vector<PP_Var> vars =
108 PpapiGlobals::Get()->GetVarTracker()->GetLiveVars();
109 for (size_t i = 0u;
110 i < std::min(static_cast<size_t>(array_size), vars.size());
111 ++i)
112 live_vars[i] = vars[i];
113 return vars.size();
114}
115
[email protected]1054c072010-11-05 22:52:48116const PPB_Testing_Dev testing_interface = {
117 &ReadImageData,
118 &RunMessageLoop,
119 &QuitMessageLoop,
[email protected]5d904b9e2011-08-30 19:38:31120 &GetLiveObjectsForInstance,
[email protected]ed33e2e2011-11-20 21:18:07121 &IsOutOfProcess,
[email protected]961e8972011-12-15 15:29:19122 &SimulateInputEvent,
[email protected]a732cec2011-12-22 08:35:52123 &GetDocumentURL,
124 &GetLiveVars
[email protected]1054c072010-11-05 22:52:48125};
126
[email protected]5c966022011-09-13 18:09:37127InterfaceProxy* CreateTestingProxy(Dispatcher* dispatcher) {
128 return new PPB_Testing_Proxy(dispatcher);
[email protected]465faa22011-02-08 16:31:46129}
130
[email protected]1054c072010-11-05 22:52:48131} // namespace
132
[email protected]5c966022011-09-13 18:09:37133PPB_Testing_Proxy::PPB_Testing_Proxy(Dispatcher* dispatcher)
134 : InterfaceProxy(dispatcher),
135 ppb_testing_impl_(NULL) {
136 if (!dispatcher->IsPlugin()) {
137 ppb_testing_impl_ = static_cast<const PPB_Testing_Dev*>(
138 dispatcher->local_get_interface()(PPB_TESTING_DEV_INTERFACE));
139 }
[email protected]1054c072010-11-05 22:52:48140}
141
142PPB_Testing_Proxy::~PPB_Testing_Proxy() {
143}
144
[email protected]465faa22011-02-08 16:31:46145// static
146const InterfaceProxy::Info* PPB_Testing_Proxy::GetInfo() {
147 static const Info info = {
148 &testing_interface,
149 PPB_TESTING_DEV_INTERFACE,
[email protected]ac4b54d2011-10-20 23:09:28150 API_ID_PPB_TESTING,
[email protected]465faa22011-02-08 16:31:46151 false,
152 &CreateTestingProxy,
153 };
154 return &info;
[email protected]1054c072010-11-05 22:52:48155}
156
[email protected]a95986a82010-12-24 06:19:28157bool PPB_Testing_Proxy::OnMessageReceived(const IPC::Message& msg) {
158 bool handled = true;
[email protected]1054c072010-11-05 22:52:48159 IPC_BEGIN_MESSAGE_MAP(PPB_Testing_Proxy, msg)
160 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBTesting_ReadImageData,
161 OnMsgReadImageData)
[email protected]4614f192011-01-21 00:26:43162 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBTesting_GetLiveObjectsForInstance,
163 OnMsgGetLiveObjectsForInstance)
[email protected]ed33e2e2011-11-20 21:18:07164 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBTesting_SimulateInputEvent,
165 OnMsgSimulateInputEvent)
[email protected]a95986a82010-12-24 06:19:28166 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]1054c072010-11-05 22:52:48167 IPC_END_MESSAGE_MAP()
[email protected]a95986a82010-12-24 06:19:28168 return handled;
[email protected]1054c072010-11-05 22:52:48169}
170
[email protected]f24448db2011-01-27 20:40:39171void PPB_Testing_Proxy::OnMsgReadImageData(
172 const HostResource& device_context_2d,
173 const HostResource& image,
174 const PP_Point& top_left,
175 PP_Bool* result) {
[email protected]5c966022011-09-13 18:09:37176 *result = ppb_testing_impl_->ReadImageData(
[email protected]f24448db2011-01-27 20:40:39177 device_context_2d.host_resource(), image.host_resource(), &top_left);
[email protected]1054c072010-11-05 22:52:48178}
179
[email protected]7358d572011-02-15 18:44:40180void PPB_Testing_Proxy::OnMsgRunMessageLoop(PP_Instance instance) {
[email protected]5c966022011-09-13 18:09:37181 ppb_testing_impl_->RunMessageLoop(instance);
[email protected]1054c072010-11-05 22:52:48182}
183
[email protected]7358d572011-02-15 18:44:40184void PPB_Testing_Proxy::OnMsgQuitMessageLoop(PP_Instance instance) {
[email protected]5c966022011-09-13 18:09:37185 ppb_testing_impl_->QuitMessageLoop(instance);
[email protected]1054c072010-11-05 22:52:48186}
187
[email protected]4614f192011-01-21 00:26:43188void PPB_Testing_Proxy::OnMsgGetLiveObjectsForInstance(PP_Instance instance,
[email protected]f24448db2011-01-27 20:40:39189 uint32_t* result) {
[email protected]5c966022011-09-13 18:09:37190 *result = ppb_testing_impl_->GetLiveObjectsForInstance(instance);
[email protected]1054c072010-11-05 22:52:48191}
192
[email protected]ed33e2e2011-11-20 21:18:07193void PPB_Testing_Proxy::OnMsgSimulateInputEvent(
194 PP_Instance instance,
195 const InputEventData& input_event) {
[email protected]9a578392011-12-07 18:59:27196 scoped_refptr<PPB_InputEvent_Shared> input_event_impl(
[email protected]00d320a2012-02-14 00:27:04197 new PPB_InputEvent_Shared(OBJECT_IS_PROXY, instance, input_event));
[email protected]ed33e2e2011-11-20 21:18:07198 ppb_testing_impl_->SimulateInputEvent(instance,
199 input_event_impl->pp_resource());
200}
201
[email protected]1054c072010-11-05 22:52:48202} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:02203} // namespace ppapi