mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-06 12:09:40 +00:00
reversed subprocess communication now Browser is host and renderer is client
This commit is contained in:
@@ -27,10 +27,10 @@ namespace CefSharp
|
||||
return CefExecuteProcess(cefMainArgs, (CefApp*)cefApp.get());
|
||||
}
|
||||
|
||||
void CefAppWrapper::RegisterJavascriptObjects(JavascriptObjectWrapper^ windowObject)
|
||||
void CefAppWrapper::Bind(JavascriptObject^ windowObject)
|
||||
{
|
||||
auto app = cefApp.get();
|
||||
|
||||
app->RegisterJavascriptObjects(windowObject);
|
||||
app->Bind(windowObject);
|
||||
}
|
||||
}
|
||||
@@ -20,14 +20,14 @@ namespace CefSharp
|
||||
{
|
||||
private:
|
||||
MCefRefPtr<CefAppUnmanagedWrapper> cefApp;
|
||||
ISubprocessCallback^ _callback;
|
||||
IBrowserProcess^ _browserProcess;
|
||||
|
||||
internal:
|
||||
|
||||
virtual property ISubprocessCallback^ Callback
|
||||
virtual property IBrowserProcess^ BrowserProcess
|
||||
{
|
||||
ISubprocessCallback^ get() { return _callback; }
|
||||
void set(ISubprocessCallback^ value) { _callback = value; }
|
||||
IBrowserProcess^ get() { return _browserProcess; }
|
||||
void set(IBrowserProcess^ value) { _browserProcess = value; }
|
||||
}
|
||||
|
||||
public:
|
||||
@@ -36,19 +36,16 @@ namespace CefSharp
|
||||
CefAppWrapper();
|
||||
int Run();
|
||||
|
||||
void RegisterJavascriptObjects(JavascriptObjectWrapper^ windowObject);
|
||||
void Bind(JavascriptObject^ windowObject);
|
||||
};
|
||||
|
||||
private class CefAppUnmanagedWrapper : CefApp, CefRenderProcessHandler
|
||||
{
|
||||
private:
|
||||
gcroot<Action<CefBrowserBase^>^> _onBrowserCreated;
|
||||
gcroot<JavascriptObject^> _boundObject;
|
||||
CefRefPtr<CefV8Context> _context;
|
||||
CefRefPtr<CefV8Value> _window;
|
||||
gcroot<JavascriptObjectWrapper^> _windowObject;
|
||||
|
||||
public:
|
||||
|
||||
CefAppUnmanagedWrapper(Action<CefBrowserBase^>^ onBrowserCreated)
|
||||
{
|
||||
_onBrowserCreated = onBrowserCreated;
|
||||
@@ -68,44 +65,27 @@ namespace CefSharp
|
||||
|
||||
virtual DECL void OnContextCreated(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefV8Context> context) OVERRIDE
|
||||
{
|
||||
_context = context;
|
||||
_window = context->GetGlobal();
|
||||
System::Diagnostics::Debugger::Break();
|
||||
|
||||
//TODO; whats the right timing here
|
||||
//TODO: _windowObject is not yet set
|
||||
Bind();
|
||||
};
|
||||
auto window = context->GetGlobal();
|
||||
|
||||
void RegisterJavascriptObjects(JavascriptObjectWrapper^ windowObject)
|
||||
{
|
||||
_windowObject = windowObject;
|
||||
JavascriptObjectWrapper^ jswindow = _windowObject;
|
||||
|
||||
//TODO; whats the right timing here
|
||||
//TODO: cant get a valid window here
|
||||
//Bind();
|
||||
};
|
||||
|
||||
void Bind()
|
||||
{
|
||||
/*auto currentthread = CefTaskRunner::GetForCurrentThread();
|
||||
auto renderThread = CefTaskRunner::GetForThread(CefThreadId::TID_RENDERER);
|
||||
|
||||
if (currentthread == nullptr || !currentthread->IsSame(renderThread))
|
||||
if (jswindow != nullptr)
|
||||
{
|
||||
CefPostTask(CefThreadId::TID_RENDERER, NewCefRunnableMethod(this, &CefAppUnmanagedWrapper::Bind));
|
||||
return;
|
||||
}*/
|
||||
|
||||
|
||||
JavascriptObjectWrapper^ windowObject = _windowObject;
|
||||
CefRefPtr<CefV8Context> context = _context;
|
||||
|
||||
if (windowObject != nullptr && context != nullptr)
|
||||
{
|
||||
windowObject->Value = _window;
|
||||
windowObject->Bind();
|
||||
jswindow->Value = window;
|
||||
jswindow->Bind();
|
||||
}
|
||||
};
|
||||
|
||||
void Bind(JavascriptObject^ windowObject)
|
||||
{
|
||||
System::Diagnostics::Debugger::Break();
|
||||
|
||||
_windowObject = gcnew JavascriptObjectWrapper();
|
||||
_windowObject->Clone(windowObject);
|
||||
};
|
||||
|
||||
IMPLEMENT_REFCOUNTING(CefAppUnmanagedWrapper);
|
||||
};
|
||||
|
||||
@@ -79,6 +79,7 @@
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Runtime.Serialization" />
|
||||
<Reference Include="System.ServiceModel" />
|
||||
@@ -103,6 +104,7 @@
|
||||
<ClCompile Include="AssemblyInfo.cpp" />
|
||||
<ClCompile Include="CefAppWrapper.cpp" />
|
||||
<ClCompile Include="JavascriptMethodWrapper.cpp" />
|
||||
<ClCompile Include="JavascriptObjectWrapper.cpp" />
|
||||
<ClCompile Include="Stdafx.cpp">
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
|
||||
|
||||
@@ -67,6 +67,9 @@
|
||||
<ClCompile Include="JavascriptMethodWrapper.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="JavascriptObjectWrapper.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
|
||||
@@ -9,12 +9,11 @@
|
||||
#include "CefAppWrapper.h"
|
||||
|
||||
using namespace System;
|
||||
using namespace System::Diagnostics;
|
||||
|
||||
namespace CefSharp
|
||||
{
|
||||
Object^ JavascriptMethodWrapper::Execute(array<Object^>^ parameters)
|
||||
{
|
||||
return CefAppWrapper::Instance->Callback->CallMethod(_owner->Id, Description->ManagedName, parameters);
|
||||
return CefAppWrapper::Instance->BrowserProcess->CallMethod(_owner->Id, Description->ManagedName, parameters);
|
||||
}
|
||||
}
|
||||
@@ -35,6 +35,11 @@ namespace CefSharp
|
||||
_owner->Value->SetValue(methodName, v8Value, V8_PROPERTY_ATTRIBUTE_NONE);
|
||||
};
|
||||
|
||||
void Clone(JavascriptMethod^ obj)
|
||||
{
|
||||
Description = obj->Description;
|
||||
}
|
||||
|
||||
Object^ Execute(array<Object^>^ parameters );
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
// 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 "JavascriptObjectWrapper.h"
|
||||
#include "JavascriptPropertyWrapper.h"
|
||||
#include "JavascriptMethodWrapper.h"
|
||||
|
||||
using namespace System;
|
||||
|
||||
namespace CefSharp
|
||||
{
|
||||
void JavascriptObjectWrapper::Clone(JavascriptObject^ obj)
|
||||
{
|
||||
Id = obj->Id;
|
||||
|
||||
for each (JavascriptProperty^ member in Enumerable::OfType<JavascriptProperty^>(obj->Members))
|
||||
{
|
||||
JavascriptPropertyWrapper^ propertywrapper = gcnew JavascriptPropertyWrapper();
|
||||
propertywrapper->Clone(member);
|
||||
Members->Add(propertywrapper);
|
||||
}
|
||||
|
||||
for each (JavascriptMethod^ member in Enumerable::OfType<JavascriptMethod^>(obj->Members))
|
||||
{
|
||||
JavascriptMethodWrapper^ methodwrapper = gcnew JavascriptMethodWrapper();
|
||||
methodwrapper->Clone(member);
|
||||
Members->Add(methodwrapper);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -8,6 +8,9 @@
|
||||
#include "include/cef_app.h"
|
||||
#include "include/cef_base.h"
|
||||
|
||||
using namespace System::Runtime::Serialization;
|
||||
using namespace System::Linq;
|
||||
|
||||
using namespace CefSharp::Internals;
|
||||
|
||||
namespace CefSharp
|
||||
@@ -26,5 +29,7 @@ namespace CefSharp
|
||||
member->Bind(this);
|
||||
}
|
||||
};
|
||||
|
||||
void Clone(JavascriptObject^ obj);
|
||||
};
|
||||
}
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
#include "Stdafx.h"
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace CefSharp
|
||||
{
|
||||
private class JavascriptPropertyHandler : public CefV8Accessor
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include "Stdafx.h"
|
||||
#include "JavascriptObjectWrapper.h"
|
||||
#include "JavascriptPropertyHandler.h"
|
||||
|
||||
using namespace CefSharp::Internals;
|
||||
|
||||
@@ -41,5 +42,12 @@ namespace CefSharp
|
||||
|
||||
Value->Bind();
|
||||
};
|
||||
|
||||
void Clone(JavascriptProperty^ obj)
|
||||
{
|
||||
Description = obj->Description;
|
||||
|
||||
Value->Clone( obj->Value );
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -1,24 +1,20 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.ServiceModel;
|
||||
using System.ServiceModel.Description;
|
||||
using System.Threading.Tasks;
|
||||
using CefSharp.Internals;
|
||||
|
||||
namespace CefSharp.BrowserSubprocess
|
||||
{
|
||||
public class CefRenderprocess : CefSubprocess
|
||||
public class CefRenderprocess : CefSubprocess, IRenderprocess
|
||||
{
|
||||
private SubprocessServiceHost javascriptServiceHost;
|
||||
private CefBrowserBase browser;
|
||||
|
||||
public CefBrowserBase Browser
|
||||
{
|
||||
get { return browser; }
|
||||
}
|
||||
|
||||
public SubprocessServiceHost ServiceHost
|
||||
{
|
||||
get { return javascriptServiceHost; }
|
||||
}
|
||||
|
||||
public CefRenderprocess(IEnumerable<string> args)
|
||||
: base(args)
|
||||
{
|
||||
@@ -33,7 +29,7 @@ namespace CefSharp.BrowserSubprocess
|
||||
|
||||
protected override void DoDispose(bool isDisposing)
|
||||
{
|
||||
DisposeMember(ref javascriptServiceHost);
|
||||
//DisposeMember(ref renderprocess);
|
||||
DisposeMember(ref browser);
|
||||
|
||||
base.DoDispose(isDisposing);
|
||||
@@ -48,11 +44,23 @@ namespace CefSharp.BrowserSubprocess
|
||||
return;
|
||||
}
|
||||
|
||||
Task.Factory.StartNew(() =>
|
||||
{
|
||||
javascriptServiceHost = SubprocessServiceHost.Create(ParentProcessId.Value, cefBrowserWrapper.BrowserId);
|
||||
javascriptServiceHost.Initialized += (c) => Callback = c;
|
||||
});
|
||||
var channel = new DuplexChannelFactory<IBrowserProcess>(
|
||||
this,
|
||||
new NetNamedPipeBinding(),
|
||||
new EndpointAddress(RenderprocessClientFactory.GetServiceName(ParentProcessId.Value, cefBrowserWrapper.BrowserId))
|
||||
);
|
||||
|
||||
channel.Open();
|
||||
|
||||
BrowserProcess = channel.CreateChannel();
|
||||
|
||||
Bind(BrowserProcess.GetRegisteredJavascriptObjects());
|
||||
}
|
||||
|
||||
public object EvaluateScript(int frameId, string script, double timeout)
|
||||
{
|
||||
var result = Browser.EvaluateScript(frameId, script, timeout);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,10 +91,6 @@
|
||||
<Compile Include="CefSubprocess.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="SubprocessProxy.cs" />
|
||||
<Compile Include="SubprocessServiceHost.cs" />
|
||||
<Compile Include="SubprocessDataContractResolver.cs" />
|
||||
<Compile Include="SubprocessSurrogates.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="app.config" />
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
using System;
|
||||
using System.CodeDom;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text;
|
||||
using System.Xml;
|
||||
using CefSharp.Internals;
|
||||
|
||||
namespace CefSharp.BrowserSubprocess
|
||||
{
|
||||
public class SubprocessDataContractResolver : DataContractResolver
|
||||
{
|
||||
public SubprocessDataContractResolver()
|
||||
{
|
||||
typesByName = new Dictionary<string, Type>();
|
||||
typesByName.Add(typeof(JavascriptObject).Name, typeof(JavascriptObjectWrapper));
|
||||
typesByName.Add(typeof(JavascriptProperty).Name, typeof(JavascriptPropertyWrapper));
|
||||
typesByName.Add(typeof(JavascriptMethod).Name, typeof(JavascriptMethodWrapper));
|
||||
}
|
||||
|
||||
private Dictionary<string, Type> typesByName;
|
||||
|
||||
public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
|
||||
{
|
||||
typeName = null;
|
||||
typeNamespace = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
|
||||
{
|
||||
Type result;
|
||||
if (!typesByName.TryGetValue(typeName, out result))
|
||||
{
|
||||
result = knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.ServiceModel;
|
||||
using CefSharp.Internals;
|
||||
|
||||
namespace CefSharp.BrowserSubprocess
|
||||
{
|
||||
public class SubprocessProxy : ISubprocessProxy
|
||||
{
|
||||
public ISubprocessCallback Callback { get; private set; }
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
Callback = OperationContext.Current.GetCallbackChannel<ISubprocessCallback>();
|
||||
|
||||
CefRenderprocess.Instance.ServiceHost.Initialize(this);
|
||||
}
|
||||
|
||||
public object EvaluateScript(int frameId, string script, double timeout)
|
||||
{
|
||||
var result = CefRenderprocess.Instance.Browser.EvaluateScript(frameId, script, timeout);
|
||||
return result;
|
||||
}
|
||||
|
||||
public void Terminate()
|
||||
{
|
||||
CefRenderprocess.Instance.Dispose();
|
||||
}
|
||||
|
||||
public void RegisterJavascriptObjects(JavascriptObject obj)
|
||||
{
|
||||
CefRenderprocess.Instance.RegisterJavascriptObjects((JavascriptObjectWrapper)obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,128 +0,0 @@
|
||||
using CefSharp.Internals;
|
||||
using System;
|
||||
using System.ServiceModel;
|
||||
using System.ServiceModel.Description;
|
||||
|
||||
namespace CefSharp.BrowserSubprocess
|
||||
{
|
||||
public class SubprocessServiceHost : ServiceHost, ISubprocessCallback
|
||||
{
|
||||
public SubprocessProxy Service { get; private set; }
|
||||
|
||||
public event Action<ISubprocessCallback> Initialized;
|
||||
|
||||
public void Initialize(SubprocessProxy service)
|
||||
{
|
||||
Service = service;
|
||||
|
||||
var handler = Initialized;
|
||||
if (handler != null)
|
||||
{
|
||||
handler(this);
|
||||
}
|
||||
}
|
||||
|
||||
private SubprocessServiceHost()
|
||||
: base(typeof(SubprocessProxy), new Uri[0])
|
||||
{
|
||||
}
|
||||
|
||||
public static SubprocessServiceHost Create(int parentProcessId, int browserId)
|
||||
{
|
||||
var host = CreateServiceHost();
|
||||
|
||||
var serviceName = SubprocessProxyFactory.GetServiceName(parentProcessId, browserId);
|
||||
host.KillExistingServiceIfNeeded(serviceName);
|
||||
|
||||
host.AddServiceEndpoint(
|
||||
typeof(ISubprocessProxy),
|
||||
new NetNamedPipeBinding(),
|
||||
new Uri(serviceName)
|
||||
);
|
||||
|
||||
var dataContractResolver = new SubprocessDataContractResolver();
|
||||
var surrogate = new SubprocessSurrogates();
|
||||
|
||||
host.ApplyOperationBehavior(
|
||||
(op) => new DataContractSerializerOperationBehavior(op),
|
||||
(b) =>
|
||||
{
|
||||
b.DataContractResolver = dataContractResolver;
|
||||
b.DataContractSurrogate = surrogate;
|
||||
} );
|
||||
|
||||
host.Open();
|
||||
return host;
|
||||
}
|
||||
|
||||
private static SubprocessServiceHost CreateServiceHost()
|
||||
{
|
||||
var host = new SubprocessServiceHost();
|
||||
var serviceDebugBehavior = host.Description.Behaviors.Find<ServiceDebugBehavior>();
|
||||
|
||||
if (serviceDebugBehavior == null)
|
||||
{
|
||||
serviceDebugBehavior = new ServiceDebugBehavior
|
||||
{
|
||||
IncludeExceptionDetailInFaults = true
|
||||
};
|
||||
host.Description.Behaviors.Add(serviceDebugBehavior);
|
||||
}
|
||||
else
|
||||
{
|
||||
serviceDebugBehavior.IncludeExceptionDetailInFaults = true;
|
||||
}
|
||||
|
||||
return host;
|
||||
}
|
||||
|
||||
private void KillExistingServiceIfNeeded(string serviceName)
|
||||
{
|
||||
// It might be that there is an existing process already bound to this port. We must get rid of that one, so that the
|
||||
// endpoint address gets available for us to use.
|
||||
try
|
||||
{
|
||||
var javascriptProxy = SubprocessProxyFactory.CreateSubprocessProxyClient(serviceName, this, TimeSpan.FromSeconds(1));
|
||||
javascriptProxy.Terminate();
|
||||
}
|
||||
catch
|
||||
{
|
||||
// We assume errors at this point are caused by things like the endpoint not being present (which will happen in
|
||||
// the first render subprocess instance).
|
||||
}
|
||||
}
|
||||
|
||||
public void ApplyOperationBehavior<T>(Func<OperationDescription,T> behaviorFactory,Action<T> behaviorManipulation)
|
||||
where T : class, IOperationBehavior
|
||||
{
|
||||
foreach (ServiceEndpoint ep in Description.Endpoints)
|
||||
{
|
||||
foreach (OperationDescription op in ep.Contract.Operations)
|
||||
{
|
||||
T behavior = op.Behaviors.Find<T>();
|
||||
if (behavior == null)
|
||||
{
|
||||
behavior = behaviorFactory(op);
|
||||
op.Behaviors.Add(behavior);
|
||||
}
|
||||
behaviorManipulation(behavior);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public object CallMethod(int objectId, string name, object[] parameters)
|
||||
{
|
||||
return Service.Callback.CallMethod(objectId, name, parameters);
|
||||
}
|
||||
|
||||
public object GetProperty(int objectId, string name)
|
||||
{
|
||||
return Service.Callback.GetProperty(objectId, name);
|
||||
}
|
||||
|
||||
public void SetProperty(int objectId, string name, object value)
|
||||
{
|
||||
Service.Callback.SetProperty(objectId, name, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
using System;
|
||||
using System.CodeDom;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text;
|
||||
using CefSharp.Internals;
|
||||
|
||||
namespace CefSharp.BrowserSubprocess
|
||||
{
|
||||
public class SubprocessSurrogates : IDataContractSurrogate
|
||||
{
|
||||
private Dictionary<Type, Type> surrogates;
|
||||
|
||||
public SubprocessSurrogates()
|
||||
{
|
||||
surrogates = new Dictionary<Type, Type>();
|
||||
surrogates.Add(typeof(JavascriptObject), typeof(JavascriptObjectWrapper));
|
||||
surrogates.Add(typeof(JavascriptProperty), typeof(JavascriptPropertyWrapper));
|
||||
surrogates.Add(typeof(JavascriptMethod), typeof(JavascriptMethodWrapper));
|
||||
}
|
||||
|
||||
public Type GetDataContractType(Type type)
|
||||
{
|
||||
Type surrogate;
|
||||
if (surrogates.TryGetValue(type, out surrogate))
|
||||
{
|
||||
return surrogate;
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
public object GetObjectToSerialize(object obj, Type targetType)
|
||||
{
|
||||
return obj;
|
||||
}
|
||||
|
||||
public object GetDeserializedObject(object obj, Type targetType)
|
||||
{
|
||||
return obj;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,10 +16,10 @@ using namespace System::ServiceModel;
|
||||
|
||||
namespace CefSharp
|
||||
{
|
||||
private ref class ManagedCefBrowserAdapter : public DisposableResource, ISubprocessCallback
|
||||
private ref class ManagedCefBrowserAdapter : public DisposableResource, IBrowserProcess
|
||||
{
|
||||
MCefRefPtr<RenderClientAdapter> _renderClientAdapter;
|
||||
ISubprocessProxy^ _javaScriptProxy;
|
||||
BrowserprocessServiceHost^ _browserprocessServiceHost;
|
||||
IWebBrowserInternal^ _webBrowserInternal;
|
||||
String^ _address;
|
||||
JavascriptObjectRepository^ _javaScriptObjectRepository;
|
||||
@@ -30,7 +30,12 @@ namespace CefSharp
|
||||
Close();
|
||||
|
||||
_renderClientAdapter = nullptr;
|
||||
_javaScriptProxy = nullptr;
|
||||
if (_browserprocessServiceHost != nullptr)
|
||||
{
|
||||
_browserprocessServiceHost->Close();
|
||||
_browserprocessServiceHost = nullptr;
|
||||
}
|
||||
|
||||
_webBrowserInternal = nullptr;
|
||||
_address = nullptr;
|
||||
_javaScriptObjectRepository = nullptr;
|
||||
@@ -100,12 +105,9 @@ namespace CefSharp
|
||||
void OnInitialized()
|
||||
{
|
||||
auto browserId = _renderClientAdapter->GetCefBrowser()->GetIdentifier();
|
||||
|
||||
auto serviceName = SubprocessProxyFactory::GetServiceName(Process::GetCurrentProcess()->Id, browserId);
|
||||
_javaScriptProxy = SubprocessProxyFactory::CreateSubprocessProxyClient(serviceName, this);
|
||||
|
||||
//CefTaskRunner::GetForCurrentThread()->PostDelayedTask(NewCefRunnableMethod(???), 100 );
|
||||
//_javaScriptProxy->Initialize();
|
||||
|
||||
_browserprocessServiceHost = gcnew BrowserprocessServiceHost(this, Process::GetCurrentProcess()->Id, browserId);
|
||||
_browserprocessServiceHost->Open();
|
||||
|
||||
_webBrowserInternal->OnInitialized();
|
||||
|
||||
@@ -389,12 +391,11 @@ namespace CefSharp
|
||||
{
|
||||
auto frame = _renderClientAdapter->TryGetCefMainFrame();
|
||||
|
||||
if (_javaScriptProxy != nullptr &&
|
||||
frame != nullptr)
|
||||
if (_browserprocessServiceHost != nullptr
|
||||
&& _browserprocessServiceHost->Renderprocess != nullptr
|
||||
&& frame != nullptr)
|
||||
{
|
||||
_javaScriptProxy->Initialize();
|
||||
_javaScriptProxy->RegisterJavascriptObjects(_javaScriptObjectRepository->RootObject);
|
||||
return _javaScriptProxy->EvaluateScript(frame->GetIdentifier(), script, timeout.TotalMilliseconds);
|
||||
return _browserprocessServiceHost->Renderprocess->EvaluateScript(frame->GetIdentifier(), script, timeout.TotalMilliseconds);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -462,5 +463,10 @@ namespace CefSharp
|
||||
virtual Object^ CallMethod(int objectId, String^ name, array<Object^>^ parameters);
|
||||
virtual Object^ GetProperty(int objectId, String^ name);
|
||||
virtual void SetProperty(int objectId, String^ name, Object^ value);
|
||||
|
||||
virtual JavascriptObject^ GetRegisteredJavascriptObjects()
|
||||
{
|
||||
return _javaScriptObjectRepository->RootObject;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -81,6 +81,7 @@
|
||||
<Reference Include="System" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.ServiceModel" />
|
||||
<Reference Include="System.Xaml">
|
||||
<RequiredTargetFramework>4.0</RequiredTargetFramework>
|
||||
</Reference>
|
||||
|
||||
@@ -89,9 +89,12 @@
|
||||
<Compile Include="CefEventFlags.cs" />
|
||||
<Compile Include="AddressChangedEventArgs.cs" />
|
||||
<Compile Include="FrameLoadStartEventArgs.cs" />
|
||||
<Compile Include="Internals\BrowserprocessService.cs" />
|
||||
<Compile Include="Internals\BrowserprocessServiceHost.cs" />
|
||||
<Compile Include="Internals\WCFExtensions.cs" />
|
||||
<Compile Include="NavStateChangedEventArgs.cs" />
|
||||
<Compile Include="Internals\BitmapInfo.cs" />
|
||||
<Compile Include="Internals\ISubprocessCallback.cs" />
|
||||
<Compile Include="Internals\IBrowserProcess.cs" />
|
||||
<Compile Include="Internals\JavascriptMemberDescription.cs" />
|
||||
<Compile Include="Internals\JavascriptMethod.cs" />
|
||||
<Compile Include="Internals\JavascriptMethodDescription.cs" />
|
||||
@@ -119,7 +122,7 @@
|
||||
<Compile Include="ISchemeHandler.cs" />
|
||||
<Compile Include="ISchemeHandlerFactory.cs" />
|
||||
<Compile Include="ISchemeHandlerResponse.cs" />
|
||||
<Compile Include="Internals\ISubprocessProxy.cs" />
|
||||
<Compile Include="Internals\IRenderprocess.cs" />
|
||||
<Compile Include="IWebBrowser.cs" />
|
||||
<Compile Include="Internals\IWebBrowserInternal.cs" />
|
||||
<Compile Include="Internals\Kernel32.cs" />
|
||||
@@ -129,7 +132,7 @@
|
||||
<Compile Include="DisposableResource.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="ScriptException.cs" />
|
||||
<Compile Include="Internals\SubprocessProxyFactory.cs" />
|
||||
<Compile Include="Internals\RenderprocessClientFactory.cs" />
|
||||
<Compile Include="TitleChangedEventArgs.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.ServiceModel;
|
||||
using System.Text;
|
||||
|
||||
namespace CefSharp.Internals
|
||||
{
|
||||
public class BrowserprocessService : IBrowserProcess
|
||||
{
|
||||
private BrowserprocessServiceHost host;
|
||||
|
||||
public BrowserprocessService()
|
||||
{
|
||||
host = (BrowserprocessServiceHost)OperationContext.Current.Host;
|
||||
host.Renderprocess = OperationContext.Current.GetCallbackChannel<IRenderprocess>();
|
||||
}
|
||||
|
||||
public object CallMethod(int objectId, string name, object[] parameters)
|
||||
{
|
||||
return host.Browserprocess.CallMethod(objectId, name, parameters);
|
||||
}
|
||||
|
||||
public object GetProperty(int objectId, string name)
|
||||
{
|
||||
return host.Browserprocess.GetProperty(objectId, name);
|
||||
}
|
||||
|
||||
public void SetProperty(int objectId, string name, object value)
|
||||
{
|
||||
host.Browserprocess.SetProperty(objectId, name, value);
|
||||
}
|
||||
|
||||
public JavascriptObject GetRegisteredJavascriptObjects()
|
||||
{
|
||||
return host.Browserprocess.GetRegisteredJavascriptObjects();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using CefSharp.Internals;
|
||||
using System;
|
||||
using System.ServiceModel;
|
||||
using System.ServiceModel.Description;
|
||||
|
||||
namespace CefSharp.Internals
|
||||
{
|
||||
public class BrowserprocessServiceHost : ServiceHost
|
||||
{
|
||||
public IBrowserProcess Browserprocess { get; private set; }
|
||||
public IRenderprocess Renderprocess { get; set; }
|
||||
|
||||
public BrowserprocessServiceHost(IBrowserProcess browserprocess, int parentProcessId, int browserId)
|
||||
: base(typeof(BrowserprocessService), new Uri[0])
|
||||
{
|
||||
Browserprocess = browserprocess;
|
||||
|
||||
var serviceName = RenderprocessClientFactory.GetServiceName(parentProcessId, browserId);
|
||||
|
||||
Description.ApplyServiceBehavior(() => new ServiceDebugBehavior(), p => p.IncludeExceptionDetailInFaults = true);
|
||||
|
||||
AddServiceEndpoint(
|
||||
typeof(IBrowserProcess),
|
||||
new NetNamedPipeBinding(),
|
||||
new Uri(serviceName)
|
||||
);
|
||||
}
|
||||
|
||||
protected override void OnClosed()
|
||||
{
|
||||
base.OnClosed();
|
||||
Browserprocess = null;
|
||||
Renderprocess = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
namespace CefSharp.Internals
|
||||
{
|
||||
[ServiceContract]
|
||||
public interface ISubprocessCallback
|
||||
[ServiceContract(CallbackContract = typeof(IRenderprocess))]
|
||||
public interface IBrowserProcess
|
||||
{
|
||||
[OperationContract]
|
||||
object CallMethod(int objectId, string name, object[] parameters);
|
||||
@@ -13,5 +13,8 @@ namespace CefSharp.Internals
|
||||
|
||||
[OperationContract]
|
||||
void SetProperty(int objectId, string name, object value);
|
||||
|
||||
[OperationContract]
|
||||
JavascriptObject GetRegisteredJavascriptObjects();
|
||||
}
|
||||
}
|
||||
@@ -2,22 +2,13 @@
|
||||
|
||||
namespace CefSharp.Internals
|
||||
{
|
||||
[ServiceContract(CallbackContract = typeof(ISubprocessCallback))]
|
||||
public interface ISubprocessProxy
|
||||
[ServiceContract()]
|
||||
public interface IRenderprocess
|
||||
{
|
||||
[OperationContract]
|
||||
void Initialize();
|
||||
|
||||
// TODO: Jan Eggers (and the rest of the world, basically) is right. We should do away with this method and instead use
|
||||
// a Task-based approach. The question is just how to make that work over the WCF channel? But perhaps this isn't really
|
||||
// a big problem if we just find a reasonable way to correlate the result back to the caller...
|
||||
[OperationContract]
|
||||
object EvaluateScript(int frameId, string script, double timeout);
|
||||
|
||||
[OperationContract]
|
||||
void Terminate();
|
||||
|
||||
[OperationContract]
|
||||
void RegisterJavascriptObjects(JavascriptObject obj);
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,7 @@ namespace CefSharp.Internals
|
||||
/// The identifier.
|
||||
/// </value>
|
||||
[DataMember]
|
||||
public long Id { get; private set; }
|
||||
public long Id { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the members of the <see cref="JavascriptObject" />.
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.ServiceModel;
|
||||
|
||||
namespace CefSharp.Internals
|
||||
{
|
||||
public class RenderprocessClientFactory
|
||||
{
|
||||
private const string BaseAddress = "net.pipe://localhost";
|
||||
private const string ServiceName = "CefSharpSubProcessProxy";
|
||||
|
||||
public static string GetServiceName(int parentProcessId, int browserId)
|
||||
{
|
||||
return string.Join("/", BaseAddress, ServiceName, parentProcessId, browserId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
using System;
|
||||
using System.ServiceModel;
|
||||
|
||||
namespace CefSharp.Internals
|
||||
{
|
||||
public class SubprocessProxyFactory
|
||||
{
|
||||
private const string BaseAddress = "net.pipe://localhost";
|
||||
private const string ServiceName = "CefSharpSubProcessProxy";
|
||||
|
||||
public static string GetServiceName(int parentProcessId, int browserId)
|
||||
{
|
||||
return string.Join("/", BaseAddress, ServiceName, parentProcessId, browserId);
|
||||
}
|
||||
|
||||
public static ISubprocessProxy CreateSubprocessProxyClient(string serviceName,
|
||||
ISubprocessCallback callbackObject)
|
||||
{
|
||||
return CreateSubprocessProxyClient(serviceName, callbackObject, null);
|
||||
}
|
||||
|
||||
public static ISubprocessProxy CreateSubprocessProxyClient(string serviceName,
|
||||
ISubprocessCallback callbackObject, TimeSpan? timeout)
|
||||
{
|
||||
var channel = new DuplexChannelFactory<ISubprocessProxy>(
|
||||
callbackObject,
|
||||
new NetNamedPipeBinding(),
|
||||
new EndpointAddress(serviceName)
|
||||
);
|
||||
|
||||
if (timeout != null)
|
||||
{
|
||||
channel.Open(timeout.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
channel.Open();
|
||||
}
|
||||
|
||||
return channel.CreateChannel();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.ServiceModel.Description;
|
||||
|
||||
namespace CefSharp.Internals
|
||||
{
|
||||
internal static class WCFExtensions
|
||||
{
|
||||
public static void ApplyOperationBehavior<T>(this ServiceDescription description,
|
||||
Func<OperationDescription, T> behaviorFactory,
|
||||
Action<T> behaviorManipulation)
|
||||
where T : class, IOperationBehavior
|
||||
{
|
||||
foreach (ServiceEndpoint ep in description.Endpoints)
|
||||
{
|
||||
ep.ApplyOperationBehavior(behaviorFactory, behaviorManipulation);
|
||||
}
|
||||
}
|
||||
|
||||
public static void ApplyOperationBehavior<T>(this ServiceEndpoint endpoint,
|
||||
Func<OperationDescription, T> behaviorFactory,
|
||||
Action<T> behaviorManipulation)
|
||||
where T : class, IOperationBehavior
|
||||
{
|
||||
foreach (OperationDescription op in endpoint.Contract.Operations)
|
||||
{
|
||||
T behavior = op.Behaviors.Find<T>();
|
||||
if (behavior == null)
|
||||
{
|
||||
behavior = behaviorFactory(op);
|
||||
op.Behaviors.Add(behavior);
|
||||
}
|
||||
behaviorManipulation(behavior);
|
||||
}
|
||||
}
|
||||
|
||||
public static void ApplyServiceBehavior<T>(this ServiceDescription description,
|
||||
Func<T> behaviorFactory,
|
||||
Action<T> behaviorManipulation)
|
||||
where T : class, IServiceBehavior
|
||||
{
|
||||
T behavior = description.Behaviors.Find<T>();
|
||||
if (behavior == null)
|
||||
{
|
||||
behavior = behaviorFactory();
|
||||
description.Behaviors.Add(behavior);
|
||||
}
|
||||
behaviorManipulation(behavior);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user