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();
50 ReplaceSubstringsAfterOffset(&result
, 0, null
, "\\0");
53 case PP_VARTYPE_OBJECT
:
55 case PP_VARTYPE_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
));
64 return "[Invalid resource]";
66 if (resource
->IsPending()) {
67 return base::StringPrintf("[Pending resource]");
68 } else if (resource
->GetPPResource()) {
69 return base::StringPrintf("[Resource %d]", resource
->GetPPResource());
71 return "[Null resource]";
75 return "[Invalid var]";
79 StringVar
* Var::AsStringVar() { return NULL
; }
81 ArrayBufferVar
* Var::AsArrayBufferVar() { return NULL
; }
83 V8ObjectVar
* Var::AsV8ObjectVar() { return NULL
; }
85 ProxyObjectVar
* Var::AsProxyObjectVar() { return NULL
; }
87 ArrayVar
* Var::AsArrayVar() { return NULL
; }
89 DictionaryVar
* Var::AsDictionaryVar() { return NULL
; }
91 ResourceVar
* Var::AsResourceVar() { return NULL
; }
93 PP_Var
Var::GetPPVar() {
94 int32 id
= GetOrCreateVarID();
99 result
.type
= GetType();
101 result
.value
.as_id
= id
;
105 int32
Var::GetExistingVarID() const { return var_id_
; }
107 Var::Var() : var_id_(0) {}
111 int32
Var::GetOrCreateVarID() {
112 VarTracker
* tracker
= PpapiGlobals::Get()->GetVarTracker();
114 if (!tracker
->AddRefVar(var_id_
))
117 var_id_
= tracker
->AddVar(this);
124 void Var::AssignVarID(int32 id
) {
125 DCHECK(!var_id_
); // Must not have already been generated.
129 // StringVar -------------------------------------------------------------------
131 StringVar::StringVar() {}
133 StringVar::StringVar(const std::string
& str
) : value_(str
) {}
135 StringVar::StringVar(const char* str
, uint32 len
) : value_(str
, len
) {}
137 StringVar::~StringVar() {}
139 StringVar
* StringVar::AsStringVar() { return this; }
141 PP_VarType
StringVar::GetType() const { return PP_VARTYPE_STRING
; }
144 PP_Var
StringVar::StringToPPVar(const std::string
& var
) {
145 return StringToPPVar(var
.c_str(), static_cast<uint32
>(var
.size()));
149 PP_Var
StringVar::StringToPPVar(const char* data
, uint32 len
) {
150 scoped_refptr
<StringVar
> str(new StringVar(data
, len
));
151 if (!str
.get() || !base::IsStringUTF8(str
->value()))
152 return PP_MakeNull();
153 return str
->GetPPVar();
157 StringVar
* StringVar::FromPPVar(PP_Var var
) {
158 if (var
.type
!= PP_VARTYPE_STRING
)
160 scoped_refptr
<Var
> var_object(
161 PpapiGlobals::Get()->GetVarTracker()->GetVar(var
));
162 if (!var_object
.get())
164 return var_object
->AsStringVar();
168 PP_Var
StringVar::SwapValidatedUTF8StringIntoPPVar(std::string
* src
) {
169 scoped_refptr
<StringVar
> str(new StringVar
);
170 str
->value_
.swap(*src
);
171 return str
->GetPPVar();
174 // ArrayBufferVar --------------------------------------------------------------
176 ArrayBufferVar::ArrayBufferVar() {}
178 ArrayBufferVar::~ArrayBufferVar() {}
180 ArrayBufferVar
* ArrayBufferVar::AsArrayBufferVar() { return this; }
182 PP_VarType
ArrayBufferVar::GetType() const { return PP_VARTYPE_ARRAY_BUFFER
; }
185 ArrayBufferVar
* ArrayBufferVar::FromPPVar(PP_Var var
) {
186 if (var
.type
!= PP_VARTYPE_ARRAY_BUFFER
)
188 scoped_refptr
<Var
> var_object(
189 PpapiGlobals::Get()->GetVarTracker()->GetVar(var
));
190 if (!var_object
.get())
192 return var_object
->AsArrayBufferVar();