From 9af82ca3a76ada771fb64fec09b8d011e9b2c6a3 Mon Sep 17 00:00:00 2001 From: Per Lundberg Date: Mon, 28 Oct 2013 00:10:13 +0200 Subject: [PATCH] More work on #159. Quite far from getting it working. We cannot really use the CefBrowser object on the "wrong" thread, even though we are in the render process. Time to roll up our sleeves and start doing some CefPostTask() work I think... --- .../CefSharp.BrowserSubprocess.vcxproj | 4 +- ...CefSharp.BrowserSubprocess.vcxproj.filters | 20 ++++-- CefSharp.BrowserSubprocess/Common.h | 9 +++ .../JavascriptProxy.cpp | 37 +++++++++++ CefSharp.BrowserSubprocess/JavascriptProxy.h | 25 ++----- .../JavascriptProxyService.cpp | 66 +++++++++++++++++++ .../JavascriptProxyService.h | 54 ++++----------- CefSharp.BrowserSubprocess/Program.cpp | 3 - CefSharp.BrowserSubprocess/Program.h | 5 -- .../SubprocessCefApp.cpp | 21 +++++- CefSharp.BrowserSubprocess/SubprocessCefApp.h | 38 ++--------- 11 files changed, 166 insertions(+), 116 deletions(-) create mode 100644 CefSharp.BrowserSubprocess/Common.h create mode 100644 CefSharp.BrowserSubprocess/JavascriptProxy.cpp create mode 100644 CefSharp.BrowserSubprocess/JavascriptProxyService.cpp delete mode 100644 CefSharp.BrowserSubprocess/Program.h diff --git a/CefSharp.BrowserSubprocess/CefSharp.BrowserSubprocess.vcxproj b/CefSharp.BrowserSubprocess/CefSharp.BrowserSubprocess.vcxproj index d457b595a..41815b45e 100644 --- a/CefSharp.BrowserSubprocess/CefSharp.BrowserSubprocess.vcxproj +++ b/CefSharp.BrowserSubprocess/CefSharp.BrowserSubprocess.vcxproj @@ -166,6 +166,8 @@ + + @@ -181,8 +183,8 @@ + - diff --git a/CefSharp.BrowserSubprocess/CefSharp.BrowserSubprocess.vcxproj.filters b/CefSharp.BrowserSubprocess/CefSharp.BrowserSubprocess.vcxproj.filters index 5e757f570..f682e6d78 100644 --- a/CefSharp.BrowserSubprocess/CefSharp.BrowserSubprocess.vcxproj.filters +++ b/CefSharp.BrowserSubprocess/CefSharp.BrowserSubprocess.vcxproj.filters @@ -15,19 +15,19 @@ - - Header Files - - - Header Files - Header Files Header Files - + + Header Files + + + Header Files + + Header Files @@ -38,5 +38,11 @@ Source Files + + Source Files + + + Source Files + \ No newline at end of file diff --git a/CefSharp.BrowserSubprocess/Common.h b/CefSharp.BrowserSubprocess/Common.h new file mode 100644 index 000000000..fe368faba --- /dev/null +++ b/CefSharp.BrowserSubprocess/Common.h @@ -0,0 +1,9 @@ +// 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. + +// Not really used so much by all our classes, but it's imperative that this file gets included early enough to avoid this issue: +// http://stackoverflow.com/questions/4000663/issue-in-compiling-with-marshal-h-error-c2872-iserviceprovider-ambiguous +#include + +#define DECL __declspec(dllexport) diff --git a/CefSharp.BrowserSubprocess/JavascriptProxy.cpp b/CefSharp.BrowserSubprocess/JavascriptProxy.cpp new file mode 100644 index 000000000..131ed7d70 --- /dev/null +++ b/CefSharp.BrowserSubprocess/JavascriptProxy.cpp @@ -0,0 +1,37 @@ +// 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. + +#include "Common.h" +#include "JavascriptProxy.h" +#include "SubprocessCefApp.h" + +namespace CefSharp +{ + namespace BrowserSubprocess + { + JavascriptProxy::JavascriptProxy() + { + _subprocessCefApp = SubprocessCefApp::GetInstance(); + } + + Object^ JavascriptProxy::EvaluateScript(int browserId, int frameId, String^ script, double timeout) + { + return nullptr; + + //auto browser = _subprocessCefApp->GetBrowserById(browserId); + //auto frame = browser->GetFrame(frameId); + //auto v8Context = frame->GetV8Context(); + + //if (v8Context.get() && v8Context->Enter()) + //{ + // CefRefPtr result; + // CefRefPtr exception; + + // v8Context->Eval("10 + 20", result, exception); + //} + + //return "gurka"; + } + } +} \ No newline at end of file diff --git a/CefSharp.BrowserSubprocess/JavascriptProxy.h b/CefSharp.BrowserSubprocess/JavascriptProxy.h index d5bde0b6c..62a185de7 100644 --- a/CefSharp.BrowserSubprocess/JavascriptProxy.h +++ b/CefSharp.BrowserSubprocess/JavascriptProxy.h @@ -7,6 +7,8 @@ using namespace CefSharp::Internals::JavascriptBinding; using namespace System; +class SubprocessCefApp; + namespace CefSharp { namespace BrowserSubprocess @@ -22,27 +24,8 @@ namespace CefSharp literal String^ ServiceName = "JavaScriptProxy"; static String^ Address = BaseAddress + "/" + ServiceName; - 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 result; - CefRefPtr exception; - - v8Context->Eval("10 + 20", result, exception); - } - - return "gurka"; - } + JavascriptProxy(); + virtual Object^ EvaluateScript(int browserId, int frameId, String^ script, double timeout); }; } } diff --git a/CefSharp.BrowserSubprocess/JavascriptProxyService.cpp b/CefSharp.BrowserSubprocess/JavascriptProxyService.cpp new file mode 100644 index 000000000..6e26f59c5 --- /dev/null +++ b/CefSharp.BrowserSubprocess/JavascriptProxyService.cpp @@ -0,0 +1,66 @@ +// 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. + +#include +#include +#include "Common.h" +#include "JavascriptProxyService.h" + +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; + +JavascriptProxyService::JavascriptProxyService(CefRefPtr browser) +{ + _browser = &browser; + _browserIdentifier = browser->GetIdentifier(); +} + +void JavascriptProxyService::CreateJavascriptProxyServiceHost() +{ + auto thread = gcnew Thread(gcnew ThreadStart(this, &JavascriptProxyService::JavascriptProxyServiceEntryPoint)); + thread->Start(); +} + +void JavascriptProxyService::JavascriptProxyServiceEntryPoint() +{ + auto uris = gcnew array(1); + uris[0] = gcnew Uri(JavascriptProxy::BaseAddress); + + auto host = gcnew ServiceHost(JavascriptProxy::typeid, uris); + AddDebugBehavior(host); + + // TODO: Include the name of the "parent process" here also, and perhaps then also clean out the debug stuff... + auto serviceName = JavascriptProxy::ServiceName + "_" + _browserIdentifier; + auto log = "Setting up JavascriptProxy listener at " + serviceName; + pin_ptr serviceNameUnmanagedStr = PtrToStringChars(log); + OutputDebugString(serviceNameUnmanagedStr); + + host->AddServiceEndpoint( + IJavascriptProxy::typeid, + gcnew NetNamedPipeBinding(), + serviceName + ); + + host->Open(); +} + +void JavascriptProxyService::AddDebugBehavior(ServiceHost^ host) +{ + auto serviceDebugBehavior = host->Description->Behaviors->Find(); + + if (serviceDebugBehavior == nullptr) + { + serviceDebugBehavior = gcnew ServiceDebugBehavior(); + serviceDebugBehavior->IncludeExceptionDetailInFaults = true; + host->Description->Behaviors->Add(serviceDebugBehavior); + } + else + { + serviceDebugBehavior->IncludeExceptionDetailInFaults = true; + } +} diff --git a/CefSharp.BrowserSubprocess/JavascriptProxyService.h b/CefSharp.BrowserSubprocess/JavascriptProxyService.h index af2cb4cb7..b3c18b554 100644 --- a/CefSharp.BrowserSubprocess/JavascriptProxyService.h +++ b/CefSharp.BrowserSubprocess/JavascriptProxyService.h @@ -5,52 +5,20 @@ #pragma once #include "JavascriptProxy.h" +#include "include/cef_browser.h" -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 JavascriptProxyServiceEntryPoint(); -static void AddDebugBehavior(ServiceHost^ host); - -void CreateJavascriptProxyServiceHost() +ref class JavascriptProxyService { - auto thread = gcnew Thread(gcnew ThreadStart(JavascriptProxyServiceEntryPoint)); - thread->Start(); -} + CefRefPtr* _browser; + int _browserIdentifier; -void JavascriptProxyServiceEntryPoint() -{ - auto uris = gcnew array(1); - uris[0] = gcnew Uri(JavascriptProxy::BaseAddress); +public: + JavascriptProxyService(CefRefPtr browser); - auto host = gcnew ServiceHost(JavascriptProxy::typeid, uris); - AddDebugBehavior(host); + void CreateJavascriptProxyServiceHost(); - host->AddServiceEndpoint( - IJavascriptProxy::typeid, - gcnew NetNamedPipeBinding(), - JavascriptProxy::ServiceName - ); +private: - host->Open(); -} - -void AddDebugBehavior(ServiceHost^ host) -{ - auto serviceDebugBehavior = host->Description->Behaviors->Find(); - - if (serviceDebugBehavior == nullptr) - { - serviceDebugBehavior = gcnew ServiceDebugBehavior(); - serviceDebugBehavior->IncludeExceptionDetailInFaults = true; - host->Description->Behaviors->Add(serviceDebugBehavior); - } - else - { - serviceDebugBehavior->IncludeExceptionDetailInFaults = true; - } -} + void JavascriptProxyServiceEntryPoint(); + void AddDebugBehavior(System::ServiceModel::ServiceHost^ host); +}; \ No newline at end of file diff --git a/CefSharp.BrowserSubprocess/Program.cpp b/CefSharp.BrowserSubprocess/Program.cpp index 974db679b..a1fa77d60 100644 --- a/CefSharp.BrowserSubprocess/Program.cpp +++ b/CefSharp.BrowserSubprocess/Program.cpp @@ -2,9 +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" void LogCommandLine(LPTSTR cmdLine); @@ -13,7 +11,6 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmd CefMainArgs mainArgs(hInstance); LogCommandLine(lpCmdLine); - //CreateJavascriptProxyServiceHost(); return ExecuteCefRenderProcess(mainArgs); } diff --git a/CefSharp.BrowserSubprocess/Program.h b/CefSharp.BrowserSubprocess/Program.h deleted file mode 100644 index 96374f673..000000000 --- a/CefSharp.BrowserSubprocess/Program.h +++ /dev/null @@ -1,5 +0,0 @@ -// 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) diff --git a/CefSharp.BrowserSubprocess/SubprocessCefApp.cpp b/CefSharp.BrowserSubprocess/SubprocessCefApp.cpp index 4015d6d01..4e08953fe 100644 --- a/CefSharp.BrowserSubprocess/SubprocessCefApp.cpp +++ b/CefSharp.BrowserSubprocess/SubprocessCefApp.cpp @@ -2,9 +2,24 @@ // // 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 "Common.h" +#include "JavascriptProxyService.h" #include "SubprocessCefApp.h" SubprocessCefApp* SubprocessCefApp::_instance = nullptr; + +SubprocessCefApp* SubprocessCefApp::GetInstance() +{ + if (_instance == nullptr) + { + _instance = new SubprocessCefApp(); + } + + return _instance; +} + +void SubprocessCefApp::OnBrowserCreated(CefRefPtr browser) +{ + auto javascriptProxyService = gcnew JavascriptProxyService(browser); + javascriptProxyService->CreateJavascriptProxyServiceHost(); +} diff --git a/CefSharp.BrowserSubprocess/SubprocessCefApp.h b/CefSharp.BrowserSubprocess/SubprocessCefApp.h index 86d8d0738..fd96f19fa 100644 --- a/CefSharp.BrowserSubprocess/SubprocessCefApp.h +++ b/CefSharp.BrowserSubprocess/SubprocessCefApp.h @@ -4,57 +4,29 @@ #pragma once -#include +#include "Common.h" #include "include/cef_app.h" private class SubprocessCefApp : public CefApp, public CefRenderProcessHandler { static SubprocessCefApp* _instance; - std::list> _browsers; public: - static SubprocessCefApp* GetInstance() - { - if (_instance == nullptr) - { - _instance = new SubprocessCefApp(); - } - - return _instance; - } + static SubprocessCefApp* GetInstance(); virtual DECL CefRefPtr GetRenderProcessHandler() OVERRIDE { return this; } - virtual DECL void OnBrowserCreated(CefRefPtr browser) OVERRIDE - { - _browsers.push_back(browser); - } + virtual DECL void OnBrowserCreated(CefRefPtr browser) OVERRIDE; virtual DECL void OnBrowserDestroyed(CefRefPtr browser) OVERRIDE { - _browsers.remove(browser); - } - - virtual DECL CefRefPtr 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.begin(); - browser != _browsers.end(); - browser++) - { - if ((*browser)->GetIdentifier() == browser_id) - { - return *browser; - } - } - - return nullptr; + // FIXME: Why do we never get called? + OutputDebugString(L"OnBrowserDestroyed called"); } IMPLEMENT_REFCOUNTING(SubprocessCefApp);