Chromium 中chrome.history扩展接口c++实现

news2024/10/9 22:02:57

一、前端 chrome.history定义

使用 chrome.history API 与浏览器的已访问网页的记录进行交互。您可以在浏览器的历史记录中添加、移除和查询网址。如需使用您自己的版本替换历史记录页面,请参阅覆盖网页。

更多参考:chrome.history  |  API  |  Chrome for Developers (google.cn)

示例

若要试用此 API,请安装 chrome-extension-samples 中的 history API 示例 存储库

二、history接口在c++定义

   chrome\common\extensions\api\history.json

out\Debug\gen\chrome\common\extensions\api\history.cc

src\out\Debug\gen\chrome\common\extensions\api\history.h

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

[
  {
    "namespace": "history",
    "description": "Use the <code>chrome.history</code> API to interact with the browser's record of visited pages. You can add, remove, and query for URLs in the browser's history. To override the history page with your own version, see <a href='override'>Override Pages</a>.",
    "types": [
      {
        "id": "TransitionType",
        "type": "string",
        "enum": ["link", "typed", "auto_bookmark", "auto_subframe", "manual_subframe", "generated", "auto_toplevel", "form_submit", "reload", "keyword", "keyword_generated"],
        "description": "The <a href='#transition_types'>transition type</a> for this visit from its referrer."
      },
      {
        "id": "HistoryItem",
        "type": "object",
        "description": "An object encapsulating one result of a history query.",
        "properties": {
          "id": {"type": "string", "minimum": 0, "description": "The unique identifier for the item."},
          "url": {"type": "string", "optional": true, "description": "The URL navigated to by a user."},
          "title": {"type": "string", "optional": true, "description": "The title of the page when it was last loaded."},
          "lastVisitTime": {"type": "number", "optional": true, "description": "When this page was last loaded, represented in milliseconds since the epoch."},
          "visitCount": {"type": "integer", "optional": true, "description": "The number of times the user has navigated to this page."},
          "typedCount": {"type": "integer", "optional": true, "description": "The number of times the user has navigated to this page by typing in the address."}
        }
      },
      {
        "id": "VisitItem",
        "type": "object",
        "description": "An object encapsulating one visit to a URL.",
        "properties": {
          "id": {"type": "string", "minimum": 0, "description": "The unique identifier for the corresponding $(ref:history.HistoryItem)."},
          "visitId": {"type": "string", "description": "The unique identifier for this visit."},
          "visitTime": {"type": "number", "optional": true, "description": "When this visit occurred, represented in milliseconds since the epoch."},
          "referringVisitId": {"type": "string", "description": "The visit ID of the referrer."},
          "transition": {
            "$ref": "TransitionType",
            "description": "The <a href='#transition_types'>transition type</a> for this visit from its referrer."
          },
          "isLocal": { "type": "boolean", "description": "True if the visit originated on this device. False if it was synced from a different device." }
        }
      },
      {
        "id": "UrlDetails",
        "type": "object",
        "properties": {
          "url": {"type": "string", "description": "The URL for the operation. It must be in the format as returned from a call to history.search."}
        }
      }
    ],
    "functions": [
      {
        "name": "search",
        "type": "function",
        "description": "Searches the history for the last visit time of each page matching the query.",
        "parameters": [
          {
            "name": "query",
            "type": "object",
            "properties": {
              "text": {"type": "string", "description": "A free-text query to the history service.  Leave empty to retrieve all pages."},
              "startTime": {"type": "number", "optional": true, "description": "Limit results to those visited after this date, represented in milliseconds since the epoch. If not specified, this defaults to 24 hours in the past."},
              "endTime": {"type": "number", "optional": true, "description": "Limit results to those visited before this date, represented in milliseconds since the epoch."},
              "maxResults": {"type": "integer", "optional": true, "minimum": 0, "description": "The maximum number of results to retrieve.  Defaults to 100."}
            }
          }
        ],
        "returns_async": {
          "name": "callback",
          "parameters": [
            { "name": "results", "type": "array", "items": { "$ref": "HistoryItem"} }
          ]
        }
      },
      {
        "name": "getVisits",
        "type": "function",
        "description": "Retrieves information about visits to a URL.",
        "parameters": [
          {
            "name": "details",
            "$ref": "UrlDetails"
          }
        ],
        "returns_async": {
          "name": "callback",
          "parameters": [
            { "name": "results", "type": "array", "items": { "$ref": "VisitItem"} }
          ]
        }
      },
      {
        "name": "addUrl",
        "type": "function",
        "description": "Adds a URL to the history at the current time with a <a href='#transition_types'>transition type</a> of \"link\".",
        "parameters": [
          {
            "name": "details",
            "$ref": "UrlDetails"
          }
        ],
        "returns_async": {
          "name": "callback",
          "optional": true,
          "parameters": []
        }
      },
      {
        "name": "deleteUrl",
        "type": "function",
        "description": "Removes all occurrences of the given URL from the history.",
        "parameters": [
          {
            "name": "details",
            "$ref": "UrlDetails"
          }
        ],
        "returns_async": {
          "name": "callback",
          "optional": true,
          "parameters": []
        }
      },
      {
        "name": "deleteRange",
        "type": "function",
        "description": "Removes all items within the specified date range from the history.  Pages will not be removed from the history unless all visits fall within the range.",
        "parameters": [
          {
            "name": "range",
            "type": "object",
            "properties": {
              "startTime": { "type": "number", "description": "Items added to history after this date, represented in milliseconds since the epoch." },
              "endTime": { "type": "number", "description": "Items added to history before this date, represented in milliseconds since the epoch." }
            }
          }
        ],
        "returns_async": {
          "name": "callback",
          "parameters": []
        }
      },
      {
        "name": "deleteAll",
        "type": "function",
        "description": "Deletes all items from the history.",
        "parameters": [],
        "returns_async": {
          "name": "callback",
          "parameters": []
        }
      }
    ],
    "events": [
      {
        "name": "onVisited",
        "type": "function",
        "description": "Fired when a URL is visited, providing the HistoryItem data for that URL.  This event fires before the page has loaded.",
        "parameters": [
          { "name": "result", "$ref": "HistoryItem"}
        ]
      },
      {
        "name": "onVisitRemoved",
        "type": "function",
        "description": "Fired when one or more URLs are removed from the history service.  When all visits have been removed the URL is purged from history.",
        "parameters": [
          {
            "name": "removed",
            "type": "object",
            "properties": {
              "allHistory": { "type": "boolean", "description": "True if all history was removed.  If true, then urls will be empty." },
              "urls": { "type": "array", "items": { "type": "string" }, "optional": true}
            }
          }
        ]
      }
    ]
  }
]

三、history API接口定义文件:

     chrome\browser\extensions\api\bookmarks\bookmarks_api.h

     chrome\browser\extensions\api\bookmarks\bookmarks_api.cc

// Copyright 2012 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_HISTORY_HISTORY_API_H_
#define CHROME_BROWSER_EXTENSIONS_API_HISTORY_HISTORY_API_H_

#include <string>
#include <vector>

#include "base/memory/raw_ptr.h"
#include "base/scoped_observation.h"
#include "base/task/cancelable_task_tracker.h"
#include "base/values.h"
#include "chrome/common/extensions/api/history.h"
#include "components/history/core/browser/history_service.h"
#include "components/history/core/browser/history_service_observer.h"
#include "extensions/browser/browser_context_keyed_api_factory.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_function.h"

class Profile;

namespace extensions {

// Observes History service and routes the notifications as events to the
// extension system.
class HistoryEventRouter : public history::HistoryServiceObserver {
 public:
  HistoryEventRouter(Profile* profile,
                     history::HistoryService* history_service);

  HistoryEventRouter(const HistoryEventRouter&) = delete;
  HistoryEventRouter& operator=(const HistoryEventRouter&) = delete;

  ~HistoryEventRouter() override;

 private:
  // history::HistoryServiceObserver.
  void OnURLVisited(history::HistoryService* history_service,
                    const history::URLRow& url_row,
                    const history::VisitRow& new_visit) override;
  void OnURLsDeleted(history::HistoryService* history_service,
                     const history::DeletionInfo& deletion_info) override;

  void DispatchEvent(Profile* profile,
                     events::HistogramValue histogram_value,
                     const std::string& event_name,
                     base::Value::List event_args);

  raw_ptr<Profile> profile_;
  base::ScopedObservation<history::HistoryService,
                          history::HistoryServiceObserver>
      history_service_observation_{this};
};

class HistoryAPI : public BrowserContextKeyedAPI, public EventRouter::Observer {
 public:
  explicit HistoryAPI(content::BrowserContext* context);
  ~HistoryAPI() override;

  // KeyedService implementation.
  void Shutdown() override;

  // BrowserContextKeyedAPI implementation.
  static BrowserContextKeyedAPIFactory<HistoryAPI>* GetFactoryInstance();

  // EventRouter::Observer implementation.
  void OnListenerAdded(const EventListenerInfo& details) override;

 private:
  friend class BrowserContextKeyedAPIFactory<HistoryAPI>;

  raw_ptr<content::BrowserContext> browser_context_;

  // BrowserContextKeyedAPI implementation.
  static const char* service_name() {
    return "HistoryAPI";
  }
  static const bool kServiceIsNULLWhileTesting = true;

  // Created lazily upon OnListenerAdded.
  std::unique_ptr<HistoryEventRouter> history_event_router_;
};

template <>
void BrowserContextKeyedAPIFactory<HistoryAPI>::DeclareFactoryDependencies();

// Base class for history function APIs.
class HistoryFunction : public ExtensionFunction {
 protected:
  ~HistoryFunction() override {}

  bool ValidateUrl(const std::string& url_string,
                   GURL* url,
                   std::string* error);
  bool VerifyDeleteAllowed(std::string* error);
  base::Time GetTime(double ms_from_epoch);
  Profile* GetProfile() const;
};

// Base class for history funciton APIs which require async interaction with
// chrome services and the extension thread.
class HistoryFunctionWithCallback : public HistoryFunction {
 public:
  HistoryFunctionWithCallback();

 protected:
  ~HistoryFunctionWithCallback() override;

  // The task tracker for the HistoryService callbacks.
  base::CancelableTaskTracker task_tracker_;
};

class HistoryGetVisitsFunction : public HistoryFunctionWithCallback {
 public:
  DECLARE_EXTENSION_FUNCTION("history.getVisits", HISTORY_GETVISITS)

 protected:
  ~HistoryGetVisitsFunction() override {}

  // ExtensionFunction:
  ResponseAction Run() override;

  // Callback for the history function to provide results.
  void QueryComplete(history::QueryURLResult result);
};

class HistorySearchFunction : public HistoryFunctionWithCallback {
 public:
  DECLARE_EXTENSION_FUNCTION("history.search", HISTORY_SEARCH)

 protected:
  ~HistorySearchFunction() override {}

  // ExtensionFunction:
  ResponseAction Run() override;

  // Callback for the history function to provide results.
  void SearchComplete(history::QueryResults results);
};

class HistoryAddUrlFunction : public HistoryFunction {
 public:
  DECLARE_EXTENSION_FUNCTION("history.addUrl", HISTORY_ADDURL)

 protected:
  ~HistoryAddUrlFunction() override {}

  // ExtensionFunction:
  ResponseAction Run() override;
};

class HistoryDeleteAllFunction : public HistoryFunctionWithCallback {
 public:
  DECLARE_EXTENSION_FUNCTION("history.deleteAll", HISTORY_DELETEALL)

 protected:
  ~HistoryDeleteAllFunction() override {}

  // ExtensionFunction:
  ResponseAction Run() override;

  // Callback for the history service to acknowledge deletion.
  void DeleteComplete();
};


class HistoryDeleteUrlFunction : public HistoryFunction {
 public:
  DECLARE_EXTENSION_FUNCTION("history.deleteUrl", HISTORY_DELETEURL)

 protected:
  ~HistoryDeleteUrlFunction() override {}

  // ExtensionFunction:
  ResponseAction Run() override;
};

class HistoryDeleteRangeFunction : public HistoryFunctionWithCallback {
 public:
  DECLARE_EXTENSION_FUNCTION("history.deleteRange", HISTORY_DELETERANGE)

 protected:
  ~HistoryDeleteRangeFunction() override {}

  // ExtensionFunction:
  ResponseAction Run() override;

  // Callback for the history service to acknowledge deletion.
  void DeleteComplete();
};

}  // namespace extensions

#endif  // CHROME_BROWSER_EXTENSIONS_API_HISTORY_HISTORY_API_H_

四、看下扩展调用history.getVisits 堆栈:

 

总结:

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

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

相关文章

LSTM 长短期记忆网络:解锁时间序列数据的深层秘密

在这个数据驱动的时代&#xff0c;理解和预测时间序列数据成为了许多领域的关键。从股票价格预测到天气模式分析&#xff0c;从自然语言处理到健康监测&#xff0c;时间序列数据无处不在&#xff0c;并且蕴含着丰富的信息。然而&#xff0c;传统的神经网络在处理这类数据时往往…

Openstack 安装教程

1.首先更新系统 sudo apt update sudo apt upgrade -y2.安装必要软件包 sudo apt install -y software-properties-common3.添加openstack官方仓库 sudo add-apt-repository cloud-archive:train sudo apt update4.安装openstack核心组件 sudo apt install -y python3-opens…

技术分享 —— JMeter接口与性能测试实战!

前言 在软件开发和运维过程中&#xff0c;接口性能测试是一项至关重要的工作。JMeter作为一款开源的Java应用&#xff0c;被广泛用于进行各种性能测试&#xff0c;包括接口性能测试。本文将详细介绍如何使用JMeter进行接口性能测试的过程和步骤。 JMeter是Apache组织开发的基…

Redis-02 持久化

redis持久化即将数据从内存写入磁盘&#xff0c;Redis提供了两种持久化的方式&#xff1a;RDB和AOF。 1.RDB RDB持久化&#xff1a;Redis可以将内存中的数据定期快照保存到磁盘上的一个二进制文件中。RDB持久化是一种比较紧凑的文件格式&#xff0c;适用于备份和灾难恢复。通过…

陈零九全新单曲《也曾想走进你的心底》 揭露爱而不得的情感遗憾

图片提供&#xff1a;种子音乐 “创作男神”陈零九于10月9日推出充满深情的全新创作单曲《也曾想走进你的心底》&#xff0c;这首歌再次延续他招牌的“九式情歌”风格&#xff0c;展现其创作魅力。歌曲以一段“爱而不得”的感情故事为主线&#xff0c;深入探讨人们在爱情中的复…

java家政预约上门系统源码,家政服务平台源码,基于SpringBoot框架,数据库使用MySQL,界面渲染采用Thymeleaf技术开发

自主知识产权的家政预约上门系统源码&#xff0c;java版本&#xff0c;支持二次开发&#xff0c;适合商用上项目。 在这个快节奏的现代生活中&#xff0c;越来越多的家庭开始寻求高效、便捷的家政服务解决方案。传统的家政服务模式已经很难满足人们日益增长的个性化与即时性需求…

GAMES202作业3

EvalDiffuse 对于一个diffuse的着色点&#xff0c;它的BRDF为&#xff1a; /** Evaluate diffuse bsdf value.** wi, wo are all in world space.* uv is in screen space, [0, 1] x [0, 1].**/ vec3 EvalDiffuse(vec3 wi, vec3 wo, vec2 uv) {vec3 albedo GetGBufferDiffus…

【Linux】基本认知全套入门

目录 Linux简介 Linux发行版本 发行版选择建议 Centos-社区企业操作系统 Centos版本选择 Linux系统目录 Linux常用命令 SSH客户端 Linux文件操作命令 vim重要快捷键 应用下载与安装 netstat&#xff0c;ps与kill命令使用 Linux应用服务化 Linux用户与权限 Linu…

接口自动化测试实战

测试前准备&#xff1a; 1、项目的介绍 是一个什么项目、项目技术、项目要测的接口和业务流程、业务路径测试用例&#xff08;通过业务流程来梳理业务路径&#xff09; 2、链接和登录密码&#xff1a; 客达天下http://huike-crm.itheima.net/#/clue 客达天下账号admin&…

支持向量机-笔记

支持向量机&#xff08;Support Vector Machine, SVM&#xff09; 是一种强大的监督学习算法&#xff0c;广泛应用于分类和回归任务&#xff0c;特别是在分类问题中表现优异。SVM 的核心思想是通过寻找一个最优超平面&#xff0c;将不同类别的数据点进行分割&#xff0c;并最大…

【YOLO学习】YOLOv4详解

文章目录 1. 整体网络结构1.1 结构图1.2 创新点概括 2. 输入端创新点2.1 Mosaic数据增强2.2 cmBN策略 3. Backbone创新点3.1 CSPDarknet533.2 Mish函数3.3 Dropblock正则化 4. Neck创新点4.1 SPP模块4.2 PAN 5. Prediction5.1 Loss5.2 NMS 1. 整体网络结构 1.1 结构图 1.2 创新…

PostgreSQL学习笔记三:数据类型和运算符

数据类型和运算符 PostgreSQL 支持多种数据类型和运算符&#xff0c;以下是一些常见的数据类型和运算符的概述&#xff1a; 数据类型 基本数据类型 整数类型&#xff1a; SMALLINT&#xff1a;2 字节&#xff0c;范围 -32,768 到 32,767。INTEGER&#xff1a;4 字节&#xff0…

vue3 vue2

vue3.0是如何变快的&#xff1f; diff算法优化 vue2的虚拟dom是进行全局的对比。vue3 新增了静态标记&#xff08;patchFlag&#xff09; 在与上次虚拟节点进行比较的时候&#xff0c;只对比带有patch Flag的节点&#xff0c;并且可以通过flag的信息得知当前节点要对比的具体内…

先进封装技术 Part03---重布线层(RDL)的科普

先进封装核心技术之一:重布线层(RDL)的科普文章 1、 引言 随着电子设备向更小型化、更高性能的方向发展,传统的芯片互连技术已经无法满足日益增长的需求。在这样的背景下,RDL(Re-distributed Layer,重布线层)技术应运而生,成为先进封装技术中的核心之一。 2、 RDL技术…

yolov8.yaml

前面说了yolov8的核心代码放在ultralytics里面&#xff0c;今天我们一起学习一下 YOLOv8模型下的Ultralytics文件目录结构。每个文件夹都有不同的作用&#xff0c;以下是对各个文件夹的解释&#xff1a; assets: 这个文件夹通常存放与模型相关的资源文件&#xff0c;可能包括训…

MySQL五千万大表查询优化实战

背景 DBA同事在钉钉发了两张告警截图&#xff0c;作为“始作俑者”的我很心虚&#xff0c;因为刚才是我在管理后台查询数据&#xff0c;结果很久都没出来&#xff0c;并且用多个维度查了N次 问题分析 这是当天上线的功能&#xff0c;完事我立马锁定SQL然后开启排查 # 原SQL&a…

系统性能优化

在程序员的职业生涯中&#xff0c;解决当前系统问题&#xff0c;优化性能&#xff0c;是走向高阶的必经之路。如果一辈子做着后台开发&#xff0c;写着CRUD&#xff0c;QPS低于10&#xff0c;那确实没必要去做性能优化&#xff0c;因为根本用不上。性能优化范围很广&#xff0c…

排序|插入排序|希尔排序|直接选择排序|堆排序的实现即特性(C)

插入排序 基本思想 直接插入排序是一种简单的插入排序法&#xff0c;其基本思想是&#xff1a; 把待排序的记录按其关键码值的大小逐个插入到一个已经排好序的有序序列中&#xff0c;直到所有的记录插入完为止&#xff0c;得到一个新的有序序列 。 单趟 当插入第 i ( i ≤ 1…

人数识别 人员超员识别系统 作业区域超员预警系统 ai#YOLO视觉

在当今复杂的生产作业与社会管理场景中&#xff0c;人员管理的精准性和高效性变得愈发重要。人数识别、人员超员识别系统、作业区域超员预警系统以及特殊岗位人员达标监测等&#xff0c;都是保障安全生产、提高运营效率和维护社会秩序的关键要素。随着人工智能(AI)技术的飞速发…

【Python实例】Python读取并绘制nc数据

【Python实例】Python读取并绘制nc数据 准备&#xff1a;安装netCDF库等读取nc数据相关信息绘制图形利用basemap绘图 参考 准备&#xff1a;安装netCDF库等 以【1960-2020年中国1km分辨率月降水数据集】中2020年降水为例。 先在Panopoly中查看数据属性&#xff0c;如下&#…