[email protected] | 502ec4dd | 2012-06-11 20:45:01 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "ppapi/proxy/ppb_flash_device_id_proxy.h" |
| 6 | |
| 7 | #include "base/compiler_specific.h" |
| 8 | #include "ppapi/c/pp_errors.h" |
| 9 | #include "ppapi/proxy/plugin_dispatcher.h" |
| 10 | #include "ppapi/proxy/plugin_globals.h" |
| 11 | #include "ppapi/proxy/plugin_proxy_delegate.h" |
| 12 | #include "ppapi/proxy/ppapi_messages.h" |
| 13 | #include "ppapi/shared_impl/resource.h" |
| 14 | #include "ppapi/shared_impl/tracked_callback.h" |
| 15 | #include "ppapi/shared_impl/var.h" |
| 16 | #include "ppapi/thunk/enter.h" |
| 17 | #include "ppapi/thunk/ppb_flash_device_id_api.h" |
| 18 | |
| 19 | using ppapi::thunk::PPB_Flash_DeviceID_API; |
| 20 | |
| 21 | namespace ppapi { |
| 22 | namespace proxy { |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | class DeviceID : public Resource, public PPB_Flash_DeviceID_API { |
| 27 | public: |
| 28 | DeviceID(PP_Instance instance); |
| 29 | virtual ~DeviceID(); |
| 30 | |
| 31 | // Resource overrides. |
| 32 | virtual PPB_Flash_DeviceID_API* AsPPB_Flash_DeviceID_API() OVERRIDE; |
| 33 | |
| 34 | // PPB_Flash_DeviceID_API implementation. |
| 35 | virtual int32_t GetDeviceID(PP_Var* id, |
| 36 | const PP_CompletionCallback& callback) OVERRIDE; |
| 37 | |
| 38 | void OnReply(int32_t result, const std::string& id); |
| 39 | |
| 40 | private: |
| 41 | // Non-null when a callback is pending. |
| 42 | PP_Var* dest_; |
| 43 | |
| 44 | scoped_refptr<TrackedCallback> callback_; |
| 45 | |
| 46 | DISALLOW_COPY_AND_ASSIGN(DeviceID); |
| 47 | }; |
| 48 | |
| 49 | DeviceID::DeviceID(PP_Instance instance) |
| 50 | : Resource(OBJECT_IS_PROXY, instance), |
| 51 | dest_(NULL) { |
| 52 | } |
| 53 | |
| 54 | DeviceID::~DeviceID() { |
| 55 | } |
| 56 | |
| 57 | PPB_Flash_DeviceID_API* DeviceID::AsPPB_Flash_DeviceID_API() { |
| 58 | return this; |
| 59 | } |
| 60 | |
| 61 | int32_t DeviceID::GetDeviceID(PP_Var* id, |
| 62 | const PP_CompletionCallback& callback) { |
| 63 | if (TrackedCallback::IsPending(callback_)) |
| 64 | return PP_ERROR_INPROGRESS; |
| 65 | if (!id) |
| 66 | return PP_ERROR_BADARGUMENT; |
| 67 | |
| 68 | callback_ = new TrackedCallback(this, callback); |
| 69 | dest_ = id; |
| 70 | |
| 71 | PluginDispatcher* dispatcher = |
| 72 | PluginDispatcher::GetForInstance(pp_instance()); |
| 73 | |
| 74 | PluginGlobals::Get()->plugin_proxy_delegate()->SendToBrowser( |
| 75 | new PpapiHostMsg_PPBFlashDeviceID_Get( |
| 76 | API_ID_PPB_FLASH_DEVICE_ID, dispatcher->plugin_dispatcher_id(), |
| 77 | pp_resource())); |
| 78 | return PP_OK_COMPLETIONPENDING; |
| 79 | } |
| 80 | |
| 81 | void DeviceID::OnReply(int32_t result, const std::string& id) { |
| 82 | if (result == PP_OK) |
| 83 | *dest_ = StringVar::StringToPPVar(id); |
| 84 | else |
| 85 | *dest_ = PP_MakeUndefined(); |
| 86 | dest_ = NULL; |
| 87 | TrackedCallback::ClearAndRun(&callback_, result); |
| 88 | } |
| 89 | |
| 90 | } // namespace |
| 91 | |
| 92 | PPB_Flash_DeviceID_Proxy::PPB_Flash_DeviceID_Proxy(Dispatcher* dispatcher) |
| 93 | : InterfaceProxy(dispatcher) { |
| 94 | } |
| 95 | |
| 96 | PPB_Flash_DeviceID_Proxy::~PPB_Flash_DeviceID_Proxy() { |
| 97 | } |
| 98 | |
| 99 | // static |
| 100 | PP_Resource PPB_Flash_DeviceID_Proxy::CreateProxyResource( |
| 101 | PP_Instance instance) { |
| 102 | return (new DeviceID(instance))->GetReference(); |
| 103 | } |
| 104 | |
| 105 | bool PPB_Flash_DeviceID_Proxy::OnMessageReceived(const IPC::Message& msg) { |
| 106 | bool handled = true; |
| 107 | IPC_BEGIN_MESSAGE_MAP(PPB_Flash_DeviceID_Proxy, msg) |
| 108 | IPC_MESSAGE_HANDLER(PpapiMsg_PPBFlashDeviceID_GetReply, |
| 109 | OnPluginMsgGetReply) |
| 110 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 111 | IPC_END_MESSAGE_MAP() |
| 112 | return handled; |
| 113 | } |
| 114 | |
| 115 | void PPB_Flash_DeviceID_Proxy::OnPluginMsgGetReply(int32 routing_id, |
| 116 | PP_Resource resource, |
| 117 | int32 result, |
| 118 | const std::string& id) { |
| 119 | thunk::EnterResourceNoLock<PPB_Flash_DeviceID_API> enter(resource, false); |
| 120 | if (enter.failed()) |
| 121 | return; // Resource destroyed. |
| 122 | static_cast<DeviceID*>(enter.object())->OnReply(result, id); |
| 123 | } |
| 124 | |
| 125 | } // namespace proxy |
| 126 | } // namespace ppapi |