From 819dfd54f9cb1864ab7e860a3b516dfdf0fdfa86 Mon Sep 17 00:00:00 2001 From: amaitland Date: Fri, 26 Jun 2015 11:19:51 +1000 Subject: [PATCH] Move WcfEnabled option into CefSharpSettings class Reduce code duplication - move EvaluateScriptAsync in WebBrowserExtensions --- CefSharp.Core/Cef.h | 13 ------ CefSharp.Core/CefSettings.h | 8 ---- CefSharp.Core/Internals/CefSharpApp.h | 2 +- CefSharp.Core/ManagedCefBrowserAdapter.cpp | 40 +------------------ CefSharp.Core/ManagedCefBrowserAdapter.h | 5 +-- CefSharp.Example/CefExample.cs | 2 +- CefSharp.OffScreen/ChromiumWebBrowser.cs | 5 --- CefSharp.WinForms/ChromiumWebBrowser.cs | 10 ----- .../Views/BrowserTabView.xaml.cs | 2 +- CefSharp.Wpf/ChromiumWebBrowser.cs | 10 ----- CefSharp/CefSharp.csproj | 1 + CefSharp/CefSharpSettings.cs | 21 ++++++++++ CefSharp/IWebBrowser.cs | 10 ----- CefSharp/Internals/IBrowserAdapter.cs | 2 - CefSharp/WebBrowserExtensions.cs | 20 ++++++++++ 15 files changed, 48 insertions(+), 103 deletions(-) create mode 100644 CefSharp/CefSharpSettings.cs diff --git a/CefSharp.Core/Cef.h b/CefSharp.Core/Cef.h index 63c24de9d..676e1e0bc 100644 --- a/CefSharp.Core/Cef.h +++ b/CefSharp.Core/Cef.h @@ -33,7 +33,6 @@ namespace CefSharp private: static Object^ _sync; - static bool _wcfEnabled = false; static bool _initialized = false; static HashSet^ _disposables; @@ -75,14 +74,6 @@ namespace CefSharp _disposables->Remove(item); } - static property bool WcfEnabled - { - bool get() - { - return _wcfEnabled; - } - } - /// Gets a value that indicates whether CefSharp is initialized. /// true if CefSharp is initialized; otherwise, false. static property bool IsInitialized @@ -191,10 +182,6 @@ namespace CefSharp } _initialized = success; - if (success) - { - _wcfEnabled = cefSettings->WcfEnabled; - } if (_initialized && shutdownOnProcessExit) { diff --git a/CefSharp.Core/CefSettings.h b/CefSharp.Core/CefSettings.h index 8b6471ad1..d3e889864 100644 --- a/CefSharp.Core/CefSettings.h +++ b/CefSharp.Core/CefSettings.h @@ -30,19 +30,11 @@ namespace CefSharp BrowserSubprocessPath = "CefSharp.BrowserSubprocess.exe"; _cefCustomSchemes = gcnew List(); _cefCommandLineArgs = gcnew Dictionary(); - WcfEnabled = true; } !CefSettings() { delete _cefSettings; } ~CefSettings() { delete _cefSettings; } - /// - /// WCF is used by JavascriptBinding and EvaluateScriptAsync. - /// Disabling effectively disables both of these features. - /// Defaults to true - /// - virtual property bool WcfEnabled; - virtual property IEnumerable^ CefCustomSchemes { IEnumerable^ get() { return _cefCustomSchemes; } diff --git a/CefSharp.Core/Internals/CefSharpApp.h b/CefSharp.Core/Internals/CefSharpApp.h index e023272f2..59b8e791e 100644 --- a/CefSharp.Core/Internals/CefSharpApp.h +++ b/CefSharp.Core/Internals/CefSharpApp.h @@ -45,7 +45,7 @@ namespace CefSharp virtual void OnBeforeChildProcessLaunch(CefRefPtr command_line) OVERRIDE { - if (_cefSettings->WcfEnabled) + if (CefSharpSettings::WcfEnabled) { command_line->AppendArgument(StringUtils::ToNative(CefSharpArguments::WcfEnabledArgument)); } diff --git a/CefSharp.Core/ManagedCefBrowserAdapter.cpp b/CefSharp.Core/ManagedCefBrowserAdapter.cpp index ef43c91f9..246f03375 100644 --- a/CefSharp.Core/ManagedCefBrowserAdapter.cpp +++ b/CefSharp.Core/ManagedCefBrowserAdapter.cpp @@ -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^ ManagedCefBrowserAdapter::EvaluateScriptAsync(int browserId, Int64 frameId, String^ script, Nullable 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^ ManagedCefBrowserAdapter::EvaluateScriptAsync(String^ script, Nullable 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(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."); } diff --git a/CefSharp.Core/ManagedCefBrowserAdapter.h b/CefSharp.Core/ManagedCefBrowserAdapter.h index 7eb9d8e1f..494de58aa 100644 --- a/CefSharp.Core/ManagedCefBrowserAdapter.h +++ b/CefSharp.Core/ManagedCefBrowserAdapter.h @@ -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^ EvaluateScriptAsync(int browserId, Int64 frameId, String^ script, Nullable timeout); - virtual Task^ EvaluateScriptAsync(String^ script, Nullable timeout); void Resize(int width, int height); void NotifyMoveOrResizeStarted(); void NotifyScreenInfoChanged(); diff --git a/CefSharp.Example/CefExample.cs b/CefSharp.Example/CefExample.cs index 6a8c7fc96..a8e4fa914 100644 --- a/CefSharp.Example/CefExample.cs +++ b/CefSharp.Example/CefExample.cs @@ -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"; diff --git a/CefSharp.OffScreen/ChromiumWebBrowser.cs b/CefSharp.OffScreen/ChromiumWebBrowser.cs index d326e08b8..5bfd68ff0 100644 --- a/CefSharp.OffScreen/ChromiumWebBrowser.cs +++ b/CefSharp.OffScreen/ChromiumWebBrowser.cs @@ -228,11 +228,6 @@ namespace CefSharp.OffScreen return completionSource.Task; } - public Task EvaluateScriptAsync(string script, TimeSpan? timeout = null) - { - return managedCefBrowserAdapter.EvaluateScriptAsync(script, timeout); - } - public void Load(string url) { Address = url; diff --git a/CefSharp.WinForms/ChromiumWebBrowser.cs b/CefSharp.WinForms/ChromiumWebBrowser.cs index a4dc05225..b9d2a41f2 100644 --- a/CefSharp.WinForms/ChromiumWebBrowser.cs +++ b/CefSharp.WinForms/ChromiumWebBrowser.cs @@ -155,16 +155,6 @@ namespace CefSharp.WinForms managedCefBrowserAdapter.RegisterJsObject(name, objectToBind, camelCaseJavascriptNames); } - public Task EvaluateScriptAsync(string script) - { - return EvaluateScriptAsync(script, timeout: null); - } - - public Task 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); diff --git a/CefSharp.Wpf.Example/Views/BrowserTabView.xaml.cs b/CefSharp.Wpf.Example/Views/BrowserTabView.xaml.cs index fed3bfbfc..0f18a4ea3 100644 --- a/CefSharp.Wpf.Example/Views/BrowserTabView.xaml.cs +++ b/CefSharp.Wpf.Example/Views/BrowserTabView.xaml.cs @@ -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()); } diff --git a/CefSharp.Wpf/ChromiumWebBrowser.cs b/CefSharp.Wpf/ChromiumWebBrowser.cs index c8da90ab7..3e0e8c041 100644 --- a/CefSharp.Wpf/ChromiumWebBrowser.cs +++ b/CefSharp.Wpf/ChromiumWebBrowser.cs @@ -1292,16 +1292,6 @@ namespace CefSharp.Wpf managedCefBrowserAdapter.RegisterJsObject(name, objectToBind, camelCaseJavascriptNames); } - public Task EvaluateScriptAsync(string script) - { - return EvaluateScriptAsync(script, timeout: null); - } - - public Task EvaluateScriptAsync(string script, TimeSpan? timeout) - { - return managedCefBrowserAdapter.EvaluateScriptAsync(script, timeout); - } - /// /// Raises Rendering event /// diff --git a/CefSharp/CefSharp.csproj b/CefSharp/CefSharp.csproj index 9c253140e..ee33d38b5 100644 --- a/CefSharp/CefSharp.csproj +++ b/CefSharp/CefSharp.csproj @@ -92,6 +92,7 @@ + diff --git a/CefSharp/CefSharpSettings.cs b/CefSharp/CefSharpSettings.cs new file mode 100644 index 000000000..22d7d7f76 --- /dev/null +++ b/CefSharp/CefSharpSettings.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; + } + + /// + /// WCF is used by JavascriptBinding and EvaluateScriptAsync. + /// Disabling effectively disables both of these features. + /// Defaults to true + /// + public static bool WcfEnabled { get; set; } + } +} diff --git a/CefSharp/IWebBrowser.cs b/CefSharp/IWebBrowser.cs index a4353011a..b753373bc 100644 --- a/CefSharp/IWebBrowser.cs +++ b/CefSharp/IWebBrowser.cs @@ -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 /// camel case the javascript names of properties/methods, defaults to true void RegisterJsObject(string name, object objectToBind, bool camelCaseJavascriptNames = true); - /// - /// Execute some Javascript code in the context of this WebBrowser, and return the result of the evaluation - /// in an Async fashion - /// - /// The Javascript code that should be executed. - /// The timeout after which the Javascript code execution should be aborted. - /// A Task that can be awaited to perform the script execution - Task EvaluateScriptAsync(string script, TimeSpan? timeout = null); - /// /// Implement and assign to handle dialog events. /// diff --git a/CefSharp/Internals/IBrowserAdapter.cs b/CefSharp/Internals/IBrowserAdapter.cs index 2c256a78f..c9c651d0e 100644 --- a/CefSharp/Internals/IBrowserAdapter.cs +++ b/CefSharp/Internals/IBrowserAdapter.cs @@ -14,8 +14,6 @@ namespace CefSharp.Internals public interface IBrowserAdapter { IJavascriptCallbackFactory JavascriptCallbackFactory { get; } - Task EvaluateScriptAsync(string script, TimeSpan? timeout); - Task EvaluateScriptAsync(int browserId, Int64 frameId, string script, TimeSpan? timeout); void OnAfterBrowserCreated(int browserId); IBrowser GetBrowser(); } diff --git a/CefSharp/WebBrowserExtensions.cs b/CefSharp/WebBrowserExtensions.cs index f61825b44..ebc25c17b 100644 --- a/CefSharp/WebBrowserExtensions.cs +++ b/CefSharp/WebBrowserExtensions.cs @@ -538,6 +538,26 @@ namespace CefSharp cefBrowser.AddWordToDictionary(word); } + public static Task 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)