From f90fee450b1becaabb9d5897b5bb0b51e898f9ac Mon Sep 17 00:00:00 2001 From: amaitland Date: Fri, 26 Apr 2019 20:10:15 +1000 Subject: [PATCH] JSB - Remove promise creator Extension Backport changes from https://github.com/cefsharp/CefSharp/commit/7450d0d51b1d8bfbe7f3aedc7ce8fe3c96e83c03 Add .editorconfig to fix formatting --- .editorconfig | 151 ++++++++++++++++++ .../Async/JavascriptAsyncMethodHandler.cpp | 15 +- .../Async/JavascriptAsyncMethodHandler.h | 8 +- .../Async/JavascriptAsyncMethodWrapper.h | 8 +- .../Async/JavascriptAsyncObjectWrapper.cpp | 8 +- .../Async/JavascriptAsyncObjectWrapper.h | 6 +- .../CefAppUnmanagedWrapper.cpp | 31 ++-- .../CefAppUnmanagedWrapper.h | 6 +- .../JavascriptRootObjectWrapper.cpp | 7 +- CefSharp3.sln | 1 + 10 files changed, 198 insertions(+), 43 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 000000000..98a1e4ffa --- /dev/null +++ b/.editorconfig @@ -0,0 +1,151 @@ +# editorconfig.org +# Mostly based on https://github.com/dotnet/corefx/blob/master/.editorconfig +# References +# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference?view=vs-2017 +# https://kent-boogaart.com/blog/editorconfig-reference-for-c-developers + +# top-most EditorConfig file +root = true + +# Default settings: +# A newline ending every file +# Use 4 spaces as indentation +[*] +insert_final_newline = true +indent_style = space +indent_size = 4 +charset = utf-8 +end_of_line = crlf +trim_trailing_whitespace = true + +# Powershell files (build.ps1) +[*.ps1] +charset = utf-8-bom + +# Xml config files +[*.{props,targets,config,nuspec,manifest}] +indent_size = 2 + +# C++ Files +[*.{cpp,h,in}] +curly_bracket_next_line = true +indent_brace_style = Allman + +[*.cs] +# Capitalization styles +dotnet_naming_style.constant_field_case_style.capitalization = pascal_case +dotnet_naming_style.property_case_style.capitalization = pascal_case +dotnet_naming_style.static_field_case_style.capitalization = pascal_case +dotnet_naming_style.private_internal_field_case_style.capitalization = camel_case + +# New line preferences +csharp_new_line_before_open_brace = all +csharp_new_line_before_else = true +csharp_new_line_before_catch = true +csharp_new_line_before_finally = true +csharp_new_line_before_members_in_object_initializers = true +csharp_new_line_before_members_in_anonymous_types = true +csharp_new_line_between_query_expression_clauses = true + +# Indentation preferences +csharp_indent_block_contents = true +csharp_indent_braces = false +csharp_indent_case_contents = true +csharp_indent_case_contents_when_block = false +csharp_indent_switch_labels = true +csharp_indent_labels = one_less_than_current + +# this. +dotnet_style_qualification_for_field = false : suggestion +dotnet_style_qualification_for_property = false : suggestion +dotnet_style_qualification_for_method = false : suggestion +dotnet_style_qualification_for_event = false : suggestion + +# Prefer using var +csharp_style_var_for_built_in_types = true : none +csharp_style_var_when_type_is_apparent = true : suggestion +csharp_style_var_elsewhere = true : suggestion + +# use language keywords instead of BCL types +dotnet_style_predefined_type_for_locals_parameters_members = true : suggestion +dotnet_style_predefined_type_for_member_access = true : suggestion + +# Constant fields +dotnet_naming_rule.constant_field_style.severity = error +dotnet_naming_rule.constant_field_style.symbols = constant_field_target +dotnet_naming_rule.constant_field_style.style = constant_field_case_style + +dotnet_naming_symbols.constant_field_target.applicable_kinds = field +dotnet_naming_symbols.constant_field_target.required_modifiers = const + +# Properties +dotnet_naming_rule.property_style.severity = error +dotnet_naming_rule.property_style.symbols = property_target +dotnet_naming_rule.property_style.style = property_case_style + +dotnet_naming_symbols.property_target.applicable_kinds = property +dotnet_naming_symbols.property_target.required_modifiers = * + +# Static fields +dotnet_naming_rule.static_field_style.severity = error +dotnet_naming_rule.static_field_style.symbols = static_field_target +dotnet_naming_rule.static_field_style.style = static_field_case_style + +dotnet_naming_symbols.static_field_target.applicable_kinds = field +dotnet_naming_symbols.static_field_target.required_modifiers = static + +# Private and internal fields +dotnet_naming_rule.private_internal_field_style.severity = error +dotnet_naming_rule.private_internal_field_style.symbols = private_internal_field_target +dotnet_naming_rule.private_internal_field_style.style = private_internal_field_case_style + +dotnet_naming_symbols.private_internal_field_target.applicable_kinds = field +dotnet_naming_symbols.private_internal_field_target.applicable_accessibilities = private, internal + +# Code style defaults +dotnet_sort_system_directives_first = true +csharp_preserve_single_line_blocks = true +csharp_preserve_single_line_statements = false +dotnet_style_prefer_auto_properties = true : suggestion + +# Expression-level preferences +dotnet_style_object_initializer = true : suggestion +dotnet_style_collection_initializer = true : suggestion +dotnet_style_explicit_tuple_names = false : suggestion +dotnet_style_coalesce_expression = false : suggestion +dotnet_style_null_propagation = false : suggestion + +# Expression-bodied members +csharp_style_expression_bodied_methods = false : none +csharp_style_expression_bodied_constructors = false : none +csharp_style_expression_bodied_operators = false : none +csharp_style_expression_bodied_properties = false : none +csharp_style_expression_bodied_indexers = false : none +csharp_style_expression_bodied_accessors = false : none + +# Space preferences +csharp_space_after_cast = false +csharp_space_after_colon_in_inheritance_clause = true +csharp_space_after_comma = true +csharp_space_after_dot = false +csharp_space_after_keywords_in_control_flow_statements = true +csharp_space_after_semicolon_in_for_statement = true +csharp_space_around_binary_operators = before_and_after +csharp_space_around_declaration_statements = do_not_ignore +csharp_space_before_colon_in_inheritance_clause = true +csharp_space_before_comma = false +csharp_space_before_dot = false +csharp_space_before_open_square_brackets = false +csharp_space_before_semicolon_in_for_statement = false +csharp_space_between_empty_square_brackets = false +csharp_space_between_method_call_empty_parameter_list_parentheses = false +csharp_space_between_method_call_name_and_opening_parenthesis = false +csharp_space_between_method_call_parameter_list_parentheses = false +csharp_space_between_method_declaration_empty_parameter_list_parentheses = false +csharp_space_between_method_declaration_name_and_open_parenthesis = false +csharp_space_between_method_declaration_parameter_list_parentheses = false +csharp_space_between_parentheses = false +csharp_space_between_square_brackets = false + +# Modifier order +csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async : error diff --git a/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncMethodHandler.cpp b/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncMethodHandler.cpp index 94ba72f77..0ab2dc991 100644 --- a/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncMethodHandler.cpp +++ b/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncMethodHandler.cpp @@ -1,9 +1,10 @@ -// Copyright © 2010-2017 The CefSharp Authors. All rights reserved. +// Copyright © 2010-2017 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. #include "stdafx.h" #include "JavascriptAsyncMethodHandler.h" +#include "CefAppUnmanagedWrapper.h" #include "../CefSharp.Core/Internals/Messaging/Messages.h" #include "../CefSharp.Core/Internals/Serialization/Primitives.h" #include "Serialization/V8Serialization.h" @@ -21,8 +22,18 @@ namespace CefSharp { auto context = CefV8Context::GetCurrentContext(); auto browser = context->GetBrowser(); + + CefRefPtr promiseData; + CefRefPtr promiseException; + //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()); + if (!context->Eval(CefAppUnmanagedWrapper::kPromiseCreatorScript, CefString(), 0, promiseData, promiseException)) + { + exception = promiseException->GetMessage(); + + return true; + } + 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 dc309d9f1..2f87e3f42 100644 --- a/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncMethodHandler.h +++ b/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncMethodHandler.h @@ -1,4 +1,4 @@ -// Copyright © 2010-2017 The CefSharp Authors. All rights reserved. +// Copyright © 2010-2017 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. @@ -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 b3fae3b28..4b72db0ae 100644 --- a/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncMethodWrapper.h +++ b/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncMethodWrapper.h @@ -1,4 +1,4 @@ -// Copyright © 2010-2017 The CefSharp Authors. All rights reserved. +// Copyright © 2010-2017 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. @@ -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)) { } @@ -29,4 +29,4 @@ namespace CefSharp }; } } -} \ No newline at end of file +} diff --git a/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncObjectWrapper.cpp b/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncObjectWrapper.cpp index e0eed5b1e..135c903da 100644 --- a/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncObjectWrapper.cpp +++ b/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncObjectWrapper.cpp @@ -1,4 +1,4 @@ -// Copyright © 2010-2017 The CefSharp Authors. All rights reserved. +// Copyright © 2010-2017 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. @@ -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); @@ -32,4 +32,4 @@ namespace CefSharp } } } -} \ No newline at end of file +} diff --git a/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncObjectWrapper.h b/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncObjectWrapper.h index 958735013..d63dcd764 100644 --- a/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncObjectWrapper.h +++ b/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncObjectWrapper.h @@ -1,4 +1,4 @@ -// Copyright © 2010-2017 The CefSharp Authors. All rights reserved. +// Copyright © 2010-2017 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. @@ -38,8 +38,8 @@ namespace CefSharp } } - void Bind(JavascriptObject^ object, const CefRefPtr &value, const CefRefPtr &promiseCreator); + void Bind(JavascriptObject^ object, const CefRefPtr &value); }; } } -} \ No newline at end of file +} diff --git a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp index 0de6f4ad0..625b5a862 100644 --- a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp +++ b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp @@ -1,4 +1,4 @@ -// Copyright © 2010-2017 The CefSharp Authors. All rights reserved. +// Copyright © 2010-2017 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 @@ -22,16 +22,16 @@ using namespace CefSharp::Internals::Serialization; namespace CefSharp { - const CefString CefAppUnmanagedWrapper::kPromiseCreatorFunction = "cefsharp_CreatePromise"; const CefString CefAppUnmanagedWrapper::kPromiseCreatorScript = "" - "function cefsharp_CreatePromise() {" + "(function()" + "{" " var result = {};" " var promise = new Promise(function(resolve, reject) {" " result.res = resolve; result.rej = reject;" " });" " result.p = promise;" " return result;" - "}"; + "})();"; CefRefPtr CefAppUnmanagedWrapper::GetRenderProcessHandler() { @@ -114,7 +114,7 @@ namespace CefSharp auto browserWrapper = FindBrowserWrapper(browser->GetIdentifier(), true); auto rootObjectWrappers = browserWrapper->JavascriptRootObjectWrappers; - + JavascriptRootObjectWrapper^ wrapper; if (rootObjectWrappers->TryRemove(frame->GetIdentifier(), wrapper)) { @@ -227,7 +227,7 @@ namespace CefSharp return true; } - + //these messages are roughly handled the same way if (name == kEvaluateJavascriptRequest || name == kJavascriptCallbackRequest) { @@ -254,7 +254,7 @@ namespace CefSharp JavascriptRootObjectWrapper^ rootObjectWrapper; browserWrapper->JavascriptRootObjectWrappers->TryGetValue(frameId, rootObjectWrapper); - + //NOTE: In the rare case when when OnContextCreated hasn't been called we need to manually create the rootObjectWrapper //It appears that OnContextCreated is only called for pages that have javascript on them, which makes sense //as without javascript there is no need for a context. @@ -275,14 +275,14 @@ namespace CefSharp if (frame.get()) { auto context = frame->GetV8Context(); - + if (context.get() && context->Enter()) { try { CefRefPtr exception; success = context->Eval(script, scriptUrl, startLine, result, exception); - + //we need to do this here to be able to store the v8context if (success) { @@ -331,14 +331,14 @@ namespace CefSharp { auto context = callbackWrapper->GetContext(); auto value = callbackWrapper->GetValue(); - + if (context.get() && context->Enter()) { try { auto parameterList = argList->GetList(3); CefV8ValueList params; - + //Needs to be called within the context as for Dictionary (mapped to struct) //a V8Object will be created for (CefV8ValueList::size_type i = 0; i < parameterList->GetSize(); i++) @@ -348,7 +348,7 @@ namespace CefSharp result = value->ExecuteFunction(nullptr, params); success = result.get() != nullptr; - + //we need to do this here to be able to store the v8context if (success) { @@ -418,7 +418,7 @@ namespace CefSharp if (rootObjectWrapper->TryGetAndRemoveMethodCallback(callbackId, callback)) { - try + try { auto frame = browser->GetFrame(frameId); if (frame.get()) @@ -484,9 +484,6 @@ namespace CefSharp void CefAppUnmanagedWrapper::OnWebKitInitialized() { - //we need to do this because the builtin Promise object is not accesible - CefRegisterExtension("cefsharp/promisecreator", kPromiseCreatorScript, NULL); - for each(CefExtension^ extension in _extensions->AsReadOnly()) { //only support extensions without handlers now @@ -501,4 +498,4 @@ namespace CefSharp registrar->AddCustomScheme(StringUtils::ToNative(scheme->SchemeName), scheme->IsStandard, scheme->IsLocal, scheme->IsDisplayIsolated, scheme->IsSecure, scheme->IsCorsEnabled); } } -} \ No newline at end of file +} diff --git a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h index 28d5b2e93..aa2f72121 100644 --- a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h +++ b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h @@ -1,4 +1,4 @@ -// Copyright © 2010-2017 The CefSharp Authors. All rights reserved. +// Copyright © 2010-2017 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. @@ -18,8 +18,6 @@ namespace CefSharp private class CefAppUnmanagedWrapper : CefApp, CefRenderProcessHandler { private: - static const CefString kPromiseCreatorScript; - gcroot^> _onBrowserCreated; gcroot^> _onBrowserDestroyed; gcroot^> _browserWrappers; @@ -34,7 +32,7 @@ namespace CefSharp gcroot _javascriptRootObject; public: - static const CefString kPromiseCreatorFunction; + static const CefString kPromiseCreatorScript; CefAppUnmanagedWrapper(List^ schemes, bool enableFocusedNodeChanged, Action^ onBrowserCreated, Action^ onBrowserDestoryed) { diff --git a/CefSharp.BrowserSubprocess.Core/JavascriptRootObjectWrapper.cpp b/CefSharp.BrowserSubprocess.Core/JavascriptRootObjectWrapper.cpp index 4f5c6c5e5..1abcab368 100644 --- a/CefSharp.BrowserSubprocess.Core/JavascriptRootObjectWrapper.cpp +++ b/CefSharp.BrowserSubprocess.Core/JavascriptRootObjectWrapper.cpp @@ -1,4 +1,4 @@ -// Copyright © 2010-2017 The CefSharp Authors. All rights reserved. +// Copyright © 2010-2017 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. @@ -37,11 +37,10 @@ namespace CefSharp { auto memberObjects = asyncRootObject->MemberObjects; auto saveMethod = gcnew Func(this, &JavascriptRootObjectWrapper::SaveMethodCallback); - auto promiseCreator = v8Value->GetValue(CefAppUnmanagedWrapper::kPromiseCreatorFunction); for each (JavascriptObject^ obj in Enumerable::OfType(memberObjects)) { auto wrapperObject = gcnew JavascriptAsyncObjectWrapper(_callbackRegistry, saveMethod); - wrapperObject->Bind(obj, v8Value, promiseCreator); + wrapperObject->Bind(obj, v8Value); _wrappedAsyncObjects->Add(wrapperObject); } @@ -75,4 +74,4 @@ namespace CefSharp } return result; } -} \ No newline at end of file +} diff --git a/CefSharp3.sln b/CefSharp3.sln index 3fd807eeb..d852ad610 100644 --- a/CefSharp3.sln +++ b/CefSharp3.sln @@ -13,6 +13,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CefSharp.Wpf.Example", "Cef EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{47D0DC8C-D707-4D8A-A578-7737AA9CE79B}" ProjectSection(SolutionItems) = preProject + .editorconfig = .editorconfig appveyor.yml = appveyor.yml CefSharp.Native.props = CefSharp.Native.props CefSharp.props = CefSharp.props