mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-06 04:00:07 +00:00
created new implementation of ijavascriptcallback to handle cef ipc solution
This commit is contained in:
@@ -155,6 +155,7 @@ namespace CefSharp
|
||||
|
||||
result = value->ExecuteFunction(nullptr, params);
|
||||
success = result.get() != nullptr;
|
||||
response = CefProcessMessage::Create(kJavascriptCallbackResponse);
|
||||
//we need to do this here to be able to store the v8context
|
||||
if (success)
|
||||
{
|
||||
@@ -166,8 +167,6 @@ namespace CefSharp
|
||||
exception = value->GetException();
|
||||
}
|
||||
}
|
||||
|
||||
response = CefProcessMessage::Create(kJavascriptCallbackResponse);
|
||||
}
|
||||
|
||||
if (response.get())
|
||||
|
||||
@@ -19,6 +19,7 @@ namespace CefSharp
|
||||
private class CefAppUnmanagedWrapper : CefApp, CefRenderProcessHandler
|
||||
{
|
||||
private:
|
||||
//Simple v8context enter/exit to avoid code duplication
|
||||
struct V8ContextScope
|
||||
{
|
||||
V8ContextScope(CefRefPtr<CefV8Context> context)
|
||||
|
||||
@@ -227,6 +227,8 @@
|
||||
<ClCompile Include="Internals\CompletionHandler.cpp" />
|
||||
<ClCompile Include="Internals\CookieVisitor.cpp" />
|
||||
<ClCompile Include="Internals\CefRequestWrapper.cpp" />
|
||||
<ClCompile Include="Internals\JavascriptCallbackImpl.cpp" />
|
||||
<ClCompile Include="Internals\JavascriptCallbackImplFactory.cpp" />
|
||||
<ClCompile Include="Internals\StringVisitor.cpp" />
|
||||
<ClCompile Include="ManagedCefBrowserAdapter.cpp" />
|
||||
<ClCompile Include="NativeMethodWrapper.cpp" />
|
||||
@@ -267,6 +269,8 @@
|
||||
<ClInclude Include="Internals\CefSharpApp.h" />
|
||||
<ClInclude Include="Internals\CefRequestWrapper.h" />
|
||||
<ClInclude Include="Internals\CefJSDialogCallbackWrapper.h" />
|
||||
<ClInclude Include="Internals\JavascriptCallbackImpl.h" />
|
||||
<ClInclude Include="Internals\JavascriptCallbackImplFactory.h" />
|
||||
<ClInclude Include="Internals\MCefRefPtr.h" />
|
||||
<ClInclude Include="Internals\Messaging\Messages.h" />
|
||||
<ClInclude Include="Internals\PluginVisitor.h" />
|
||||
|
||||
@@ -65,6 +65,12 @@
|
||||
<ClCompile Include="Internals\Serialization\Primitives.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Internals\JavascriptCallbackImpl.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Internals\JavascriptCallbackImplFactory.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="vcclr_local.h">
|
||||
@@ -194,6 +200,12 @@
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Internals\CefWindowInfoWrapper.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Internals\JavascriptCallbackImpl.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Internals\JavascriptCallbackImplFactory.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
// 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 "JavascriptCallbackImpl.h"
|
||||
#include "Messaging/Messages.h"
|
||||
#include "Serialization/Primitives.h"
|
||||
#include "Serialization/V8Serialization.h"
|
||||
|
||||
namespace CefSharp
|
||||
{
|
||||
namespace Internals
|
||||
{
|
||||
using namespace Messaging;
|
||||
using namespace Serialization;
|
||||
|
||||
JavascriptCallbackImpl::JavascriptCallbackImpl(JavascriptCallback^ callback, PendingTaskRepository<JavascriptResponse^>^ pendingTasks, WeakReference^ browserWrapper)
|
||||
:_callback(callback), _pendingTasks(pendingTasks)
|
||||
{
|
||||
_browserWrapper = browserWrapper;
|
||||
}
|
||||
|
||||
Task<JavascriptResponse^>^ JavascriptCallbackImpl::ExecuteAsync(array<Object^>^ parameters)
|
||||
{
|
||||
auto browser = GetBrowser();
|
||||
if (browser.get())
|
||||
{
|
||||
auto doneCallback = _pendingTasks->CreatePendingTask(Nullable<TimeSpan>());
|
||||
auto callbackMessage = CreateCallMessage(doneCallback.Key, parameters);
|
||||
browser->SendProcessMessage(CefProcessId::PID_RENDERER, callbackMessage);
|
||||
|
||||
return doneCallback.Value->Task;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw gcnew InvalidOperationException("Browser instance is null.");
|
||||
}
|
||||
}
|
||||
|
||||
CefRefPtr<CefProcessMessage> JavascriptCallbackImpl::CreateCallMessage(int64 doneCallbackId, array<Object^>^ parameters)
|
||||
{
|
||||
auto result = CefProcessMessage::Create(kJavascriptCallbackRequest);
|
||||
auto browser = GetBrowser();
|
||||
auto argList = result->GetArgumentList();
|
||||
argList->SetInt(0, browser->GetIdentifier());
|
||||
SetInt64(_callback->Id, argList, 1);
|
||||
SetInt64(doneCallbackId, argList, 2);
|
||||
auto paramList = CefListValue::Create();
|
||||
for (int i = 0; i < parameters->Length; i++)
|
||||
{
|
||||
auto param = parameters[i];
|
||||
SerializeV8Object(param, paramList, i);
|
||||
}
|
||||
argList->SetList(3, paramList);
|
||||
return result;
|
||||
}
|
||||
|
||||
CefRefPtr<CefProcessMessage> JavascriptCallbackImpl::CreateDestroyMessage()
|
||||
{
|
||||
auto result = CefProcessMessage::Create(kJavascriptCallbackDestroyRequest);
|
||||
auto argList = result->GetArgumentList();
|
||||
SetInt64(_callback->Id, argList, 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
CefRefPtr<CefBrowser> JavascriptCallbackImpl::GetBrowser()
|
||||
{
|
||||
CefRefPtr<CefBrowser> result;
|
||||
if (_browserWrapper->IsAlive)
|
||||
{
|
||||
auto wrapper = static_cast<CefSharpBrowserWrapper^>(_browserWrapper->Target);
|
||||
result = wrapper == nullptr ? nullptr : wrapper->Browser.get();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void JavascriptCallbackImpl::DisposedGuard()
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
throw gcnew ObjectDisposedException("JavascriptCallbackImpl");
|
||||
}
|
||||
}
|
||||
|
||||
JavascriptCallbackImpl::!JavascriptCallbackImpl()
|
||||
{
|
||||
auto browser = GetBrowser();
|
||||
if (browser.get())
|
||||
{
|
||||
browser->SendProcessMessage(CefProcessId::PID_RENDERER, CreateDestroyMessage());
|
||||
}
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
// 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"
|
||||
#include "include/cef_app.h"
|
||||
|
||||
#include "CefSharpBrowserWrapper.h"
|
||||
|
||||
using namespace System;
|
||||
using namespace System::Threading::Tasks;
|
||||
|
||||
namespace CefSharp
|
||||
{
|
||||
namespace Internals
|
||||
{
|
||||
ref class JavascriptCallbackImpl : public IJavascriptCallback
|
||||
{
|
||||
private:
|
||||
WeakReference^ _browserWrapper;
|
||||
JavascriptCallback^ _callback;
|
||||
PendingTaskRepository<JavascriptResponse^>^ _pendingTasks;
|
||||
bool _disposed;
|
||||
|
||||
CefRefPtr<CefProcessMessage> CreateCallMessage(int64 doneCallbackId, array<Object^>^ parameters);
|
||||
CefRefPtr<CefProcessMessage> CreateDestroyMessage();
|
||||
CefRefPtr<CefBrowser> GetBrowser();
|
||||
void DisposedGuard();
|
||||
public:
|
||||
JavascriptCallbackImpl(JavascriptCallback^ callback, PendingTaskRepository<JavascriptResponse^>^ pendingTasks, WeakReference^ browser);
|
||||
|
||||
virtual Task<JavascriptResponse^>^ ExecuteAsync(array<Object^>^ parameters) override;
|
||||
|
||||
~JavascriptCallbackImpl() { this->!JavascriptCallbackImpl(); }
|
||||
!JavascriptCallbackImpl();
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// 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 "CefSharpBrowserWrapper.h"
|
||||
#include "JavascriptCallbackImpl.h"
|
||||
#include "JavascriptCallbackImplFactory.h"
|
||||
|
||||
namespace CefSharp
|
||||
{
|
||||
namespace Internals
|
||||
{
|
||||
JavascriptCallbackImplFactory::JavascriptCallbackImplFactory(PendingTaskRepository<JavascriptResponse^>^ pendingTasks)
|
||||
:_pendingTasks(pendingTasks)
|
||||
{
|
||||
}
|
||||
|
||||
void JavascriptCallbackImplFactory::BrowserWrapper::set(CefSharpBrowserWrapper^ browserWrapper)
|
||||
{
|
||||
_browserWrapper = gcnew WeakReference(browserWrapper);
|
||||
}
|
||||
|
||||
IJavascriptCallback^ JavascriptCallbackImplFactory::Create(JavascriptCallback^ callback)
|
||||
{
|
||||
return gcnew JavascriptCallbackImpl(callback, _pendingTasks, _browserWrapper);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// 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
|
||||
|
||||
using namespace System;
|
||||
|
||||
namespace CefSharp
|
||||
{
|
||||
namespace Internals
|
||||
{
|
||||
ref class CefSharpBrowserWrapper;
|
||||
|
||||
ref class JavascriptCallbackImplFactory : public IJavascriptCallbackFactory
|
||||
{
|
||||
private:
|
||||
WeakReference^ _browserWrapper;
|
||||
PendingTaskRepository<JavascriptResponse^>^ _pendingTasks;
|
||||
public:
|
||||
JavascriptCallbackImplFactory(PendingTaskRepository<JavascriptResponse^>^ pendingTasks);
|
||||
|
||||
property CefSharpBrowserWrapper^ BrowserWrapper
|
||||
{
|
||||
void set(CefSharpBrowserWrapper^ browserWrapper);
|
||||
};
|
||||
|
||||
virtual IJavascriptCallback^ Create(JavascriptCallback^ callback);
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -163,7 +163,7 @@ namespace CefSharp
|
||||
throw gcnew NotSupportedException("Complex types cannot be serialized to Cef lists");
|
||||
}
|
||||
|
||||
seen->Pop(obj);
|
||||
seen->Pop();
|
||||
}
|
||||
|
||||
DateTime ConvertCefTimeToDateTime(CefTime time)
|
||||
@@ -185,6 +185,8 @@ namespace CefSharp
|
||||
|
||||
template Object^ DeserializeV8Object(CefRefPtr<CefListValue> list, int index, IJavascriptCallbackFactory^ javascriptCallbackFactory);
|
||||
template Object^ DeserializeV8Object(CefRefPtr<CefDictionaryValue> list, CefString index, IJavascriptCallbackFactory^ javascriptCallbackFactory);
|
||||
template void SerializeV8Object(Object^ obj, CefRefPtr<CefListValue> list, int index);
|
||||
template void SerializeV8Object(Object^ obj, CefRefPtr<CefDictionaryValue> list, CefString index);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user