Chromium源码阅读:Mojo实战:从浏览器JS API 到blink实现


通过在前面几篇文章,我们粗略梳理了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++ 绑定和实现

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值