mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-05 11:42:36 +00:00
Move WcfEnabled option into CefSharpSettings class
Reduce code duplication - move EvaluateScriptAsync in WebBrowserExtensions
This commit is contained in:
@@ -33,7 +33,6 @@ namespace CefSharp
|
||||
private:
|
||||
static Object^ _sync;
|
||||
|
||||
static bool _wcfEnabled = false;
|
||||
static bool _initialized = false;
|
||||
static HashSet<IDisposable^>^ _disposables;
|
||||
|
||||
@@ -75,14 +74,6 @@ namespace CefSharp
|
||||
_disposables->Remove(item);
|
||||
}
|
||||
|
||||
static property bool WcfEnabled
|
||||
{
|
||||
bool get()
|
||||
{
|
||||
return _wcfEnabled;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Gets a value that indicates whether CefSharp is initialized.</summary>
|
||||
/// <value>true if CefSharp is initialized; otherwise, false.</value>
|
||||
static property bool IsInitialized
|
||||
@@ -191,10 +182,6 @@ namespace CefSharp
|
||||
}
|
||||
|
||||
_initialized = success;
|
||||
if (success)
|
||||
{
|
||||
_wcfEnabled = cefSettings->WcfEnabled;
|
||||
}
|
||||
|
||||
if (_initialized && shutdownOnProcessExit)
|
||||
{
|
||||
|
||||
@@ -30,19 +30,11 @@ namespace CefSharp
|
||||
BrowserSubprocessPath = "CefSharp.BrowserSubprocess.exe";
|
||||
_cefCustomSchemes = gcnew List<CefCustomScheme^>();
|
||||
_cefCommandLineArgs = gcnew Dictionary<String^, String^>();
|
||||
WcfEnabled = true;
|
||||
}
|
||||
|
||||
!CefSettings() { delete _cefSettings; }
|
||||
~CefSettings() { delete _cefSettings; }
|
||||
|
||||
/// <summary>
|
||||
/// WCF is used by JavascriptBinding and EvaluateScriptAsync.
|
||||
/// Disabling effectively disables both of these features.
|
||||
/// Defaults to true
|
||||
/// </summary>
|
||||
virtual property bool WcfEnabled;
|
||||
|
||||
virtual property IEnumerable<CefCustomScheme^>^ CefCustomSchemes
|
||||
{
|
||||
IEnumerable<CefCustomScheme^>^ get() { return _cefCustomSchemes; }
|
||||
|
||||
@@ -45,7 +45,7 @@ namespace CefSharp
|
||||
|
||||
virtual void OnBeforeChildProcessLaunch(CefRefPtr<CefCommandLine> command_line) OVERRIDE
|
||||
{
|
||||
if (_cefSettings->WcfEnabled)
|
||||
if (CefSharpSettings::WcfEnabled)
|
||||
{
|
||||
command_line->AppendArgument(StringUtils::ToNative(CefSharpArguments::WcfEnabledArgument));
|
||||
}
|
||||
|
||||
@@ -39,9 +39,7 @@ void ManagedCefBrowserAdapter::LoadUrl(String^ address)
|
||||
|
||||
void ManagedCefBrowserAdapter::OnAfterBrowserCreated(int browserId)
|
||||
{
|
||||
_wcfEnabled = Cef::WcfEnabled;
|
||||
|
||||
if (_wcfEnabled)
|
||||
if (CefSharpSettings::WcfEnabled)
|
||||
{
|
||||
_browserProcessServiceHost = gcnew BrowserProcessServiceHost(_javaScriptObjectRepository, Process::GetCurrentProcess()->Id, browserId);
|
||||
//NOTE: Attempt to solve timing issue where browser is opened and rapidly disposed. In some cases a call to Open throws
|
||||
@@ -284,40 +282,6 @@ void ManagedCefBrowserAdapter::OnMouseWheel(int x, int y, int deltaX, int deltaY
|
||||
}
|
||||
}
|
||||
|
||||
Task<JavascriptResponse^>^ ManagedCefBrowserAdapter::EvaluateScriptAsync(int browserId, Int64 frameId, String^ script, Nullable<TimeSpan> timeout)
|
||||
{
|
||||
if (timeout.HasValue && timeout.Value.TotalMilliseconds > UInt32::MaxValue)
|
||||
{
|
||||
throw gcnew ArgumentOutOfRangeException("timeout", "Timeout greater than Maximum allowable value of " + UInt32::MaxValue);
|
||||
}
|
||||
|
||||
return _clientAdapter->EvaluateScriptAsync(browserId, frameId, script, timeout);
|
||||
}
|
||||
|
||||
Task<JavascriptResponse^>^ ManagedCefBrowserAdapter::EvaluateScriptAsync(String^ script, Nullable<TimeSpan> timeout)
|
||||
{
|
||||
if (timeout.HasValue && timeout.Value.TotalMilliseconds > UInt32::MaxValue)
|
||||
{
|
||||
throw gcnew ArgumentOutOfRangeException("timeout", "Timeout greater than Maximum allowable value of " + UInt32::MaxValue);
|
||||
}
|
||||
|
||||
auto browser = _clientAdapter->GetCefBrowser();
|
||||
|
||||
if (browser == nullptr)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto frame = browser->GetMainFrame();
|
||||
|
||||
if (frame == nullptr)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return _clientAdapter->EvaluateScriptAsync(browser->GetIdentifier(), frame->GetIdentifier(), script, timeout);
|
||||
}
|
||||
|
||||
void ManagedCefBrowserAdapter::CreateBrowser(BrowserSettings^ browserSettings, IntPtr sourceHandle, String^ address)
|
||||
{
|
||||
HWND hwnd = static_cast<HWND>(sourceHandle.ToPointer());
|
||||
@@ -371,7 +335,7 @@ void ManagedCefBrowserAdapter::NotifyScreenInfoChanged()
|
||||
|
||||
void ManagedCefBrowserAdapter::RegisterJsObject(String^ name, Object^ object, bool lowerCaseJavascriptNames)
|
||||
{
|
||||
if (!_wcfEnabled)
|
||||
if (!CefSharpSettings::WcfEnabled)
|
||||
{
|
||||
throw gcnew InvalidOperationException("To enable synchronous JS bindings set WcfEnabled true in CefSettings during initialization.");
|
||||
}
|
||||
|
||||
@@ -34,7 +34,6 @@ namespace CefSharp
|
||||
JavascriptObjectRepository^ _javaScriptObjectRepository;
|
||||
IBrowser^ _browserWrapper;
|
||||
bool _isDisposed;
|
||||
bool _wcfEnabled;
|
||||
|
||||
private:
|
||||
// Private keyboard functions:
|
||||
@@ -79,7 +78,7 @@ namespace CefSharp
|
||||
delete _browserWrapper;
|
||||
_browserWrapper = nullptr;
|
||||
|
||||
if (_wcfEnabled)
|
||||
if (CefSharpSettings::WcfEnabled)
|
||||
{
|
||||
_browserProcessServiceHost->Close();
|
||||
_browserProcessServiceHost = nullptr;
|
||||
@@ -108,8 +107,6 @@ namespace CefSharp
|
||||
void OnMouseMove(int x, int y, bool mouseLeave, CefEventFlags modifiers);
|
||||
void OnMouseButton(int x, int y, int mouseButtonType, bool mouseUp, int clickCount, CefEventFlags modifiers);
|
||||
void OnMouseWheel(int x, int y, int deltaX, int deltaY);
|
||||
virtual Task<JavascriptResponse^>^ EvaluateScriptAsync(int browserId, Int64 frameId, String^ script, Nullable<TimeSpan> timeout);
|
||||
virtual Task<JavascriptResponse^>^ EvaluateScriptAsync(String^ script, Nullable<TimeSpan> timeout);
|
||||
void Resize(int width, int height);
|
||||
void NotifyMoveOrResizeStarted();
|
||||
void NotifyScreenInfoChanged();
|
||||
|
||||
@@ -29,7 +29,7 @@ namespace CefSharp.Example
|
||||
var settings = new CefSettings();
|
||||
settings.RemoteDebuggingPort = 8088;
|
||||
//This will disable WCF (Effectively disabling JavascriptBinding and EvaluateScriptAsync)
|
||||
//settings.WcfEnabled = false;
|
||||
//CefSharpSettings.WcfEnabled = false;
|
||||
//The location where cache data will be stored on disk. If empty an in-memory cache will be used for some features and a temporary disk cache for others.
|
||||
//HTML5 databases such as localStorage will only persist across sessions if a cache path is specified.
|
||||
settings.CachePath = "cache";
|
||||
|
||||
@@ -228,11 +228,6 @@ namespace CefSharp.OffScreen
|
||||
return completionSource.Task;
|
||||
}
|
||||
|
||||
public Task<JavascriptResponse> EvaluateScriptAsync(string script, TimeSpan? timeout = null)
|
||||
{
|
||||
return managedCefBrowserAdapter.EvaluateScriptAsync(script, timeout);
|
||||
}
|
||||
|
||||
public void Load(string url)
|
||||
{
|
||||
Address = url;
|
||||
|
||||
@@ -155,16 +155,6 @@ namespace CefSharp.WinForms
|
||||
managedCefBrowserAdapter.RegisterJsObject(name, objectToBind, camelCaseJavascriptNames);
|
||||
}
|
||||
|
||||
public Task<JavascriptResponse> EvaluateScriptAsync(string script)
|
||||
{
|
||||
return EvaluateScriptAsync(script, timeout: null);
|
||||
}
|
||||
|
||||
public Task<JavascriptResponse> EvaluateScriptAsync(string script, TimeSpan? timeout)
|
||||
{
|
||||
return managedCefBrowserAdapter.EvaluateScriptAsync(script, timeout);
|
||||
}
|
||||
|
||||
public void SendMouseWheelEvent(int x, int y, int deltaX, int deltaY)
|
||||
{
|
||||
managedCefBrowserAdapter.OnMouseWheel(x, y, deltaX, deltaY);
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace CefSharp.Wpf.Example.Views
|
||||
InitializeComponent();
|
||||
|
||||
browser.RequestHandler = new RequestHandler();
|
||||
if (Cef.WcfEnabled)
|
||||
if (CefSharpSettings.WcfEnabled)
|
||||
{
|
||||
browser.RegisterJsObject("bound", new BoundObject());
|
||||
}
|
||||
|
||||
@@ -1292,16 +1292,6 @@ namespace CefSharp.Wpf
|
||||
managedCefBrowserAdapter.RegisterJsObject(name, objectToBind, camelCaseJavascriptNames);
|
||||
}
|
||||
|
||||
public Task<JavascriptResponse> EvaluateScriptAsync(string script)
|
||||
{
|
||||
return EvaluateScriptAsync(script, timeout: null);
|
||||
}
|
||||
|
||||
public Task<JavascriptResponse> EvaluateScriptAsync(string script, TimeSpan? timeout)
|
||||
{
|
||||
return managedCefBrowserAdapter.EvaluateScriptAsync(script, timeout);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raises Rendering event
|
||||
/// </summary>
|
||||
|
||||
@@ -92,6 +92,7 @@
|
||||
<Compile Include="CefFileDialogMode.cs" />
|
||||
<Compile Include="CefJsDialogType.cs" />
|
||||
<Compile Include="CefReturnValue.cs" />
|
||||
<Compile Include="CefSharpSettings.cs" />
|
||||
<Compile Include="CefThreadIds.cs" />
|
||||
<Compile Include="Cookie.cs" />
|
||||
<Compile Include="IAuthCallback.cs" />
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
// Copyright © 2010-2015 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.
|
||||
|
||||
namespace CefSharp
|
||||
{
|
||||
public static class CefSharpSettings
|
||||
{
|
||||
static CefSharpSettings()
|
||||
{
|
||||
WcfEnabled = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// WCF is used by JavascriptBinding and EvaluateScriptAsync.
|
||||
/// Disabling effectively disables both of these features.
|
||||
/// Defaults to true
|
||||
/// </summary>
|
||||
public static bool WcfEnabled { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CefSharp
|
||||
{
|
||||
@@ -60,15 +59,6 @@ namespace CefSharp
|
||||
/// <param name="camelCaseJavascriptNames">camel case the javascript names of properties/methods, defaults to true</param>
|
||||
void RegisterJsObject(string name, object objectToBind, bool camelCaseJavascriptNames = true);
|
||||
|
||||
/// <summary>
|
||||
/// Execute some Javascript code in the context of this WebBrowser, and return the result of the evaluation
|
||||
/// in an Async fashion
|
||||
/// </summary>
|
||||
/// <param name="script">The Javascript code that should be executed.</param>
|
||||
/// <param name="timeout">The timeout after which the Javascript code execution should be aborted.</param>
|
||||
/// <returns>A Task that can be awaited to perform the script execution</returns>
|
||||
Task<JavascriptResponse> EvaluateScriptAsync(string script, TimeSpan? timeout = null);
|
||||
|
||||
/// <summary>
|
||||
/// Implement <see cref="IDialogHandler"/> and assign to handle dialog events.
|
||||
/// </summary>
|
||||
|
||||
@@ -14,8 +14,6 @@ namespace CefSharp.Internals
|
||||
public interface IBrowserAdapter
|
||||
{
|
||||
IJavascriptCallbackFactory JavascriptCallbackFactory { get; }
|
||||
Task<JavascriptResponse> EvaluateScriptAsync(string script, TimeSpan? timeout);
|
||||
Task<JavascriptResponse> EvaluateScriptAsync(int browserId, Int64 frameId, string script, TimeSpan? timeout);
|
||||
void OnAfterBrowserCreated(int browserId);
|
||||
IBrowser GetBrowser();
|
||||
}
|
||||
|
||||
@@ -538,6 +538,26 @@ namespace CefSharp
|
||||
cefBrowser.AddWordToDictionary(word);
|
||||
}
|
||||
|
||||
public static Task<JavascriptResponse> EvaluateScriptAsync(this IWebBrowser browser, string script, TimeSpan? timeout = null)
|
||||
{
|
||||
if (timeout.HasValue && timeout.Value.TotalMilliseconds > UInt32.MaxValue)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("timeout", "Timeout greater than Maximum allowable value of " + UInt32.MaxValue);
|
||||
}
|
||||
|
||||
if (!CefSharpSettings.WcfEnabled)
|
||||
{
|
||||
throw new InvalidOperationException("To wait for javascript code set WcfEnabled true in CefSettings during initialization.");
|
||||
}
|
||||
|
||||
using (var frame = browser.GetMainFrame())
|
||||
{
|
||||
ThrowExceptionIfFrameNull(frame);
|
||||
|
||||
return frame.EvaluateScriptAsync(script, timeout);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ThrowExceptionIfFrameNull(IFrame frame)
|
||||
{
|
||||
if (frame == null)
|
||||
|
||||
Reference in New Issue
Block a user