dom/plugins/test/testplugin/nptest_utils.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/plugins/test/testplugin/nptest_utils.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,114 @@
     1.4 +/* ***** BEGIN LICENSE BLOCK *****
     1.5 + * 
     1.6 + * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
     1.7 + *
     1.8 + * Redistribution and use in source and binary forms, with or without
     1.9 + * modification, are permitted provided that the following conditions
    1.10 + * are met:
    1.11 + * 1. Redistributions of source code must retain the above copyright
    1.12 + *    notice, this list of conditions and the following disclaimer.
    1.13 + * 2. Redistributions in binary form must reproduce the above copyright
    1.14 + *    notice, this list of conditions and the following disclaimer in the
    1.15 + *    documentation and/or other materials provided with the distribution.
    1.16 + *
    1.17 + * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
    1.18 + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.19 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    1.20 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
    1.21 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    1.22 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    1.23 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    1.24 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
    1.25 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.26 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.27 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.28 + * 
    1.29 + * Contributor(s):
    1.30 + *   Josh Aas <josh@mozilla.com>
    1.31 + * 
    1.32 + * ***** END LICENSE BLOCK ***** */
    1.33 +
    1.34 +#include "nptest_utils.h"
    1.35 +
    1.36 +#include <stdlib.h>
    1.37 +#include <string.h>
    1.38 +#include <assert.h>
    1.39 +#include "mozilla/NullPtr.h"
    1.40 +
    1.41 +NPUTF8*
    1.42 +createCStringFromNPVariant(const NPVariant* variant)
    1.43 +{
    1.44 +  size_t length = NPVARIANT_TO_STRING(*variant).UTF8Length;
    1.45 +  NPUTF8* result = (NPUTF8*)malloc(length + 1);
    1.46 +  memcpy(result, NPVARIANT_TO_STRING(*variant).UTF8Characters, length);
    1.47 +  result[length] = '\0';
    1.48 +  return result;
    1.49 +}
    1.50 +
    1.51 +NPIdentifier
    1.52 +variantToIdentifier(NPVariant variant)
    1.53 +{
    1.54 +  if (NPVARIANT_IS_STRING(variant))
    1.55 +    return stringVariantToIdentifier(variant);
    1.56 +  else if (NPVARIANT_IS_INT32(variant))
    1.57 +    return int32VariantToIdentifier(variant);
    1.58 +  else if (NPVARIANT_IS_DOUBLE(variant))
    1.59 +    return doubleVariantToIdentifier(variant);
    1.60 +  return 0;
    1.61 +}
    1.62 +
    1.63 +NPIdentifier
    1.64 +stringVariantToIdentifier(NPVariant variant)
    1.65 +{
    1.66 +  assert(NPVARIANT_IS_STRING(variant));
    1.67 +  NPUTF8* utf8String = createCStringFromNPVariant(&variant);
    1.68 +  NPIdentifier identifier = NPN_GetStringIdentifier(utf8String);
    1.69 +  free(utf8String);
    1.70 +  return identifier;
    1.71 +}
    1.72 +
    1.73 +NPIdentifier
    1.74 +int32VariantToIdentifier(NPVariant variant)
    1.75 +{
    1.76 +  assert(NPVARIANT_IS_INT32(variant));
    1.77 +  int32_t integer = NPVARIANT_TO_INT32(variant);
    1.78 +  return NPN_GetIntIdentifier(integer);
    1.79 +}
    1.80 +
    1.81 +NPIdentifier
    1.82 +doubleVariantToIdentifier(NPVariant variant)
    1.83 +{
    1.84 +  assert(NPVARIANT_IS_DOUBLE(variant));
    1.85 +  double value = NPVARIANT_TO_DOUBLE(variant);
    1.86 +  // sadly there is no "getdoubleidentifier"
    1.87 +  int32_t integer = static_cast<int32_t>(value);
    1.88 +  return NPN_GetIntIdentifier(integer);
    1.89 +}
    1.90 +
    1.91 +/*
    1.92 + * Parse a color in hex format, #AARRGGBB or AARRGGBB.
    1.93 + */
    1.94 +uint32_t
    1.95 +parseHexColor(const char* color, int len)
    1.96 +{
    1.97 +  uint8_t bgra[4] = { 0, 0, 0, 0xFF };
    1.98 +  int i = 0;
    1.99 +
   1.100 +  // Ignore unsupported formats.
   1.101 +  if (len != 9 && len != 8)
   1.102 +    return 0;
   1.103 +
   1.104 +  // start from the right and work to the left
   1.105 +  while (len >= 2) { // we have at least #AA or AA left.
   1.106 +    char byte[3];
   1.107 +    // parse two hex digits
   1.108 +    byte[0] = color[len - 2];
   1.109 +    byte[1] = color[len - 1];
   1.110 +    byte[2] = '\0';
   1.111 +
   1.112 +    bgra[i] = (uint8_t)(strtoul(byte, nullptr, 16) & 0xFF);
   1.113 +    i++;
   1.114 +    len -= 2;
   1.115 +  }
   1.116 +  return (bgra[3] << 24) | (bgra[2] << 16) | (bgra[1] << 8) | bgra[0];
   1.117 +}

mercurial