mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-04 10:22:55 +00:00
deca49517b
- Add useImmediatelyInvokedFuncExpression param to EvaluateScriptAsync which wraps the script in an IIFE with the cefSharpInternalCallbackId variable defined in scope to allow for deferred completion - Wrap script in Immediately Invoked Function Expression and call promise.resolve to guarantee a promise. - Return 'CefSharpDefEvalScriptRes' as the result of the js function to defer execution of the callback - Call newly added cefSharp.sendEvalScriptResponse method in javascript when promise has been resolved/rejected This is built up of a few parts and can technically be used by the new user to defer the response until they are ready. Just return `'CefSharpDefEvalScriptRes'` and manually call cefSharp.sendEvalScriptResponse using the cefSharpInternalCallbackId variable which is injected into the script execution (wrapped in an IIFE to limit scope)
86 lines
2.5 KiB
C++
86 lines
2.5 KiB
C++
// Copyright © 2020 The CefSharp Authors. All rights reserved.
|
|
//
|
|
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
|
|
|
#pragma once
|
|
|
|
#include "include/cef_v8.h"
|
|
#include "..\CefSharp.Core\Internals\Messaging\Messages.h"
|
|
#include "..\CefSharp.Core\Internals\Serialization\Primitives.h"
|
|
#include "Serialization\V8Serialization.h"
|
|
|
|
using namespace System;
|
|
using namespace CefSharp::Internals::Messaging;
|
|
using namespace CefSharp::Internals::Serialization;
|
|
|
|
namespace CefSharp
|
|
{
|
|
const CefString kSendEvalScriptResponse = CefString("SendEvalScriptResponse");
|
|
const CefString kSendEvalScriptResponseCamelCase = CefString("sendEvalScriptResponse");
|
|
|
|
private class JavascriptPromiseHandler : public CefV8Handler
|
|
{
|
|
public:
|
|
|
|
virtual bool Execute(const CefString& name,
|
|
CefRefPtr<CefV8Value> object,
|
|
const CefV8ValueList& arguments,
|
|
CefRefPtr<CefV8Value>& retval,
|
|
CefString& exception)
|
|
{
|
|
if (arguments.size() < 2)
|
|
{
|
|
//TODO: Improve error message
|
|
exception = "Must specify a callback Id and then/catch.";
|
|
|
|
return true;
|
|
}
|
|
|
|
auto callbackId = arguments[0]->GetIntValue();
|
|
|
|
if (callbackId == 0)
|
|
{
|
|
exception = "Invalid callback Id";
|
|
|
|
return true;
|
|
}
|
|
|
|
auto success = arguments[1]->GetBoolValue();
|
|
|
|
auto response = CefProcessMessage::Create(kEvaluateJavascriptResponse);
|
|
|
|
auto responseArgList = response->GetArgumentList();
|
|
//Success
|
|
responseArgList->SetBool(0, success);
|
|
//Callback Id
|
|
SetInt64(responseArgList, 1, callbackId);
|
|
if (exception == "")
|
|
{
|
|
if (success)
|
|
{
|
|
SerializeV8Object(arguments[2], responseArgList, 2, nullptr);
|
|
}
|
|
else
|
|
{
|
|
auto reason = arguments[2];
|
|
responseArgList->SetString(2, reason->GetStringValue());
|
|
}
|
|
}
|
|
else
|
|
{
|
|
responseArgList->SetString(2, exception);
|
|
}
|
|
|
|
auto context = CefV8Context::GetCurrentContext();
|
|
|
|
auto frame = context->GetFrame();
|
|
|
|
frame->SendProcessMessage(CefProcessId::PID_BROWSER, response);
|
|
|
|
return false;
|
|
}
|
|
|
|
IMPLEMENT_REFCOUNTING(JavascriptPromiseHandler);
|
|
};
|
|
}
|