Vectorize website settings icons in omnibox
[chromium-blink-merge.git] / ppapi / shared_impl / var.cc
blob98c762aa69133a4404068222bcfa1e4b8016a47a
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 base::ReplaceSubstringsAfterOffset(
49 &result, 0, base::StringPiece("\0", 1), "\\0");
50 return result;
52 case PP_VARTYPE_OBJECT:
53 return "[Object]";
54 case PP_VARTYPE_ARRAY:
55 return "[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));
62 if (!resource)
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());
69 } else {
70 return "[Null resource]";
73 default:
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();
94 if (!id)
95 return PP_MakeNull();
97 PP_Var result;
98 result.type = GetType();
99 result.padding = 0;
100 result.value.as_id = id;
101 return result;
104 int32 Var::GetExistingVarID() const { return var_id_; }
106 Var::Var() : var_id_(0) {}
108 Var::~Var() {}
110 int32 Var::GetOrCreateVarID() {
111 VarTracker* tracker = PpapiGlobals::Get()->GetVarTracker();
112 if (var_id_) {
113 if (!tracker->AddRefVar(var_id_))
114 return 0;
115 } else {
116 var_id_ = tracker->AddVar(this);
117 if (!var_id_)
118 return 0;
120 return var_id_;
123 void Var::AssignVarID(int32 id) {
124 DCHECK(!var_id_); // Must not have already been generated.
125 var_id_ = id;
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; }
142 // static
143 PP_Var StringVar::StringToPPVar(const std::string& var) {
144 return StringToPPVar(var.c_str(), static_cast<uint32>(var.size()));
147 // static
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();
155 // static
156 StringVar* StringVar::FromPPVar(PP_Var var) {
157 if (var.type != PP_VARTYPE_STRING)
158 return NULL;
159 scoped_refptr<Var> var_object(
160 PpapiGlobals::Get()->GetVarTracker()->GetVar(var));
161 if (!var_object.get())
162 return NULL;
163 return var_object->AsStringVar();
166 // static
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; }
183 // static
184 ArrayBufferVar* ArrayBufferVar::FromPPVar(PP_Var var) {
185 if (var.type != PP_VARTYPE_ARRAY_BUFFER)
186 return NULL;
187 scoped_refptr<Var> var_object(
188 PpapiGlobals::Get()->GetVarTracker()->GetVar(var));
189 if (!var_object.get())
190 return NULL;
191 return var_object->AsArrayBufferVar();
194 } // namespace ppapi