Chrome 扩展内部结构

发布于 2025-01-08 11:17:37 字数 1031 浏览 2 评论 0原文

我正在尝试了解 Chrome 扩展的内部结构。如果您能提供更多有关此问题的详细信息,那就太好了。

我的理解如下:

  1. 扩展在自己的进程中运行。
  2. 扩展通过 IPC 与浏览器进程交互。

我的问题: 1. 每当我们从扩展用户脚本调用扩展 api(例如 chrome.module.function())时,我希望该函数首先转到渲染器进程,然后将 IPC(例如 ExtensionHostMsg_ABC)发送到浏览器进程以执行所需的操作手术。

                Extension Process                                  Browser Process

                                             ExtensionHostMsg_Req
               chrome.module.function()      ---------------->     Handle the msg

                                             ExtensionMsg_Response
                Send the result back to js   <----------------- 

但令我惊讶的是,我看到所有扩展 api 函数都在 chrome/browser/extensions/xyz_api.h & 中声明和定义。 .cc 文件。这样看起来,当js调用一个api时,我们就直接来到了浏览器进程。这个魔法在哪里以及如何发生?

  1. 我还在 chrome/renderer/extensions/abc_bindings.cc 中看到了一些扩展绑定 c++ 代码,在 chrome/renderer/extensions/resources/abc.js 中看到了相应的 js 代码。我认为我们有这段代码可以在扩展渲染器和 js 代码之间共享一些信息。对吗?您能为我提供一个示例场景吗?

很抱歉问这些基本问题。我真的很感谢你的帮助。

I am trying to understand the internals of Chrome Extension. It will be great if you can provide more details regarding this.

My understandings are as follows:

  1. Extensions run in its own process.
  2. Extensions interact with Browser process through IPC.

My question:
1. Whenever we call a extension api such as chrome.module.function() from extension user script, I would expect the function to go to the renderer process first and then send a IPC such as ExtensionHostMsg_ABC to the browser process to do the required operation.

                Extension Process                                  Browser Process

                                             ExtensionHostMsg_Req
               chrome.module.function()      ---------------->     Handle the msg

                                             ExtensionMsg_Response
                Send the result back to js   <----------------- 

But to my surprise, I see that all the extension api functions are declared and defined in chrome/browser/extensions/xyz_api.h & .cc files. So it looks like when a api is called from js, we come directly to the browser process. Where and how does this magic happens?

  1. I also see some extensions bindings c++ code in chrome/renderer/extensions/abc_bindings.cc and the corresponding js code in chrome/renderer/extensions/resources/abc.js. I think we have this code to share some information between the extension renderer and js code. Is it right? Can you provide me a example scenario for this?

Sorry for asking these basic questions. I really appreciate your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只等公子 2025-01-15 11:17:37

关于您关于如何进行绑定的问题。您可以在 chrome/common/extensions/api/extension_api.json 中定义扩展 API,这就是 V8 设置 C++ JavaScript 绑定的方式。这就是所有魔法发生的地方:)

如果您想从内容脚本调用特权 API,您需要将命名空间添加到 chrome/renderer/resources/extensions/renderer_extension_bindings.js

有关更多信息Chromium 中扩展系统的设计,有很好的文档解释了整个流程模型和交互:
http://www.chromium.org/developers/design-documents/extensions

Regarding your question on how the bindings are made. You define your extension API in chrome/common/extensions/api/extension_api.json which is how V8 sets up the C++ JavaScript bindings. That is where all the magic happens :)

If you want to call privileged APIs from the content script you would need to add the namespace to chrome/renderer/resources/extensions/renderer_extension_bindings.js

For more information regarding the design of the Extension System in Chromium, there are good docs that explain the whole process model and interactions:
http://www.chromium.org/developers/design-documents/extensions

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文