Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ppapi / shared_impl / var.cc
blob195a5544dfe0553df92039c5e9b69baa390b44c2
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"
7 #include <limits>
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"
18 namespace ppapi {
20 // Var -------------------------------------------------------------------------
22 // static
23 std::string Var::PPVarToLogString(PP_Var var) {
24 switch (var.type) {
25 case PP_VARTYPE_UNDEFINED:
26 return "[Undefined]";
27 case PP_VARTYPE_NULL:
28 return "[Null]";
29 case PP_VARTYPE_BOOL:
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));
37 if (!string)
38 return "[Invalid string]";
40 // Since this is for logging, escape NULLs, truncate length.
41 std::string result;
42 const size_t kTruncateAboveLength = 128;
43 if (string->value().size() > kTruncateAboveLength)
44 result = string->value().substr(0, kTruncateAboveLength) + "...";
45 else
46 result = string->value();
48 std::string null;
49 null.push_back(0);
50 ReplaceSubstringsAfterOffset(&result, 0, null, "\\0");
51 return result;
53 case PP_VARTYPE_OBJECT:
54 return "[Object]";
55 case PP_VARTYPE_ARRAY:
56 return "[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));
63 if (!resource)
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());
70 } else {
71 return "[Null resource]";
74 default:
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();
97 if (!id)
98 return PP_MakeNull();
100 PP_Var result;
101 result.type = GetType();
102 result.padding = 0;
103 result.value.as_id = id;
104 return result;
107 int32 Var::GetExistingVarID() const { return var_id_; }
109 Var::Var() : var_id_(0) {}
111 Var::~Var() {}
113 int32 Var::GetOrCreateVarID() {
114 VarTracker* tracker = PpapiGlobals::Get()->GetVarTracker();
115 if (var_id_) {
116 if (!tracker->AddRefVar(var_id_))
117 return 0;
118 } else {
119 var_id_ = tracker->AddVar(this);
120 if (!var_id_)
121 return 0;
123 return var_id_;
126 void Var::AssignVarID(int32 id) {
127 DCHECK(!var_id_); // Must not have already been generated.
128 var_id_ = id;
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; }
145 // static
146 PP_Var StringVar::StringToPPVar(const std::string& var) {
147 return StringToPPVar(var.c_str(), static_cast<uint32>(var.size()));
150 // static
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();
158 // static
159 StringVar* StringVar::FromPPVar(PP_Var var) {
160 if (var.type != PP_VARTYPE_STRING)
161 return NULL;
162 scoped_refptr<Var> var_object(
163 PpapiGlobals::Get()->GetVarTracker()->GetVar(var));
164 if (!var_object.get())
165 return NULL;
166 return var_object->AsStringVar();
169 // static
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; }
186 // static
187 ArrayBufferVar* ArrayBufferVar::FromPPVar(PP_Var var) {
188 if (var.type != PP_VARTYPE_ARRAY_BUFFER)
189 return NULL;
190 scoped_refptr<Var> var_object(
191 PpapiGlobals::Get()->GetVarTracker()->GetVar(var));
192 if (!var_object.get())
193 return NULL;
194 return var_object->AsArrayBufferVar();
197 } // namespace ppapi