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