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#ifndef PPAPI_PROXY_DEVICE_ENUMERATION_RESOURCE_HELPER_H_
6#define PPAPI_PROXY_DEVICE_ENUMERATION_RESOURCE_HELPER_H_
7
8#include <vector>
9
10#include "base/basictypes.h"
11#include "base/memory/ref_counted.h"
12#include "base/memory/weak_ptr.h"
13#include "ppapi/c/dev/ppb_device_ref_dev.h"
14#include "ppapi/proxy/ppapi_proxy_export.h"
15#include "ppapi/shared_impl/thread_aware_callback.h"
16
17namespace IPC {
18class Message;
19}
20
21struct PP_ArrayOutput;
22
23namespace ppapi {
24
25struct DeviceRefData;
26class TrackedCallback;
27
28namespace proxy {
29
30class PluginResource;
31class ResourceMessageReplyParams;
32
33class PPAPI_PROXY_EXPORT DeviceEnumerationResourceHelper
34    : public base::SupportsWeakPtr<DeviceEnumerationResourceHelper> {
35 public:
36  // |owner| must outlive this object.
37  explicit DeviceEnumerationResourceHelper(PluginResource* owner);
38  ~DeviceEnumerationResourceHelper();
39
40  int32_t EnumerateDevices(const PP_ArrayOutput& output,
41                           scoped_refptr<TrackedCallback> callback);
42  int32_t EnumerateDevicesSync(const PP_ArrayOutput& output);
43  int32_t MonitorDeviceChange(PP_MonitorDeviceChangeCallback callback,
44                              void* user_data);
45
46  // Returns true if the message has been handled.
47  bool HandleReply(const ResourceMessageReplyParams& params,
48                   const IPC::Message& msg);
49
50  void LastPluginRefWasDeleted();
51
52 private:
53  void OnPluginMsgEnumerateDevicesReply(
54      const PP_ArrayOutput& output,
55      scoped_refptr<TrackedCallback> callback,
56      const ResourceMessageReplyParams& params,
57      const std::vector<DeviceRefData>& devices);
58  void OnPluginMsgNotifyDeviceChange(const ResourceMessageReplyParams& params,
59                                     uint32_t callback_id,
60                                     const std::vector<DeviceRefData>& devices);
61
62  int32_t WriteToArrayOutput(const std::vector<DeviceRefData>& devices,
63                             const PP_ArrayOutput& output);
64
65  // Not owned by this object.
66  PluginResource* owner_;
67
68  bool pending_enumerate_devices_;
69
70  uint32_t monitor_callback_id_;
71  scoped_ptr<ThreadAwareCallback<PP_MonitorDeviceChangeCallback> >
72      monitor_callback_;
73  void* monitor_user_data_;
74
75  DISALLOW_COPY_AND_ASSIGN(DeviceEnumerationResourceHelper);
76};
77
78}  // namespace proxy
79}  // namespace ppapi
80
81#endif  // PPAPI_PROXY_DEVICE_ENUMERATION_RESOURCE_HELPER_H_
82