1# @ohos.enterprise.browser(浏览器管理) 2 3本模块提供浏览器管理能力,包括设置/取消浏览器策略、获取浏览器策略等。 4 5> **说明:** 6> 7> 本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> 9> 本模块接口仅可在Stage模型下使用。 10> 11> 本模块接口仅对[设备管理应用](../../mdm/mdm-kit-guide.md#功能介绍)开放,需将设备管理应用激活后调用,实现相应功能。 12 13## 导入模块 14 15```ts 16import { browser } from '@kit.MDMKit'; 17``` 18 19## browser.setPolicySync 20 21setPolicySync(admin: Want, appId: string, policyName: string, policyValue: string): void 22 23为指定的浏览器设置浏览器子策略。 24 25**需要权限:** ohos.permission.ENTERPRISE_SET_BROWSER_POLICY 26 27**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 28 29**参数:** 30 31| 参数名 | 类型 | 必填 | 说明 | 32| ----------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 33| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 34| appId | string | 是 | 应用ID,用于指定浏览器。 | 35| policyName | string | 是 | 浏览器子策略名。当此值为空字符串时,表示设置应用ID对应的浏览器策略。 | 36| policyValue | string | 是 | 浏览器子策略值。当此值为空字符串时,表示取消浏览器策略名对应浏览器子策略。 | 37 38**错误码**: 39 40以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 41 42| 错误码ID | 错误信息 | 43| -------- | ------------------------------------------------------------ | 44| 9200001 | The application is not an administrator application of the device. | 45| 9200002 | The administrator application does not have permission to manage the device. | 46| 201 | Permission verification failed. The application does not have the permission required to call the API. | 47| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 48 49**示例:** 50 51```ts 52import { Want } from '@kit.AbilityKit'; 53let wantTemp: Want = { 54 bundleName: 'com.example.myapplication', 55 abilityName: 'EntryAbility', 56}; 57 58// 此处参数appId的赋值应替换为开发者自己指定的浏览器的应用ID 59let appId: string = 'com.example.******_******/******5t5CoBM='; 60let policyName: string = 'InsecurePrivateNetworkRequestsAllowed'; 61let policyValue: string = '{"level":"mandatory","scope":"machine","source":"platform","value":true}'; 62 63try { 64 browser.setPolicySync(wantTemp, appId, policyName, policyValue); 65 console.info('Succeeded in setting browser policies.'); 66} catch (err) { 67 console.error(`Failed to set browser policies. Code is ${err.code}, message is ${err.message}`); 68} 69``` 70 71## browser.getPoliciesSync 72 73getPoliciesSync(admin: Want, appId: string): string 74 75指定管理员应用获取指定浏览器的策略。 76 77**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 78 79 80**参数:** 81 82| 参数名 | 类型 | 必填 | 说明 | 83| ------ | ------------------------------------------------------- | ---- | ------------------------ | 84| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 85| appId | string | 是 | 应用ID,用于指定浏览器。 | 86 87**返回值:** 88 89| 类型 | 说明 | 90| ------ | ------------ | 91| string | 浏览器策略。 | 92 93**错误码**: 94 95以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 96 97| 错误码ID | 错误信息 | 98| -------- | ------------------------------------------------------------ | 99| 9200001 | The application is not an administrator application of the device. | 100| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 101 102**示例:** 103 104```ts 105import { Want } from '@kit.AbilityKit'; 106let wantTemp: Want = { 107 bundleName: 'com.example.myapplication', 108 abilityName: 'EntryAbility', 109}; 110 111// 此处参数appId的赋值应替换为开发者自己指定的浏览器的应用ID 112let appId: string = 'com.example.******_******/******5t5CoBM='; 113 114try { 115 let result: string = browser.getPoliciesSync(wantTemp, appId); 116 console.info(`Succeeded in getting browser policies, result : ${JSON.stringify(result)}`); 117} catch(err) { 118 console.error(`Failed to get browser policies. Code is ${err.code}, message is ${err.message}`); 119} 120``` 121 122## browser.setManagedBrowserPolicy<sup>15+</sup> 123 124setManagedBrowserPolicy(admin: Want, bundleName: string, policyName: string, policyValue: string): void 125 126为指定的浏览器设置浏览器托管策略,成功后会发布系统公共事件[BROWSER_POLICY_CHANGED_EVENT](../apis-basic-services-kit/common_event/commonEventManager-definitions.md#common_event_managed_browser_policy_changed)。 127 128**需要权限:** ohos.permission.ENTERPRISE_SET_BROWSER_POLICY 129 130**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 131 132**参数:** 133 134| 参数名 | 类型 | 必填 | 说明 | 135| ----------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 136| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 137| bundleName | string | 是 | 应用包名,用于指定浏览器。 | 138| policyName | string | 是 | 浏览器策略名。 | 139| policyValue | string | 是 | 浏览器策略值。当此值为空字符串时,表示取消浏览器策略名对应浏览器子策略。 | 140 141**错误码**: 142 143以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 144 145| 错误码ID | 错误信息 | 146| -------- | ------------------------------------------------------------ | 147| 9200001 | The application is not an administrator application of the device. | 148| 9200002 | The administrator application does not have permission to manage the device. | 149| 201 | Permission verification failed. The application does not have the permission required to call the API. | 150| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 151 152**示例:** 153 154```ts 155import { Want } from '@kit.AbilityKit'; 156let wantTemp: Want = { 157 bundleName: 'com.example.myapplication', 158 abilityName: 'EntryAbility', 159}; 160let bundleName: string = 'com.example.testbrowser'; 161let policyName: string = 'InsecurePrivateNetworkRequestsAllowed'; 162let policyValue: string = '{"level":"mandatory","scope":"machine","source":"platform","value":true}'; 163 164try { 165 browser.setManagedBrowserPolicy(wantTemp, bundleName, policyName, policyValue); 166 console.info('Succeeded in setting managed browser policy.'); 167} catch (err) { 168 console.error(`Failed to set managed browser policy. Code is ${err.code}, message is ${err.message}`); 169} 170``` 171 172## browser.getManagedBrowserPolicy<sup>15+</sup> 173 174getManagedBrowserPolicy(admin: Want, bundleName: string): ArrayBuffer 175 176获取指定浏览器的浏览器托管策略。 177 178**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 179 180 181**参数:** 182 183| 参数名 | 类型 | 必填 | 说明 | 184| ----------- | ------------------------------------------------------- | ---- | ------------------------ | 185| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 186| bundleName | string | 是 | 应用包名,用于指定浏览器。 | 187 188**返回值:** 189 190| 类型 | 说明 | 191| ----------- | ------------ | 192| ArrayBuffer | 浏览器托管策略。 | 193 194**错误码**: 195 196以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 197 198| 错误码ID | 错误信息 | 199| -------- | ------------------------------------------------------------ | 200| 9200001 | The application is not an administrator application of the device. | 201| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 202 203**示例:** 204 205```ts 206import { Want } from '@kit.AbilityKit'; 207import { util } from '@kit.ArkTS'; 208let wantTemp: Want = { 209 bundleName: 'com.example.myapplication', 210 abilityName: 'EntryAbility', 211}; 212let bundleName: string = 'com.example.testbrowser'; 213 214try { 215 let buffer: ArrayBuffer = browser.getManagedBrowserPolicy(wantTemp, bundleName); 216 let intBuffer: Uint8Array = new Uint8Array(buffer); 217 let decoder: util.TextDecoder = util.TextDecoder.create('utf-8'); 218 let stringData: string = decoder.decodeWithStream(intBuffer); 219 console.info(`Succeeded in getting managed browser policy, result : ${stringData}`); 220} catch(err) { 221 console.error(`Failed to get managed browser policy. Code is ${err.code}, message is ${err.message}`); 222} 223``` 224 225## browser.getSelfManagedBrowserPolicyVersion<sup>15+</sup> 226 227getSelfManagedBrowserPolicyVersion(): string 228 229获取指定浏览器的浏览器托管策略版本。 230 231**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 232 233 234**返回值:** 235 236| 类型 | 说明 | 237| ------ | ------------ | 238| string | 浏览器托管策略版本。 | 239 240**示例:** 241 242```ts 243 244try { 245 let version: string = browser.getSelfManagedBrowserPolicyVersion(); 246 console.info(`Succeeded in getting self managed browser policy version, result : ${version}`); 247} catch(err) { 248 console.error(`Failed to get self managed browser policy version. Code is ${err.code}, message is ${err.message}`); 249} 250``` 251 252## browser.getSelfManagedBrowserPolicy<sup>15+</sup> 253 254getSelfManagedBrowserPolicy(): ArrayBuffer 255 256获取指定浏览器的浏览器托管策略。 257 258**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 259 260 261**返回值:** 262 263| 类型 | 说明 | 264| ----------- | ------------ | 265| ArrayBuffer | 浏览器托管策略。 | 266 267**示例:** 268 269```ts 270import { Want } from '@kit.AbilityKit'; 271import { util } from '@kit.ArkTS'; 272 273try { 274 let buffer: ArrayBuffer = browser.getSelfManagedBrowserPolicy(); 275 let intBuffer: Uint8Array = new Uint8Array(buffer); 276 let decoder: util.TextDecoder = util.TextDecoder.create('utf-8'); 277 let stringData: string = decoder.decodeWithStream(intBuffer); 278 console.info(`Succeeded in getting self managed browser policy, result : ${stringData}`); 279} catch(err) { 280 console.error(`Failed to get self managed browser policy. Code is ${err.code}, message is ${err.message}`); 281} 282```