|
| 1 | +// Copyright © 2019 The CefSharp Authors. All rights reserved. |
| 2 | +// |
| 3 | +// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +#pragma once |
| 6 | + |
| 7 | +#include "Stdafx.h" |
| 8 | + |
| 9 | +#include "SubProcess.h" |
| 10 | +#include "WcfEnabledSubProcess.h" |
| 11 | + |
| 12 | +using namespace System; |
| 13 | +using namespace CefSharp::Internals; |
| 14 | + |
| 15 | +namespace CefSharp |
| 16 | +{ |
| 17 | + namespace BrowserSubprocess |
| 18 | + { |
| 19 | + /// <summary> |
| 20 | + /// BrowserSubprocessExecutable provides the fundimental browser process handling for |
| 21 | + /// CefSharp.BrowserSubprocess.exe and can be used to self host the BrowserSubProcess in your |
| 22 | + /// existing application (preferred approach for .Net Core). |
| 23 | + /// </summary> |
| 24 | + public ref class BrowserSubprocessExecutable |
| 25 | + { |
| 26 | + public: |
| 27 | + BrowserSubprocessExecutable() |
| 28 | + { |
| 29 | + |
| 30 | + } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// This function should be called from the application entry point function (typically Program.Main) |
| 34 | + /// to execute a secondary process e.g. gpu, plugin, renderer, utility |
| 35 | + /// It can be used to run secondary processes (BrowserSubProcess) from your main applications executable |
| 36 | + /// or from a separate executable specified by the CefSettings.BrowserSubprocessPath value. |
| 37 | + /// CefSharp defaults to using the latter approach, a default implementation (CefSharp.BrowserSubProcess.exe) is |
| 38 | + /// supplied, see https://github.com/cefsharp/CefSharp/wiki/General-Usage#processes for more details. |
| 39 | + /// </summary> |
| 40 | + /// <param name="args">command line args</param> |
| 41 | + /// <returns> |
| 42 | + /// If called for the browser process (identified by no "type" command-line value) it will return immediately |
| 43 | + /// with a value of -1. If called for a recognized secondary process it will block until the process should exit |
| 44 | + /// and then return the process exit code. |
| 45 | + /// </returns |
| 46 | + int Main(IEnumerable<String^>^ args) |
| 47 | + { |
| 48 | + return Main(args, nullptr); |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// This function should be called from the application entry point function (typically Program.Main) |
| 53 | + /// to execute a secondary process e.g. gpu, plugin, renderer, utility |
| 54 | + /// It can be used to run secondary processes (BrowserSubProcess) from your main applications executable |
| 55 | + /// or from a separate executable specified by the CefSettings.BrowserSubprocessPath value. |
| 56 | + /// CefSharp defaults to using the latter approach, a default implementation (CefSharp.BrowserSubProcess.exe) is |
| 57 | + /// supplied, see https://github.com/cefsharp/CefSharp/wiki/General-Usage#processes for more details. |
| 58 | + /// </summary> |
| 59 | + /// <param name="args">command line args</param> |
| 60 | + /// <param name="handler">An option IRenderProcessHandler implementation, use null if no handler is required</param> |
| 61 | + /// <returns> |
| 62 | + /// If called for the browser process (identified by no "type" command-line value) it will return immediately |
| 63 | + /// with a value of -1. If called for a recognized secondary process it will block until the process should exit |
| 64 | + /// and then return the process exit code. |
| 65 | + /// </returns> |
| 66 | + int Main(IEnumerable<String^>^ args, IRenderProcessHandler^ handler) |
| 67 | + { |
| 68 | + auto type = CommandLineArgsParser::GetArgumentValue(args, CefSharpArguments::SubProcessTypeArgument); |
| 69 | + |
| 70 | + auto parentProcessId = -1; |
| 71 | + |
| 72 | + // The Crashpad Handler doesn't have any HostProcessIdArgument, so we must not try to |
| 73 | + // parse it lest we want an ArgumentNullException. |
| 74 | + if (type != "crashpad-handler") |
| 75 | + { |
| 76 | + parentProcessId = int::Parse(CommandLineArgsParser::GetArgumentValue(args, CefSharpArguments::HostProcessIdArgument)); |
| 77 | + if (CommandLineArgsParser::HasArgument(args, CefSharpArguments::ExitIfParentProcessClosed)) |
| 78 | + { |
| 79 | + ParentProcessMonitor::StartMonitorTask(parentProcessId); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + // Use our custom subProcess provides features like EvaluateJavascript |
| 84 | + if (type == "renderer") |
| 85 | + { |
| 86 | + auto subProcess = GetSubprocess(args, parentProcessId, handler); |
| 87 | + |
| 88 | + try |
| 89 | + { |
| 90 | + return subProcess->Run(); |
| 91 | + } |
| 92 | + finally |
| 93 | + { |
| 94 | + delete subProcess; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return SubProcess::ExecuteProcess(args); |
| 99 | + } |
| 100 | + |
| 101 | + protected: |
| 102 | + virtual SubProcess^ GetSubprocess(IEnumerable<String^>^ args, int parentProcessId, IRenderProcessHandler^ handler) |
| 103 | + { |
| 104 | + return gcnew SubProcess(handler, args); |
| 105 | + } |
| 106 | + }; |
| 107 | + } |
| 108 | +} |
0 commit comments