2019-04-26 20:10:15 +10:00
|
|
|
// Copyright © 2010-2017 The CefSharp Authors. All rights reserved.
|
2015-07-14 10:59:10 +02:00
|
|
|
//
|
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
|
#include "JavascriptAsyncMethodHandler.h"
|
2019-04-26 20:10:15 +10:00
|
|
|
#include "CefAppUnmanagedWrapper.h"
|
2015-07-14 10:59:10 +02:00
|
|
|
#include "../CefSharp.Core/Internals/Messaging/Messages.h"
|
|
|
|
|
#include "../CefSharp.Core/Internals/Serialization/Primitives.h"
|
|
|
|
|
#include "Serialization/V8Serialization.h"
|
|
|
|
|
|
|
|
|
|
using namespace CefSharp::Internals::Messaging;
|
|
|
|
|
using namespace CefSharp::Internals::Serialization;
|
|
|
|
|
|
|
|
|
|
namespace CefSharp
|
|
|
|
|
{
|
|
|
|
|
namespace Internals
|
|
|
|
|
{
|
|
|
|
|
namespace Async
|
|
|
|
|
{
|
|
|
|
|
bool JavascriptAsyncMethodHandler::Execute(const CefString& name, CefRefPtr<CefV8Value> object, const CefV8ValueList& arguments, CefRefPtr<CefV8Value>& retval, CefString& exception)
|
|
|
|
|
{
|
2015-07-14 15:09:50 +02:00
|
|
|
auto context = CefV8Context::GetCurrentContext();
|
|
|
|
|
auto browser = context->GetBrowser();
|
2019-04-26 20:10:15 +10:00
|
|
|
|
|
|
|
|
CefRefPtr<CefV8Value> promiseData;
|
|
|
|
|
CefRefPtr<CefV8Exception> promiseException;
|
|
|
|
|
|
2015-07-14 15:09:50 +02:00
|
|
|
//this will create a promise and give us the reject/resolve functions {p: Promise, res: resolve(), rej: reject()}
|
2019-04-26 20:10:15 +10:00
|
|
|
if (!context->Eval(CefAppUnmanagedWrapper::kPromiseCreatorScript, CefString(), 0, promiseData, promiseException))
|
|
|
|
|
{
|
|
|
|
|
exception = promiseException->GetMessage();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-14 15:09:50 +02:00
|
|
|
retval = promiseData->GetValue("p");
|
|
|
|
|
|
|
|
|
|
auto resolve = promiseData->GetValue("res");
|
|
|
|
|
auto reject = promiseData->GetValue("rej");
|
|
|
|
|
auto callback = gcnew JavascriptAsyncMethodCallback(context, resolve, reject);
|
|
|
|
|
auto callbackId = _methodCallbackSave->Invoke(callback);
|
|
|
|
|
|
2015-07-29 11:11:09 +02:00
|
|
|
auto request = CefProcessMessage::Create(kJavascriptAsyncMethodCallRequest);
|
2015-07-14 10:59:10 +02:00
|
|
|
auto argList = request->GetArgumentList();
|
|
|
|
|
auto params = CefListValue::Create();
|
|
|
|
|
for (auto i = 0; i < arguments.size(); i++)
|
|
|
|
|
{
|
|
|
|
|
SerializeV8Object(arguments[i], params, i, _callbackRegistry);
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-02 19:20:54 +10:00
|
|
|
SetInt64(argList, 0, context->GetFrame()->GetIdentifier());
|
|
|
|
|
SetInt64(argList, 1, _objectId);
|
|
|
|
|
SetInt64(argList, 2, callbackId);
|
2015-10-11 20:41:17 +10:00
|
|
|
argList->SetString(3, name);
|
|
|
|
|
argList->SetList(4, params);
|
2015-07-14 10:59:10 +02:00
|
|
|
|
2015-07-14 15:09:50 +02:00
|
|
|
browser->SendProcessMessage(CefProcessId::PID_BROWSER, request);
|
2015-07-29 13:05:34 +02:00
|
|
|
|
2015-07-14 10:59:10 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|