1# @ohos.enterprise.browser (Browser Management) 2 3The **browser** module provides browser management, including setting, deleting, and obtaining browser policies. 4 5> **NOTE** 6> 7> - The initial APIs of this module are supported since API version 12. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8> 9> - The APIs of this module can be used only in the stage model. 10> 11> - The APIs of this module can be called only by a [device administrator application](../../mdm/mdm-kit-guide.md#introduction) that is enabled. 12 13## Modules to Import 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 23Sets a policy for a specified browser. 24 25**Required permissions**: ohos.permission.ENTERPRISE_SET_BROWSER_POLICY 26 27**System capability**: SystemCapability.Customization.EnterpriseDeviceManager 28 29**Parameters** 30 31| Name | Type | Mandatory| Description | 32| ----------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 33| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes | EnterpriseAdminExtensionAbility. | 34| appId | string | Yes | Application ID, which is used to specify the browser. | 35| policyName | string | Yes | Name of the browser policy to set. If the value is an empty string, the browser policy corresponding to the application ID is set.| 36| policyValue | string | Yes | Browser policy to set. If the value is an empty string, the policy corresponding to the policy name is removed.| 37 38**Error codes** 39 40For details about the error codes, see [Enterprise Device Management Error Codes](errorcode-enterpriseDeviceManager.md) and [Universal Error Codes](../errorcode-universal.md). 41 42| ID| Error Message | 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**Example** 50 51```ts 52import { Want } from '@kit.AbilityKit'; 53let wantTemp: Want = { 54 bundleName: 'com.example.myapplication', 55 abilityName: 'EntryAbility', 56}; 57 58// Replace the value of appId with the specified application ID of the browser. 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 75Obtains the policies of a browser through the specified device administrator application. 76 77**System capability**: SystemCapability.Customization.EnterpriseDeviceManager 78 79 80**Parameters** 81 82| Name| Type | Mandatory| Description | 83| ------ | ------------------------------------------------------- | ---- | ------------------------ | 84| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes | EnterpriseAdminExtensionAbility. | 85| appId | string | Yes | Application ID, which is used to specify the browser.| 86 87**Return value** 88 89| Type | Description | 90| ------ | ------------ | 91| string | Browser policies obtained.| 92 93**Error codes** 94 95For details about the error codes, see [Enterprise Device Management Error Codes](errorcode-enterpriseDeviceManager.md) and [Universal Error Codes](../errorcode-universal.md). 96 97| ID| Error Message | 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**Example** 103 104```ts 105import { Want } from '@kit.AbilityKit'; 106let wantTemp: Want = { 107 bundleName: 'com.example.myapplication', 108 abilityName: 'EntryAbility', 109}; 110 111// Replace the value of appId with the specified application ID of the browser. 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 126Sets the browser hosting policy. After the setting is successful, the system common event [BROWSER_POLICY_CHANGED_EVENT](../apis-basic-services-kit/common_event/commonEventManager-definitions.md#common_event_managed_browser_policy_changed) is released. 127 128**Required permissions**: ohos.permission.ENTERPRISE_SET_BROWSER_POLICY 129 130**System capability**: SystemCapability.Customization.EnterpriseDeviceManager 131 132**Parameters** 133 134| Name | Type | Mandatory| Description | 135| ----------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 136| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes | EnterpriseAdminExtensionAbility. | 137| bundleName | string | Yes | Application bundle name, which is used to specify the browser. | 138| policyName | string | Yes | Browser policy name.| 139| policyValue | string | Yes | Browser policy value. If the value is an empty string, the policy corresponding to the policy name is removed.| 140 141**Error codes** 142 143For details about the error codes, see [Enterprise Device Management Error Codes](errorcode-enterpriseDeviceManager.md) and [Universal Error Codes](../errorcode-universal.md). 144 145| ID| Error Message | 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**Example** 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 176Obtains the browser hosting policy. 177 178**System capability**: SystemCapability.Customization.EnterpriseDeviceManager 179 180 181**Parameters** 182 183| Name | Type | Mandatory| Description | 184| ----------- | ------------------------------------------------------- | ---- | ------------------------ | 185| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes | EnterpriseAdminExtensionAbility. | 186| bundleName | string | Yes | Application bundle name, which is used to specify the browser.| 187 188**Return value** 189 190| Type | Description | 191| ----------- | ------------ | 192| ArrayBuffer | Browser hosting policy.| 193 194**Error codes** 195 196For details about the error codes, see [Enterprise Device Management Error Codes](errorcode-enterpriseDeviceManager.md) and [Universal Error Codes](../errorcode-universal.md). 197 198| ID| Error Message | 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**Example** 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 229Obtains the browser hosting policy version. 230 231**System capability**: SystemCapability.Customization.EnterpriseDeviceManager 232 233 234**Return value** 235 236| Type | Description | 237| ------ | ------------ | 238| string | Browser hosting policy version.| 239 240**Example** 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 256Obtains the browser hosting policy. 257 258**System capability**: SystemCapability.Customization.EnterpriseDeviceManager 259 260 261**Return value** 262 263| Type | Description | 264| ----------- | ------------ | 265| ArrayBuffer | Browser hosting policy.| 266 267**Example** 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``` 283