Chromium 在WebContents中添加自定义数据c++

为了能在WebContents中添加自定义数据先看下几个关键类的介绍。

一、WebContents 介绍:

  WebContents是content模块核心,是呈现 Web 内容(通常为 HTML)位于矩形区域中。

最直观的是一个浏览器标签对应一个WebContents,里面加载一个网页等。

二、看下WebContents定义:

content\public\browser\web_contents.h

{

WebContents具体实现在:

content\browser\web_contents\web_contents_impl.cc

content\browser\web_contents\web_contents_impl.h

}

WebContents继承自base::SupportsUserData

// WebContents is the core class in content/. A WebContents renders web content
// (usually HTML) in a rectangular area.
//
// Instantiating one is simple:
//   std::unique_ptr<content::WebContents> web_contents(
//       content::WebContents::Create(
//           content::WebContents::CreateParams(browser_context)));
//   gfx::NativeView view = web_contents->GetNativeView();
//   // |view| is an HWND, NSView*, etc.; insert it into the view hierarchy
//   // wherever it needs to go.
//
// That's it; go to your kitchen, grab a scone, and chill. WebContents will do
// all the multi-process stuff behind the scenes. More details are at
// https://www.chromium.org/developers/design-documents/multi-process-architecture
// .
//
// The owner of `std::unique_ptr<content::WebContents> web_contents` is
// responsible for ensuring that `web_contents` are destroyed (e.g. closed)
// *before* the corresponding `browser_context` is destroyed.
//
// Each WebContents has a `NavigationController`, which can be obtained from
// `GetController()`, and is used to load URLs into the WebContents, navigate
// it backwards/forwards, etc.
// See navigation_controller.h for more details.
class WebContents : public PageNavigator,public base::SupportsUserData {// Do not remove this macro!// The macro is maintained by the memory safety team.ADVANCED_MEMORY_SAFETY_CHECKS();public:struct CONTENT_EXPORT CreateParams {explicit CreateParams(BrowserContext* context,base::Location creator_location = base::Location::Current());CreateParams(BrowserContext* context,scoped_refptr<SiteInstance> site,base::Location creator_location = base::Location::Current());CreateParams(const CreateParams& other);~CreateParams();
..................................
};

三、base::SupportsUserData定义:

  base\supports_user_data.h

注意:看下这几个方法:

  Data* GetUserData(const void* key) const;
  [[nodiscard]] std::unique_ptr<Data> TakeUserData(const void* key);
  void SetUserData(const void* key, std::unique_ptr<Data> data);
  void RemoveUserData(const void* key);

namespace base {// This is a helper for classes that want to allow users to stash random data by
// key. At destruction all the objects will be destructed.
class BASE_EXPORT SupportsUserData {public:SupportsUserData();SupportsUserData(SupportsUserData&&);SupportsUserData& operator=(SupportsUserData&&);SupportsUserData(const SupportsUserData&) = delete;SupportsUserData& operator=(const SupportsUserData&) = delete;// Derive from this class and add your own data members to associate extra// information with this object. Alternatively, add this as a public base// class to any class with a virtual destructor.class BASE_EXPORT Data {public:virtual ~Data() = default;// Returns a copy of |this|; null if copy is not supported.virtual std::unique_ptr<Data> Clone();};// The user data allows the clients to associate data with this object.// |key| must not be null--that value is too vulnerable for collision.// NOTE: SetUserData() with an empty unique_ptr behaves the same as// RemoveUserData().Data* GetUserData(const void* key) const;[[nodiscard]] std::unique_ptr<Data> TakeUserData(const void* key);void SetUserData(const void* key, std::unique_ptr<Data> data);void RemoveUserData(const void* key);// Adds all data from |other|, that is clonable, to |this|. That is, this// iterates over the data in |other|, and any data that returns non-null from// Clone() is added to |this|.void CloneDataFrom(const SupportsUserData& other);// SupportsUserData is not thread-safe, and on debug build will assert it is// only used on one execution sequence. Calling this method allows the caller// to hand the SupportsUserData instance across execution sequences. Use only// if you are taking full control of the synchronization of that hand over.void DetachFromSequence();protected:virtual ~SupportsUserData();// Clear all user data from this object. This can be used if the subclass// needs to provide reset functionality.void ClearAllUserData();private:// Externally-defined data accessible by key.absl::flat_hash_map<const void*, std::unique_ptr<Data>> user_data_;bool in_destructor_ = false;// Guards usage of |user_data_|SEQUENCE_CHECKER(sequence_checker_);
};// Adapter class that releases a refcounted object when the
// SupportsUserData::Data object is deleted.
template <typename T>
class UserDataAdapter : public SupportsUserData::Data {public:static T* Get(const SupportsUserData* supports_user_data, const void* key) {UserDataAdapter* data =static_cast<UserDataAdapter*>(supports_user_data->GetUserData(key));return data ? static_cast<T*>(data->object_.get()) : nullptr;}explicit UserDataAdapter(T* object) : object_(object) {}UserDataAdapter(const UserDataAdapter&) = delete;UserDataAdapter& operator=(const UserDataAdapter&) = delete;~UserDataAdapter() override = default;T* release() { return object_.release(); }private:scoped_refptr<T> const object_;
};}  // namespace base

四、在WebContents添加数据定义:

   由于WebContents继承自base::SupportsUserData,所以只需要调用

base::SupportsUserData::SetUserData 方法即可。

1、需要定义一个类AwSettingsUserData 继承自base::SupportsUserData::Data

class AwSettingsUserData : public base::SupportsUserData::Data {public://添加自己的数据private:};

2、base::SupportsUserData::SetUserData设置数据:

  web_contents->SetUserData(kAwSettingsUserDataKey,

                            std::make_unique<AwSettingsUserData>(this));

3、base::SupportsUserData::GetUserData获取数据:

 AwSettingsUserData* data = static_cast<AwSettingsUserData*>(

        web_contents->GetUserData(kAwSettingsUserDataKey));

总结:至此在WebContents添加自定义数据方法介绍完毕。

添加自定义数据主要是为了标记WebContents 可以根据此标记对标签进行特殊处理。

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

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

相关文章

公众号黑名单(资源类)仅个人备份,便于查看

公众号黑名单&#xff08;资源类&#xff09; 如标题&#xff0c;仅作为云备份供我本人参考&#xff0c;我本地还有一个备份&#xff0c;只是为了方便本人在不同设备查看。 本来有一个内容更多的列表&#xff0c;后来无意间丢失了&#xff0c;因此重建了一个表 名称原因CAE仿真…

砥砺十年风雨路,向新而行创新程丨怿星科技十周年庆典回顾

10月24日&#xff0c;是一年中的第256天&#xff0c;也是程序员节&#xff0c;同时也是怿星的生日。2014年到2024年&#xff0c;年华似水匆匆一瞥&#xff0c;多少岁月轻描淡写&#xff0c;怿星人欢聚一堂&#xff0c;共同为怿星科技的十周年庆生&#xff01; 01.回忆往昔&…

Android——横屏竖屏

系统配置变更的处理机制 为了避免横竖屏切换时重新加载界面的情况&#xff0c;Android设计了一中配置变更机制&#xff0c;在指定的环境配置发生变更之时&#xff0c;无需重启活动页面&#xff0c;只需执行特定的变更行为。该机制的视线过程分为两步&#xff1a; 修改 Androi…

mysql上课总结(5)(MySQL的完整性约束(详细介绍))

目录 一、完整性约束。 &#xff08;1&#xff09;概念与目的。 <1>概念。 <2>目的。 &#xff08;2&#xff09;各个约束的详细&#xff08;表格&#xff09; &#xff08;3&#xff09;各个约束的简要总结。 <1>主键约束。 <2>唯一约束。 <3>非…

《GBDT 算法的原理推导》 11-13初始化模型 公式解析

本文是将文章《GBDT 算法的原理推导》中的公式单独拿出来做一个详细的解析&#xff0c;便于初学者更好的理解。 公式(11-13)是GBDT算法的第一步&#xff0c;它描述了如何初始化模型。公式如下&#xff1a; f 0 ( x ) arg ⁡ min ⁡ c ∑ i 1 N L ( y i , c ) f_0(x) \arg \m…

C++学习笔记----9、发现继承的技巧(七)---- 转换(1)

我们来看下将一种数据类型转换为另一种数据类型中的一些令人迷惑的地方。 C提供了五种类型转换&#xff1a;const_cast()&#xff0c;static_cast()&#xff0c;reinterpret_cast()&#xff0c;dynamic_cast()与std::bit_cast()。对于static_cast对于继承还有一些内容要讨论。现…

msys2更换国内源(多个文件(不是3个文件的版本!))

msys2更换国内源 起因排查答案如下mirrorlist.mingw64mirrorlist.ucrt64mirrorlist.mingw32mirrorlist.mingwmirrorlist.clang64mirrorlist.clang32mirrorlist.msys 不想看经过的直接跳到答案 起因 查了很多个教程大部分都是【打开MSYS2软件内的\etc\pacman.d\ 中3个文件&…

使用 MMDetection 实现 Pascal VOC 数据集的目标检测项目练习(二) ubuntu的下载安装

首先&#xff0c;Linux系统是人工智能和深度学习首选系统。原因如下: 开放性和自由度&#xff1a;Linux 是一个开源操作系统&#xff0c;允许开发者自由修改和分发代码。这在开发和研究阶段非常有用&#xff0c;因为开发者可以轻松地访问和修改底层代码。社区支持&#xff1a;…

TCP Analysis Flags 之 TCP Keep-Alive

前言 默认情况下&#xff0c;Wireshark 的 TCP 解析器会跟踪每个 TCP 会话的状态&#xff0c;并在检测到问题或潜在问题时提供额外的信息。在第一次打开捕获文件时&#xff0c;会对每个 TCP 数据包进行一次分析&#xff0c;数据包按照它们在数据包列表中出现的顺序进行处理。可…

Android的SQLiteOpenHelper类 笔记241027

SQLiteOpenHelper SQLiteOpenHelper是Android开发中用于管理SQLite数据库的一个非常重要的工具类。以下是对SQLiteOpenHelper的详细介绍&#xff1a; 一、基本概念 SQLiteOpenHelper是一个抽象类&#xff0c;它主要用于管理数据库的创建和版本管理。通过继承这个类&#xff…

3.2 页面异常-2

系列文章目录 文章目录 系列文章目录IoPageRead() IoPageRead() /** implemented*/ NTSTATUS NTAPI IoPageRead(IN PFILE_OBJECT FileObject,IN PMDL Mdl,IN PLARGE_INTEGER Offset,IN PKEVENT Event,IN PIO_STATUS_BLOCK StatusBlock) {PIRP Irp;PIO_STACK_LOCATION StackPtr;…

关于数学建模的一些介绍

为了更好了解世界&#xff0c;我们可以通过数学来描述许多特定的现象&#xff0c;而数学模型就是现实世界的理想化&#xff0c;不过它永远不能完全精确地表示现实世界。 在这篇文章中&#xff0c;我将介绍一些数学建模的基本概念以及相应的基础知识&#xff0c;而关于更具体的…

CSRA的LINUX操作系统24年11月2日下午上课笔记

压缩和解压缩&#xff1a;zip 、gzip、bz、xz # zip 压缩 # 压缩文件夹 # 解压缩 # unzip -v 查看压缩包中的内容 # bzip2 dir1/* :将dir1中的所有文件压缩 # gzip # 压缩文件夹 # 解压缩 tar 归档命令&#xff1a; # 创建tar包 tar -c*f # 释放tar包 tar -xf[c] # c …

Java JUC(四) 自定义线程池实现与原理分析

目录 一. 阻塞队列 BlockingQue 二. 拒绝策略 RejectPolicy 三. 线程池 ThreadPool 四. 模拟运行 在 Java基础&#xff08;二&#xff09; 多线程编程 中&#xff0c;我们简单介绍了线程池 ThreadPoolExecutor 的核心概念与基本使用。在本文中&#xff0c;我们将基于前面学…

五、SpringBoot3实战(1)

一、SpringBoot3介绍 1.1 SpringBoot3简介 SpringBoot版本&#xff1a;3.0.5 https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started.html#getting-started.introducing-spring-boot 到目前为止&#xff0c;你已经学习了多种配置Spring程序的方式…

施耐德M310PLC通讯之ModbusTCP(一)

这是另一个专题----施耐德国产化PLC(M310)的通讯篇 本节是ModbusTcp通讯 测试对象: M310plc与M241PLC 通讯协议: ModbusTcp 主站:M310PLC 从站:M241PLC 1.M310端: 1.1 新建工程(M310采用EcoStruxure Motion Expert 软件) 新建工程,这里不区分PLC型号的,只要是M310即…

使用ssh-key免密登录服务器或git代码仓库网站

现在常用的git代码仓库网站&#xff08;简称git站&#xff09;有: gitee、coding、github、gitlab、bitbucket等。 你在git站注册了账号后&#xff0c;可以进入账号设置里面添加ssh-key&#xff0c;从而实现你本地机器免密clone、pull、push你在该git网站的仓库&#xff08;即项…

C#程序开发,使用OpenHardwareMonitor测量 Windows 运行状态

1、数据结构 using System;namespace PcMonitor.Protocol {[Serializable]public class MachineInfo{public CpuInfo Cpu;public MemoryInfo Memory;public GpuInfo Gpu;public MachineInfo(CpuInfo cpu, MemoryInfo memory, GpuInfo gpu){Cpu cpu;Memory memory;Gpu gpu;}…

三、k8s快速入门之Kubectl 命令基础操作

⭐️创建Pod [rootmaster ~]# kubectl run nginx --imageharbor.tanc.com/library/ngix:latest kubectl run --generatordeployment/apps.v1 is DEPRECATED and will be rmoved in a future version. Use kubectl run --generatorrun-pod/v1 or kbectl create instead. deplo…

Spring Boot 集成阿里云直播点播

在当今数字化时代&#xff0c;视频直播和点播服务已经成为许多应用的核心功能。阿里云提供了强大的直播和点播服务&#xff0c;能够满足各种规模的应用需求。而 Spring Boot 作为一种流行的 Java 开发框架&#xff0c;能够快速构建高效的应用程序。本文将详细介绍如何在 Spring…