1/* 2 * Copyright (c) 2024 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 16class ComponentContent extends Content implements IDisposable { 17 // the name of "builderNode_" is used in ace_engine/interfaces/native/node/native_node_napi.cpp. 18 private builderNode_: BuilderNode; 19 private attachNodeRef_: NativeStrongRef; 20 private parentWeak_: WeakRef<FrameNode> | undefined; 21 private disposable_: Disposable; 22 constructor(uiContext: UIContext, builder: WrappedBuilder<[]> | WrappedBuilder<[Object]>, params?: Object, options?: BuildOptions) { 23 super(); 24 let builderNode = new BuilderNode(uiContext, {}); 25 this.builderNode_ = builderNode; 26 this.builderNode_.build(builder, params ?? undefined, options); 27 this.disposable_ = new Disposable(); 28 } 29 30 public update(params: Object) { 31 this.builderNode_.update(params); 32 } 33 34 public getFrameNode(): FrameNode | null | undefined { 35 return this.builderNode_.getFrameNodeWithoutCheck(); 36 } 37 public setAttachedParent(parent: WeakRef<FrameNode> | undefined) { 38 this.parentWeak_ = parent; 39 } 40 public getNodePtr(): NodePtr { 41 if (this.attachNodeRef_ !== undefined) { 42 return this.attachNodeRef_.getNativeHandle(); 43 } 44 return this.builderNode_.getNodePtr(); 45 } 46 public reuse(param: Object): void { 47 this.builderNode_.reuse(param); 48 } 49 public recycle(): void { 50 this.builderNode_.recycle(); 51 } 52 public onReuseWithBindObject(param?: Object): void { 53 this.builderNode_.onReuseWithBindObject(param); 54 } 55 public onRecycleWithBindObject(): void { 56 this.builderNode_.onRecycleWithBindObject(); 57 } 58 public dispose(): void { 59 this.disposable_.dispose(); 60 if (this.getNodePtr()) { 61 getUINativeModule().frameNode.fireArkUIObjectLifecycleCallback(new WeakRef(this), 'ComponentContent', this.getFrameNode()?.getNodeType() || 'ComponentContent', this.getNodePtr()); 62 } 63 this.detachFromParent(); 64 this.attachNodeRef_?.dispose(); 65 this.builderNode_?.dispose(); 66 } 67 68 public isDisposed(): boolean { 69 return this.disposable_.isDisposed() && (this.builderNode_ ? this.builderNode_.isDisposed() : true); 70 } 71 72 public detachFromParent() { 73 if (this.parentWeak_ === undefined) { 74 return; 75 } 76 let parent = this.parentWeak_.deref(); 77 if (parent !== undefined) { 78 parent.removeComponentContent(this); 79 } 80 } 81 82 public getNodeWithoutProxy(): NodePtr { 83 const node = this.getNodePtr(); 84 const nodeType = getUINativeModule().frameNode.getNodeType(node); 85 if (nodeType === "BuilderProxyNode") { 86 const result = getUINativeModule().frameNode.getFirstUINode(node); 87 this.attachNodeRef_ = getUINativeModule().nativeUtils.createNativeStrongRef(result); 88 getUINativeModule().frameNode.removeChild(node, result); 89 return result; 90 } 91 return node; 92 } 93 94 public updateConfiguration(): void { 95 this.builderNode_.updateConfiguration(); 96 } 97 98 public inheritFreezeOptions(enable: boolean): void { 99 this.builderNode_.inheritFreezeOptions(enable); 100 } 101} 102