Make certificate viewer a tab-modal dialog.
[chromium-blink-merge.git] / ppapi / shared_impl / var.cc
blob0aff5837fe9a4a383061d9558c5c702bcebbbcc2
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/string_number_conversions.h"
11 #include "base/string_util.h"
12 #include "ppapi/c/pp_var.h"
13 #include "ppapi/shared_impl/ppapi_globals.h"
14 #include "ppapi/shared_impl/var_tracker.h"
16 namespace ppapi {
18 // Var -------------------------------------------------------------------------
20 Var::Var() : var_id_(0) {
23 Var::~Var() {
26 // static
27 std::string Var::PPVarToLogString(PP_Var var) {
28 switch (var.type) {
29 case PP_VARTYPE_UNDEFINED:
30 return "[Undefined]";
31 case PP_VARTYPE_NULL:
32 return "[Null]";
33 case PP_VARTYPE_BOOL:
34 return var.value.as_bool ? "[True]" : "[False]";
35 case PP_VARTYPE_INT32:
36 return base::IntToString(var.value.as_int);
37 case PP_VARTYPE_DOUBLE:
38 return base::DoubleToString(var.value.as_double);
39 case PP_VARTYPE_STRING: {
40 StringVar* string(StringVar::FromPPVar(var));
41 if (!string)
42 return "[Invalid string]";
44 // Since this is for logging, escape NULLs, truncate length.
45 std::string result;
46 const size_t kTruncateAboveLength = 128;
47 if (string->value().size() > kTruncateAboveLength)
48 result = string->value().substr(0, kTruncateAboveLength) + "...";
49 else
50 result = string->value();
52 std::string null;
53 null.push_back(0);
54 ReplaceSubstringsAfterOffset(&result, 0, null, "\\0");
55 return result;
57 case PP_VARTYPE_OBJECT:
58 return "[Object]";
59 case PP_VARTYPE_ARRAY:
60 return "[Array]";
61 case PP_VARTYPE_DICTIONARY:
62 return "[Dictionary]";
63 case PP_VARTYPE_ARRAY_BUFFER:
64 return "[Array buffer]";
65 default:
66 return "[Invalid var]";
70 StringVar* Var::AsStringVar() {
71 return NULL;
74 ArrayBufferVar* Var::AsArrayBufferVar() {
75 return NULL;
78 NPObjectVar* Var::AsNPObjectVar() {
79 return NULL;
82 ProxyObjectVar* Var::AsProxyObjectVar() {
83 return NULL;
86 ArrayVar* Var::AsArrayVar() {
87 return NULL;
90 DictionaryVar* Var::AsDictionaryVar() {
91 return NULL;
94 PP_Var Var::GetPPVar() {
95 int32 id = GetOrCreateVarID();
96 if (!id)
97 return PP_MakeNull();
99 PP_Var result;
100 result.type = GetType();
101 result.padding = 0;
102 result.value.as_id = id;
103 return result;
106 int32 Var::GetExistingVarID() const {
107 return var_id_;
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() {
133 StringVar::StringVar(const std::string& str)
134 : value_(str) {
137 StringVar::StringVar(const char* str, uint32 len)
138 : value_(str, len) {
141 StringVar::~StringVar() {
144 StringVar* StringVar::AsStringVar() {
145 return this;
148 PP_VarType StringVar::GetType() const {
149 return PP_VARTYPE_STRING;
152 // static
153 PP_Var StringVar::StringToPPVar(const std::string& var) {
154 return StringToPPVar(var.c_str(), static_cast<uint32>(var.size()));
157 // static
158 PP_Var StringVar::StringToPPVar(const char* data, uint32 len) {
159 scoped_refptr<StringVar> str(new StringVar(data, len));
160 if (!str || !IsStringUTF8(str->value()))
161 return PP_MakeNull();
162 return str->GetPPVar();
165 // static
166 StringVar* StringVar::FromPPVar(PP_Var var) {
167 if (var.type != PP_VARTYPE_STRING)
168 return NULL;
169 scoped_refptr<Var> var_object(
170 PpapiGlobals::Get()->GetVarTracker()->GetVar(var));
171 if (!var_object)
172 return NULL;
173 return var_object->AsStringVar();
176 // static
177 PP_Var StringVar::SwapValidatedUTF8StringIntoPPVar(std::string* src) {
178 scoped_refptr<StringVar> str(new StringVar);
179 str->value_.swap(*src);
180 return str->GetPPVar();
183 // ArrayBufferVar --------------------------------------------------------------
185 ArrayBufferVar::ArrayBufferVar() {
188 ArrayBufferVar::~ArrayBufferVar() {
191 ArrayBufferVar* ArrayBufferVar::AsArrayBufferVar() {
192 return this;
195 PP_VarType ArrayBufferVar::GetType() const {
196 return PP_VARTYPE_ARRAY_BUFFER;
199 // static
200 ArrayBufferVar* ArrayBufferVar::FromPPVar(PP_Var var) {
201 if (var.type != PP_VARTYPE_ARRAY_BUFFER)
202 return NULL;
203 scoped_refptr<Var> var_object(
204 PpapiGlobals::Get()->GetVarTracker()->GetVar(var));
205 if (!var_object)
206 return NULL;
207 return var_object->AsArrayBufferVar();
210 } // namespace ppapi