diff --git a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp index 7c95e673f..0cded658c 100644 --- a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp +++ b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp @@ -88,6 +88,8 @@ namespace CefSharp _javascriptObjects->Add(obj->JavascriptName, obj); } } + + _jsBindingApiEnabled = extraInfo->GetBool("JavascriptBindingApiEnabled"); } } @@ -135,50 +137,53 @@ namespace CefSharp } } - //TODO: Look at adding some sort of javascript mapping layer to reduce the code duplication - auto global = context->GetGlobal(); - auto browserWrapper = FindBrowserWrapper(browser->GetIdentifier()); - auto processId = System::Diagnostics::Process::GetCurrentProcess()->Id; - - //TODO: JSB: Split functions into their own classes - //Browser wrapper is only used for BindObjectAsync - auto bindObjAsyncFunction = CefV8Value::CreateFunction(kBindObjectAsync, new BindObjectAsyncHandler(_registerBoundObjectRegistry, _javascriptObjects, browserWrapper)); - auto unBindObjFunction = CefV8Value::CreateFunction(kDeleteBoundObject, new RegisterBoundObjectHandler(_javascriptObjects)); - auto removeObjectFromCacheFunction = CefV8Value::CreateFunction(kRemoveObjectFromCache, new RegisterBoundObjectHandler(_javascriptObjects)); - auto isObjectCachedFunction = CefV8Value::CreateFunction(kIsObjectCached, new RegisterBoundObjectHandler(_javascriptObjects)); - auto postMessageFunction = CefV8Value::CreateFunction(kPostMessage, new JavascriptPostMessageHandler(rootObject == nullptr ? nullptr : rootObject->CallbackRegistry)); - auto promiseHandlerFunction = CefV8Value::CreateFunction(kSendEvalScriptResponse, new JavascriptPromiseHandler()); - - //By default We'll support both CefSharp and cefSharp, for those who prefer the JS style - auto createCefSharpObj = !_jsBindingPropertyName.empty(); - auto createCefSharpObjCamelCase = !_jsBindingPropertyNameCamelCase.empty(); - - if (createCefSharpObj) + if (_jsBindingApiEnabled) { - auto cefSharpObj = CefV8Value::CreateObject(NULL, NULL); - cefSharpObj->SetValue(kBindObjectAsync, bindObjAsyncFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); - cefSharpObj->SetValue(kDeleteBoundObject, unBindObjFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); - cefSharpObj->SetValue(kRemoveObjectFromCache, removeObjectFromCacheFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); - cefSharpObj->SetValue(kIsObjectCached, isObjectCachedFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); - cefSharpObj->SetValue(kPostMessage, postMessageFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); - cefSharpObj->SetValue(kSendEvalScriptResponse, promiseHandlerFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); - cefSharpObj->SetValue(kRenderProcessId, CefV8Value::CreateInt(processId), CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); + //TODO: Look at adding some sort of javascript mapping layer to reduce the code duplication + auto global = context->GetGlobal(); + auto browserWrapper = FindBrowserWrapper(browser->GetIdentifier()); + auto processId = System::Diagnostics::Process::GetCurrentProcess()->Id; - global->SetValue(_jsBindingPropertyName, cefSharpObj, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_READONLY); - } + //TODO: JSB: Split functions into their own classes + //Browser wrapper is only used for BindObjectAsync + auto bindObjAsyncFunction = CefV8Value::CreateFunction(kBindObjectAsync, new BindObjectAsyncHandler(_registerBoundObjectRegistry, _javascriptObjects, browserWrapper)); + auto unBindObjFunction = CefV8Value::CreateFunction(kDeleteBoundObject, new RegisterBoundObjectHandler(_javascriptObjects)); + auto removeObjectFromCacheFunction = CefV8Value::CreateFunction(kRemoveObjectFromCache, new RegisterBoundObjectHandler(_javascriptObjects)); + auto isObjectCachedFunction = CefV8Value::CreateFunction(kIsObjectCached, new RegisterBoundObjectHandler(_javascriptObjects)); + auto postMessageFunction = CefV8Value::CreateFunction(kPostMessage, new JavascriptPostMessageHandler(rootObject == nullptr ? nullptr : rootObject->CallbackRegistry)); + auto promiseHandlerFunction = CefV8Value::CreateFunction(kSendEvalScriptResponse, new JavascriptPromiseHandler()); - if (createCefSharpObjCamelCase) - { - auto cefSharpObjCamelCase = CefV8Value::CreateObject(NULL, NULL); - cefSharpObjCamelCase->SetValue(kBindObjectAsyncCamelCase, bindObjAsyncFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); - cefSharpObjCamelCase->SetValue(kDeleteBoundObjectCamelCase, unBindObjFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); - cefSharpObjCamelCase->SetValue(kRemoveObjectFromCacheCamelCase, removeObjectFromCacheFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); - cefSharpObjCamelCase->SetValue(kIsObjectCachedCamelCase, isObjectCachedFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); - cefSharpObjCamelCase->SetValue(kPostMessageCamelCase, postMessageFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); - cefSharpObjCamelCase->SetValue(kSendEvalScriptResponseCamelCase, promiseHandlerFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); - cefSharpObjCamelCase->SetValue(kRenderProcessIdCamelCase, CefV8Value::CreateInt(processId), CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); + //By default We'll support both CefSharp and cefSharp, for those who prefer the JS style + auto createCefSharpObj = !_jsBindingPropertyName.empty(); + auto createCefSharpObjCamelCase = !_jsBindingPropertyNameCamelCase.empty(); - global->SetValue(_jsBindingPropertyNameCamelCase, cefSharpObjCamelCase, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_READONLY); + if (createCefSharpObj) + { + auto cefSharpObj = CefV8Value::CreateObject(NULL, NULL); + cefSharpObj->SetValue(kBindObjectAsync, bindObjAsyncFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); + cefSharpObj->SetValue(kDeleteBoundObject, unBindObjFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); + cefSharpObj->SetValue(kRemoveObjectFromCache, removeObjectFromCacheFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); + cefSharpObj->SetValue(kIsObjectCached, isObjectCachedFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); + cefSharpObj->SetValue(kPostMessage, postMessageFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); + cefSharpObj->SetValue(kSendEvalScriptResponse, promiseHandlerFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); + cefSharpObj->SetValue(kRenderProcessId, CefV8Value::CreateInt(processId), CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); + + global->SetValue(_jsBindingPropertyName, cefSharpObj, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_READONLY); + } + + if (createCefSharpObjCamelCase) + { + auto cefSharpObjCamelCase = CefV8Value::CreateObject(NULL, NULL); + cefSharpObjCamelCase->SetValue(kBindObjectAsyncCamelCase, bindObjAsyncFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); + cefSharpObjCamelCase->SetValue(kDeleteBoundObjectCamelCase, unBindObjFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); + cefSharpObjCamelCase->SetValue(kRemoveObjectFromCacheCamelCase, removeObjectFromCacheFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); + cefSharpObjCamelCase->SetValue(kIsObjectCachedCamelCase, isObjectCachedFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); + cefSharpObjCamelCase->SetValue(kPostMessageCamelCase, postMessageFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); + cefSharpObjCamelCase->SetValue(kSendEvalScriptResponseCamelCase, promiseHandlerFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); + cefSharpObjCamelCase->SetValue(kRenderProcessIdCamelCase, CefV8Value::CreateInt(processId), CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); + + global->SetValue(_jsBindingPropertyNameCamelCase, cefSharpObjCamelCase, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_READONLY); + } } //Send a message to the browser processing signaling that OnContextCreated has been called diff --git a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h index 32590ebfb..4275de2ac 100644 --- a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h +++ b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h @@ -26,6 +26,7 @@ namespace CefSharp gcroot^> _browserWrappers; bool _focusedNodeChangedEnabled; bool _legacyBindingEnabled; + bool _jsBindingApiEnabled = true; // The property names used to call bound objects CefString _jsBindingPropertyName; diff --git a/CefSharp.Core.Runtime/ManagedCefBrowserAdapter.cpp b/CefSharp.Core.Runtime/ManagedCefBrowserAdapter.cpp index 59313ad1f..8391965b3 100644 --- a/CefSharp.Core.Runtime/ManagedCefBrowserAdapter.cpp +++ b/CefSharp.Core.Runtime/ManagedCefBrowserAdapter.cpp @@ -41,14 +41,15 @@ namespace CefSharp } auto objectRepository = _javaScriptObjectRepository; + auto objectRepositorySettings = objectRepository->Settings; //It's no longer possible to change these settings - objectRepository->Settings->Freeze(); + objectRepositorySettings->Freeze(); CefRefPtr extraInfo = CefDictionaryValue::Create(); auto legacyBindingEnabled = false; - if (objectRepository->Settings->LegacyBindingEnabled) + if (objectRepositorySettings->LegacyBindingEnabled) { auto legacyBoundObjects = objectRepository->GetLegacyBoundObjects(); @@ -67,9 +68,9 @@ namespace CefSharp extraInfo->SetBool("LegacyBindingEnabled", legacyBindingEnabled); - if (!String::IsNullOrEmpty(objectRepository->Settings->JavascriptBindingApiGlobalObjectName)) + if (!String::IsNullOrEmpty(objectRepositorySettings->JavascriptBindingApiGlobalObjectName)) { - auto globalObjName = objectRepository->Settings->JavascriptBindingApiGlobalObjectName; + auto globalObjName = objectRepositorySettings->JavascriptBindingApiGlobalObjectName; if (StringCheck::IsFirstCharacterLowercase(globalObjName)) { @@ -81,6 +82,8 @@ namespace CefSharp } } + extraInfo->SetBool("JavascriptBindingApiEnabled", objectRepositorySettings->JavascriptBindingApiEnabled); + CefRefPtr requestCtx; if (requestContext != nullptr) diff --git a/CefSharp.Test/CefSharpFixture.cs b/CefSharp.Test/CefSharpFixture.cs index 853b3e5f1..a92788894 100644 --- a/CefSharp.Test/CefSharpFixture.cs +++ b/CefSharp.Test/CefSharpFixture.cs @@ -51,6 +51,7 @@ namespace CefSharp.Test //HTML5 databases such as localStorage will only persist across sessions if a cache path is specified. settings.CachePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CefSharp\\Tests\\Cache"); settings.RootCachePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CefSharp\\Tests"); + //settings.CefCommandLineArgs.Add("renderer-startup-dialog"); Cef.Initialize(settings, performDependencyCheck: false, browserProcessHandler: null); } diff --git a/CefSharp.Test/JavascriptBinding/IntegrationTestFacts.cs b/CefSharp.Test/JavascriptBinding/IntegrationTestFacts.cs index e8d8d0b63..b0459e460 100644 --- a/CefSharp.Test/JavascriptBinding/IntegrationTestFacts.cs +++ b/CefSharp.Test/JavascriptBinding/IntegrationTestFacts.cs @@ -140,6 +140,54 @@ namespace CefSharp.Test.JavascriptBinding } } + [Fact] + public async Task JsBindingGlobalApiDisabled() + { + using (var browser = new ChromiumWebBrowser(CefExample.BindingApiCustomObjectNameTestUrl, automaticallyCreateBrowser: false)) + { + var settings = browser.JavascriptObjectRepository.Settings; + settings.JavascriptBindingApiEnabled = false; + + //To modify the settings we need to defer browser creation slightly + browser.CreateBrowser(); + + await browser.LoadPageAsync(); + + var response1 = await browser.EvaluateScriptAsync("typeof window.cefSharp === 'undefined'"); + var response2 = await browser.EvaluateScriptAsync("typeof window.CefSharp === 'undefined'"); + + Assert.True(response1.Success); + Assert.True((bool)response1.Result); + + Assert.True(response2.Success); + Assert.True((bool)response2.Result); + } + } + + [Fact] + public async Task JsBindingGlobalApiEnabled() + { + using (var browser = new ChromiumWebBrowser(CefExample.BindingApiCustomObjectNameTestUrl, automaticallyCreateBrowser: false)) + { + var settings = browser.JavascriptObjectRepository.Settings; + settings.JavascriptBindingApiEnabled = true; + + //To modify the settings we need to defer browser creation slightly + browser.CreateBrowser(); + + await browser.LoadPageAsync(); + + var response1 = await browser.EvaluateScriptAsync("typeof window.cefSharp === 'undefined'"); + var response2 = await browser.EvaluateScriptAsync("typeof window.CefSharp === 'undefined'"); + + Assert.True(response1.Success); + Assert.False((bool)response1.Result); + + Assert.True(response2.Success); + Assert.False((bool)response2.Result); + } + } + [Theory] [InlineData("CefSharp.RenderProcessId")] [InlineData("cefSharp.renderProcessId")] diff --git a/CefSharp/JavascriptBinding/JavascriptBindingSettings.cs b/CefSharp/JavascriptBinding/JavascriptBindingSettings.cs index 470d61848..7d89aedd6 100644 --- a/CefSharp/JavascriptBinding/JavascriptBindingSettings.cs +++ b/CefSharp/JavascriptBinding/JavascriptBindingSettings.cs @@ -14,6 +14,24 @@ namespace CefSharp.JavascriptBinding private bool alwaysInterceptAsynchronously; private bool legacyBindingEnabled; private string jsBindingGlobalObjectName; + private bool jsBindingApiEnabled = true; + + /// + /// The Javascript methods that CefSharp provides in relation to JavaScript Binding are + /// created using a Global (window) Object. Settings this property allows you to disable + /// the creation of this object. Features like EvaluateScriptAsPromiseAsync that rely on + /// the creation of this object will no longer function. + /// + public bool JavascriptBindingApiEnabled + { + get { return jsBindingApiEnabled; } + set + { + ThrowIfFrozen(); + + jsBindingApiEnabled = value; + } + } /// /// The Javascript methods that CefSharp provides in relation to JavaScript Binding are