• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/shared_impl/ppb_var_shared.h"
6 
7 #include <limits>
8 
9 #include "ppapi/c/ppb_var.h"
10 #include "ppapi/c/ppb_var_array_buffer.h"
11 #include "ppapi/c/pp_var.h"
12 #include "ppapi/shared_impl/ppapi_globals.h"
13 #include "ppapi/shared_impl/proxy_lock.h"
14 #include "ppapi/shared_impl/resource_tracker.h"
15 #include "ppapi/shared_impl/resource_var.h"
16 #include "ppapi/shared_impl/var.h"
17 #include "ppapi/shared_impl/var_tracker.h"
18 
19 using ppapi::PpapiGlobals;
20 using ppapi::StringVar;
21 
22 namespace ppapi {
23 namespace {
24 
25 // PPB_Var methods -------------------------------------------------------------
26 
AddRefVar(PP_Var var)27 void AddRefVar(PP_Var var) {
28   ProxyAutoLock lock;
29   PpapiGlobals::Get()->GetVarTracker()->AddRefVar(var);
30 }
31 
ReleaseVar(PP_Var var)32 void ReleaseVar(PP_Var var) {
33   ProxyAutoLock lock;
34   PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(var);
35 }
36 
VarFromUtf8(const char * data,uint32_t len)37 PP_Var VarFromUtf8(const char* data, uint32_t len) {
38   ProxyAutoLock lock;
39   return StringVar::StringToPPVar(data, len);
40 }
41 
VarFromUtf8_1_0(PP_Module,const char * data,uint32_t len)42 PP_Var VarFromUtf8_1_0(PP_Module /*module*/, const char* data, uint32_t len) {
43   return VarFromUtf8(data, len);
44 }
45 
VarToUtf8(PP_Var var,uint32_t * len)46 const char* VarToUtf8(PP_Var var, uint32_t* len) {
47   ProxyAutoLock lock;
48   StringVar* str = StringVar::FromPPVar(var);
49   if (str) {
50     *len = static_cast<uint32_t>(str->value().size());
51     return str->value().c_str();
52   }
53   *len = 0;
54   return NULL;
55 }
56 
VarToResource(PP_Var var)57 PP_Resource VarToResource(PP_Var var) {
58   ProxyAutoLock lock;
59   ResourceVar* resource = ResourceVar::FromPPVar(var);
60   if (!resource)
61     return 0;
62   PP_Resource pp_resource = resource->GetPPResource();
63   PpapiGlobals::Get()->GetResourceTracker()->AddRefResource(pp_resource);
64   return pp_resource;
65 }
66 
VarFromResource(PP_Resource resource)67 PP_Var VarFromResource(PP_Resource resource) {
68   ProxyAutoLock lock;
69   return PpapiGlobals::Get()->GetVarTracker()->MakeResourcePPVar(resource);
70 }
71 
72 const PPB_Var var_interface = {&AddRefVar, &ReleaseVar,    &VarFromUtf8,
73                                &VarToUtf8, &VarToResource, &VarFromResource};
74 
75 const PPB_Var_1_1 var_interface1_1 = {&AddRefVar,   &ReleaseVar,
76                                       &VarFromUtf8, &VarToUtf8};
77 
78 const PPB_Var_1_0 var_interface1_0 = {&AddRefVar,       &ReleaseVar,
79                                       &VarFromUtf8_1_0, &VarToUtf8};
80 
81 // PPB_VarArrayBuffer methods --------------------------------------------------
82 
CreateArrayBufferVar(uint32_t size_in_bytes)83 PP_Var CreateArrayBufferVar(uint32_t size_in_bytes) {
84   ProxyAutoLock lock;
85   return PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferPPVar(
86       size_in_bytes);
87 }
88 
ByteLength(PP_Var array,uint32_t * byte_length)89 PP_Bool ByteLength(PP_Var array, uint32_t* byte_length) {
90   ProxyAutoLock lock;
91   ArrayBufferVar* buffer = ArrayBufferVar::FromPPVar(array);
92   if (!buffer)
93     return PP_FALSE;
94   *byte_length = buffer->ByteLength();
95   return PP_TRUE;
96 }
97 
Map(PP_Var array)98 void* Map(PP_Var array) {
99   ProxyAutoLock lock;
100   ArrayBufferVar* buffer = ArrayBufferVar::FromPPVar(array);
101   if (!buffer)
102     return NULL;
103   return buffer->Map();
104 }
105 
Unmap(PP_Var array)106 void Unmap(PP_Var array) {
107   ProxyAutoLock lock;
108   ArrayBufferVar* buffer = ArrayBufferVar::FromPPVar(array);
109   if (buffer)
110     buffer->Unmap();
111 }
112 
113 const PPB_VarArrayBuffer_1_0 var_arraybuffer_interface = {
114     &CreateArrayBufferVar, &ByteLength, &Map, &Unmap};
115 
116 }  // namespace
117 
118 // static
GetVarInterface1_2()119 const PPB_Var_1_2* PPB_Var_Shared::GetVarInterface1_2() {
120   return &var_interface;
121 }
122 
123 // static
GetVarInterface1_1()124 const PPB_Var_1_1* PPB_Var_Shared::GetVarInterface1_1() {
125   return &var_interface1_1;
126 }
127 
128 // static
GetVarInterface1_0()129 const PPB_Var_1_0* PPB_Var_Shared::GetVarInterface1_0() {
130   return &var_interface1_0;
131 }
132 
133 // static
GetVarArrayBufferInterface1_0()134 const PPB_VarArrayBuffer_1_0* PPB_Var_Shared::GetVarArrayBufferInterface1_0() {
135   return &var_arraybuffer_interface;
136 }
137 
138 }  // namespace ppapi
139