Chromium headless模式開發的一切

獲取和編譯chromium

Linux:https://chromium.googlesource.com/chromium/src/+/refs/heads/main/docs/linux/build_instructions.md

Windows:https://chromium.googlesource.com/chromium/src/+/refs/heads/main/docs/windows_build_instructions.md

Mac:https://chromium.googlesource.com/chromium/src/+/refs/heads/main/docs/mac_build_instructions.md

重要文檔

headless模式:https://chromium.googlesource.com/chromium/src/+/lkgr/headless

Vscode配置:https://chromium.googlesource.com/chromium/src/+/refs/heads/main/docs/vscode.md

源碼查看:https://source.chromium.org/chromium/chromium/src

GN構建參數:https://www.chromium.org/developers/gn-build-configuration

Linux安裝依賴

安裝上依賴,不安裝會有缺庫等問題。

yum install git python bzip2 tar pkgconfig atk-devel alsa-lib-devel \
bison binutils brlapi-devel bluez-libs-devel bzip2-devel cairo-devel \
cups-devel dbus-devel dbus-glib-devel expat-devel fontconfig-devel \
freetype-devel gcc-c++ glib2-devel glibc.i686 gperf glib2-devel gtk2-devel \
gtk3-devel java-1.*.0-openjdk-devel libatomic libcap-devel libffi-devel \
libgcc.i686 libgnome-keyring-devel libjpeg-devel libstdc++.i686 libX11-devel \
libXScrnSaver-devel libXtst-devel libxkbcommon-x11-devel ncurses-compat-libs \
nspr-devel nss-devel pam-devel pango-devel pciutils-devel \
pulseaudio-libs-devel zlib.i686 httpd mod_ssl php php-cli python-psutil wdiff \
xorg-x11-server-Xvfb

設置代理獲取chromium代碼

git 設置代理:

git config --global http.proxy http://127.0.0.1:1080
git config --global https.proxy https://127.0.0.1:1080

git config --global --unset http.proxy
git config --global --unset https.proxy 

全局代理:

export http_proxy="http://127.0.0.1:1080"
export https_proxy="https://127.0.0.1:1080"

Boto代理設置:

[Boto文件]
proxy=127.0.0.1
proxy_port = 1080

然後設置:
export NO_AUTH_BOTO_CONFIG=/usr/local/dev/chromium/chromium_src/http_proxy.boto(linux)
set NO_AUTH_BOTO_CONFIG=E:\chromium_src\httpproxy.boto (windows)

參考文章:
Windows源碼下載編譯:https://blog.berd.moe/archives/get-code-and-compile-chromium/

踩坑文章:http://blog.wils0n.cn/archives/171/

代理使用心得:https://blog.csdn.net/Vincent95/article/details/79828480

Chromium默認編譯不支持音視頻的播放

爲了避免授權和專利的問題,在 Chromium 中是不能直接內置音頻以及視頻解碼器的,所以就造成了默認編譯出來的 Chromium 不能播放音視頻。

解決方法是,在args.gn文件中增加編譯參數

proprietary_codecs = true
ffmpeg_branding = "Chrome"

chromium官方文檔中對此說明

GN Flags
There are a few GN flags which can alter the behaviour of Chromium's HTML5 audio/video implementation.

ffmpeg_branding
  Overrides which version of FFmpeg to use
  Default: $(branding)
  Values:
    Chrome - includes additional proprietary codecs (MP3, etc..) for use with Google Chrome
    Chromium - builds default set of codecs

proprietary_codecs
  Alters the list of codecs Chromium claims to support, which affects <source> and canPlayType() behaviour
  Default: 0(gyp)/false(gn)
  Values:
    0/false - <source> and canPlayType() assume the default set of codecs
    1/true - <source> and canPlayType() assume they support additional proprietary codecs

加速編譯

Chromium官方文檔中提供了一些可以加速編譯的GN編譯項。

symbol_level = 0
blink_symbol_level= 0
enable_nacl = false

一些重要的編譯參數介紹

  • is_debug。這個選項值可以爲true或者false。當爲true時編譯debug版本,false時編譯release版本。

  • is_component_build。這個選項值可以爲true或者false。當爲true時將chromium代碼編譯成多個小的dll,false時代碼編譯成單個dll。一般我們編譯debug版本時,設置is_component_build = true,這樣每次改動編譯鏈接花費的時間就會減少很多。編譯release版本時,設置is_component_build = false,這樣就可以把所有代碼編譯到一個dll裏面。

  • target_cpu。這個選項值爲字符串,控制我們編譯出的程序所匹配的cpu。編譯32位x86版本設置成target_cpu =”x86″,編譯64位x64版本設置成target_cpu =”x64″。如果我們沒有顯式指定target_cpu的值,那麼target_cpu的值爲編譯它的電腦所用的cpu類型。通常target_cpu的值爲x86會比x64編譯速度更快,並且支持增量編譯。另外如果設置了target_cpu =”x86″,也必須設置enable_nacl = false,否則編譯速度會慢很多。

  • enable_nacl。這個選項值可以爲true或者false。控制是否啓用Native Client,通常我們並不需要。所以把其值設置成enable_nacl = false。

  • is_clang。這個選項值可以爲true或者false。控制是否啓用clang進行編譯。目前m63 clang編譯還不穩定,所以這個選項設置成is_clang = false。m64開始支持clang編譯。

  • ffmpeg_branding=”Chrome” proprietary_codecs=true。這個兩個選項是控制代碼編譯支持的多媒體格式跟chrome一樣,支持mp4等格式。

  • symbol_level。其值爲整數。當值爲0時,不生成調試符號,可以加快代碼編譯鏈接速度。當值爲1時,生成的調試符號中不包含源代碼信息,無法進行源代碼級調試,但是可以加快代碼編譯鏈接速度。當值爲2時,生成完整的調試符號,編譯鏈接時間比較長。

  • is_official_build。這個選項值可以爲true或者false。控制是否啓用official編譯模式。official編譯模式會進行代碼編譯優化,非常耗時。僅發佈的時候設置成is_official_build = true開啓優化。

GN編譯命令

# 生成編譯目錄
gn gen out/Default

# 設置編譯目錄的編譯參數
gn args out/Default

# 查看編譯目錄的編譯參數
gn args --list out/Default

# 啓動編譯
ninja -C out/Default

# headless_shell編譯
ninja -C out/Release headless_shell

headless_shell編譯參數

##### debug

import("//build/args/headless.gn")
is_component_build = true
is_debug = true
symbol_level = 0
blink_symbol_level= 0
enable_nacl = false
proprietary_codecs = true
ffmpeg_branding = "Chrome"

#### release

import("//build/args/headless.gn")
is_component_build = false
is_debug = false
symbol_level = 0
blink_symbol_level= 0
enable_nacl = false
proprietary_codecs = true
ffmpeg_branding = "Chrome"

Chromium中視頻不自動播放

chromuim 66 版本以後的內核,在默認情況下<video>和<audio>標籤已經不能自動播放了。需要用戶點擊觸發後才播放,或者要把播放設置爲靜音模式纔可自動播放。

解決方法是,啓動參數中增加--autoplay-policy=no-user-gesture-required來關閉這個默認策略。

啓動參數

chrome:

./out/Default/chrome --headless --no-sandbox --ignore-certificate-errors --ignore-ssl-errors --disable-gpu --disable-software-rasterizer --remote-debugging-port=9222 https://www.baidu.com

headless_shell:

./out/Release/headless_shell --no-sandbox --ignore-certificate-errors --ignore-ssl-errors --disable-gpu --disable-software-rasterizer --remote-debugging-address=0.0.0.0 --remote-debugging-port=9222 https://www.baidu.com

headless_shell進程指令

查看headless_shell進程是否存在

ps -ef | grep headless_shell

關閉headless_shell進程

pkill -f '(chrome)?(--headless)'

VirtualBox虛擬機遠程調試

  1. 啓動參數增加--remote-debugging-address=0.0.0.0 --remote-debugging-port=9222。
  2. 關閉虛擬機中操作系統的防火牆,或者開放9222端口。
  3. VirtualBox設置端口轉發,從子系統9222到主機任意可用端口。
  4. 瀏覽器打開chrome://inspect/#devices開始調試。

調試相關

調試地址:chrome://inspect/#devices

一些調試接口:

  1. http://127.0.0.1:9222/json 查看已經打開的Tab列表
  2. http://127.0.0.1:9222/json/version : 查看瀏覽器版本信息
  3. http://127.0.0.1:9222/json/new?http://www.baidu.com : 新開Tab打開指定地址
  4. http://127.0.0.1:9222/json/close/8795FFF09B01BD41B1F2931110475A67 :關閉指定Tab,close後爲tab頁面的id
  5. http://127.0.0.1:9222/json/activate/5C7774203404DC082182AF4563CC7256 : 切換到目標Ta

chromium C++與javascript互操作

  • 仿照extensions_v8::LoadTimesExtension

  • 在ChromeContentRendererClient的函數RenderThreadStarted()中註冊

    thread->RegisterExtension(extensions_v8::XXXXExtension::Get());

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章