1 /*
2  * Copyright (c) 2021-2022 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_PLUGIN_SUB_CONTAINER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_PLUGIN_SUB_CONTAINER_H
18 
19 #include "base/thread/task_executor.h"
20 #include "bridge/plugin_frontend/plugin_frontend.h"
21 #include "core/components_ng/pattern/plugin/plugin_node.h"
22 #include "core/components_ng/pattern/plugin/plugin_pattern.h"
23 #include "core/pipeline/pipeline_base.h"
24 #include "core/pipeline/pipeline_context.h"
25 #include "core/pipeline_ng/pipeline_context.h"
26 
27 namespace OHOS::Ace {
28 class ACE_EXPORT PluginSubContainer : public virtual AceType {
29     DECLARE_ACE_TYPE(PluginSubContainer, AceType);
30 
31 public:
32     using OnPluginAcquiredCallback = std::function<void(const size_t)>;
33     using onPluginUpdateWithValueParams = std::function<void(const std::string&)>;
34 
PluginSubContainer(const WeakPtr<PipelineBase> & context)35     explicit PluginSubContainer(const WeakPtr<PipelineBase>& context) : outSidePipelineContext_(context) {}
PluginSubContainer(const WeakPtr<PipelineBase> & context,int32_t instanceId)36     PluginSubContainer(const WeakPtr<PipelineBase>& context, int32_t instanceId)
37         : outSidePipelineContext_(context), instanceId_(instanceId)
38     {}
39     ~PluginSubContainer() = default;
40 
41     void Initialize();
42     void RunPlugin(const std::string& path, const std::string& module, const std::string& source,
43         const std::string& moduleResPath, const std::string& data);
44     void RunDecompressedPlugin(const std::string& path, const std::string& module, const std::string& source,
45         const std::string& moduleResPath, const std::string& data);
46     void UpdatePlugin(const std::string& content);
47     void Destroy();
48 
SetPluginElement(const WeakPtr<Element> & element)49     void SetPluginElement(const WeakPtr<Element>& element)
50     {
51         pluginElement_ = element;
52     }
53 
GetPluginElement()54     const WeakPtr<Element> GetPluginElement() const
55     {
56         return pluginElement_;
57     }
58 
SetPluginComponent(const RefPtr<Component> & mountPoint)59     void SetPluginComponent(const RefPtr<Component>& mountPoint)
60     {
61         pluginComponent_ = mountPoint;
62     }
63 
GetTaskExecutor()64     RefPtr<TaskExecutor> GetTaskExecutor() const
65     {
66         return taskExecutor_;
67     }
68 
GetPipelineContext()69     RefPtr<PipelineBase> GetPipelineContext() const
70     {
71         return pipelineContext_;
72     }
73 
74     void UpdateRootElementSize();
75     void UpdateSurfaceSize();
76 
AddPluginAcquireCallback(const OnPluginAcquiredCallback & callback)77     void AddPluginAcquireCallback(const OnPluginAcquiredCallback& callback)
78     {
79         if (callback) {
80             onPluginAcquiredCallback_ = callback;
81         }
82     }
83 
SetAllowUpdate(bool update)84     void SetAllowUpdate(bool update)
85     {
86         allowUpdate_ = update;
87     }
88 
SetInstanceId(int32_t instanceId)89     void SetInstanceId(int32_t instanceId)
90     {
91         instanceId_ = instanceId;
92     }
93 
GetInstanceId()94     int32_t GetInstanceId() const
95     {
96         return instanceId_;
97     }
98 
SetPluginPattern(const WeakPtr<NG::PluginPattern> & pluginPattern)99     void SetPluginPattern(const WeakPtr<NG::PluginPattern>& pluginPattern)
100     {
101         pluginPattern_ = pluginPattern;
102     }
103 
GetPluginPattern()104     RefPtr<NG::PluginPattern> GetPluginPattern() const
105     {
106         return pluginPattern_.Upgrade();
107     }
108 
SetPluginNode(const WeakPtr<NG::FrameNode> & pluginNode)109     void SetPluginNode(const WeakPtr<NG::FrameNode>& pluginNode)
110     {
111         pluginNode_ = pluginNode;
112     }
113 
GetPluginNode()114     WeakPtr<NG::FrameNode> GetPluginNode() const
115     {
116         return pluginNode_;
117     }
118 
FlushReload()119     void FlushReload() const
120     {
121         auto pattern = pluginPattern_.Upgrade();
122         CHECK_NULL_VOID(pattern);
123         pattern->FlushReload();
124     }
125 
SetDeclarativeOnUpdateWithValueParamsCallback(onPluginUpdateWithValueParams && callback)126     void SetDeclarativeOnUpdateWithValueParamsCallback(onPluginUpdateWithValueParams&& callback)
127     {
128         if (frontend_) {
129             frontend_->SetDeclarativeOnUpdateWithValueParamsCallback(std::move(callback));
130         }
131     }
132 
SetPluginBundleName(const std::string & pluginBundleName)133     void SetPluginBundleName(const std::string& pluginBundleName)
134     {
135         if (frontend_) {
136             frontend_->SetPluginBundleName(pluginBundleName);
137         }
138     }
139 
SetPluginModuleName(const std::string & pluginModuleName)140     void SetPluginModuleName(const std::string& pluginModuleName)
141     {
142         if (frontend_) {
143             frontend_->SetPluginModuleName(pluginModuleName);
144         }
145     }
146 
SetPluginWindowId(const int32_t pluginWindowId)147     void SetPluginWindowId(const int32_t pluginWindowId)
148     {
149         pluginWindowId_ = pluginWindowId;
150     }
151 
152 private:
153     void SetPluginComponentTheme(const std::string& path, const RefPtr<AssetManager>& assetManager);
154     void SetActionEventHandler();
155     RefPtr<AssetManager> SetAssetManager(const std::string& path, const std::string& module);
156     RefPtr<AssetManager> GetDecompressedAssetManager(const std::string& path, const std::string& module);
157 
158 private:
159     RefPtr<PluginFrontend> frontend_;
160     RefPtr<TaskExecutor> taskExecutor_;
161     RefPtr<PipelineBase> pipelineContext_;
162     WeakPtr<PipelineBase> outSidePipelineContext_;
163     RefPtr<AssetManager> assetManager_;
164 
165     int32_t instanceId_ = 0;
166     int32_t pluginWindowId_ = -1;
167 
168     bool allowUpdate_ = true;
169 
170     RefPtr<Component> pluginComponent_;
171     WeakPtr<Element> pluginElement_;
172 
173     double surfaceWidth_ = 1.0f;
174     double surfaceHeight_ = 1.0f;
175     Dimension rootWidth_ = 0.0_vp;
176     Dimension rootHeight_ = 0.0_vp;
177     double density_ = 1.0f;
178 
179     // Use for NG.
180     OnPluginAcquiredCallback onPluginAcquiredCallback_;
181     onPluginUpdateWithValueParams onUpdateWithValueParams_;
182     WeakPtr<NG::PluginPattern> pluginPattern_;
183     WeakPtr<NG::FrameNode> pluginNode_;
184 };
185 } // namespace OHOS::Ace
186 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_PLUGIN_SUB_CONTAINER_H
187