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 -------------------------------------------------------------------------
23 std::string
Var::PPVarToLogString(PP_Var var
) {
25 case PP_VARTYPE_UNDEFINED
:
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
));
38 return "[Invalid string]";
40 // Since this is for logging, escape NULLs, truncate length.
42 const size_t kTruncateAboveLength
= 128;
43 if (string
->value().size() > kTruncateAboveLength
)
44 result
= string
->value().substr(0, kTruncateAboveLength
) + "...";
46 result
= string
->value();
48 base::ReplaceSubstringsAfterOffset(
49 &result
, 0, base::StringPiece("\0", 1), "\\0");
52 case PP_VARTYPE_OBJECT
:
54 case PP_VARTYPE_ARRAY
:
56 case PP_VARTYPE_DICTIONARY
:
57 return "[Dictionary]";
58 case PP_VARTYPE_ARRAY_BUFFER
:
59 return "[Array buffer]";
60 case PP_VARTYPE_RESOURCE
: {
61 ResourceVar
* resource(ResourceVar::FromPPVar(var
));
63 return "[Invalid resource]";
65 if (resource
->IsPending()) {
66 return base::StringPrintf("[Pending resource]");
67 } else if (resource
->GetPPResource()) {
68 return base::StringPrintf("[Resource %d]", resource
->GetPPResource());
70 return "[Null resource]";
74 return "[Invalid var]";
78 StringVar
* Var::AsStringVar() { return NULL
; }
80 ArrayBufferVar
* Var::AsArrayBufferVar() { return NULL
; }
82 V8ObjectVar
* Var::AsV8ObjectVar() { return NULL
; }
84 ProxyObjectVar
* Var::AsProxyObjectVar() { return NULL
; }
86 ArrayVar
* Var::AsArrayVar() { return NULL
; }
88 DictionaryVar
* Var::AsDictionaryVar() { return NULL
; }
90 ResourceVar
* Var::AsResourceVar() { return NULL
; }
92 PP_Var
Var::GetPPVar() {
93 int32 id
= GetOrCreateVarID();
98 result
.type
= GetType();
100 result
.value
.as_id
= id
;
104 int32
Var::GetExistingVarID() const { return var_id_
; }
106 Var::Var() : var_id_(0) {}
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() {}
132 StringVar::StringVar(const std::string
& str
) : value_(str
) {}
134 StringVar::StringVar(const char* str
, uint32 len
) : value_(str
, len
) {}
136 StringVar::~StringVar() {}
138 StringVar
* StringVar::AsStringVar() { return this; }
140 PP_VarType
StringVar::GetType() const { return PP_VARTYPE_STRING
; }
143 PP_Var
StringVar::StringToPPVar(const std::string
& var
) {
144 return StringToPPVar(var
.c_str(), static_cast<uint32
>(var
.size()));
148 PP_Var
StringVar::StringToPPVar(const char* data
, uint32 len
) {
149 scoped_refptr
<StringVar
> str(new StringVar(data
, len
));
150 if (!str
.get() || !base::IsStringUTF8(str
->value()))
151 return PP_MakeNull();
152 return str
->GetPPVar();
156 StringVar
* StringVar::FromPPVar(PP_Var var
) {
157 if (var
.type
!= PP_VARTYPE_STRING
)
159 scoped_refptr
<Var
> var_object(
160 PpapiGlobals::Get()->GetVarTracker()->GetVar(var
));
161 if (!var_object
.get())
163 return var_object
->AsStringVar();
167 PP_Var
StringVar::SwapValidatedUTF8StringIntoPPVar(std::string
* src
) {
168 scoped_refptr
<StringVar
> str(new StringVar
);
169 str
->value_
.swap(*src
);
170 return str
->GetPPVar();
173 // ArrayBufferVar --------------------------------------------------------------
175 ArrayBufferVar::ArrayBufferVar() {}
177 ArrayBufferVar::~ArrayBufferVar() {}
179 ArrayBufferVar
* ArrayBufferVar::AsArrayBufferVar() { return this; }
181 PP_VarType
ArrayBufferVar::GetType() const { return PP_VARTYPE_ARRAY_BUFFER
; }
184 ArrayBufferVar
* ArrayBufferVar::FromPPVar(PP_Var var
) {
185 if (var
.type
!= PP_VARTYPE_ARRAY_BUFFER
)
187 scoped_refptr
<Var
> var_object(
188 PpapiGlobals::Get()->GetVarTracker()->GetVar(var
));
189 if (!var_object
.get())
191 return var_object
->AsArrayBufferVar();