From 7bbcf162f9eb24a8705567304b3be52e326cfa97 Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 16 May 2016 18:52:35 +1000 Subject: [PATCH] Remove CefSubProcess.cpp - having such trivial cpp files seems silly Convert CefRenderProcess into a cpp class - this should theoretically allow users to add CefSharp.BrowserSubprocess.Core as a reference then use their own process to spawn it Move CefRenderProcess and CefSubProcess into CefSharp.BrowserSubprocess namespace - otherclasses should really be moved as well Only use our custom subProcess when type is render, for gpu and plugin processes the standard implementation is just fine Remove CefSubProcess Rename CefAppWrapper to CefSubProcess --- .../CefAppWrapper.cpp | 23 ---- .../CefAppWrapper.h | 57 ---------- .../CefRenderProcess.cpp | 75 +++++++++++++ .../CefRenderProcess.h | 38 +++++++ .../CefSharp.BrowserSubprocess.Core.vcxproj | 5 +- ...arp.BrowserSubprocess.Core.vcxproj.filters | 15 ++- .../CefSubProcess.h | 83 ++++++++++++++ .../CefRenderProcess.cs | 106 ------------------ .../CefSharp.BrowserSubprocess.csproj | 2 - CefSharp.BrowserSubprocess/CefSubProcess.cs | 26 ----- CefSharp.BrowserSubprocess/Program.cs | 43 +++---- CefSharp/Internals/CommandLineArgsParser.cs | 19 ++++ .../Internals/RenderprocessClientFactory.cs | 2 +- 13 files changed, 246 insertions(+), 248 deletions(-) delete mode 100644 CefSharp.BrowserSubprocess.Core/CefAppWrapper.cpp delete mode 100644 CefSharp.BrowserSubprocess.Core/CefAppWrapper.h create mode 100644 CefSharp.BrowserSubprocess.Core/CefRenderProcess.cpp create mode 100644 CefSharp.BrowserSubprocess.Core/CefRenderProcess.h create mode 100644 CefSharp.BrowserSubprocess.Core/CefSubProcess.h delete mode 100644 CefSharp.BrowserSubprocess/CefRenderProcess.cs delete mode 100644 CefSharp.BrowserSubprocess/CefSubProcess.cs diff --git a/CefSharp.BrowserSubprocess.Core/CefAppWrapper.cpp b/CefSharp.BrowserSubprocess.Core/CefAppWrapper.cpp deleted file mode 100644 index d782f69e3..000000000 --- a/CefSharp.BrowserSubprocess.Core/CefAppWrapper.cpp +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright © 2010-2016 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 "CefAppWrapper.h" - -using namespace System::Diagnostics; -using namespace System::Collections::Generic; - -namespace CefSharp -{ - int CefAppWrapper::Run() - { - auto hInstance = Process::GetCurrentProcess()->Handle; - - CefMainArgs cefMainArgs((HINSTANCE)hInstance.ToPointer()); - - return CefExecuteProcess(cefMainArgs, (CefApp*)_cefApp.get(), NULL); - } -} \ No newline at end of file diff --git a/CefSharp.BrowserSubprocess.Core/CefAppWrapper.h b/CefSharp.BrowserSubprocess.Core/CefAppWrapper.h deleted file mode 100644 index eadbd3b55..000000000 --- a/CefSharp.BrowserSubprocess.Core/CefAppWrapper.h +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright © 2010-2016 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_app.h" -#include "include/cef_base.h" - -#include "CefBrowserWrapper.h" -#include "CefAppUnmanagedWrapper.h" - -using namespace System::Collections::Generic; -using namespace System::Linq; -using namespace CefSharp::Internals; - -namespace CefSharp -{ - // Wrap CefAppUnmangedWrapper in a nice managed wrapper - public ref class CefAppWrapper abstract - { - private: - MCefRefPtr _cefApp; - - public: - CefAppWrapper(IEnumerable^ args) - { - auto onBrowserCreated = gcnew Action(this, &CefAppWrapper::OnBrowserCreated); - auto onBrowserDestroyed = gcnew Action(this, &CefAppWrapper::OnBrowserDestroyed); - auto schemes = CefCustomScheme::ParseCommandLineArguments(args); - auto enableFocusedNodeChanged = CommandLineArgsParser::HasArgument(args, CefSharpArguments::FocusedNodeChangedEnabledArgument); - - _cefApp = new CefAppUnmanagedWrapper(schemes, enableFocusedNodeChanged, onBrowserCreated, onBrowserDestroyed); - }; - - !CefAppWrapper() - { - _cefApp = nullptr; - } - - ~CefAppWrapper() - { - this->!CefAppWrapper(); - } - - int Run(); - - virtual void OnBrowserCreated(CefBrowserWrapper^ cefBrowserWrapper) abstract; - virtual void OnBrowserDestroyed(CefBrowserWrapper^ cefBrowserWrapper) abstract; - - static void EnableHighDPISupport() - { - CefEnableHighDPISupport(); - } - }; -} diff --git a/CefSharp.BrowserSubprocess.Core/CefRenderProcess.cpp b/CefSharp.BrowserSubprocess.Core/CefRenderProcess.cpp new file mode 100644 index 000000000..269cc05c6 --- /dev/null +++ b/CefSharp.BrowserSubprocess.Core/CefRenderProcess.cpp @@ -0,0 +1,75 @@ +// Copyright © 2010-2016 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 "CefRenderProcess.h" + +using namespace System::ServiceModel; + +namespace CefSharp +{ + namespace BrowserSubprocess + { + void CefRenderProcess::OnBrowserCreated(CefBrowserWrapper^ browser) + { + if (!parentBrowserId.HasValue) + { + parentBrowserId = browser->BrowserId; + } + + if (!parentProcessId.HasValue || !parentBrowserId.HasValue) + { + return; + } + + auto browserId = browser->IsPopup ? parentBrowserId.Value : browser->BrowserId; + + auto serviceName = RenderprocessClientFactory::GetServiceName(parentProcessId.Value, browserId); + + auto binding = BrowserProcessServiceHost::CreateBinding(); + + auto channelFactory = gcnew ChannelFactory( + binding, + gcnew EndpointAddress(serviceName) + ); + + channelFactory->Open(); + + auto browserProcess = channelFactory->CreateChannel(); + auto clientChannel = ((IClientChannel^)browserProcess); + + try + { + clientChannel->Open(); + + browser->ChannelFactory = channelFactory; + browser->BrowserProcess = browserProcess; + } + catch (Exception^) + { + } + } + + void CefRenderProcess::OnBrowserDestroyed(CefBrowserWrapper^ browser) + { + auto channelFactory = browser->ChannelFactory; + + if (channelFactory->State == CommunicationState::Opened) + { + channelFactory->Close(); + } + + auto clientChannel = ((IClientChannel^)browser->BrowserProcess); + + if (clientChannel->State == CommunicationState::Opened) + { + clientChannel->Close(); + } + + browser->ChannelFactory = nullptr; + browser->BrowserProcess = nullptr; + } + } +} \ No newline at end of file diff --git a/CefSharp.BrowserSubprocess.Core/CefRenderProcess.h b/CefSharp.BrowserSubprocess.Core/CefRenderProcess.h new file mode 100644 index 000000000..0ac14b438 --- /dev/null +++ b/CefSharp.BrowserSubprocess.Core/CefRenderProcess.h @@ -0,0 +1,38 @@ +// Copyright © 2010-2016 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 "CefSubProcess.h" +#include "CefBrowserWrapper.h" + +using namespace System; + +namespace CefSharp +{ + namespace BrowserSubprocess + { + public ref class CefRenderProcess : CefSubProcess + { + private: + Nullable parentBrowserId; + + /// + /// The PID for the parent (browser) process + /// + Nullable parentProcessId; + + public: + CefRenderProcess(IEnumerable^ args) : CefSubProcess(args) + { + parentProcessId = CommandLineArgsParser::LocateParentProcessId(args); + } + + void OnBrowserCreated(CefBrowserWrapper^ browser) override; + void OnBrowserDestroyed(CefBrowserWrapper^ browser) override; + + }; + } +} \ No newline at end of file diff --git a/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj b/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj index 549fda94b..ec1d639ae 100644 --- a/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj +++ b/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj @@ -171,7 +171,8 @@ - + + @@ -196,7 +197,7 @@ - + diff --git a/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj.filters b/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj.filters index e141521fd..35c208eb2 100644 --- a/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj.filters +++ b/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj.filters @@ -14,9 +14,6 @@ Header Files - - Header Files - Header Files @@ -86,6 +83,12 @@ Header Files + + Header Files + + + Header Files + @@ -94,9 +97,6 @@ Source Files - - Source Files - Source Files @@ -145,6 +145,9 @@ Source Files + + Source Files + diff --git a/CefSharp.BrowserSubprocess.Core/CefSubProcess.h b/CefSharp.BrowserSubprocess.Core/CefSubProcess.h new file mode 100644 index 000000000..c9b896122 --- /dev/null +++ b/CefSharp.BrowserSubprocess.Core/CefSubProcess.h @@ -0,0 +1,83 @@ +// Copyright © 2010-2016 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_app.h" +#include "include/cef_base.h" + +#include "CefBrowserWrapper.h" +#include "CefAppUnmanagedWrapper.h" + +using namespace System::Collections::Generic; +using namespace System::Linq; +using namespace CefSharp::Internals; + +namespace CefSharp +{ + namespace BrowserSubprocess + { + // Wrap CefAppUnmangedWrapper in a nice managed wrapper + public ref class CefSubProcess + { + private: + MCefRefPtr _cefApp; + + public: + CefSubProcess(IEnumerable^ args) + { + auto onBrowserCreated = gcnew Action(this, &CefSubProcess::OnBrowserCreated); + auto onBrowserDestroyed = gcnew Action(this, &CefSubProcess::OnBrowserDestroyed); + auto schemes = CefCustomScheme::ParseCommandLineArguments(args); + auto enableFocusedNodeChanged = CommandLineArgsParser::HasArgument(args, CefSharpArguments::FocusedNodeChangedEnabledArgument); + + _cefApp = new CefAppUnmanagedWrapper(schemes, enableFocusedNodeChanged, onBrowserCreated, onBrowserDestroyed); + } + + !CefSubProcess() + { + _cefApp = nullptr; + } + + ~CefSubProcess() + { + this->!CefSubProcess(); + } + + int Run() + { + auto hInstance = Process::GetCurrentProcess()->Handle; + + CefMainArgs cefMainArgs((HINSTANCE)hInstance.ToPointer()); + + return CefExecuteProcess(cefMainArgs, (CefApp*)_cefApp.get(), NULL); + } + + virtual void OnBrowserCreated(CefBrowserWrapper^ cefBrowserWrapper) + { + + } + + virtual void OnBrowserDestroyed(CefBrowserWrapper^ cefBrowserWrapper) + { + + } + + static void EnableHighDPISupport() + { + CefEnableHighDPISupport(); + } + + static int ExecuteProcess() + { + auto hInstance = Process::GetCurrentProcess()->Handle; + + CefMainArgs cefMainArgs((HINSTANCE)hInstance.ToPointer()); + + return CefExecuteProcess(cefMainArgs, NULL, NULL); + } + }; + } +} diff --git a/CefSharp.BrowserSubprocess/CefRenderProcess.cs b/CefSharp.BrowserSubprocess/CefRenderProcess.cs deleted file mode 100644 index 02ae43d77..000000000 --- a/CefSharp.BrowserSubprocess/CefRenderProcess.cs +++ /dev/null @@ -1,106 +0,0 @@ -// Copyright © 2010-2016 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.Collections.Generic; -using System.Linq; -using System.ServiceModel; -using CefSharp.Internals; - -namespace CefSharp.BrowserSubprocess -{ - public class CefRenderProcess : CefSubProcess - { - private int? parentBrowserId; - - /// - /// The PID for the parent (browser) process - /// - private int? parentProcessId; - - public CefRenderProcess(IEnumerable args) : base(args) - { - parentProcessId = LocateParentProcessId(args); - } - - private static int? LocateParentProcessId(IEnumerable args) - { - // Format being parsed: - // --channel=3828.2.1260352072\1102986608 - // We only really care about the PID (3828) part. - const string channelPrefix = "--channel="; - var channelArgument = args.SingleOrDefault(arg => arg.StartsWith(channelPrefix)); - if (channelArgument == null) - { - return null; - } - - var parentProcessId = channelArgument - .Substring(channelPrefix.Length) - .Split('.') - .First(); - return int.Parse(parentProcessId); - } - - public override void OnBrowserCreated(CefBrowserWrapper browser) - { - if (parentBrowserId == null) - { - parentBrowserId = browser.BrowserId; - } - - if (parentProcessId == null || parentBrowserId == null) - { - return; - } - - var browserId = browser.IsPopup ? parentBrowserId.Value : browser.BrowserId; - - var serviceName = RenderprocessClientFactory.GetServiceName(parentProcessId.Value, browserId); - - var binding = BrowserProcessServiceHost.CreateBinding(); - - var channelFactory = new ChannelFactory( - binding, - new EndpointAddress(serviceName) - ); - - channelFactory.Open(); - - var browserProcess = channelFactory.CreateChannel(); - var clientChannel = ((IClientChannel)browserProcess); - - try - { - clientChannel.Open(); - - browser.ChannelFactory = channelFactory; - browser.BrowserProcess = browserProcess; - } - catch(Exception) - { - } - } - - public override void OnBrowserDestroyed(CefBrowserWrapper browser) - { - var channelFactory = browser.ChannelFactory; - - if (channelFactory.State == CommunicationState.Opened) - { - channelFactory.Close(); - } - - var clientChannel = ((IClientChannel)browser.BrowserProcess); - - if (clientChannel.State == CommunicationState.Opened) - { - clientChannel.Close(); - } - - browser.ChannelFactory = null; - browser.BrowserProcess = null; - } - } -} diff --git a/CefSharp.BrowserSubprocess/CefSharp.BrowserSubprocess.csproj b/CefSharp.BrowserSubprocess/CefSharp.BrowserSubprocess.csproj index 4bc112ba3..20e5dd28b 100644 --- a/CefSharp.BrowserSubprocess/CefSharp.BrowserSubprocess.csproj +++ b/CefSharp.BrowserSubprocess/CefSharp.BrowserSubprocess.csproj @@ -89,8 +89,6 @@ - - diff --git a/CefSharp.BrowserSubprocess/CefSubProcess.cs b/CefSharp.BrowserSubprocess/CefSubProcess.cs deleted file mode 100644 index f469c70e7..000000000 --- a/CefSharp.BrowserSubprocess/CefSubProcess.cs +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright © 2010-2016 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.Collections.Generic; - -namespace CefSharp.BrowserSubprocess -{ - public class CefSubProcess : CefAppWrapper - { - public CefSubProcess(IEnumerable args) : base(args) - { - - } - - public override void OnBrowserCreated(CefBrowserWrapper cefBrowserWrapper) - { - - } - - public override void OnBrowserDestroyed(CefBrowserWrapper cefBrowserWrapper) - { - - } - } -} \ No newline at end of file diff --git a/CefSharp.BrowserSubprocess/Program.cs b/CefSharp.BrowserSubprocess/Program.cs index 3b9252715..4c7297a36 100644 --- a/CefSharp.BrowserSubprocess/Program.cs +++ b/CefSharp.BrowserSubprocess/Program.cs @@ -15,39 +15,32 @@ namespace CefSharp.BrowserSubprocess { Kernel32.OutputDebugString("BrowserSubprocess starting up with command line: " + String.Join("\n", args)); - CefAppWrapper.EnableHighDPISupport(); + CefSubProcess.EnableHighDPISupport(); int result; - using (var subprocess = Create(args)) + const string typePrefix = "--type="; + var typeArgument = args.SingleOrDefault(arg => arg.StartsWith(typePrefix)); + var type = typeArgument.Substring(typePrefix.Length); + + //Use our custom subProcess provides features like EvaluateJavascript + if (type == "renderer") { - result = subprocess.Run(); + var wcfEnabled = args.HasArgument(CefSharpArguments.WcfEnabledArgument); + var subProcess = wcfEnabled ? new CefRenderProcess(args) : new CefSubProcess(args); + + using (subProcess) + { + result = subProcess.Run(); + } } + else + { + result = CefSubProcess.ExecuteProcess(); + } Kernel32.OutputDebugString("BrowserSubprocess shutting down."); return result; } - - private static CefSubProcess Create(IEnumerable args) - { - const string typePrefix = "--type="; - var typeArgument = args.SingleOrDefault(arg => arg.StartsWith(typePrefix)); - var wcfEnabled = args.HasArgument(CefSharpArguments.WcfEnabledArgument); - - var type = typeArgument.Substring(typePrefix.Length); - - switch (type) - { - case "renderer": - { - return wcfEnabled ? new CefRenderProcess(args) : new CefSubProcess(args); - } - case "gpu-process": - default: - { - return new CefSubProcess(args); - } - } - } } } diff --git a/CefSharp/Internals/CommandLineArgsParser.cs b/CefSharp/Internals/CommandLineArgsParser.cs index 1755361df..cc691f322 100644 --- a/CefSharp/Internals/CommandLineArgsParser.cs +++ b/CefSharp/Internals/CommandLineArgsParser.cs @@ -16,5 +16,24 @@ namespace CefSharp.Internals { return args.Any(a => a.StartsWith(arg)); } + + public static int? LocateParentProcessId(this IEnumerable args) + { + // Format being parsed: + // --channel=3828.2.1260352072\1102986608 + // We only really care about the PID (3828) part. + const string channelPrefix = "--channel="; + var channelArgument = args.SingleOrDefault(arg => arg.StartsWith(channelPrefix)); + if (channelArgument == null) + { + return null; + } + + var parentProcessId = channelArgument + .Substring(channelPrefix.Length) + .Split('.') + .First(); + return int.Parse(parentProcessId); + } } } diff --git a/CefSharp/Internals/RenderprocessClientFactory.cs b/CefSharp/Internals/RenderprocessClientFactory.cs index bebd52506..d0b9124fc 100644 --- a/CefSharp/Internals/RenderprocessClientFactory.cs +++ b/CefSharp/Internals/RenderprocessClientFactory.cs @@ -4,7 +4,7 @@ namespace CefSharp.Internals { - internal class RenderprocessClientFactory + public class RenderprocessClientFactory { private const string BaseAddress = "net.pipe://localhost"; private const string ServiceName = "CefSharpSubProcessProxy";