1# @ohos.dlpPermission (数据防泄漏) 2<!--Kit: Data Protection Kit--> 3<!--Subsystem: Security--> 4<!--Owner: @winnieHuYu--> 5<!--Designer: @lucky-jinduo--> 6<!--Tester: @nacyli--> 7<!--Adviser: @zengyawen--> 8 9数据防泄漏(DLP)是系统提供的系统级的数据防泄漏解决方案,提供跨设备的文件的权限管理、加密存储、授权访问等能力。 10 11> **说明:** 12> 13> - 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 14> - @ohos.dlpPermission归属的Kit已由'DataLossPreventionKit'变更为'DataProtectionKit',建议开发者使用新模块名'@kit.DataProtectionKit'完成模块导入。如果使用'@kit.DataLossPreventionKit'导入,仅能调用改名前的接口,无法使用新增接口。 15 16## 导入模块 17 18```ts 19import { dlpPermission } from '@kit.DataProtectionKit'; 20``` 21 22## dlpPermission.isDLPFile 23 24isDLPFile(fd: number): Promise<boolean> 25 26根据文件的fd,查询该文件是否是DLP文件。使用Promise方式异步返回结果。 27 28**系统能力:** SystemCapability.Security.DataLossPrevention 29 30**参数:** 31 32| 参数名 | 类型 | 必填 | 说明 | 33| -------- | -------- | -------- | -------- | 34| fd | number | 是 | 待查询文件的fd(文件描述符)。 | 35 36**返回值:** 37| 类型 | 说明 | 38| -------- | -------- | 39| Promise<boolean> | Promise对象。返回true表示是DLP文件,返回false表示非DLP文件。 | 40 41**错误码:** 42 43以下错误码的详细介绍请参见[DLP服务错误码](errorcode-dlp.md)。 44 45| 错误码ID | 错误信息 | 46| -------- | -------- | 47| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 48| 19100001 | Invalid parameter value. | 49| 19100011 | The system ability works abnormally. | 50 51**示例:** 52 53```ts 54import { dlpPermission } from '@kit.DataProtectionKit'; 55import { fileIo } from '@kit.CoreFileKit'; 56import { BusinessError } from '@kit.BasicServicesKit'; 57 58let uri = "file://docs/storage/Users/currentUser/Desktop/test.txt.dlp"; 59let file: number | undefined = undefined; 60try { 61 file = fileIo.openSync(uri).fd; 62 let res = dlpPermission.isDLPFile(file); // 是否加密DLP文件。 63 console.info('res', res); 64} catch (err) { 65 console.error('error', (err as BusinessError).code, (err as BusinessError).message); // 失败报错。 66} finally { 67 if (file !== undefined) { 68 fileIo.closeSync(file); 69 } 70} 71``` 72 73## dlpPermission.isDLPFile 74 75isDLPFile(fd: number, callback: AsyncCallback<boolean>): void 76 77根据文件的fd,查询该文件是否是DLP文件。使用callback方式异步返回结果。 78 79**系统能力:** SystemCapability.Security.DataLossPrevention 80 81**参数:** 82 83| 参数名 | 类型 | 必填 | 说明 | 84| -------- | -------- | -------- | -------- | 85| fd | number | 是 | 待查询文件的fd(文件描述符)。 | 86| callback | AsyncCallback<boolean> | 是 | 回调函数。返回true表示是DLP文件,返回false表示非DLP文件。 | 87 88**错误码:** 89 90以下错误码的详细介绍请参见[DLP服务错误码](errorcode-dlp.md)。 91 92| 错误码ID | 错误信息 | 93| -------- | -------- | 94| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 95| 19100001 | Invalid parameter value. | 96| 19100011 | The system ability works abnormally. | 97 98**示例:** 99 100```ts 101import { dlpPermission } from '@kit.DataProtectionKit'; 102import { fileIo } from '@kit.CoreFileKit'; 103import { BusinessError } from '@kit.BasicServicesKit'; 104 105let uri = "file://docs/storage/Users/currentUser/Desktop/test.txt.dlp"; 106let file: number | undefined = undefined; 107try { 108 file = fileIo.openSync(uri).fd; 109 dlpPermission.isDLPFile(file, (err, res) => { 110 if (err != undefined) { 111 console.error('isDLPFile error,', err.code, err.message); 112 } else { 113 console.info('res', res); 114 } 115 fileIo.closeSync(file); 116 }); 117} catch (err) { 118 console.error('isDLPFile error,', (err as BusinessError).code, (err as BusinessError).message); 119 if (file !== undefined) { 120 fileIo.closeSync(file); 121 } 122} 123``` 124 125## dlpPermission.getDLPPermissionInfo 126 127getDLPPermissionInfo(): Promise<DLPPermissionInfo> 128 129查询当前DLP沙箱的权限信息。使用Promise方式异步返回结果。 130 131**系统能力:** SystemCapability.Security.DataLossPrevention 132 133**返回值:** 134 135| 类型 | 说明 | 136| -------- | -------- | 137| Promise<[DLPPermissionInfo](#dlppermissioninfo)> | Promise对象。返回查询的DLP文件的权限信息,无异常则表明查询成功。 | 138 139**错误码:** 140 141以下错误码的详细介绍请参见[DLP服务错误码](errorcode-dlp.md)。 142 143| 错误码ID | 错误信息 | 144| -------- | -------- | 145| 19100001 | Invalid parameter value. | 146| 19100006 | No permission to call this API, which is available only for DLP sandbox applications. | 147| 19100011 | The system ability works abnormally. | 148 149**示例:** 150 151```ts 152import { dlpPermission } from '@kit.DataProtectionKit'; 153import { BusinessError } from '@kit.BasicServicesKit'; 154 155async function ExampleFunction() { 156 try { 157 dlpPermission.isInSandbox().then(async (inSandbox) => { // 是否在沙箱内。 158 if (inSandbox) { 159 let res: dlpPermission.DLPPermissionInfo = await dlpPermission.getDLPPermissionInfo(); // 获取当前权限信息。 160 console.info('res', JSON.stringify(res)); 161 } 162 }); 163 } catch (err) { 164 console.error('error', (err as BusinessError).code, (err as BusinessError).message); // 失败报错。 165 } 166} 167``` 168 169## dlpPermission.getDLPPermissionInfo 170 171getDLPPermissionInfo(callback: AsyncCallback<DLPPermissionInfo>): void 172 173查询当前DLP沙箱的权限信息。使用callback方式异步返回结果。 174 175**系统能力:** SystemCapability.Security.DataLossPrevention 176 177**参数:** 178 179| 参数名 | 类型 | 必填 | 说明 | 180| -------- | -------- | -------- | -------- | 181| callback | AsyncCallback<[DLPPermissionInfo](#dlppermissioninfo)> | 是 | 回调函数。err为undefine时表示查询成功;否则为错误对象。 | 182 183**错误码:** 184 185以下错误码的详细介绍请参见[DLP服务错误码](errorcode-dlp.md)。 186 187| 错误码ID | 错误信息 | 188| -------- | -------- | 189| 401 | Parameter error. Possible causes: 1. Incorrect parameter types. | 190| 19100001 | Invalid parameter value. | 191| 19100006 | No permission to call this API, which is available only for DLP sandbox applications. | 192| 19100011 | The system ability works abnormally. | 193 194**示例:** 195 196```ts 197import { dlpPermission } from '@kit.DataProtectionKit'; 198import { fileIo } from '@kit.CoreFileKit'; 199import { BusinessError } from '@kit.BasicServicesKit'; 200 201try { 202 dlpPermission.isInSandbox().then((inSandbox) => { // 是否在沙箱内。 203 if (inSandbox) { 204 dlpPermission.getDLPPermissionInfo((err, res) => { 205 if (err != undefined) { 206 console.error('getDLPPermissionInfo error,', err.code, err.message); 207 } else { 208 console.info('res', JSON.stringify(res)); 209 } 210 }); // 获取当前权限信息。 211 } 212 }); 213} catch (err) { 214 console.error('getDLPPermissionInfo error,', (err as BusinessError).code, (err as BusinessError).message); 215} 216``` 217 218## dlpPermission.getOriginalFileName 219 220getOriginalFileName(fileName: string): string 221 222获取指定DLP文件名的原始文件名。接口为同步接口。 223 224**系统能力:** SystemCapability.Security.DataLossPrevention 225 226**参数:** 227 228| 参数名 | 类型 | 必填 | 说明 | 229| -------- | -------- | -------- | -------- | 230| fileName | string | 是 | 指定要查询的文件名。不超过255字节。 | 231 232**返回值:** 233 234| 类型 | 说明 | 235| -------- | -------- | 236| string | 返回DLP文件的原始文件名。例如:DLP文件名为test.txt.dlp,则返回的原始文件名为test.txt。 | 237 238**错误码:** 239 240以下错误码的详细介绍请参见[DLP服务错误码](errorcode-dlp.md)。 241 242| 错误码ID | 错误信息 | 243| -------- | -------- | 244| 19100001 | Invalid parameter value. | 245| 19100011 | The system ability works abnormally. | 246 247**示例:** 248 249```ts 250import { dlpPermission } from '@kit.DataProtectionKit'; 251import { BusinessError } from '@kit.BasicServicesKit'; 252 253try { 254 let res = dlpPermission.getOriginalFileName('test.txt.dlp'); // 获取原始文件名。 255 console.info('res', res); 256} catch (err) { 257 console.error('error', (err as BusinessError).code, (err as BusinessError).message); // 失败报错。 258} 259``` 260 261## dlpPermission.getDLPSuffix 262 263getDLPSuffix(): string 264 265获取DLP文件扩展名。接口为同步接口。 266 267**系统能力:** SystemCapability.Security.DataLossPrevention 268 269**返回值:** 270 271| 类型 | 说明 | 272| -------- | -------- | 273| string | 返回DLP文件扩展名。例如:原文件"text.txt",加密后的DLP文件名为"test.txt.dlp",返回拓展名为".dlp"。 | 274 275**错误码:** 276 277以下错误码的详细介绍请参见[DLP服务错误码](errorcode-dlp.md)。 278 279| 错误码ID | 错误信息 | 280| -------- | -------- | 281| 19100011 | The system ability works abnormally. | 282 283**示例:** 284 285```ts 286import { dlpPermission } from '@kit.DataProtectionKit'; 287import { BusinessError } from '@kit.BasicServicesKit'; 288 289try { 290 let res = dlpPermission.getDLPSuffix(); // 获取DLP拓展名。 291 console.info('res', res); 292} catch (err) { 293 console.error('error', (err as BusinessError).code, (err as BusinessError).message); // 失败报错。 294} 295``` 296 297## dlpPermission.on('openDLPFile') 298 299on(type: 'openDLPFile', listener: Callback<AccessedDLPFileInfo>): void 300 301监听打开DLP文件。在当前应用的沙箱应用打开DLP文件时,通知当前应用。 302 303**系统能力:** SystemCapability.Security.DataLossPrevention 304 305**参数:** 306 307| 参数名 | 类型 | 必填 | 说明 | 308| -------- | -------- | -------- | -------- | 309| type | 'openDLPFile' | 是 | 监听事件类型。固定值为'openDLPFile':打开DLP文件事件。 | 310| listener | Callback<[AccessedDLPFileInfo](#accesseddlpfileinfo)> | 是 | DLP文件打开事件的回调。在当前应用的沙箱应用打开DLP文件时,通知当前应用。 | 311 312**错误码:** 313 314以下错误码的详细介绍请参见[DLP服务错误码](errorcode-dlp.md)。 315 316| 错误码ID | 错误信息 | 317| -------- | -------- | 318| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 319| 19100001 | Invalid parameter value. | 320| 19100007 | No permission to call this API, which is available only for non-DLP sandbox applications. | 321| 19100011 | The system ability works abnormally. | 322 323**示例:** 324 325```ts 326import { dlpPermission } from '@kit.DataProtectionKit'; 327import { BusinessError } from '@kit.BasicServicesKit'; 328 329try { 330 dlpPermission.on('openDLPFile', (info: dlpPermission.AccessedDLPFileInfo) => { 331 console.info('openDlpFile event', info.uri, info.lastOpenTime) 332 }); // 订阅。 333} catch (err) { 334 console.error('error', (err as BusinessError).code, (err as BusinessError).message); // 失败报错。 335} 336``` 337 338## dlpPermission.off('openDLPFile') 339 340off(type: 'openDLPFile', listener?: Callback<AccessedDLPFileInfo>): void 341 342取消监听打开DLP文件。在当前应用的沙箱应用打开DLP文件时,取消通知当前应用。 343 344**系统能力:** SystemCapability.Security.DataLossPrevention 345 346**参数:** 347| 参数名 | 类型 | 必填 | 说明 | 348| -------- | -------- | -------- | -------- | 349| type | 'openDLPFile' | 是 | 监听事件类型。固定值为'openDLPFile':打开DLP文件事件。 | 350| listener | Callback<[AccessedDLPFileInfo](#accesseddlpfileinfo)> | 否 | DLP文件被打开的事件的回调。在当前应用的沙箱应用打开DLP文件时,取消通知当前应用。默认为空,表示取消该类型事件的所有回调。 | 351 352**错误码:** 353 354以下错误码的详细介绍请参见[DLP服务错误码](errorcode-dlp.md)。 355 356| 错误码ID | 错误信息 | 357| -------- | -------- | 358| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 359| 19100001 | Invalid parameter value. | 360| 19100007 | No permission to call this API, which is available only for non-DLP sandbox applications. | 361| 19100011 | The system ability works abnormally. | 362 363**示例:** 364 365```ts 366import { dlpPermission } from '@kit.DataProtectionKit'; 367import { BusinessError } from '@kit.BasicServicesKit'; 368 369try { 370 dlpPermission.off('openDLPFile', (info: dlpPermission.AccessedDLPFileInfo) => { 371 console.info('openDlpFile event', info.uri, info.lastOpenTime) 372 }); // 取消订阅。 373} catch (err) { 374 console.error('error', (err as BusinessError).code, (err as BusinessError).message); // 失败报错。 375} 376``` 377 378## dlpPermission.isInSandbox 379 380isInSandbox(): Promise<boolean> 381 382查询当前应用是否运行在DLP沙箱环境。使用Promise方式异步返回结果。 383 384**系统能力:** SystemCapability.Security.DataLossPrevention 385 386**返回值:** 387 388| 类型 | 说明 | 389| -------- | -------- | 390| Promise<boolean> | Promise对象。返回true表示当前应用运行在沙箱中,返回false表示当前应用不是运行在沙箱中。 | 391 392**错误码:** 393 394以下错误码的详细介绍请参见[DLP服务错误码](errorcode-dlp.md)。 395 396| 错误码ID | 错误信息 | 397| -------- | -------- | 398| 19100001 | Invalid parameter value. | 399| 19100011 | The system ability works abnormally. | 400 401**示例:** 402 403```ts 404import { dlpPermission } from '@kit.DataProtectionKit'; 405import { BusinessError } from '@kit.BasicServicesKit'; 406 407try { 408 let inSandbox = dlpPermission.isInSandbox(); // 是否在沙箱内。 409 console.info('res', inSandbox); 410} catch (err) { 411 console.error('error', (err as BusinessError).code, (err as BusinessError).message); // 失败报错。 412} 413``` 414 415## dlpPermission.isInSandbox 416 417isInSandbox(callback: AsyncCallback<boolean>): void 418 419查询当前应用是否运行在DLP沙箱环境。使用callback方式异步返回结果。 420 421**系统能力:** SystemCapability.Security.DataLossPrevention 422 423**参数:** 424 425| 参数名 | 类型 | 必填 | 说明 | 426| -------- | -------- | -------- | -------- | 427| callback | AsyncCallback<boolean> | 是 | 回调函数。err为undefine时表示查询成功;否则为错误对象。返回true表示当前应用运行在沙箱中,返回false表示当前应用不是运行在沙箱中。 | 428 429**错误码:** 430 431以下错误码的详细介绍请参见[DLP服务错误码](errorcode-dlp.md)。 432 433| 错误码ID | 错误信息 | 434| -------- | -------- | 435| 401 | Parameter error. Possible causes: 1. Incorrect parameter types. | 436| 19100001 | Invalid parameter value. | 437| 19100011 | The system ability works abnormally. | 438 439**示例:** 440 441```ts 442import { dlpPermission } from '@kit.DataProtectionKit'; 443import { BusinessError } from '@kit.BasicServicesKit'; 444 445try { 446 dlpPermission.isInSandbox((err, data) => { 447 if (err) { 448 console.error('isInSandbox error,', err.code, err.message); 449 } else { 450 console.info('isInSandbox, data', JSON.stringify(data)); 451 } 452 }); // 是否在沙箱内。 453} catch (err) { 454 console.error('isInSandbox error,', (err as BusinessError).code, (err as BusinessError).message); 455} 456``` 457 458## dlpPermission.getDLPSupportedFileTypes 459 460getDLPSupportedFileTypes(): Promise<Array<string>> 461 462查询当前可支持权限设置和校验的文件扩展名类型列表。使用Promise方式异步返回结果。 463 464**系统能力:** SystemCapability.Security.DataLossPrevention 465 466**返回值:** 467 468| 类型 | 说明 | 469| -------- | -------- | 470| Promise<Array<string>> | Promise对象。返回当前可支持权限设置和校验的文件扩展名类型列表。 | 471 472**错误码:** 473 474以下错误码的详细介绍请参见[DLP服务错误码](errorcode-dlp.md)。 475 476| 错误码ID | 错误信息 | 477| -------- | -------- | 478| 19100001 | Invalid parameter value. | 479| 19100011 | The system ability works abnormally. | 480 481**示例:** 482 483```ts 484import { dlpPermission } from '@kit.DataProtectionKit'; 485import { BusinessError } from '@kit.BasicServicesKit'; 486 487try { 488 let res = dlpPermission.getDLPSupportedFileTypes(); // 获取支持DLP的文件类型。 489 console.info('res', JSON.stringify(res)); 490} catch (err) { 491 console.error('error', (err as BusinessError).code, (err as BusinessError).message); // 失败报错。 492} 493``` 494 495## dlpPermission.getDLPSupportedFileTypes 496 497getDLPSupportedFileTypes(callback: AsyncCallback<Array<string>>): void 498 499查询当前可支持权限设置和校验的文件扩展名类型列表。使用callback方式异步返回结果。 500 501**系统能力:** SystemCapability.Security.DataLossPrevention 502 503**参数:** 504 505| 参数名 | 类型 | 必填 | 说明 | 506| -------- | -------- | -------- | -------- | 507| callback | AsyncCallback<Array<string>> | 是 | 回调函数。err为undefine时表示查询成功;否则为错误对象。 | 508 509**错误码:** 510 511以下错误码的详细介绍请参见[DLP服务错误码](errorcode-dlp.md)。 512 513| 错误码ID | 错误信息 | 514| -------- | -------- | 515| 401 | Parameter error. Possible causes: 1. Incorrect parameter types. | 516| 19100001 | Invalid parameter value. | 517| 19100011 | The system ability works abnormally. | 518 519**示例:** 520 521```ts 522import { dlpPermission } from '@kit.DataProtectionKit'; 523import { BusinessError } from '@kit.BasicServicesKit'; 524 525try { 526 dlpPermission.getDLPSupportedFileTypes((err, res) => { 527 if (err != undefined) { 528 console.error('getDLPSupportedFileTypes error,', err.code, err.message); 529 } else { 530 console.info('res', JSON.stringify(res)); 531 } 532 }); // 获取支持DLP的文件类型。 533} catch (err) { 534 console.error('getDLPSupportedFileTypes error,', (err as BusinessError).code, (err as BusinessError).message); 535} 536``` 537 538## dlpPermission.setRetentionState 539 540setRetentionState(docUris: Array<string>): Promise<void> 541 542打开DLP文件时自动安装沙箱,关闭DLP文件时自动卸载沙箱。设置沙箱保留状态时DLP文件关闭时自动卸载暂时失效。使用Promise方式异步返回结果。 543 544**系统能力:** SystemCapability.Security.DataLossPrevention 545 546**参数:** 547 548| 参数名 | 类型 | 必填 | 说明 | 549| -------- | -------- | -------- | -------- | 550| docUris | Array<string> | 是 | 表示需要设置保留状态的文件uri列表。 | 551 552**返回值:** 553 554| 类型 | 说明 | 555| -------- | -------- | 556| Promise<void> | Promise对象。无返回结果的Promise对象。 | 557 558**错误码:** 559 560以下错误码的详细介绍请参见[DLP服务错误码](errorcode-dlp.md)。 561 562| 错误码ID | 错误信息 | 563| -------- | -------- | 564| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 565| 19100001 | Invalid parameter value. | 566| 19100006 | No permission to call this API, which is available only for DLP sandbox applications. | 567| 19100011 | The system ability works abnormally. | 568 569**示例:** 570 571```ts 572import { dlpPermission } from '@kit.DataProtectionKit'; 573import { BusinessError } from '@kit.BasicServicesKit'; 574 575async function ExampleFunction() { 576 let uri = "file://docs/storage/Users/currentUser/Desktop/test.txt.dlp"; 577 try { 578 let inSandbox = await dlpPermission.isInSandbox(); // 是否在沙箱内。 579 if (inSandbox) { 580 dlpPermission.setRetentionState([uri]); // 设置沙箱保留。 581 } 582 } catch (err) { 583 console.error('error', (err as BusinessError).code, (err as BusinessError).message); // 失败报错。 584 } 585} 586``` 587 588## dlpPermission.setRetentionState 589 590setRetentionState(docUris: Array<string>, callback: AsyncCallback<void>): void 591 592打开DLP文件时自动安装沙箱,关闭DLP文件时自动卸载沙箱。设置沙箱保留状态时DLP文件关闭时自动卸载暂时失效。使用callback方式异步返回结果。 593 594**系统能力:** SystemCapability.Security.DataLossPrevention 595 596**参数:** 597 598| 参数名 | 类型 | 必填 | 说明 | 599| -------- | -------- | -------- | -------- | 600| docUris | Array<string> | 是 | 表示需要设置保留状态的文件uri列表。Array不限长度,每个string不超过4095字节。 | 601| callback | AsyncCallback<void> | 是 | 回调函数。err为undefine时表示设置成功;否则为错误对象。 | 602 603**错误码:** 604 605以下错误码的详细介绍请参见[DLP服务错误码](errorcode-dlp.md)。 606 607| 错误码ID | 错误信息 | 608| -------- | -------- | 609| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 610| 19100001 | Invalid parameter value. | 611| 19100006 | No permission to call this API, which is available only for DLP sandbox applications. | 612| 19100011 | The system ability works abnormally. | 613 614**示例:** 615 616```ts 617import { dlpPermission } from '@kit.DataProtectionKit'; 618import { BusinessError } from '@kit.BasicServicesKit'; 619 620let uri = "file://docs/storage/Users/currentUser/Desktop/test.txt.dlp"; 621try { 622 dlpPermission.setRetentionState([uri], (err, res) => { 623 if (err != undefined) { 624 console.error('setRetentionState error,', err.code, err.message); 625 } else { 626 console.info('setRetentionState success'); 627 console.info('res', JSON.stringify(res)); 628 } 629 }); // 设置沙箱保留。 630} catch (err) { 631 console.error('setRetentionState error,', (err as BusinessError).code, (err as BusinessError).message); 632} 633``` 634 635## dlpPermission.cancelRetentionState 636 637cancelRetentionState(docUris: Array<string>): Promise<void> 638 639取消沙箱保留状态即恢复DLP文件关闭时自动卸载沙箱策略。使用Promise方式异步返回结果。 640 641**系统能力:** SystemCapability.Security.DataLossPrevention 642 643**参数:** 644 645| 参数名 | 类型 | 必填 | 说明 | 646| -------- | -------- | -------- | -------- | 647| docUris | Array<string> | 是 | 表示需要设置保留状态的文件uri列表。Array不限长度,每个string不超过4095字节。 | 648 649**返回值:** 650 651| 类型 | 说明 | 652| -------- | -------- | 653| Promise<void> | Promise对象。无返回结果的Promise对象。 | 654 655**错误码:** 656 657以下错误码的详细介绍请参见[DLP服务错误码](errorcode-dlp.md)。 658 659| 错误码ID | 错误信息 | 660| -------- | -------- | 661| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 662| 19100001 | Invalid parameter value. | 663| 19100011 | The system ability works abnormally. | 664 665**示例:** 666 667```ts 668import { dlpPermission } from '@kit.DataProtectionKit'; 669import { BusinessError } from '@kit.BasicServicesKit'; 670 671let uri = "file://docs/storage/Users/currentUser/Desktop/test.txt.dlp"; 672try { 673 dlpPermission.cancelRetentionState([uri]); // 取消沙箱保留。 674} catch (err) { 675 console.error('error', (err as BusinessError).code, (err as BusinessError).message); // 失败报错。 676} 677``` 678 679## dlpPermission.cancelRetentionState 680 681cancelRetentionState(docUris: Array<string>, callback: AsyncCallback<void>): void 682 683取消沙箱保留状态即恢复DLP文件关闭时自动卸载沙箱策略。使用callback方式异步返回结果。 684 685**系统能力:** SystemCapability.Security.DataLossPrevention 686 687**参数:** 688 689| 参数名 | 类型 | 必填 | 说明 | 690| -------- | -------- | -------- | -------- | 691| docUris | Array<string> | 是 | 表示需要设置保留状态的文件uri列表。Array不限长度,每个string不超过4095字节。 | 692| callback | AsyncCallback<void> | 是 | 回调函数。err为undefine时表示设置成功;否则为错误对象。 | 693 694**错误码:** 695 696以下错误码的详细介绍请参见[DLP服务错误码](errorcode-dlp.md)。 697 698| 错误码ID | 错误信息 | 699| -------- | -------- | 700| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 701| 19100001 | Invalid parameter value. | 702| 19100011 | The system ability works abnormally. | 703 704**示例:** 705 706```ts 707import { dlpPermission } from '@kit.DataProtectionKit'; 708import { BusinessError } from '@kit.BasicServicesKit'; 709 710let uri = "file://docs/storage/Users/currentUser/Desktop/test.txt.dlp"; 711try { 712 dlpPermission.cancelRetentionState([uri], (err, res) => { 713 if (err != undefined) { 714 console.error('cancelRetentionState error,', err.code, err.message); 715 } else { 716 console.info('cancelRetentionState success'); 717 } 718 }); // 取消沙箱保留。 719} catch (err) { 720 console.error('cancelRetentionState error,', (err as BusinessError).code, (err as BusinessError).message); 721} 722``` 723 724## dlpPermission.getRetentionSandboxList 725 726getRetentionSandboxList(bundleName?: string): Promise<Array<RetentionSandboxInfo>> 727 728查询指定应用的保留沙箱信息列表。使用Promise方式异步返回结果。 729 730**系统能力:** SystemCapability.Security.DataLossPrevention 731 732**参数:** 733 734| 参数名 | 类型 | 必填 | 说明 | 735| -------- | -------- | -------- | -------- | 736| bundleName | string | 否 | 指定应用包名。默认为空,查询当前应用的保留沙箱信息列表。最小7字节,最大128字节。 | 737 738**返回值:** 739 740| 类型 | 说明 | 741| -------- | -------- | 742| Promise<Array<[RetentionSandboxInfo](#retentionsandboxinfo)>> | Promise对象。返回查询的沙箱信息列表。 | 743 744**错误码:** 745 746以下错误码的详细介绍请参见[DLP服务错误码](errorcode-dlp.md)。 747 748| 错误码ID | 错误信息 | 749| -------- | -------- | 750| 401 | Parameter error. Possible causes: 1. Incorrect parameter types. | 751| 19100001 | Invalid parameter value. | 752| 19100007 | No permission to call this API, which is available only for non-DLP sandbox applications. | 753| 19100011 | The system ability works abnormally. | 754 755**示例:** 756 757```ts 758import { dlpPermission } from '@kit.DataProtectionKit'; 759import { BusinessError } from '@kit.BasicServicesKit'; 760 761async function ExampleFunction() { 762 try { 763 let res: Array<dlpPermission.RetentionSandboxInfo> = await dlpPermission.getRetentionSandboxList(); // 获取沙箱保留列表。 764 console.info('res', JSON.stringify(res)) 765 } catch (err) { 766 console.error('error', (err as BusinessError).code, (err as BusinessError).message); // 失败报错。 767 } 768} 769``` 770 771## dlpPermission.getRetentionSandboxList 772 773getRetentionSandboxList(bundleName: string, callback: AsyncCallback<Array<RetentionSandboxInfo>>): void 774 775查询指定应用的保留沙箱信息列表。使用callback方式异步返回结果。 776 777**系统能力:** SystemCapability.Security.DataLossPrevention 778 779**参数:** 780 781| 参数名 | 类型 | 必填 | 说明 | 782| -------- | -------- | -------- | -------- | 783| bundleName | string | 是 | 指定应用包名。最小7字节,最大128字节。 | 784| callback | AsyncCallback<Array<[RetentionSandboxInfo](#retentionsandboxinfo)>> | 是 | 回调函数。err为undefine时表示查询成功;否则为错误对象。 | 785 786**错误码:** 787 788以下错误码的详细介绍请参见[DLP服务错误码](errorcode-dlp.md)。 789 790| 错误码ID | 错误信息 | 791| -------- | -------- | 792| 401 | Parameter error. Possible causes: 1. Incorrect parameter types. | 793| 19100001 | Invalid parameter value. | 794| 19100007 | No permission to call this API, which is available only for non-DLP sandbox applications. | 795| 19100011 | The system ability works abnormally. | 796 797**示例:** 798 799```ts 800import { dlpPermission } from '@kit.DataProtectionKit'; 801import { BusinessError } from '@kit.BasicServicesKit'; 802 803try { 804 dlpPermission.getRetentionSandboxList("bundleName", (err, res) => { 805 if (err != undefined) { 806 console.error('getRetentionSandboxList error,', err.code, err.message); 807 } else { 808 console.info('res', JSON.stringify(res)); 809 } 810 }); // 获取沙箱保留列表。 811} catch (err) { 812 console.error('getRetentionSandboxList error,', (err as BusinessError).code, (err as BusinessError).message); 813} 814``` 815 816## dlpPermission.getRetentionSandboxList 817 818getRetentionSandboxList(callback: AsyncCallback<Array<RetentionSandboxInfo>>): void 819 820查询指定应用的保留沙箱信息列表。使用callback方式异步返回结果。 821 822**系统能力:** SystemCapability.Security.DataLossPrevention 823 824**参数:** 825 826| 参数名 | 类型 | 必填 | 说明 | 827| -------- | -------- | -------- | -------- | 828| callback | AsyncCallback<Array<[RetentionSandboxInfo](#retentionsandboxinfo)>> | 是 | 回调函数。err为undefine时表示查询成功;否则为错误对象。 | 829 830**错误码:** 831 832以下错误码的详细介绍请参见[DLP服务错误码](errorcode-dlp.md)。 833 834| 错误码ID | 错误信息 | 835| -------- | -------- | 836| 401 | Parameter error. Possible causes: 1. Incorrect parameter types. | 837| 19100001 | Invalid parameter value. | 838| 19100007 | No permission to call this API, which is available only for non-DLP sandbox applications. | 839| 19100011 | The system ability works abnormally. | 840 841**示例:** 842 843```ts 844import { dlpPermission } from '@kit.DataProtectionKit'; 845import { BusinessError } from '@kit.BasicServicesKit'; 846 847try { 848 dlpPermission.getRetentionSandboxList((err, res) => { 849 if (err != undefined) { 850 console.error('getRetentionSandboxList error,', err.code, err.message); 851 } else { 852 console.info('res', JSON.stringify(res)); 853 } 854 }); // 获取沙箱保留列表。 855} catch (err) { 856 console.error('getRetentionSandboxList error,', (err as BusinessError).code, (err as BusinessError).message); 857} 858``` 859 860## dlpPermission.getDLPFileAccessRecords 861 862getDLPFileAccessRecords(): Promise<Array<AccessedDLPFileInfo>> 863 864查询最近访问的DLP文件列表。使用Promise方式异步返回结果。 865 866**系统能力:** SystemCapability.Security.DataLossPrevention 867 868**返回值:** 869 870| 类型 | 说明 | 871| -------- | -------- | 872| Promise<Array<[AccessedDLPFileInfo](#accesseddlpfileinfo)>> | Promise对象。返回最近访问的DLP文件列表。 | 873 874**错误码:** 875 876以下错误码的详细介绍请参见[DLP服务错误码](errorcode-dlp.md)。 877 878| 错误码ID | 错误信息 | 879| -------- | -------- | 880| 19100001 | Invalid parameter value. | 881| 19100007 | No permission to call this API, which is available only for non-DLP sandbox applications. | 882| 19100011 | The system ability works abnormally. | 883 884**示例:** 885 886```ts 887import { dlpPermission } from '@kit.DataProtectionKit'; 888import { BusinessError } from '@kit.BasicServicesKit'; 889 890async function ExampleFunction() { 891 try { 892 let res: Array<dlpPermission.AccessedDLPFileInfo> = await dlpPermission.getDLPFileAccessRecords(); // 获取DLP访问列表。 893 console.info('res', JSON.stringify(res)) 894 } catch (err) { 895 console.error('error', (err as BusinessError).code, (err as BusinessError).message); // 失败报错。 896 } 897} 898``` 899 900## dlpPermission.getDLPFileAccessRecords 901 902getDLPFileAccessRecords(callback: AsyncCallback<Array<AccessedDLPFileInfo>>): void 903 904查询最近访问的DLP文件列表。使用callback方式异步返回结果。 905 906**系统能力:** SystemCapability.Security.DataLossPrevention 907 908**参数:** 909 910| 参数名 | 类型 | 必填 | 说明 | 911| -------- | -------- | -------- | -------- | 912| callback | AsyncCallback<Array<[AccessedDLPFileInfo](#accesseddlpfileinfo)>> | 是 | 回调函数。err为undefine时表示查询成功;否则为错误对象。 | 913 914**错误码:** 915 916以下错误码的详细介绍请参见[DLP服务错误码](errorcode-dlp.md)。 917 918| 错误码ID | 错误信息 | 919| -------- | -------- | 920| 401 | Parameter error. Possible causes: 1. Incorrect parameter types. | 921| 19100001 | Invalid parameter value. | 922| 19100007 | No permission to call this API, which is available only for non-DLP sandbox applications. | 923| 19100011 | The system ability works abnormally. | 924 925**示例:** 926 927```ts 928import { dlpPermission } from '@kit.DataProtectionKit'; 929import { BusinessError } from '@kit.BasicServicesKit'; 930 931try { 932 dlpPermission.getDLPFileAccessRecords((err, res) => { 933 if (err != undefined) { 934 console.error('getDLPFileAccessRecords error,', err.code, err.message); 935 } else { 936 console.info('res', JSON.stringify(res)); 937 } 938 }); // 获取DLP访问列表。 939} catch (err) { 940 console.error('getDLPFileAccessRecords error,', (err as BusinessError).code, (err as BusinessError).message); 941} 942``` 943 944## dlpPermission.startDLPManagerForResult<sup>11+</sup> 945 946startDLPManagerForResult(context: common.UIAbilityContext, want: Want): Promise<DLPManagerResult> 947 948在当前UIAbility界面以无边框形式打开DLP权限管理应用。使用Promise方式异步返回结果。 949 950**模型约束:** 此接口仅可在Stage模型下使用。 951 952**系统能力:** SystemCapability.Security.DataLossPrevention 953 954**参数:** 955 956| 参数名 | 类型 | 必填 | 说明 | 957| -------- | -------- | -------- | -------- | 958| context | [common.UIAbilityContext](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md) | 是 | 当前窗口UIAbility上下文。 | 959| want | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 请求对象。 | 960 961**返回值:** 962 963| 类型 | 说明 | 964| -------- | -------- | 965| Promise<[DLPManagerResult](#dlpmanagerresult11)> | Promise对象。打开DLP权限管理应用并退出后的结果。 | 966 967**错误码:** 968 969以下错误码的详细介绍请参见[DLP服务错误码](errorcode-dlp.md)。 970 971| 错误码ID | 错误信息 | 972| -------- | -------- | 973| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 974| 19100001 | Invalid parameter value. | 975| 19100011 | The system ability works abnormally. | 976| 19100016 | The uri field is missing in the want parameter. | 977| 19100017 | The displayName field is missing in the want parameter. | 978 979**示例:** 980 981```ts 982import { dlpPermission } from '@kit.DataProtectionKit'; 983import { common, Want } from '@kit.AbilityKit'; 984import { UIContext } from '@kit.ArkUI'; 985 986try { 987 let context = new UIContext().getHostContext() as common.UIAbilityContext; // 获取当前UIAbilityContext。 988 let want: Want = { 989 "uri": "file://docs/storage/Users/currentUser/Desktop/1.txt", 990 "parameters": { 991 "displayName": "1.txt" 992 } 993 }; // 请求参数。 994 dlpPermission.startDLPManagerForResult(context, want).then((res) => { 995 console.info('res.resultCode', res.resultCode); 996 console.info('res.want', JSON.stringify(res.want)); 997 }); // 打开DLP权限管理应用。 998} catch (err) { 999 console.error('error', err.code, err.message); // 失败报错。 1000} 1001``` 1002 1003## dlpPermission.setSandboxAppConfig<sup>11+<sup> 1004setSandboxAppConfig(configInfo: string): Promise<void> 1005 1006设置沙箱应用配置信息,使用Promise方式异步返回结果。 1007 1008**系统能力:** SystemCapability.Security.DataLossPrevention 1009 1010**参数:** 1011 1012| 参数名 | 类型 | 必填 | 说明 | 1013| -------- | -------- | -------- | -------- | 1014| configInfo | string | 是 | 沙箱应用配置信息。长度小于4MB。 | 1015 1016**返回值:** 1017 1018| 类型 | 说明 | 1019| -------- | -------- | 1020| Promise<void> | Promise对象。无返回结果的Promise对象。 | 1021 1022**错误码:** 1023 1024以下错误码的详细介绍请参见[DLP服务错误码](errorcode-dlp.md)。 1025 1026| 错误码ID | 错误信息 | 1027| -------- | -------- | 1028| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 1029| 19100001 | Invalid parameter value. | 1030| 19100007 | No permission to call this API, which is available only for non-DLP sandbox applications. | 1031| 19100011 | The system ability works abnormally. | 1032| 19100018 | The application is not authorized. | 1033 1034**示例:** 1035 1036```ts 1037import { dlpPermission } from '@kit.DataProtectionKit'; 1038import { BusinessError } from '@kit.BasicServicesKit'; 1039 1040try { 1041 dlpPermission.setSandboxAppConfig('configInfo'); // 设置沙箱应用配置信息。 1042} catch (err) { 1043 console.error('error', (err as BusinessError).code, (err as BusinessError).message); // 失败报错。 1044} 1045``` 1046 1047## dlpPermission.cleanSandboxAppConfig<sup>11+<sup> 1048cleanSandboxAppConfig(): Promise<void> 1049 1050清理沙箱应用配置信息,使用Promise方式异步返回结果。 1051 1052**系统能力:** SystemCapability.Security.DataLossPrevention 1053 1054**返回值:** 1055 1056| 类型 | 说明 | 1057| -------- | -------- | 1058| Promise<void> | Promise对象。无返回结果的Promise对象。 | 1059 1060**错误码:** 1061 1062以下错误码的详细介绍请参见[DLP服务错误码](errorcode-dlp.md)。 1063 1064| 错误码ID | 错误信息 | 1065| -------- | -------- | 1066| 19100001 | Invalid parameter value. | 1067| 19100007 | No permission to call this API, which is available only for non-DLP sandbox applications. | 1068| 19100011 | The system ability works abnormally. | 1069| 19100018 | The application is not authorized. | 1070 1071**示例:** 1072 1073```ts 1074import { dlpPermission } from '@kit.DataProtectionKit'; 1075import { BusinessError } from '@kit.BasicServicesKit'; 1076 1077try { 1078 dlpPermission.cleanSandboxAppConfig(); // 清理沙箱应用配置信息。 1079} catch (err) { 1080 console.error('error', (err as BusinessError).code, (err as BusinessError).message); // 失败报错。 1081} 1082``` 1083 1084## dlpPermission.getSandboxAppConfig<sup>11+<sup> 1085getSandboxAppConfig(): Promise<string> 1086 1087获取沙箱应用配置信息,使用Promise方式异步返回结果。 1088 1089**系统能力:** SystemCapability.Security.DataLossPrevention 1090 1091**返回值:** 1092| 类型 | 说明 | 1093| -------- | -------- | 1094| Promise<string> | Promise对象。返回沙箱应用配置信息。 | 1095 1096**错误码:** 1097 1098以下错误码的详细介绍请参见[DLP服务错误码](errorcode-dlp.md)。 1099 1100| 错误码ID | 错误信息 | 1101| -------- | -------- | 1102| 19100001 | Invalid parameter value. | 1103| 19100011 | The system ability works abnormally. | 1104| 19100018 | The application is not authorized. | 1105 1106**示例:** 1107 1108```ts 1109import { dlpPermission } from '@kit.DataProtectionKit'; 1110import { BusinessError } from '@kit.BasicServicesKit'; 1111 1112async function ExampleFunction() { 1113 try { 1114 let res = await dlpPermission.getSandboxAppConfig() // 获取沙箱应用配置信息。 1115 console.info('res', JSON.stringify(res)); 1116 } catch (err) { 1117 console.error('error', (err as BusinessError).code, (err as BusinessError).message); // 失败报错。 1118 } 1119} 1120``` 1121 1122## dlpPermission.isDLPFeatureProvided<sup>12+<sup> 1123isDLPFeatureProvided(): Promise<boolean> 1124 1125查询当前系统是否提供DLP特性,使用Promise方式异步返回结果。 1126 1127**系统能力:** SystemCapability.Security.DataLossPrevention 1128 1129**返回值:** 1130| 类型 | 说明 | 1131| -------- | -------- | 1132| Promise<boolean> | Promise对象。返回true表示当前系统提供DLP特性,返回false表示不提供DLP特性。 | 1133 1134**错误码:** 1135 1136以下错误码的详细介绍请参见[DLP服务错误码](errorcode-dlp.md)。 1137 1138| 错误码ID | 错误信息 | 1139| -------- | -------- | 1140| 19100011 | The system ability works abnormally. | 1141 1142**示例:** 1143 1144```ts 1145import { dlpPermission } from '@kit.DataProtectionKit'; 1146import { BusinessError } from '@kit.BasicServicesKit'; 1147 1148dlpPermission.isDLPFeatureProvided().then((res) => { 1149 console.info('res', JSON.stringify(res)); 1150}).catch((err: BusinessError) => { 1151 console.error('error', (err as BusinessError).code, (err as BusinessError).message); // 失败报错。 1152}); 1153``` 1154 1155## ActionFlagType 1156 1157可以对DLP文件进行的操作类型枚举。例如:DLP沙箱应用可以根据是否具有操作权限,对其按钮进行置灰。 1158 1159**系统能力:** SystemCapability.Security.DataLossPrevention 1160 1161| 名称 | 值 | 说明 | 1162| -------- | -------- | -------- | 1163| ACTION_VIEW | 0x00000001 | 表示文件的查看权限。 | 1164| ACTION_SAVE | 0x00000002 | 表示文件的保存权限。 | 1165| ACTION_SAVE_AS | 0x00000004 | 表示文件的另存为权限。 | 1166| ACTION_EDIT | 0x00000008 | 表示文件的编辑权限。 | 1167| ACTION_SCREEN_CAPTURE | 0x00000010 | 表示文件的截屏权限。 | 1168| ACTION_SCREEN_SHARE | 0x00000020 | 表示文件的共享屏幕权限。 | 1169| ACTION_SCREEN_RECORD | 0x00000040 | 表示文件的录屏权限。 | 1170| ACTION_COPY | 0x00000080 | 表示文件的复制权限。 | 1171| ACTION_PRINT | 0x00000100 | 表示文件的打印权限。 | 1172| ACTION_EXPORT | 0x00000200 | 表示文件的导出权限。 | 1173| ACTION_PERMISSION_CHANGE | 0x00000400 | 表示文件的修改文件权限。 | 1174 1175## DLPFileAccess 1176 1177DLP文件授权类型的枚举。 1178 1179**系统能力:** SystemCapability.Security.DataLossPrevention 1180 1181| 名称 | 值 | 说明 | 1182| -------- | -------- | -------- | 1183| NO_PERMISSION | 0 | 表示无文件权限。 | 1184| READ_ONLY | 1 | 表示文件的只读权限。 | 1185| CONTENT_EDIT | 2 | 表示文件的编辑权限。 | 1186| FULL_CONTROL | 3 | 表示文件的完全控制权限。 | 1187 1188## DLPPermissionInfo 1189 1190表示DLP文件的权限信息。 1191 1192**系统能力:** SystemCapability.Security.DataLossPrevention 1193 1194| 名称 | 类型 | 只读 | 可选 | 说明 | 1195| -------- | -------- | -------- | -------- | -------- | 1196| dlpFileAccess | [DLPFileAccess](#dlpfileaccess) | 否 | 否 | 表示DLP文件针对用户的授权类型,例如:只读。 | 1197| flags | number | 否 | 否 | 表示DLP文件的详细操作权限,是不同[ActionFlagType](#actionflagtype)的组合。 | 1198 1199## AccessedDLPFileInfo 1200 1201表示被打开的DLP文件的信息。 1202 1203**系统能力:** SystemCapability.Security.DataLossPrevention 1204 1205| 名称 | 类型 | 只读 | 可选 | 说明 | 1206| -------- | -------- | -------- | -------- | -------- | 1207| uri | string | 否 | 否 | 表示DLP文件的uri。不超过4095字节。 | 1208| lastOpenTime | number | 否 | 否 | 表示DLP文件最近打开时间。 | 1209 1210## DLPManagerResult<sup>11+</sup> 1211 1212表示打开DLP权限管理应用的结果。 1213 1214**模型约束:** 此接口仅可在Stage模型下使用。 1215 1216**系统能力:** SystemCapability.Security.DataLossPrevention 1217 1218| 名称 | 类型 | 只读 | 可选 | 说明 | 1219| -------- | -------- | -------- | -------- | -------- | 1220| resultCode | number | 否 | 否 | 表示打开DLP权限管理应用并退出后返回的结果码。 | 1221| want | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 否 | 否 | 表示打开DLP权限管理应用并退出后返回的数据。 | 1222 1223## RetentionSandboxInfo 1224 1225保留沙箱的沙箱信息。 1226 1227**系统能力:** SystemCapability.Security.DataLossPrevention 1228 1229| 名称 | 类型 | 只读 | 可选 | 说明 | 1230| -------- | -------- | -------- | -------- | -------- | 1231| appIndex | number | 否 | 否 | 表示DLP沙箱应用索引。 | 1232| bundleName | string | 否 | 否 | 表示应用包名。最小7字节,最大128字节。 | 1233| docUris | Array<string> | 否 | 否 | 表示DLP文件的URI列表。Array不限长度,每个string不超过4095字节。 | 1234 1235