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/string_number_conversions.h"
11 #include "base/string_util.h"
12 #include "ppapi/c/pp_var.h"
13 #include "ppapi/shared_impl/ppapi_globals.h"
14 #include "ppapi/shared_impl/var_tracker.h"
18 // Var -------------------------------------------------------------------------
20 Var::Var() : var_id_(0) {
27 std::string
Var::PPVarToLogString(PP_Var var
) {
29 case PP_VARTYPE_UNDEFINED
:
34 return var
.value
.as_bool
? "[True]" : "[False]";
35 case PP_VARTYPE_INT32
:
36 return base::IntToString(var
.value
.as_int
);
37 case PP_VARTYPE_DOUBLE
:
38 return base::DoubleToString(var
.value
.as_double
);
39 case PP_VARTYPE_STRING
: {
40 StringVar
* string(StringVar::FromPPVar(var
));
42 return "[Invalid string]";
44 // Since this is for logging, escape NULLs, truncate length.
46 const size_t kTruncateAboveLength
= 128;
47 if (string
->value().size() > kTruncateAboveLength
)
48 result
= string
->value().substr(0, kTruncateAboveLength
) + "...";
50 result
= string
->value();
54 ReplaceSubstringsAfterOffset(&result
, 0, null
, "\\0");
57 case PP_VARTYPE_OBJECT
:
59 case PP_VARTYPE_ARRAY
:
61 case PP_VARTYPE_DICTIONARY
:
62 return "[Dictionary]";
63 case PP_VARTYPE_ARRAY_BUFFER
:
64 return "[Array buffer]";
66 return "[Invalid var]";
70 StringVar
* Var::AsStringVar() {
74 ArrayBufferVar
* Var::AsArrayBufferVar() {
78 NPObjectVar
* Var::AsNPObjectVar() {
82 ProxyObjectVar
* Var::AsProxyObjectVar() {
86 ArrayVar
* Var::AsArrayVar() {
90 DictionaryVar
* Var::AsDictionaryVar() {
94 PP_Var
Var::GetPPVar() {
95 int32 id
= GetOrCreateVarID();
100 result
.type
= GetType();
102 result
.value
.as_id
= id
;
106 int32
Var::GetExistingVarID() const {
110 int32
Var::GetOrCreateVarID() {
111 VarTracker
* tracker
= PpapiGlobals::Get()->GetVarTracker();
113 if (!tracker
->AddRefVar(var_id_
))
116 var_id_
= tracker
->AddVar(this);
123 void Var::AssignVarID(int32 id
) {
124 DCHECK(!var_id_
); // Must not have already been generated.
128 // StringVar -------------------------------------------------------------------
130 StringVar::StringVar() {
133 StringVar::StringVar(const std::string
& str
)
137 StringVar::StringVar(const char* str
, uint32 len
)
141 StringVar::~StringVar() {
144 StringVar
* StringVar::AsStringVar() {
148 PP_VarType
StringVar::GetType() const {
149 return PP_VARTYPE_STRING
;
153 PP_Var
StringVar::StringToPPVar(const std::string
& var
) {
154 return StringToPPVar(var
.c_str(), static_cast<uint32
>(var
.size()));
158 PP_Var
StringVar::StringToPPVar(const char* data
, uint32 len
) {
159 scoped_refptr
<StringVar
> str(new StringVar(data
, len
));
160 if (!str
|| !IsStringUTF8(str
->value()))
161 return PP_MakeNull();
162 return str
->GetPPVar();
166 StringVar
* StringVar::FromPPVar(PP_Var var
) {
167 if (var
.type
!= PP_VARTYPE_STRING
)
169 scoped_refptr
<Var
> var_object(
170 PpapiGlobals::Get()->GetVarTracker()->GetVar(var
));
173 return var_object
->AsStringVar();
177 PP_Var
StringVar::SwapValidatedUTF8StringIntoPPVar(std::string
* src
) {
178 scoped_refptr
<StringVar
> str(new StringVar
);
179 str
->value_
.swap(*src
);
180 return str
->GetPPVar();
183 // ArrayBufferVar --------------------------------------------------------------
185 ArrayBufferVar::ArrayBufferVar() {
188 ArrayBufferVar::~ArrayBufferVar() {
191 ArrayBufferVar
* ArrayBufferVar::AsArrayBufferVar() {
195 PP_VarType
ArrayBufferVar::GetType() const {
196 return PP_VARTYPE_ARRAY_BUFFER
;
200 ArrayBufferVar
* ArrayBufferVar::FromPPVar(PP_Var var
) {
201 if (var
.type
!= PP_VARTYPE_ARRAY_BUFFER
)
203 scoped_refptr
<Var
> var_object(
204 PpapiGlobals::Get()->GetVarTracker()->GetVar(var
));
207 return var_object
->AsArrayBufferVar();