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 16 #ifndef PROPERTY_PROXY_H 17 #define PROPERTY_PROXY_H 18 #include <meta/interface/intf_event.h> 19 #include <meta/interface/intf_task_queue.h> 20 #include <meta/interface/property/property.h> 21 #include <meta/interface/property/property_events.h> 22 #include <meta/api/util.h> 23 #include <napi_api.h> 24 25 #include <scene/interface/intf_image.h> 26 #include <base/containers/string.h> 27 #include <base/containers/string_view.h> 28 #include <base/containers/vector.h> 29 30 class PropertyProxy { 31 public: 32 explicit PropertyProxy(META_NS::IProperty::Ptr prop); 33 virtual ~PropertyProxy(); 34 35 virtual void SetValue(NapiApi::FunctionContext<>& info) = 0; 36 virtual napi_value Value() = 0; 37 virtual void Reset() = 0; 38 39 virtual void SetExtra(const BASE_NS::shared_ptr<CORE_NS::IInterface>); 40 virtual const BASE_NS::shared_ptr<CORE_NS::IInterface> GetExtra() const noexcept; 41 protected: 42 /// Returns a Property<Type> instance from underlying property 43 template<typename Type> GetProperty()44 META_NS::Property<Type> GetProperty() const 45 { 46 return META_NS::Property<Type>(prop_); 47 } 48 /// Returns the underlying property ptr 49 META_NS::IProperty::Ptr GetPropertyPtr() const noexcept; 50 private: 51 BASE_NS::weak_ptr<CORE_NS::IInterface> extra_; 52 META_NS::IProperty::Ptr prop_; 53 }; 54 55 class ObjectPropertyProxy : public PropertyProxy { 56 public: 57 explicit ObjectPropertyProxy(META_NS::IProperty::Ptr prop); 58 ~ObjectPropertyProxy(); 59 void Reset() override; 60 // copy data from jsobject 61 void SetValue(NapiApi::FunctionContext<>& info) override; 62 napi_value Value() override; 63 64 virtual void SetValue(NapiApi::Object obj) = 0; 65 66 virtual void SetMemberValue(NapiApi::FunctionContext<>& info, BASE_NS::string_view memb) = 0; 67 virtual napi_value GetMemberValue(const NapiApi::Env info, BASE_NS::string_view memb) = 0; 68 69 void Create(napi_env env, const BASE_NS::string jsName); 70 void Hook(const BASE_NS::string member); 71 void Destroy(); 72 NapiApi::StrongRef obj_; 73 74 class MemberProxy { 75 public: 76 MemberProxy(ObjectPropertyProxy* p, BASE_NS::string m); 77 ~MemberProxy(); 78 const BASE_NS::string_view Name() const; 79 static napi_value Getter(napi_env e, napi_callback_info i); 80 static napi_value Setter(napi_env e, napi_callback_info i); 81 82 private: 83 ObjectPropertyProxy* proxy_; 84 BASE_NS::string memb_; 85 }; 86 87 BASE_NS::vector<BASE_NS::unique_ptr<MemberProxy>> accessors_; 88 }; 89 90 class EntityProxy : public PropertyProxy { 91 public: 92 EntityProxy(NapiApi::Object scn, NapiApi::Object obj, META_NS::Property<CORE_NS::Entity> prop); 93 ~EntityProxy() override; 94 void Reset() override; 95 96 protected: 97 napi_value Value() override; 98 void SetValue(NapiApi::FunctionContext<>& info) override; 99 void SetValue(const CORE_NS::Entity v); 100 NapiApi::WeakRef obj_; 101 NapiApi::WeakRef scene_; 102 }; 103 104 class ImageProxy : public PropertyProxy { 105 public: 106 ImageProxy(NapiApi::Object scn, NapiApi::Object obj, META_NS::Property<SCENE_NS::IImage::Ptr> prop); 107 ~ImageProxy() override; 108 void Reset() override; 109 110 protected: 111 napi_value Value() override; 112 void SetValue(NapiApi::FunctionContext<>& info) override; 113 void SetValue(const SCENE_NS::IImage::Ptr& v); 114 NapiApi::WeakRef obj_; 115 NapiApi::WeakRef scene_; 116 }; 117 118 template<typename Type> 119 class TypeProxy : public PropertyProxy { 120 public: TypeProxy(NapiApi::Object obj,META_NS::Property<Type> prop)121 TypeProxy(NapiApi::Object obj, META_NS::Property<Type> prop) : PropertyProxy(prop), obj_(obj) {} ~TypeProxy()122 ~TypeProxy() override 123 { 124 Reset(); 125 } Reset()126 void Reset() override 127 { 128 if (!obj_.IsEmpty()) { 129 if (auto prop = GetPropertyPtr()) { 130 obj_.GetObject().DeleteProperty(prop->GetName()); 131 } 132 } 133 obj_.Reset(); 134 } 135 136 protected: Value()137 napi_value Value() override 138 { 139 auto value = META_NS::GetValue(GetProperty<Type>()); 140 if constexpr (BASE_NS::is_same_v<Type, BASE_NS::string>) { 141 return NapiApi::Env(obj_.GetEnv()).GetString(value); 142 } else if constexpr (BASE_NS::is_same_v<Type, bool>) { 143 return NapiApi::Env(obj_.GetEnv()).GetBoolean(value); 144 } else { 145 return NapiApi::Env(obj_.GetEnv()).GetNumber(value); 146 } 147 } SetValue(NapiApi::FunctionContext<> & info)148 void SetValue(NapiApi::FunctionContext<>& info) override 149 { 150 NapiApi::FunctionContext<Type> data { info }; 151 if (data) { 152 Type value = data.template Arg<0>(); 153 META_NS::SetValue(GetProperty<Type>(), value); 154 } 155 } SetValue(const Type & v)156 void SetValue(const Type& v) 157 { 158 META_NS::SetValue(GetProperty<Type>(), v); 159 } 160 161 private: 162 NapiApi::WeakRef obj_; 163 }; 164 165 166 BASE_NS::shared_ptr<PropertyProxy> PropertyToProxy( 167 168 NapiApi::Object scene, NapiApi::Object obj, const META_NS::IProperty::Ptr& t); 169 170 napi_property_descriptor CreateProxyDesc(const char* name, BASE_NS::shared_ptr<PropertyProxy> proxy); 171 172 #endif 173