How to convert CefValue to CefV8Value

Do not post support requests, bug reports or feature requests. Discuss CEF here. Non-CEF related discussion goes in General Discussion!

How to convert CefValue to CefV8Value

Postby erinus » Fri Dec 30, 2016 10:10 pm

I retrive CefValue (by CefParseJSON) in CefUrlRequestClient.OnRequestComplete. I want to convert it to CefV8Value, then pass it into JS Function Callback (CefV8Value). Is there some conversion could do that ?

Thank you.

[main.html]
Code: Select all
mycefobj.GetHttpJson(function (res) {
  alert(res.ip);
});


[myapp.cc]
Code: Select all
bool V8Handler::Execute(...) {
  ...
  if (name == "GetHttpJson") {
    CefRefPtr<CefV8Value> onrequestcomplete = arguments[0].get();
    CefRefPtr<CefRequestContext> context = CefRequestContext::GetGlobalContext();
    CefRefPtr<CefRequest> req = CefRequest::Create();
    req->SetMethod("GET");
    req->SetURL("http://ip.jsontest.com/");
    CefRefPtr<POSURLRequestClient> client = new POSURLRequestClient ();
    client->SetRequestCompleteCallback(onrequestcomplete, CefV8Context::GetCurrentContext());
    CefURLRequest::Create(req, client, NULL);
    retval = CefV8Value::CreateNull();
    return true;
  ...
}


[myrequest.cc]
Code: Select all
void URLRequestClient::OnRequestComplete(CefRefPtr<CefURLRequest> request) {
  CEF_REQUIRE_UI_THREAD();
  CefRefPtr<CefValue> json = CefParseJSON(buffer.data(), JSON_PARSER_ALLOW_TRAILING_COMMAS);
  if (json->IsValid()) {
    OnRequestCompleteCallbackContext->Enter();
    CefV8ValueList args;
    args.push_back(json);  // I want to make conversion here
    OnRequestCompleteCallback->ExecuteFunctionWithContext(OnRequestCompleteCallbackContext, NULL, args);
    OnRequestCompleteCallbackContext->Exit();
  }
}
erinus
Newbie
 
Posts: 4
Joined: Fri Dec 30, 2016 9:59 pm

Re: How to convert CefValue to CefV8Value

Postby erinus » Sun Jan 01, 2017 8:59 am

answered by myself

Code: Select all
CefRefPtr<CefV8Value> CefValueToCefV8Value(CefRefPtr<CefValue> value) {
  CefRefPtr<CefV8Value> result;
  switch (value->GetType()) {
    case VTYPE_INVALID:
      {
        //std::cout << "Type: VTYPE_INVALID" << std::endl;
        result = CefV8Value::CreateNull();
      }
      break;
    case VTYPE_NULL:
      {
        //std::cout << "Type: VTYPE_NULL" << std::endl;
        result = CefV8Value::CreateNull();
      }
      break;
    case VTYPE_BOOL:
      {
        //std::cout << "Type: VTYPE_BOOL" << std::endl;
        result = CefV8Value::CreateBool(value->GetBool());
      }
      break;
    case VTYPE_INT:
      {
        //std::cout << "Type: VTYPE_INT" << std::endl;
        result = CefV8Value::CreateInt(value->GetInt());
      }
      break;
    case VTYPE_DOUBLE:
      {
        //std::cout << "Type: VTYPE_DOUBLE" << std::endl;
        result = CefV8Value::CreateDouble(value->GetDouble());
      }
      break;
    case VTYPE_STRING:
      {
        //std::cout << "Type: VTYPE_STRING" << std::endl;
        result = CefV8Value::CreateString(value->GetString());
      }
      break;
    case VTYPE_BINARY:
      {
        //std::cout << "Type: VTYPE_BINARY" << std::endl;
        result = CefV8Value::CreateNull();
      }
      break;
    case VTYPE_DICTIONARY:
      {
        //std::cout << "Type: VTYPE_DICTIONARY" << std::endl;
        result = CefV8Value::CreateObject(NULL, NULL);
        CefRefPtr<CefDictionaryValue> dict = value->GetDictionary();
        CefDictionaryValue::KeyList keys;
        dict->GetKeys(keys);
        for (unsigned int i = 0; i < keys.size(); i++) {
          CefString key = keys[i];
          result->SetValue(key, CefValueToCefV8Value(dict->GetValue(key)), V8_PROPERTY_ATTRIBUTE_NONE);
        }
      }
      break;
    case VTYPE_LIST:
      {
        //std::cout << "Type: VTYPE_LIST" << std::endl;
        CefRefPtr<CefListValue> list = value->GetList();
        int size = list->GetSize();
        result = CefV8Value::CreateArray(size);
        for (int i = 0; i < size; i++) {
          result->SetValue(i, CefValueToCefV8Value(list->GetValue(i)));
        }
      }
      break;
  }
  return result;
}
erinus
Newbie
 
Posts: 4
Joined: Fri Dec 30, 2016 9:59 pm


Return to CEF Discussion

Who is online

Users browsing this forum: No registered users and 6 guests