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 #include "ppapi/proxy/ppb_testing_proxy.h"
6 
7 #include <stddef.h>
8 
9 #include "base/run_loop.h"
10 #include "ppapi/c/private/ppb_testing_private.h"
11 #include "ppapi/proxy/enter_proxy.h"
12 #include "ppapi/proxy/plugin_dispatcher.h"
13 #include "ppapi/proxy/ppapi_messages.h"
14 #include "ppapi/shared_impl/ppapi_globals.h"
15 #include "ppapi/shared_impl/proxy_lock.h"
16 #include "ppapi/shared_impl/resource.h"
17 #include "ppapi/shared_impl/resource_tracker.h"
18 #include "ppapi/thunk/enter.h"
19 #include "ppapi/thunk/ppb_graphics_2d_api.h"
20 #include "ppapi/thunk/ppb_input_event_api.h"
21 
22 using ppapi::thunk::EnterInstance;
23 using ppapi::thunk::EnterResource;
24 using ppapi::thunk::EnterResourceNoLock;
25 using ppapi::thunk::PPB_Graphics2D_API;
26 using ppapi::thunk::PPB_InputEvent_API;
27 
28 namespace ppapi {
29 namespace proxy {
30 
31 namespace {
32 
ReadImageData(PP_Resource graphics_2d,PP_Resource image,const PP_Point * top_left)33 PP_Bool ReadImageData(PP_Resource graphics_2d,
34                       PP_Resource image,
35                       const PP_Point* top_left) {
36   ProxyAutoLock lock;
37   Resource* image_object =
38       PpapiGlobals::Get()->GetResourceTracker()->GetResource(image);
39   if (!image_object)
40     return PP_FALSE;
41   Resource* graphics_2d_object =
42       PpapiGlobals::Get()->GetResourceTracker()->GetResource(graphics_2d);
43   if (!graphics_2d_object ||
44       image_object->pp_instance() != graphics_2d_object->pp_instance())
45     return PP_FALSE;
46 
47   EnterResourceNoLock<PPB_Graphics2D_API> enter(graphics_2d, true);
48   if (enter.failed())
49     return PP_FALSE;
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;
53 }
54 
RunMessageLoop(PP_Instance instance)55 void RunMessageLoop(PP_Instance instance) {
56   CHECK(PpapiGlobals::Get()->GetMainThreadMessageLoop()->
57       BelongsToCurrentThread());
58   base::RunLoop(base::RunLoop::Type::kNestableTasksAllowed).Run();
59 }
60 
QuitMessageLoop(PP_Instance instance)61 void QuitMessageLoop(PP_Instance instance) {
62   CHECK(PpapiGlobals::Get()->GetMainThreadMessageLoop()->
63             BelongsToCurrentThread());
64   base::RunLoop::QuitCurrentDeprecated();
65 }
66 
GetLiveObjectsForInstance(PP_Instance instance_id)67 uint32_t GetLiveObjectsForInstance(PP_Instance instance_id) {
68   ProxyAutoLock lock;
69   PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance_id);
70   if (!dispatcher)
71     return static_cast<uint32_t>(-1);
72 
73   uint32_t result = 0;
74   dispatcher->Send(new PpapiHostMsg_PPBTesting_GetLiveObjectsForInstance(
75       API_ID_PPB_TESTING, instance_id, &result));
76   return result;
77 }
78 
IsOutOfProcess()79 PP_Bool IsOutOfProcess() {
80   return PP_TRUE;
81 }
82 
SimulateInputEvent(PP_Instance instance_id,PP_Resource input_event)83 void SimulateInputEvent(PP_Instance instance_id, PP_Resource input_event) {
84   ProxyAutoLock lock;
85   PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance_id);
86   if (!dispatcher)
87     return;
88   EnterResourceNoLock<PPB_InputEvent_API> enter(input_event, false);
89   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 
GetDocumentURL(PP_Instance instance,PP_URLComponents_Dev * components)97 PP_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 
104 // 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.
GetLiveVars(PP_Var live_vars[],uint32_t array_size)107 uint32_t GetLiveVars(PP_Var live_vars[], uint32_t array_size) {
108   ProxyAutoLock lock;
109   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 static_cast<uint32_t>(vars.size());
116 }
117 
SetMinimumArrayBufferSizeForShmem(PP_Instance instance,uint32_t threshold)118 void 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 
RunV8GC(PP_Instance instance)130 void RunV8GC(PP_Instance instance) {
131   // TODO(raymes): Implement this if we need it.
132   NOTIMPLEMENTED();
133 }
134 
135 const PPB_Testing_Private testing_interface = {
136     &ReadImageData,
137     &RunMessageLoop,
138     &QuitMessageLoop,
139     &GetLiveObjectsForInstance,
140     &IsOutOfProcess,
141     &SimulateInputEvent,
142     &GetDocumentURL,
143     &GetLiveVars,
144     &SetMinimumArrayBufferSizeForShmem,
145     &RunV8GC};
146 
147 }  // namespace
148 
PPB_Testing_Proxy(Dispatcher * dispatcher)149 PPB_Testing_Proxy::PPB_Testing_Proxy(Dispatcher* dispatcher)
150     : InterfaceProxy(dispatcher),
151       ppb_testing_impl_(NULL) {
152   if (!dispatcher->IsPlugin()) {
153     ppb_testing_impl_ = static_cast<const PPB_Testing_Private*>(
154         dispatcher->local_get_interface()(PPB_TESTING_PRIVATE_INTERFACE));
155   }
156 }
157 
~PPB_Testing_Proxy()158 PPB_Testing_Proxy::~PPB_Testing_Proxy() {
159 }
160 
161 // static
GetProxyInterface()162 const PPB_Testing_Private* PPB_Testing_Proxy::GetProxyInterface() {
163   return &testing_interface;
164 }
165 
OnMessageReceived(const IPC::Message & msg)166 bool PPB_Testing_Proxy::OnMessageReceived(const IPC::Message& msg) {
167   if (!dispatcher()->permissions().HasPermission(PERMISSION_TESTING))
168     return false;
169 
170   bool handled = true;
171   IPC_BEGIN_MESSAGE_MAP(PPB_Testing_Proxy, msg)
172     IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBTesting_ReadImageData,
173                         OnMsgReadImageData)
174     IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBTesting_GetLiveObjectsForInstance,
175                         OnMsgGetLiveObjectsForInstance)
176     IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBTesting_SimulateInputEvent,
177                         OnMsgSimulateInputEvent)
178     IPC_MESSAGE_HANDLER(
179         PpapiHostMsg_PPBTesting_SetMinimumArrayBufferSizeForShmem,
180         OnMsgSetMinimumArrayBufferSizeForShmem)
181     IPC_MESSAGE_UNHANDLED(handled = false)
182   IPC_END_MESSAGE_MAP()
183   return handled;
184 }
185 
OnMsgReadImageData(const HostResource & device_context_2d,const HostResource & image,const PP_Point & top_left,PP_Bool * result)186 void PPB_Testing_Proxy::OnMsgReadImageData(
187     const HostResource& device_context_2d,
188     const HostResource& image,
189     const PP_Point& top_left,
190     PP_Bool* result) {
191   *result = ppb_testing_impl_->ReadImageData(
192       device_context_2d.host_resource(), image.host_resource(), &top_left);
193 }
194 
OnMsgRunMessageLoop(PP_Instance instance)195 void PPB_Testing_Proxy::OnMsgRunMessageLoop(PP_Instance instance) {
196   ppb_testing_impl_->RunMessageLoop(instance);
197 }
198 
OnMsgQuitMessageLoop(PP_Instance instance)199 void PPB_Testing_Proxy::OnMsgQuitMessageLoop(PP_Instance instance) {
200   ppb_testing_impl_->QuitMessageLoop(instance);
201 }
202 
OnMsgGetLiveObjectsForInstance(PP_Instance instance,uint32_t * result)203 void PPB_Testing_Proxy::OnMsgGetLiveObjectsForInstance(PP_Instance instance,
204                                                        uint32_t* result) {
205   *result = ppb_testing_impl_->GetLiveObjectsForInstance(instance);
206 }
207 
OnMsgSimulateInputEvent(PP_Instance instance,const InputEventData & input_event)208 void PPB_Testing_Proxy::OnMsgSimulateInputEvent(
209     PP_Instance instance,
210     const InputEventData& input_event) {
211   scoped_refptr<PPB_InputEvent_Shared> input_event_impl(
212       new PPB_InputEvent_Shared(OBJECT_IS_PROXY, instance, input_event));
213   ppb_testing_impl_->SimulateInputEvent(instance,
214                                         input_event_impl->pp_resource());
215 }
216 
OnMsgSetMinimumArrayBufferSizeForShmem(uint32_t threshold)217 void PPB_Testing_Proxy::OnMsgSetMinimumArrayBufferSizeForShmem(
218     uint32_t threshold) {
219   RawVarDataGraph::SetMinimumArrayBufferSizeForShmemForTest(threshold);
220 }
221 
222 }  // namespace proxy
223 }  // namespace ppapi
224