OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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. |
| 4 |
| 5 /* |
| 6 * Copyright (C) 2010 Google Inc. All rights reserved. |
| 7 * |
| 8 * Redistribution and use in source and binary forms, with or without |
| 9 * modification, are permitted provided that the following conditions are |
| 10 * met: |
| 11 * |
| 12 * * Redistributions of source code must retain the above copyright |
| 13 * notice, this list of conditions and the following disclaimer. |
| 14 * * Redistributions in binary form must reproduce the above |
| 15 * copyright notice, this list of conditions and the following disclaimer |
| 16 * in the documentation and/or other materials provided with the |
| 17 * distribution. |
| 18 * * Neither the name of Google Inc. nor the names of its |
| 19 * contributors may be used to endorse or promote products derived from |
| 20 * this software without specific prior written permission. |
| 21 * |
| 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 */ |
| 34 |
| 35 #include "content/test/layout_tests/runner/CppVariant.h" |
| 36 |
| 37 #include "content/test/layout_tests/runner/TestCommon.h" |
| 38 #include <limits> |
| 39 |
| 40 using namespace blink; |
| 41 using namespace std; |
| 42 |
| 43 namespace WebTestRunner { |
| 44 |
| 45 CppVariant::CppVariant() |
| 46 { |
| 47 type = NPVariantType_Null; |
| 48 } |
| 49 |
| 50 // Note that Set() performs a deep copy, which is necessary to safely |
| 51 // call FreeData() on the value in the destructor. |
| 52 CppVariant::CppVariant(const CppVariant& original) |
| 53 { |
| 54 type = NPVariantType_Null; |
| 55 set(original); |
| 56 } |
| 57 |
| 58 // See comment for copy constructor, above. |
| 59 CppVariant& CppVariant::operator=(const CppVariant& original) |
| 60 { |
| 61 if (&original != this) |
| 62 set(original); |
| 63 return *this; |
| 64 } |
| 65 |
| 66 CppVariant::~CppVariant() |
| 67 { |
| 68 freeData(); |
| 69 } |
| 70 |
| 71 void CppVariant::freeData() |
| 72 { |
| 73 WebBindings::releaseVariantValue(this); |
| 74 } |
| 75 |
| 76 bool CppVariant::isEqual(const CppVariant& other) const |
| 77 { |
| 78 if (type != other.type) |
| 79 return false; |
| 80 |
| 81 switch (type) { |
| 82 case NPVariantType_Bool: |
| 83 return (value.boolValue == other.value.boolValue); |
| 84 case NPVariantType_Int32: |
| 85 return (value.intValue == other.value.intValue); |
| 86 case NPVariantType_Double: |
| 87 return (value.doubleValue == other.value.doubleValue); |
| 88 case NPVariantType_String: { |
| 89 const NPString *this_value = &value.stringValue; |
| 90 const NPString *other_value = &other.value.stringValue; |
| 91 uint32_t len = this_value->UTF8Length; |
| 92 return len == other_value->UTF8Length |
| 93 && !strncmp(this_value->UTF8Characters, |
| 94 other_value->UTF8Characters, len); |
| 95 } |
| 96 case NPVariantType_Null: |
| 97 case NPVariantType_Void: |
| 98 return true; |
| 99 case NPVariantType_Object: { |
| 100 NPObject* thisValue = value.objectValue; |
| 101 NPObject* otherValue = other.value.objectValue; |
| 102 return thisValue->_class == otherValue->_class |
| 103 && thisValue->referenceCount == otherValue->referenceCount; |
| 104 } |
| 105 } |
| 106 return false; |
| 107 } |
| 108 |
| 109 void CppVariant::copyToNPVariant(NPVariant* result) const |
| 110 { |
| 111 result->type = type; |
| 112 switch (type) { |
| 113 case NPVariantType_Bool: |
| 114 result->value.boolValue = value.boolValue; |
| 115 break; |
| 116 case NPVariantType_Int32: |
| 117 result->value.intValue = value.intValue; |
| 118 break; |
| 119 case NPVariantType_Double: |
| 120 result->value.doubleValue = value.doubleValue; |
| 121 break; |
| 122 case NPVariantType_String: |
| 123 WebBindings::initializeVariantWithStringCopy(result, &value.stringValue)
; |
| 124 break; |
| 125 case NPVariantType_Null: |
| 126 case NPVariantType_Void: |
| 127 // Nothing to set. |
| 128 break; |
| 129 case NPVariantType_Object: |
| 130 result->type = NPVariantType_Object; |
| 131 result->value.objectValue = WebBindings::retainObject(value.objectValue)
; |
| 132 break; |
| 133 } |
| 134 } |
| 135 |
| 136 void CppVariant::set(const NPVariant& newValue) |
| 137 { |
| 138 freeData(); |
| 139 switch (newValue.type) { |
| 140 case NPVariantType_Bool: |
| 141 set(newValue.value.boolValue); |
| 142 break; |
| 143 case NPVariantType_Int32: |
| 144 set(newValue.value.intValue); |
| 145 break; |
| 146 case NPVariantType_Double: |
| 147 set(newValue.value.doubleValue); |
| 148 break; |
| 149 case NPVariantType_String: |
| 150 set(newValue.value.stringValue); |
| 151 break; |
| 152 case NPVariantType_Null: |
| 153 case NPVariantType_Void: |
| 154 type = newValue.type; |
| 155 break; |
| 156 case NPVariantType_Object: |
| 157 set(newValue.value.objectValue); |
| 158 break; |
| 159 } |
| 160 } |
| 161 |
| 162 void CppVariant::setNull() |
| 163 { |
| 164 freeData(); |
| 165 type = NPVariantType_Null; |
| 166 } |
| 167 |
| 168 void CppVariant::set(bool newValue) |
| 169 { |
| 170 freeData(); |
| 171 type = NPVariantType_Bool; |
| 172 value.boolValue = newValue; |
| 173 } |
| 174 |
| 175 void CppVariant::set(int32_t newValue) |
| 176 { |
| 177 freeData(); |
| 178 type = NPVariantType_Int32; |
| 179 value.intValue = newValue; |
| 180 } |
| 181 |
| 182 void CppVariant::set(double newValue) |
| 183 { |
| 184 freeData(); |
| 185 type = NPVariantType_Double; |
| 186 value.doubleValue = newValue; |
| 187 } |
| 188 |
| 189 // The newValue must be a null-terminated string. |
| 190 void CppVariant::set(const char* newValue) |
| 191 { |
| 192 freeData(); |
| 193 type = NPVariantType_String; |
| 194 NPString newString = {newValue, |
| 195 static_cast<uint32_t>(strlen(newValue))}; |
| 196 WebBindings::initializeVariantWithStringCopy(this, &newString); |
| 197 } |
| 198 |
| 199 void CppVariant::set(const string& newValue) |
| 200 { |
| 201 freeData(); |
| 202 type = NPVariantType_String; |
| 203 NPString newString = {newValue.data(), |
| 204 static_cast<uint32_t>(newValue.size())}; |
| 205 WebBindings::initializeVariantWithStringCopy(this, &newString); |
| 206 } |
| 207 |
| 208 void CppVariant::set(const NPString& newValue) |
| 209 { |
| 210 freeData(); |
| 211 type = NPVariantType_String; |
| 212 WebBindings::initializeVariantWithStringCopy(this, &newValue); |
| 213 } |
| 214 |
| 215 void CppVariant::set(NPObject* newValue) |
| 216 { |
| 217 freeData(); |
| 218 type = NPVariantType_Object; |
| 219 value.objectValue = WebBindings::retainObject(newValue); |
| 220 } |
| 221 |
| 222 string CppVariant::toString() const |
| 223 { |
| 224 BLINK_ASSERT(isString()); |
| 225 return string(value.stringValue.UTF8Characters, |
| 226 value.stringValue.UTF8Length); |
| 227 } |
| 228 |
| 229 int32_t CppVariant::toInt32() const |
| 230 { |
| 231 if (isInt32()) |
| 232 return value.intValue; |
| 233 if (isDouble()) |
| 234 return static_cast<int32_t>(value.doubleValue); |
| 235 BLINK_ASSERT_NOT_REACHED(); |
| 236 return 0; |
| 237 } |
| 238 |
| 239 double CppVariant::toDouble() const |
| 240 { |
| 241 if (isInt32()) |
| 242 return static_cast<double>(value.intValue); |
| 243 if (isDouble()) |
| 244 return value.doubleValue; |
| 245 BLINK_ASSERT_NOT_REACHED(); |
| 246 return 0; |
| 247 } |
| 248 |
| 249 bool CppVariant::toBoolean() const |
| 250 { |
| 251 BLINK_ASSERT(isBool()); |
| 252 return value.boolValue; |
| 253 } |
| 254 |
| 255 vector<string> CppVariant::toStringVector() const |
| 256 { |
| 257 |
| 258 BLINK_ASSERT(isObject()); |
| 259 vector<string> stringVector; |
| 260 NPObject* npValue = value.objectValue; |
| 261 NPIdentifier lengthId = WebBindings::getStringIdentifier("length"); |
| 262 |
| 263 if (!WebBindings::hasProperty(0, npValue, lengthId)) |
| 264 return stringVector; |
| 265 |
| 266 NPVariant lengthValue; |
| 267 if (!WebBindings::getProperty(0, npValue, lengthId, &lengthValue)) |
| 268 return stringVector; |
| 269 |
| 270 int length = 0; |
| 271 // The length is a double in some cases. |
| 272 if (NPVARIANT_IS_DOUBLE(lengthValue)) |
| 273 length = static_cast<int>(NPVARIANT_TO_DOUBLE(lengthValue)); |
| 274 else if (NPVARIANT_IS_INT32(lengthValue)) |
| 275 length = NPVARIANT_TO_INT32(lengthValue); |
| 276 WebBindings::releaseVariantValue(&lengthValue); |
| 277 |
| 278 // For sanity, only allow 100 items. |
| 279 length = min(100, length); |
| 280 for (int i = 0; i < length; ++i) { |
| 281 // Get each of the items. |
| 282 char indexInChar[20]; // Enough size to store 32-bit integer |
| 283 snprintf(indexInChar, 20, "%d", i); |
| 284 string index(indexInChar); |
| 285 NPIdentifier indexId = WebBindings::getStringIdentifier(index.c_str()); |
| 286 if (!WebBindings::hasProperty(0, npValue, indexId)) |
| 287 continue; |
| 288 NPVariant indexValue; |
| 289 if (!WebBindings::getProperty(0, npValue, indexId, &indexValue)) |
| 290 continue; |
| 291 if (NPVARIANT_IS_STRING(indexValue)) { |
| 292 string item(NPVARIANT_TO_STRING(indexValue).UTF8Characters, |
| 293 NPVARIANT_TO_STRING(indexValue).UTF8Length); |
| 294 stringVector.push_back(item); |
| 295 } |
| 296 WebBindings::releaseVariantValue(&indexValue); |
| 297 } |
| 298 return stringVector; |
| 299 } |
| 300 |
| 301 bool CppVariant::invoke(const string& method, const CppVariant* arguments, |
| 302 uint32_t argumentCount, CppVariant& result) const |
| 303 { |
| 304 BLINK_ASSERT(isObject()); |
| 305 NPIdentifier methodName = WebBindings::getStringIdentifier(method.c_str()); |
| 306 NPObject* npObject = value.objectValue; |
| 307 if (!WebBindings::hasMethod(0, npObject, methodName)) |
| 308 return false; |
| 309 NPVariant r; |
| 310 bool status = WebBindings::invoke(0, npObject, methodName, arguments, argume
ntCount, &r); |
| 311 result.set(r); |
| 312 return status; |
| 313 } |
| 314 |
| 315 bool CppVariant::invokeDefault(const CppVariant* arguments, uint32_t argumentCou
nt, |
| 316 CppVariant& result) const |
| 317 { |
| 318 BLINK_ASSERT(isObject()); |
| 319 NPObject* npObject = value.objectValue; |
| 320 NPVariant r; |
| 321 bool status = WebBindings::invokeDefault(0, npObject, arguments, argumentCou
nt, &r); |
| 322 result.set(r); |
| 323 return status; |
| 324 } |
| 325 |
| 326 } |
OLD | NEW |