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 uri from '@ohos.uri'; 17 18class Cleaner { 19 static callback(cleaner: Cleaner): void { 20 console.println("enter Cleaner.callback"); 21 cleaner.clean() 22 } 23 24 constructor(targetPtr: long) { 25 this.targetPtr = targetPtr 26 } 27 28 native clean(): void 29 30 private targetPtr: long = 0 31}; 32 33class FinalizationAgent<T extends Object> { 34 constructor(obj: T, ptr: long) { 35 this.register(obj, ptr); 36 } 37 38 register(obj: T, ptr: long): void { 39 if (this.unregisterToken) { 40 this.unregister(); 41 } 42 this.unregisterToken = {}; 43 this.cleaner = new Cleaner(ptr); 44 finalizer.register(obj, this.cleaner!, this.unregisterToken); 45 } 46 47 unregister(): void { 48 finalizer.unregister(this.unregisterToken); 49 } 50 51 private cleaner: Cleaner | null = null; 52 private unregisterToken: object = {}; 53} 54 55let finalizer = new FinalizationRegistry<Cleaner>(Cleaner.callback) 56 57export default namespace fileUri { 58 loadLibrary("fileuri_ani") 59 60 export class FileUri { 61 native constructor(uriOrPath: string); 62 getFileUriEntity():long { 63 return this.fileUriEntity_; 64 } 65 66 private fileUriEntity_: long = 0; 67 private acquireFileUriEntity(fileUriEntity: long) { 68 this.fileUriEntity_ = fileUriEntity; 69 if (this.fzAgent) { 70 this.fzAgent.unregister(); 71 } 72 this.fzAgent = new FinalizationAgent<FileUri>(this, this.fileUriEntity_); 73 } 74 75 unregisterCleaner(): void { 76 this.fzAgent.unregister(); 77 } 78 79 private fzAgent: FinalizationAgent<FileUri>; 80 }; 81 export native function getUriFromPath(path: string): string; 82} 83