Skip to content

Commit

Permalink
Very much work-in-progress towards #159. The idea is to marshal the J…
Browse files Browse the repository at this point in the history
…avascript execution over to the CefFrame in the render process, but it doesn't seem to work yet… Have to check with Marshall & friends.
  • Loading branch information
perlun committed Sep 2, 2013
1 parent 632f016 commit 5193bce
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 6 deletions.
7 changes: 5 additions & 2 deletions CefSharp.BrowserSubprocess/CefRenderProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
//
// 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_app.h"
#include "SubprocessCefApp.h"

int ExecuteCefRenderProcess(HINSTANCE hInstance)
{
CefMainArgs main_args(hInstance);
CefRefPtr<CefApp> app;
return CefExecuteProcess(main_args, app);
CefRefPtr<SubprocessCefApp> subprocessCefAppPtr = SubprocessCefApp::GetInstance();
return CefExecuteProcess(main_args, (CefRefPtr<CefApp>) subprocessCefAppPtr);
}
3 changes: 3 additions & 0 deletions CefSharp.BrowserSubprocess/CefSharp.BrowserSubprocess.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Program.cpp" />
<ClCompile Include="SubprocessCefApp.cpp" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CefSharp\CefSharp.vcxproj">
Expand All @@ -105,6 +106,8 @@
<ClInclude Include="CefRenderProcess.h" />
<ClInclude Include="JavascriptProxy.h" />
<ClInclude Include="JavascriptProxyService.h" />
<ClInclude Include="Program.h" />
<ClInclude Include="SubprocessCefApp.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,19 @@
<ClInclude Include="CefRenderProcess.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="SubprocessCefApp.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Program.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Program.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="SubprocessCefApp.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
21 changes: 20 additions & 1 deletion CefSharp.BrowserSubprocess/JavascriptProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,34 @@ namespace CefSharp
{
private ref class JavascriptProxy : IJavascriptProxy
{
SubprocessCefApp* _subprocessCefApp;

public:
// TODO: Don't hardwire the address like this, since it makes it impossible to run multiple apps that use
// CefSharp simultaneously...
literal String^ BaseAddress = "net.pipe://localhost";
literal String^ ServiceName = "JavaScriptProxy";
static String^ Address = BaseAddress + "/" + ServiceName;

virtual Object^ EvaluateScript(String^ script, double timeout)
JavascriptProxy()
{
_subprocessCefApp = SubprocessCefApp::GetInstance();
}

virtual Object^ EvaluateScript(int browserId, int frameId, String^ script, double timeout)
{
auto browser = _subprocessCefApp->GetBrowserById(browserId);
auto frame = browser->GetFrame(frameId);
auto v8Context = frame->GetV8Context();

if (v8Context.get() && v8Context->Enter())
{
CefRefPtr<CefV8Value> result;
CefRefPtr<CefV8Exception> exception;

v8Context->Eval("10 + 20", result, exception);
}

return "gurka";
}
};
Expand Down
26 changes: 23 additions & 3 deletions CefSharp.BrowserSubprocess/JavascriptProxyService.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,26 @@ using namespace CefSharp::Internals::JavascriptBinding;
using namespace CefSharp::BrowserSubprocess;
using namespace System;
using namespace System::ServiceModel;
using namespace System::ServiceModel::Description;
using namespace System::Threading;

static void ThreadEntryPoint();
static void JavascriptProxyServiceEntryPoint();
static void AddDebugBehavior(ServiceHost^ host);

void CreateJavascriptProxyServiceHost()
{
auto thread = gcnew Thread(gcnew ThreadStart(ThreadEntryPoint));
auto thread = gcnew Thread(gcnew ThreadStart(JavascriptProxyServiceEntryPoint));
thread->Start();
}

void ThreadEntryPoint()
void JavascriptProxyServiceEntryPoint()
{
auto uris = gcnew array<Uri^>(1);
uris[0] = gcnew Uri(JavascriptProxy::BaseAddress);

auto host = gcnew ServiceHost(JavascriptProxy::typeid, uris);
AddDebugBehavior(host);

host->AddServiceEndpoint(
IJavascriptProxy::typeid,
gcnew NetNamedPipeBinding(),
Expand All @@ -34,3 +38,19 @@ void ThreadEntryPoint()

host->Open();
}

void AddDebugBehavior(ServiceHost^ host)
{
auto serviceDebugBehavior = host->Description->Behaviors->Find<ServiceDebugBehavior^>();

if (serviceDebugBehavior == nullptr)
{
serviceDebugBehavior = gcnew ServiceDebugBehavior();
serviceDebugBehavior->IncludeExceptionDetailInFaults = true;
host->Description->Behaviors->Add(serviceDebugBehavior);
}
else
{
serviceDebugBehavior->IncludeExceptionDetailInFaults = true;
}
}
1 change: 1 addition & 0 deletions CefSharp.BrowserSubprocess/Program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

#include "Program.h"
#include "CefRenderProcess.h"
#include "JavascriptProxyService.h"

Expand Down
5 changes: 5 additions & 0 deletions CefSharp.BrowserSubprocess/Program.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright © 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.

#define DECL __declspec(dllexport)
10 changes: 10 additions & 0 deletions CefSharp.BrowserSubprocess/SubprocessCefApp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright © 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 "Program.h"
#include "SubprocessCefApp.h"

SubprocessCefApp* SubprocessCefApp::_instance = nullptr;
59 changes: 59 additions & 0 deletions CefSharp.BrowserSubprocess/SubprocessCefApp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright © 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 <list>
#include "include/cef_app.h"

private class SubprocessCefApp : public CefApp,
public CefRenderProcessHandler
{
static SubprocessCefApp* _instance;
std::list<CefRefPtr<CefBrowser>> _browsers;

public:

static SubprocessCefApp* GetInstance()
{
if (_instance == nullptr)
{
_instance = new SubprocessCefApp();
}

return _instance;
}

virtual DECL CefRefPtr<CefRenderProcessHandler> GetRenderProcessHandler() OVERRIDE
{
return this;
}

virtual DECL void OnBrowserCreated(CefRefPtr<CefBrowser> browser) OVERRIDE
{
_browsers.push_back(browser);
}

virtual DECL void OnBrowserDestroyed(CefRefPtr<CefBrowser> browser) OVERRIDE
{
_browsers.remove(browser);
}

virtual DECL CefRefPtr<CefBrowser> GetBrowserById(int browser_id)
{
// FIXME: Doesn't seem to work. Our list contains a browser w/ ID == 0, and the parameter we get is 1. How come?
// We could experiment with OnContextCreated() to see if it would work better...
for (auto browser : _browsers)
{
if (browser->GetIdentifier() == browser_id)
{
return browser;
}
}

return nullptr;
}

IMPLEMENT_REFCOUNTING(SubprocessCefApp);
};

0 comments on commit 5193bce

Please sign in to comment.