mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-05 11:42:36 +00:00
Merge pull request #887 from amaitland/feature/jsb-callback
Javascript Callbacks - Take 2
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
// Copyright © 2010-2014 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 "Stdafx.h"
|
||||
|
||||
#include "CefBrowserWrapper.h"
|
||||
|
||||
namespace CefSharp
|
||||
{
|
||||
CefBrowserWrapper::CefBrowserWrapper(CefRefPtr<CefBrowser> cefBrowser)
|
||||
{
|
||||
_cefBrowser = cefBrowser;
|
||||
BrowserId = cefBrowser->GetIdentifier();
|
||||
IsPopup = cefBrowser->IsPopup();
|
||||
_callbackRegistry = gcnew JavascriptCallbackRegistry(BrowserId);
|
||||
}
|
||||
|
||||
JavascriptRootObjectWrapper^ CefBrowserWrapper::JavascriptRootObjectWrapper::get()
|
||||
{
|
||||
return _javascriptRootObjectWrapper;
|
||||
}
|
||||
|
||||
void CefBrowserWrapper::JavascriptRootObjectWrapper::set(CefSharp::JavascriptRootObjectWrapper^ value)
|
||||
{
|
||||
_javascriptRootObjectWrapper = value;
|
||||
if (_javascriptRootObjectWrapper != nullptr)
|
||||
{
|
||||
_javascriptRootObjectWrapper->CallbackRegistry = _callbackRegistry;
|
||||
}
|
||||
}
|
||||
|
||||
JavascriptResponse^ CefBrowserWrapper::EvaluateScriptInContext(CefRefPtr<CefV8Context> context, CefString script)
|
||||
{
|
||||
CefRefPtr<CefV8Value> result;
|
||||
CefRefPtr<CefV8Exception> 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;
|
||||
}
|
||||
|
||||
void CefBrowserWrapper::DoDispose(bool disposing)
|
||||
{
|
||||
_cefBrowser = nullptr;
|
||||
if (disposing)
|
||||
{
|
||||
delete _callbackRegistry;
|
||||
_callbackRegistry = nullptr;
|
||||
}
|
||||
DisposableResource::DoDispose(disposing);
|
||||
}
|
||||
|
||||
JavascriptResponse^ CefBrowserWrapper::DoEvaluateScript(System::Int64 frameId, String^ script)
|
||||
{
|
||||
auto frame = _cefBrowser->GetFrame(frameId);
|
||||
CefRefPtr<CefV8Context> 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<Object^>^ parameters)
|
||||
{
|
||||
return _callbackRegistry->Execute(callbackId, parameters);
|
||||
}
|
||||
|
||||
void CefBrowserWrapper::DestroyJavascriptCallback(Int64 id)
|
||||
{
|
||||
_callbackRegistry->Deregister(id);
|
||||
}
|
||||
|
||||
CefBrowserWrapper::~CefBrowserWrapper()
|
||||
{
|
||||
_cefBrowser = nullptr;
|
||||
delete _callbackRegistry;
|
||||
_callbackRegistry = nullptr;
|
||||
}
|
||||
}
|
||||
@@ -26,70 +26,32 @@ namespace CefSharp
|
||||
|
||||
private:
|
||||
MCefRefPtr<CefBrowser> _cefBrowser;
|
||||
JavascriptCallbackRegistry^ _callbackRegistry;
|
||||
JavascriptRootObjectWrapper^ _javascriptRootObjectWrapper;
|
||||
|
||||
public:
|
||||
CefBrowserWrapper(CefRefPtr<CefBrowser> cefBrowser)
|
||||
{
|
||||
_cefBrowser = cefBrowser;
|
||||
BrowserId = cefBrowser->GetIdentifier();
|
||||
IsPopup = cefBrowser->IsPopup();
|
||||
}
|
||||
|
||||
~CefBrowserWrapper()
|
||||
{
|
||||
_cefBrowser = nullptr;
|
||||
}
|
||||
CefBrowserWrapper(CefRefPtr<CefBrowser> cefBrowser);
|
||||
~CefBrowserWrapper();
|
||||
|
||||
property int BrowserId;
|
||||
property bool IsPopup;
|
||||
property DuplexChannelFactory<IBrowserProcess^>^ ChannelFactory;
|
||||
property JavascriptRootObject^ JavascriptRootObject;
|
||||
property JavascriptRootObjectWrapper^ JavascriptRootObjectWrapper;
|
||||
property JavascriptRootObjectWrapper^ JavascriptRootObjectWrapper
|
||||
{
|
||||
CefSharp::JavascriptRootObjectWrapper^ get();
|
||||
void set(CefSharp::JavascriptRootObjectWrapper^ value);
|
||||
};
|
||||
property IBrowserProcess^ BrowserProcess;
|
||||
|
||||
JavascriptResponse^ EvaluateScriptInContext(CefRefPtr<CefV8Context> context, CefString script)
|
||||
{
|
||||
CefRefPtr<CefV8Value> result;
|
||||
CefRefPtr<CefV8Exception> exception;
|
||||
JavascriptResponse^ response = gcnew JavascriptResponse();
|
||||
JavascriptResponse^ EvaluateScriptInContext(CefRefPtr<CefV8Context> context, CefString script);
|
||||
|
||||
response->Success = context->Eval(script, result, exception);
|
||||
if (response->Success)
|
||||
{
|
||||
response->Result = TypeUtils::ConvertFromCef(result);
|
||||
}
|
||||
else if (exception.get())
|
||||
{
|
||||
response->Message = StringUtils::ToClr(exception->GetMessage());
|
||||
}
|
||||
virtual void DoDispose(bool disposing) override;
|
||||
|
||||
return response;
|
||||
}
|
||||
virtual JavascriptResponse^ DoEvaluateScript(System::Int64 frameId, String^ script);
|
||||
|
||||
virtual void DoDispose( bool disposing ) override
|
||||
{
|
||||
_cefBrowser = nullptr;
|
||||
DisposableResource::DoDispose( disposing );
|
||||
}
|
||||
JavascriptResponse^ DoCallback(System::Int64 callbackId, array<Object^>^ parameters);
|
||||
|
||||
virtual JavascriptResponse^ DoEvaluateScript(System::Int64 frameId, String^ script)
|
||||
{
|
||||
auto frame = _cefBrowser->GetFrame(frameId);
|
||||
CefRefPtr<CefV8Context> context = frame->GetV8Context();
|
||||
|
||||
if (context.get() && context->Enter())
|
||||
{
|
||||
try
|
||||
{
|
||||
return EvaluateScriptInContext(context, StringUtils::ToNative(script));
|
||||
}
|
||||
finally
|
||||
{
|
||||
context->Exit();
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
void DestroyJavascriptCallback(Int64 id);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -157,6 +157,8 @@
|
||||
<ClInclude Include="CefAppWrapper.h" />
|
||||
<ClInclude Include="CefBrowserWrapper.h" />
|
||||
<ClInclude Include="CefTaskScheduler.h" />
|
||||
<ClInclude Include="JavascriptCallbackRegistry.h" />
|
||||
<ClInclude Include="JavascriptCallbackWrapper.h" />
|
||||
<ClInclude Include="JavascriptMethodWrapper.h" />
|
||||
<ClInclude Include="JavascriptMethodHandler.h" />
|
||||
<ClInclude Include="JavascriptObjectWrapper.h" />
|
||||
@@ -172,10 +174,15 @@
|
||||
<ClCompile Include="AssemblyInfo.cpp" />
|
||||
<ClCompile Include="CefAppUnmanagedWrapper.cpp" />
|
||||
<ClCompile Include="CefAppWrapper.cpp" />
|
||||
<ClCompile Include="CefBrowserWrapper.cpp" />
|
||||
<ClCompile Include="CefTaskScheduler.cpp" />
|
||||
<ClCompile Include="JavascriptCallbackRegistry.cpp" />
|
||||
<ClCompile Include="JavascriptCallbackWrapper.cpp" />
|
||||
<ClCompile Include="JavascriptMethodHandler.cpp" />
|
||||
<ClCompile Include="JavascriptMethodWrapper.cpp" />
|
||||
<ClCompile Include="JavascriptObjectWrapper.cpp" />
|
||||
<ClCompile Include="JavascriptPropertyWrapper.cpp" />
|
||||
<ClCompile Include="JavascriptRootObjectWrapper.cpp" />
|
||||
<ClCompile Include="Stdafx.cpp">
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
|
||||
|
||||
@@ -56,6 +56,12 @@
|
||||
<ClInclude Include="CefBrowserWrapper.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="JavascriptCallbackRegistry.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="JavascriptCallbackWrapper.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="AssemblyInfo.cpp">
|
||||
@@ -88,6 +94,21 @@
|
||||
<ClCompile Include="CefTaskScheduler.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="CefBrowserWrapper.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="JavascriptMethodHandler.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="JavascriptRootObjectWrapper.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="JavascriptCallbackRegistry.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="JavascriptCallbackWrapper.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
// Copyright © 2010-2014 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 "JavascriptCallbackRegistry.h"
|
||||
|
||||
using namespace System::Threading;
|
||||
|
||||
namespace CefSharp
|
||||
{
|
||||
namespace Internals
|
||||
{
|
||||
JavascriptCallback^ JavascriptCallbackRegistry::Register(CefRefPtr<CefV8Context> context, CefRefPtr<CefV8Value> value)
|
||||
{
|
||||
Int64 newId = Interlocked::Increment(_lastId);
|
||||
JavascriptCallbackWrapper^ wrapper = gcnew JavascriptCallbackWrapper(value, context);
|
||||
_callbacks->TryAdd(newId, wrapper);
|
||||
|
||||
auto result = gcnew JavascriptCallback();
|
||||
result->Id = newId;
|
||||
result->BrowserId = _browserId;
|
||||
return result;
|
||||
}
|
||||
|
||||
JavascriptResponse^ JavascriptCallbackRegistry::Execute(Int64 id, array<Object^>^ params)
|
||||
{
|
||||
JavascriptCallbackWrapper^ callback;
|
||||
|
||||
if (_callbacks->TryGetValue(id, callback))
|
||||
{
|
||||
return callback->Execute(params);
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void JavascriptCallbackRegistry::Deregister(Int64 id)
|
||||
{
|
||||
JavascriptCallbackWrapper^ callback;
|
||||
if(_callbacks->TryRemove(id, callback))
|
||||
{
|
||||
delete callback;
|
||||
}
|
||||
}
|
||||
|
||||
JavascriptCallbackRegistry::~JavascriptCallbackRegistry()
|
||||
{
|
||||
for each (JavascriptCallbackWrapper^ callback in _callbacks->Values)
|
||||
{
|
||||
delete callback;
|
||||
}
|
||||
_callbacks->Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Copyright © 2010-2014 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 "JavascriptCallbackWrapper.h"
|
||||
|
||||
using namespace System::Collections::Concurrent;
|
||||
|
||||
namespace CefSharp
|
||||
{
|
||||
namespace Internals
|
||||
{
|
||||
private ref class JavascriptCallbackRegistry
|
||||
{
|
||||
private:
|
||||
int _browserId;
|
||||
Int64 _lastId;
|
||||
ConcurrentDictionary<Int64, JavascriptCallbackWrapper^>^ _callbacks;
|
||||
public:
|
||||
JavascriptCallbackRegistry(int browserId) : _browserId(browserId)
|
||||
{
|
||||
_callbacks = gcnew ConcurrentDictionary<Int64, JavascriptCallbackWrapper^>();
|
||||
}
|
||||
JavascriptCallback^ Register(CefRefPtr<CefV8Context> context, CefRefPtr<CefV8Value> value);
|
||||
JavascriptResponse^ Execute(Int64 id, array<Object^>^ params);
|
||||
void Deregister(Int64 id);
|
||||
~JavascriptCallbackRegistry();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
// Copyright © 2010-2014 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 "TypeUtils.h"
|
||||
#include "JavascriptCallbackWrapper.h"
|
||||
|
||||
namespace CefSharp
|
||||
{
|
||||
namespace Internals
|
||||
{
|
||||
JavascriptCallbackWrapper::~JavascriptCallbackWrapper()
|
||||
{
|
||||
value = nullptr;
|
||||
context = nullptr;
|
||||
}
|
||||
|
||||
JavascriptResponse^ JavascriptCallbackWrapper::Execute(array<Object^>^ parms)
|
||||
{
|
||||
JavascriptResponse^ response = nullptr;
|
||||
if (context->Enter())
|
||||
{
|
||||
response = gcnew JavascriptResponse();
|
||||
try
|
||||
{
|
||||
CefV8ValueList args;
|
||||
for each (auto parm in parms)
|
||||
{
|
||||
auto cefParm = TypeUtils::ConvertToCef(parm, nullptr);
|
||||
args.push_back(cefParm);
|
||||
}
|
||||
|
||||
auto retval = value->ExecuteFunctionWithContext(context.get(), nullptr, args);
|
||||
response->Success = retval != nullptr;
|
||||
if (response->Success)
|
||||
{
|
||||
response->Result = TypeUtils::ConvertFromCef(retval);
|
||||
}
|
||||
else
|
||||
{
|
||||
auto exception = value->GetException();
|
||||
if (exception.get())
|
||||
{
|
||||
response->Message = StringUtils::ToClr(exception->GetMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
context->Exit();
|
||||
}
|
||||
}
|
||||
return response;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// Copyright © 2010-2014 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_v8.h>
|
||||
|
||||
using namespace CefSharp::Internals;
|
||||
|
||||
namespace CefSharp
|
||||
{
|
||||
namespace Internals
|
||||
{
|
||||
private ref class JavascriptCallbackWrapper
|
||||
{
|
||||
private:
|
||||
MCefRefPtr<CefV8Value> value;
|
||||
MCefRefPtr<CefV8Context> context;
|
||||
public:
|
||||
JavascriptCallbackWrapper(CefRefPtr<CefV8Value> value, CefRefPtr<CefV8Context> context)
|
||||
: value(value), context(context) {}
|
||||
|
||||
JavascriptResponse^ Execute(array<Object^>^ parms);
|
||||
|
||||
~JavascriptCallbackWrapper();
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
// Copyright © 2010-2013 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 "TypeUtils.h"
|
||||
#include "JavascriptMethodHandler.h"
|
||||
|
||||
using namespace CefSharp::Internals;
|
||||
|
||||
namespace CefSharp
|
||||
{
|
||||
JavascriptMethodHandler::JavascriptMethodHandler(Func<array<Object^>^, BrowserProcessResponse^>^ method, JavascriptCallbackRegistry^ callbackRegistry)
|
||||
{
|
||||
_method = method;
|
||||
_callbackRegistry = callbackRegistry;
|
||||
}
|
||||
|
||||
JavascriptMethodHandler::~JavascriptMethodHandler()
|
||||
{
|
||||
delete _method;
|
||||
delete _callbackRegistry;
|
||||
}
|
||||
|
||||
bool JavascriptMethodHandler::Execute(const CefString& name, CefRefPtr<CefV8Value> object, const CefV8ValueList& arguments, CefRefPtr<CefV8Value>& retval, CefString& exception)
|
||||
{
|
||||
auto parameter = gcnew array<Object^>(arguments.size());
|
||||
|
||||
for (std::vector<CefRefPtr<CefV8Value>>::size_type i = 0; i != arguments.size(); i++)
|
||||
{
|
||||
if (arguments[i]->IsFunction())
|
||||
{
|
||||
parameter[i] = _callbackRegistry->Register(CefV8Context::GetCurrentContext(), arguments[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
parameter[i] = TypeUtils::ConvertFromCef(arguments[i]);
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
auto response = _method->Invoke(parameter);
|
||||
|
||||
retval = ConvertToCefObject(response->Result);
|
||||
if (!response->Success)
|
||||
{
|
||||
exception = StringUtils::ToNative(response->Message);
|
||||
}
|
||||
}
|
||||
catch (Exception^ ex)
|
||||
{
|
||||
exception = StringUtils::ToNative(ex->Message);
|
||||
}
|
||||
|
||||
//NOTE: Return true otherwise exception is ignored
|
||||
return true;
|
||||
}
|
||||
|
||||
CefRefPtr<CefV8Value> JavascriptMethodHandler::ConvertToCefObject(Object^ obj)
|
||||
{
|
||||
if (obj == nullptr)
|
||||
{
|
||||
return CefV8Value::CreateNull();
|
||||
}
|
||||
|
||||
auto type = obj->GetType();
|
||||
|
||||
if (type == JavascriptObject::typeid)
|
||||
{
|
||||
JavascriptObject^ javascriptObject = (JavascriptObject^)obj;
|
||||
CefRefPtr<CefV8Value> cefObject = CefV8Value::CreateObject(NULL);
|
||||
|
||||
for (int i = 0; i < javascriptObject->Properties->Count; i++)
|
||||
{
|
||||
auto prop = javascriptObject->Properties[i];
|
||||
|
||||
if (prop->IsComplexType)
|
||||
{
|
||||
auto v8Value = ConvertToCefObject(prop->JsObject);
|
||||
|
||||
cefObject->SetValue(StringUtils::ToNative(prop->JavascriptName), v8Value, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE);
|
||||
}
|
||||
else
|
||||
{
|
||||
auto v8Value = TypeUtils::ConvertToCef(prop->PropertyValue, nullptr);
|
||||
|
||||
cefObject->SetValue(StringUtils::ToNative(prop->JavascriptName), v8Value, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE);
|
||||
}
|
||||
}
|
||||
|
||||
return cefObject;
|
||||
}
|
||||
|
||||
return TypeUtils::ConvertToCef(obj, nullptr);
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,11 @@
|
||||
// Copyright © 2010-2013 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 "Stdafx.h"
|
||||
|
||||
#include "include/cef_v8.h"
|
||||
#include "TypeUtils.h"
|
||||
#include "JavascriptCallbackRegistry.h"
|
||||
|
||||
using namespace CefSharp::Internals;
|
||||
|
||||
@@ -16,83 +15,16 @@ namespace CefSharp
|
||||
{
|
||||
private:
|
||||
gcroot<Func<array<Object^>^, BrowserProcessResponse^>^> _method;
|
||||
gcroot<JavascriptCallbackRegistry^> _callbackRegistry;
|
||||
|
||||
public:
|
||||
JavascriptMethodHandler(Func<array<Object^>^, BrowserProcessResponse^>^ method)
|
||||
{
|
||||
_method = method;
|
||||
}
|
||||
JavascriptMethodHandler(Func<array<Object^>^, BrowserProcessResponse^>^ method, JavascriptCallbackRegistry^ callbackRegistry);
|
||||
|
||||
~JavascriptMethodHandler()
|
||||
{
|
||||
delete _method;
|
||||
}
|
||||
~JavascriptMethodHandler();
|
||||
|
||||
virtual bool Execute(const CefString& name, CefRefPtr<CefV8Value> object, const CefV8ValueList& arguments, CefRefPtr<CefV8Value>& retval, CefString& exception)
|
||||
{
|
||||
auto parameter = gcnew array<Object^>(arguments.size());
|
||||
virtual bool Execute(const CefString& name, CefRefPtr<CefV8Value> object, const CefV8ValueList& arguments, CefRefPtr<CefV8Value>& retval, CefString& exception);
|
||||
|
||||
for (std::vector<CefRefPtr<CefV8Value>>::size_type i = 0; i != arguments.size(); i++)
|
||||
{
|
||||
parameter[i] = TypeUtils::ConvertFromCef(arguments[i]);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
auto response = _method->Invoke(parameter);
|
||||
|
||||
retval = ConvertToCefObject(response->Result);
|
||||
if (!response->Success)
|
||||
{
|
||||
exception = StringUtils::ToNative(response->Message);
|
||||
}
|
||||
}
|
||||
catch (Exception^ ex)
|
||||
{
|
||||
exception = StringUtils::ToNative(ex->Message);
|
||||
}
|
||||
|
||||
//NOTE: Return true otherwise exception is ignored
|
||||
return true;
|
||||
}
|
||||
|
||||
CefRefPtr<CefV8Value> ConvertToCefObject(Object^ obj)
|
||||
{
|
||||
if (obj == nullptr)
|
||||
{
|
||||
return CefV8Value::CreateNull();
|
||||
}
|
||||
|
||||
auto type = obj->GetType();
|
||||
|
||||
if (type == JavascriptObject::typeid)
|
||||
{
|
||||
JavascriptObject^ javascriptObject = (JavascriptObject^)obj;
|
||||
CefRefPtr<CefV8Value> cefObject = CefV8Value::CreateObject(NULL);
|
||||
|
||||
for (int i = 0; i < javascriptObject->Properties->Count; i++)
|
||||
{
|
||||
auto prop = javascriptObject->Properties[i];
|
||||
|
||||
if(prop->IsComplexType)
|
||||
{
|
||||
auto v8Value = ConvertToCefObject(prop->JsObject);
|
||||
|
||||
cefObject->SetValue(StringUtils::ToNative(prop->JavascriptName), v8Value, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE);
|
||||
}
|
||||
else
|
||||
{
|
||||
auto v8Value = TypeUtils::ConvertToCef(prop->PropertyValue, nullptr);
|
||||
|
||||
cefObject->SetValue(StringUtils::ToNative(prop->JavascriptName), v8Value, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE);
|
||||
}
|
||||
}
|
||||
|
||||
return cefObject;
|
||||
}
|
||||
|
||||
return TypeUtils::ConvertToCef(obj, nullptr);
|
||||
}
|
||||
CefRefPtr<CefV8Value> ConvertToCefObject(Object^ obj);
|
||||
|
||||
IMPLEMENT_REFCOUNTING(JavascriptMethodHandler)
|
||||
};
|
||||
|
||||
@@ -25,12 +25,12 @@ namespace CefSharp
|
||||
MCefRefPtr<CefV8Value> V8Value;
|
||||
|
||||
public:
|
||||
JavascriptMethodWrapper(JavascriptMethod^ javascriptMethod, int64 ownerId, IBrowserProcess^ browserProcess)
|
||||
JavascriptMethodWrapper(JavascriptMethod^ javascriptMethod, int64 ownerId, IBrowserProcess^ browserProcess, JavascriptCallbackRegistry^ callbackRegistry)
|
||||
{
|
||||
_javascriptMethod = javascriptMethod;
|
||||
_ownerId = ownerId;
|
||||
_browserProcess = browserProcess;
|
||||
_javascriptMethodHandler = new JavascriptMethodHandler(gcnew Func<array<Object^>^, BrowserProcessResponse^>(this, &JavascriptMethodWrapper::Execute));
|
||||
_javascriptMethodHandler = new JavascriptMethodHandler(gcnew Func<array<Object^>^, BrowserProcessResponse^>(this, &JavascriptMethodWrapper::Execute), callbackRegistry);
|
||||
}
|
||||
|
||||
~JavascriptMethodWrapper()
|
||||
|
||||
@@ -27,7 +27,7 @@ namespace CefSharp
|
||||
|
||||
for each (JavascriptMethod^ method in Enumerable::OfType<JavascriptMethod^>(_object->Methods))
|
||||
{
|
||||
auto wrappedMethod = gcnew JavascriptMethodWrapper(method, _object->Id, _browserProcess);
|
||||
auto wrappedMethod = gcnew JavascriptMethodWrapper(method, _object->Id, _browserProcess, CallbackRegistry);
|
||||
wrappedMethod->V8Value = javascriptObject;
|
||||
wrappedMethod->Bind();
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ namespace CefSharp
|
||||
|
||||
internal:
|
||||
MCefRefPtr<CefV8Value> V8Value;
|
||||
|
||||
JavascriptCallbackRegistry^ CallbackRegistry;
|
||||
|
||||
public:
|
||||
JavascriptObjectWrapper(JavascriptObject^ object, IBrowserProcess^ browserProcess)
|
||||
@@ -45,6 +45,7 @@ namespace CefSharp
|
||||
~JavascriptObjectWrapper()
|
||||
{
|
||||
V8Value = nullptr;
|
||||
CallbackRegistry = nullptr;
|
||||
_jsPropertyHandler->Cleanup();
|
||||
_jsPropertyHandler = nullptr;
|
||||
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
// Copyright © 2010-2014 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 "Stdafx.h"
|
||||
#include "JavascriptRootObjectWrapper.h"
|
||||
|
||||
namespace CefSharp
|
||||
{
|
||||
JavascriptRootObjectWrapper::JavascriptRootObjectWrapper(JavascriptRootObject^ rootObject, IBrowserProcess^ browserProcess)
|
||||
{
|
||||
_rootObject = rootObject;
|
||||
_browserProcess = browserProcess;
|
||||
_wrappedObjects = gcnew List<JavascriptObjectWrapper^>();
|
||||
}
|
||||
|
||||
JavascriptRootObjectWrapper::~JavascriptRootObjectWrapper()
|
||||
{
|
||||
V8Value = nullptr;
|
||||
CallbackRegistry = nullptr;
|
||||
for each (JavascriptObjectWrapper^ var in _wrappedObjects)
|
||||
{
|
||||
delete var;
|
||||
}
|
||||
}
|
||||
|
||||
void JavascriptRootObjectWrapper::Bind()
|
||||
{
|
||||
auto memberObjects = _rootObject->MemberObjects;
|
||||
for each (JavascriptObject^ obj in Enumerable::OfType<JavascriptObject^>(memberObjects))
|
||||
{
|
||||
auto wrapperObject = gcnew JavascriptObjectWrapper(obj, _browserProcess);
|
||||
wrapperObject->CallbackRegistry = CallbackRegistry;
|
||||
wrapperObject->V8Value = V8Value.get();
|
||||
wrapperObject->Bind();
|
||||
|
||||
_wrappedObjects->Add(wrapperObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,9 +4,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Stdafx.h"
|
||||
#include "include/cef_v8.h"
|
||||
|
||||
#include "JavascriptCallbackRegistry.h"
|
||||
#include "JavascriptObjectWrapper.h"
|
||||
|
||||
using namespace System::Runtime::Serialization;
|
||||
@@ -26,36 +25,14 @@ namespace CefSharp
|
||||
|
||||
internal:
|
||||
MCefRefPtr<CefV8Value> V8Value;
|
||||
JavascriptCallbackRegistry^ CallbackRegistry;
|
||||
|
||||
public:
|
||||
JavascriptRootObjectWrapper(JavascriptRootObject^ rootObject, IBrowserProcess^ browserProcess)
|
||||
{
|
||||
_rootObject = rootObject;
|
||||
_browserProcess = browserProcess;
|
||||
_wrappedObjects = gcnew List<JavascriptObjectWrapper^>();
|
||||
}
|
||||
JavascriptRootObjectWrapper(JavascriptRootObject^ rootObject, IBrowserProcess^ browserProcess);
|
||||
|
||||
~JavascriptRootObjectWrapper()
|
||||
{
|
||||
V8Value = nullptr;
|
||||
for each (JavascriptObjectWrapper^ var in _wrappedObjects)
|
||||
{
|
||||
delete var;
|
||||
}
|
||||
}
|
||||
~JavascriptRootObjectWrapper();
|
||||
|
||||
void Bind()
|
||||
{
|
||||
auto memberObjects = _rootObject->MemberObjects;
|
||||
for each (JavascriptObject^ obj in Enumerable::OfType<JavascriptObject^>(memberObjects))
|
||||
{
|
||||
auto wrapperObject = gcnew JavascriptObjectWrapper(obj, _browserProcess);
|
||||
wrapperObject->V8Value = V8Value.get();
|
||||
wrapperObject->Bind();
|
||||
|
||||
_wrappedObjects->Add(wrapperObject);
|
||||
}
|
||||
};
|
||||
void Bind();
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -137,7 +137,6 @@ namespace CefSharp.BrowserSubprocess
|
||||
Message = ex.ToString()
|
||||
};
|
||||
}
|
||||
|
||||
}, TaskCreationOptions.AttachedToParent);
|
||||
|
||||
return timeout.HasValue ? task.WithTimeout(timeout.Value) : task;
|
||||
@@ -174,5 +173,80 @@ namespace CefSharp.BrowserSubprocess
|
||||
{
|
||||
return ((Task<JavascriptResponse>)result).Result;
|
||||
}
|
||||
|
||||
public IAsyncResult BeginJavascriptCallbackAsync(int browserId, long callbackId, object[] parameters, TimeSpan? timeout, AsyncCallback callback, object state)
|
||||
{
|
||||
var tcs = new TaskCompletionSource<JavascriptResponse>(state);
|
||||
var task = JavascriptCallbackAsync(browserId, callbackId, parameters, 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 EndJavascriptCallbackAsync(IAsyncResult result)
|
||||
{
|
||||
return ((Task<JavascriptResponse>)result).Result;
|
||||
}
|
||||
|
||||
public void DestroyJavascriptCallback(int browserId, long id)
|
||||
{
|
||||
var browser = browsers.FirstOrDefault(x => x.BrowserId == browserId);
|
||||
if (browser != null)
|
||||
{
|
||||
browser.DestroyJavascriptCallback(id);
|
||||
}
|
||||
}
|
||||
|
||||
private Task<JavascriptResponse> JavascriptCallbackAsync(int browserId, long callbackId, object[] parameters, 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.DoCallback(callbackId, parameters);
|
||||
|
||||
return response;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new JavascriptResponse
|
||||
{
|
||||
Success = false,
|
||||
Message = ex.ToString()
|
||||
};
|
||||
}
|
||||
}, TaskCreationOptions.AttachedToParent);
|
||||
|
||||
return timeout.HasValue ? task.WithTimeout(timeout.Value) : task;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CefSharp.Example
|
||||
{
|
||||
@@ -19,6 +20,21 @@ namespace CefSharp.Example
|
||||
SubObject = new SubBoundObject();
|
||||
}
|
||||
|
||||
public void TestCallback(IJavascriptCallback javascriptCallback)
|
||||
{
|
||||
const int taskDelay = 1500;
|
||||
|
||||
Task.Run(async () =>
|
||||
{
|
||||
await Task.Delay(taskDelay);
|
||||
|
||||
using (javascriptCallback)
|
||||
{
|
||||
await javascriptCallback.ExecuteAsync("This callback from C# was delayed " + taskDelay + "ms");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public int EchoMyProperty()
|
||||
{
|
||||
return MyProperty;
|
||||
|
||||
@@ -5,21 +5,43 @@
|
||||
</head>
|
||||
<body>
|
||||
<p>
|
||||
Result of calling bound.repeat("hi ", 5) =
|
||||
<script type="text/javascript">
|
||||
var result = bound.repeat("hi ", 5);
|
||||
document.write('"' + result + '"');
|
||||
if(result == "hi hi hi hi hi ")
|
||||
{
|
||||
document.write(" SUCCESS");
|
||||
}
|
||||
else
|
||||
{
|
||||
document.write(" FAIL!");
|
||||
}
|
||||
</script>
|
||||
Javscript Callback Test
|
||||
<br />
|
||||
<script type="text/javascript">
|
||||
function callback(s)
|
||||
{
|
||||
var result = document.getElementById('cbresult');
|
||||
result.innerText += "Callback: " + s + "" + Date();
|
||||
}
|
||||
|
||||
function testCallback()
|
||||
{
|
||||
bound.testCallback(callback);
|
||||
|
||||
var result = document.getElementById('cbresult');
|
||||
result.innerText = "The function has returned: " + Date() + "\n";
|
||||
}
|
||||
</script>
|
||||
<button onclick="testCallback()">Test Callback</button>
|
||||
<br />
|
||||
<span id="cbresult"></span>
|
||||
</p>
|
||||
|
||||
|
||||
<p>
|
||||
Result of calling bound.repeat("hi ", 5) =
|
||||
<script type="text/javascript">
|
||||
var result = bound.repeat("hi ", 5);
|
||||
document.write('"' + result + '"');
|
||||
if (result == "hi hi hi hi hi ")
|
||||
{
|
||||
document.write(" SUCCESS");
|
||||
} else
|
||||
{
|
||||
document.write(" FAIL!");
|
||||
}
|
||||
</script>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Function delegate to c# method
|
||||
<br />
|
||||
@@ -32,7 +54,7 @@
|
||||
document.write("echoMyProperty result: " + myFunction(bound.echoMyProperty));
|
||||
</script>
|
||||
</p>
|
||||
|
||||
|
||||
<p>
|
||||
Function returning complex type
|
||||
<br />
|
||||
@@ -46,14 +68,15 @@
|
||||
<br />
|
||||
<script type="text/javascript">
|
||||
var stressTestCallCount = 1000;
|
||||
for (var i = 1; i <= stressTestCallCount; i++) {
|
||||
for (var i = 1; i <= stressTestCallCount; i++)
|
||||
{
|
||||
bound.repeat("hi ", 5);
|
||||
}
|
||||
|
||||
document.write("Stress Test done with : " + stressTestCallCount + " call to bound.repeat(\"hi \", 5)");
|
||||
</script>
|
||||
</p>
|
||||
|
||||
|
||||
<p>
|
||||
JSON Serializer Test
|
||||
<br />
|
||||
@@ -70,9 +93,6 @@
|
||||
}
|
||||
</script>
|
||||
</p>
|
||||
|
||||
|
||||
|
||||
|
||||
Methods on bound object 'bound':<br />
|
||||
<ul>
|
||||
@@ -105,4 +125,4 @@
|
||||
</script>
|
||||
</ul>
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
||||
|
||||
@@ -56,7 +56,6 @@ namespace CefSharp.Wpf.Example.ViewModels
|
||||
}
|
||||
|
||||
private object evaluateJavaScriptResult;
|
||||
|
||||
public object EvaluateJavaScriptResult
|
||||
{
|
||||
get { return evaluateJavaScriptResult; }
|
||||
@@ -98,6 +97,10 @@ namespace CefSharp.Wpf.Example.ViewModels
|
||||
try
|
||||
{
|
||||
var response = await webBrowser.EvaluateScriptAsync(s);
|
||||
if (response.Success && response.Result is IJavascriptCallback)
|
||||
{
|
||||
response = await ((IJavascriptCallback)response.Result).ExecuteAsync("This is a callback from EvaluateJavaScript");
|
||||
}
|
||||
|
||||
EvaluateJavaScriptResult = response.Success ? (response.Result ?? "null") : response.Message;
|
||||
}
|
||||
|
||||
@@ -91,6 +91,12 @@
|
||||
<Compile Include="CefDirtyRect.cs" />
|
||||
<Compile Include="CefFileDialogMode.cs" />
|
||||
<Compile Include="DefaultResourceHandlerFactory.cs" />
|
||||
<Compile Include="IJavascriptCallback.cs" />
|
||||
<Compile Include="Internals\JavascriptCallbackProxy.cs" />
|
||||
<Compile Include="Internals\JavascriptCallback.cs" />
|
||||
<Compile Include="Internals\JavascriptCallbackEndpointBehavior.cs" />
|
||||
<Compile Include="Internals\JavascriptCallbackSurrogate.cs" />
|
||||
<Compile Include="Internals\ReflectionUtils.cs" />
|
||||
<Compile Include="IResourceHandlerFactory.cs" />
|
||||
<Compile Include="MouseEvent.cs" />
|
||||
<Compile Include="DragOperationsMask.cs" />
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace CefSharp
|
||||
{
|
||||
@@ -11,6 +12,7 @@ namespace CefSharp
|
||||
/// <summary>
|
||||
/// Provides base class functionality for implementing the Dispose Pattern.
|
||||
/// </summary>
|
||||
[DataContract]
|
||||
public abstract class DisposableResource : IDisposable
|
||||
{
|
||||
~DisposableResource()
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
// Copyright © 2010-2014 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.
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CefSharp
|
||||
{
|
||||
public interface IJavascriptCallback : IDisposable
|
||||
{
|
||||
Task<JavascriptResponse> ExecuteAsync(params object[] parms);
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,7 @@ namespace CefSharp.Internals
|
||||
|
||||
public JavascriptObjectRepository JavascriptObjectRepository { get; private set; }
|
||||
private TaskCompletionSource<OperationContext> operationContextTaskCompletionSource = new TaskCompletionSource<OperationContext>();
|
||||
|
||||
|
||||
public BrowserProcessServiceHost(JavascriptObjectRepository javascriptObjectRepository, int parentProcessId, int browserId)
|
||||
: base(typeof(BrowserProcessService), new Uri[0])
|
||||
{
|
||||
@@ -30,12 +30,13 @@ namespace CefSharp.Internals
|
||||
var binding = CreateBinding();
|
||||
|
||||
var endPoint = AddServiceEndpoint(
|
||||
typeof(IBrowserProcess),
|
||||
typeof (IBrowserProcess),
|
||||
binding,
|
||||
new Uri(serviceName)
|
||||
);
|
||||
|
||||
endPoint.Contract.ProtectionLevel = ProtectionLevel.None;
|
||||
endPoint.Behaviors.Add(new JavascriptCallbackEndpointBehavior(this));
|
||||
}
|
||||
|
||||
public void SetOperationContext(OperationContext operationContext)
|
||||
@@ -85,10 +86,32 @@ namespace CefSharp.Internals
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
internal Task<JavascriptResponse> JavascriptCallback(int browserId, long id, object[] parameters, TimeSpan? timeout)
|
||||
{
|
||||
var operationContextTask = operationContextTaskCompletionSource.Task;
|
||||
return operationContextTask.ContinueWith(t =>
|
||||
{
|
||||
var context = t.Result;
|
||||
var renderProcess = context.GetCallbackChannel<IRenderProcess>();
|
||||
var asyncResult = renderProcess.BeginJavascriptCallbackAsync(browserId, id, parameters, timeout, null, null);
|
||||
return Task.Factory.FromAsync<JavascriptResponse>(asyncResult, renderProcess.EndJavascriptCallbackAsync);
|
||||
}).Unwrap();
|
||||
}
|
||||
|
||||
internal void DestroyJavascriptCallback(int browserId, long id)
|
||||
{
|
||||
var operationContextTask = operationContextTaskCompletionSource.Task;
|
||||
operationContextTask.ContinueWith(t =>
|
||||
{
|
||||
var context = t.Result;
|
||||
var renderProcess = context.GetCallbackChannel<IRenderProcess>();
|
||||
renderProcess.DestroyJavascriptCallback(browserId, id);
|
||||
});
|
||||
}
|
||||
|
||||
protected override void OnClosed()
|
||||
{
|
||||
base.OnClosed();
|
||||
|
||||
@@ -13,6 +13,7 @@ namespace CefSharp.Internals
|
||||
[ServiceKnownType(typeof(JavascriptObject))]
|
||||
[ServiceKnownType(typeof(JavascriptMethod))]
|
||||
[ServiceKnownType(typeof(JavascriptProperty))]
|
||||
[ServiceKnownType(typeof(JavascriptCallback))]
|
||||
public interface IBrowserProcess
|
||||
{
|
||||
[OperationContract]
|
||||
|
||||
@@ -9,11 +9,19 @@ using System.Threading.Tasks;
|
||||
namespace CefSharp.Internals
|
||||
{
|
||||
[ServiceContract]
|
||||
public interface IRenderProcess
|
||||
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);
|
||||
|
||||
JavascriptResponse EndJavascriptCallbackAsync(IAsyncResult result);
|
||||
|
||||
[OperationContract]
|
||||
void DestroyJavascriptCallback(int browserId, long id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
// Copyright © 2010-2014 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.Runtime.Serialization;
|
||||
|
||||
namespace CefSharp.Internals
|
||||
{
|
||||
[DataContract]
|
||||
public class JavascriptCallback
|
||||
{
|
||||
[DataMember]
|
||||
public long Id { get; set; }
|
||||
|
||||
[DataMember]
|
||||
public int BrowserId { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
// Copyright © 2010-2014 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.Generic;
|
||||
using System.Linq;
|
||||
using System.ServiceModel.Channels;
|
||||
using System.ServiceModel.Description;
|
||||
using System.ServiceModel.Dispatcher;
|
||||
|
||||
namespace CefSharp.Internals
|
||||
{
|
||||
internal sealed class JavascriptCallbackEndpointBehavior : IEndpointBehavior
|
||||
{
|
||||
private static readonly List<string> Methods = new List<string>
|
||||
{
|
||||
ReflectionUtils.GetMethodName<IBrowserProcess>(p => p.CallMethod(0, null, null)),
|
||||
ReflectionUtils.GetMethodName<IRenderProcess>(p => p.BeginEvaluateScriptAsync(0, 0, null, null, null, null)).Substring(5),
|
||||
ReflectionUtils.GetMethodName<IRenderProcess>(p => p.BeginJavascriptCallbackAsync(0, 0, null, null, null, null)).Substring(5),
|
||||
};
|
||||
|
||||
private readonly WeakReference browserProcessWeakReference;
|
||||
|
||||
public JavascriptCallbackEndpointBehavior(BrowserProcessServiceHost browserProcess)
|
||||
{
|
||||
browserProcessWeakReference = new WeakReference(browserProcess);
|
||||
}
|
||||
|
||||
public void Validate(ServiceEndpoint endpoint)
|
||||
{
|
||||
}
|
||||
|
||||
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
|
||||
{
|
||||
}
|
||||
|
||||
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
|
||||
{
|
||||
foreach (var operation in endpoint.Contract.Operations.Where(o => Methods.Contains(o.Name)))
|
||||
{
|
||||
operation.Behaviors.Find<DataContractSerializerOperationBehavior>().DataContractSurrogate = new JavascriptCallbackSurrogate(browserProcessWeakReference);
|
||||
}
|
||||
}
|
||||
|
||||
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
// Copyright © 2010-2014 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.Runtime.Serialization;
|
||||
using System.ServiceModel;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CefSharp.Internals
|
||||
{
|
||||
/// <summary>
|
||||
/// JavascriptCallbackProxy is a wrapper class around the WCF BrowserProcess.
|
||||
/// When the render process detects a function as param or return type it will add a
|
||||
/// <see cref="JavascriptCallback"/> that identifies the function. On the server side
|
||||
/// WCF Transforms <see cref="JavascriptCallback"/> into <see cref="JavascriptCallbackProxy"/>
|
||||
/// using the <see cref="JavascriptCallbackSurrogate"/> class.
|
||||
/// </summary>
|
||||
[DataContract]
|
||||
internal sealed class JavascriptCallbackProxy : DisposableResource, IJavascriptCallback
|
||||
{
|
||||
private readonly long id;
|
||||
private readonly int browserId;
|
||||
private readonly WeakReference browserProcessWeakReference;
|
||||
|
||||
public JavascriptCallbackProxy(long id, int browserId, WeakReference browserProcessWeakReference)
|
||||
{
|
||||
this.id = id;
|
||||
this.browserId = browserId;
|
||||
this.browserProcessWeakReference = browserProcessWeakReference;
|
||||
}
|
||||
|
||||
public Task<JavascriptResponse> ExecuteAsync(params object[] parms)
|
||||
{
|
||||
if (IsDisposed)
|
||||
{
|
||||
throw new ObjectDisposedException("JavascriptCallbackProxy is already disposed.");
|
||||
}
|
||||
|
||||
var browserProcess = (BrowserProcessServiceHost)browserProcessWeakReference.Target;
|
||||
if (browserProcess == null)
|
||||
{
|
||||
throw new ObjectDisposedException("BrowserProcessServiceHost is already disposed.");
|
||||
}
|
||||
|
||||
return browserProcess.JavascriptCallback(browserId, id, parms, null);
|
||||
}
|
||||
|
||||
protected override void DoDispose(bool isDisposing)
|
||||
{
|
||||
var browserProcess = (BrowserProcessServiceHost)browserProcessWeakReference.Target;
|
||||
if (!IsDisposed && browserProcess != null && browserProcess.State == CommunicationState.Opened)
|
||||
{
|
||||
browserProcess.DestroyJavascriptCallback(browserId, id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
// Copyright © 2010-2014 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.CodeDom;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace CefSharp.Internals
|
||||
{
|
||||
internal sealed class JavascriptCallbackSurrogate : IDataContractSurrogate
|
||||
{
|
||||
private readonly WeakReference browserProcessWeakReference;
|
||||
|
||||
public JavascriptCallbackSurrogate(WeakReference browserProcessWeakReference)
|
||||
{
|
||||
this.browserProcessWeakReference = browserProcessWeakReference;
|
||||
}
|
||||
|
||||
public Type GetDataContractType(Type type)
|
||||
{
|
||||
if (type == typeof (JavascriptCallback))
|
||||
{
|
||||
return typeof (JavascriptCallbackProxy);
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
public object GetObjectToSerialize(object obj, Type targetType)
|
||||
{
|
||||
return obj;
|
||||
}
|
||||
|
||||
public object GetDeserializedObject(object obj, Type targetType)
|
||||
{
|
||||
var result = obj;
|
||||
var dto = obj as JavascriptCallback;
|
||||
if (dto != null)
|
||||
{
|
||||
result = new JavascriptCallbackProxy(dto.Id, dto.BrowserId, browserProcessWeakReference);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public object GetCustomDataToExport(MemberInfo memberInfo, Type dataContractType)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public object GetCustomDataToExport(Type clrType, Type dataContractType)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void GetKnownCustomDataTypes(Collection<Type> customDataTypes)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Type GetReferencedTypeOnImport(string typeName, string typeNamespace, object customData)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public CodeTypeDeclaration ProcessImportedType(CodeTypeDeclaration typeDeclaration, CodeCompileUnit compileUnit)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// Copyright © 2010-2014 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.Linq.Expressions;
|
||||
|
||||
namespace CefSharp.Internals
|
||||
{
|
||||
internal static class ReflectionUtils
|
||||
{
|
||||
public static string GetMethodName<T>(Expression<Func<T, object>> expression)
|
||||
{
|
||||
var methodCallExpression = (MethodCallExpression)expression.Body;
|
||||
return methodCallExpression.Method.Name;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,20 +1,22 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.Serialization;
|
||||
using CefSharp.Internals;
|
||||
|
||||
namespace CefSharp
|
||||
{
|
||||
[DataContract]
|
||||
[KnownType(typeof(object[]))]
|
||||
[KnownType(typeof(Dictionary<string,object>))]
|
||||
public class JavascriptResponse
|
||||
{
|
||||
[DataMember]
|
||||
public string Message { get; set; }
|
||||
[DataContract]
|
||||
[KnownType(typeof (object[]))]
|
||||
[KnownType(typeof (JavascriptCallback))]
|
||||
[KnownType(typeof (Dictionary<string, object>))]
|
||||
public class JavascriptResponse
|
||||
{
|
||||
[DataMember]
|
||||
public string Message { get; set; }
|
||||
|
||||
[DataMember]
|
||||
public bool Success { get; set; }
|
||||
[DataMember]
|
||||
public bool Success { get; set; }
|
||||
|
||||
[DataMember]
|
||||
public object Result { get; set; }
|
||||
}
|
||||
[DataMember]
|
||||
public object Result { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user