Skip to content

Commit 775b8a2

Browse files
authored
Merge 58c2d4c into cdc694e
2 parents cdc694e + 58c2d4c commit 775b8a2

File tree

6 files changed

+80
-3
lines changed

6 files changed

+80
-3
lines changed

CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ namespace CefSharp
8686
}
8787
}
8888
}
89+
90+
_jsBindingPropertyName = extraInfo->GetString("JsBindingPropertyName");
91+
_jsBindingPropertyNameCamelCase = extraInfo->GetString("JsBindingPropertyNameCamelCase");
8992
}
9093

9194
void CefAppUnmanagedWrapper::OnBrowserDestroyed(CefRefPtr<CefBrowser> browser)
@@ -124,11 +127,11 @@ namespace CefSharp
124127
auto browserWrapper = FindBrowserWrapper(browser->GetIdentifier());
125128

126129
auto cefSharpObj = CefV8Value::CreateObject(NULL, NULL);
127-
global->SetValue("CefSharp", cefSharpObj, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_READONLY);
130+
global->SetValue(_jsBindingPropertyName, cefSharpObj, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_READONLY);
128131

129132
//We'll support both CefSharp and cefSharp, for those who prefer the JS style
130133
auto cefSharpObjCamelCase = CefV8Value::CreateObject(NULL, NULL);
131-
global->SetValue("cefSharp", cefSharpObjCamelCase, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_READONLY);
134+
global->SetValue(_jsBindingPropertyNameCamelCase, cefSharpObjCamelCase, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_READONLY);
132135

133136
//TODO: JSB: Split functions into their own classes
134137
//Browser wrapper is only used for BindObjectAsync

CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ namespace CefSharp
2828
bool _focusedNodeChangedEnabled;
2929
bool _legacyBindingEnabled;
3030

31+
// The property names used to call binded objects
32+
CefString _jsBindingPropertyName;
33+
CefString _jsBindingPropertyNameCamelCase;
34+
3135
// The serialized registered object data waiting to be used.
3236
gcroot<Dictionary<String^, JavascriptObject^>^> _javascriptObjects;
3337

CefSharp.Core/ManagedCefBrowserAdapter.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void ManagedCefBrowserAdapter::CreateBrowser(IWindowInfo^ windowInfo, BrowserSet
4848

4949
legacyBindingEnabled = objectRepository->HasBoundObjects;
5050

51-
//For legacy binding we only add values if we have bond objects
51+
//For legacy binding we only add values if we have bond objects.
5252
if (legacyBindingEnabled)
5353
{
5454
auto listValue = CefListValue::Create();
@@ -61,6 +61,23 @@ void ManagedCefBrowserAdapter::CreateBrowser(IWindowInfo^ windowInfo, BrowserSet
6161

6262
extraInfo->SetBool("LegacyBindingEnabled", legacyBindingEnabled);
6363

64+
// Retrieve the configurable binding property names, throw exception if empty or illegal characters
65+
auto jsBindingPropertyName = CefSharpSettings::JavascriptBindingPropertyName;
66+
auto jsBindingPropertyNameCamelCase = CefSharpSettings::JavascriptBindingPropertyNameCamelCase;
67+
if (!StringCheck::EnsureLettersAndNumbers(jsBindingPropertyName) || !StringCheck::EnsureLettersAndNumbers(jsBindingPropertyNameCamelCase))
68+
{
69+
throw gcnew InvalidOperationException("CefBrowserHost::CreateBrowser invalid or illegal characters used for binding property names. Alphanumeric and underscores characters only.");
70+
}
71+
72+
extraInfo->SetString("JsBindingPropertyName", StringUtils::ToNative(jsBindingPropertyName));
73+
extraInfo->SetString("JsBindingPropertyNameCamelCase", StringUtils::ToNative(jsBindingPropertyNameCamelCase));
74+
75+
if (!CefBrowserHost::CreateBrowser(*cefWindowInfoWrapper->GetWindowInfo(), _clientAdapter.get(), addressNative,
76+
*browserSettings->_browserSettings, extraInfo, static_cast<CefRefPtr<CefRequestContext>>(requestContext)))
77+
{
78+
throw gcnew InvalidOperationException("CefBrowserHost::CreateBrowser call failed, review the CEF log file for more details.");
79+
}
80+
6481
if (!CefBrowserHost::CreateBrowser(*cefWindowInfoWrapper->GetWindowInfo(), _clientAdapter.get(), addressNative,
6582
*browserSettings->_browserSettings, extraInfo, static_cast<CefRefPtr<CefRequestContext>>(requestContext)))
6683
{

CefSharp/CefSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
<Compile Include="Internals\CookieManagerDecorator.cs" />
112112
<Compile Include="Internals\ParentProcessMonitor.cs" />
113113
<Compile Include="Internals\HeaderNameValueCollection.cs" />
114+
<Compile Include="Internals\StringCheck.cs" />
114115
<Compile Include="Internals\TaskScheduler\LimitedConcurrencyLevelTaskScheduler.cs" />
115116
<Compile Include="IUrlRequest.cs" />
116117
<Compile Include="IUrlRequestClient.cs" />

CefSharp/CefSharpSettings.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,30 @@ static CefSharpSettings()
2020
LegacyJavascriptBindingEnabled = false;
2121
WcfTimeout = TimeSpan.FromSeconds(2);
2222
SubprocessExitIfParentProcessClosed = true;
23+
JavascriptBindingPropertyName = "CefSharp";
24+
JavascriptBindingPropertyNameCamelCase = "cefSharp";
2325
}
2426

27+
/// <summary>
28+
/// Configure the browser binding property name.
29+
/// Use a custom property name when calling registred objects, example 'CefSharp.BindObjectAsync'.
30+
/// Alphanumeric and underscores characters only.
31+
/// The default value is 'CefSharp'
32+
///
33+
/// NOTE: There are two properties to be mindful of, this and <see cref="JavascriptBindingPropertyNameCamelCase"/>
34+
/// </summary>
35+
public static string JavascriptBindingPropertyName { get; set; }
36+
37+
/// <summary>
38+
/// Configure the browser binding property name with camel case.
39+
/// Use a custom property name when calling registred objects, example 'cefSharp.BindObjectAsync'
40+
/// Alphanumeric and underscores characters only.
41+
/// The default value is 'cefSharp'.
42+
///
43+
/// NOTE: There are two properties to be mindful of, this and <see cref="JavascriptBindingPropertyName"/>
44+
/// </summary>
45+
public static string JavascriptBindingPropertyNameCamelCase { get; set; }
46+
2547
/// <summary>
2648
/// Objects registered using RegisterJsObject and RegisterAsyncJsObject
2749
/// will be automatically bound when a V8Context is created. (Soon as the Javascript

CefSharp/Internals/StringCheck.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright © 2020 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
// Licensed to the .NET Foundation under one or more agreements.
6+
// The .NET Foundation licenses this file to you under the MIT license.
7+
// See the LICENSE file in the project root for more information.
8+
9+
using System.Text.RegularExpressions;
10+
11+
namespace CefSharp.Internals
12+
{
13+
public static class StringCheck
14+
{
15+
/// <summary>
16+
/// Regex check to ensure string contains only letters, numbers and underscores.
17+
/// </summary>
18+
/// <param name="stringToCheck"></param>
19+
/// <returns></returns>
20+
public static bool EnsureLettersAndNumbers(string stringToCheck)
21+
{
22+
if (!string.IsNullOrWhiteSpace(stringToCheck))
23+
{
24+
return Regex.IsMatch(stringToCheck, @"^\w+$");
25+
}
26+
27+
return false;
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)