Firefox扩展开发之chrome注册机制

Posted by c4pr1c3 on July 24, 2010
参考自:https://developer.mozilla.org/en/chrome_registration

Chrome是什么?

Chrome是一个内容数据窗口之外的应用程序窗口用户界面元素的集合。工具栏、菜单栏、进度条和窗口标题栏都是典型的chrome组成元素。
chrome.manifest是唯一定义这些元素如何集成的文件。作为特例,XULRunner应用程序还会读取其他的chrome/chrome.manifest配置文件。

Chrome提供者

为一个指定窗口类型(如浏览器窗口)提供chrome被称作chrome提供者。不同的提供者共通为一个完整的chrome集合提供一个特定的窗口,这其中包括了工具栏按钮上的图片、文本描述文件、窗口内容和外观本身。
有三种基本类型的chrome提供者。

Content

The main source file for a window description comes from the content provider, and it can be any file type viewable from within Mozilla. It will typically be a XUL file, since XUL is designed for describing the contents of windows and dialogs. The JavaScript files that define the user interface are also contained within the content packages, as well as most XBL binding files.

Locale

Localizable applications keep all their localized information in locale providers. This allows translators to plug in a different chrome package to translate an application without altering the rest of the source code. The two main types of localizable files are DTD files and Java-style properties files.

Skin

A skin provider is responsible for providing a complete set of files that describe the visual appearance of the chrome. Typically a skin provider will provide CSS files and images.
Note: Scripts (including those found in XBL) loaded from skin packages will not execute.

Chrome的注册

Gecko内核维持着chrome注册服务,该服务提供了将chrome包名称映射到文件系统上chrome安装包的机制。chrome注册机制是可配置和持久化的,因此用户可以安装不同的chrome提供者,选择自己喜欢的皮肤和本地语言。这是通过xpinstall和扩展管理器来实现的。为了告知chrome注册机制所有可用的chrome提供者,使用了扩展的根目录下的chrome.manifest文本文件,theme目录下的chrome.manifest。对于XULRunner应用程序来说,还包括所有的chrome/*.manifest文件。
这种纯文本的chrome清单机制基于一个简单的按行格式。每行内容被独立解析,如果该行内容可解析则chrome注册机制执行相应的操作。否则chrome注册机制忽略该行内容,并会在运行时错误控制台上打印警告消息。

Manifest指令

注释

使用#作为注释符

content

语法:
content packagename uri/to/files/ [flags]

以上指令会注册一个可以引用的路径为:chrome://packagename/content/。指令中既可以使用相对路径URI,也可以使用绝对路径URI。注意:必须用/结束URI定义。

locale

语法:
locale packagename localename uri/to/files/ [flags]

以上指令会注册一个本地化语言包引用路径为:chrome://packagename/locale/。应用程序会根据用户的语言环境选择最适合的语言包。

skin

语法:
skin packagename skinname uri/to/files/ [flags]

以上指令会注册一个皮肤包的引用路径为:chrome://packagename/skin/。其中的skinname指向一个本地已安装的皮肤名。当为一个皮肤包注册了不止一个皮肤名时,chrome注册机制会根据用户首选项选择一个最适合的皮肤。

overlay

语法:
overlay chrome://URI-to-be-overlaid chrome://overlay-URI [flags]

style

语法:
style chrome://URI-to-style chrome://stylesheet-URI [flags]

override

语法:
override chrome://package/type/original-uri.whatever new-resolved-URI [flags]
注意:override指令是非递归指令。

resource

Firefox 3新引入的指令
When using JavaScript code modules it may be necessary to create resource protocol aliases so extensions and applications can load modules using Components.utils.import. Aliases can be created using the resource instruction:
当使用JavaScript代码模块功能时,可以通过resource伪协议来让扩展和应用程序通过使用Components.utils.import访问JavaScript模块。
语法:
resource aliasname uri/to/files/ [flags]

以上指令会创建一个resource:///的路径映射。 </p>

Note that there are no security restrictions preventing web content from including content at resource: URIs, so take care with what you make visible there.

注意:对于resource指令,Firefox不提供同源性安全策略保护。因此需要谨慎使用resource指令开放js代码模块中的导出符号。

Manifest标记

application

语法:
application=app-ID

以上指令标明该Manifest指令适用的目标应用程序。可以通过该指令实现对不同的目标应用程序执行特定应用程序的Manifest指令
如:
overlay chrome://browser/content/browser.xul chrome://myaddon/content/ffOverlay.xul application={ec8030f7-c20a-464f-9b0e-13a3a9e97384}
overlay chrome://messenger/content/mailWindowOverlay.xul chrome://myaddon/content/tbOverlay.xul application={3550f703-e582-4d05-9a08-453d09bdfdc6}
overlay chrome://songbird/content/xul/layoutBaseOverlay.xul chrome://myaddon/content/sbOverlay.xul application=songbird@songbirdnest.com

appversion

语法:
appversion=version
appversion<version
appversion<=version
appversion>version
appversion>=version

以上指令详细定义了该Manifest指令适用的目标应用程序版本。可以通过该指令实现对不同的目标应用程序版本执行特定应用程序的Manifest指令

contentaccessible

Firefox3中的新增Manifest标记
实例:
content packagename chrome/path/ contentaccessible=yes
该标记仅对content指令有效。用于显式的指明该chrome内容可以被外部非信任源的标签和脚本代码访问。

os

Firefox3中的新增Manifest标记
Extensions (or themes) may offer different features depending on the operating system on which Firefox is running. The value is compared to the value of OS_TARGET for the platform.
实例:
os=WINNT
os=Darwin

osversion

Firefox3中的新增Manifest标记,用处类似appversion

platform

仅用于content注册标记。
实例:
content global-platform jar:toolkit.jar!/toolkit/content/global-platform/ platform
如上实例,一旦使用该Manifest标记,global-platform目录下必须有根据不同平台名(如win,mac,unix)命名的子目录,并将相应平台适用的content资源存放在相应的子目录中。任何子目录之外的资源将被忽略。

xpcnativewrappers

默认情况下,该Manifest标记的值为yes。该标记用于防止恶意代码访问chrome资源。从Firefox 4开始,将强制该值为yes,无法设置为no。

调试chrome.manifest

The Chrome List extension shows all registered chrome. This is very helpful when trying to write a chrome.manifest file as you can inspect where the files are being mapped from (jar files, local directory, etc.)</div>