如何在Chromium源代码中添加自定义命令行开关

huangapple go评论63阅读模式
英文:

How to add a custom command line switch in Chromium source code

问题

我正在尝试在Windows上向Chromium源代码中添加自定义命令行选项,以便我可以使用类似chrome.exe --my-custom-flag=value的标志来运行浏览器。目标是在Chromium代码库的任何位置都能访问这个值。然而,到目前为止,我的尝试都没有成功。

我尝试使用以下代码片段在Chromium源代码中读取命令行值:

std::string value = base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII("my-custom-flag");

然而,这种方法没有产生期望的结果。我还尝试了另一种方法,使用CommandLine::ForCurrentProcess()->HasSwitch()方法来检查自定义标志是否存在,但它也没有起作用。

我将非常感激关于如何正确实现和读取Chromium源代码中的自定义命令行选项的任何建议或指导。提前感谢您的帮助。

英文:

I'm attempting to add a custom command line option to the Chromium source code on windows, allowing me to run the browser with a flag like chrome.exe --my-custom-flag=value. The goal is to access this value anywhere within the Chromium codebase. However, my attempts so far have been unsuccessful.

I've tried using the following code snippet to read the command line value within the Chromium source:

std::string value = base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII("my-custom-flag");

However, this method didn't yield the desired result. I also tried another approach using the CommandLine::ForCurrentProcess()->HasSwitch() method to check if the custom flag was present, but it didn't work either.

I would greatly appreciate any advice or guidance on how to properly implement and read a custom command line option in the Chromium source code. Thank you in advance for your assistance.

答案1

得分: 0

third_party/blink/renderer/core/frame/navigator.cc 是 Chromium 渲染器进程的一部分,您尝试在渲染器中运行您的代码。您的命令行开关 --my-custom-flag 不会被传递到渲染器进程,它会在以下函数中被过滤掉:

void RenderProcessHostImpl::PropagateBrowserCommandLineToRenderer(
    const base::CommandLine& browser_cmd,
    base::CommandLine* renderer_cmd);

将您的开关添加到该成员函数中的数组 kSwitchNames 中,它将被传递到渲染器进程。请注意,所有开关必须在定义时不包括开始的双破折号 --

英文:

third_party/blink/renderer/core/frame/navigator.cc is a part of Chromium renderer process, you attempt to run your code in a renderer. You command line switch --my-custom-flag is not forwarded to a renderer process, it is filtered out in

void RenderProcessHostImpl::PropagateBrowserCommandLineToRenderer(
    const base::CommandLine& browser_cmd,
    base::CommandLine* renderer_cmd);

Add your switch to the array kSwitchNames in that member function, and it will be propagated to a renderer process. Note, all switches must be defined without the beginning double dashes --.

huangapple
  • 本文由 发表于 2023年6月12日 10:56:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453400.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定