与 local
和 sync
存储区域不同,managed
存储区域需要将其结构声明为 JSON 架构,并由 Chrome 严格验证。此架构必须存储在 "storage"
清单密钥的 "managed_schema"
属性指示的文件中,并声明该扩展程序支持的企业政策。
政策与选项类似,但由系统管理员(而不是用户)配置,允许为组织的所有用户预先配置扩展程序。有关 Chrome 自身的示例,请参阅 Chrome 如何处理政策。
声明政策后,可以通过 storage.managed API 读取这些政策。由扩展程序强制执行管理员配置的政策。
manifest.json 示例
storage.managed_schema
属性指示扩展程序中包含政策架构的文件。
{
"name": "My enterprise extension",
"storage": {
"managed_schema": "schema.json"
},
...
}
然后,Chrome 将从底层操作系统和 Google Apps 中为已登录的用户加载这些政策。每当检测到政策更改时,就会触发 storage.onChanged 事件,包括当扩展程序使用事件页面时浏览器未运行时。您可以访问 chrome://policy 验证 Chrome 加载的政策。
架构格式
Chrome 对 JSON 架构格式有一些额外要求:
- 顶级架构的类型必须为
object
。 - 顶级
object
不能包含additionalProperties
。声明的properties
是此扩展程序的政策。 - 每个架构必须有一个
$ref
值或一个type
。
如果架构无效,则 Chrome 不会加载扩展程序,并指出架构未通过验证的原因。如果政策值不符合架构,则 storage.managed
API 无法发布该政策。
示例架构
{
"type": "object",
// "properties" maps an optional key of this object to its schema. At the
// top-level object, these keys are the policy names supported.
"properties": {
// The policy name "AutoSave" is mapped to its schema, which in this case
// declares it as a simple boolean value.
// "title" and "description" are optional and are used to show a
// user-friendly name and documentation to the administrator.
"AutoSave": {
"title": "Automatically save changes.",
"description": "If set to true then changes will be automatically saved.",
"type": "boolean"
},
// Other simple types supported include "integer", "string" and "number".
"PollRefreshRate": {
"type": "integer"
},
"DefaultServiceUrl": {
"type": "string"
},
// "array" is a list of items that conform to another schema, described
// in "items". An example to this schema is [ "one", "two" ].
"ServiceUrls": {
"type": "array",
"items": {
"type": "string"
}
},
// A more complex example that describes a list of bookmarks. Each bookmark
// has a "title", and can have a "url" or a list of "children" bookmarks.
// The "id" attribute is used to name a schema, and other schemas can reuse
// it using the "$ref" attribute.
"Bookmarks": {
"type": "array",
"id": "ListOfBookmarks",
"items": {
"type": "object",
"properties": {
"title": { "type": "string" },
"url": { "type": "string" },
"children": { "$ref": "ListOfBookmarks" }
}
}
},
// An "object" can have known properties listed as "properties", and can
// optionally have "additionalProperties" indicating a schema to apply to
// keys that aren't found in "properties".
// This example policy could map a URL to its settings. An example value:
// {
// "youtube.com": {
// "blocklisted": true
// },
// "google.com": {
// "bypass_proxy": true
// }
// }
"SettingsForUrls": {
"type": "object",
"additionalProperties": {
"type": "object",
"properties": {
"blocklisted": { "type": "boolean" },
"bypass_proxy": { "type": "boolean" }
}
}
}
}
}