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 NPObjectVar
* Var::AsNPObjectVar() { return NULL
; }
85 V8ObjectVar
* Var::AsV8ObjectVar() { return NULL
; }
87 ProxyObjectVar
* Var::AsProxyObjectVar() { return NULL
; }
89 ArrayVar
* Var::AsArrayVar() { return NULL
; }
91 DictionaryVar
* Var::AsDictionaryVar() { return NULL
; }
93 ResourceVar
* Var::AsResourceVar() { return NULL
; }
95 PP_Var
Var::GetPPVar() {
96 int32 id
= GetOrCreateVarID();
101 result
.type
= GetType();
103 result
.value
.as_id
= id
;
107 int32
Var::GetExistingVarID() const { return var_id_
; }
109 Var::Var() : var_id_(0) {}
113 int32
Var::GetOrCreateVarID() {
114 VarTracker
* tracker
= PpapiGlobals::Get()->GetVarTracker();
116 if (!tracker
->AddRefVar(var_id_
))
119 var_id_
= tracker
->AddVar(this);
126 void Var::AssignVarID(int32 id
) {
127 DCHECK(!var_id_
); // Must not have already been generated.
131 // StringVar -------------------------------------------------------------------
133 StringVar::StringVar() {}
135 StringVar::StringVar(const std::string
& str
) : value_(str
) {}
137 StringVar::StringVar(const char* str
, uint32 len
) : value_(str
, len
) {}
139 StringVar::~StringVar() {}
141 StringVar
* StringVar::AsStringVar() { return this; }
143 PP_VarType
StringVar::GetType() const { return PP_VARTYPE_STRING
; }
146 PP_Var
StringVar::StringToPPVar(const std::string
& var
) {
147 return StringToPPVar(var
.c_str(), static_cast<uint32
>(var
.size()));
151 PP_Var
StringVar::StringToPPVar(const char* data
, uint32 len
) {
152 scoped_refptr
<StringVar
> str(new StringVar(data
, len
));
153 if (!str
.get() || !base::IsStringUTF8(str
->value()))
154 return PP_MakeNull();
155 return str
->GetPPVar();
159 StringVar
* StringVar::FromPPVar(PP_Var var
) {
160 if (var
.type
!= PP_VARTYPE_STRING
)
162 scoped_refptr
<Var
> var_object(
163 PpapiGlobals::Get()->GetVarTracker()->GetVar(var
));
164 if (!var_object
.get())
166 return var_object
->AsStringVar();
170 PP_Var
StringVar::SwapValidatedUTF8StringIntoPPVar(std::string
* src
) {
171 scoped_refptr
<StringVar
> str(new StringVar
);
172 str
->value_
.swap(*src
);
173 return str
->GetPPVar();
176 // ArrayBufferVar --------------------------------------------------------------
178 ArrayBufferVar::ArrayBufferVar() {}
180 ArrayBufferVar::~ArrayBufferVar() {}
182 ArrayBufferVar
* ArrayBufferVar::AsArrayBufferVar() { return this; }
184 PP_VarType
ArrayBufferVar::GetType() const { return PP_VARTYPE_ARRAY_BUFFER
; }
187 ArrayBufferVar
* ArrayBufferVar::FromPPVar(PP_Var var
) {
188 if (var
.type
!= PP_VARTYPE_ARRAY_BUFFER
)
190 scoped_refptr
<Var
> var_object(
191 PpapiGlobals::Get()->GetVarTracker()->GetVar(var
));
192 if (!var_object
.get())
194 return var_object
->AsArrayBufferVar();