示例#1
0
  // Simple function for formatted output of a V8 value.
  void PrintValue(CefRefPtr<CefV8Value> value, std::stringstream &stream,
                  int indent) {
    std::stringstream indent_stream;
    for (int i = 0; i < indent; ++i)
      indent_stream << "  ";
    std::string indent_str = indent_stream.str();

    if (value->IsUndefined())
      stream << "(undefined)";
    else if (value->IsNull())
      stream << "(null)";
    else if (value->IsBool())
      stream << "(bool) " << (value->GetBoolValue() ? "true" : "false");
    else if (value->IsInt())
      stream << "(int) " << value->GetIntValue();
    else if (value->IsDouble())
      stream << "(double) " << value->GetDoubleValue();
    else if (value->IsString())
      stream << "(string) " << std::string(value->GetStringValue());
    else if (value->IsFunction())
      stream << "(function) " << std::string(value->GetFunctionName());
    else if (value->IsArray()) {
      stream << "(array) [";
      int len = value->GetArrayLength();
      for (int i = 0; i < len; ++i) {
        stream << "\n  " << indent_str.c_str() << i << " = ";
        PrintValue(value->GetValue(i), stream, indent+1);
      }
      stream << "\n" << indent_str.c_str() << "]";
    } else if (value->IsObject()) {
      stream << "(object) [";
      std::vector<CefString> keys;
      if (value->GetKeys(keys)) {
        for (size_t i = 0; i < keys.size(); ++i) {
          stream << "\n  " << indent_str.c_str() << keys[i].c_str() << " = ";
          PrintValue(value->GetValue(keys[i]), stream, indent+1);
        }
      }
      stream << "\n" << indent_str.c_str() << "]";
    }
  }
void IPC_Container::SetIPCArgumentsValue(
    CefRefPtr<CefListValue> args,
    int& position,
    CefRefPtr<CefV8Value> v8Value,
    ContainerAttribute attr)
{

    if (attr.GetArraySize() == 0)
    {
        switch (attr.GetDatatype())
        {
            case Datatype::INTEGER:
            {
                if (v8Value->IsInt())
                {
                    args->SetInt(position++, v8Value->GetIntValue());
                }
                else
                {
                    //DLOG(WARNING) << "ERROR(SetIPCArgumentsValue): Said \"int\", got something else at position=" << position++;
                }
                break;
            }

            case Datatype::DOUBLE:
            {
                if (v8Value->IsDouble())
                {
                    args->SetDouble(position++, v8Value->GetDoubleValue());
                }
                else
                {
                    //DLOG(WARNING) << "ERROR(SetIPCArgumentsValue): Said \"double\", got something else at position=" << position++;
                }
                break;
            }

            case Datatype::STRING:
            {
                if (v8Value->IsString())
                {
                    args->SetString(position++, v8Value->GetStringValue());
                }
                else
                {
                    //DLOG(WARNING) << "ERROR(SetIPCArgumentsValue): Said \"string\", got something else at position=" << position++;
                }
                break;
            }
        }
    }
    else if (attr.GetArraySize() > 0) // V8Value is an array
    {
        for (int i = 0; i < attr.GetArraySize(); i++)
        {
            switch (attr.GetDatatype())
            {
            case Datatype::INTEGER:
            {
                if (v8Value->GetValue(i)->IsInt())
                {
                    args->SetInt(position++, v8Value->GetValue(i)->GetIntValue());
                }
                else
                {
                    //DLOG(WARNING) << "ERROR(SetIPCArgumentsValue): Said \"int\", got something else at position=" << position++;
                }
                break;
            }

            case Datatype::DOUBLE:
            {
                if (v8Value->GetValue(i)->IsDouble())
                {
                    args->SetDouble(position++, v8Value->GetValue(i)->GetDoubleValue());
                }
                else
                {
                    //DLOG(WARNING) << "ERROR(SetIPCArgumentsValue): Said \"double\", got something else at position=" << position++;
                }
                break;
            }

            case Datatype::STRING:
            {
                if (v8Value->GetValue(i)->IsString())
                {
                    args->SetString(position++, v8Value->GetValue(i)->GetStringValue());
                }
                else
                {
                    //DLOG(WARNING) << "ERROR(SetIPCArgumentsValue): Said \"string\", got something else at position=" << position++;
                }
                break;
            }
            }
        }
    }
    else // ERROR CASE
    {
        //DLOG(WARNING) << "ERROR(SetIPCArgumentsValue): Array size < 0!";
        position++;	// Go on with next element
    }
}
示例#3
0
void V8ValueAppendToCefListValue(CefRefPtr<CefV8Value> v8Value,
                                 CefRefPtr<CefListValue> listValue,
                                 int nestingLevel) {
    if (!v8Value->IsValid()) {
        DebugLog("V8ValueAppendToCefListValue(): IsValid() FAILED");
        return;
    }
    if (nestingLevel > 8) {
        DebugLog("V8ValueAppendToCefListValue(): WARNING: max nesting level (8) " \
                "exceeded");
        return;
    }
    if (v8Value->IsUndefined() || v8Value->IsNull()) {
        listValue->SetNull((int)listValue->GetSize());
    } else if (v8Value->IsBool()) {
        listValue->SetBool((int)listValue->GetSize(), v8Value->GetBoolValue());
    } else if (v8Value->IsInt()) {
        listValue->SetInt((int)listValue->GetSize(), v8Value->GetIntValue());
    } else if (v8Value->IsUInt()) {
        uint32 uint32_value = v8Value->GetUIntValue();
        CefRefPtr<CefBinaryValue> binaryValue = CefBinaryValue::Create(
            &uint32_value, sizeof(uint32_value));
        listValue->SetBinary((int)listValue->GetSize(), binaryValue);
    } else if (v8Value->IsDouble()) {
        listValue->SetDouble((int)listValue->GetSize(), v8Value->GetDoubleValue());
    } else if (v8Value->IsDate()) {
        // TODO: in time_utils.pyx there are already functions for
        // converting cef_time_t to python DateTime, we could easily
        // add a new function for converting the python DateTime to
        // string and then to CefString and expose the function using
        // the "public" keyword. But how do we get the cef_time_t
        // structure from the CefTime class? GetDateValue() returns
        // CefTime class.
        listValue->SetNull((int)listValue->GetSize());
    } else if (v8Value->IsString()) {
        listValue->SetString((int)listValue->GetSize(), v8Value->GetStringValue());
    } else if (v8Value->IsArray()) {
        // Check for IsArray() must happen before the IsObject() check.
        int length = v8Value->GetArrayLength();
        CefRefPtr<CefListValue> newListValue = CefListValue::Create();
        for (int i = 0; i < length; ++i) {
            V8ValueAppendToCefListValue(v8Value->GetValue(i), newListValue,
                    nestingLevel + 1);
        }
        listValue->SetList((int)listValue->GetSize(), newListValue);
    } else if (v8Value->IsFunction()) {
        // Check for IsFunction() must happen before the IsObject() check.
        if (CefV8Context::InContext()) {
            CefRefPtr<CefV8Context> context = \
                    CefV8Context::GetCurrentContext();
            CefRefPtr<CefFrame> frame = context->GetFrame();
            std::string strCallbackId = PutJavascriptCallback(frame, v8Value);
            /* strCallbackId = '####cefpython####' \
                               '{"what"=>"javascript-callback", ..}' */
            listValue->SetString((int)listValue->GetSize(), strCallbackId);
        } else {
            listValue->SetNull((int)listValue->GetSize());
            DebugLog("V8ValueAppendToCefListValue() FAILED: not in V8 context"
                    " , FATAL ERROR!");
        }
    } else if (v8Value->IsObject()) {
        // Check for IsObject() must happen after the IsArray()
        // and IsFunction() checks.
        listValue->SetDictionary((int)listValue->GetSize(),
                V8ObjectToCefDictionaryValue(v8Value, nestingLevel + 1));
    } else {
        listValue->SetNull((int)listValue->GetSize());
        DebugLog("V8ValueAppendToCefListValue() FAILED: unknown V8 type");
    }
}