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