Home | History | Annotate | Download | only in cpp
      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.
      4 
      5 #include "ppapi/cpp/var.h"
      6 
      7 #include <stdio.h>
      8 #include <string.h>
      9 
     10 #include <algorithm>
     11 
     12 #include "ppapi/c/pp_var.h"
     13 #include "ppapi/c/ppb_var.h"
     14 #include "ppapi/cpp/instance.h"
     15 #include "ppapi/cpp/logging.h"
     16 #include "ppapi/cpp/module.h"
     17 #include "ppapi/cpp/module_impl.h"
     18 
     19 // Define equivalent to snprintf on Windows.
     20 #if defined(_MSC_VER)
     21 #  define snprintf sprintf_s
     22 #endif
     23 
     24 namespace pp {
     25 
     26 namespace {
     27 
     28 template <> const char* interface_name<PPB_Var_1_1>() {
     29   return PPB_VAR_INTERFACE_1_1;
     30 }
     31 template <> const char* interface_name<PPB_Var_1_0>() {
     32   return PPB_VAR_INTERFACE_1_0;
     33 }
     34 
     35 // Technically you can call AddRef and Release on any Var, but it may involve
     36 // cross-process calls depending on the plugin. This is an optimization so we
     37 // only do refcounting on the necessary objects.
     38 inline bool NeedsRefcounting(const PP_Var& var) {
     39   return var.type > PP_VARTYPE_DOUBLE;
     40 }
     41 
     42 // This helper function detects whether PPB_Var version 1.1 is available. If so,
     43 // it uses it to create a PP_Var for the given string. Otherwise it falls back
     44 // to PPB_Var version 1.0.
     45 PP_Var VarFromUtf8Helper(const char* utf8_str, uint32_t len) {
     46   if (has_interface<PPB_Var_1_1>()) {
     47     return get_interface<PPB_Var_1_1>()->VarFromUtf8(utf8_str, len);
     48   } else if (has_interface<PPB_Var_1_0>()) {
     49     return get_interface<PPB_Var_1_0>()->VarFromUtf8(Module::Get()->pp_module(),
     50                                                      utf8_str,
     51                                                      len);
     52   } else {
     53     return PP_MakeNull();
     54   }
     55 }
     56 
     57 }  // namespace
     58 
     59 Var::Var() {
     60   memset(&var_, 0, sizeof(var_));
     61   var_.type = PP_VARTYPE_UNDEFINED;
     62   is_managed_ = true;
     63 }
     64 
     65 Var::Var(Null) {
     66   memset(&var_, 0, sizeof(var_));
     67   var_.type = PP_VARTYPE_NULL;
     68   is_managed_ = true;
     69 }
     70 
     71 Var::Var(bool b) {
     72   var_.type = PP_VARTYPE_BOOL;
     73   var_.padding = 0;
     74   var_.value.as_bool = PP_FromBool(b);
     75   is_managed_ = true;
     76 }
     77 
     78 Var::Var(int32_t i) {
     79   var_.type = PP_VARTYPE_INT32;
     80   var_.padding = 0;
     81   var_.value.as_int = i;
     82   is_managed_ = true;
     83 }
     84 
     85 Var::Var(double d) {
     86   var_.type = PP_VARTYPE_DOUBLE;
     87   var_.padding = 0;
     88   var_.value.as_double = d;
     89   is_managed_ = true;
     90 }
     91 
     92 Var::Var(const char* utf8_str) {
     93   uint32_t len = utf8_str ? static_cast<uint32_t>(strlen(utf8_str)) : 0;
     94   var_ = VarFromUtf8Helper(utf8_str, len);
     95   is_managed_ = true;
     96 }
     97 
     98 Var::Var(const std::string& utf8_str) {
     99   var_ = VarFromUtf8Helper(utf8_str.c_str(),
    100                            static_cast<uint32_t>(utf8_str.size()));
    101   is_managed_ = true;
    102 }
    103 
    104 
    105 Var::Var(const PP_Var& var) {
    106   var_ = var;
    107   is_managed_ = true;
    108   if (NeedsRefcounting(var_)) {
    109     if (has_interface<PPB_Var_1_0>())
    110       get_interface<PPB_Var_1_0>()->AddRef(var_);
    111     else
    112       var_.type = PP_VARTYPE_NULL;
    113   }
    114 }
    115 
    116 Var::Var(const Var& other) {
    117   var_ = other.var_;
    118   is_managed_ = true;
    119   if (NeedsRefcounting(var_)) {
    120     if (has_interface<PPB_Var_1_0>())
    121       get_interface<PPB_Var_1_0>()->AddRef(var_);
    122     else
    123       var_.type = PP_VARTYPE_NULL;
    124   }
    125 }
    126 
    127 Var::~Var() {
    128   if (NeedsRefcounting(var_) &&
    129       is_managed_ &&
    130       has_interface<PPB_Var_1_0>())
    131     get_interface<PPB_Var_1_0>()->Release(var_);
    132 }
    133 
    134 Var& Var::operator=(const Var& other) {
    135   // Early return for self-assignment. Note however, that two distinct vars
    136   // can refer to the same object, so we still need to be careful about the
    137   // refcounting below.
    138   if (this == &other)
    139     return *this;
    140 
    141   // Be careful to keep the ref alive for cases where we're assigning an
    142   // object to itself by addrefing the new one before releasing the old one.
    143   bool old_is_managed = is_managed_;
    144   is_managed_ = true;
    145   if (NeedsRefcounting(other.var_)) {
    146     // Assume we already has_interface<PPB_Var_1_0> for refcounted vars or else
    147     // we couldn't have created them in the first place.
    148     get_interface<PPB_Var_1_0>()->AddRef(other.var_);
    149   }
    150   if (NeedsRefcounting(var_) && old_is_managed)
    151     get_interface<PPB_Var_1_0>()->Release(var_);
    152 
    153   var_ = other.var_;
    154   return *this;
    155 }
    156 
    157 bool Var::operator==(const Var& other) const {
    158   if (var_.type != other.var_.type)
    159     return false;
    160   switch (var_.type) {
    161     case PP_VARTYPE_UNDEFINED:
    162     case PP_VARTYPE_NULL:
    163       return true;
    164     case PP_VARTYPE_BOOL:
    165       return AsBool() == other.AsBool();
    166     case PP_VARTYPE_INT32:
    167       return AsInt() == other.AsInt();
    168     case PP_VARTYPE_DOUBLE:
    169       return AsDouble() == other.AsDouble();
    170     case PP_VARTYPE_STRING:
    171       if (var_.value.as_id == other.var_.value.as_id)
    172         return true;
    173       return AsString() == other.AsString();
    174     case PP_VARTYPE_OBJECT:
    175     case PP_VARTYPE_ARRAY:
    176     case PP_VARTYPE_ARRAY_BUFFER:
    177     case PP_VARTYPE_DICTIONARY:
    178     default:  // Objects, arrays, dictionaries.
    179       return var_.value.as_id == other.var_.value.as_id;
    180   }
    181 }
    182 
    183 bool Var::AsBool() const {
    184   if (!is_bool()) {
    185     PP_NOTREACHED();
    186     return false;
    187   }
    188   return PP_ToBool(var_.value.as_bool);
    189 }
    190 
    191 int32_t Var::AsInt() const {
    192   if (is_int())
    193     return var_.value.as_int;
    194   if (is_double())
    195     return static_cast<int>(var_.value.as_double);
    196   PP_NOTREACHED();
    197   return 0;
    198 }
    199 
    200 double Var::AsDouble() const {
    201   if (is_double())
    202     return var_.value.as_double;
    203   if (is_int())
    204     return static_cast<double>(var_.value.as_int);
    205   PP_NOTREACHED();
    206   return 0.0;
    207 }
    208 
    209 std::string Var::AsString() const {
    210   if (!is_string()) {
    211     PP_NOTREACHED();
    212     return std::string();
    213   }
    214 
    215   if (!has_interface<PPB_Var_1_0>())
    216     return std::string();
    217   uint32_t len;
    218   const char* str = get_interface<PPB_Var_1_0>()->VarToUtf8(var_, &len);
    219   return std::string(str, len);
    220 }
    221 
    222 std::string Var::DebugString() const {
    223   char buf[256];
    224   if (is_undefined()) {
    225     snprintf(buf, sizeof(buf), "Var(UNDEFINED)");
    226   } else if (is_null()) {
    227     snprintf(buf, sizeof(buf), "Var(NULL)");
    228   } else if (is_bool()) {
    229     snprintf(buf, sizeof(buf), AsBool() ? "Var(true)" : "Var(false)");
    230   } else if (is_int()) {
    231     snprintf(buf, sizeof(buf), "Var(%d)", static_cast<int>(AsInt()));
    232   } else if (is_double()) {
    233     snprintf(buf, sizeof(buf), "Var(%f)", AsDouble());
    234   } else if (is_string()) {
    235     char format[] = "Var<'%s'>";
    236     size_t decoration = sizeof(format) - 2;  // The %s is removed.
    237     size_t available = sizeof(buf) - decoration;
    238     std::string str = AsString();
    239     if (str.length() > available) {
    240       str.resize(available - 3);  // Reserve space for ellipsis.
    241       str.append("...");
    242     }
    243     snprintf(buf, sizeof(buf), format, str.c_str());
    244   } else if (is_object()) {
    245     snprintf(buf, sizeof(buf), "Var(OBJECT)");
    246   } else if (is_array()) {
    247     snprintf(buf, sizeof(buf), "Var(ARRAY)");
    248   } else if (is_dictionary()) {
    249     snprintf(buf, sizeof(buf), "Var(DICTIONARY)");
    250   } else if (is_array_buffer()) {
    251     snprintf(buf, sizeof(buf), "Var(ARRAY_BUFFER)");
    252   } else {
    253     buf[0] = '\0';
    254   }
    255   return buf;
    256 }
    257 
    258 }  // namespace pp
    259