1 /*
2  * Copyright (c) 2022-2023 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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_XCOMPONENT_XCOMPONENT_MODEL_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_XCOMPONENT_XCOMPONENT_MODEL_H
18 
19 #include <mutex>
20 #include <optional>
21 
22 #include "base/memory/ace_type.h"
23 #include "core/common/container.h"
24 #include "core/components/common/layout/constants.h"
25 #include "core/components_ng/pattern/xcomponent/inner_xcomponent_controller.h"
26 
27 namespace OHOS::Ace {
28 using LoadEvent = std::function<void(const std::string&)>;
29 using DestroyEvent = std::function<void(const std::string&)>;
30 using DetachCallback = std::function<void(const std::string&)>;
31 using SurfaceCreatedEvent = std::function<void(const std::string&, const std::string&)>;
32 using SurfaceChangedEvent = std::function<void(const std::string&, const NG::RectF&)>;
33 using SurfaceDestroyedEvent = std::function<void(const std::string&, const std::string&)>;
34 
35 class XComponentModel {
36 public:
37     static XComponentModel* GetInstance();
IsBackGroundColorAvailable(const XComponentType & type)38     static bool IsBackGroundColorAvailable(const XComponentType& type)
39     {
40         return type == XComponentType::TEXTURE || type == XComponentType::NODE ||
41                (type == XComponentType::SURFACE && Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_ELEVEN));
42     }
IsCommonEventAvailable(const XComponentType & type,std::optional<std::string> & libraryName)43     static bool IsCommonEventAvailable(const XComponentType& type, std::optional<std::string>& libraryName)
44     {
45         return type == XComponentType::NODE ||
46                (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_TWELVE) && !libraryName.has_value());
47     }
48     virtual ~XComponentModel() = default;
49 
50     virtual void Create(const std::optional<std::string>& id, XComponentType type,
51         const std::optional<std::string>& libraryname,
52         const std::shared_ptr<InnerXComponentController>& xcomponentController) = 0;
Create(int32_t,float,float,const std::string &,XComponentType,const std::string &,const std::shared_ptr<InnerXComponentController> &)53     virtual RefPtr<AceType> Create(int32_t /* nodeId */, float /* width */, float /* height */,
54         const std::string& /* id */, XComponentType /* type */, const std::string& /* libraryname */,
55         const std::shared_ptr<InnerXComponentController>& /* xcomponentController */)
56     {
57         return nullptr;
58     };
59     virtual void SetSoPath(const std::string& soPath) = 0;
60     virtual void SetOnLoad(LoadEvent&& onLoad) = 0;
61     virtual void SetOnDestroy(DestroyEvent&& onDestroy) = 0;
RegisterOnCreate(const RefPtr<AceType> & node,LoadEvent && onLoad)62     virtual void RegisterOnCreate(const RefPtr<AceType>& node, LoadEvent&& onLoad) {};
RegisterOnDestroy(const RefPtr<AceType> & node,DestroyEvent && onDestroy)63     virtual void RegisterOnDestroy(const RefPtr<AceType>& node, DestroyEvent&& onDestroy) {};
IsTexture()64     virtual bool IsTexture()
65     {
66         return false;
67     }
SetDetachCallback(DetachCallback && onDetach)68     virtual void SetDetachCallback(DetachCallback&& onDetach) {}
GetType()69     virtual XComponentType GetType()
70     {
71         return XComponentType::UNKNOWN;
72     }
SetControllerOnCreated(SurfaceCreatedEvent && onCreated)73     virtual void SetControllerOnCreated(SurfaceCreatedEvent&& onCreated) {}
SetControllerOnChanged(SurfaceChangedEvent && onChanged)74     virtual void SetControllerOnChanged(SurfaceChangedEvent&& onChanged) {}
SetControllerOnDestroyed(SurfaceDestroyedEvent && onDestroyed)75     virtual void SetControllerOnDestroyed(SurfaceDestroyedEvent&& onDestroyed) {}
GetLibraryName()76     virtual std::optional<std::string> GetLibraryName()
77     {
78         return std::nullopt;
79     }
EnableAnalyzer(bool enable)80     virtual void EnableAnalyzer(bool enable) {}
SetImageAIOptions(void * options)81     virtual void SetImageAIOptions(void* options) {}
SetRenderFit(RenderFit renderFit)82     virtual void SetRenderFit(RenderFit renderFit) {}
EnableSecure(bool isSecure)83     virtual void EnableSecure(bool isSecure) {}
84 
85 private:
86     static std::unique_ptr<XComponentModel> instance_;
87     static std::mutex mutex_;
88 };
89 
90 } // namespace OHOS::Ace
91 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_XCOMPONENT_XCOMPONENT_MODEL_H
92