1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This file contains definitions for CppVariant.
#include <limits>
#include "third_party/WebKit/WebKit/chromium/public/WebBindings.h"
#include "webkit/glue/cpp_variant.h"
#include "base/logging.h"
#include "base/string_util.h"
#include "base/stringprintf.h"
#include "base/utf_string_conversions.h"
using WebKit::WebBindings;
CppVariant::CppVariant() {
type = NPVariantType_Null;
}
// Note that Set() performs a deep copy, which is necessary to safely
// call FreeData() on the value in the destructor.
CppVariant::CppVariant(const CppVariant& original) {
type = NPVariantType_Null;
Set(original);
}
// See comment for copy constructor, above.
CppVariant& CppVariant::operator=(const CppVariant& original) {
if (&original != this)
Set(original);
return *this;
}
CppVariant::~CppVariant() {
FreeData();
}
void CppVariant::FreeData() {
WebBindings::releaseVariantValue(this);
}
bool CppVariant::isEqual(const CppVariant& other) const {
if (type != other.type)
return false;
switch (type) {
case NPVariantType_Bool: {
return (value.boolValue == other.value.boolValue);
}
case NPVariantType_Int32: {
return (value.intValue == other.value.intValue);
}
case NPVariantType_Double: {
return (value.doubleValue == other.value.doubleValue);
}
case NPVariantType_String: {
const NPString *this_value = &value.stringValue;
const NPString *other_value = &other.value.stringValue;
uint32_t len = this_value->UTF8Length;
return (len == other_value->UTF8Length &&
!strncmp(this_value->UTF8Characters, other_value->UTF8Characters,
len));
}
case NPVariantType_Null:
case NPVariantType_Void: {
return true;
}
case NPVariantType_Object: {
NPObject *this_value = value.objectValue;
NPObject *other_value = other.value.objectValue;
return (this_value->_class == other_value->_class &&
this_value->referenceCount == other_value->referenceCount);
}
}
return false;
}
void CppVariant::CopyToNPVariant(NPVariant* result) const {
result->type = type;
switch (type) {
case NPVariantType_Bool:
result->value.boolValue = value.boolValue;
break;
case NPVariantType_Int32:
result->value.intValue = value.intValue;
break;
case NPVariantType_Double:
result->value.doubleValue = value.doubleValue;
break;
case NPVariantType_String:
WebBindings::initializeVariantWithStringCopy(result, &value.stringValue);
break;
case NPVariantType_Null:
case NPVariantType_Void:
// Nothing to set.
break;
case NPVariantType_Object:
result->type = NPVariantType_Object;
result->value.objectValue = WebBindings::retainObject(value.objectValue);
break;
}
}
void CppVariant::Set(const NPVariant& new_value) {
FreeData();
switch (new_value.type) {
case NPVariantType_Bool:
Set(new_value.value.boolValue);
break;
case NPVariantType_Int32:
Set(new_value.value.intValue);
break;
case NPVariantType_Double:
Set(new_value.value.doubleValue);
break;
case NPVariantType_String:
Set(new_value.value.stringValue);
break;
case NPVariantType_Null:
case NPVariantType_Void:
type = new_value.type;
break;
case NPVariantType_Object:
Set(new_value.value.objectValue);
break;
}
}
void CppVariant::SetNull() {
FreeData();
type = NPVariantType_Null;
}
void CppVariant::Set(bool new_value) {
FreeData();
type = NPVariantType_Bool;
value.boolValue = new_value;
}
void CppVariant::Set(int32_t new_value) {
FreeData();
type = NPVariantType_Int32;
value.intValue = new_value;
}
void CppVariant::Set(double new_value) {
FreeData();
type = NPVariantType_Double;
value.doubleValue = new_value;
}
// The new_value must be a null-terminated string.
void CppVariant::Set(const char* new_value) {
FreeData();
type = NPVariantType_String;
NPString new_string = {new_value,
static_cast<uint32_t>(strlen(new_value))};
WebBindings::initializeVariantWithStringCopy(this, &new_string);
}
void CppVariant::Set(const std::string& new_value) {
FreeData();
type = NPVariantType_String;
NPString new_string = {new_value.data(),
static_cast<uint32_t>(new_value.size())};
WebBindings::initializeVariantWithStringCopy(this, &new_string);
}
void CppVariant::Set(const NPString& new_value) {
FreeData();
type = NPVariantType_String;
WebBindings::initializeVariantWithStringCopy(this, &new_value);
}
void CppVariant::Set(NPObject* new_value) {
FreeData();
type = NPVariantType_Object;
value.objectValue = WebBindings::retainObject(new_value);
}
std::string CppVariant::ToString() const {
DCHECK(isString());
return std::string(value.stringValue.UTF8Characters,
value.stringValue.UTF8Length);
}
int32_t CppVariant::ToInt32() const {
if (isInt32()) {
return value.intValue;
} else if (isDouble()) {
return static_cast<int32_t>(value.doubleValue);
} else {
NOTREACHED();
return 0;
}
}
double CppVariant::ToDouble() const {
if (isInt32()) {
return static_cast<double>(value.intValue);
} else if (isDouble()) {
return value.doubleValue;
} else {
NOTREACHED();
return 0.0;
}
}
bool CppVariant::ToBoolean() const {
DCHECK(isBool());
return value.boolValue;
}
std::vector<std::string> CppVariant::ToStringVector() const {
DCHECK(isObject());
std::vector<std::string> string_vector;
NPObject* np_value = value.objectValue;
NPIdentifier length_id = WebBindings::getStringIdentifier("length");
if (WebBindings::hasProperty(NULL, np_value, length_id)) {
NPVariant length_value;
if (WebBindings::getProperty(NULL, np_value, length_id, &length_value)) {
int length = 0;
// The length is a double in some cases.
if (NPVARIANT_IS_DOUBLE(length_value))
length = static_cast<int>(NPVARIANT_TO_DOUBLE(length_value));
else if (NPVARIANT_IS_INT32(length_value))
length = NPVARIANT_TO_INT32(length_value);
WebBindings::releaseVariantValue(&length_value);
// For sanity, only allow 60000 items.
length = std::min(60000, length);
for (int i = 0; i < length; ++i) {
// Get each of the items.
std::string index = base::StringPrintf("%d", i);
NPIdentifier index_id = WebBindings::getStringIdentifier(index.c_str());
if (WebBindings::hasProperty(NULL, np_value, index_id)) {
NPVariant index_value;
if (WebBindings::getProperty(NULL, np_value, index_id, &index_value)) {
if (NPVARIANT_IS_STRING(index_value)) {
std::string string(
NPVARIANT_TO_STRING(index_value).UTF8Characters,
NPVARIANT_TO_STRING(index_value).UTF8Length);
string_vector.push_back(string);
}
WebBindings::releaseVariantValue(&index_value);
}
}
}
}
}
return string_vector;
}
bool CppVariant::Invoke(const std::string& method, const CppVariant* args,
uint32 arg_count, CppVariant& result) const {
DCHECK(isObject());
NPIdentifier method_name = WebBindings::getStringIdentifier(method.c_str());
NPObject* np_object = value.objectValue;
if (WebBindings::hasMethod(NULL, np_object, method_name)) {
NPVariant r;
bool status = WebBindings::invoke(NULL, np_object, method_name, args, arg_count, &r);
result.Set(r);
return status;
} else {
return false;
}
}
|