blob: bb66b33b224ab9299c983e9c9fee9f48180357a3 [file] [log] [blame]
[email protected]502ec4dd2012-06-11 20:45:011// 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
19using ppapi::thunk::PPB_Flash_DeviceID_API;
20
21namespace ppapi {
22namespace proxy {
23
24namespace {
25
26class 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
49DeviceID::DeviceID(PP_Instance instance)
50 : Resource(OBJECT_IS_PROXY, instance),
51 dest_(NULL) {
52}
53
54DeviceID::~DeviceID() {
55}
56
57PPB_Flash_DeviceID_API* DeviceID::AsPPB_Flash_DeviceID_API() {
58 return this;
59}
60
61int32_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
81void 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
92PPB_Flash_DeviceID_Proxy::PPB_Flash_DeviceID_Proxy(Dispatcher* dispatcher)
93 : InterfaceProxy(dispatcher) {
94}
95
96PPB_Flash_DeviceID_Proxy::~PPB_Flash_DeviceID_Proxy() {
97}
98
99// static
100PP_Resource PPB_Flash_DeviceID_Proxy::CreateProxyResource(
101 PP_Instance instance) {
102 return (new DeviceID(instance))->GetReference();
103}
104
105bool 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
115void 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