summaryrefslogtreecommitdiffstats
path: root/ppapi/shared_impl/ppb_device_ref_shared.cc
blob: 73a9a7c7d105db068c804a61c74678bc0972cfca (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
// 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/shared_impl/ppb_device_ref_shared.h"

#include "base/memory/scoped_ptr.h"
#include "ppapi/shared_impl/host_resource.h"
#include "ppapi/shared_impl/ppapi_globals.h"
#include "ppapi/shared_impl/ppb_resource_array_shared.h"
#include "ppapi/shared_impl/resource_tracker.h"
#include "ppapi/shared_impl/var.h"

using ppapi::thunk::PPB_DeviceRef_API;

namespace ppapi {

DeviceRefData::DeviceRefData()
    : type(PP_DEVICETYPE_DEV_INVALID) {
}

PPB_DeviceRef_Shared::PPB_DeviceRef_Shared(ResourceObjectType type,
                                           PP_Instance instance,
                                           const DeviceRefData& data)
    : Resource(type, instance),
      data_(data) {
}

PPB_DeviceRef_API* PPB_DeviceRef_Shared::AsPPB_DeviceRef_API() {
  return this;
}

const DeviceRefData& PPB_DeviceRef_Shared::GetDeviceRefData() const {
  return data_;
}

PP_DeviceType_Dev PPB_DeviceRef_Shared::GetType() {
  return data_.type;
}

PP_Var PPB_DeviceRef_Shared::GetName() {
  return StringVar::StringToPPVar(data_.name);
}

// static
PP_Resource PPB_DeviceRef_Shared::CreateResourceArray(
    ResourceObjectType type,
    PP_Instance instance,
    const std::vector<DeviceRefData>& devices) {
  scoped_array<PP_Resource> elements;
  size_t size = devices.size();
  if (size > 0) {
    elements.reset(new PP_Resource[size]);
    for (size_t index = 0; index < size; ++index) {
      PPB_DeviceRef_Shared* device_object =
          new PPB_DeviceRef_Shared(type, instance, devices[index]);
      elements[index] = device_object->GetReference();
    }
  }
  PPB_ResourceArray_Shared* array_object =
      new PPB_ResourceArray_Shared(type, instance, elements.get(),
                                   static_cast<uint32_t>(size));

  for (size_t index = 0; index < size; ++index)
    PpapiGlobals::Get()->GetResourceTracker()->ReleaseResource(elements[index]);

  return array_object->GetReference();
}

}  // namespace ppapi