Chromium 关闭 Google Chrome 后继续运行后台应用功能分析c++

此功能允许关闭 Google Chrome 后继续运行后台,控制此功能的开关是

// Set to true if background mode is enabled on this browser.

//更改此值可以修改默认开启关闭

inline constexpr char kBackgroundModeEnabled[] = "background_mode.enabled";

chrome\browser\background\background_mode_manager.cc

// staticvoid BackgroundModeManager::RegisterPrefs(PrefRegistrySimple* registry) {//更改此值可以修改默认开启关闭registry->RegisterBooleanPref(prefs::kBackgroundModeEnabled, true);}

1、此功能前端代码

chrome\browser\resources\settings\system_page\system_page.html

<if expr="not is_macosx and not chromeos_lacros"><settings-toggle-buttonpref="{{prefs.background_mode.enabled}}" //监听prefs变化label="$i18n{backgroundAppsLabel}"></settings-toggle-button><div class="hr"></div>
</if>

2、c++对应kBackgroundModeEnabled监听实现代码

chrome\browser\background\background_mode_manager.cc

///
//  BackgroundModeManager, public
BackgroundModeManager::BackgroundModeManager(const base::CommandLine& command_line,ProfileAttributesStorage* profile_storage): profile_storage_(profile_storage), task_runner_(CreateTaskRunner()) {// We should never start up if there is no browser process or if we are// currently quitting.CHECK(g_browser_process);CHECK(!browser_shutdown::IsTryingToQuit());// Add self as an observer for the ProfileAttributesStorage so we know when// profiles are deleted and their names change.// This observer is never unregistered because the BackgroundModeManager// outlives the profile storage.profile_storage_->AddObserver(this);// Listen for the background mode preference changing.if (g_browser_process->local_state()) {  // Skip for unit testspref_registrar_.Init(g_browser_process->local_state());pref_registrar_.Add(prefs::kBackgroundModeEnabled, //监听prefs变化,控制功能开启关闭base::BindRepeating(&BackgroundModeManager::OnBackgroundModeEnabledPrefChanged,base::Unretained(this)));}}

3、允许前端更改prefs值需要加白在

chrome\browser\extensions\api\settings_private\prefs_util.cc

const PrefsUtil::TypedPrefMap& PrefsUtil::GetAllowlistedKeys(){// System settings.(*s_allowlist)[::prefs::kBackgroundModeEnabled] =settings_api::PrefType::kBoolean;}

=========================================================================

注意:以下是前端更改prefs过程

     前端通过chrome.settingsPrivate.setPref接口 通过mojom发送给主进程chrome\browser\extensions\api\settings_private\settings_private_api.h接口进行设置。

c++如何定义一个chrome.settingsPrivate接口给前端调用呢?

1、接口定义chrome\common\extensions\api\settings_private.idl

// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.// Use the <code>chrome.settingsPrivate</code> API to get or set preferences
// from the settings UI. Access is restricted to a set of allowed user facing
// preferences.
namespace settingsPrivate {enum PrefType { BOOLEAN, NUMBER, STRING, URL, LIST, DICTIONARY };enum ControlledBy {DEVICE_POLICY,USER_POLICY,OWNER,PRIMARY_USER,EXTENSION,// Preferences are controlled by the parent of the child user.PARENT,// Preferences are controlled neither by parent nor the child user.// Preference values are hard-coded values and can not be changed.CHILD_RESTRICTION};enum Enforcement {// Value cannot be changed by user.ENFORCED,// Value can be changed, but the administrator recommends a default.RECOMMENDED,// Value is protected by a code that only parents can access. The logic to// require the code is NOT automatically added to a preference using this// enforcement. This is only used to display the correct pref indicator.PARENT_SUPERVISED};dictionary PrefObject {// The key for the pref.DOMString key;// The type of the pref (e.g., boolean, string, etc.).PrefType type;// The current value of the pref.any? value;// The policy source of the pref; an undefined value means there is no// policy.ControlledBy? controlledBy;// The owner name if controlledBy == OWNER.// The primary user name if controlledBy == PRIMARY_USER.// The extension name if controlledBy == EXTENSION.DOMString? controlledByName;// The policy enforcement of the pref; must be specified if controlledBy is// also present.Enforcement? enforcement;// The recommended value if enforcement == RECOMMENDED.any? recommendedValue;// If enforcement == ENFORCED this optionally specifies preference values// that are still available for selection by the user. If set, must contain// at least 2 distinct values, as must contain |value| and// |recommendedValue| (if present).any[]? userSelectableValues;// If true, user control of the preference is disabled for reasons unrelated// to controlledBy (e.g. no signed-in profile is present). A false value is// a no-op.boolean? userControlDisabled;// The extension ID if controlledBy == EXTENSION.DOMString? extensionId;// Whether the controlling extension can be disabled if controlledBy ==// EXTENSION.boolean? extensionCanBeDisabled;};callback OnPrefSetCallback = void (boolean success);callback GetAllPrefsCallback = void (PrefObject[] prefs);callback GetPrefCallback = void (PrefObject pref);callback GetDefaultZoomCallback = void (double zoom);callback SetDefaultZoomCallback = void (boolean success);interface Functions {// Sets a pref value.// |name|: The name of the pref.// |value|: The new value of the pref.// |pageId|: An optional user metrics identifier.// |callback|: The callback for whether the pref was set or not.[supportsPromises] static void setPref(DOMString name,any value,optional DOMString pageId,optional OnPrefSetCallback callback);// Gets an array of all the prefs.[supportsPromises] static void getAllPrefs(GetAllPrefsCallback callback);// Gets the value of a specific pref.[supportsPromises] static void getPref(DOMString name,GetPrefCallback callback);// Gets the default page zoom factor. Possible values are currently between// 0.25 and 5. For a full list, see zoom::kPresetZoomFactors.[supportsPromises] static void getDefaultZoom(GetDefaultZoomCallback callback);// Sets the page zoom factor. Must be less than 0.001 different than a value// in zoom::kPresetZoomFactors.[supportsPromises] static void setDefaultZoom(double zoom,optional SetDefaultZoomCallback callback);};interface Events {// Fired when a set of prefs has changed.//// |prefs| The prefs that changed.static void onPrefsChanged(PrefObject[] prefs);};
};

2、c++settingsPrivate接口实现

chrome\browser\extensions\api\settings_private\settings_private_api.h

// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.#ifndef CHROME_BROWSER_EXTENSIONS_API_SETTINGS_PRIVATE_SETTINGS_PRIVATE_API_H_
#define CHROME_BROWSER_EXTENSIONS_API_SETTINGS_PRIVATE_SETTINGS_PRIVATE_API_H_#include "extensions/browser/extension_function.h"namespace extensions {// Implements the chrome.settingsPrivate.setPref method.
class SettingsPrivateSetPrefFunction : public ExtensionFunction {public:SettingsPrivateSetPrefFunction() {}SettingsPrivateSetPrefFunction(const SettingsPrivateSetPrefFunction&) =delete;SettingsPrivateSetPrefFunction& operator=(const SettingsPrivateSetPrefFunction&) = delete;DECLARE_EXTENSION_FUNCTION("settingsPrivate.setPref", SETTINGSPRIVATE_SETPREF)protected:~SettingsPrivateSetPrefFunction() override;// ExtensionFunction overrides.ResponseAction Run() override;
};// Implements the chrome.settingsPrivate.getAllPrefs method.
class SettingsPrivateGetAllPrefsFunction : public ExtensionFunction {public:SettingsPrivateGetAllPrefsFunction() {}SettingsPrivateGetAllPrefsFunction(const SettingsPrivateGetAllPrefsFunction&) = delete;SettingsPrivateGetAllPrefsFunction& operator=(const SettingsPrivateGetAllPrefsFunction&) = delete;DECLARE_EXTENSION_FUNCTION("settingsPrivate.getAllPrefs",SETTINGSPRIVATE_GETALLPREFS)protected:~SettingsPrivateGetAllPrefsFunction() override;// ExtensionFunction overrides.ResponseAction Run() override;
};// Implements the chrome.settingsPrivate.getPref method.
class SettingsPrivateGetPrefFunction : public ExtensionFunction {public:SettingsPrivateGetPrefFunction() {}SettingsPrivateGetPrefFunction(const SettingsPrivateGetPrefFunction&) =delete;SettingsPrivateGetPrefFunction& operator=(const SettingsPrivateGetPrefFunction&) = delete;DECLARE_EXTENSION_FUNCTION("settingsPrivate.getPref", SETTINGSPRIVATE_GETPREF)protected:~SettingsPrivateGetPrefFunction() override;// ExtensionFunction overrides.ResponseAction Run() override;
};// Implements the chrome.settingsPrivate.getDefaultZoom method.
class SettingsPrivateGetDefaultZoomFunction : public ExtensionFunction {public:SettingsPrivateGetDefaultZoomFunction() {}SettingsPrivateGetDefaultZoomFunction(const SettingsPrivateGetDefaultZoomFunction&) = delete;SettingsPrivateGetDefaultZoomFunction& operator=(const SettingsPrivateGetDefaultZoomFunction&) = delete;DECLARE_EXTENSION_FUNCTION("settingsPrivate.getDefaultZoom",SETTINGSPRIVATE_GETDEFAULTZOOMFUNCTION)protected:~SettingsPrivateGetDefaultZoomFunction() override;// ExtensionFunction overrides.ResponseAction Run() override;
};// Implements the chrome.settingsPrivate.setDefaultZoom method.
class SettingsPrivateSetDefaultZoomFunction : public ExtensionFunction {public:SettingsPrivateSetDefaultZoomFunction() {}SettingsPrivateSetDefaultZoomFunction(const SettingsPrivateSetDefaultZoomFunction&) = delete;SettingsPrivateSetDefaultZoomFunction& operator=(const SettingsPrivateSetDefaultZoomFunction&) = delete;DECLARE_EXTENSION_FUNCTION("settingsPrivate.setDefaultZoom",SETTINGSPRIVATE_SETDEFAULTZOOMFUNCTION)protected:~SettingsPrivateSetDefaultZoomFunction() override;// ExtensionFunction overrides.ResponseAction Run() override;
};}  // namespace extensions#endif  // CHROME_BROWSER_EXTENSIONS_API_SETTINGS_PRIVATE_SETTINGS_PRIVATE_API_H_

3、在extensions\browser\extension_function_histogram_value.h中定义函数ID

  注意extensions\browser\extension_function_histogram_value.h中与chrome\browser\extensions\api\settings_private\settings_private_api.h中类名的对应关系,否则关联失败。//SETTINGSPRIVATE_SETPREF 应是类名(SettingsPrivateSetPrefFunction)去掉Function之后大写!!!!!

4、该类注册在out\Debug\gen\chrome\browser\extensions\api\generated_api_registration.cc

   [自动生成的代码,不需要手动添加]

namespace extensions {
namespace api {// static
void ChromeGeneratedFunctionRegistry::RegisterAll(ExtensionFunctionRegistry* registry)
{   {&NewExtensionFunction<SettingsPrivateSetPrefFunction>,SettingsPrivateSetPrefFunction::static_function_name(),SettingsPrivateSetPrefFunction::static_histogram_value(),},........................................
}

5、注册api地方 chrome\browser\extensions\chrome_extensions_browser_api_provider.cc

namespace extensions {ChromeExtensionsBrowserAPIProvider::ChromeExtensionsBrowserAPIProvider() =default;
ChromeExtensionsBrowserAPIProvider::~ChromeExtensionsBrowserAPIProvider() =default;void ChromeExtensionsBrowserAPIProvider::RegisterExtensionFunctions(ExtensionFunctionRegistry* registry) {// Preferences.registry->RegisterFunction<GetPreferenceFunction>();registry->RegisterFunction<SetPreferenceFunction>();registry->RegisterFunction<ClearPreferenceFunction>();// Generated APIs from Chrome.api::ChromeGeneratedFunctionRegistry::RegisterAll(registry);
}}  // namespace extensions

6、前端获取prefs实现

base::Value::List SettingsPrivateDelegate::GetAllPrefs() {base::Value::List prefs;//GetAllowlistedKeys()定义在//chrome\browser\extensions\api\settings_private\prefs_util.cc//所以要给前端添加新的prefs监听 要在此处加白,否则前端获取不到该prefs值const TypedPrefMap& keys = prefs_util_->GetAllowlistedKeys();for (const auto& it : keys) {if (absl::optional<base::Value::Dict> pref = GetPref(it.first); pref) {prefs.Append(std::move(*pref));}}return prefs;
}

7、最后将settings_private.idl 放到chrome\common\extensions\api\api_sources.gni 

中,然后进行编译即可。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/55926.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

案例分享—国外优秀UI设计作品赏析

深色UI界面的优点众多&#xff0c;首先体现在视觉舒适度上。深色背景能减少屏幕高亮面积&#xff0c;降低眼部压力&#xff0c;尤其在夜间或光线不足的环境下&#xff0c;深色模式能显著缓解眼睛疲劳&#xff0c;提供更舒适的使用体验。 深色UI界面在设计上更具高端感和优雅氛围…

用Raspberry Pi Imager重装树莓派系统

今天删东西的时候&#xff0c;无意中把系统文件给remove了&#xff0c;结果树莓派无法正常启动&#xff0c;只能重新安装。 用DiskGenius工具将SD卡彻底清空&#xff0c;并将boot分区和文件分区合并为一&#xff0c;之后再对这个新分区进行了格式化。接下来就是烧录镜像了。以…

自动化测试 | 窗口截图

driver.get_screenshot_as_file 是 Selenium WebDriver 的一个方法&#xff0c;它允许你将当前浏览器窗口&#xff08;或标签页&#xff09;的截图保存为文件。这个方法对于自动化测试中的截图验证非常有用&#xff0c;因为它可以帮助你捕获测试执行过程中的页面状态。 以下是…

布隆过滤器(Bloom Filter)详解

一、引言 在处理大量数据的场景中&#xff0c;我们经常会遇到判断一个元素是否在某个集合中的问题。传统的方法可能是使用 HashMap 等集合将数据保存起来&#xff0c;然后进行比较确定&#xff0c;但在元素很多的情况下&#xff0c;这种方式会非常浪费空间&#xff0c;检索速度…

Map的实现类:TreeMap

1.存储结构&#xff1a;红黑树 2.实现了SortedMap接口&#xff08;是Map的子接口&#xff09;&#xff0c;可以对key自动排序。 3.实例代码&#xff1a;Student类和Demo03 如果出现类转换异常 参考【TreeSet&#xff08;红黑树&#xff09;】 package com.map;import java…

使用Git生成SSH密钥教程(附Git常用命令)

一、为什么使用SSH&#xff1f; 使用 Git 的 SSH&#xff08;安全外壳协议&#xff09;主要有以下几个原因&#xff1a;1. 安全性&#xff1a;SSH 是一种加密的网络协议&#xff0c;用于在网络中安全地运行网络服务。使用 SSH&#xff0c;所有传输的数据都会被加密&#xff0c…

Lory: 推进大型语言模型训练的新篇章

人工智能咨询培训老师叶梓 转载标明出处 随着模型规模的增长&#xff0c;如何有效训练并利用这些模型成为了一个挑战。陈丹琦团队一项新的研究提出了一种创新的预训练方法——Lory&#xff0c;旨在解决大模型在混合专家&#xff08;MoE&#xff09;架构中的可微分性和计算效率…

主机加固的关键要素:服务器防病毒

在数字化浪潮中&#xff0c;网络安全已成为企业不可忽视的一环。尤其是安全运维人员&#xff0c;他们肩负着保护企业数据不受侵害的重任。MCK主机加固解决方案&#xff0c;正是为了应对这一挑战而生。 网络安全的严峻现实 不久前&#xff0c;一家知名企业因勒索病毒攻击而被迫…

2024 kali虚拟机安装教程,分两大步骤,图文讲解(1)

第二步链接&#xff1a; 2024 kali虚拟机安装教程&#xff0c;分两大步骤&#xff0c;图文讲解&#xff08;2&#xff09;-CSDN博客 准备工作 1.kali的iso镜像文件 2.VMware Workstation Pro 虚拟机软件 正式开始 1.创建新的虚拟机&#xff0c;勾选自定义&#xff08;高级…

ssm基于SSM框架的餐馆点餐系统的设计+VUE

系统包含&#xff1a;源码论文 所用技术&#xff1a;SpringBootVueSSMMybatisMysql 免费提供给大家参考或者学习&#xff0c;获取源码请私聊我 需要定制请私聊 目 录 摘要 I Abstract II 1绪论 1 1.1研究背景与意义 1 1.1.1研究背景 1 1.1.2研究意义 1 1.2国内外研究…

【AGC005D】~K Perm Counting(计数抽象成图)

容斥原理。 求出f(m) &#xff0c;f(m)指代至少有m个位置不合法的方案数。 怎么求&#xff1f; 注意到位置为id&#xff0c;权值为v ,不合法的情况&#xff0c;当且仅当 v idk或 v id-k 因此&#xff0c;我们把每一个位置和权值抽象成点 &#xff0c;不合法的情况之间连一…

Docker容器简介及部署方法

1.1 Docker简介 Docker之父Solomon Hykes&#xff1a;Docker就好比传统的货运集装箱 2008 年LXC(LinuX Contiainer)发布&#xff0c;但是没有行业标准&#xff0c;兼容性非常差 docker2013年首次发布&#xff0c;由Docker, Inc开发 1.1.1什么是Docker Docker是管理容器的引…

数据结构-LRU缓存(C语言实现)

遇到困难&#xff0c;不必慌张&#xff0c;正是成长的时候&#xff0c;耐心一点&#xff01; 目录 前言一、题目介绍二、实现过程2.1 实现原理2.2 实现思路2.2.1 双向链表2.2.2 散列表 2.3 代码实现2.3.1 结构定义2.3.2 双向链表操作实现2.3.3 实现散列表的操作2.3.4 内存释放代…

Java后端面试----某团一面

美团一面 1.介绍一下你的第一个项目 这个就不多说了&#xff0c;主要是根据自己的简历上面的项目进行一个简短的概括使用的技术栈和什么背景解决了什么问题等等。 2.线程安全的类有哪些&#xff0c;平时有使用过哪些&#xff0c;主要解决什么问题 在Java中线程安全的类比如…

vue使用table实现动态数据报表(行合并)

<template><div class"previewTable"><h2>***项目研发数据报告</h2><table id"previewTable" width"100%"><tr><th>项目名称</th><td colspan"6">{{ resultData.proName }}<…

【D3.js in Action 3 精译_030】3.5 给 D3 条形图加注图表标签(下):Krisztina Szűcs 人物专访 + 3.6 本章小结

当前内容所在位置&#xff08;可进入专栏查看其他译好的章节内容&#xff09; 第一部分 D3.js 基础知识 第一章 D3.js 简介&#xff08;已完结&#xff09; 1.1 何为 D3.js&#xff1f;1.2 D3 生态系统——入门须知1.3 数据可视化最佳实践&#xff08;上&#xff09;1.3 数据可…

一键将表格嵌入ppt作为附件!2个做ppt必知的技巧分享!

怎样把表格作为附件放入ppt&#xff1f; 众所周知&#xff0c;微软推出的Office套件包含了Powerpoint和Excel这两款软件&#xff0c;如果想在Powerpoint中插入表格&#xff0c;且表格数据量比较大&#xff0c;此时最好的呈现方式&#xff0c;是在Excel中来展示这些数据&#x…

【Unity学习笔记】解决疑似升级Win11或使用Unity6导致Unity旧版本无法打开的问题

【Unity学习笔记】解决疑似升级Win11或使用Unity6导致Unity旧版本无法打开的问题 一句话省流&#xff1a; 确保项目地址没有任何中文&#xff0c;重新申请个许可证&#xff0c;然后该咋就咋&#xff0c;完事。 ——————————————————————————————…

华为云应用侧Android测试APP

05.华为云应用侧Android测试APP 本APP在填写或修改部分参数后能够完成token获取&#xff0c;影子消息读取&#xff0c;命令下发。APP共包含三个界面&#xff1a;主界面获取token、影子消息获取界面、命令下发界面。 实现过程参见&#xff1a;华为云应用侧Android Studio开发-…

企业如何制定适合自己的专利布局策略

在竞争激烈的市场环境中&#xff0c;专利布局对于企业的发展和竞争优势的建立至关重要。以下将分要点解析企业如何制定适合自己的专利布局策略。 1、明确企业的发展战略和市场定位 企业首先需要深入了解自身的长期发展规划和短期业务目标。明确是要通过技术创新来开拓新市场&am…