一、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数据回调给扩展。