Exemplo n.º 1
0
        public static int Main(string[] args)
        {
            Debug.WriteLine("BrowserSubprocess starting up with command line: " + String.Join("\n", args));

            SubProcess.EnableHighDPISupport();

            int result;

            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")
            {
                var wcfEnabled = args.HasArgument(CefSharpArguments.WcfEnabledArgument);
                var subProcess = wcfEnabled ? new WcfEnabledSubProcess(args) : new SubProcess(args);

                using (subProcess)
                {
                    result = subProcess.Run();
                }
            }
            else
            {
                result = SubProcess.ExecuteProcess();
            }

            Debug.WriteLine("BrowserSubprocess shutting down.");

            return(result);
        }
Exemplo n.º 2
0
        public static int Main(string[] args)
        {
            Debug.WriteLine("BrowserSubprocess starting up with command line: " + String.Join("\n", args));

            SubProcess.EnableHighDPISupport();

            int result;
            var type            = args.GetArgumentValue(CefSharpArguments.SubProcessTypeArgument);
            var parentProcessId = int.Parse(args.GetArgumentValue(CefSharpArguments.HostProcessIdArgument));

            if (args.HasArgument(CefSharpArguments.ExitIfParentProcessClosed))
            {
                Task.Factory.StartNew(() => AwaitParentProcessExit(parentProcessId), TaskCreationOptions.LongRunning);
            }

            //Use our custom subProcess provides features like EvaluateJavascript
            if (type == "renderer")
            {
                var wcfEnabled = args.HasArgument(CefSharpArguments.WcfEnabledArgument);
                var subProcess = wcfEnabled ? new WcfEnabledSubProcess(parentProcessId, args) : new SubProcess(args);

                using (subProcess)
                {
                    result = subProcess.Run();
                }
            }
            else
            {
                result = SubProcess.ExecuteProcess();
            }

            Debug.WriteLine("BrowserSubprocess shutting down.");

            return(result);
        }
Exemplo n.º 3
0
        private static int Main(string[] args)
        {
            SubProcess.EnableHighDPISupport();

            string FindArg(string key)
            {
                return(Array.Find(args, arg => arg.StartsWith(key, StringComparison.OrdinalIgnoreCase)).Substring(key.Length));
            }

            const string typePrefix     = "--type=";
            const string parentIdPrefix = "--host-process-id=";

            if (!int.TryParse(FindArg(parentIdPrefix), out int parentId))
            {
                return(0);
            }

            Task.Factory.StartNew(() => KillWhenHung(parentId), TaskCreationOptions.LongRunning);

            if (FindArg(typePrefix) == "renderer")
            {
                using SubProcess subProcess = new SubProcess(null, args);
                return(subProcess.Run());
            }
            else
            {
                return(SubProcess.ExecuteProcess(args));
            }
        }
Exemplo n.º 4
0
        public static int Main(string[] args)
        {
            Debug.WriteLine("BrowserSubprocess starting up with command line: " + string.Join("\n", args));

            SubProcess.EnableHighDPISupport();

            if (!System.IO.File.Exists("CefSharp.dll") && System.IO.File.Exists("..\\CefSharp.dll"))
            {
                //For publshing ClickOnce AnyCPU CefSharp.dll isn't included in the x64 build
                //and the  BrowserSubprocess fails to launch as a result.
                //As a temp workaround load the file from the parent directory.
                System.Reflection.Assembly.LoadFrom("..\\CefSharp.dll");
            }

            return(MainInternal(args));
        }
Exemplo n.º 5
0
        private static int MainInternal(string[] args)
        {
            SubProcess.EnableHighDPISupport();

            //Add your own custom implementation of IRenderProcessHandler here
            IRenderProcessHandler handler = null;

            //The WcfBrowserSubprocessExecutable provides BrowserSubProcess functionality
            //specific to CefSharp, WCF support (required for Sync JSB) will optionally be
            //enabled if the CefSharpArguments.WcfEnabledArgument command line arg is present
            //For .Net Core use BrowserSubprocessExecutable as there is no WCF support
            var browserProcessExe = new WcfBrowserSubprocessExecutable();
            var result            = browserProcessExe.Main(args, handler);

            Debug.WriteLine("BrowserSubprocess shutting down.");

            return(result);
        }
Exemplo n.º 6
0
        private static int Main(string[] args)
        {
            SubProcess.EnableHighDPISupport();

            const string typePrefix = "--type=";
            string       type       = Array.Find(args, arg => arg.StartsWith(typePrefix, StringComparison.OrdinalIgnoreCase)).Substring(typePrefix.Length);

            if (type == "renderer")
            {
                using (RendererProcess subProcess = new RendererProcess(args)){
                    return(subProcess.Run());
                }
            }
            else
            {
                return(SubProcess.ExecuteProcess());
            }
        }
Exemplo n.º 7
0
        public static int Main(string[] args)
        {
            Debug.WriteLine("BrowserSubprocess starting up with command line: " + string.Join("\n", args));

            SubProcess.EnableHighDPISupport();

            //Add your own custom implementation of IRenderProcessHandler here
            IRenderProcessHandler handler = null;

            //The BrowserSubprocessExecutable provides BrowserSubProcess functionality
            //specific to CefSharp there is no WCF support used for the sync JSB feature.
            var browserProcessExe = new BrowserSubprocessExecutable();
            var result            = browserProcessExe.Main(args, handler);

            Debug.WriteLine("BrowserSubprocess shutting down.");

            return(result);
        }
Exemplo n.º 8
0
        public static int Main(string[] args)
        {
            Debug.WriteLine("BrowserSubprocess starting up with command line: " + String.Join("\n", args));

            SubProcess.EnableHighDPISupport();

            int result;
            var type = args.GetArgumentValue(CefSharpArguments.SubProcessTypeArgument);

            var parentProcessId = -1;

            // The Crashpad Handler doesn't have any HostProcessIdArgument, so we must not try to
            // parse it lest we want an ArgumentNullException.
            if (type != "crashpad-handler")
            {
                parentProcessId = int.Parse(args.GetArgumentValue(CefSharpArguments.HostProcessIdArgument));
                if (args.HasArgument(CefSharpArguments.ExitIfParentProcessClosed))
                {
                    Task.Factory.StartNew(() => AwaitParentProcessExit(parentProcessId), TaskCreationOptions.LongRunning);
                }
            }

            // Use our custom subProcess provides features like EvaluateJavascript
            if (type == "renderer")
            {
                //Add your own custom implementation of IRenderProcessHandler here
                IRenderProcessHandler handler = null;
                var wcfEnabled = args.HasArgument(CefSharpArguments.WcfEnabledArgument);
                var subProcess = wcfEnabled ? new WcfEnabledSubProcess(parentProcessId, handler, args) : new SubProcess(handler, args);

                using (subProcess)
                {
                    result = subProcess.Run();
                }
            }
            else
            {
                result = SubProcess.ExecuteProcess(args);
            }

            Debug.WriteLine("BrowserSubprocess shutting down.");

            return(result);
        }
Exemplo n.º 9
0
        private static int Main(string[] args)
        {
            // Check if the "--type:" argument was supplied, which means we need
            // to run the browser subprocess.
            if (args.HasArgument(CefSharpArguments.SubProcessTypeArgument + "="))
            {
                Debug.WriteLine("BrowserSubprocess starting up with command line: " + String.Join("\n", args));

                SubProcess.EnableHighDPISupport();

                var browserProcessExe = new BrowserSubprocessExecutable();
                var result            = browserProcessExe.Main(args, null);

                Debug.WriteLine("BrowserSubprocess shutting down.");

                return(result);
            }

            // Search for other Main methods in this assembly.
            var mainMethods = new List <MethodInfo>();

            var thisType = typeof(SubprocessStartupHandler);

            Type[] assemblyTypes;
            try {
                assemblyTypes = thisType.Assembly.GetTypes();
            }
            catch (ReflectionTypeLoadException ex)
            {
                // Note: ex.Types can contain null for types that weren't loaded.
                assemblyTypes = ex.Types;
            }

            foreach (var type in assemblyTypes)
            {
                if (type == null || type == thisType || !(type.IsClass || type.IsValueType) || type.IsEnum || type.IsGenericTypeDefinition)
                {
                    continue;
                }

                var mainMethodEmpty       = type.GetMethod(MainMethodName, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null);
                var mainMethodStringArray = type.GetMethod(MainMethodName, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, stringArrayParameterTypes, null);

                if (ValidateMainMethod(mainMethodEmpty))
                {
                    mainMethods.Add(mainMethodEmpty);
                }
                if (ValidateMainMethod(mainMethodStringArray))
                {
                    mainMethods.Add(mainMethodStringArray);
                }
            }

            if (mainMethods.Count == 0)
            {
                throw new InvalidOperationException("No Main method suitable as entry point was found.");
            }

            if (mainMethods.Count > 1)
            {
                for (int i = mainMethods.Count - 1; i >= 0; i--)
                {
                    if (mainMethods[i].GetCustomAttribute <EntryPointAttribute>() == null)
                    {
                        mainMethods.RemoveAt(i);
                    }
                }

                if (mainMethods.Count == 0)
                {
                    throw new InvalidOperationException(
                              $"More than one Main method suitable as entry point was found. " +
                              $"Please annotate the method to be used with the " +
                              $"{nameof(CefSharp)}.{nameof(CefSharp.BrowserSubprocess)}.{nameof(CefSharp.BrowserSubprocess.WinForms)}.{nameof(EntryPointAttribute)}.");
                }
                else if (mainMethods.Count > 1)
                {
                    throw new InvalidOperationException(
                              $"More than one Main method annotated with the " +
                              $"{nameof(CefSharp)}.{nameof(CefSharp.BrowserSubprocess)}.{nameof(CefSharp.BrowserSubprocess.WinForms)}.{nameof(EntryPointAttribute)} " +
                              $"was found.");
                }
            }

            // Call the main method that we found.
            var mainMethod           = mainMethods[0];
            var mainMethodParameters = mainMethod.GetParameters();

            // Create a delegate with the exact type to avoid reflection overhead
            // (like exceptions being wrapped in InvocationTargetExceptions) when
            // calling the method.
            if (mainMethod.ReturnType == typeof(int) && mainMethodParameters.Length == 0)
            {
                var d = (Func <int>)Delegate.CreateDelegate(typeof(Func <int>), mainMethod);
                return(d());
            }
            else if (mainMethod.ReturnType == typeof(int))
            {
                var d = (Func <string[], int>)Delegate.CreateDelegate(typeof(Func <string[], int>), mainMethod);
                return(d(args));
            }
            else if (mainMethodParameters.Length == 0)
            {
                var d = (Action)Delegate.CreateDelegate(typeof(Action), mainMethod);
                d();
                return(Environment.ExitCode);
            }
            else
            {
                var d = (Action <string[]>)Delegate.CreateDelegate(typeof(Action <string[]>), mainMethod);
                d(args);
                return(Environment.ExitCode);
            }
        }