From 5f3dca4d960f6306c3a1b03e7312a8f6d5a0e961 Mon Sep 17 00:00:00 2001 From: amaitland Date: Tue, 15 May 2018 15:05:29 +1000 Subject: [PATCH] JavascriptAsyncMethodHandler - Obtain Promise from context on Execute Previously the promise reference was passed into the handler, now we obtain the reference on demand --- .../Async/JavascriptAsyncMethodHandler.cpp | 6 +++++- .../Async/JavascriptAsyncMethodHandler.h | 6 ++---- .../Async/JavascriptAsyncMethodWrapper.h | 4 ++-- .../Async/JavascriptAsyncObjectWrapper.cpp | 4 ++-- .../Async/JavascriptAsyncObjectWrapper.h | 2 +- .../JavascriptRootObjectWrapper.cpp | 3 +-- 6 files changed, 13 insertions(+), 12 deletions(-) diff --git a/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncMethodHandler.cpp b/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncMethodHandler.cpp index 94ba72f77c..77152cb388 100644 --- a/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncMethodHandler.cpp +++ b/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncMethodHandler.cpp @@ -7,6 +7,7 @@ #include "../CefSharp.Core/Internals/Messaging/Messages.h" #include "../CefSharp.Core/Internals/Serialization/Primitives.h" #include "Serialization/V8Serialization.h" +#include "CefAppUnmanagedWrapper.h" using namespace CefSharp::Internals::Messaging; using namespace CefSharp::Internals::Serialization; @@ -21,8 +22,11 @@ namespace CefSharp { auto context = CefV8Context::GetCurrentContext(); auto browser = context->GetBrowser(); + + auto promiseCreator = context->GetGlobal()->GetValue(CefAppUnmanagedWrapper::kPromiseCreatorFunction); + //this will create a promise and give us the reject/resolve functions {p: Promise, res: resolve(), rej: reject()} - auto promiseData = _promiseCreator->ExecuteFunctionWithContext(context, nullptr, CefV8ValueList()); + auto promiseData = promiseCreator->ExecuteFunctionWithContext(context, nullptr, CefV8ValueList()); retval = promiseData->GetValue("p"); auto resolve = promiseData->GetValue("res"); diff --git a/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncMethodHandler.h b/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncMethodHandler.h index dc309d9f1b..1047f34646 100644 --- a/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncMethodHandler.h +++ b/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncMethodHandler.h @@ -19,12 +19,11 @@ namespace CefSharp private: gcroot _callbackRegistry; gcroot^> _methodCallbackSave; - CefRefPtr _promiseCreator; int64 _objectId; public: - JavascriptAsyncMethodHandler(int64 objectId, JavascriptCallbackRegistry^ callbackRegistry, CefRefPtr promiseCreator, Func^ methodCallbackSave) - :_callbackRegistry(callbackRegistry), _objectId(objectId), _promiseCreator(promiseCreator), _methodCallbackSave(methodCallbackSave) + JavascriptAsyncMethodHandler(int64 objectId, JavascriptCallbackRegistry^ callbackRegistry, Func^ methodCallbackSave) + :_callbackRegistry(callbackRegistry), _objectId(objectId), _methodCallbackSave(methodCallbackSave) { } @@ -37,7 +36,6 @@ namespace CefSharp // It's lifecycle is managed in the JavascriptRootObjectWrapper. _callbackRegistry = nullptr; _methodCallbackSave = nullptr; - _promiseCreator = NULL; } IMPLEMENT_REFCOUNTING(JavascriptAsyncMethodHandler) diff --git a/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncMethodWrapper.h b/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncMethodWrapper.h index b3fae3b280..d8e6ad9e58 100644 --- a/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncMethodWrapper.h +++ b/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncMethodWrapper.h @@ -19,8 +19,8 @@ namespace CefSharp MCefRefPtr _javascriptMethodHandler; public: - JavascriptAsyncMethodWrapper(int64 ownerId, JavascriptCallbackRegistry^ callbackRegistry, CefRefPtr promiseCreator, Func^ methodCallbackSave) - : _javascriptMethodHandler(new JavascriptAsyncMethodHandler(ownerId, callbackRegistry, promiseCreator, methodCallbackSave)) + JavascriptAsyncMethodWrapper(int64 ownerId, JavascriptCallbackRegistry^ callbackRegistry, Func^ methodCallbackSave) + : _javascriptMethodHandler(new JavascriptAsyncMethodHandler(ownerId, callbackRegistry, methodCallbackSave)) { } diff --git a/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncObjectWrapper.cpp b/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncObjectWrapper.cpp index e0eed5b1e4..da343264de 100644 --- a/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncObjectWrapper.cpp +++ b/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncObjectWrapper.cpp @@ -15,7 +15,7 @@ namespace CefSharp { namespace Async { - void JavascriptAsyncObjectWrapper::Bind(JavascriptObject^ object, const CefRefPtr &value, const CefRefPtr &promiseCreator) + void JavascriptAsyncObjectWrapper::Bind(JavascriptObject^ object, const CefRefPtr &value) { //V8Value that represents this javascript object - only one per complex type, no accessor auto javascriptObject = CefV8Value::CreateObject(nullptr, nullptr); @@ -24,7 +24,7 @@ namespace CefSharp for each (JavascriptMethod^ method in Enumerable::OfType(object->Methods)) { - auto wrappedMethod = gcnew JavascriptAsyncMethodWrapper(object->Id, _callbackRegistry, promiseCreator, _methodCallbackSave); + auto wrappedMethod = gcnew JavascriptAsyncMethodWrapper(object->Id, _callbackRegistry, _methodCallbackSave); wrappedMethod->Bind(method, javascriptObject); _wrappedMethods->Add(wrappedMethod); diff --git a/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncObjectWrapper.h b/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncObjectWrapper.h index 9587350131..047deb4cc9 100644 --- a/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncObjectWrapper.h +++ b/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncObjectWrapper.h @@ -38,7 +38,7 @@ namespace CefSharp } } - void Bind(JavascriptObject^ object, const CefRefPtr &value, const CefRefPtr &promiseCreator); + void Bind(JavascriptObject^ object, const CefRefPtr &value); }; } } diff --git a/CefSharp.BrowserSubprocess.Core/JavascriptRootObjectWrapper.cpp b/CefSharp.BrowserSubprocess.Core/JavascriptRootObjectWrapper.cpp index 3141e63e36..c5f75bb0d1 100644 --- a/CefSharp.BrowserSubprocess.Core/JavascriptRootObjectWrapper.cpp +++ b/CefSharp.BrowserSubprocess.Core/JavascriptRootObjectWrapper.cpp @@ -17,14 +17,13 @@ namespace CefSharp if (objects->Count > 0) { auto saveMethod = gcnew Func(this, &JavascriptRootObjectWrapper::SaveMethodCallback); - auto promiseCreator = v8Value->GetValue(CefAppUnmanagedWrapper::kPromiseCreatorFunction); for each (JavascriptObject^ obj in Enumerable::OfType(objects)) { if (obj->IsAsync) { auto wrapperObject = gcnew JavascriptAsyncObjectWrapper(_callbackRegistry, saveMethod); - wrapperObject->Bind(obj, v8Value, promiseCreator); + wrapperObject->Bind(obj, v8Value); _wrappedAsyncObjects->Add(wrapperObject); }