[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 1 | // Copyright (c) 2011 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 | |
| 18 | using ppapi::thunk::EnterResource; |
| 19 | using ppapi::thunk::PPB_InputEvent_API; |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 20 | |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 21 | namespace ppapi { |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 22 | namespace proxy { |
| 23 | |
| 24 | namespace { |
| 25 | |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 26 | PP_Bool ReadImageData(PP_Resource graphics_2d, |
[email protected] | 19d2b01 | 2010-11-08 16:32:18 | [diff] [blame] | 27 | PP_Resource image, |
| 28 | const PP_Point* top_left) { |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 29 | Resource* image_object = |
| 30 | PpapiGlobals::Get()->GetResourceTracker()->GetResource(image); |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 31 | if (!image_object) |
| 32 | return PP_FALSE; |
[email protected] | 7f8b26b | 2011-08-18 15:41:01 | [diff] [blame] | 33 | Resource* graphics_2d_object = |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 34 | PpapiGlobals::Get()->GetResourceTracker()->GetResource(graphics_2d); |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 35 | if (!graphics_2d_object || |
[email protected] | 7f8b26b | 2011-08-18 15:41:01 | [diff] [blame] | 36 | image_object->pp_instance() != graphics_2d_object->pp_instance()) |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 37 | return PP_FALSE; |
| 38 | |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 39 | PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance( |
[email protected] | 7f8b26b | 2011-08-18 15:41:01 | [diff] [blame] | 40 | image_object->pp_instance()); |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 41 | if (!dispatcher) |
| 42 | return PP_FALSE; |
| 43 | |
[email protected] | 19d2b01 | 2010-11-08 16:32:18 | [diff] [blame] | 44 | PP_Bool result = PP_FALSE; |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 45 | dispatcher->Send(new PpapiHostMsg_PPBTesting_ReadImageData( |
[email protected] | ac4b54d | 2011-10-20 23:09:28 | [diff] [blame] | 46 | API_ID_PPB_TESTING, graphics_2d_object->host_resource(), |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 47 | image_object->host_resource(), *top_left, &result)); |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 48 | return result; |
| 49 | } |
| 50 | |
[email protected] | 7358d57 | 2011-02-15 18:44:40 | [diff] [blame] | 51 | void RunMessageLoop(PP_Instance instance) { |
[email protected] | b20df1c | 2011-08-03 14:38:24 | [diff] [blame] | 52 | bool old_state = MessageLoop::current()->NestableTasksAllowed(); |
| 53 | MessageLoop::current()->SetNestableTasksAllowed(true); |
| 54 | MessageLoop::current()->Run(); |
| 55 | MessageLoop::current()->SetNestableTasksAllowed(old_state); |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 56 | } |
| 57 | |
[email protected] | 7358d57 | 2011-02-15 18:44:40 | [diff] [blame] | 58 | void QuitMessageLoop(PP_Instance instance) { |
[email protected] | b20df1c | 2011-08-03 14:38:24 | [diff] [blame] | 59 | MessageLoop::current()->QuitNow(); |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 60 | } |
| 61 | |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 62 | uint32_t GetLiveObjectsForInstance(PP_Instance instance_id) { |
| 63 | PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance_id); |
| 64 | if (!dispatcher) |
| 65 | return static_cast<uint32_t>(-1); |
| 66 | |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 67 | uint32_t result = 0; |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 68 | dispatcher->Send(new PpapiHostMsg_PPBTesting_GetLiveObjectsForInstance( |
[email protected] | ac4b54d | 2011-10-20 23:09:28 | [diff] [blame] | 69 | API_ID_PPB_TESTING, instance_id, &result)); |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 70 | return result; |
| 71 | } |
| 72 | |
[email protected] | 5d904b9e | 2011-08-30 19:38:31 | [diff] [blame] | 73 | PP_Bool IsOutOfProcess() { |
| 74 | return PP_TRUE; |
| 75 | } |
| 76 | |
[email protected] | ed33e2e | 2011-11-20 21:18:07 | [diff] [blame] | 77 | void SimulateInputEvent(PP_Instance instance_id, PP_Resource input_event) { |
| 78 | PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance_id); |
| 79 | if (!dispatcher) |
| 80 | return; |
| 81 | EnterResource<PPB_InputEvent_API> enter(input_event, false); |
| 82 | if (enter.failed()) |
| 83 | return; |
| 84 | |
| 85 | const InputEventData& input_event_data = enter.object()->GetInputEventData(); |
| 86 | dispatcher->Send(new PpapiHostMsg_PPBTesting_SimulateInputEvent( |
| 87 | API_ID_PPB_TESTING, instance_id, input_event_data)); |
| 88 | } |
| 89 | |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 90 | const PPB_Testing_Dev testing_interface = { |
| 91 | &ReadImageData, |
| 92 | &RunMessageLoop, |
| 93 | &QuitMessageLoop, |
[email protected] | 5d904b9e | 2011-08-30 19:38:31 | [diff] [blame] | 94 | &GetLiveObjectsForInstance, |
[email protected] | ed33e2e | 2011-11-20 21:18:07 | [diff] [blame] | 95 | &IsOutOfProcess, |
| 96 | &SimulateInputEvent |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 97 | }; |
| 98 | |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 99 | InterfaceProxy* CreateTestingProxy(Dispatcher* dispatcher) { |
| 100 | return new PPB_Testing_Proxy(dispatcher); |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 101 | } |
| 102 | |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 103 | } // namespace |
| 104 | |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 105 | PPB_Testing_Proxy::PPB_Testing_Proxy(Dispatcher* dispatcher) |
| 106 | : InterfaceProxy(dispatcher), |
| 107 | ppb_testing_impl_(NULL) { |
| 108 | if (!dispatcher->IsPlugin()) { |
| 109 | ppb_testing_impl_ = static_cast<const PPB_Testing_Dev*>( |
| 110 | dispatcher->local_get_interface()(PPB_TESTING_DEV_INTERFACE)); |
| 111 | } |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | PPB_Testing_Proxy::~PPB_Testing_Proxy() { |
| 115 | } |
| 116 | |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 117 | // static |
| 118 | const InterfaceProxy::Info* PPB_Testing_Proxy::GetInfo() { |
| 119 | static const Info info = { |
| 120 | &testing_interface, |
| 121 | PPB_TESTING_DEV_INTERFACE, |
[email protected] | ac4b54d | 2011-10-20 23:09:28 | [diff] [blame] | 122 | API_ID_PPB_TESTING, |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 123 | false, |
| 124 | &CreateTestingProxy, |
| 125 | }; |
| 126 | return &info; |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 127 | } |
| 128 | |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 129 | bool PPB_Testing_Proxy::OnMessageReceived(const IPC::Message& msg) { |
| 130 | bool handled = true; |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 131 | IPC_BEGIN_MESSAGE_MAP(PPB_Testing_Proxy, msg) |
| 132 | IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBTesting_ReadImageData, |
| 133 | OnMsgReadImageData) |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 134 | IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBTesting_GetLiveObjectsForInstance, |
| 135 | OnMsgGetLiveObjectsForInstance) |
[email protected] | ed33e2e | 2011-11-20 21:18:07 | [diff] [blame] | 136 | IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBTesting_SimulateInputEvent, |
| 137 | OnMsgSimulateInputEvent) |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 138 | IPC_MESSAGE_UNHANDLED(handled = false) |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 139 | IPC_END_MESSAGE_MAP() |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 140 | return handled; |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 141 | } |
| 142 | |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 143 | void PPB_Testing_Proxy::OnMsgReadImageData( |
| 144 | const HostResource& device_context_2d, |
| 145 | const HostResource& image, |
| 146 | const PP_Point& top_left, |
| 147 | PP_Bool* result) { |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 148 | *result = ppb_testing_impl_->ReadImageData( |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 149 | device_context_2d.host_resource(), image.host_resource(), &top_left); |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 150 | } |
| 151 | |
[email protected] | 7358d57 | 2011-02-15 18:44:40 | [diff] [blame] | 152 | void PPB_Testing_Proxy::OnMsgRunMessageLoop(PP_Instance instance) { |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 153 | ppb_testing_impl_->RunMessageLoop(instance); |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 154 | } |
| 155 | |
[email protected] | 7358d57 | 2011-02-15 18:44:40 | [diff] [blame] | 156 | void PPB_Testing_Proxy::OnMsgQuitMessageLoop(PP_Instance instance) { |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 157 | ppb_testing_impl_->QuitMessageLoop(instance); |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 158 | } |
| 159 | |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 160 | void PPB_Testing_Proxy::OnMsgGetLiveObjectsForInstance(PP_Instance instance, |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 161 | uint32_t* result) { |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 162 | *result = ppb_testing_impl_->GetLiveObjectsForInstance(instance); |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 163 | } |
| 164 | |
[email protected] | ed33e2e | 2011-11-20 21:18:07 | [diff] [blame] | 165 | void PPB_Testing_Proxy::OnMsgSimulateInputEvent( |
| 166 | PP_Instance instance, |
| 167 | const InputEventData& input_event) { |
[email protected] | 9a57839 | 2011-12-07 18:59:27 | [diff] [blame^] | 168 | scoped_refptr<PPB_InputEvent_Shared> input_event_impl( |
| 169 | new PPB_InputEvent_Shared(PPB_InputEvent_Shared::InitAsProxy(), |
| 170 | instance, |
| 171 | input_event)); |
[email protected] | ed33e2e | 2011-11-20 21:18:07 | [diff] [blame] | 172 | ppb_testing_impl_->SimulateInputEvent(instance, |
| 173 | input_event_impl->pp_resource()); |
| 174 | } |
| 175 | |
[email protected] | 1054c07 | 2010-11-05 22:52:48 | [diff] [blame] | 176 | } // namespace proxy |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 177 | } // namespace ppapi |