当前位置: 首页 > article >正文

Chromium 中chrome.topSites扩展接口定义c++

一、chrome.topSites

使用 chrome.topSites API 访问新标签页上显示的热门网站(即最常访问的网站)。不包括用户自定义的快捷方式。

权限

topSites

您必须声明“topSites”扩展程序清单中授予使用此 API 的权限。

{
  "name": "My extension",
  ...
  "permissions": [
    "topSites",
  ],
  ...
}

示例

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

类型

MostVisitedURL

用于封装最常访问的网址(例如新标签页上的默认快捷方式)的对象。

属性
  • 标题

    字符串

    网页的标题

  • 网址

    字符串

    最常访问的网址。

方法

get()

<ph type="x-smartling-placeholder"></ph> 承诺

chrome.topSites.get(
  callback?: function,
)

获取热门网站列表。

api更多介绍参考:chrome.topSites  |  API  |  Chrome for Developers

二、top_sites.json接口定义:

chrome\common\extensions\api\top_sites.json

// 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": "topSites",
    "description": "Use the <code>chrome.topSites</code> API to access the top sites (i.e. most visited sites) that are displayed on the new tab page. These do not include shortcuts customized by the user.",
    "types": [
      {
        "id": "MostVisitedURL",
        "type": "object",
        "description": "An object encapsulating a most visited URL, such as the default shortcuts on the new tab page.",
        "properties": {
          "url": {"type": "string", "description": "The most visited URL."},
          "title": {"type": "string", "description": "The title of the page"}
        }
      }
    ],
    "functions": [
      {
        "name": "get",
        "type": "function",
        "description": "Gets a list of top sites.",
        "parameters": [],
        "returns_async": {
          "name": "callback",
          "parameters": [
            {
              "type": "array",
              "name": "data",
              "items": {"$ref": "MostVisitedURL"}
            }
          ]
        }
      }
    ]
  }
]

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

constexpr char kTopSites[] = R
"R({"namespace":"topSites","types":[{"id":"topSites.MostVisitedURL","type":"object","properties":
{"url":{"type":"string"},"title":{"type":"string"}}}],
"functions":[{"name":"get","type":"function","parameters":[],"returns_async":{"name":"callback","parameters":[{"type":"array","name":"data","items":{"$ref":"topSites.MostVisitedURL"}}]}}]})R";

三、top_sites_api定义:

chrome\browser\extensions\api\top_sites\top_sites_api.h

chrome\browser\extensions\api\top_sites\top_sites_api.cc

namespace extensions {

class TopSitesGetFunction : public ExtensionFunction {
 public:
  DECLARE_EXTENSION_FUNCTION("topSites.get", TOPSITES_GET)

  TopSitesGetFunction();

 protected:
  ~TopSitesGetFunction() override;

  // ExtensionFunction:
  ResponseAction Run() override;

 private:
  void OnMostVisitedURLsAvailable(const history::MostVisitedURLList& data);
};

}  // namespace extensions

namespace extensions {

TopSitesGetFunction::TopSitesGetFunction() = default;
TopSitesGetFunction::~TopSitesGetFunction() = default;

ExtensionFunction::ResponseAction TopSitesGetFunction::Run() {
  scoped_refptr<history::TopSites> ts = TopSitesFactory::GetForProfile(
      Profile::FromBrowserContext(browser_context()));
  if (!ts)
    return RespondNow(Error(kUnknownErrorDoNotUse));

  ts->GetMostVisitedURLs(
      base::BindOnce(&TopSitesGetFunction::OnMostVisitedURLsAvailable, this));

  // GetMostVisitedURLs() will invoke the callback synchronously if the URLs are
  // already populated.
  return did_respond() ? AlreadyResponded() : RespondLater();
}

void TopSitesGetFunction::OnMostVisitedURLsAvailable(
    const history::MostVisitedURLList& data) {
  base::Value::List pages_value;
  for (const auto& url : data) {
    if (!url.url.is_empty()) {
      base::Value::Dict page_value;
      page_value.Set("url", url.url.spec());
      if (url.title.empty()) {
        page_value.Set("title", url.url.spec());
      } else {
        page_value.Set("title", url.title);
      }
      pages_value.Append(std::move(page_value));
    }
  }

  Respond(WithArguments(std::move(pages_value)));
}

}  // namespace extensions

四、chrome.topSites.get数据源介绍:

1、GetMostVisitedURLs函数具体定义在

components\history\core\browser\top_sites_impl.h

components\history\core\browser\top_sites_impl.cc

  // Initializes TopSitesImpl.
  void Init(const base::FilePath& db_name);

  // TopSites implementation.
  void GetMostVisitedURLs(GetMostVisitedURLsCallback callback) override;


// WARNING: this function may be invoked on any thread.
void TopSitesImpl::GetMostVisitedURLs(GetMostVisitedURLsCallback callback) {
  MostVisitedURLList filtered_urls;
  {
    base::AutoLock lock(lock_);
    if (!loaded_) {
      // A request came in before we finished loading. Store the callback and
      // we'll run it on current thread when we finish loading.
      pending_callbacks_.push_back(base::BindOnce(
          &RunOrPostGetMostVisitedURLsCallback,
          base::RetainedRef(base::SingleThreadTaskRunner::GetCurrentDefault()),
          std::move(callback)));
      return;
    }
    filtered_urls = thread_safe_cache_;
  }
  std::move(callback).Run(filtered_urls);
}

2、topSites数据库操作类:

components\history\core\browser\top_sites_backend.h

components\history\core\browser\top_sites_backend.cc

3、topSites数据库初始化类:

components\history\core\browser\top_sites_database.h

components\history\core\browser\top_sites_database.cc

截取数据库表初始化代码:

bool InitTables(sql::Database* db) {
  static constexpr char kTopSitesSql[] =
      "CREATE TABLE IF NOT EXISTS top_sites("
      "url TEXT NOT NULL PRIMARY KEY,"
      "url_rank INTEGER NOT NULL,"
      "title TEXT NOT NULL)";
  return db->Execute(kTopSitesSql);
}

4、topSites数据库存储位置:

  C:\Users\Administrator\AppData\Local\Chromium\User Data\Default\Top Sites

数据库表定义如下:

五、加载扩展看下堆栈:

1、chrome.topSites.get->TopSitesGetFunction::Run

2、TopSitesImpl::GetMostVisitedURLs

3、TopSitesGetFunction::OnMostVisitedURLsAvailable 

    调用 Respond(WithArguments(std::move(pages_value))); 将返回的history::MostVisitedURLList& data数据回调给扩展。

4、看下扩展运行效果: 

总结:分析完毕。


http://www.kler.cn/a/375800.html

相关文章:

  • 智能指针(内存泄漏问题)
  • 齐次线性微分方程的解的性质与结构
  • Spring Scheduled自定任务运行久了就出现数据库连接的错误记录[个人笔记]
  • Raspberry Pi 树莓派产品系列说明
  • 蓝牙MCU蓝牙医疗检测相关案例
  • 游戏和各大APP改IP地址方法教程
  • Unity中有什么情况下是需要用UniTask替代其他异步方式的吗?
  • kdd比赛方案
  • C++【string的模拟实现】
  • Llama 3.2 Vision Molmo:多模态开源生态系统基础
  • 《双指针篇》---移动零
  • 「Mac畅玩鸿蒙与硬件20」鸿蒙UI组件篇10 - Canvas 组件自定义绘图
  • Spring Boot 与 Vue 共筑电影院选票新体验
  • Kong Gateway 指南
  • HTML 基础标签——链接标签 <a> 和 <iframe>
  • Javaweb 实验4 xml
  • 国内百家SRC平台
  • 20241102解决荣品PRO-RK3566开发板刷Rockchip原厂的Buildroot使用荣品的DTS出现
  • Vue基础知识——async指令、scope和样式穿透
  • Maven(20) 如何使用Maven进行版本管理?
  • npm入门教程18:npm发布npm包
  • CVPR2024:完全测试时域适应​​​​(Test-time Adaptation)的目标检测
  • [实战-12] flinkSql 时间属性
  • 互联网技术比游戏后端技术领先十年吗?
  • Android Pair
  • Yocto中的DISTRO和MACHINE的含义与机制