diff --git a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp index ac8deba8d..8f9606f00 100644 --- a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp +++ b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp @@ -8,10 +8,15 @@ #include "CefBrowserWrapper.h" #include "CefAppUnmanagedWrapper.h" #include "JavascriptRootObjectWrapper.h" +#include "Serialization\V8Serialization.h" +#include "..\CefSharp.Core\Internals\Messaging\Messages.h" +#include "..\CefSharp.Core\Internals\Serialization\Primitives.h" using namespace System; using namespace System::Diagnostics; using namespace System::Collections::Generic; +using namespace CefSharp::Internals::Messaging; +using namespace CefSharp::Internals::Serialization; namespace CefSharp { @@ -32,7 +37,7 @@ namespace CefSharp void CefAppUnmanagedWrapper::OnBrowserDestroyed(CefRefPtr browser) { - auto wrapper = FindBrowserWrapper(browser, false); + auto wrapper = FindBrowserWrapper(browser->GetIdentifier(), false); if (wrapper != nullptr) { @@ -44,7 +49,7 @@ namespace CefSharp void CefAppUnmanagedWrapper::OnContextCreated(CefRefPtr browser, CefRefPtr frame, CefRefPtr context) { - auto wrapper = FindBrowserWrapper(browser, true); + auto wrapper = FindBrowserWrapper(browser->GetIdentifier(), true); if (wrapper->JavascriptRootObject != nullptr) { @@ -59,7 +64,7 @@ namespace CefSharp void CefAppUnmanagedWrapper::OnContextReleased(CefRefPtr browser, CefRefPtr frame, CefRefPtr context) { - auto wrapper = FindBrowserWrapper(browser, true); + auto wrapper = FindBrowserWrapper(browser->GetIdentifier(), true); if (wrapper->JavascriptRootObjectWrapper != nullptr) { @@ -68,9 +73,8 @@ namespace CefSharp } }; - CefBrowserWrapper^ CefAppUnmanagedWrapper::FindBrowserWrapper(CefRefPtr browser, bool mustExist) + CefBrowserWrapper^ CefAppUnmanagedWrapper::FindBrowserWrapper(int browserId, bool mustExist) { - auto browserId = browser->GetIdentifier(); CefBrowserWrapper^ wrapper = nullptr; _browserWrappers->TryGetValue(browserId, wrapper); @@ -81,5 +85,71 @@ namespace CefSharp } return wrapper; + } + + bool CefAppUnmanagedWrapper::OnProcessMessageReceived(CefRefPtr browser, CefProcessId sourceProcessId, CefRefPtr message) + { + auto handled = false; + auto name = message->GetName(); + if (name == kEvaluateJavascriptRequest) + { + auto argList = message->GetArgumentList(); + auto browserId = argList->GetInt(0); + auto frameId = GetInt64(argList, 1); + auto callbackId = GetInt64(argList, 2); + auto script = argList->GetString(3); + + if(browser->GetIdentifier() != browserId) + { + throw gcnew InvalidOperationException(String::Format("Request BrowserId : {0} does not match browser Id : {1}", browserId, browser->GetIdentifier())); + } + + auto browserWrapper = FindBrowserWrapper(browserId, true); + auto frame = browser->GetFrame(frameId); + if (frame.get()) + { + auto context = frame->GetV8Context(); + + if (context.get() && context->Enter()) + { + try + { + CefRefPtr result; + CefRefPtr exception; + auto success = context->Eval(script, result, exception); + auto response = CefProcessMessage::Create(kEvaluateJavascriptResponse); + auto argList = response->GetArgumentList(); + + argList->SetBool(0, success); + SetInt64(callbackId, argList, 1); + if (success) + { + SerializeV8Object(result, argList, 2, browserWrapper->CallbackRegistry); + } + else + { + argList->SetString(2, exception->GetMessage()); + } + + if (response.get()) + { + browser->SendProcessMessage(sourceProcessId, response); + } + } + finally + { + context->Exit(); + } + } + } + else + { + //TODO handle error + } + + handled = true; + } + + return handled; }; } \ No newline at end of file diff --git a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h index 2d4c179e4..c04091d44 100644 --- a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h +++ b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h @@ -19,10 +19,10 @@ namespace CefSharp private class CefAppUnmanagedWrapper : CefApp, CefRenderProcessHandler { private: + gcroot^> _onBrowserCreated; gcroot^> _onBrowserDestroyed; gcroot^> _browserWrappers; - CefBrowserWrapper^ FindBrowserWrapper(CefRefPtr browser, bool mustExist); public: CefAppUnmanagedWrapper(Action^ onBrowserCreated, Action^ onBrowserDestoryed) @@ -39,11 +39,14 @@ namespace CefSharp delete _onBrowserDestroyed; } + CefBrowserWrapper^ FindBrowserWrapper(int browserId, bool mustExist); + virtual DECL CefRefPtr GetRenderProcessHandler() OVERRIDE; virtual DECL void OnBrowserCreated(CefRefPtr browser) OVERRIDE; virtual DECL void OnBrowserDestroyed(CefRefPtr browser) OVERRIDE; virtual DECL void OnContextCreated(CefRefPtr browser, CefRefPtr frame, CefRefPtr context) OVERRIDE; virtual DECL void OnContextReleased(CefRefPtr browser, CefRefPtr frame, CefRefPtr context) OVERRIDE; + virtual DECL bool OnProcessMessageReceived(CefRefPtr browser, CefProcessId sourceProcessId, CefRefPtr message) OVERRIDE; IMPLEMENT_REFCOUNTING(CefAppUnmanagedWrapper); }; diff --git a/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.cpp b/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.cpp index 7f415274f..f89b213e1 100644 --- a/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.cpp +++ b/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.cpp @@ -31,52 +31,6 @@ namespace CefSharp } } - JavascriptResponse^ CefBrowserWrapper::EvaluateScriptInContext(CefRefPtr context, CefString script) - { - CefRefPtr result; - CefRefPtr exception; - JavascriptResponse^ response = gcnew JavascriptResponse(); - - response->Success = context->Eval(script, result, exception); - if (response->Success) - { - if (result->IsFunction()) - { - response->Result = _callbackRegistry->Register(context, result); - } - else - { - response->Result = TypeUtils::ConvertFromCef(result); - } - } - else if (exception.get()) - { - response->Message = StringUtils::ToClr(exception->GetMessage()); - } - - return response; - } - - JavascriptResponse^ CefBrowserWrapper::DoEvaluateScript(System::Int64 frameId, String^ script) - { - auto frame = _cefBrowser->GetFrame(frameId); - CefRefPtr context = frame->GetV8Context(); - - if (context.get() && context->Enter()) - { - try - { - return EvaluateScriptInContext(context, StringUtils::ToNative(script)); - } - finally - { - context->Exit(); - } - } - - return nullptr; - } - JavascriptResponse^ CefBrowserWrapper::DoCallback(System::Int64 callbackId, array^ parameters) { return _callbackRegistry->Execute(callbackId, parameters); @@ -87,6 +41,11 @@ namespace CefSharp _callbackRegistry->Deregister(id); } + JavascriptCallbackRegistry^ CefBrowserWrapper::CallbackRegistry::get() + { + return _callbackRegistry; + } + CefBrowserWrapper::!CefBrowserWrapper() { _cefBrowser = nullptr; diff --git a/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.h b/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.h index 7c5b85a9a..9c20cd855 100644 --- a/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.h +++ b/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.h @@ -30,6 +30,12 @@ namespace CefSharp JavascriptCallbackRegistry^ _callbackRegistry; JavascriptRootObjectWrapper^ _javascriptRootObjectWrapper; + internal: + property JavascriptCallbackRegistry^ CallbackRegistry + { + CefSharp::Internals::JavascriptCallbackRegistry^ get(); + } + public: CefBrowserWrapper(CefRefPtr cefBrowser); !CefBrowserWrapper(); @@ -53,10 +59,6 @@ namespace CefSharp // The WCF proxy to the parent process. property IBrowserProcess^ BrowserProcess; - JavascriptResponse^ EvaluateScriptInContext(CefRefPtr context, CefString script); - - virtual JavascriptResponse^ DoEvaluateScript(System::Int64 frameId, String^ script); - JavascriptResponse^ DoCallback(System::Int64 callbackId, array^ parameters); void DestroyJavascriptCallback(Int64 id); diff --git a/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj b/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj index 2e2ce536a..3c11f2d64 100644 --- a/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj +++ b/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj @@ -155,6 +155,7 @@ + @@ -171,9 +172,11 @@ + + @@ -193,6 +196,7 @@ Create + diff --git a/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj.filters b/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj.filters index 5550541d5..5c744c4a0 100644 --- a/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj.filters +++ b/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj.filters @@ -68,6 +68,12 @@ Header Files + + Header Files + + + Header Files + @@ -112,6 +118,12 @@ Source Files + + Source Files + + + Source Files + diff --git a/CefSharp.BrowserSubprocess.Core/Serialization/V8Serialization.cpp b/CefSharp.BrowserSubprocess.Core/Serialization/V8Serialization.cpp new file mode 100644 index 000000000..90d86ac6b --- /dev/null +++ b/CefSharp.BrowserSubprocess.Core/Serialization/V8Serialization.cpp @@ -0,0 +1,85 @@ +// Copyright © 2010-2015 The CefSharp Project. 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 "V8Serialization.h" +#include "JavascriptCallbackRegistry.h" +#include "../CefSharp.Core/Internals/Serialization/Primitives.h" + +namespace CefSharp +{ + namespace Internals + { + namespace Serialization + { + template + void SerializeV8Object(CefRefPtr obj, CefRefPtr list, TIndex index, JavascriptCallbackRegistry^ callbackRegistry) + { + if (obj->IsNull() || obj->IsUndefined()) + { + list->SetNull(index); + } + else if (obj->IsBool()) + list->SetBool(index, obj->GetBoolValue()); + else if (obj->IsInt()) + list->SetInt(index, obj->GetIntValue()); + else if (obj->IsDouble()) + list->SetDouble(index, obj->GetDoubleValue()); + else if (obj->IsString()) + list->SetString(index, obj->GetStringValue()); + else if (obj->IsDate()) + SetCefTime(obj->GetDateValue(), list, index); + else if (obj->IsArray()) + { + int arrLength = obj->GetArrayLength(); + std::vector keys; + if (arrLength > 0 && obj->GetKeys(keys)) + { + auto array = CefListValue::Create(); + for (int i = 0; i < arrLength; i++) + { + SerializeV8Object(obj->GetValue(keys[i]), array, i, callbackRegistry); + } + + list->SetList(index, array); + } + else + { + list->SetNull(index); + } + } + else if (obj->IsFunction()) + { + auto context = CefV8Context::GetCurrentContext(); + auto jsCallback = callbackRegistry->Register(context, obj); + SetJsCallback(jsCallback, list, index); + } + else if (obj->IsObject()) + { + std::vector keys; + if (obj->GetKeys(keys) && keys.size() > 0) + { + auto result = CefDictionaryValue::Create(); + for (int i = 0; i < keys.size(); i++) + { + auto p_keyStr = StringUtils::ToClr(keys[i].ToString()); + if ((obj->HasValue(keys[i])) && (!p_keyStr->StartsWith("__"))) + { + SerializeV8Object(obj->GetValue(keys[i]), result, keys[i], callbackRegistry); + } + } + list->SetDictionary(index, result); + } + } + else + { + list->SetNull(index); + } + } + + template void SerializeV8Object(CefRefPtr value, CefRefPtr list, int index, JavascriptCallbackRegistry^ callbackRegistry); + template void SerializeV8Object(CefRefPtr value, CefRefPtr list, CefString index, JavascriptCallbackRegistry^ callbackRegistry); + } + } +} \ No newline at end of file diff --git a/CefSharp.BrowserSubprocess.Core/Serialization/V8Serialization.h b/CefSharp.BrowserSubprocess.Core/Serialization/V8Serialization.h new file mode 100644 index 000000000..d5b7a69a3 --- /dev/null +++ b/CefSharp.BrowserSubprocess.Core/Serialization/V8Serialization.h @@ -0,0 +1,25 @@ +// Copyright © 2010-2015 The CefSharp Project. 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 + +#include "include/cef_v8.h" + +namespace CefSharp +{ + namespace Internals + { + ref class JavascriptCallbackRegistry; + + namespace Serialization + { + //Functions to sserialize data to be sent to the browser process. + + //Serializes a V8 structure into a given index of a CefListValue or CefDictionaryValue + //JavascriptCallbackRegistry should be passed to save V8Values with function types + template + void SerializeV8Object(CefRefPtr value, CefRefPtr list, TIndex index, JavascriptCallbackRegistry^ callbackRegistry); + } + } +} \ No newline at end of file diff --git a/CefSharp.BrowserSubprocess/CefRenderProcess.cs b/CefSharp.BrowserSubprocess/CefRenderProcess.cs index 5232a6e96..839c1b965 100644 --- a/CefSharp.BrowserSubprocess/CefRenderProcess.cs +++ b/CefSharp.BrowserSubprocess/CefRenderProcess.cs @@ -113,72 +113,6 @@ namespace CefSharp.BrowserSubprocess browser.JavascriptRootObject = null; } - public Task EvaluateScriptAsync(int browserId, long frameId, string script, TimeSpan? timeout) - { - var factory = RenderThreadTaskFactory; - var browser = browsers.FirstOrDefault(x => x.BrowserId == browserId); - if (browser == null) - { - return TaskExtensions.FromResult(new JavascriptResponse - { - Success = false, - Message = string.Format("Browser with Id {0} not found in Render Sub Process.", browserId) - }); - } - - var task = factory.StartNew(() => - { - try - { - var response = browser.DoEvaluateScript(frameId, script); - - return response; - } - catch (Exception ex) - { - return new JavascriptResponse - { - Success = false, - Message = ex.ToString() - }; - } - }, TaskCreationOptions.AttachedToParent); - - return timeout.HasValue ? task.WithTimeout(timeout.Value) : task; - } - - public IAsyncResult BeginEvaluateScriptAsync(int browserId, long frameId, string script, TimeSpan? timeout, AsyncCallback callback, object state) - { - var tcs = new TaskCompletionSource(state); - var task = EvaluateScriptAsync(browserId, frameId, script, timeout); - task.ContinueWith(t => - { - if (t.IsFaulted) - { - tcs.TrySetException(t.Exception.InnerExceptions); - } - else if (t.IsCanceled) - { - tcs.TrySetCanceled(); - } - else - { - tcs.TrySetResult(t.Result); - } - - if (callback != null) - { - callback(tcs.Task); - } - }); - return tcs.Task; - } - - public JavascriptResponse EndEvaluateScriptAsync(IAsyncResult result) - { - return ((Task)result).Result; - } - public IAsyncResult BeginJavascriptCallbackAsync(int browserId, long callbackId, object[] parameters, TimeSpan? timeout, AsyncCallback callback, object state) { var tcs = new TaskCompletionSource(state); 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/CefSharp.Core.vcxproj b/CefSharp.Core/CefSharp.Core.vcxproj index b19988af5..4859ba5e4 100644 --- a/CefSharp.Core/CefSharp.Core.vcxproj +++ b/CefSharp.Core/CefSharp.Core.vcxproj @@ -230,6 +230,8 @@ + + Create @@ -265,6 +267,7 @@ + @@ -272,9 +275,11 @@ + + diff --git a/CefSharp.Core/CefSharp.Core.vcxproj.filters b/CefSharp.Core/CefSharp.Core.vcxproj.filters index 42ee67e83..5bb8eb369 100644 --- a/CefSharp.Core/CefSharp.Core.vcxproj.filters +++ b/CefSharp.Core/CefSharp.Core.vcxproj.filters @@ -59,6 +59,12 @@ Source Files + + Source Files + + + Source Files + @@ -181,6 +187,15 @@ Header Files + + Header Files + + + Header Files + + + Header Files + diff --git a/CefSharp.Core/Internals/CefFrameWrapper.cpp b/CefSharp.Core/Internals/CefFrameWrapper.cpp index 9a523ff08..2e59f9834 100644 --- a/CefSharp.Core/Internals/CefFrameWrapper.cpp +++ b/CefSharp.Core/Internals/CefFrameWrapper.cpp @@ -175,11 +175,12 @@ void CefFrameWrapper::ExecuteJavaScriptAsync(String^ code, String^ scriptUrl, in Task^ CefFrameWrapper::EvaluateScriptAsync(String^ script, Nullable timeout) { ThrowIfDisposed(); - if (_browserAdapter == nullptr) - { - throw gcnew InvalidOperationException(gcnew String(L"IBrowserAdapter is NULL, EvaluateScriptAsync MUST NOT be called from the render process!")); - } - return _browserAdapter->EvaluateScriptAsync(_frame->GetBrowser()->GetIdentifier(), _frame->GetIdentifier(), script, timeout); + + auto host = _frame->GetBrowser()->GetHost(); + + auto client = static_cast(host->GetClient().get()); + + return client->EvaluateScriptAsync(_frame->GetBrowser()->GetIdentifier(), _frame->GetIdentifier(), script, timeout); } /// @@ -242,7 +243,7 @@ IFrame^ CefFrameWrapper::Parent::get() auto parent = _frame->GetParent(); if (parent != nullptr && _parentFrame != nullptr) { - _parentFrame = gcnew CefFrameWrapper(parent, _browserAdapter); + _parentFrame = gcnew CefFrameWrapper(parent); } else if (parent == nullptr) { @@ -286,7 +287,7 @@ IBrowser^ CefFrameWrapper::Browser::get() return _owningBrowser; } - _owningBrowser = gcnew CefSharpBrowserWrapper(_frame->GetBrowser(), _browserAdapter); + _owningBrowser = gcnew CefSharpBrowserWrapper(_frame->GetBrowser()); return _owningBrowser; } diff --git a/CefSharp.Core/Internals/CefFrameWrapper.h b/CefSharp.Core/Internals/CefFrameWrapper.h index 45fecf694..07d5abd4d 100644 --- a/CefSharp.Core/Internals/CefFrameWrapper.h +++ b/CefSharp.Core/Internals/CefFrameWrapper.h @@ -24,15 +24,14 @@ namespace CefSharp { private: MCefRefPtr _frame; - IBrowserAdapter^ _browserAdapter; IFrame^ _parentFrame; IBrowser^ _owningBrowser; Object^ _syncRoot; bool _disposed; internal: - CefFrameWrapper::CefFrameWrapper(CefRefPtr &frame, IBrowserAdapter^ browserAdapter) - : _frame(frame), _browserAdapter(browserAdapter), _disposed(false), + CefFrameWrapper::CefFrameWrapper(CefRefPtr &frame) + : _frame(frame), _disposed(false), _parentFrame(nullptr), _owningBrowser(nullptr), _syncRoot(gcnew Object()) { } @@ -46,7 +45,6 @@ namespace CefSharp { this->!CefFrameWrapper(); - _browserAdapter = nullptr; delete _parentFrame; delete _owningBrowser; _parentFrame = nullptr; 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/Internals/CefSharpBrowserWrapper.cpp b/CefSharp.Core/Internals/CefSharpBrowserWrapper.cpp index 1605c96ce..87cc4c30f 100644 --- a/CefSharp.Core/Internals/CefSharpBrowserWrapper.cpp +++ b/CefSharp.Core/Internals/CefSharpBrowserWrapper.cpp @@ -154,7 +154,7 @@ IFrame^ CefSharpBrowserWrapper::MainFrame::get() { ThrowIfDisposed(); auto frame = _browser->GetMainFrame(); - return gcnew CefFrameWrapper(frame, _browserAdapter); + return gcnew CefFrameWrapper(frame); } /// @@ -164,7 +164,7 @@ IFrame^ CefSharpBrowserWrapper::MainFrame::get() IFrame^ CefSharpBrowserWrapper::FocusedFrame::get() { ThrowIfDisposed(); - return gcnew CefFrameWrapper(_browser->GetFocusedFrame(), _browserAdapter); + return gcnew CefFrameWrapper(_browser->GetFocusedFrame()); } /// @@ -174,7 +174,7 @@ IFrame^ CefSharpBrowserWrapper::FocusedFrame::get() IFrame^ CefSharpBrowserWrapper::GetFrame(Int64 identifier) { ThrowIfDisposed(); - return gcnew CefFrameWrapper(_browser->GetFrame(identifier), _browserAdapter); + return gcnew CefFrameWrapper(_browser->GetFrame(identifier)); } /// @@ -184,7 +184,7 @@ IFrame^ CefSharpBrowserWrapper::GetFrame(Int64 identifier) IFrame^ CefSharpBrowserWrapper::GetFrame(String^ name) { ThrowIfDisposed(); - return gcnew CefFrameWrapper(_browser->GetFrame(StringUtils::ToNative(name)), _browserAdapter); + return gcnew CefFrameWrapper(_browser->GetFrame(StringUtils::ToNative(name))); } /// diff --git a/CefSharp.Core/Internals/CefSharpBrowserWrapper.h b/CefSharp.Core/Internals/CefSharpBrowserWrapper.h index 2c2587f41..a1b0d9379 100644 --- a/CefSharp.Core/Internals/CefSharpBrowserWrapper.h +++ b/CefSharp.Core/Internals/CefSharpBrowserWrapper.h @@ -17,13 +17,12 @@ namespace CefSharp { private: MCefRefPtr _browser; - IBrowserAdapter^ _browserAdapter; IBrowserHost^ _browserHost; bool _disposed; internal: - CefSharpBrowserWrapper::CefSharpBrowserWrapper(CefRefPtr &browser, IBrowserAdapter^ browserAdapter) - : _browser(browser), _browserAdapter(browserAdapter), _disposed(false) + CefSharpBrowserWrapper::CefSharpBrowserWrapper(CefRefPtr &browser) + : _browser(browser), _disposed(false) { } @@ -36,7 +35,6 @@ namespace CefSharp ~CefSharpBrowserWrapper() { this->!CefSharpBrowserWrapper(); - _browserAdapter = nullptr; delete _browserHost; _browserHost = nullptr; _disposed = true; diff --git a/CefSharp.Core/Internals/ClientAdapter.cpp b/CefSharp.Core/Internals/ClientAdapter.cpp index c2b846b5d..fede990d9 100644 --- a/CefSharp.Core/Internals/ClientAdapter.cpp +++ b/CefSharp.Core/Internals/ClientAdapter.cpp @@ -18,6 +18,12 @@ #include "CefAuthCallbackWrapper.h" #include "CefJSDialogCallbackWrapper.h" #include "CefRequestCallbackWrapper.h" +#include "Serialization\Primitives.h" +#include "Serialization\V8Serialization.h" +#include "Messaging\Messages.h" + +using namespace CefSharp::Internals::Messaging; +using namespace CefSharp::Internals::Serialization; namespace CefSharp { @@ -71,7 +77,7 @@ namespace CefSharp bool createdWrapper = false; IBrowser^ browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup()); - CefFrameWrapper frameWrapper(frame, _browserAdapter); + CefFrameWrapper frameWrapper(frame); auto result = handler->OnBeforePopup( _browserControl, browserWrapper, %frameWrapper, StringUtils::ToClr(target_url), @@ -83,7 +89,7 @@ namespace CefSharp { if (browser->IsPopup()) { - auto browserWrapper = gcnew CefSharpBrowserWrapper(browser, _browserAdapter); + auto browserWrapper = gcnew CefSharpBrowserWrapper(browser); // Add to the list of popup browsers. _popupBrowsers->Add(browser->GetIdentifier(), browserWrapper); auto handler = _browserControl->PopupHandler; @@ -100,6 +106,9 @@ namespace CefSharp if (static_cast(_browserAdapter) != nullptr) { _browserAdapter->OnAfterBrowserCreated(browser->GetIdentifier()); + //save callback factory for this browser + //it's only going to be present after browseradapter is initialized + _javascriptCallbackFactories->Add(browser->GetIdentifier(), _browserAdapter->JavascriptCallbackFactory); } } } @@ -312,14 +321,14 @@ namespace CefSharp if (popupHandler != nullptr) { IBrowser^ browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), true); - CefFrameWrapper frameWrapper(frame, _browserAdapter); + CefFrameWrapper frameWrapper(frame); popupHandler->OnFrameLoadStart(_browserControl, gcnew FrameLoadStartEventArgs(browserWrapper, %frameWrapper)); } } else { IBrowser^ browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), false); - CefFrameWrapper frameWrapper(frame, _browserAdapter); + CefFrameWrapper frameWrapper(frame); _browserControl->OnFrameLoadStart(gcnew FrameLoadStartEventArgs(browserWrapper, %frameWrapper)); } } @@ -333,13 +342,13 @@ namespace CefSharp { IBrowser^ browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), true); - CefFrameWrapper frameWrapper(frame, _browserAdapter); + CefFrameWrapper frameWrapper(frame); popupHandler->OnFrameLoadEnd(_browserControl, gcnew FrameLoadEndEventArgs(browserWrapper, %frameWrapper, httpStatusCode)); } } else { - CefFrameWrapper frameWrapper(frame, _browserAdapter); + CefFrameWrapper frameWrapper(frame); auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), false); _browserControl->OnFrameLoadEnd(gcnew FrameLoadEndEventArgs(browserWrapper, %frameWrapper, httpStatusCode)); } @@ -353,14 +362,14 @@ namespace CefSharp if (popupHandler != nullptr) { IBrowser^ browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), true); - CefFrameWrapper frameWrapper(frame, _browserAdapter); + CefFrameWrapper frameWrapper(frame); popupHandler->OnLoadError(_browserControl, browserWrapper, gcnew LoadErrorEventArgs(%frameWrapper, static_cast(errorCode), StringUtils::ToClr(errorText), StringUtils::ToClr(failedUrl))); } } else { - CefFrameWrapper frameWrapper(frame, _browserAdapter); + CefFrameWrapper frameWrapper(frame); _browserControl->OnLoadError(%frameWrapper, (CefErrorCode)errorCode, StringUtils::ToClr(errorText), StringUtils::ToClr(failedUrl)); } } @@ -377,7 +386,7 @@ namespace CefSharp IBrowser^ browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), true); - CefFrameWrapper frameWrapper(frame, _browserAdapter); + CefFrameWrapper frameWrapper(frame); CefRequestWrapper requestWrapper(request); return popupHandler->OnBeforeBrowse(_browserControl, browserWrapper, %requestWrapper, isRedirect, %frameWrapper); } @@ -390,7 +399,7 @@ namespace CefSharp } IBrowser^ browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), false); - CefFrameWrapper frameWrapper(frame, _browserAdapter); + CefFrameWrapper frameWrapper(frame); CefRequestWrapper requestWrapper(request); return handler->OnBeforeBrowse(_browserControl, browserWrapper, %frameWrapper, %requestWrapper, isRedirect); @@ -485,7 +494,7 @@ namespace CefSharp { auto managedNewUrl = StringUtils::ToClr(newUrl); IBrowser^ browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), true);; - CefFrameWrapper frameWrapper(frame, _browserAdapter); + CefFrameWrapper frameWrapper(frame); popupHandler->OnResourceRedirect(_browserControl, browserWrapper, %frameWrapper, managedNewUrl); @@ -498,7 +507,7 @@ namespace CefSharp if (handler != nullptr) { auto managedNewUrl = StringUtils::ToClr(newUrl); - CefFrameWrapper frameWrapper(frame, _browserAdapter); + CefFrameWrapper frameWrapper(frame); handler->OnResourceRedirect(_browserControl, %frameWrapper, managedNewUrl); @@ -533,7 +542,7 @@ namespace CefSharp } IBrowser^ browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup()); - auto frameWrapper = gcnew CefFrameWrapper(frame, _browserAdapter); + auto frameWrapper = gcnew CefFrameWrapper(frame); auto requestWrapper = gcnew CefRequestWrapper(request); auto resourceHandler = factory->GetResourceHandler(_browserControl, browserWrapper, frameWrapper, requestWrapper); @@ -567,7 +576,7 @@ namespace CefSharp auto popupHandler = _browserControl->PopupHandler; if (popupHandler != nullptr) { - auto frameWrapper = gcnew CefFrameWrapper(frame, _browserAdapter); + auto frameWrapper = gcnew CefFrameWrapper(frame); auto requestWrapper = gcnew CefRequestWrapper(request); IBrowser^ browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), true); @@ -578,7 +587,7 @@ namespace CefSharp } else { - auto frameWrapper = gcnew CefFrameWrapper(frame, _browserAdapter); + auto frameWrapper = gcnew CefFrameWrapper(frame); auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), false); auto requestWrapper = gcnew CefRequestWrapper(request); auto requestCallback = gcnew CefRequestCallbackWrapper(callback, frameWrapper, requestWrapper); @@ -598,7 +607,7 @@ namespace CefSharp } auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup()); - auto frameWrapper = gcnew CefFrameWrapper(frame, _browserAdapter); + auto frameWrapper = gcnew CefFrameWrapper(frame); auto callbackWrapper = gcnew CefAuthCallbackWrapper(callback, frameWrapper); return handler->GetAuthCredentials( @@ -615,7 +624,7 @@ namespace CefSharp // Context menu params CefContextMenuParamsWrapper contextMenuParamsWrapper(params); - CefFrameWrapper frameWrapper(frame, _browserAdapter); + CefFrameWrapper frameWrapper(frame); auto result = handler->OnBeforeContextMenu(_browserControl, %frameWrapper, %contextMenuParamsWrapper); if (!result) { @@ -798,5 +807,60 @@ namespace CefSharp handler->OnDownloadUpdated(browserWrapper, TypeConversion::FromNative(download_item), callbackWrapper); } } + + + bool ClientAdapter::OnProcessMessageReceived(CefRefPtr browser, CefProcessId source_process, CefRefPtr message) + { + auto handled = false; + auto name = message->GetName(); + if (name == kEvaluateJavascriptResponse) + { + auto argList = message->GetArgumentList(); + auto success = argList->GetBool(0); + auto callbackId = GetInt64(argList, 1); + + IJavascriptCallbackFactory^ callbackFactory; + _javascriptCallbackFactories->TryGetValue(browser->GetIdentifier(), callbackFactory); + + auto pendingTask = _pendingTaskRepository->RemovePendingTask(callbackId); + if (pendingTask != nullptr) + { + auto response = gcnew JavascriptResponse(); + response->Success = success; + + if (success) + { + response->Result = DeserializeV8Object(argList, 2, callbackFactory); + } + else + { + response->Message = StringUtils::ToClr(argList->GetString(2)); + } + + pendingTask->SetResult(response); + } + + handled = true; + } + + return handled; + } + + Task^ ClientAdapter::EvaluateScriptAsync(int browserId, int64 frameId, String^ script, Nullable timeout) + { + //create a new taskcompletionsource + auto idAndComplectionSource = _pendingTaskRepository->CreatePendingTask(timeout); + + auto message = CefProcessMessage::Create(kEvaluateJavascriptRequest); + auto argList = message->GetArgumentList(); + argList->SetInt(0, browserId); + SetInt64(frameId, argList, 1); + SetInt64(idAndComplectionSource.Key, argList, 2); + argList->SetString(3, StringUtils::ToNative(script)); + + _cefBrowser->SendProcessMessage(CefProcessId::PID_RENDERER, message); + + return idAndComplectionSource.Value->Task; + } } } diff --git a/CefSharp.Core/Internals/ClientAdapter.h b/CefSharp.Core/Internals/ClientAdapter.h index bda148803..4985591da 100644 --- a/CefSharp.Core/Internals/ClientAdapter.h +++ b/CefSharp.Core/Internals/ClientAdapter.h @@ -35,9 +35,14 @@ namespace CefSharp gcroot _browserControl; HWND _browserHwnd; CefRefPtr _cefBrowser; + gcroot^> _popupBrowsers; gcroot _tooltip; gcroot _browserAdapter; + //contains in-progress eval script tasks + gcroot^> _pendingTaskRepository; + //contains js callback factories for each browser + gcroot^> _javascriptCallbackFactories; void ThrowUnknownPopupBrowser(String^ context) { @@ -50,13 +55,20 @@ namespace CefSharp ClientAdapter(IWebBrowserInternal^ browserControl, IBrowserAdapter^ browserAdapter) : _browserControl(browserControl), _popupBrowsers(gcnew Dictionary()), + _pendingTaskRepository(gcnew PendingTaskRepository()), + _javascriptCallbackFactories(gcnew Dictionary()), _browserAdapter(browserAdapter) { + } ~ClientAdapter() { CloseAllPopups(true); + + //this will dispose the repository and cancel all pending tasks + delete _pendingTaskRepository; + _browserControl = nullptr; _browserHwnd = nullptr; _cefBrowser = NULL; @@ -82,6 +94,8 @@ namespace CefSharp virtual DECL CefRefPtr GetDialogHandler() OVERRIDE { return this; } virtual DECL CefRefPtr GetDragHandler() OVERRIDE { return this; } virtual DECL CefRefPtr GetGeolocationHandler() OVERRIDE { return this; } + virtual DECL bool OnProcessMessageReceived(CefRefPtr browser, CefProcessId source_process, CefRefPtr message) OVERRIDE; + // CefLifeSpanHandler virtual DECL bool OnBeforePopup(CefRefPtr browser, CefRefPtr frame, @@ -158,6 +172,9 @@ namespace CefSharp virtual void OnDownloadUpdated(CefRefPtr browser, CefRefPtr download_item, CefRefPtr callback) OVERRIDE; + //sends out an eval script request to the render process + Task^ EvaluateScriptAsync(int browserId, int64 frameId, String^ script, Nullable timeout); + IMPLEMENT_REFCOUNTING(ClientAdapter); }; } diff --git a/CefSharp.Core/Internals/Messaging/Messages.h b/CefSharp.Core/Internals/Messaging/Messages.h new file mode 100644 index 000000000..dad7864dc --- /dev/null +++ b/CefSharp.Core/Internals/Messaging/Messages.h @@ -0,0 +1,23 @@ +// 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. + +#pragma once + +#include "include/cef_base.h" + +namespace CefSharp +{ + namespace Internals + { + namespace Messaging + { + //contains process message names for all handled messages + + //Message containing a script to be evaluated + const CefString kEvaluateJavascriptRequest = "EvaluateJavascriptRequest"; + //Message containing the result for a given evaluation + const CefString kEvaluateJavascriptResponse = "EvaluateJavascriptDoneResponse"; + } + } +} \ No newline at end of file diff --git a/CefSharp.Core/Internals/Serialization/Primitives.cpp b/CefSharp.Core/Internals/Serialization/Primitives.cpp new file mode 100644 index 000000000..ce2cc91ef --- /dev/null +++ b/CefSharp.Core/Internals/Serialization/Primitives.cpp @@ -0,0 +1,155 @@ +// 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. + +#include "Stdafx.h" +#include "Primitives.h" + +#include "include/cef_app.h" + +using namespace std; + +namespace CefSharp +{ + namespace Internals + { + namespace Serialization + { + enum class PrimitiveType : unsigned char + { + INT64, + CEFTIME, + JSCALLBACK + }; + + template + bool IsType(PrimitiveType type, CefRefPtr list, TIndex index) + { + auto result = list->GetType(index) == VTYPE_BINARY; + if (result) + { + underlying_type::type typeRead; + auto binaryValue = list->GetBinary(index); + binaryValue->GetData(&typeRead, sizeof(underlying_type::type), 0); + result = typeRead == static_cast::type>(type); + } + return result; + } + + template + void SetInt64(const int64 &value, CefRefPtr list, TIndex index) + { + unsigned char mem[1 + sizeof(int64)]; + mem[0] = static_cast(PrimitiveType::INT64); + memcpy(reinterpret_cast(mem + 1), &value, sizeof(int64)); + + auto binaryValue = CefBinaryValue::Create(mem, sizeof(mem)); + list->SetBinary(index, binaryValue); + } + + template + int64 GetInt64(CefRefPtr list, TIndex index) + { + int64 result; + + auto binaryValue = list->GetBinary(index); + binaryValue->GetData(&result, sizeof(int64), 1); + + return result; + } + + template + bool IsInt64(CefRefPtr list, TIndex index) + { + return IsType(PrimitiveType::INT64, list, index); + } + + template + void SetCefTime(const CefTime &value, CefRefPtr list, TIndex index) + { + auto doubleT = value.GetDoubleT(); + unsigned char mem[1 + sizeof(double)]; + mem[0] = static_cast(PrimitiveType::CEFTIME); + memcpy(reinterpret_cast(mem + 1), &doubleT, sizeof(double)); + + auto binaryValue = CefBinaryValue::Create(mem, sizeof(mem)); + list->SetBinary(index, binaryValue); + } + + template + CefTime GetCefTime(CefRefPtr list, TIndex index) + { + double doubleT; + + auto binaryValue = list->GetBinary(index); + binaryValue->GetData(&doubleT, sizeof(double), 1); + + return CefTime(doubleT); + } + + template + bool IsCefTime(CefRefPtr list, TIndex index) + { + return IsType(PrimitiveType::CEFTIME, list, index); + } + template + void SetJsCallback(JavascriptCallback^ value, CefRefPtr list, TIndex index) + { + auto id = value->Id; + auto browserId = value->BrowserId; + + unsigned char mem[1 + sizeof(int) + sizeof(int64)]; + mem[0] = static_cast(PrimitiveType::JSCALLBACK); + memcpy(reinterpret_cast(mem + 1), &browserId, sizeof(int)); + memcpy(reinterpret_cast(mem + 1 + sizeof(int)), &id, sizeof(int64)); + + auto binaryValue = CefBinaryValue::Create(mem, sizeof(mem)); + list->SetBinary(index, binaryValue); + } + + template + JavascriptCallback^ GetJsCallback(CefRefPtr list, TIndex index) + { + auto result = gcnew JavascriptCallback(); + int64 id; + int browserId; + + auto binaryValue = list->GetBinary(index); + binaryValue->GetData(&browserId, sizeof(int), 1); + binaryValue->GetData(&id, sizeof(int64), 1 + sizeof(int)); + + result->Id = id; + result->BrowserId = browserId; + + return result; + } + + template + bool IsJsCallback(CefRefPtr list, TIndex index) + { + return IsType(PrimitiveType::JSCALLBACK, list, index); + } + + template void SetInt64(const int64 &value, CefRefPtr list, int index); + template void SetInt64(const int64 &value, CefRefPtr list, CefString index); + template int64 GetInt64(CefRefPtr list, int index); + template int64 GetInt64(CefRefPtr list, CefString index); + template bool IsInt64(CefRefPtr list, int index); + template bool IsInt64(CefRefPtr list, CefString index); + + template void SetCefTime(const CefTime &value, CefRefPtr list, int index); + template void SetCefTime(const CefTime &value, CefRefPtr list, CefString index); + template CefTime GetCefTime(CefRefPtr list, int index); + template CefTime GetCefTime(CefRefPtr list, CefString index); + template bool IsCefTime(CefRefPtr list, int index); + template bool IsCefTime(CefRefPtr list, CefString index); + + template void SetJsCallback(JavascriptCallback^ value, CefRefPtr list, int index); + template void SetJsCallback(JavascriptCallback^ value, CefRefPtr list, CefString index); + template JavascriptCallback^ GetJsCallback(CefRefPtr list, int index); + template JavascriptCallback^ GetJsCallback(CefRefPtr list, CefString index); + template bool IsJsCallback(CefRefPtr list, int index); + template bool IsJsCallback(CefRefPtr list, CefString index); + } + } +} \ No newline at end of file diff --git a/CefSharp.Core/Internals/Serialization/Primitives.h b/CefSharp.Core/Internals/Serialization/Primitives.h new file mode 100644 index 000000000..e451ae6ff --- /dev/null +++ b/CefSharp.Core/Internals/Serialization/Primitives.h @@ -0,0 +1,37 @@ +// 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. + +#pragma once + +namespace CefSharp +{ + namespace Internals + { + namespace Serialization + { + //Functions to serialize/deserialize specific types into CefBinaryValue + + template + void SetInt64(const int64 &value, CefRefPtr list, TIndex index); + template + int64 GetInt64(CefRefPtr list, TIndex index); + template + bool IsInt64(CefRefPtr list, TIndex index); + + template + void SetCefTime(const CefTime &value, CefRefPtr list, TIndex index); + template + CefTime GetCefTime(CefRefPtr list, TIndex index); + template + bool IsCefTime(CefRefPtr list, TIndex index); + + template + void SetJsCallback(JavascriptCallback^ value, CefRefPtr list, TIndex index); + template + JavascriptCallback^ GetJsCallback(CefRefPtr list, TIndex index); + template + bool IsJsCallback(CefRefPtr list, TIndex index); + } + } +} \ No newline at end of file diff --git a/CefSharp.Core/Internals/Serialization/V8Serialization.cpp b/CefSharp.Core/Internals/Serialization/V8Serialization.cpp new file mode 100644 index 000000000..d0e91f587 --- /dev/null +++ b/CefSharp.Core/Internals/Serialization/V8Serialization.cpp @@ -0,0 +1,93 @@ +// 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. + +#include "Stdafx.h" +#include "V8Serialization.h" +#include "Primitives.h" + +namespace CefSharp +{ + namespace Internals + { + namespace Serialization + { + template + Object^ DeserializeV8Object(CefRefPtr list, TIndex index, IJavascriptCallbackFactory^ javascriptCallbackFactory) + { + Object^ result = nullptr; + auto type = list->GetType(index); + + if (type == VTYPE_BOOL) + { + result = list->GetBool(index); + } + else if (type == VTYPE_INT) + { + result = list->GetInt(index); + } + else if (IsInt64(list, index)) + { + result = GetInt64(list, index); + } + else if (IsCefTime(list, index)) + { + auto cefTime = GetCefTime(list, index); + result = ConvertCefTimeToDateTime(cefTime); + } + else if (IsJsCallback(list, index) && javascriptCallbackFactory != nullptr) + { + auto jsCallbackDto = GetJsCallback(list, index); + result = javascriptCallbackFactory->Create(jsCallbackDto); + } + else if (type == VTYPE_DOUBLE) + { + result = list->GetDouble(index); + } + else if (type == VTYPE_STRING) + { + result = StringUtils::ToClr(list->GetString(index)); + } + else if (type == VTYPE_LIST) + { + auto subList = list->GetList(index); + auto array = gcnew List(subList->GetSize()); + for (auto i = 0; i < subList->GetSize(); i++) + { + array->Add(DeserializeV8Object(subList, i, javascriptCallbackFactory)); + } + result = array; + } + else if (type == VTYPE_DICTIONARY) + { + auto dict = gcnew Dictionary(); + auto subDict = list->GetDictionary(index); + std::vector keys; + subDict->GetKeys(keys); + + for (auto i = 0; i < keys.size(); i++) + { + dict->Add(StringUtils::ToClr(keys[i]), DeserializeV8Object(subDict, keys[i], javascriptCallbackFactory)); + } + + result = dict; + } + + return result; + } + + DateTime ConvertCefTimeToDateTime(CefTime time) + { + auto epoch = time.GetDoubleT(); + if (epoch == 0) + { + return DateTime::MinValue; + } + return DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(epoch).ToLocalTime(); + } + + template Object^ DeserializeV8Object(CefRefPtr list, int index, IJavascriptCallbackFactory^ javascriptCallbackFactory); + template Object^ DeserializeV8Object(CefRefPtr list, CefString index, IJavascriptCallbackFactory^ javascriptCallbackFactory); + } + } +} \ No newline at end of file diff --git a/CefSharp.Core/Internals/Serialization/V8Serialization.h b/CefSharp.Core/Internals/Serialization/V8Serialization.h new file mode 100644 index 000000000..714846891 --- /dev/null +++ b/CefSharp.Core/Internals/Serialization/V8Serialization.h @@ -0,0 +1,24 @@ +// 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. + +#pragma once + +namespace CefSharp +{ + namespace Internals + { + namespace Serialization + { + //Functions to deserialize data returned from the render process. + + //Deserializes data into Object from a given index of a CefListValue or CefDictionaryValue + //IJavascriptCallbackFactory implementation should be passed to allow creation of valid javascript callbacks + template + Object^ DeserializeV8Object(CefRefPtr list, TIndex index, IJavascriptCallbackFactory^ javascriptCallbackFactory); + + //Converts CefTime to DateTime + DateTime ConvertCefTimeToDateTime(CefTime time); + } + } +} \ No newline at end of file diff --git a/CefSharp.Core/ManagedCefBrowserAdapter.cpp b/CefSharp.Core/ManagedCefBrowserAdapter.cpp index bcc744df2..298649bd7 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 @@ -56,7 +54,7 @@ void ManagedCefBrowserAdapter::OnAfterBrowserCreated(int browserId) auto browser = _clientAdapter->GetCefBrowser(); if (browser != nullptr) { - _browserWrapper = gcnew CefSharpBrowserWrapper(browser, this); + _browserWrapper = gcnew CefSharpBrowserWrapper(browser); } if (_webBrowserInternal != nullptr) @@ -284,50 +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); - } - - if (!_wcfEnabled) - { - throw gcnew InvalidOperationException("To wait for javascript code set WcfEnabled true in CefSettings during initialization."); - } - - return _browserProcessServiceHost->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); - } - - if (!_wcfEnabled) - { - throw gcnew InvalidOperationException("To wait for javascript code set WcfEnabled true in CefSettings during initialization."); - } - - auto browser = _clientAdapter->GetCefBrowser(); - - if (_browserProcessServiceHost == nullptr || browser == nullptr) - { - return nullptr; - } - - auto frame = browser->GetMainFrame(); - - if (frame == nullptr) - { - return nullptr; - } - - return _browserProcessServiceHost->EvaluateScriptAsync(browser->GetIdentifier(), frame->GetIdentifier(), script, timeout); -} - void ManagedCefBrowserAdapter::CreateBrowser(BrowserSettings^ browserSettings, IntPtr sourceHandle, String^ address) { HWND hwnd = static_cast(sourceHandle.ToPointer()); @@ -381,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."); } @@ -505,7 +459,7 @@ IFrame^ ManagedCefBrowserAdapter::GetMainFrame() { return nullptr; } - return gcnew CefFrameWrapper(browser->GetMainFrame(), this); + return gcnew CefFrameWrapper(browser->GetMainFrame()); } /// @@ -520,7 +474,7 @@ IFrame^ ManagedCefBrowserAdapter::GetFocusedFrame() return nullptr; } - return gcnew CefFrameWrapper(browser->GetFocusedFrame(), this); + return gcnew CefFrameWrapper(browser->GetFocusedFrame()); } /// @@ -540,7 +494,7 @@ IFrame^ ManagedCefBrowserAdapter::GetFrame(System::Int64 identifier) return nullptr; } - return gcnew CefFrameWrapper(result, this); + return gcnew CefFrameWrapper(result); } /// @@ -560,7 +514,7 @@ IFrame^ ManagedCefBrowserAdapter::GetFrame(String^ name) return nullptr; } - return gcnew CefFrameWrapper(result, this); + return gcnew CefFrameWrapper(result); } /// @@ -571,3 +525,13 @@ IBrowser^ ManagedCefBrowserAdapter::GetBrowser() { return _browserWrapper; } + +IJavascriptCallbackFactory^ ManagedCefBrowserAdapter::JavascriptCallbackFactory::get() +{ + IJavascriptCallbackFactory^ result = nullptr; + if (_browserProcessServiceHost != nullptr) + { + result = _browserProcessServiceHost->JavascriptCallbackFactory; + } + return result; +} diff --git a/CefSharp.Core/ManagedCefBrowserAdapter.h b/CefSharp.Core/ManagedCefBrowserAdapter.h index e662c741d..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(); @@ -165,5 +162,10 @@ namespace CefSharp /// /// Gets the current instance or null virtual IBrowser^ GetBrowser(); + + virtual property IJavascriptCallbackFactory^ JavascriptCallbackFactory + { + CefSharp::Internals::IJavascriptCallbackFactory^ get(); + } }; } diff --git a/CefSharp.Core/SchemeHandlerFactoryWrapper.h b/CefSharp.Core/SchemeHandlerFactoryWrapper.h index 6edef9f81..b728b4942 100644 --- a/CefSharp.Core/SchemeHandlerFactoryWrapper.h +++ b/CefSharp.Core/SchemeHandlerFactoryWrapper.h @@ -32,8 +32,8 @@ namespace CefSharp // NOTE: NOT supplying a browser adapter here means they currently // can't issue a JavaScript ExecuteScriptAsync call from any IFrame generated via browserWrapper // and IFrame itself. - auto browserWrapper = gcnew CefSharpBrowserWrapper(browser, nullptr); - auto frameWrapper = gcnew CefFrameWrapper(frame, nullptr); + auto browserWrapper = gcnew CefSharpBrowserWrapper(browser); + auto frameWrapper = gcnew CefFrameWrapper(frame); auto requestWrapper = gcnew CefRequestWrapper(request); auto handler = _factory->Create(browserWrapper, frameWrapper, StringUtils::ToClr(schemeName), requestWrapper); 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 1864dcf7b..ee33d38b5 100644 --- a/CefSharp/CefSharp.csproj +++ b/CefSharp/CefSharp.csproj @@ -92,6 +92,7 @@ + @@ -112,11 +113,14 @@ Code + + + 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/BrowserProcessServiceHost.cs b/CefSharp/Internals/BrowserProcessServiceHost.cs index 23f57b7c9..0e458760a 100644 --- a/CefSharp/Internals/BrowserProcessServiceHost.cs +++ b/CefSharp/Internals/BrowserProcessServiceHost.cs @@ -15,6 +15,7 @@ namespace CefSharp.Internals { private const long OneHundredAndTwentyEightMegaBytesInBytes = 128*1024*1024; + public IJavascriptCallbackFactory JavascriptCallbackFactory { get; private set; } public JavascriptObjectRepository JavascriptObjectRepository { get; private set; } private TaskCompletionSource operationContextTaskCompletionSource = new TaskCompletionSource(); @@ -22,6 +23,7 @@ namespace CefSharp.Internals : base(typeof(BrowserProcessService), new Uri[0]) { JavascriptObjectRepository = javascriptObjectRepository; + JavascriptCallbackFactory = new JavascriptCallbackFactory(new WeakReference(this)); var serviceName = RenderprocessClientFactory.GetServiceName(parentProcessId, browserId); @@ -49,20 +51,6 @@ namespace CefSharp.Internals operationContextTaskCompletionSource.SetResult(operationContext); } - public Task EvaluateScriptAsync(int browserId, long frameId, string script, TimeSpan? timeout) - { - var operationContextTask = operationContextTaskCompletionSource.Task; - // Use TaskScheduler.Default to ensure that the work occurs - // on a background thread. - return operationContextTask.ContinueWith(t => - { - var context = t.Result; - var renderProcess = context.GetCallbackChannel(); - var asyncResult = renderProcess.BeginEvaluateScriptAsync(browserId, frameId, script, timeout, null, null); - return Task.Factory.FromAsync(asyncResult, renderProcess.EndEvaluateScriptAsync); - }, TaskScheduler.Default).Unwrap(); - } - protected override void OnClose(TimeSpan timeout) { var task = operationContextTaskCompletionSource.Task; diff --git a/CefSharp/Internals/IBrowserAdapter.cs b/CefSharp/Internals/IBrowserAdapter.cs index 312a96757..1dda9138c 100644 --- a/CefSharp/Internals/IBrowserAdapter.cs +++ b/CefSharp/Internals/IBrowserAdapter.cs @@ -2,9 +2,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.Internals { /// @@ -13,8 +10,7 @@ namespace CefSharp.Internals /// public interface IBrowserAdapter { - Task EvaluateScriptAsync(string script, TimeSpan? timeout); - Task EvaluateScriptAsync(int browserId, Int64 frameId, string script, TimeSpan? timeout); + IJavascriptCallbackFactory JavascriptCallbackFactory { get; } void OnAfterBrowserCreated(int browserId); IBrowser GetBrowser(); } diff --git a/CefSharp/Internals/IJavascriptCallbackFactory.cs b/CefSharp/Internals/IJavascriptCallbackFactory.cs new file mode 100644 index 000000000..f9804e31d --- /dev/null +++ b/CefSharp/Internals/IJavascriptCallbackFactory.cs @@ -0,0 +1,14 @@ +// 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.Internals +{ + /// + /// Interface to convert a JavascriptCallback dto to a callable implementation. + /// + public interface IJavascriptCallbackFactory + { + IJavascriptCallback Create(JavascriptCallback callback); + } +} diff --git a/CefSharp/Internals/IRenderProcess.cs b/CefSharp/Internals/IRenderProcess.cs index 8b2c70a68..4b232314f 100644 --- a/CefSharp/Internals/IRenderProcess.cs +++ b/CefSharp/Internals/IRenderProcess.cs @@ -11,11 +11,6 @@ namespace CefSharp.Internals [ServiceContract] public interface IRenderProcess { - [OperationContract(AsyncPattern=true)] - IAsyncResult BeginEvaluateScriptAsync(int browserId, long frameId, string script, TimeSpan? timeout, AsyncCallback callback, object state); - - JavascriptResponse EndEvaluateScriptAsync(IAsyncResult result); - [OperationContract(AsyncPattern = true)] IAsyncResult BeginJavascriptCallbackAsync(int browserId, long callbackId, object[] parameters, TimeSpan? timeout, AsyncCallback callback, object state); diff --git a/CefSharp/Internals/JavascriptCallbackEndpointBehavior.cs b/CefSharp/Internals/JavascriptCallbackEndpointBehavior.cs index ad4a8be12..36a50624c 100644 --- a/CefSharp/Internals/JavascriptCallbackEndpointBehavior.cs +++ b/CefSharp/Internals/JavascriptCallbackEndpointBehavior.cs @@ -16,7 +16,6 @@ namespace CefSharp.Internals private static readonly List Methods = new List { ReflectionUtils.GetMethodName(p => p.CallMethod(0, null, null)), - ReflectionUtils.GetMethodName(p => p.BeginEvaluateScriptAsync(0, 0, null, null, null, null)).Substring(5), ReflectionUtils.GetMethodName(p => p.BeginJavascriptCallbackAsync(0, 0, null, null, null, null)).Substring(5), }; diff --git a/CefSharp/Internals/JavascriptCallbackFactory.cs b/CefSharp/Internals/JavascriptCallbackFactory.cs new file mode 100644 index 000000000..cf197be0e --- /dev/null +++ b/CefSharp/Internals/JavascriptCallbackFactory.cs @@ -0,0 +1,19 @@ +using System; + +namespace CefSharp.Internals +{ + public class JavascriptCallbackFactory : IJavascriptCallbackFactory + { + private readonly WeakReference browserProcessServiceHost; + + public JavascriptCallbackFactory(WeakReference browserProcessServiceHost) + { + this.browserProcessServiceHost = browserProcessServiceHost; + } + + public IJavascriptCallback Create(JavascriptCallback callback) + { + return new JavascriptCallbackProxy(callback.Id, callback.BrowserId, browserProcessServiceHost); + } + } +} diff --git a/CefSharp/Internals/JavascriptCallbackSurrogate.cs b/CefSharp/Internals/JavascriptCallbackSurrogate.cs index ae2007ec3..29c72e773 100644 --- a/CefSharp/Internals/JavascriptCallbackSurrogate.cs +++ b/CefSharp/Internals/JavascriptCallbackSurrogate.cs @@ -10,13 +10,11 @@ using System.Runtime.Serialization; namespace CefSharp.Internals { - internal sealed class JavascriptCallbackSurrogate : IDataContractSurrogate + internal sealed class JavascriptCallbackSurrogate : JavascriptCallbackFactory, IDataContractSurrogate { - private readonly WeakReference browserProcessWeakReference; - public JavascriptCallbackSurrogate(WeakReference browserProcessWeakReference) + :base(browserProcessWeakReference) { - this.browserProcessWeakReference = browserProcessWeakReference; } public Type GetDataContractType(Type type) @@ -39,7 +37,7 @@ namespace CefSharp.Internals var dto = obj as JavascriptCallback; if (dto != null) { - result = new JavascriptCallbackProxy(dto.Id, dto.BrowserId, browserProcessWeakReference); + result = Create(dto); } return result; } diff --git a/CefSharp/Internals/PendingTaskRepository.cs b/CefSharp/Internals/PendingTaskRepository.cs new file mode 100644 index 000000000..97eb769e2 --- /dev/null +++ b/CefSharp/Internals/PendingTaskRepository.cs @@ -0,0 +1,64 @@ +// Copyright © 2010-2015 The CefSharp Project. All rights reserved. +// +// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. + +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; + +namespace CefSharp.Internals +{ + /// + /// Class to store TaskCompletionSources indexed by a unique id. + /// + /// The type of the result produced by the tasks held. + public sealed class PendingTaskRepository + { + private readonly ConcurrentDictionary> pendingTasks = + new ConcurrentDictionary>(); + //should only be accessed by Interlocked.Increment + private long lastId; + + /// + /// Creates a new pending task with a timeout. + /// + /// The maximum running time of the task. + /// The unique id of the newly created pending task and the newly created . + public KeyValuePair> CreatePendingTask(TimeSpan? timeout = null) + { + var taskCompletionSource = new TaskCompletionSource(); + + var id = Interlocked.Increment(ref lastId); + pendingTasks.TryAdd(id, taskCompletionSource); + + if (timeout.HasValue) + { + Timer timer = null; + timer = new Timer(state => + { + timer.Dispose(); + RemovePendingTask(id); + taskCompletionSource.TrySetCanceled(); + }, null, timeout.Value, TimeSpan.FromMilliseconds(-1)); + } + + return new KeyValuePair>(id, taskCompletionSource); + } + + /// + /// Gets and removed pending task by id. + /// + /// Unique id of the pending task. + /// + /// The associated with the given id. + /// + public TaskCompletionSource RemovePendingTask(long id) + { + TaskCompletionSource result; + pendingTasks.TryRemove(id, out result); + return result; + } + } +} \ No newline at end of file diff --git a/CefSharp/WebBrowserExtensions.cs b/CefSharp/WebBrowserExtensions.cs index f61825b44..dafe1c10f 100644 --- a/CefSharp/WebBrowserExtensions.cs +++ b/CefSharp/WebBrowserExtensions.cs @@ -538,6 +538,21 @@ 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); + } + + using (var frame = browser.GetMainFrame()) + { + ThrowExceptionIfFrameNull(frame); + + return frame.EvaluateScriptAsync(script, timeout); + } + } + private static void ThrowExceptionIfFrameNull(IFrame frame) { if (frame == null)