CefRefPtr<CefV8Value> CefListValueToV8Value( CefRefPtr<CefListValue> listValue, int nestingLevel) { if (!listValue->IsValid()) { DebugLog("CefListValueToV8Value() FAILED: " \ "CefDictionaryValue is invalid"); return CefV8Value::CreateNull(); } if (nestingLevel > 8) { DebugLog("CefListValueToV8Value(): WARNING: " \ "max nesting level (8) exceeded"); return CefV8Value::CreateNull(); } int listSize = (int)listValue->GetSize(); CefRefPtr<CefV8Value> ret = CefV8Value::CreateArray(listSize); CefRefPtr<CefBinaryValue> binaryValue; PythonCallback pyCallback; CefRefPtr<CefV8Handler> v8FunctionHandler; for (int key = 0; key < listSize; ++key) { cef_value_type_t valueType = listValue->GetType(key); bool success; std::string callbackName = "python_callback_"; if (valueType == VTYPE_NULL) { success = ret->SetValue(key, CefV8Value::CreateNull()); } else if (valueType == VTYPE_BOOL) { success = ret->SetValue(key, CefV8Value::CreateBool(listValue->GetBool(key))); } else if (valueType == VTYPE_INT) { success = ret->SetValue(key, CefV8Value::CreateInt(listValue->GetInt(key))); } else if (valueType == VTYPE_DOUBLE) { success = ret->SetValue(key, CefV8Value::CreateDouble(listValue->GetDouble(key))); } else if (valueType == VTYPE_STRING) { success = ret->SetValue(key, CefV8Value::CreateString(listValue->GetString(key))); } else if (valueType == VTYPE_BINARY) { binaryValue = listValue->GetBinary(key); if (binaryValue->GetSize() == sizeof(pyCallback)) { binaryValue->GetData(&pyCallback, sizeof(pyCallback), 0); v8FunctionHandler = new V8FunctionHandler( NULL, pyCallback.callbackId); // You must provide a function name to // CefV8Value::CreateFunction(), otherwise it fails. callbackName.append(AnyToString(pyCallback.callbackId)); success = ret->SetValue(key, CefV8Value::CreateFunction( callbackName, v8FunctionHandler)); } else { DebugLog("CefListValueToV8Value(): WARNING: " \ "unknown binary value, setting value to null"); success = ret->SetValue(key, CefV8Value::CreateNull()); } } else if (valueType == VTYPE_DICTIONARY) { success = ret->SetValue(key, CefDictionaryValueToV8Value( listValue->GetDictionary(key), nestingLevel + 1)); } else if (valueType == VTYPE_LIST) { success = ret->SetValue(key, CefListValueToV8Value( listValue->GetList(key), nestingLevel + 1)); } else { DebugLog("CefListValueToV8Value(): WARNING: " \ "unknown type, setting value to null"); success = ret->SetValue(key, CefV8Value::CreateNull()); } if (!success) { DebugLog("CefListValueToV8Value(): WARNING: " \ "ret->SetValue() failed"); } } return ret; }
CefRefPtr<CefV8Value> CefDictionaryValueToV8Value( CefRefPtr<CefDictionaryValue> dictValue, int nestingLevel) { if (!dictValue->IsValid()) { DebugLog("CefDictionaryValueToV8Value() FAILED: " \ "CefDictionaryValue is invalid"); return CefV8Value::CreateNull(); } if (nestingLevel > 8) { DebugLog("CefDictionaryValueToV8Value(): WARNING: " \ "max nesting level (8) exceeded"); return CefV8Value::CreateNull(); } std::vector<CefString> keys; if (!dictValue->GetKeys(keys)) { DebugLog("CefDictionaryValueToV8Value() FAILED: " \ "dictValue->GetKeys() failed"); return CefV8Value::CreateNull(); } CefRefPtr<CefV8Value> ret = CefV8Value::CreateObject(NULL); CefRefPtr<CefBinaryValue> binaryValue; PythonCallback pyCallback; CefRefPtr<CefV8Handler> v8FunctionHandler; for (std::vector<CefString>::iterator it = keys.begin(); \ it != keys.end(); ++it) { CefString key = *it; cef_value_type_t valueType = dictValue->GetType(key); bool success; std::string callbackName = "python_callback_"; if (valueType == VTYPE_NULL) { success = ret->SetValue(key, CefV8Value::CreateNull(), V8_PROPERTY_ATTRIBUTE_NONE); } else if (valueType == VTYPE_BOOL) { success = ret->SetValue(key, CefV8Value::CreateBool(dictValue->GetBool(key)), V8_PROPERTY_ATTRIBUTE_NONE); } else if (valueType == VTYPE_INT) { success = ret->SetValue(key, CefV8Value::CreateInt(dictValue->GetInt(key)), V8_PROPERTY_ATTRIBUTE_NONE); } else if (valueType == VTYPE_DOUBLE) { success = ret->SetValue(key, CefV8Value::CreateDouble(dictValue->GetDouble(key)), V8_PROPERTY_ATTRIBUTE_NONE); } else if (valueType == VTYPE_STRING) { success = ret->SetValue(key, CefV8Value::CreateString(dictValue->GetString(key)), V8_PROPERTY_ATTRIBUTE_NONE); } else if (valueType == VTYPE_BINARY) { binaryValue = dictValue->GetBinary(key); if (binaryValue->GetSize() == sizeof(pyCallback)) { binaryValue->GetData(&pyCallback, sizeof(pyCallback), 0); v8FunctionHandler = new V8FunctionHandler( NULL, pyCallback.callbackId); // You must provide a function name to // CefV8Value::CreateFunction(), otherwise it fails. callbackName.append(AnyToString(pyCallback.callbackId)); success = ret->SetValue(key, CefV8Value::CreateFunction( callbackName, v8FunctionHandler), V8_PROPERTY_ATTRIBUTE_NONE); } else { DebugLog("CefListValueToV8Value(): WARNING: " \ "unknown binary value, setting value to null"); success = ret->SetValue(key, CefV8Value::CreateNull(), V8_PROPERTY_ATTRIBUTE_NONE); } } else if (valueType == VTYPE_DICTIONARY) { success = ret->SetValue(key, CefDictionaryValueToV8Value( dictValue->GetDictionary(key), nestingLevel + 1), V8_PROPERTY_ATTRIBUTE_NONE); } else if (valueType == VTYPE_LIST) { success = ret->SetValue(key, CefListValueToV8Value( dictValue->GetList(key), nestingLevel + 1), V8_PROPERTY_ATTRIBUTE_NONE); } else { DebugLog("CefDictionaryValueToV8Value(): WARNING: " \ "unknown type, setting value to null"); success = ret->SetValue(key, CefV8Value::CreateNull(), V8_PROPERTY_ATTRIBUTE_NONE); } if (!success) { DebugLog("CefDictionaryValueToV8Value(): WARNING: " \ "ret->SetValue() failed"); } } return ret; }
//----------------------------------------------------------------------------- // Purpose: //----------------------------------------------------------------------------- boost::python::dict CefDictionaryValueToPy( CefRefPtr<CefDictionaryValue> d ) { boost::python::dict result; CefDictionaryValue::KeyList keys; d->GetKeys( keys ); for( size_t i = 0; i < keys.size(); i++ ) { std::string k = keys[i].ToString(); switch( d->GetType( keys[i] ) ) { case VTYPE_INT: { result[k.c_str()] = d->GetInt( keys[i] ); break; } case VTYPE_DOUBLE: { result[k.c_str()] = d->GetDouble( keys[i] ); break; } case VTYPE_BOOL: { result[k.c_str()] = d->GetBool( keys[i] ); break; } case VTYPE_STRING: { result[k.c_str()] = d->GetString( keys[i] ).ToString().c_str(); break; } case VTYPE_BINARY: { WarsCefData_t warsCefData; CefRefPtr<CefBinaryValue> binaryData = d->GetBinary( keys[i] ); binaryData->GetData( &warsCefData, sizeof( warsCefData ), 0 ); if( warsCefData.type == WARSCEF_TYPE_JSOBJECT ) { WarsCefJSObject_t warsCefJSObject; binaryData->GetData( &warsCefJSObject, sizeof( warsCefJSObject ), 0 ); CefRefPtr<JSObject> jsResultObject = new JSObject( "", warsCefJSObject.uuid ); result[k.c_str()] = boost::python::object( PyJSObject( jsResultObject ) ); } else { result[k.c_str()] = boost::python::object(); } break; } case VTYPE_DICTIONARY: { result[k.c_str()] = CefDictionaryValueToPy( d->GetDictionary( keys[i] ) ); break; } case VTYPE_LIST: { result[k.c_str()] = CefValueListToPy( d->GetList( keys[i] ) ); break; } default: { result[k.c_str()] = d->GetString( keys[i] ).ToString().c_str(); break; } } } return result; }