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.
5 #include "ppapi/proxy/ppb_testing_proxy.h"
7 #include "base/message_loop/message_loop.h"
8 #include "ppapi/c/dev/ppb_testing_dev.h"
9 #include "ppapi/proxy/enter_proxy.h"
10 #include "ppapi/proxy/plugin_dispatcher.h"
11 #include "ppapi/proxy/ppapi_messages.h"
12 #include "ppapi/shared_impl/ppapi_globals.h"
13 #include "ppapi/shared_impl/proxy_lock.h"
14 #include "ppapi/shared_impl/resource.h"
15 #include "ppapi/shared_impl/resource_tracker.h"
16 #include "ppapi/thunk/enter.h"
17 #include "ppapi/thunk/ppb_graphics_2d_api.h"
18 #include "ppapi/thunk/ppb_input_event_api.h"
20 using ppapi::thunk::EnterInstance
;
21 using ppapi::thunk::EnterResource
;
22 using ppapi::thunk::EnterResourceNoLock
;
23 using ppapi::thunk::PPB_Graphics2D_API
;
24 using ppapi::thunk::PPB_InputEvent_API
;
31 PP_Bool
ReadImageData(PP_Resource graphics_2d
,
33 const PP_Point
* top_left
) {
35 Resource
* image_object
=
36 PpapiGlobals::Get()->GetResourceTracker()->GetResource(image
);
39 Resource
* graphics_2d_object
=
40 PpapiGlobals::Get()->GetResourceTracker()->GetResource(graphics_2d
);
41 if (!graphics_2d_object
||
42 image_object
->pp_instance() != graphics_2d_object
->pp_instance())
45 EnterResourceNoLock
<PPB_Graphics2D_API
> enter(graphics_2d
, true);
48 const HostResource
& host_image
= image_object
->host_resource();
49 return enter
.object()->ReadImageData(host_image
.host_resource(), top_left
) ?
53 void RunMessageLoop(PP_Instance instance
) {
54 base::MessageLoop::ScopedNestableTaskAllower
allow(
55 base::MessageLoop::current());
56 CHECK(PpapiGlobals::Get()->GetMainThreadMessageLoop()->
57 BelongsToCurrentThread());
58 base::MessageLoop::current()->Run();
61 void QuitMessageLoop(PP_Instance instance
) {
62 CHECK(PpapiGlobals::Get()->GetMainThreadMessageLoop()->
63 BelongsToCurrentThread());
64 base::MessageLoop::current()->QuitNow();
67 uint32_t GetLiveObjectsForInstance(PP_Instance instance_id
) {
69 PluginDispatcher
* dispatcher
= PluginDispatcher::GetForInstance(instance_id
);
71 return static_cast<uint32_t>(-1);
74 dispatcher
->Send(new PpapiHostMsg_PPBTesting_GetLiveObjectsForInstance(
75 API_ID_PPB_TESTING
, instance_id
, &result
));
79 PP_Bool
IsOutOfProcess() {
83 void SimulateInputEvent(PP_Instance instance_id
, PP_Resource input_event
) {
85 PluginDispatcher
* dispatcher
= PluginDispatcher::GetForInstance(instance_id
);
88 EnterResourceNoLock
<PPB_InputEvent_API
> enter(input_event
, false);
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
));
97 PP_Var
GetDocumentURL(PP_Instance instance
, PP_URLComponents_Dev
* components
) {
98 EnterInstance
enter(instance
);
100 return PP_MakeUndefined();
101 return enter
.functions()->GetDocumentURL(instance
, components
);
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.
107 uint32_t GetLiveVars(PP_Var live_vars
[], uint32_t array_size
) {
109 std::vector
<PP_Var
> vars
=
110 PpapiGlobals::Get()->GetVarTracker()->GetLiveVars();
112 i
< std::min(static_cast<size_t>(array_size
), vars
.size());
114 live_vars
[i
] = vars
[i
];
118 void SetMinimumArrayBufferSizeForShmem(PP_Instance instance
,
119 uint32_t threshold
) {
121 RawVarDataGraph::SetMinimumArrayBufferSizeForShmemForTest(threshold
);
122 PluginDispatcher
* dispatcher
= PluginDispatcher::GetForInstance(instance
);
126 new PpapiHostMsg_PPBTesting_SetMinimumArrayBufferSizeForShmem(
127 API_ID_PPB_TESTING
, threshold
));
130 const PPB_Testing_Dev testing_interface
= {
134 &GetLiveObjectsForInstance
,
139 &SetMinimumArrayBufferSizeForShmem
142 InterfaceProxy
* CreateTestingProxy(Dispatcher
* dispatcher
) {
143 return new PPB_Testing_Proxy(dispatcher
);
148 PPB_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
));
157 PPB_Testing_Proxy::~PPB_Testing_Proxy() {
161 const InterfaceProxy::Info
* PPB_Testing_Proxy::GetInfo() {
162 static const Info info
= {
164 PPB_TESTING_DEV_INTERFACE
,
172 bool PPB_Testing_Proxy::OnMessageReceived(const IPC::Message
& msg
) {
173 if (!dispatcher()->permissions().HasPermission(PERMISSION_TESTING
))
177 IPC_BEGIN_MESSAGE_MAP(PPB_Testing_Proxy
, msg
)
178 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBTesting_ReadImageData
,
180 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBTesting_GetLiveObjectsForInstance
,
181 OnMsgGetLiveObjectsForInstance
)
182 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBTesting_SimulateInputEvent
,
183 OnMsgSimulateInputEvent
)
185 PpapiHostMsg_PPBTesting_SetMinimumArrayBufferSizeForShmem
,
186 OnMsgSetMinimumArrayBufferSizeForShmem
)
187 IPC_MESSAGE_UNHANDLED(handled
= false)
188 IPC_END_MESSAGE_MAP()
192 void PPB_Testing_Proxy::OnMsgReadImageData(
193 const HostResource
& device_context_2d
,
194 const HostResource
& image
,
195 const PP_Point
& top_left
,
197 *result
= ppb_testing_impl_
->ReadImageData(
198 device_context_2d
.host_resource(), image
.host_resource(), &top_left
);
201 void PPB_Testing_Proxy::OnMsgRunMessageLoop(PP_Instance instance
) {
202 ppb_testing_impl_
->RunMessageLoop(instance
);
205 void PPB_Testing_Proxy::OnMsgQuitMessageLoop(PP_Instance instance
) {
206 ppb_testing_impl_
->QuitMessageLoop(instance
);
209 void PPB_Testing_Proxy::OnMsgGetLiveObjectsForInstance(PP_Instance instance
,
211 *result
= ppb_testing_impl_
->GetLiveObjectsForInstance(instance
);
214 void PPB_Testing_Proxy::OnMsgSimulateInputEvent(
215 PP_Instance instance
,
216 const InputEventData
& input_event
) {
217 scoped_refptr
<PPB_InputEvent_Shared
> input_event_impl(
218 new PPB_InputEvent_Shared(OBJECT_IS_PROXY
, instance
, input_event
));
219 ppb_testing_impl_
->SimulateInputEvent(instance
,
220 input_event_impl
->pp_resource());
223 void PPB_Testing_Proxy::OnMsgSetMinimumArrayBufferSizeForShmem(
224 uint32_t threshold
) {
225 RawVarDataGraph::SetMinimumArrayBufferSizeForShmemForTest(threshold
);