• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_COMMON_CONTAINER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_CONTAINER_H
18 
19 #include <functional>
20 #include <mutex>
21 #include <unordered_map>
22 
23 #include "base/memory/ace_type.h"
24 #include "base/resource/asset_manager.h"
25 #include "base/resource/shared_image_manager.h"
26 #include "base/thread/task_executor.h"
27 #include "base/utils/macros.h"
28 #include "base/utils/noncopyable.h"
29 #include "core/common/ace_application_info.h"
30 #include "core/common/frontend.h"
31 #include "core/common/page_url_checker.h"
32 #include "core/common/platform_res_register.h"
33 #include "core/common/settings.h"
34 #include "core/common/window.h"
35 #include "core/components_ng/pattern/navigator/navigator_event_hub.h"
36 #include "core/pipeline/pipeline_base.h"
37 
38 namespace OHOS::Ace {
39 
40 using PageTask = std::function<void()>;
41 using TouchEventCallback = std::function<void(const TouchEvent&, const std::function<void()>&)>;
42 using KeyEventCallback = std::function<bool(const KeyEvent&)>;
43 using MouseEventCallback = std::function<void(const MouseEvent&, const std::function<void()>&)>;
44 using AxisEventCallback = std::function<void(const AxisEvent&, const std::function<void()>&)>;
45 using RotationEventCallBack = std::function<bool(const RotationEvent&)>;
46 using CardViewPositionCallBack = std::function<void(int id, float offsetX, float offsetY)>;
47 using DragEventCallBack = std::function<void(int32_t x, int32_t y, const DragEventAction& action)>;
48 
49 constexpr int32_t INSTANCE_ID_UNDEFINED = -1;
50 constexpr int32_t INSTANCE_ID_PLATFORM = -2;
51 constexpr int32_t MIN_PLUGIN_SUBCONTAINER_ID = 2000000;
52 
53 class ACE_FORCE_EXPORT_WITH_PREVIEW Container : public virtual AceType {
54     DECLARE_ACE_TYPE(Container, AceType);
55 
56 public:
57     Container() = default;
58     ~Container() override = default;
59 
60     virtual void Initialize() = 0;
61 
62     virtual void Destroy() = 0;
63 
DestroyView()64     virtual void DestroyView() {}
65 
66     // Get the instance id of this container
67     virtual int32_t GetInstanceId() const = 0;
68 
69     // Get the ability name of this container
70     virtual std::string GetHostClassName() const = 0;
71 
72     // Get the frontend of container
73     virtual RefPtr<Frontend> GetFrontend() const = 0;
74 
75     // Get task executor.
76     virtual RefPtr<TaskExecutor> GetTaskExecutor() const = 0;
77 
78     // Get assert manager.
79     virtual RefPtr<AssetManager> GetAssetManager() const = 0;
80 
81     // Get platform resource register.
82     virtual RefPtr<PlatformResRegister> GetPlatformResRegister() const = 0;
83 
84     // Get the pipelineContext of container.
85     virtual RefPtr<PipelineBase> GetPipelineContext() const = 0;
86 
87     // Dump container.
88     virtual bool Dump(const std::vector<std::string>& params) = 0;
89 
90     // Get the width/height of the view
91     virtual int32_t GetViewWidth() const = 0;
92     virtual int32_t GetViewHeight() const = 0;
93     virtual int32_t GetViewPosX() const = 0;
94     virtual int32_t GetViewPosY() const = 0;
95 
96     virtual uint32_t GetWindowId() const = 0;
SetWindowId(uint32_t windowId)97     virtual void SetWindowId(uint32_t windowId) {}
98 
99     virtual void* GetView() const = 0;
100 
101     // Trigger garbage collection
TriggerGarbageCollection()102     virtual void TriggerGarbageCollection() {}
103 
DumpHeapSnapshot(bool isPrivate)104     virtual void DumpHeapSnapshot(bool isPrivate) {}
105 
NotifyFontNodes()106     virtual void NotifyFontNodes() {}
107 
NotifyAppStorage(const std::string & key,const std::string & value)108     virtual void NotifyAppStorage(const std::string& key, const std::string& value) {}
109 
SetCardFrontend(WeakPtr<Frontend> frontend,int64_t cardId)110     virtual void SetCardFrontend(WeakPtr<Frontend> frontend, int64_t cardId) {}
111 
GetCardFrontend(int64_t cardId)112     virtual WeakPtr<Frontend> GetCardFrontend(int64_t cardId) const
113     {
114         return nullptr;
115     }
116 
SetCardPipeline(WeakPtr<PipelineBase>,int64_t cardId)117     virtual void SetCardPipeline(WeakPtr<PipelineBase>, int64_t cardId) {}
118 
GetCardPipeline(int64_t cardId)119     virtual WeakPtr<PipelineBase> GetCardPipeline(int64_t cardId) const
120     {
121         return nullptr;
122     }
123 
124     // Get MutilModal ptr.
GetMutilModalPtr()125     virtual uintptr_t GetMutilModalPtr() const
126     {
127         return 0;
128     }
129 
ProcessScreenOnEvents()130     virtual void ProcessScreenOnEvents() {}
131 
ProcessScreenOffEvents()132     virtual void ProcessScreenOffEvents() {}
133 
SetCreateTime(std::chrono::time_point<std::chrono::high_resolution_clock> time)134     void SetCreateTime(std::chrono::time_point<std::chrono::high_resolution_clock> time)
135     {
136         createTime_ = time;
137     }
138 
IsFirstUpdate()139     bool IsFirstUpdate() const
140     {
141         return firstUpdateData_;
142     }
143 
AlreadyFirstUpdate()144     void AlreadyFirstUpdate()
145     {
146         firstUpdateData_ = false;
147     }
148 
SetModuleName(const std::string & moduleName)149     void SetModuleName(const std::string& moduleName)
150     {
151         moduleName_ = moduleName;
152     }
153 
GetModuleName()154     std::string GetModuleName() const
155     {
156         return moduleName_;
157     }
158 
IsMainWindow()159     virtual bool IsMainWindow() const
160     {
161         return false;
162     }
163 
IsSubContainer()164     virtual bool IsSubContainer() const
165     {
166         return false;
167     }
168 
GetCardHapPath()169     const std::string& GetCardHapPath() const
170     {
171         return cardHapPath_;
172     }
173 
174     bool UpdateState(const Frontend::State& state);
175 
GetSettings()176     Settings& GetSettings()
177     {
178         return settings_;
179     }
180 
SetBundlePath(const std::string & path)181     void SetBundlePath(const std::string& path)
182     {
183         bundlePath_ = path;
184     }
185 
GetBundlePath()186     const std::string& GetBundlePath() const
187     {
188         return bundlePath_;
189     }
190 
SetFilesDataPath(const std::string & path)191     void SetFilesDataPath(const std::string& path)
192     {
193         filesDataPath_ = path;
194     }
195 
GetFilesDataPath()196     const std::string& GetFilesDataPath() const
197     {
198         return filesDataPath_;
199     }
200 
SetViewFirstUpdating(std::chrono::time_point<std::chrono::high_resolution_clock> time)201     virtual void SetViewFirstUpdating(std::chrono::time_point<std::chrono::high_resolution_clock> time) {}
202 
UpdateResourceConfiguration(const std::string & jsonStr)203     virtual void UpdateResourceConfiguration(const std::string& jsonStr) {}
204 
205     static int32_t CurrentId();
206     static RefPtr<Container> Current();
207     static RefPtr<Container> GetActive();
208     static RefPtr<TaskExecutor> CurrentTaskExecutor();
209     static void UpdateCurrent(int32_t id);
210 
SetUseNewPipeline()211     void SetUseNewPipeline()
212     {
213         useNewPipeline_ = true;
214     }
215 
IsUseNewPipeline()216     bool IsUseNewPipeline() const
217     {
218         return useNewPipeline_;
219     }
220 
IsCurrentUseNewPipeline()221     static bool IsCurrentUseNewPipeline()
222     {
223         auto container = Current();
224         return container ? container->useNewPipeline_ : false;
225     }
226 
227     // SetCurrentUsePartialUpdate is called when initial render on a page
228     // starts, see zyz_view_register loadDocument() implementation
IsCurrentUsePartialUpdate()229     static bool IsCurrentUsePartialUpdate()
230     {
231         auto container = Current();
232         return container ? container->usePartialUpdate_ : false;
233     }
234 
235     static void SetCurrentUsePartialUpdate(bool useIt = false)
236     {
237         auto container = Current();
238         if (container) {
239             container->usePartialUpdate_ = useIt;
240         }
241     }
242 
GetWindow()243     Window* GetWindow() const
244     {
245         auto context = GetPipelineContext();
246         return context ? context->GetWindow() : nullptr;
247     }
248 
IsUseStageModel()249     virtual bool IsUseStageModel() const
250     {
251         return false;
252     }
253 
GetCardFrontendMap(std::unordered_map<int64_t,WeakPtr<Frontend>> & cardFrontendMap)254     virtual void GetCardFrontendMap(std::unordered_map<int64_t, WeakPtr<Frontend>>& cardFrontendMap) const {}
255 
SetSharedRuntime(void * runtime)256     virtual void SetSharedRuntime(void* runtime) {}
GetSharedRuntime()257     virtual void* GetSharedRuntime()
258     {
259         return nullptr;
260     }
261 
SetPageUrlChecker(const RefPtr<PageUrlChecker> & pageUrlChecker)262     void SetPageUrlChecker(const RefPtr<PageUrlChecker>& pageUrlChecker)
263     {
264         pageUrlChecker_ = pageUrlChecker;
265     }
266 
GetPageUrlChecker()267     const RefPtr<PageUrlChecker>& GetPageUrlChecker()
268     {
269         return pageUrlChecker_;
270     }
271 
IsFRSCardContainer()272     bool IsFRSCardContainer() const
273     {
274         return isFRSCardContainer_;
275     }
276 
SetIsFRSCardContainer(bool isFRSCardContainer)277     void SetIsFRSCardContainer(bool isFRSCardContainer)
278     {
279         isFRSCardContainer_ = isFRSCardContainer;
280     }
281 
IsDialogContainer()282     virtual bool IsDialogContainer() const
283     {
284         return false;
285     }
286 
287 protected:
288     std::chrono::time_point<std::chrono::high_resolution_clock> createTime_;
289     bool firstUpdateData_ = true;
290     std::string cardHapPath_;
291     bool useNewPipeline_ = false;
292     std::mutex stateMutex_;
293     Frontend::State state_ = Frontend::State::UNDEFINED;
294     bool isFRSCardContainer_ = false;
295 
296 private:
297     std::string moduleName_;
298     std::string bundlePath_;
299     std::string filesDataPath_;
300     bool usePartialUpdate_ = false;
301     Settings settings_;
302     RefPtr<PageUrlChecker> pageUrlChecker_;
303     ACE_DISALLOW_COPY_AND_MOVE(Container);
304 };
305 
306 } // namespace OHOS::Ace
307 
308 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_CONTAINER_H
309