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 
PostPowerSaverStatus(PP_Instance instance_id)83 void PostPowerSaverStatus(PP_Instance instance_id) {
84   ProxyAutoLock lock;
85   PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance_id);
86   if (!dispatcher)
87     return;
88 
89   dispatcher->Send(new PpapiHostMsg_PPBTesting_PostPowerSaverStatus(
90       API_ID_PPB_TESTING, instance_id));
91 }
92 
SubscribeToPowerSaverNotifications(PP_Instance instance_id)93 void SubscribeToPowerSaverNotifications(PP_Instance instance_id) {
94   ProxyAutoLock lock;
95   PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance_id);
96   if (!dispatcher)
97     return;
98 
99   dispatcher->Send(
100       new PpapiHostMsg_PPBTesting_SubscribeToPowerSaverNotifications(
101           API_ID_PPB_TESTING, instance_id));
102 }
103 
SimulateInputEvent(PP_Instance instance_id,PP_Resource input_event)104 void SimulateInputEvent(PP_Instance instance_id, PP_Resource input_event) {
105   ProxyAutoLock lock;
106   PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance_id);
107   if (!dispatcher)
108     return;
109   EnterResourceNoLock<PPB_InputEvent_API> enter(input_event, false);
110   if (enter.failed())
111     return;
112 
113   const InputEventData& input_event_data = enter.object()->GetInputEventData();
114   dispatcher->Send(new PpapiHostMsg_PPBTesting_SimulateInputEvent(
115       API_ID_PPB_TESTING, instance_id, input_event_data));
116 }
117 
GetDocumentURL(PP_Instance instance,PP_URLComponents_Dev * components)118 PP_Var GetDocumentURL(PP_Instance instance, PP_URLComponents_Dev* components) {
119   EnterInstance enter(instance);
120   if (enter.failed())
121     return PP_MakeUndefined();
122   return enter.functions()->GetDocumentURL(instance, components);
123 }
124 
125 // TODO(dmichael): Ideally we could get a way to check the number of vars in the
126 // host-side tracker when running out-of-process, to make sure the proxy does
127 // not leak host-side vars.
GetLiveVars(PP_Var live_vars[],uint32_t array_size)128 uint32_t GetLiveVars(PP_Var live_vars[], uint32_t array_size) {
129   ProxyAutoLock lock;
130   std::vector<PP_Var> vars =
131       PpapiGlobals::Get()->GetVarTracker()->GetLiveVars();
132   for (size_t i = 0u;
133        i < std::min(static_cast<size_t>(array_size), vars.size());
134        ++i)
135     live_vars[i] = vars[i];
136   return static_cast<uint32_t>(vars.size());
137 }
138 
SetMinimumArrayBufferSizeForShmem(PP_Instance instance,uint32_t threshold)139 void SetMinimumArrayBufferSizeForShmem(PP_Instance instance,
140                                        uint32_t threshold) {
141   ProxyAutoLock lock;
142   RawVarDataGraph::SetMinimumArrayBufferSizeForShmemForTest(threshold);
143   PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
144   if (!dispatcher)
145     return;
146   dispatcher->Send(
147       new PpapiHostMsg_PPBTesting_SetMinimumArrayBufferSizeForShmem(
148           API_ID_PPB_TESTING, threshold));
149 }
150 
RunV8GC(PP_Instance instance)151 void RunV8GC(PP_Instance instance) {
152   // TODO(raymes): Implement this if we need it.
153   NOTIMPLEMENTED();
154 }
155 
156 const PPB_Testing_Private testing_interface = {
157     &ReadImageData,
158     &RunMessageLoop,
159     &QuitMessageLoop,
160     &GetLiveObjectsForInstance,
161     &IsOutOfProcess,
162     &PostPowerSaverStatus,
163     &SubscribeToPowerSaverNotifications,
164     &SimulateInputEvent,
165     &GetDocumentURL,
166     &GetLiveVars,
167     &SetMinimumArrayBufferSizeForShmem,
168     &RunV8GC};
169 
170 }  // namespace
171 
PPB_Testing_Proxy(Dispatcher * dispatcher)172 PPB_Testing_Proxy::PPB_Testing_Proxy(Dispatcher* dispatcher)
173     : InterfaceProxy(dispatcher),
174       ppb_testing_impl_(NULL) {
175   if (!dispatcher->IsPlugin()) {
176     ppb_testing_impl_ = static_cast<const PPB_Testing_Private*>(
177         dispatcher->local_get_interface()(PPB_TESTING_PRIVATE_INTERFACE));
178   }
179 }
180 
~PPB_Testing_Proxy()181 PPB_Testing_Proxy::~PPB_Testing_Proxy() {
182 }
183 
184 // static
GetProxyInterface()185 const PPB_Testing_Private* PPB_Testing_Proxy::GetProxyInterface() {
186   return &testing_interface;
187 }
188 
OnMessageReceived(const IPC::Message & msg)189 bool PPB_Testing_Proxy::OnMessageReceived(const IPC::Message& msg) {
190   if (!dispatcher()->permissions().HasPermission(PERMISSION_TESTING))
191     return false;
192 
193   bool handled = true;
194   IPC_BEGIN_MESSAGE_MAP(PPB_Testing_Proxy, msg)
195     IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBTesting_ReadImageData,
196                         OnMsgReadImageData)
197     IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBTesting_GetLiveObjectsForInstance,
198                         OnMsgGetLiveObjectsForInstance)
199     IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBTesting_PostPowerSaverStatus,
200                         OnMsgPostPowerSaverStatus)
201     IPC_MESSAGE_HANDLER(
202         PpapiHostMsg_PPBTesting_SubscribeToPowerSaverNotifications,
203         OnMsgSubscribeToPowerSaverNotifications)
204     IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBTesting_SimulateInputEvent,
205                         OnMsgSimulateInputEvent)
206     IPC_MESSAGE_HANDLER(
207         PpapiHostMsg_PPBTesting_SetMinimumArrayBufferSizeForShmem,
208         OnMsgSetMinimumArrayBufferSizeForShmem)
209     IPC_MESSAGE_UNHANDLED(handled = false)
210   IPC_END_MESSAGE_MAP()
211   return handled;
212 }
213 
OnMsgReadImageData(const HostResource & device_context_2d,const HostResource & image,const PP_Point & top_left,PP_Bool * result)214 void PPB_Testing_Proxy::OnMsgReadImageData(
215     const HostResource& device_context_2d,
216     const HostResource& image,
217     const PP_Point& top_left,
218     PP_Bool* result) {
219   *result = ppb_testing_impl_->ReadImageData(
220       device_context_2d.host_resource(), image.host_resource(), &top_left);
221 }
222 
OnMsgRunMessageLoop(PP_Instance instance)223 void PPB_Testing_Proxy::OnMsgRunMessageLoop(PP_Instance instance) {
224   ppb_testing_impl_->RunMessageLoop(instance);
225 }
226 
OnMsgQuitMessageLoop(PP_Instance instance)227 void PPB_Testing_Proxy::OnMsgQuitMessageLoop(PP_Instance instance) {
228   ppb_testing_impl_->QuitMessageLoop(instance);
229 }
230 
OnMsgGetLiveObjectsForInstance(PP_Instance instance,uint32_t * result)231 void PPB_Testing_Proxy::OnMsgGetLiveObjectsForInstance(PP_Instance instance,
232                                                        uint32_t* result) {
233   *result = ppb_testing_impl_->GetLiveObjectsForInstance(instance);
234 }
235 
OnMsgPostPowerSaverStatus(PP_Instance instance)236 void PPB_Testing_Proxy::OnMsgPostPowerSaverStatus(PP_Instance instance) {
237   ppb_testing_impl_->PostPowerSaverStatus(instance);
238 }
239 
OnMsgSubscribeToPowerSaverNotifications(PP_Instance instance)240 void PPB_Testing_Proxy::OnMsgSubscribeToPowerSaverNotifications(
241     PP_Instance instance) {
242   ppb_testing_impl_->SubscribeToPowerSaverNotifications(instance);
243 }
244 
OnMsgSimulateInputEvent(PP_Instance instance,const InputEventData & input_event)245 void PPB_Testing_Proxy::OnMsgSimulateInputEvent(
246     PP_Instance instance,
247     const InputEventData& input_event) {
248   scoped_refptr<PPB_InputEvent_Shared> input_event_impl(
249       new PPB_InputEvent_Shared(OBJECT_IS_PROXY, instance, input_event));
250   ppb_testing_impl_->SimulateInputEvent(instance,
251                                         input_event_impl->pp_resource());
252 }
253 
OnMsgSetMinimumArrayBufferSizeForShmem(uint32_t threshold)254 void PPB_Testing_Proxy::OnMsgSetMinimumArrayBufferSizeForShmem(
255     uint32_t threshold) {
256   RawVarDataGraph::SetMinimumArrayBufferSizeForShmemForTest(threshold);
257 }
258 
259 }  // namespace proxy
260 }  // namespace ppapi
261