• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 */
15import image from "@ohos.multimedia.image";
16import Want from "@ohos.app.ability.Want";
17
18type ValueType = number | string | boolean | image.PixelMap | Want | ArrayBuffer | object | null | undefined;
19
20 export default namespace unifiedDataChannel {
21    export class Cleaner {
22        static { loadLibrary("unifieddatachannel_ani") }
23        static callback(cleaner: Cleaner): void {
24            cleaner.clean()
25        }
26        constructor(targetPtr: long) {
27            this.targetPtr = targetPtr
28        }
29
30        native clean(): void
31
32        private targetPtr: long = 0
33    }
34    class FinalizationAgent<T extends Object> {
35        constructor(obj: T, ptr: long) {
36            this.register(obj, ptr);
37        }
38
39        register(obj: T, ptr: long): void {
40            this.unregisterToken = {};
41            this.cleaner = new Cleaner(ptr);
42            finalizer.register(obj, this.cleaner!, this.unregisterToken);
43        }
44
45        unregister(): void {
46            finalizer.unregister(this.unregisterToken);
47        }
48
49        private cleaner: Cleaner | null = null;
50        private unregisterToken: object;
51    }
52
53    let finalizer = new FinalizationRegistry<Cleaner>(Cleaner.callback)
54    export class UnifiedRecord {
55        static { loadLibrary("unifieddatachannel_ani") }
56
57        constructor(type?: string, value?: ValueType) {
58            if (type == undefined) {
59                this.initRecordWithOutTypeValue();
60            } else {
61                this.init(type, value);
62            }
63            this.registerCleaner();
64        }
65
66        registerCleaner(): void {
67            this.fzAgent = new FinalizationAgent<UnifiedRecord>(this, this.nativePtr);
68        }
69
70        unregisterCleaner(): void {
71            this.fzAgent.unregister();
72        }
73        setNativePtr(context: long) {
74            this.nativePtr = context;
75        }
76
77        private fzAgent: FinalizationAgent<UnifiedRecord>;
78        native init(type: string, value: ValueType): void;
79        native initRecordWithOutTypeValue(): void;
80        private nativePtr: long = 0;
81    }
82
83    export class UnifiedData {
84        static { loadLibrary("unifieddatachannel_ani") }
85        constructor(record?: UnifiedRecord) {
86            if (record == undefined) {
87                this.initDataWithOutRecord();
88            } else {
89                this.initData(record);
90            }
91            this.registerCleaner();
92        }
93
94        registerCleaner(): void {
95            this.fzAgent = new FinalizationAgent<UnifiedData>(this, this.nativePtr);
96        }
97
98        unregisterCleaner(): void {
99            this.fzAgent.unregister();
100        }
101        native initData(record: UnifiedRecord): void;
102        native initDataWithOutRecord(): void;
103        native getRecords(): Array<UnifiedRecord>;
104
105        getNativePtr(): long {
106            return this.nativePtr;
107        }
108
109        private fzAgent: FinalizationAgent<UnifiedData>;
110        private nativePtr: long = 0;
111    }
112}