Move prefs::kLastPolicyStatisticsUpdate to the policy component.
[chromium-blink-merge.git] / ppapi / shared_impl / var.cc
blob9644a44034cc07a7c0fe7276464cf5ff19757f25
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 Var::Var() : var_id_(0) {
25 Var::~Var() {
28 // static
29 std::string Var::PPVarToLogString(PP_Var var) {
30 switch (var.type) {
31 case PP_VARTYPE_UNDEFINED:
32 return "[Undefined]";
33 case PP_VARTYPE_NULL:
34 return "[Null]";
35 case PP_VARTYPE_BOOL:
36 return var.value.as_bool ? "[True]" : "[False]";
37 case PP_VARTYPE_INT32:
38 return base::IntToString(var.value.as_int);
39 case PP_VARTYPE_DOUBLE:
40 return base::DoubleToString(var.value.as_double);
41 case PP_VARTYPE_STRING: {
42 StringVar* string(StringVar::FromPPVar(var));
43 if (!string)
44 return "[Invalid string]";
46 // Since this is for logging, escape NULLs, truncate length.
47 std::string result;
48 const size_t kTruncateAboveLength = 128;
49 if (string->value().size() > kTruncateAboveLength)
50 result = string->value().substr(0, kTruncateAboveLength) + "...";
51 else
52 result = string->value();
54 std::string null;
55 null.push_back(0);
56 ReplaceSubstringsAfterOffset(&result, 0, null, "\\0");
57 return result;
59 case PP_VARTYPE_OBJECT:
60 return "[Object]";
61 case PP_VARTYPE_ARRAY:
62 return "[Array]";
63 case PP_VARTYPE_DICTIONARY:
64 return "[Dictionary]";
65 case PP_VARTYPE_ARRAY_BUFFER:
66 return "[Array buffer]";
67 case PP_VARTYPE_RESOURCE: {
68 ResourceVar* resource(ResourceVar::FromPPVar(var));
69 if (!resource)
70 return "[Invalid resource]";
72 if (resource->IsPending()) {
73 return base::StringPrintf("[Pending resource]");
74 } else if (resource->pp_resource()) {
75 return base::StringPrintf("[Resource %d]", resource->pp_resource());
76 } else {
77 return "[Null resource]";
80 default:
81 return "[Invalid var]";
85 StringVar* Var::AsStringVar() {
86 return NULL;
89 ArrayBufferVar* Var::AsArrayBufferVar() {
90 return NULL;
93 NPObjectVar* Var::AsNPObjectVar() {
94 return NULL;
97 ProxyObjectVar* Var::AsProxyObjectVar() {
98 return NULL;
101 ArrayVar* Var::AsArrayVar() {
102 return NULL;
105 DictionaryVar* Var::AsDictionaryVar() {
106 return NULL;
109 ResourceVar* Var::AsResourceVar() {
110 return NULL;
113 PP_Var Var::GetPPVar() {
114 int32 id = GetOrCreateVarID();
115 if (!id)
116 return PP_MakeNull();
118 PP_Var result;
119 result.type = GetType();
120 result.padding = 0;
121 result.value.as_id = id;
122 return result;
125 int32 Var::GetExistingVarID() const {
126 return var_id_;
129 int32 Var::GetOrCreateVarID() {
130 VarTracker* tracker = PpapiGlobals::Get()->GetVarTracker();
131 if (var_id_) {
132 if (!tracker->AddRefVar(var_id_))
133 return 0;
134 } else {
135 var_id_ = tracker->AddVar(this);
136 if (!var_id_)
137 return 0;
139 return var_id_;
142 void Var::AssignVarID(int32 id) {
143 DCHECK(!var_id_); // Must not have already been generated.
144 var_id_ = id;
147 // StringVar -------------------------------------------------------------------
149 StringVar::StringVar() {
152 StringVar::StringVar(const std::string& str)
153 : value_(str) {
156 StringVar::StringVar(const char* str, uint32 len)
157 : value_(str, len) {
160 StringVar::~StringVar() {
163 StringVar* StringVar::AsStringVar() {
164 return this;
167 PP_VarType StringVar::GetType() const {
168 return PP_VARTYPE_STRING;
171 // static
172 PP_Var StringVar::StringToPPVar(const std::string& var) {
173 return StringToPPVar(var.c_str(), static_cast<uint32>(var.size()));
176 // static
177 PP_Var StringVar::StringToPPVar(const char* data, uint32 len) {
178 scoped_refptr<StringVar> str(new StringVar(data, len));
179 if (!str.get() || !IsStringUTF8(str->value()))
180 return PP_MakeNull();
181 return str->GetPPVar();
184 // static
185 StringVar* StringVar::FromPPVar(PP_Var var) {
186 if (var.type != PP_VARTYPE_STRING)
187 return NULL;
188 scoped_refptr<Var> var_object(
189 PpapiGlobals::Get()->GetVarTracker()->GetVar(var));
190 if (!var_object.get())
191 return NULL;
192 return var_object->AsStringVar();
195 // static
196 PP_Var StringVar::SwapValidatedUTF8StringIntoPPVar(std::string* src) {
197 scoped_refptr<StringVar> str(new StringVar);
198 str->value_.swap(*src);
199 return str->GetPPVar();
202 // ArrayBufferVar --------------------------------------------------------------
204 ArrayBufferVar::ArrayBufferVar() {
207 ArrayBufferVar::~ArrayBufferVar() {
210 ArrayBufferVar* ArrayBufferVar::AsArrayBufferVar() {
211 return this;
214 PP_VarType ArrayBufferVar::GetType() const {
215 return PP_VARTYPE_ARRAY_BUFFER;
218 // static
219 ArrayBufferVar* ArrayBufferVar::FromPPVar(PP_Var var) {
220 if (var.type != PP_VARTYPE_ARRAY_BUFFER)
221 return NULL;
222 scoped_refptr<Var> var_object(
223 PpapiGlobals::Get()->GetVarTracker()->GetVar(var));
224 if (!var_object.get())
225 return NULL;
226 return var_object->AsArrayBufferVar();
229 } // namespace ppapi