[email protected] | 00d320a | 2012-02-14 00:27:04 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 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 "base/message_loop.h" |
| 8 | #include "ppapi/c/dev/ppb_testing_dev.h" |
[email protected] | ed33e2e | 2011-11-20 21:18:07 | [diff] [blame] | 9 | #include "ppapi/proxy/enter_proxy.h" |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 10 | #include "ppapi/proxy/plugin_dispatcher.h" |
| 11 | #include "ppapi/proxy/ppapi_messages.h" |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 12 | #include "ppapi/shared_impl/ppapi_globals.h" |
[email protected] | 4c41d3f | 2012-02-15 01:44:47 | [diff] [blame] | 13 | #include "ppapi/shared_impl/proxy_lock.h" |
[email protected] | 7f8b26b | 2011-08-18 15:41:01 | [diff] [blame] | 14 | #include "ppapi/shared_impl/resource.h" |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 15 | #include "ppapi/shared_impl/resource_tracker.h" |
[email protected] | ed33e2e | 2011-11-20 21:18:07 | [diff] [blame] | 16 | #include "ppapi/thunk/enter.h" |
[email protected] | 08e65c1 | 2012-11-29 18:53:29 | [diff] [blame^] | 17 | #include "ppapi/thunk/ppb_graphics_2d_api.h" |
[email protected] | ed33e2e | 2011-11-20 21:18:07 | [diff] [blame] | 18 | #include "ppapi/thunk/ppb_input_event_api.h" |
| 19 | |
[email protected] | 961e897 | 2011-12-15 15:29:19 | [diff] [blame] | 20 | using ppapi::thunk::EnterInstance; |
[email protected] | 08e65c1 | 2012-11-29 18:53:29 | [diff] [blame^] | 21 | using ppapi::thunk::EnterResource; |
[email protected] | 4c41d3f | 2012-02-15 01:44:47 | [diff] [blame] | 22 | using ppapi::thunk::EnterResourceNoLock; |
[email protected] | 08e65c1 | 2012-11-29 18:53:29 | [diff] [blame^] | 23 | using ppapi::thunk::PPB_Graphics2D_API; |
[email protected] | ed33e2e | 2011-11-20 21:18:07 | [diff] [blame] | 24 | using ppapi::thunk::PPB_InputEvent_API; |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 25 | |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 26 | namespace ppapi { |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 27 | namespace proxy { |
| 28 | |
| 29 | namespace { |
| 30 | |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 31 | PP_Bool ReadImageData(PP_Resource graphics_2d, |
[email protected] | 19d2b01 | 2010-11-08 16:32:18 | [diff] [blame] | 32 | PP_Resource image, |
| 33 | const PP_Point* top_left) { |
[email protected] | 4c41d3f | 2012-02-15 01:44:47 | [diff] [blame] | 34 | ProxyAutoLock lock; |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 35 | Resource* image_object = |
| 36 | PpapiGlobals::Get()->GetResourceTracker()->GetResource(image); |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 37 | if (!image_object) |
| 38 | return PP_FALSE; |
[email protected] | 7f8b26b | 2011-08-18 15:41:01 | [diff] [blame] | 39 | Resource* graphics_2d_object = |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 40 | PpapiGlobals::Get()->GetResourceTracker()->GetResource(graphics_2d); |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 41 | if (!graphics_2d_object || |
[email protected] | 7f8b26b | 2011-08-18 15:41:01 | [diff] [blame] | 42 | image_object->pp_instance() != graphics_2d_object->pp_instance()) |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 43 | return PP_FALSE; |
| 44 | |
[email protected] | 08e65c1 | 2012-11-29 18:53:29 | [diff] [blame^] | 45 | EnterResourceNoLock<PPB_Graphics2D_API> enter(graphics_2d, true); |
| 46 | if (enter.failed()) |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 47 | return PP_FALSE; |
[email protected] | 08e65c1 | 2012-11-29 18:53:29 | [diff] [blame^] | 48 | 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] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 51 | } |
| 52 | |
[email protected] | 7358d57 | 2011-02-15 18:44:40 | [diff] [blame] | 53 | void RunMessageLoop(PP_Instance instance) { |
[email protected] | b5717a4 | 2012-02-14 19:33:52 | [diff] [blame] | 54 | MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
[email protected] | 77c3417 | 2012-11-08 18:55:16 | [diff] [blame] | 55 | CHECK(PpapiGlobals::Get()->GetMainThreadMessageLoop()-> |
| 56 | BelongsToCurrentThread()); |
[email protected] | b20df1c | 2011-08-03 14:38:24 | [diff] [blame] | 57 | MessageLoop::current()->Run(); |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 58 | } |
| 59 | |
[email protected] | 7358d57 | 2011-02-15 18:44:40 | [diff] [blame] | 60 | void QuitMessageLoop(PP_Instance instance) { |
[email protected] | 77c3417 | 2012-11-08 18:55:16 | [diff] [blame] | 61 | CHECK(PpapiGlobals::Get()->GetMainThreadMessageLoop()-> |
| 62 | BelongsToCurrentThread()); |
[email protected] | b20df1c | 2011-08-03 14:38:24 | [diff] [blame] | 63 | MessageLoop::current()->QuitNow(); |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 64 | } |
| 65 | |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 66 | uint32_t GetLiveObjectsForInstance(PP_Instance instance_id) { |
[email protected] | 4c41d3f | 2012-02-15 01:44:47 | [diff] [blame] | 67 | ProxyAutoLock lock; |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 68 | PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance_id); |
| 69 | if (!dispatcher) |
| 70 | return static_cast<uint32_t>(-1); |
| 71 | |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 72 | uint32_t result = 0; |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 73 | dispatcher->Send(new PpapiHostMsg_PPBTesting_GetLiveObjectsForInstance( |
[email protected] | ac4b54d | 2011-10-20 23:09:28 | [diff] [blame] | 74 | API_ID_PPB_TESTING, instance_id, &result)); |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 75 | return result; |
| 76 | } |
| 77 | |
[email protected] | 5d904b9e | 2011-08-30 19:38:31 | [diff] [blame] | 78 | PP_Bool IsOutOfProcess() { |
| 79 | return PP_TRUE; |
| 80 | } |
| 81 | |
[email protected] | ed33e2e | 2011-11-20 21:18:07 | [diff] [blame] | 82 | void SimulateInputEvent(PP_Instance instance_id, PP_Resource input_event) { |
[email protected] | 4c41d3f | 2012-02-15 01:44:47 | [diff] [blame] | 83 | ProxyAutoLock lock; |
[email protected] | ed33e2e | 2011-11-20 21:18:07 | [diff] [blame] | 84 | PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance_id); |
| 85 | if (!dispatcher) |
| 86 | return; |
[email protected] | 4c41d3f | 2012-02-15 01:44:47 | [diff] [blame] | 87 | EnterResourceNoLock<PPB_InputEvent_API> enter(input_event, false); |
[email protected] | ed33e2e | 2011-11-20 21:18:07 | [diff] [blame] | 88 | if (enter.failed()) |
| 89 | return; |
| 90 | |
| 91 | const InputEventData& input_event_data = enter.object()->GetInputEventData(); |
| 92 | dispatcher->Send(new PpapiHostMsg_PPBTesting_SimulateInputEvent( |
| 93 | API_ID_PPB_TESTING, instance_id, input_event_data)); |
| 94 | } |
| 95 | |
[email protected] | 961e897 | 2011-12-15 15:29:19 | [diff] [blame] | 96 | PP_Var GetDocumentURL(PP_Instance instance, PP_URLComponents_Dev* components) { |
| 97 | EnterInstance enter(instance); |
| 98 | if (enter.failed()) |
| 99 | return PP_MakeUndefined(); |
| 100 | return enter.functions()->GetDocumentURL(instance, components); |
| 101 | } |
| 102 | |
[email protected] | a732cec | 2011-12-22 08:35:52 | [diff] [blame] | 103 | // TODO(dmichael): Ideally we could get a way to check the number of vars in the |
| 104 | // host-side tracker when running out-of-process, to make sure the proxy does |
| 105 | // not leak host-side vars. |
| 106 | uint32_t GetLiveVars(PP_Var live_vars[], uint32_t array_size) { |
[email protected] | 4c41d3f | 2012-02-15 01:44:47 | [diff] [blame] | 107 | ProxyAutoLock lock; |
[email protected] | a732cec | 2011-12-22 08:35:52 | [diff] [blame] | 108 | std::vector<PP_Var> vars = |
| 109 | PpapiGlobals::Get()->GetVarTracker()->GetLiveVars(); |
| 110 | for (size_t i = 0u; |
| 111 | i < std::min(static_cast<size_t>(array_size), vars.size()); |
| 112 | ++i) |
| 113 | live_vars[i] = vars[i]; |
| 114 | return vars.size(); |
| 115 | } |
| 116 | |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 117 | const PPB_Testing_Dev testing_interface = { |
| 118 | &ReadImageData, |
| 119 | &RunMessageLoop, |
| 120 | &QuitMessageLoop, |
[email protected] | 5d904b9e | 2011-08-30 19:38:31 | [diff] [blame] | 121 | &GetLiveObjectsForInstance, |
[email protected] | ed33e2e | 2011-11-20 21:18:07 | [diff] [blame] | 122 | &IsOutOfProcess, |
[email protected] | 961e897 | 2011-12-15 15:29:19 | [diff] [blame] | 123 | &SimulateInputEvent, |
[email protected] | a732cec | 2011-12-22 08:35:52 | [diff] [blame] | 124 | &GetDocumentURL, |
| 125 | &GetLiveVars |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 126 | }; |
| 127 | |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 128 | InterfaceProxy* CreateTestingProxy(Dispatcher* dispatcher) { |
| 129 | return new PPB_Testing_Proxy(dispatcher); |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 130 | } |
| 131 | |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 132 | } // namespace |
| 133 | |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 134 | PPB_Testing_Proxy::PPB_Testing_Proxy(Dispatcher* dispatcher) |
| 135 | : InterfaceProxy(dispatcher), |
| 136 | ppb_testing_impl_(NULL) { |
| 137 | if (!dispatcher->IsPlugin()) { |
| 138 | ppb_testing_impl_ = static_cast<const PPB_Testing_Dev*>( |
| 139 | dispatcher->local_get_interface()(PPB_TESTING_DEV_INTERFACE)); |
| 140 | } |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | PPB_Testing_Proxy::~PPB_Testing_Proxy() { |
| 144 | } |
| 145 | |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 146 | // static |
| 147 | const InterfaceProxy::Info* PPB_Testing_Proxy::GetInfo() { |
| 148 | static const Info info = { |
| 149 | &testing_interface, |
| 150 | PPB_TESTING_DEV_INTERFACE, |
[email protected] | ac4b54d | 2011-10-20 23:09:28 | [diff] [blame] | 151 | API_ID_PPB_TESTING, |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 152 | false, |
| 153 | &CreateTestingProxy, |
| 154 | }; |
| 155 | return &info; |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 156 | } |
| 157 | |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 158 | bool PPB_Testing_Proxy::OnMessageReceived(const IPC::Message& msg) { |
| 159 | bool handled = true; |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 160 | IPC_BEGIN_MESSAGE_MAP(PPB_Testing_Proxy, msg) |
| 161 | IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBTesting_ReadImageData, |
| 162 | OnMsgReadImageData) |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 163 | IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBTesting_GetLiveObjectsForInstance, |
| 164 | OnMsgGetLiveObjectsForInstance) |
[email protected] | ed33e2e | 2011-11-20 21:18:07 | [diff] [blame] | 165 | IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBTesting_SimulateInputEvent, |
| 166 | OnMsgSimulateInputEvent) |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 167 | IPC_MESSAGE_UNHANDLED(handled = false) |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 168 | IPC_END_MESSAGE_MAP() |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 169 | return handled; |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 170 | } |
| 171 | |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 172 | void PPB_Testing_Proxy::OnMsgReadImageData( |
| 173 | const HostResource& device_context_2d, |
| 174 | const HostResource& image, |
| 175 | const PP_Point& top_left, |
| 176 | PP_Bool* result) { |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 177 | *result = ppb_testing_impl_->ReadImageData( |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 178 | device_context_2d.host_resource(), image.host_resource(), &top_left); |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 179 | } |
| 180 | |
[email protected] | 7358d57 | 2011-02-15 18:44:40 | [diff] [blame] | 181 | void PPB_Testing_Proxy::OnMsgRunMessageLoop(PP_Instance instance) { |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 182 | ppb_testing_impl_->RunMessageLoop(instance); |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 183 | } |
| 184 | |
[email protected] | 7358d57 | 2011-02-15 18:44:40 | [diff] [blame] | 185 | void PPB_Testing_Proxy::OnMsgQuitMessageLoop(PP_Instance instance) { |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 186 | ppb_testing_impl_->QuitMessageLoop(instance); |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 187 | } |
| 188 | |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 189 | void PPB_Testing_Proxy::OnMsgGetLiveObjectsForInstance(PP_Instance instance, |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 190 | uint32_t* result) { |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 191 | *result = ppb_testing_impl_->GetLiveObjectsForInstance(instance); |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 192 | } |
| 193 | |
[email protected] | ed33e2e | 2011-11-20 21:18:07 | [diff] [blame] | 194 | void PPB_Testing_Proxy::OnMsgSimulateInputEvent( |
| 195 | PP_Instance instance, |
| 196 | const InputEventData& input_event) { |
[email protected] | 9a57839 | 2011-12-07 18:59:27 | [diff] [blame] | 197 | scoped_refptr<PPB_InputEvent_Shared> input_event_impl( |
[email protected] | 00d320a | 2012-02-14 00:27:04 | [diff] [blame] | 198 | new PPB_InputEvent_Shared(OBJECT_IS_PROXY, instance, input_event)); |
[email protected] | ed33e2e | 2011-11-20 21:18:07 | [diff] [blame] | 199 | ppb_testing_impl_->SimulateInputEvent(instance, |
| 200 | input_event_impl->pp_resource()); |
| 201 | } |
| 202 | |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 203 | } // namespace proxy |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 204 | } // namespace ppapi |