1/* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import type { AsyncCallback } from '@ohos.base'; 17import { BusinessError } from '@ohos.base'; 18import type wantConstant from '@ohos.app.ability.wantConstant'; 19import hilog from '@ohos.hilog' 20 21export namespace fileShare { 22 loadLibrary("fileshare_ani") 23 24 export enum OperationMode { 25 READ_MODE = 0b1, 26 WRITE_MODE = 0b10 27 } 28 export interface PolicyInfo { 29 uri: string; 30 operationMode: int; 31 } 32 33 class PolicyInfo_inner implements PolicyInfo { 34 uri: string; 35 operationMode: int; 36 } 37 38 export function grantUriPermission(uri: string, bundleName: string, flag: wantConstant.Flags, 39 callback: AsyncCallback<void>): void { 40 hilog.info(0x0000, 'grantUriPermission', "Start grantUriPermission in callback main thread."); 41 let p1 = taskpool.execute(grantUriPermissionSync, uri, bundleName, flag); 42 p1.then((data: NullishType) => { 43 let ret = data as int; 44 hilog.info(0x0000, 'grantUriPermission', "grantUriPermission ret = " + ret); 45 let error: BusinessError<void>; 46 callback(error, undefined); 47 }); 48 p1.catch((err: NullishType) => { 49 hilog.info(0x0000, 'grantUriPermission', "grantUriPermission catch in callback thread."); 50 let error = err as BusinessError<void>; 51 callback(error, undefined); 52 }); 53 } 54 55 export function grantUriPermission(uri: string, bundleName: string, flag: wantConstant.Flags): Promise<void> { 56 hilog.info(0x0000, 'grantUriPermission', "Start grantUriPermission in promise main thread"); 57 let p = new Promise<void>((resolve: (value: undefined) => void, reject: (error: Object) => void): void => { 58 let p1 = taskpool.execute(grantUriPermissionSync, uri, bundleName, flag); 59 p1.then((e: NullishType): void => { 60 resolve(undefined); 61 }); 62 p1.catch((e: Error): void => { 63 hilog.info(0x0000, 'grantUriPermission', "grantUriPermission catch in promise thread."); 64 reject(e); 65 }); 66 }); 67 return p; 68 } 69 70 export function grantUriPermissionSync(uri: string, bundleName: string, flag: wantConstant.Flags): int { 71 try { 72 grantUriPermissionInner(uri, bundleName, flag); 73 } catch (error) { 74 hilog.info(0x0000, 'grantUriPermission', "grantUriPermissionInner catch error"); 75 return -1; 76 } 77 return 0; 78 } 79 native function grantUriPermissionInner(uri: string, bundleName: string, flag: wantConstant.Flags):void; 80} 81