Chrome 扩展内部结构
我正在尝试了解 Chrome 扩展的内部结构。如果您能提供更多有关此问题的详细信息,那就太好了。
我的理解如下:
- 扩展在自己的进程中运行。
- 扩展通过 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时,我们就直接来到了浏览器进程。这个魔法在哪里以及如何发生?
- 我还在 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:
- Extensions run in its own process.
- 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?
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于您关于如何进行绑定的问题。您可以在 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