通过在前面几篇文章,我们粗略梳理了Mojo这套跨进程通信的设计思路和IDL细节。
实际上,Mojo不止是跨进程通信框架,而是跨语言的模块通信自动化系统。
在浏览器暴露的JS API,也是需要通过Mojo这个系统进行桥接,最终到blink的实现上。
我们打开浏览器控制台,可以输入 window.cookieStore.getAll()
获取当前默认Frame的Cookie。如下图:
我们以这个API为例子,看看Chromium是如何实现这个功能的。
确定Web标准
这个API是基于Cookie Store API的Web标准的一部分。首先,我们可以先查找相关的Web标准文档以了解这个API的定义和预期行为。
Cookie Store API
CookieStore 接口的 getAll() 方法返回与传递给它的 name 或 options 匹配的所有 cookie 列表。 语法
getAll(name) getAll(options)
参数 name 可选 记录 cookie 名称的字符串。 或 options 可选 一个包括以下属性的对象:name:记录 cookie
名称的字符串。 url:记录 cookie URL 的字符串。 返回值:一个兑现为与指定的 name 或 options 对象匹配的
cookie 信息对象的 Promise。
寻找IDL 定义
这个API是Web标准API,肯定在blink/render下面,简单一搜就找到了:
打开third_party\blink\renderer\modules\cookie_store\cookie_store.idl:
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// https://wicg.github.io/cookie-store/explainer.html
[
Exposed=(ServiceWorker,Window),
SecureContext
] interface CookieStore : EventTarget {
// https://wicg.github.io/cookie-store/explainer.html#the-query-api
[CallWith=ScriptState, Measure, RaisesException] Promise<CookieListItem?> get(
USVString name);
[CallWith=ScriptState, Measure, RaisesException] Promise<CookieListItem?> get(
optional CookieStoreGetOptions options = {
});
[CallWith=ScriptState, Measure, RaisesException] Promise<CookieList> getAll(
USVString name);
[CallWith=ScriptState, Measure, RaisesException] Promise<CookieList> getAll(
optional CookieStoreGetOptions options = {
});
// https://wicg.github.io/cookie-store/explainer.html#the-modifications-api
[CallWith=ScriptState, Measure, RaisesException] Promise<undefined> set(
USVString name, USVString value);
[CallWith=ScriptState, Measure, RaisesException] Promise<undefined> set(
CookieInit cookieInit);
[CallWith=ScriptState, ImplementedAs=Delete, Measure, RaisesException]
Promise<undefined> delete(USVString name);
[CallWith=ScriptState, ImplementedAs=Delete, Measure, RaisesException]
Promise<undefined> delete(CookieStoreDeleteOptions options);
// https://wicg.github.io/cookie-store/explainer.html#the-change-events-api
[Exposed=Window] attribute EventHandler onchange;
};
这个idl不复杂,我们针对其内容,简单做了注释标注,方便读者理解:
// Cookie Store API的解释器文档链接
// https://wicg.github.io/cookie-store/explainer.html
// 定义一个名为CookieStore的接口,它在ServiceWorker和Window环境中被暴露,
[
Exposed=(ServiceWorker,Window), // 暴露给ServiceWorker和Window
SecureContext // 并且仅在安全上下文中可用(如HTTPS)
] interface CookieStore : EventTarget {
// 查询API部分的解释器文档链接
// https://wicg.github.io/cookie-store/explainer.html#the-query-api
// ...
[CallWith=ScriptState, Measure, RaisesException] Promise<CookieList> getAll(
USVString name);
// 使用选项获取所有匹配的cookies。返回一个包含cookie信息数组的Promise对象。
[CallWith=ScriptState, Measure, RaisesException] Promise<CookieList> getAll(
optional CookieStoreGetOptions options = {
});
// ...
};
[CallWith=ScriptState]表示函数在调用时需要当前的脚本状态,[Measure]表示该函数调用会被性能监控工具记录,而[RaisesException]则表明该函数可能会抛出异常。Promise表示该方法操作成功后会返回一个解决为undefined的Promise对象,Promise<CookieListItem?>表示返回的Promise对象会解决为CookieListItem类型或者null。ImplementedAs属性用于指定C++实现中对应的方法名。
查看对应生成的文件:
所有的mojom生成的文件都会放到out\xxx\gen目录下。根据源文件路径,很容易找到对应的生成文件:
看起来有点多,别紧张,我们依次进行解释:
C++ 绑定和实现