• 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/var.h"
6 
7 #include <limits>
8 
9 #include "base/logging.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/stringprintf.h"
13 #include "ppapi/c/pp_var.h"
14 #include "ppapi/shared_impl/ppapi_globals.h"
15 #include "ppapi/shared_impl/resource_var.h"
16 #include "ppapi/shared_impl/var_tracker.h"
17 
18 namespace ppapi {
19 
20 // Var -------------------------------------------------------------------------
21 
22 // static
PPVarToLogString(PP_Var var)23 std::string Var::PPVarToLogString(PP_Var var) {
24   switch (var.type) {
25     case PP_VARTYPE_UNDEFINED:
26       return "[Undefined]";
27     case PP_VARTYPE_NULL:
28       return "[Null]";
29     case PP_VARTYPE_BOOL:
30       return var.value.as_bool ? "[True]" : "[False]";
31     case PP_VARTYPE_INT32:
32       return base::IntToString(var.value.as_int);
33     case PP_VARTYPE_DOUBLE:
34       return base::DoubleToString(var.value.as_double);
35     case PP_VARTYPE_STRING: {
36       StringVar* string(StringVar::FromPPVar(var));
37       if (!string)
38         return "[Invalid string]";
39 
40       // Since this is for logging, escape NULLs, truncate length.
41       std::string result;
42       const size_t kTruncateAboveLength = 128;
43       if (string->value().size() > kTruncateAboveLength)
44         result = string->value().substr(0, kTruncateAboveLength) + "...";
45       else
46         result = string->value();
47 
48       std::string null;
49       null.push_back(0);
50       ReplaceSubstringsAfterOffset(&result, 0, null, "\\0");
51       return result;
52     }
53     case PP_VARTYPE_OBJECT:
54       return "[Object]";
55     case PP_VARTYPE_ARRAY:
56       return "[Array]";
57     case PP_VARTYPE_DICTIONARY:
58       return "[Dictionary]";
59     case PP_VARTYPE_ARRAY_BUFFER:
60       return "[Array buffer]";
61     case PP_VARTYPE_RESOURCE: {
62       ResourceVar* resource(ResourceVar::FromPPVar(var));
63       if (!resource)
64         return "[Invalid resource]";
65 
66       if (resource->IsPending()) {
67         return base::StringPrintf("[Pending resource]");
68       } else if (resource->GetPPResource()) {
69         return base::StringPrintf("[Resource %d]", resource->GetPPResource());
70       } else {
71         return "[Null resource]";
72       }
73     }
74     default:
75       return "[Invalid var]";
76   }
77 }
78 
AsStringVar()79 StringVar* Var::AsStringVar() {
80   return NULL;
81 }
82 
AsArrayBufferVar()83 ArrayBufferVar* Var::AsArrayBufferVar() {
84   return NULL;
85 }
86 
AsNPObjectVar()87 NPObjectVar* Var::AsNPObjectVar() {
88   return NULL;
89 }
90 
AsProxyObjectVar()91 ProxyObjectVar* Var::AsProxyObjectVar() {
92   return NULL;
93 }
94 
AsArrayVar()95 ArrayVar* Var::AsArrayVar() {
96   return NULL;
97 }
98 
AsDictionaryVar()99 DictionaryVar* Var::AsDictionaryVar() {
100   return NULL;
101 }
102 
AsResourceVar()103 ResourceVar* Var::AsResourceVar() {
104   return NULL;
105 }
106 
GetPPVar()107 PP_Var Var::GetPPVar() {
108   int32 id = GetOrCreateVarID();
109   if (!id)
110     return PP_MakeNull();
111 
112   PP_Var result;
113   result.type = GetType();
114   result.padding = 0;
115   result.value.as_id = id;
116   return result;
117 }
118 
GetExistingVarID() const119 int32 Var::GetExistingVarID() const {
120   return var_id_;
121 }
122 
Var()123 Var::Var() : var_id_(0) {
124 }
125 
~Var()126 Var::~Var() {
127 }
128 
GetOrCreateVarID()129 int32 Var::GetOrCreateVarID() {
130   VarTracker* tracker = PpapiGlobals::Get()->GetVarTracker();
131   if (var_id_) {
132     if (!tracker->AddRefVar(var_id_))
133       return 0;
134   } else {
135     var_id_ = tracker->AddVar(this);
136     if (!var_id_)
137       return 0;
138   }
139   return var_id_;
140 }
141 
AssignVarID(int32 id)142 void Var::AssignVarID(int32 id) {
143   DCHECK(!var_id_);  // Must not have already been generated.
144   var_id_ = id;
145 }
146 
147 // StringVar -------------------------------------------------------------------
148 
StringVar()149 StringVar::StringVar() {
150 }
151 
StringVar(const std::string & str)152 StringVar::StringVar(const std::string& str)
153     : value_(str) {
154 }
155 
StringVar(const char * str,uint32 len)156 StringVar::StringVar(const char* str, uint32 len)
157     : value_(str, len) {
158 }
159 
~StringVar()160 StringVar::~StringVar() {
161 }
162 
AsStringVar()163 StringVar* StringVar::AsStringVar() {
164   return this;
165 }
166 
GetType() const167 PP_VarType StringVar::GetType() const {
168   return PP_VARTYPE_STRING;
169 }
170 
171 // static
StringToPPVar(const std::string & var)172 PP_Var StringVar::StringToPPVar(const std::string& var) {
173   return StringToPPVar(var.c_str(), static_cast<uint32>(var.size()));
174 }
175 
176 // static
StringToPPVar(const char * data,uint32 len)177 PP_Var StringVar::StringToPPVar(const char* data, uint32 len) {
178   scoped_refptr<StringVar> str(new StringVar(data, len));
179   if (!str.get() || !IsStringUTF8(str->value()))
180     return PP_MakeNull();
181   return str->GetPPVar();
182 }
183 
184 // static
FromPPVar(PP_Var var)185 StringVar* StringVar::FromPPVar(PP_Var var) {
186   if (var.type != PP_VARTYPE_STRING)
187     return NULL;
188   scoped_refptr<Var> var_object(
189       PpapiGlobals::Get()->GetVarTracker()->GetVar(var));
190   if (!var_object.get())
191     return NULL;
192   return var_object->AsStringVar();
193 }
194 
195 // static
SwapValidatedUTF8StringIntoPPVar(std::string * src)196 PP_Var StringVar::SwapValidatedUTF8StringIntoPPVar(std::string* src) {
197   scoped_refptr<StringVar> str(new StringVar);
198   str->value_.swap(*src);
199   return str->GetPPVar();
200 }
201 
202 // ArrayBufferVar --------------------------------------------------------------
203 
ArrayBufferVar()204 ArrayBufferVar::ArrayBufferVar() {
205 }
206 
~ArrayBufferVar()207 ArrayBufferVar::~ArrayBufferVar() {
208 }
209 
AsArrayBufferVar()210 ArrayBufferVar* ArrayBufferVar::AsArrayBufferVar() {
211   return this;
212 }
213 
GetType() const214 PP_VarType ArrayBufferVar::GetType() const {
215   return PP_VARTYPE_ARRAY_BUFFER;
216 }
217 
218 // static
FromPPVar(PP_Var var)219 ArrayBufferVar* ArrayBufferVar::FromPPVar(PP_Var var) {
220   if (var.type != PP_VARTYPE_ARRAY_BUFFER)
221     return NULL;
222   scoped_refptr<Var> var_object(
223       PpapiGlobals::Get()->GetVarTracker()->GetVar(var));
224   if (!var_object.get())
225     return NULL;
226   return var_object->AsArrayBufferVar();
227 }
228 
229 }  // namespace ppapi
230 
231