Chromium内核浏览器编译记UI定制

Posted 容华谢后

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Chromium内核浏览器编译记UI定制相关的知识,希望对你有一定的参考价值。

转载请注明出处:https://blog.csdn.net/kong_gu_you_lan/article/details/125842497

本文出自 容华谢后的博客

往期回顾:

Chromium内核浏览器编译记(一)踩坑实录

Chromium内核浏览器编译记(二)UI定制

0.写在前面

在上一篇文章中,我们学习了如何编译Chromium内核浏览器,在开发中,经常需要对浏览器进行一些UI和功能的定制,今天就一起来看下,如何修改浏览器的UI功能。

定制需求是这样的:

  • 修改包名、版本号、应用图标、应用名称

  • 不显示应用第一次启动时的设置引导页

  • 应用全屏显示,移除首页的所有菜单栏

  • 增加点击两次退出应用的功能

  • 默认不加载新标签页

1.修改包名

修改包名有两种方法,一种是在编译脚本中修改,一种是直接在源码中修改,一起来看下:

1.1 编译脚本

很简单,直接在args.gn文件中增加一个配置就可以了:

system_webview_package_name="包名"

1.2 源码修改

源码路径:/chromium/src/chrome/android/BUILD.gn

将下面两个属性的值替换成你要修改的包名就可以了:

_default_package = "org.chromium.chrome"

chrome_public_test_manifest_package = "org.chromium.chrome.tests"

2.修改版本号

源码路径:/chromium/src/chrome/android/java/AndroidMainfest.xml

在manifest节点中,增加版本属性就可以了,注意这个修改不会影响到浏览器中显示的内核版本号:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package=" manifest_package "
    % set definitions_in_split = definitions_in_split|default(0) == 'true' %
    % if definitions_in_split %
    android:isolatedSplits="true"
    % endif %
    tools:ignore="MissingVersion"
    android:versionCode="10000"
	android:versionName="1.00.00">

	...

</manifest>    

3.修改应用图标

源码路径:/chromium/src/chrome/android/java/res_chromium

在这个目录下,有几个drawable文件夹,把里面的图标替换下就可以,和AS项目是一样的。

4.修改应用名称

源码路径:/chromium/src/chrome/android/java/res_chromium_base/values/channel_constants.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <string name="app_name" translatable="false">应用名称</string>
</resources>

5.屏蔽启动引导页

源码路径:/chromium/src/chrome/android/java/src/org/chromium/chrome/browser/init/AsyncInitializationActivity.java

将requiresFirstRunToBeCompleted方法的返回值修改为false:

/**
 * Overriding this function is almost always wrong.
 * @return Whether or not the user needs to go through First Run before using this Activity.
 */
protected boolean requiresFirstRunToBeCompleted(Intent intent) 
    return false;

源码路径:/chromium/src/chrome/android/java/src/org/chromium/chrome/browser/LaunchIntentDispatcher.java

删除或注释掉dispatch方法中下面的判断逻辑:

// Check if we should push the user through First Run.
if (FirstRunFlowSequencer.launch(mActivity, mIntent, false /* requiresBroadcast */,
            false /* preferLightweightFre */)) 
    return Action.FINISH_ACTIVITY;

6.应用全屏显示

源码路径:

  • /chromium/src/chrome/android/java/res/values-sw600dp-v17/styles.xml

  • /chromium/src/chrome/android/java/res/values-sw600dp-v26/styles.xml

  • /chromium/src/chrome/android/java/res/values-v26/styles.xml

在上面的源码中,找到TabbedModeTheme主题,增加下面的全屏设置属性:

<style name="TabbedModeTheme" parent="MainTheme">
   
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowActionBar">false</item>
</style>

7.移除首页的所有菜单栏

源码路径:/chromium/src/chrome/android/java/res_app/layout/main.xml

在CoordinatorLayoutForPointer控件中,增加layout_marginTop属性,其实是往上移动了UI,把菜单栏隐藏了:

<merge
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

	...

    <org.chromium.components.browser_ui.widget.CoordinatorLayoutForPointer
        android:id="@+id/coordinator"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" 
        android:layout_marginTop="-96dp">

</mergr>        

8.增加点击两次退出应用的功能

源码路径:/chromium/src/chrome/android/java/src/org/chromium/chrome/browser/app/ChromeActivity.java

在ChromeActivity类中增加下面的方法:

public int getTabsCount() 
    TabModelSelector modelSelector = getTabModelSelector();
    if (modelSelector == null) return 0;
    return modelSelector.getTotalTabCount();
    

源码路径:/chromium/src/chrome/android/java/src/org/chromium/chrome/browser/ChromeTabbedActivity.java

修改handleBackPressed方法:

private boolean mExitApp;

@Override
public boolean handleBackPressed() 
    if (!mUIWithNativeInitialized) return false;

    if (getManualFillingComponent().handleBackPress()) return true;

    if (exitFullscreenIfShowing()) 
        return true;
    

    // TODO(1091411): Find a better mechanism for back-press handling for features.
    if (mRootUiCoordinator.getBottomSheetController().handleBackPress()) return true;

    if (mTabModalHandler.handleBackPress()) return true;

    final Tab currentTab = getActivityTab();
    if (currentTab == null) 
        moveTaskToBack(true);
        return true;
    

    // If we are in the tab switcher mode (not in the Start surface homepage) and not a tablet,
    // then leave tab switcher mode on back.
    if (mOverviewModeController.overviewVisible() && !isTablet()
            && (mStartSurfaceSupplier.get() == null
                    || mStartSurfaceSupplier.get().getController().getStartSurfaceState()
                            == StartSurfaceState.SHOWN_TABSWITCHER)) 
        mLayoutManager.showLayout(LayoutType.BROWSING, true);
        return true;
    

    final WebContents webContents = currentTab.getWebContents();
    if (webContents != null) 
        RenderFrameHost focusedFrame = webContents.getFocusedFrame();
        if (focusedFrame != null && focusedFrame.signalCloseWatcherIfActive()) return true;
    

    if (getToolbarManager().back()) return true;

    // If the current tab url is HELP_URL, then the back button should close the tab to
    // get back to the previous state. The reason for startsWith check is that the
    // actual redirected URL is a different system language based help url.
    final @TabLaunchType int type = currentTab.getLaunchType();
    final boolean helpUrl = currentTab.getUrl().getSpec().startsWith(HELP_URL_PREFIX);
    if (type == TabLaunchType.FROM_CHROME_UI && helpUrl) 
        getCurrentTabModel().closeTab(currentTab);
        return true;
    

    // If we aren't in the overview mode, we handle the Tab with launchType
    // TabLaunchType.FROM_START_SURFACE or has "OpenedFromStart" property.
    if (!mOverviewModeController.overviewVisible()
            && (type == TabLaunchType.FROM_START_SURFACE
                    || StartSurfaceUserData.isOpenedFromStart(currentTab))) 
        if (StartSurfaceUserData.getKeepTab(currentTab)
                || StartSurfaceUserData.isOpenedFromStart(currentTab)) 
            // If the current tab is created from the start surface with the keepTab property,
            // shows the Start surface non-incognito homepage to prevent a loop between the
            // current tab and previous overview mode. Once in the Start surface, it will close
            // Chrome if back button is tapped again.
            if (currentTab.isIncognito()) 
                if (!currentTab.isClosing()) 
                    mTabModelSelector.getModel(true).closeTab(currentTab);
                
                mTabModelSelector.selectModel(/*incognito=*/false);
            
            showOverview(StartSurfaceState.SHOWING_HOMEPAGE);
            if (type == TabLaunchType.FROM_LONGPRESS_BACKGROUND
                    && !StartSurfaceUserData.getKeepTab(currentTab)) 
                getCurrentTabModel().closeTab(currentTab);
            
         else 
            // Otherwise, clicking the back button should close the tab and go back to the
            // previous overview mode.
            showOverview(StartSurfaceState.SHOWING_PREVIOUS);
            if (!currentTab.isClosing()) 
                getCurrentTabModel().closeTab(currentTab);
            
        
        return true;
    

    final boolean shouldCloseTab = true;

    // Minimize the app if either:
    // - we decided not to close the tab
    // - we decided to close the tab, but it was opened by an external app, so we will go
    //   exit Chrome on top of closing the tab
    final boolean minimizeApp = false;
    if (minimizeApp) 
        if (shouldCloseTab) 
            sendToBackground(currentTab);
            return true;
         else 
            sendToBackground(null);
            return true;
        
     else if (shouldCloseTab) 
       if (getTabsCount() > 1) 
            if (webContents != null) webContents.dispatchBeforeUnload(false);
            return true;
        
        if (mExitApp) 
            if (webContents != null) webContents.dispatchBeforeUnload(false);
            return false;
        
        Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
        mExitApp = true;
        new Timer().schedule(new TimerTask() 
            @Override
            public void run() 
                mExitApp = false;
            
        , 2000);
        return true;
    

    assert false : "The back button should have already been handled by this point";
    return false;

9.默认不加载新标签页

源码路径:/chromium/src/components/embedder_support/android/java/src/org/chromium/components/embedder_support/util/UrlConstants.java

把下面两个参数,改成你想要显示的网页地址就可以了:

public static final String NTP_URL = "chrome-native://newtab/";

public static final String NTP_NON_NATIVE_URL = "chrome://newtab/";

10.编译脚本

最后在附上args.gn编译脚本:

target_os = "android"
target_cpu = "arm"
# Release版本
is_debug = false
# 不允许远程调试
enable_remoting = false
# 使用Chrome官方的编译优化建议
is_official_build = true
# 分散链接编译
is_component_build = false
# 不使用官方API秘钥
use_official_google_api_keys = false
enable_resource_whitelist_generation = false
# 关闭支持NACL,这是一种Chrome插件,因为安全性,稳定性存在问题,已经很少使用了
enable_nacl = false
# 删除内核层支持调试的符号文件
remove_webcore_debug_symbols = false
# 支持H264编码
proprietary_codecs = true
# 解码器
ffmpeg_branding = "Chrome"

# 解决不能播放MP4格式的视频问题
use_openh264 = true 
chrome_pgo_phase = 0 
full_wpo_on_official = true
rtc_initialize_ffmpeg = true
rtc_use_h264  = true

11.写在最后

到这里,Chromium源码的UI定制就完成了,有问题可以给我留言评论,谢谢。

以上是关于Chromium内核浏览器编译记UI定制的主要内容,如果未能解决你的问题,请参考以下文章

Chromium内核浏览器编译记UI定制

Chromium内核浏览器编译记踩坑实录

Chromium内核浏览器编译记踩坑实录

Chromium内核浏览器编译记采坑实录

Chromium和cef的区别

教程 | 如何下载chrome系列浏览器插件的crx文件