2018-09-10 02:41:13 +02:00
|
|
|
// Copyright © 2015 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"
|
2020-12-16 10:47:34 +10:00
|
|
|
#include "../CefSharp.Core.Runtime/Internals/Messaging/Messages.h"
|
|
|
|
|
#include "../CefSharp.Core.Runtime/Internals/Serialization/Primitives.h"
|
2015-07-14 10:59:10 +02:00
|
|
|
#include "Serialization/V8Serialization.h"
|
2018-05-15 15:05:29 +10:00
|
|
|
#include "CefAppUnmanagedWrapper.h"
|
2015-07-14 10:59:10 +02:00
|
|
|
|
|
|
|
|
using namespace CefSharp::Internals::Messaging;
|
|
|
|
|
using namespace CefSharp::Internals::Serialization;
|
2021-02-27 12:11:35 +10:00
|
|
|
using namespace CefSharp::BrowserSubprocess::Serialization;
|
2015-07-14 10:59:10 +02:00
|
|
|
|
|
|
|
|
namespace CefSharp
|
|
|
|
|
{
|
2021-02-27 12:11:35 +10:00
|
|
|
namespace BrowserSubprocess
|
2015-07-14 10:59:10 +02:00
|
|
|
{
|
|
|
|
|
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();
|
2019-06-11 12:29:57 +10:00
|
|
|
auto frame = context->GetFrame();
|
2018-05-15 15:05:29 +10:00
|
|
|
|
2018-08-27 13:12:55 +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()}
|
2018-08-27 13:12:55 +10:00
|
|
|
if (!context->Eval(CefAppUnmanagedWrapper::kPromiseCreatorScript, CefString(), 0, promiseData, promiseException))
|
|
|
|
|
{
|
|
|
|
|
LOG(WARNING) << "JavascriptAsyncMethodHandler::Execute promiseData returned exception: " + promiseException->GetMessage().ToString();
|
|
|
|
|
|
|
|
|
|
exception = promiseException->GetMessage();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//when refreshing the browser this is sometimes null, in this case return true and log message
|
|
|
|
|
//https://github.com/cefsharp/CefSharp/pull/2446
|
2021-07-31 15:35:38 +10:00
|
|
|
if (promiseData == nullptr)
|
2018-07-09 20:31:15 +10:00
|
|
|
{
|
2021-07-31 15:35:38 +10:00
|
|
|
LOG(WARNING) << "JavascriptAsyncMethodHandler::Execute promiseData returned nullptr";
|
2018-07-09 20:31:15 +10:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-07-07 11:19:02 -05:00
|
|
|
|
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();
|
2020-08-26 02:13:13 -07:00
|
|
|
for (size_t i = 0; i < arguments.size(); i++)
|
2015-07-14 10:59:10 +02:00
|
|
|
{
|
|
|
|
|
SerializeV8Object(arguments[i], params, i, _callbackRegistry);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-13 22:18:01 +10:00
|
|
|
SetInt64(argList, 0, _objectId);
|
|
|
|
|
SetInt64(argList, 1, callbackId);
|
|
|
|
|
argList->SetString(2, name);
|
|
|
|
|
argList->SetList(3, params);
|
2015-07-14 10:59:10 +02:00
|
|
|
|
2019-06-11 12:29:57 +10:00
|
|
|
frame->SendProcessMessage(CefProcessId::PID_BROWSER, request);
|
2015-07-29 13:05:34 +02:00
|
|
|
|
2015-07-14 10:59:10 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|