summaryrefslogtreecommitdiffstats
path: root/ppapi/proxy/ppb_flash_device_id_proxy.cc
blob: bb66b33b224ab9299c983e9c9fee9f48180357a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ppapi/proxy/ppb_flash_device_id_proxy.h"

#include "base/compiler_specific.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/proxy/plugin_dispatcher.h"
#include "ppapi/proxy/plugin_globals.h"
#include "ppapi/proxy/plugin_proxy_delegate.h"
#include "ppapi/proxy/ppapi_messages.h"
#include "ppapi/shared_impl/resource.h"
#include "ppapi/shared_impl/tracked_callback.h"
#include "ppapi/shared_impl/var.h"
#include "ppapi/thunk/enter.h"
#include "ppapi/thunk/ppb_flash_device_id_api.h"

using ppapi::thunk::PPB_Flash_DeviceID_API;

namespace ppapi {
namespace proxy {

namespace {

class DeviceID : public Resource, public PPB_Flash_DeviceID_API {
 public:
  DeviceID(PP_Instance instance);
  virtual ~DeviceID();

  // Resource overrides.
  virtual PPB_Flash_DeviceID_API* AsPPB_Flash_DeviceID_API() OVERRIDE;

  // PPB_Flash_DeviceID_API implementation.
  virtual int32_t GetDeviceID(PP_Var* id,
                              const PP_CompletionCallback& callback) OVERRIDE;

  void OnReply(int32_t result, const std::string& id);

 private:
  // Non-null when a callback is pending.
  PP_Var* dest_;

  scoped_refptr<TrackedCallback> callback_;

  DISALLOW_COPY_AND_ASSIGN(DeviceID);
};

DeviceID::DeviceID(PP_Instance instance)
    : Resource(OBJECT_IS_PROXY, instance),
      dest_(NULL) {
}

DeviceID::~DeviceID() {
}

PPB_Flash_DeviceID_API* DeviceID::AsPPB_Flash_DeviceID_API() {
  return this;
}

int32_t DeviceID::GetDeviceID(PP_Var* id,
                              const PP_CompletionCallback& callback) {
  if (TrackedCallback::IsPending(callback_))
    return PP_ERROR_INPROGRESS;
  if (!id)
    return PP_ERROR_BADARGUMENT;

  callback_ = new TrackedCallback(this, callback);
  dest_ = id;

  PluginDispatcher* dispatcher =
      PluginDispatcher::GetForInstance(pp_instance());

  PluginGlobals::Get()->plugin_proxy_delegate()->SendToBrowser(
      new PpapiHostMsg_PPBFlashDeviceID_Get(
          API_ID_PPB_FLASH_DEVICE_ID, dispatcher->plugin_dispatcher_id(),
          pp_resource()));
  return PP_OK_COMPLETIONPENDING;
}

void DeviceID::OnReply(int32_t result, const std::string& id) {
  if (result == PP_OK)
    *dest_ = StringVar::StringToPPVar(id);
  else
    *dest_ = PP_MakeUndefined();
  dest_ = NULL;
  TrackedCallback::ClearAndRun(&callback_, result);
}

}  // namespace

PPB_Flash_DeviceID_Proxy::PPB_Flash_DeviceID_Proxy(Dispatcher* dispatcher)
    : InterfaceProxy(dispatcher) {
}

PPB_Flash_DeviceID_Proxy::~PPB_Flash_DeviceID_Proxy() {
}

// static
PP_Resource PPB_Flash_DeviceID_Proxy::CreateProxyResource(
    PP_Instance instance) {
  return (new DeviceID(instance))->GetReference();
}

bool PPB_Flash_DeviceID_Proxy::OnMessageReceived(const IPC::Message& msg) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(PPB_Flash_DeviceID_Proxy, msg)
    IPC_MESSAGE_HANDLER(PpapiMsg_PPBFlashDeviceID_GetReply,
                        OnPluginMsgGetReply)
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()
  return handled;
}

void PPB_Flash_DeviceID_Proxy::OnPluginMsgGetReply(int32 routing_id,
                                                   PP_Resource resource,
                                                   int32 result,
                                                   const std::string& id) {
  thunk::EnterResourceNoLock<PPB_Flash_DeviceID_API> enter(resource, false);
  if (enter.failed())
    return;  // Resource destroyed.
  static_cast<DeviceID*>(enter.object())->OnReply(result, id);
}

}  // namespace proxy
}  // namespace ppapi