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 OHOS_RENDER_3D_PROPERTY_PROXY_H
17 #define OHOS_RENDER_3D_PROPERTY_PROXY_H
18 #include <meta/interface/intf_event.h>
19 #include <meta/interface/intf_task_queue.h>
20 #include <meta/interface/property/intf_property.h>
21 #include <meta/interface/property/property_events.h>
22 #include <napi_api.h>
23 
24 #include <base/containers/string.h>
25 #include <base/containers/string_view.h>
26 #include <base/containers/vector.h>
27 
28 class PropertyProxy {
29     BASE_NS::vector<class PropCtx*> accessors;
30 
31 protected:
32     CORE_NS::SpinLock duh;
33     META_NS::IProperty::Ptr prop_;
34     NapiApi::StrongRef obj;
35     void Create(napi_env env, const BASE_NS::string jsName);
36     void Hook(const BASE_NS::string member);
37     META_NS::IOnChanged::InterfaceTypePtr changeHandler_;
38     META_NS::IEvent::Token changeToken_;
39     META_NS::ITaskQueueTask::Ptr updateTask_;
40     META_NS::ITaskQueue::Token updateToken_ { nullptr };
41     virtual void UpdateLocalValues() = 0;
42     virtual void UpdateRemoteValues() = 0;
43     void UpdateLocal();
44     int32_t UpdateRemote();
45     void ScheduleUpdate();
46 
47     // synchronized get value.
48     void SyncGet();
49     BASE_NS::string name_;
50 public:
51     operator napi_value();
52     explicit PropertyProxy(META_NS::IProperty::Ptr prop);
53     virtual ~PropertyProxy();
54 
55     // copy data from jsobject
56     virtual bool SetValue(NapiApi::Object data) = 0;
57     NapiApi::Value<NapiApi::Object> Value();
58     virtual void SetValue(NapiApi::FunctionContext<>& info, BASE_NS::string_view memb) = 0;
59     virtual napi_value GetValue(NapiApi::FunctionContext<>& info, BASE_NS::string_view memb) = 0;
60     const META_NS::IProperty::Ptr& GetProperty() const;
61 };
62 #endif // OHOS_RENDER_3D_PROPERTY_PROXY_H
63