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.
5 #include "ppapi/shared_impl/var.h"
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"
20 // Var -------------------------------------------------------------------------
22 Var::Var() : var_id_(0) {
29 std::string
Var::PPVarToLogString(PP_Var var
) {
31 case PP_VARTYPE_UNDEFINED
:
36 return var
.value
.as_bool
? "[True]" : "[False]";
37 case PP_VARTYPE_INT32
:
38 return base::IntToString(var
.value
.as_int
);
39 case PP_VARTYPE_DOUBLE
:
40 return base::DoubleToString(var
.value
.as_double
);
41 case PP_VARTYPE_STRING
: {
42 StringVar
* string(StringVar::FromPPVar(var
));
44 return "[Invalid string]";
46 // Since this is for logging, escape NULLs, truncate length.
48 const size_t kTruncateAboveLength
= 128;
49 if (string
->value().size() > kTruncateAboveLength
)
50 result
= string
->value().substr(0, kTruncateAboveLength
) + "...";
52 result
= string
->value();
56 ReplaceSubstringsAfterOffset(&result
, 0, null
, "\\0");
59 case PP_VARTYPE_OBJECT
:
61 case PP_VARTYPE_ARRAY
:
63 case PP_VARTYPE_DICTIONARY
:
64 return "[Dictionary]";
65 case PP_VARTYPE_ARRAY_BUFFER
:
66 return "[Array buffer]";
67 case PP_VARTYPE_RESOURCE
: {
68 ResourceVar
* resource(ResourceVar::FromPPVar(var
));
70 return "[Invalid resource]";
72 if (resource
->IsPending()) {
73 return base::StringPrintf("[Pending resource]");
74 } else if (resource
->pp_resource()) {
75 return base::StringPrintf("[Resource %d]", resource
->pp_resource());
77 return "[Null resource]";
81 return "[Invalid var]";
85 StringVar
* Var::AsStringVar() {
89 ArrayBufferVar
* Var::AsArrayBufferVar() {
93 NPObjectVar
* Var::AsNPObjectVar() {
97 ProxyObjectVar
* Var::AsProxyObjectVar() {
101 ArrayVar
* Var::AsArrayVar() {
105 DictionaryVar
* Var::AsDictionaryVar() {
109 ResourceVar
* Var::AsResourceVar() {
113 PP_Var
Var::GetPPVar() {
114 int32 id
= GetOrCreateVarID();
116 return PP_MakeNull();
119 result
.type
= GetType();
121 result
.value
.as_id
= id
;
125 int32
Var::GetExistingVarID() const {
129 int32
Var::GetOrCreateVarID() {
130 VarTracker
* tracker
= PpapiGlobals::Get()->GetVarTracker();
132 if (!tracker
->AddRefVar(var_id_
))
135 var_id_
= tracker
->AddVar(this);
142 void Var::AssignVarID(int32 id
) {
143 DCHECK(!var_id_
); // Must not have already been generated.
147 // StringVar -------------------------------------------------------------------
149 StringVar::StringVar() {
152 StringVar::StringVar(const std::string
& str
)
156 StringVar::StringVar(const char* str
, uint32 len
)
160 StringVar::~StringVar() {
163 StringVar
* StringVar::AsStringVar() {
167 PP_VarType
StringVar::GetType() const {
168 return PP_VARTYPE_STRING
;
172 PP_Var
StringVar::StringToPPVar(const std::string
& var
) {
173 return StringToPPVar(var
.c_str(), static_cast<uint32
>(var
.size()));
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();
185 StringVar
* StringVar::FromPPVar(PP_Var var
) {
186 if (var
.type
!= PP_VARTYPE_STRING
)
188 scoped_refptr
<Var
> var_object(
189 PpapiGlobals::Get()->GetVarTracker()->GetVar(var
));
190 if (!var_object
.get())
192 return var_object
->AsStringVar();
196 PP_Var
StringVar::SwapValidatedUTF8StringIntoPPVar(std::string
* src
) {
197 scoped_refptr
<StringVar
> str(new StringVar
);
198 str
->value_
.swap(*src
);
199 return str
->GetPPVar();
202 // ArrayBufferVar --------------------------------------------------------------
204 ArrayBufferVar::ArrayBufferVar() {
207 ArrayBufferVar::~ArrayBufferVar() {
210 ArrayBufferVar
* ArrayBufferVar::AsArrayBufferVar() {
214 PP_VarType
ArrayBufferVar::GetType() const {
215 return PP_VARTYPE_ARRAY_BUFFER
;
219 ArrayBufferVar
* ArrayBufferVar::FromPPVar(PP_Var var
) {
220 if (var
.type
!= PP_VARTYPE_ARRAY_BUFFER
)
222 scoped_refptr
<Var
> var_object(
223 PpapiGlobals::Get()->GetVarTracker()->GetVar(var
));
224 if (!var_object
.get())
226 return var_object
->AsArrayBufferVar();