1 /*
2 * Copyright (c) 2021-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_COMMON_CONTAINER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_CONTAINER_H
18
19 #include <functional>
20 #include <mutex>
21 #include <unordered_map>
22 #include <atomic>
23
24 #include "interfaces/inner_api/ace/ace_forward_compatibility.h"
25 #include "interfaces/inner_api/ace/navigation_controller.h"
26
27 #include "base/memory/ace_type.h"
28 #include "base/resource/asset_manager.h"
29 #include "base/resource/shared_image_manager.h"
30 #include "base/thread/task_executor.h"
31 #include "base/utils/macros.h"
32 #include "base/utils/noncopyable.h"
33 #include "base/utils/system_properties.h"
34 #include "core/common/ace_application_info.h"
35 #include "core/common/display_info.h"
36 #include "core/common/frontend.h"
37 #include "core/common/page_url_checker.h"
38 #include "core/common/platform_res_register.h"
39 #include "core/common/resource/resource_configuration.h"
40 #include "core/common/settings.h"
41 #include "core/common/window.h"
42 #include "core/components/common/layout/constants.h"
43 #include "core/components_ng/base/distributed_ui.h"
44 #include "core/components_ng/pattern/app_bar/app_bar_view.h"
45 #include "core/components_ng/pattern/navigator/navigator_event_hub.h"
46 #include "core/event/pointer_event.h"
47 #include "core/pipeline/pipeline_base.h"
48 #include "core/common/container_consts.h"
49
50 namespace OHOS::Ace {
51
52 using PageTask = std::function<void()>;
53 using TouchEventCallback = std::function<void(const TouchEvent&, const std::function<void()>&,
54 const RefPtr<NG::FrameNode>&)>;
55 using KeyEventCallback = std::function<bool(const KeyEvent&)>;
56 using MouseEventCallback = std::function<void(const MouseEvent&, const std::function<void()>&,
57 const RefPtr<NG::FrameNode>&)>;
58 using AxisEventCallback = std::function<void(const AxisEvent&, const std::function<void()>&,
59 const RefPtr<NG::FrameNode>&)>;
60 using RotationEventCallBack = std::function<bool(const RotationEvent&)>;
61 using CardViewPositionCallBack = std::function<void(int id, float offsetX, float offsetY)>;
62 using DragEventCallBack = std::function<void(const PointerEvent& pointerEvent, const DragEventAction& action)>;
63 using StopDragCallback = std::function<void()>;
64
65 class ACE_FORCE_EXPORT Container : public virtual AceType {
66 DECLARE_ACE_TYPE(Container, AceType);
67
68 public:
69 Container() = default;
70 ~Container() override = default;
71
72 virtual void Initialize() = 0;
73
74 virtual void Destroy() = 0;
75
IsKeyboard()76 virtual bool IsKeyboard()
77 {
78 return false;
79 }
80
DestroyView()81 virtual void DestroyView() {}
82
83 // Get the instance id of this container
84 virtual int32_t GetInstanceId() const = 0;
85
86 // Get the ability name of this container
87 virtual std::string GetHostClassName() const = 0;
88
89 // Get the frontend of container
90 virtual RefPtr<Frontend> GetFrontend() const = 0;
91
92 // Get task executor.
93 virtual RefPtr<TaskExecutor> GetTaskExecutor() const = 0;
94
95 // Get assert manager.
96 virtual RefPtr<AssetManager> GetAssetManager() const = 0;
97
98 // Get platform resource register.
99 virtual RefPtr<PlatformResRegister> GetPlatformResRegister() const = 0;
100
101 // Get the pipelineContext of container.
102 virtual RefPtr<PipelineBase> GetPipelineContext() const = 0;
103
104 // Dump container.
105 virtual bool Dump(const std::vector<std::string>& params, std::vector<std::string>& info);
106
107 // Get the width/height of the view
108 virtual int32_t GetViewWidth() const = 0;
109 virtual int32_t GetViewHeight() const = 0;
110 virtual int32_t GetViewPosX() const = 0;
111 virtual int32_t GetViewPosY() const = 0;
112
113 virtual uint32_t GetWindowId() const = 0;
SetWindowId(uint32_t windowId)114 virtual void SetWindowId(uint32_t windowId) {}
WindowIsShow()115 virtual bool WindowIsShow() const
116 {
117 return false;
118 }
119
120 virtual void* GetView() const = 0;
121
122 // Trigger garbage collection
TriggerGarbageCollection()123 virtual void TriggerGarbageCollection() {}
124
DumpHeapSnapshot(bool isPrivate)125 virtual void DumpHeapSnapshot(bool isPrivate) {}
126
DestroyHeapProfiler()127 virtual void DestroyHeapProfiler() {}
128
ForceFullGC()129 virtual void ForceFullGC() {}
130
NotifyFontNodes()131 virtual void NotifyFontNodes() {}
132
NotifyAppStorage(const std::string & key,const std::string & value)133 virtual void NotifyAppStorage(const std::string& key, const std::string& value) {}
134
SetCardFrontend(WeakPtr<Frontend> frontend,int64_t cardId)135 virtual void SetCardFrontend(WeakPtr<Frontend> frontend, int64_t cardId) {}
136
GetCardFrontend(int64_t cardId)137 virtual WeakPtr<Frontend> GetCardFrontend(int64_t cardId) const
138 {
139 return nullptr;
140 }
141
SetCardPipeline(WeakPtr<PipelineBase>,int64_t cardId)142 virtual void SetCardPipeline(WeakPtr<PipelineBase>, int64_t cardId) {}
143
GetCardPipeline(int64_t cardId)144 virtual WeakPtr<PipelineBase> GetCardPipeline(int64_t cardId) const
145 {
146 return nullptr;
147 }
148
149 // Get MultiModal ptr.
GetMutilModalPtr()150 virtual uintptr_t GetMutilModalPtr() const
151 {
152 return 0;
153 }
154
ProcessScreenOnEvents()155 virtual void ProcessScreenOnEvents() {}
156
ProcessScreenOffEvents()157 virtual void ProcessScreenOffEvents() {}
158
SetOrientation(Orientation orientation)159 virtual void SetOrientation(Orientation orientation) {}
160
GetOrientation()161 virtual Orientation GetOrientation()
162 {
163 return Orientation::UNSPECIFIED;
164 }
165
GetDisplayInfo()166 virtual RefPtr<DisplayInfo> GetDisplayInfo()
167 {
168 return MakeRefPtr<DisplayInfo>();
169 }
170
GetKeyboardSafeArea()171 virtual NG::SafeAreaInsets GetKeyboardSafeArea()
172 {
173 return {};
174 }
175
GetHapPath()176 virtual std::string GetHapPath() const
177 {
178 return {};
179 }
180
GetWebHapPath()181 virtual std::string GetWebHapPath() const
182 {
183 return {};
184 }
185
SetCreateTime(std::chrono::time_point<std::chrono::high_resolution_clock> time)186 void SetCreateTime(std::chrono::time_point<std::chrono::high_resolution_clock> time)
187 {
188 createTime_ = time;
189 }
190
IsFirstUpdate()191 bool IsFirstUpdate() const
192 {
193 return firstUpdateData_;
194 }
195
AlreadyFirstUpdate()196 void AlreadyFirstUpdate()
197 {
198 firstUpdateData_ = false;
199 }
200
SetModuleName(const std::string & moduleName)201 void SetModuleName(const std::string& moduleName)
202 {
203 moduleName_ = moduleName;
204 }
205
GetModuleName()206 std::string GetModuleName() const
207 {
208 return moduleName_;
209 }
210
IsMainWindow()211 virtual bool IsMainWindow() const
212 {
213 return false;
214 }
215
IsSubContainer()216 virtual bool IsSubContainer() const
217 {
218 return false;
219 }
220
GetCardHapPath()221 const std::string& GetCardHapPath() const
222 {
223 return cardHapPath_;
224 }
225
226 bool UpdateState(const Frontend::State& state);
227
GetSettings()228 Settings& GetSettings()
229 {
230 return settings_;
231 }
232
SetBundlePath(const std::string & path)233 void SetBundlePath(const std::string& path)
234 {
235 bundlePath_ = path;
236 }
237
GetBundlePath()238 const std::string& GetBundlePath() const
239 {
240 return bundlePath_;
241 }
242
SetFilesDataPath(const std::string & path)243 void SetFilesDataPath(const std::string& path)
244 {
245 filesDataPath_ = path;
246 }
247
GetFilesDataPath()248 const std::string& GetFilesDataPath() const
249 {
250 return filesDataPath_;
251 }
252
SetTempDir(const std::string & path)253 void SetTempDir(const std::string& path)
254 {
255 tempDir_ = path;
256 }
257
GetTempDir()258 const std::string& GetTempDir() const
259 {
260 return tempDir_;
261 }
262
SetViewFirstUpdating(std::chrono::time_point<std::chrono::high_resolution_clock> time)263 virtual void SetViewFirstUpdating(std::chrono::time_point<std::chrono::high_resolution_clock> time) {}
264
UpdateResourceConfiguration(const std::string & jsonStr)265 virtual void UpdateResourceConfiguration(const std::string& jsonStr) {}
266
267 static int32_t CurrentId();
268 static int32_t CurrentIdSafely();
269 static RefPtr<Container> Current();
270 static RefPtr<Container> CurrentSafely();
271 static RefPtr<Container> GetContainer(int32_t containerId);
272 static RefPtr<Container> GetActive();
273 static RefPtr<Container> GetDefault();
274 static RefPtr<Container> GetFoucsed();
275 static RefPtr<TaskExecutor> CurrentTaskExecutor();
276 static void UpdateCurrent(int32_t id);
277
SetUseNewPipeline()278 void SetUseNewPipeline()
279 {
280 useNewPipeline_ = true;
281 }
282
IsUseNewPipeline()283 bool IsUseNewPipeline() const
284 {
285 return useNewPipeline_;
286 }
287
IsCurrentUseNewPipeline()288 static bool IsCurrentUseNewPipeline()
289 {
290 auto container = Current();
291 #ifdef OHOS_STANDARD_SYSTEM
292 return container ? container->useNewPipeline_ : AceForwardCompatibility::IsUseNG();
293 #endif
294 return container ? container->useNewPipeline_ : false;
295 }
296
297 // SetCurrentUsePartialUpdate is called when initial render on a page
298 // starts, see zyz_view_register loadDocument() implementation
IsCurrentUsePartialUpdate()299 static bool IsCurrentUsePartialUpdate()
300 {
301 auto container = Current();
302 return container ? container->usePartialUpdate_ : false;
303 }
304
305 static void SetCurrentUsePartialUpdate(bool useIt = false)
306 {
307 auto container = Current();
308 if (container) {
309 container->usePartialUpdate_ = useIt;
310 }
311 }
312
IsInFormContainer()313 static bool IsInFormContainer() {
314 auto container = Current();
315 return container ? container->isFRSCardContainer_ : false;
316 }
317
GetWindow()318 Window* GetWindow() const
319 {
320 auto context = GetPipelineContext();
321 return context ? context->GetWindow() : nullptr;
322 }
323
IsUseStageModel()324 virtual bool IsUseStageModel() const
325 {
326 return false;
327 }
328
GetCardFrontendMap(std::unordered_map<int64_t,WeakPtr<Frontend>> & cardFrontendMap)329 virtual void GetCardFrontendMap(std::unordered_map<int64_t, WeakPtr<Frontend>>& cardFrontendMap) const {}
330
SetSharedRuntime(void * runtime)331 virtual void SetSharedRuntime(void* runtime) {}
GetSharedRuntime()332 virtual void* GetSharedRuntime()
333 {
334 return nullptr;
335 }
336
IsFRSCardContainer()337 bool IsFRSCardContainer() const
338 {
339 return isFRSCardContainer_;
340 }
341
SetIsFRSCardContainer(bool isFRSCardContainer)342 void SetIsFRSCardContainer(bool isFRSCardContainer)
343 {
344 isFRSCardContainer_ = isFRSCardContainer;
345 }
346
IsDynamicRender()347 bool IsDynamicRender() const
348 {
349 return isDynamicRender_;
350 }
351
SetIsDynamicRender(bool isDynamicRender)352 void SetIsDynamicRender(bool isDynamicRender)
353 {
354 isDynamicRender_ = isDynamicRender;
355 }
356
SetPageUrlChecker(const RefPtr<PageUrlChecker> & pageUrlChecker)357 void SetPageUrlChecker(const RefPtr<PageUrlChecker>& pageUrlChecker)
358 {
359 pageUrlChecker_ = pageUrlChecker;
360 }
361
GetPageUrlChecker()362 const RefPtr<PageUrlChecker>& GetPageUrlChecker()
363 {
364 return pageUrlChecker_;
365 }
366
IsDialogContainer()367 virtual bool IsDialogContainer() const
368 {
369 return false;
370 }
371
SetWindowScale(float windowScale)372 virtual void SetWindowScale(float windowScale) {}
373
GetWindowScale()374 virtual float GetWindowScale() const
375 {
376 return 1.0f;
377 }
378
379 virtual void NotifyConfigurationChange(bool, const ConfigurationChange& configurationChange = { false, false }) {}
HotReload()380 virtual void HotReload() {}
381
SetIsModule(bool isModule)382 void SetIsModule(bool isModule)
383 {
384 isModule_ = isModule;
385 }
386
IsModule()387 bool IsModule() const
388 {
389 return isModule_;
390 }
391
SetDistributedUI(std::shared_ptr<NG::DistributedUI> & distributedUI)392 void SetDistributedUI(std::shared_ptr<NG::DistributedUI>& distributedUI)
393 {
394 distributedUI_ = distributedUI;
395 }
396
GetDistributedUI()397 std::shared_ptr<NG::DistributedUI>& GetDistributedUI()
398 {
399 return distributedUI_;
400 }
401
IsLauncherContainer()402 virtual bool IsLauncherContainer()
403 {
404 return false;
405 }
406
IsScenceBoardWindow()407 virtual bool IsScenceBoardWindow()
408 {
409 return false;
410 }
411
IsUIExtensionWindow()412 virtual bool IsUIExtensionWindow()
413 {
414 return false;
415 }
416
IsSceneBoardEnabled()417 virtual bool IsSceneBoardEnabled()
418 {
419 return false;
420 }
421
GetCurPointerEventInfo(int32_t pointerId,int32_t & globalX,int32_t & globalY,int32_t & sourceType,StopDragCallback && stopDragCallback)422 virtual bool GetCurPointerEventInfo(
423 int32_t pointerId, int32_t& globalX, int32_t& globalY, int32_t& sourceType, StopDragCallback&& stopDragCallback)
424 {
425 return false;
426 }
427
RequestAutoFill(const RefPtr<NG::FrameNode> & node,AceAutoFillType autoFillType)428 virtual bool RequestAutoFill(const RefPtr<NG::FrameNode>& node, AceAutoFillType autoFillType)
429 {
430 return false;
431 }
432
RequestAutoSave(const RefPtr<NG::FrameNode> & node)433 virtual bool RequestAutoSave(const RefPtr<NG::FrameNode>& node)
434 {
435 return false;
436 }
437
GetNavigationController(const std::string & navigationId)438 virtual std::shared_ptr<NavigationController> GetNavigationController(const std::string& navigationId)
439 {
440 return nullptr;
441 }
442
LessThanAPIVersion(PlatformVersion version)443 static bool LessThanAPIVersion(PlatformVersion version)
444 {
445 return PipelineBase::GetCurrentContext() &&
446 PipelineBase::GetCurrentContext()->GetMinPlatformVersion() < static_cast<int32_t>(version);
447 }
448
GreatOrEqualAPIVersion(PlatformVersion version)449 static bool GreatOrEqualAPIVersion(PlatformVersion version)
450 {
451 return PipelineBase::GetCurrentContext() &&
452 PipelineBase::GetCurrentContext()->GetMinPlatformVersion() >= static_cast<int32_t>(version);
453 }
454
IsLauncherApp()455 static bool IsLauncherApp()
456 {
457 return isLauncherApp_;
458 }
459
SetIsLauncherApp(bool isLauncherApp)460 static void SetIsLauncherApp(bool isLauncherApp)
461 {
462 isLauncherApp_ = isLauncherApp;
463 }
464
SetAppBar(const RefPtr<NG::AppBarView> & appBar)465 void SetAppBar(const RefPtr<NG::AppBarView>& appBar)
466 {
467 appBar_ = appBar;
468 }
469
GetAppBar()470 RefPtr<NG::AppBarView> GetAppBar() const
471 {
472 return appBar_;
473 }
474
475 template<ContainerType type>
476 static int32_t GenerateId();
477
478 private:
479 static bool IsIdAvailable(int32_t id);
480 static bool isLauncherApp_;
481
482 protected:
483 std::chrono::time_point<std::chrono::high_resolution_clock> createTime_;
484 bool firstUpdateData_ = true;
485 std::string cardHapPath_;
486 bool useNewPipeline_ = false;
487 std::mutex stateMutex_;
488 Frontend::State state_ = Frontend::State::UNDEFINE;
489 bool isFRSCardContainer_ = false;
490 bool isDynamicRender_ = false;
491
492 private:
493 std::string moduleName_;
494 std::string bundlePath_;
495 std::string filesDataPath_;
496 std::string tempDir_;
497 bool usePartialUpdate_ = false;
498 Settings settings_;
499 RefPtr<PageUrlChecker> pageUrlChecker_;
500 bool isModule_ = false;
501 std::shared_ptr<NG::DistributedUI> distributedUI_;
502 RefPtr<NG::AppBarView> appBar_;
503 ACE_DISALLOW_COPY_AND_MOVE(Container);
504 };
505
506 template<ContainerType type>
GenerateId()507 int32_t Container::GenerateId()
508 {
509 static std::atomic<int32_t> gInstanceId;
510 int32_t id;
511 do {
512 id = type * CONTAINER_ID_DIVIDE_SIZE + gInstanceId.fetch_add(1) % CONTAINER_ID_DIVIDE_SIZE;
513 } while (!IsIdAvailable(id));
514 return id;
515 }
516
517 template<>
518 int32_t Container::GenerateId<PLUGIN_SUBCONTAINER>();
519
520 } // namespace OHOS::Ace
521
522 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_CONTAINER_H
523