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