mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-06 12:09:40 +00:00
Replaced kPromiseCreatorScript with native promise
This commit is contained in:
committed by
Alex Maitland
parent
e9bc7f3c02
commit
e3012e8ecb
@@ -13,13 +13,11 @@ namespace CefSharp
|
||||
{
|
||||
void JavascriptAsyncMethodCallback::Success(const CefRefPtr<CefV8Value>& result)
|
||||
{
|
||||
if (_resolve.get() && _context.get() && _context->Enter())
|
||||
if (_promise.get() && _context.get() && _context->Enter())
|
||||
{
|
||||
try
|
||||
{
|
||||
CefV8ValueList args;
|
||||
args.push_back(result);
|
||||
_resolve->ExecuteFunction(nullptr, args);
|
||||
_promise->ResolvePromise(result);
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -30,13 +28,11 @@ namespace CefSharp
|
||||
|
||||
void JavascriptAsyncMethodCallback::Fail(const CefString& exception)
|
||||
{
|
||||
if (_reject.get() && _context.get() && _context->Enter())
|
||||
if (_promise.get() && _context.get() && _context->Enter())
|
||||
{
|
||||
try
|
||||
{
|
||||
CefV8ValueList args;
|
||||
args.push_back(CefV8Value::CreateString(exception));
|
||||
_reject->ExecuteFunction(nullptr, args);
|
||||
_promise->RejectPromise(exception);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
||||
@@ -16,21 +16,19 @@ namespace CefSharp
|
||||
{
|
||||
private:
|
||||
MCefRefPtr<CefV8Context> _context;
|
||||
MCefRefPtr<CefV8Value> _resolve;
|
||||
MCefRefPtr<CefV8Value> _reject;
|
||||
MCefRefPtr<CefV8Value> _promise;
|
||||
|
||||
public:
|
||||
JavascriptAsyncMethodCallback(CefRefPtr<CefV8Context> context, CefRefPtr<CefV8Value> resolve, CefRefPtr<CefV8Value> reject)
|
||||
:_context(context), _resolve(resolve.get()), _reject(reject.get())
|
||||
JavascriptAsyncMethodCallback(CefRefPtr<CefV8Context> context, CefRefPtr<CefV8Value> promise)
|
||||
:_context(context), _promise(promise.get())
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
!JavascriptAsyncMethodCallback()
|
||||
{
|
||||
_resolve = nullptr;
|
||||
_reject = nullptr;
|
||||
_context = nullptr;
|
||||
_promise = nullptr;
|
||||
}
|
||||
|
||||
~JavascriptAsyncMethodCallback()
|
||||
|
||||
@@ -24,32 +24,10 @@ namespace CefSharp
|
||||
auto context = CefV8Context::GetCurrentContext();
|
||||
auto frame = context->GetFrame();
|
||||
|
||||
CefRefPtr<CefV8Value> promiseData;
|
||||
CefRefPtr<CefV8Exception> promiseException;
|
||||
//this will create a promise and give us the reject/resolve functions {p: Promise, res: resolve(), rej: reject()}
|
||||
if (!context->Eval(CefAppUnmanagedWrapper::kPromiseCreatorScript, CefString(), 0, promiseData, promiseException))
|
||||
{
|
||||
LOG(WARNING) << "JavascriptAsyncMethodHandler::Execute promiseData returned exception: " + promiseException->GetMessage().ToString();
|
||||
CefRefPtr<CefV8Value> promise = CefV8Value::CreatePromise();
|
||||
retval = promise;
|
||||
|
||||
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
|
||||
if (promiseData == nullptr)
|
||||
{
|
||||
LOG(WARNING) << "JavascriptAsyncMethodHandler::Execute promiseData returned nullptr";
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
retval = promiseData->GetValue("p");
|
||||
|
||||
auto resolve = promiseData->GetValue("res");
|
||||
auto reject = promiseData->GetValue("rej");
|
||||
auto callback = gcnew JavascriptAsyncMethodCallback(context, resolve, reject);
|
||||
auto callback = gcnew JavascriptAsyncMethodCallback(context, promise);
|
||||
auto callbackId = _methodCallbackSave->Invoke(callback);
|
||||
|
||||
auto request = CefProcessMessage::Create(kJavascriptAsyncMethodCallRequest);
|
||||
|
||||
@@ -154,54 +154,15 @@ namespace CefSharp
|
||||
rootObject->Bind(cachedObjects, context->GetGlobal());
|
||||
|
||||
//Objects already bound or ignore cache
|
||||
CefRefPtr<CefV8Value> promiseResolve;
|
||||
CefRefPtr<CefV8Exception> promiseException;
|
||||
|
||||
auto promiseResolveScript = StringUtils::ToNative("Promise.resolve({Success:true, Count:" + cachedObjects->Count + ", Message:'OK'});");
|
||||
|
||||
if (context->Eval(promiseResolveScript, CefString(), 0, promiseResolve, promiseException))
|
||||
{
|
||||
retval = promiseResolve;
|
||||
}
|
||||
else
|
||||
{
|
||||
exception = promiseException->GetMessage();
|
||||
|
||||
return true;
|
||||
}
|
||||
retval = CreateResultObject(cachedObjects->Count, "OK", true);
|
||||
|
||||
NotifyObjectBound(frame, objectNamesWithBoundStatus);
|
||||
}
|
||||
else
|
||||
{
|
||||
CefRefPtr<CefV8Value> promiseData;
|
||||
CefRefPtr<CefV8Exception> promiseException;
|
||||
//this will create a promise and give us the reject/resolve functions {p: Promise, res: resolve(), rej: reject()}
|
||||
if (!context->Eval(CefAppUnmanagedWrapper::kPromiseCreatorScript, CefString(), 0, promiseData, promiseException))
|
||||
{
|
||||
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
|
||||
if (promiseData == nullptr)
|
||||
{
|
||||
LOG(WARNING) << "BindObjectAsyncHandler::Execute promiseData returned nullptr";
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//return the promose
|
||||
retval = promiseData->GetValue("p");
|
||||
|
||||
//References to the promise resolve and reject methods
|
||||
auto resolve = promiseData->GetValue("res");
|
||||
auto reject = promiseData->GetValue("rej");
|
||||
|
||||
auto callback = gcnew JavascriptAsyncMethodCallback(context, resolve, reject);
|
||||
|
||||
CefRefPtr<CefV8Value> promise = CefV8Value::CreatePromise();
|
||||
retval = promise;
|
||||
auto callback = gcnew JavascriptAsyncMethodCallback(context, promise);
|
||||
auto request = CefProcessMessage::Create(kJavascriptRootObjectRequest);
|
||||
auto argList = request->GetArgumentList();
|
||||
|
||||
@@ -217,24 +178,7 @@ namespace CefSharp
|
||||
else
|
||||
{
|
||||
//Objects already bound or ignore cache
|
||||
CefRefPtr<CefV8Value> promiseResolve;
|
||||
CefRefPtr<CefV8Exception> promiseException;
|
||||
|
||||
auto promiseResolveScript = CefString("Promise.resolve({Success:false, Count:0, Message:'Object(s) already bound'});");
|
||||
|
||||
if (context->Eval(promiseResolveScript, CefString(), 0, promiseResolve, promiseException))
|
||||
{
|
||||
retval = promiseResolve;
|
||||
|
||||
if (notifyIfAlreadyBound)
|
||||
{
|
||||
NotifyObjectBound(frame, objectNamesWithBoundStatus);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
exception = promiseException->GetMessage();
|
||||
}
|
||||
retval = CreateResultObject(0, "Object(s) already bound", false);
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -256,6 +200,17 @@ namespace CefSharp
|
||||
return true;
|
||||
}
|
||||
|
||||
static CefRefPtr<CefV8Value> CreateResultObject(int count, String^ message, bool isSuccess)
|
||||
{
|
||||
auto response = CefV8Value::CreateObject(nullptr, nullptr);
|
||||
|
||||
response->SetValue("Count", CefV8Value::CreateInt(count), CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_READONLY);
|
||||
response->SetValue("Message", CefV8Value::CreateString(StringUtils::ToNative(message)), CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_READONLY);
|
||||
response->SetValue("Success", CefV8Value::CreateBool(isSuccess), CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_READONLY);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
private:
|
||||
void NotifyObjectBound(const CefRefPtr<CefFrame> frame, List<Tuple<String^, bool, bool>^>^ objectNamesWithBoundStatus)
|
||||
{
|
||||
|
||||
@@ -36,16 +36,6 @@ namespace CefSharp
|
||||
{
|
||||
namespace BrowserSubprocess
|
||||
{
|
||||
const CefString CefAppUnmanagedWrapper::kPromiseCreatorScript = ""
|
||||
"(function()"
|
||||
"{"
|
||||
" var result = {};"
|
||||
" var promise = new Promise(function(resolve, reject) {"
|
||||
" result.res = resolve; result.rej = reject;"
|
||||
" });"
|
||||
" result.p = promise;"
|
||||
" return result;"
|
||||
"})();";
|
||||
|
||||
const CefString kRenderProcessId = CefString("RenderProcessId");
|
||||
const CefString kRenderProcessIdCamelCase = CefString("renderProcessId");
|
||||
@@ -699,23 +689,15 @@ namespace CefSharp
|
||||
|
||||
if (_registerBoundObjectRegistry->TryGetAndRemoveMethodCallback(callbackId, callback))
|
||||
{
|
||||
//Response object has no Accessor or Interceptor
|
||||
auto response = CefV8Value::CreateObject(nullptr, nullptr);
|
||||
|
||||
response->SetValue("Count", CefV8Value::CreateInt(javascriptObjects->Count), CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_READONLY);
|
||||
|
||||
if (javascriptObjects->Count > 0)
|
||||
{
|
||||
//TODO: JSB Should we include a list of successfully bound object names?
|
||||
response->SetValue("Success", CefV8Value::CreateBool(true), CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_READONLY);
|
||||
response->SetValue("Message", CefV8Value::CreateString("OK"), CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_READONLY);
|
||||
callback->Success(response);
|
||||
callback->Success(BindObjectAsyncHandler::CreateResultObject(javascriptObjects->Count, "OK", true));
|
||||
}
|
||||
else
|
||||
{
|
||||
response->SetValue("Success", CefV8Value::CreateBool(false), CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_READONLY);
|
||||
response->SetValue("Message", CefV8Value::CreateString("Zero objects bounds"), CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_READONLY);
|
||||
callback->Success(response);
|
||||
callback->Success(BindObjectAsyncHandler::CreateResultObject(javascriptObjects->Count, "Zero objects bounds", false));
|
||||
}
|
||||
|
||||
//Send message notifying Browser Process of which objects were bound
|
||||
|
||||
@@ -44,7 +44,6 @@ namespace CefSharp
|
||||
}
|
||||
|
||||
public:
|
||||
static const CefString kPromiseCreatorScript;
|
||||
|
||||
CefAppUnmanagedWrapper(IRenderProcessHandler^ handler, List<CefCustomScheme^>^ schemes, bool enableFocusedNodeChanged, Action<CefBrowserWrapper^>^ onBrowserCreated, Action<CefBrowserWrapper^>^ onBrowserDestroyed) : SubProcessApp(schemes)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user