与 local
和 sync
存储区域不同,managed
存储区域需要将其结构声明为 JSON 架构,并且会由 Chrome 进行严格验证。此架构必须存储在由 "storage"
清单键的 "managed_schema"
属性指示的文件中,并声明应用支持的企业政策。
政策与选项类似,但由系统管理员(而非用户)进行配置,可为组织中的所有用户预配置应用。如需查看 Chrome 本身的示例,请参阅Chrome 如何处理政策。
声明政策后,您可以从 storage.managed API 读取这些政策。应用可以自行强制执行管理员配置的政策。
manifest.json 示例
storage.managed_schema
属性表示应用中包含政策架构的文件。
{
"name": "My enterprise app",
"storage": {
"managed_schema": "schema.json"
},
...
}
然后,Chrome 会从底层操作系统和 Google 应用中加载这些政策,以供已登录的用户使用。每当检测到政策更改时,系统都会触发 storage.onChanged 事件,包括在应用使用事件页面时,即使浏览器未运行也是如此。您可以前往 chrome://policy 验证 Chrome 加载的政策。
架构格式
对于 JSON 架构格式,Chrome 还需遵循一些额外要求:
- 顶级架构的类型必须为
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" }
}
}
}
}
}