1# @ohos.enterprise.browser(浏览器管理)(系统接口) 2 3本模块提供浏览器管理能力,包括设置/取消浏览器策略、获取浏览器策略等。 4 5> **说明:** 6> 7> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> 9> 本模块接口仅可在Stage模型下使用。 10> 11> 本模块接口仅对[设备管理应用](../../mdm/mdm-kit-guide.md#功能介绍)开放,需将[设备管理应用激活](js-apis-enterprise-adminManager-sys.md#adminmanagerenableadmin)后调用,实现相应功能。 12> 13> 当前页面仅包含本模块的系统接口,其他公开接口参见。其他公开接口参见[@ohos.enterprise.browser](js-apis-enterprise-browser.md)。 14 15## 导入模块 16 17```ts 18import { browser } from '@kit.MDMKit'; 19``` 20 21## browser.setPolicies 22 23setPolicies(admin: Want, appId: string, policies: string, callback: AsyncCallback<void>): void 24 25为指定的浏览器设置浏览策略,使用callback异步回调。 26 27**需要权限:** ohos.permission.ENTERPRISE_SET_BROWSER_POLICY 28 29**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 30 31 32**参数:** 33 34| 参数名 | 类型 | 必填 | 说明 | 35| -------- | ---------------------------------------- | ---- | ------------------------------- | 36| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 37| appId | string | 是 | 应用ID,用于指定浏览器。 | 38| policies | string | 是 | 浏览器策略,当参数policies为空字符串时,表示取消指定浏览器的策略。 | 39| callback | AsyncCallback<void> | 是 | 回调函数。当接口调用成功,err为null,否则为错误对象。 | 40 41**错误码**: 42 43以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 44 45| 错误码ID | 错误信息 | 46| ------- | ---------------------------------------------------------------------------- | 47| 9200001 | The application is not an administrator application of the device. | 48| 9200002 | The administrator application does not have permission to manage the device. | 49| 201 | Permission verification failed. The application does not have the permission required to call the API. | 50| 202 | Permission verification failed. A non-system application calls a system API. | 51| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 52 53**示例:** 54 55```ts 56import { Want } from '@kit.AbilityKit'; 57let wantTemp: Want = { 58 bundleName: 'com.example.myapplication', 59 abilityName: 'EntryAbility', 60}; 61// 此处参数appId的赋值应替换为开发者自己指定的浏览器的应用ID 62let appId: string = 'com.example.******_******/******5t5CoBM='; 63let policies: string = '{"InsecurePrivateNetworkRequestsAllowed":{"level":"mandatory","scope":"machine","source":"platform","value":true},"LegacySameSiteCookieBehaviorEnabledForDomainList":{"level":"mandatory","scope":"machine","source":"platform","value":["[*.]"]}}'; 64browser.setPolicies(wantTemp, appId, policies, (err) => { 65 if (err) { 66 console.error(`Failed to set browser policies. Code is ${err.code}, message is ${err.message}`); 67 return; 68 } 69 console.info('Succeeded in setting browser policies.'); 70}); 71``` 72 73## browser.setPolicies 74 75setPolicies(admin: Want, appId: string, policies: string): Promise<void> 76 77为指定的浏览器设置浏览策略,使用promise异步回调。 78 79**需要权限:** ohos.permission.ENTERPRISE_SET_BROWSER_POLICY 80 81**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 82 83 84**参数:** 85 86| 参数名 | 类型 | 必填 | 说明 | 87| ----- | ----------------------------------- | ---- | ------- | 88| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 89| appId | string | 是 | 应用ID,用于指定浏览器。 | 90| policies | string | 是 | 浏览器策略,当参数policies为空字符串时,表示取消指定浏览器的策略。 | 91 92**返回值:** 93 94| 类型 | 说明 | 95| --------------------- | ------------------------- | 96| Promise<void> | 无返回结果的Promise对象。当设置浏览器策略失败时,会抛出错误对象。 | 97 98**错误码**: 99 100以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 101 102| 错误码ID | 错误信息 | 103| ------- | ---------------------------------------------------------------------------- | 104| 9200001 | The application is not an administrator application of the device. | 105| 9200002 | The administrator application does not have permission to manage the device. | 106| 201 | Permission verification failed. The application does not have the permission required to call the API. | 107| 202 | Permission verification failed. A non-system application calls a system API. | 108| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 109 110**示例:** 111 112```ts 113import { Want } from '@kit.AbilityKit'; 114import { BusinessError } from '@kit.BasicServicesKit'; 115let wantTemp: Want = { 116 bundleName: 'com.example.myapplication', 117 abilityName: 'EntryAbility', 118}; 119// 此处参数appId的赋值应替换为开发者自己指定的浏览器的应用ID 120let appId: string = 'com.example.******_******/******5t5CoBM='; 121let policies: string = '{"InsecurePrivateNetworkRequestsAllowed":{"level":"mandatory","scope":"machine","source":"platform","value":true},"LegacySameSiteCookieBehaviorEnabledForDomainList":{"level":"mandatory","scope":"machine","source":"platform","value":["[*.]"]}}'; 122browser.setPolicies(wantTemp, appId, policies).then(() => { 123 console.info('Succeeded in setting browser policies.'); 124}).catch((err: BusinessError) => { 125 console.error(`Failed to set browser policies. Code is ${err.code}, message is ${err.message}`); 126}); 127``` 128 129## browser.getPolicies 130 131getPolicies(admin: Want, appId: string, callback: AsyncCallback<string>): void 132 133获取指定浏览器的策略,使用callback异步回调。 134 135**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 136 137 138**参数:** 139 140| 参数名 | 类型 | 必填 | 说明 | 141| -------- | ---------------------------------------- | ---- | ------------------------------- | 142| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 143| appId | string | 是 | 应用ID,用于指定浏览器。 | 144| callback | AsyncCallback<string> | 是 | 回调函数,当接口调用成功,err为null,否则为错误对象。 | 145 146**错误码**: 147 148以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 149 150| 错误码ID | 错误信息 | 151| ------- | ---------------------------------------------------------------------------- | 152| 9200001 | The application is not an administrator application of the device. | 153| 202 | Permission verification failed. A non-system application calls a system API. | 154| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 155 156**示例:** 157 158```ts 159import { Want } from '@kit.AbilityKit'; 160let wantTemp: Want = { 161 bundleName: 'com.example.myapplication', 162 abilityName: 'EntryAbility', 163}; 164// 此处参数appId的赋值应替换为开发者自己指定的浏览器的应用ID 165let appId: string = 'com.example.******_******/******5t5CoBM='; 166browser.getPolicies(wantTemp, appId, (err, result) => { 167 if (err) { 168 console.error(`Failed to get browser policies. Code is ${err.code}, message is ${err.message}`); 169 return; 170 } 171 console.info(`Succeeded in getting browser policies, result : ${JSON.stringify(result)}`); 172}); 173``` 174 175## browser.getPolicies 176 177getPolicies(admin: Want, appId: string): Promise<string> 178 179获取指定浏览器的策略,使用promise异步回调。 180 181**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 182 183 184**参数:** 185 186| 参数名 | 类型 | 必填 | 说明 | 187| ----- | ----------------------------------- | ---- | ------- | 188| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 189| appId | string | 是 | 应用ID,用于指定浏览器。 | 190 191**返回值:** 192 193| 类型 | 说明 | 194| --------------------- | ------------------------- | 195| Promise<string> | Promise对象,返回浏览器策略。 | 196 197**错误码**: 198 199以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 200 201| 错误码ID | 错误信息 | 202| ------- | ---------------------------------------------------------------------------- | 203| 9200001 | The application is not an administrator application of the device. | 204| 202 | Permission verification failed. A non-system application calls a system API. | 205| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 206 207**示例:** 208 209```ts 210import { Want } from '@kit.AbilityKit'; 211import { BusinessError } from '@kit.BasicServicesKit'; 212let wantTemp: Want = { 213 bundleName: 'com.example.myapplication', 214 abilityName: 'EntryAbility', 215}; 216// 此处参数appId的赋值应替换为开发者自己指定的浏览器的应用ID 217let appId: string = 'com.example.******_******/******5t5CoBM='; 218browser.getPolicies(wantTemp, appId).then((result) => { 219 console.info(`Succeeded in getting browser policies, result : ${JSON.stringify(result)}`); 220}).catch((err: BusinessError) => { 221 console.error(`Failed to get browser policies. Code is ${err.code}, message is ${err.message}`); 222}); 223``` 224