1 /*
2 * Copyright (c) 2021-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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_CONTAINER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_CONTAINER_H
18
19 #include <atomic>
20 #include <functional>
21 #include <mutex>
22 #include <unordered_map>
23
24 #include "interfaces/inner_api/ace/ace_forward_compatibility.h"
25 #include "interfaces/inner_api/ace/constants.h"
26 #include "interfaces/inner_api/ace/navigation_controller.h"
27
28 #include "base/memory/ace_type.h"
29 #include "base/view_data/hint_to_type_wrap.h"
30 #include "base/resource/asset_manager.h"
31 #include "base/resource/shared_image_manager.h"
32 #include "base/thread/task_executor.h"
33 #include "base/utils/macros.h"
34 #include "base/utils/noncopyable.h"
35 #include "base/utils/system_properties.h"
36 #include "base/utils/utils.h"
37 #include "core/common/ace_application_info.h"
38 #include "core/common/container_consts.h"
39 #include "core/common/display_info.h"
40 #include "core/common/display_info_utils.h"
41 #include "core/common/frontend.h"
42 #include "core/common/page_url_checker.h"
43 #include "core/common/platform_res_register.h"
44 #include "core/common/resource/resource_configuration.h"
45 #include "core/common/settings.h"
46 #include "core/common/window.h"
47 #include "core/components/common/layout/constants.h"
48 #include "core/components_ng/base/distributed_ui.h"
49 #include "core/components_ng/pattern/app_bar/app_bar_view.h"
50 #include "core/components_ng/pattern/navigation/navigation_route.h"
51 #include "core/components_ng/pattern/navigator/navigator_event_hub.h"
52 #include "core/event/pointer_event.h"
53 #include "core/pipeline/pipeline_base.h"
54
55 namespace OHOS::Ace {
56
57 using PageTask = std::function<void()>;
58 using TouchEventCallback = std::function<void(const TouchEvent&, const std::function<void()>&,
59 const RefPtr<NG::FrameNode>&)>;
60 using KeyEventCallback = std::function<bool(const KeyEvent&)>;
61 using MouseEventCallback = std::function<void(const MouseEvent&, const std::function<void()>&,
62 const RefPtr<NG::FrameNode>&)>;
63 using AxisEventCallback = std::function<void(const AxisEvent&, const std::function<void()>&,
64 const RefPtr<NG::FrameNode>&)>;
65 using RotationEventCallBack = std::function<bool(const RotationEvent&)>;
66 using CardViewPositionCallBack = std::function<void(int id, float offsetX, float offsetY)>;
67 using DragEventCallBack = std::function<void(const PointerEvent&, const DragEventAction&,
68 const RefPtr<NG::FrameNode>&)>;
69 using StopDragCallback = std::function<void()>;
70
71 class ACE_FORCE_EXPORT Container : public virtual AceType {
72 DECLARE_ACE_TYPE(Container, AceType);
73
74 public:
75 Container() = default;
76 ~Container() override = default;
77
78 virtual void Initialize() = 0;
79
80 virtual void Destroy() = 0;
81
IsKeyboard()82 virtual bool IsKeyboard()
83 {
84 return false;
85 }
86
DestroyView()87 virtual void DestroyView() {}
88 virtual bool UpdatePopupUIExtension(const RefPtr<NG::FrameNode>& node,
89 uint32_t autoFillSessionId, bool isNative = true)
90 {
91 return false;
92 }
93
ClosePopupUIExtension(uint32_t autoFillSessionId)94 virtual bool ClosePopupUIExtension(uint32_t autoFillSessionId)
95 {
96 return false;
97 }
98
PlaceHolderToType(const std::string & onePlaceHolder)99 virtual HintToTypeWrap PlaceHolderToType(const std::string& onePlaceHolder)
100 {
101 HintToTypeWrap hintToTypeWrap;
102 return hintToTypeWrap;
103 }
104
105 // Get the instance id of this container
106 virtual int32_t GetInstanceId() const = 0;
107
108 // Get the ability name of this container
109 virtual std::string GetHostClassName() const = 0;
110
111 // Get the frontend of container
112 virtual RefPtr<Frontend> GetFrontend() const = 0;
113
114 // Get task executor.
115 virtual RefPtr<TaskExecutor> GetTaskExecutor() const = 0;
116
117 // Get assert manager.
118 virtual RefPtr<AssetManager> GetAssetManager() const = 0;
119
120 // Get platform resource register.
121 virtual RefPtr<PlatformResRegister> GetPlatformResRegister() const = 0;
122
123 // Get the pipelineContext of container.
124 virtual RefPtr<PipelineBase> GetPipelineContext() const = 0;
125
126 // Dump container.
127 virtual bool Dump(const std::vector<std::string>& params, std::vector<std::string>& info);
128
129 // Get the width/height of the view
130 virtual int32_t GetViewWidth() const = 0;
131 virtual int32_t GetViewHeight() const = 0;
132 virtual int32_t GetViewPosX() const = 0;
133 virtual int32_t GetViewPosY() const = 0;
134
135 virtual uint32_t GetWindowId() const = 0;
SetWindowId(uint32_t windowId)136 virtual void SetWindowId(uint32_t windowId) {}
WindowIsShow()137 virtual bool WindowIsShow() const
138 {
139 return false;
140 }
141
142 virtual RefPtr<AceView> GetAceView() const = 0;
143
144 virtual void* GetView() const = 0;
145
146 // Trigger garbage collection
TriggerGarbageCollection()147 virtual void TriggerGarbageCollection() {}
148
DumpHeapSnapshot(bool isPrivate)149 virtual void DumpHeapSnapshot(bool isPrivate) {}
150
DestroyHeapProfiler()151 virtual void DestroyHeapProfiler() {}
152
ForceFullGC()153 virtual void ForceFullGC() {}
154
NotifyFontNodes()155 virtual void NotifyFontNodes() {}
156
NotifyAppStorage(const std::string & key,const std::string & value)157 virtual void NotifyAppStorage(const std::string& key, const std::string& value) {}
158
SetCardFrontend(WeakPtr<Frontend> frontend,int64_t cardId)159 virtual void SetCardFrontend(WeakPtr<Frontend> frontend, int64_t cardId) {}
160
GetCardFrontend(int64_t cardId)161 virtual WeakPtr<Frontend> GetCardFrontend(int64_t cardId) const
162 {
163 return nullptr;
164 }
165
SetCardPipeline(WeakPtr<PipelineBase>,int64_t cardId)166 virtual void SetCardPipeline(WeakPtr<PipelineBase>, int64_t cardId) {}
167
GetCardPipeline(int64_t cardId)168 virtual WeakPtr<PipelineBase> GetCardPipeline(int64_t cardId) const
169 {
170 return nullptr;
171 }
172
173 // Get MultiModal ptr.
GetMutilModalPtr()174 virtual uintptr_t GetMutilModalPtr() const
175 {
176 return 0;
177 }
178
ProcessScreenOnEvents()179 virtual void ProcessScreenOnEvents() {}
180
ProcessScreenOffEvents()181 virtual void ProcessScreenOffEvents() {}
182
SetOrientation(Orientation orientation)183 virtual void SetOrientation(Orientation orientation) {}
184
GetOrientation()185 virtual Orientation GetOrientation()
186 {
187 return Orientation::UNSPECIFIED;
188 }
189
190 virtual RefPtr<DisplayInfo> GetDisplayInfo();
191
192 virtual void InitIsFoldable();
193
194 virtual bool IsFoldable();
195
196 virtual FoldStatus GetCurrentFoldStatus();
197
GetKeyboardSafeArea()198 virtual NG::SafeAreaInsets GetKeyboardSafeArea()
199 {
200 return {};
201 }
202
GetHapPath()203 virtual std::string GetHapPath() const
204 {
205 return {};
206 }
207
GetWebHapPath()208 virtual std::string GetWebHapPath() const
209 {
210 return {};
211 }
212
SetCreateTime(std::chrono::time_point<std::chrono::high_resolution_clock> time)213 void SetCreateTime(std::chrono::time_point<std::chrono::high_resolution_clock> time)
214 {
215 createTime_ = time;
216 }
217
IsFirstUpdate()218 bool IsFirstUpdate() const
219 {
220 return firstUpdateData_;
221 }
222
AlreadyFirstUpdate()223 void AlreadyFirstUpdate()
224 {
225 firstUpdateData_ = false;
226 }
227
SetBundleName(const std::string & bundleName)228 void SetBundleName(const std::string& bundleName)
229 {
230 bundleName_ = bundleName;
231 }
232
GetBundleName()233 const std::string& GetBundleName() const
234 {
235 return bundleName_;
236 }
237
SetModuleName(const std::string & moduleName)238 void SetModuleName(const std::string& moduleName)
239 {
240 moduleName_ = moduleName;
241 }
242
GetModuleName()243 std::string GetModuleName() const
244 {
245 return moduleName_;
246 }
247
IsMainWindow()248 virtual bool IsMainWindow() const { return false; }
IsSubWindow()249 virtual bool IsSubWindow() const { return false; }
IsDialogWindow()250 virtual bool IsDialogWindow() const { return false; }
IsSystemWindow()251 virtual bool IsSystemWindow() const { return false; }
IsHostMainWindow()252 virtual bool IsHostMainWindow() const { return false; }
IsHostSubWindow()253 virtual bool IsHostSubWindow() const { return false; }
IsHostDialogWindow()254 virtual bool IsHostDialogWindow() const { return false; }
IsHostSystemWindow()255 virtual bool IsHostSystemWindow() const { return false; }
IsHostScenceBoardWindow()256 virtual bool IsHostScenceBoardWindow() const { return false; }
IsSubContainer()257 virtual bool IsSubContainer() const { return false; }
IsFormRender()258 virtual bool IsFormRender() const { return false; }
GetParentMainWindowId(uint32_t currentWindowId)259 virtual uint32_t GetParentMainWindowId(uint32_t currentWindowId) const
260 {
261 return 0;
262 }
263
SetIsFormRender(bool isFormRender)264 virtual void SetIsFormRender(bool isFormRender) {};
265
GetCardHapPath()266 const std::string& GetCardHapPath() const
267 {
268 return cardHapPath_;
269 }
270
271 bool UpdateState(const Frontend::State& state);
272
GetSettings()273 Settings& GetSettings()
274 {
275 return settings_;
276 }
277
SetBundlePath(const std::string & path)278 void SetBundlePath(const std::string& path)
279 {
280 bundlePath_ = path;
281 }
282
GetBundlePath()283 const std::string& GetBundlePath() const
284 {
285 return bundlePath_;
286 }
287
SetFilesDataPath(const std::string & path)288 void SetFilesDataPath(const std::string& path)
289 {
290 filesDataPath_ = path;
291 }
292
GetFilesDataPath()293 const std::string& GetFilesDataPath() const
294 {
295 return filesDataPath_;
296 }
297
SetTempDir(const std::string & path)298 void SetTempDir(const std::string& path)
299 {
300 tempDir_ = path;
301 }
302
GetTempDir()303 const std::string& GetTempDir() const
304 {
305 return tempDir_;
306 }
307
SetViewFirstUpdating(std::chrono::time_point<std::chrono::high_resolution_clock> time)308 virtual void SetViewFirstUpdating(std::chrono::time_point<std::chrono::high_resolution_clock> time) {}
309
UpdateResourceConfiguration(const std::string & jsonStr)310 virtual void UpdateResourceConfiguration(const std::string& jsonStr) {}
311
312 static int32_t SafelyId();
313 static int32_t CurrentId();
314 static int32_t CurrentIdSafely();
315 static int32_t CurrentIdSafelyWithCheck();
316 static RefPtr<Container> Current();
317 static RefPtr<Container> CurrentSafely();
318 static RefPtr<Container> CurrentSafelyWithCheck();
319 static RefPtr<Container> GetContainer(int32_t containerId);
320 static RefPtr<Container> GetActive();
321 static RefPtr<Container> GetDefault();
322 static RefPtr<Container> GetFoucsed();
323 static RefPtr<TaskExecutor> CurrentTaskExecutor();
324 static RefPtr<TaskExecutor> CurrentTaskExecutorSafely();
325 static RefPtr<TaskExecutor> CurrentTaskExecutorSafelyWithCheck();
326 static void UpdateCurrent(int32_t id);
327
SetUseNewPipeline()328 void SetUseNewPipeline()
329 {
330 useNewPipeline_ = true;
331 }
332
SetUsePartialUpdate()333 void SetUsePartialUpdate()
334 {
335 usePartialUpdate_ = true;
336 }
337
IsUseNewPipeline()338 bool IsUseNewPipeline() const
339 {
340 return useNewPipeline_;
341 }
342
IsCurrentUseNewPipeline()343 static bool IsCurrentUseNewPipeline()
344 {
345 auto container = Current();
346 return container ? container->useNewPipeline_ : AceForwardCompatibility::IsUseNG();
347 }
348
349 // SetCurrentUsePartialUpdate is called when initial render on a page
350 // starts, see zyz_view_register loadDocument() implementation
IsCurrentUsePartialUpdate()351 static bool IsCurrentUsePartialUpdate()
352 {
353 auto container = Current();
354 return container ? container->usePartialUpdate_ : false;
355 }
356
357 static void SetCurrentUsePartialUpdate(bool useIt = false)
358 {
359 auto container = Current();
360 if (container) {
361 container->usePartialUpdate_ = useIt;
362 }
363 }
364
IsInFormContainer()365 static bool IsInFormContainer() {
366 auto container = Current();
367 return container ? container->isFRSCardContainer_ : false;
368 }
369
IsInSubContainer()370 static bool IsInSubContainer()
371 {
372 auto container = Current();
373 return container ? container->IsSubContainer() : false;
374 }
375
GetWindow()376 Window* GetWindow() const
377 {
378 auto context = GetPipelineContext();
379 return context ? context->GetWindow() : nullptr;
380 }
381
IsUseStageModel()382 virtual bool IsUseStageModel() const
383 {
384 return false;
385 }
386
GetCardFrontendMap(std::unordered_map<int64_t,WeakPtr<Frontend>> & cardFrontendMap)387 virtual void GetCardFrontendMap(std::unordered_map<int64_t, WeakPtr<Frontend>>& cardFrontendMap) const {}
388
SetSharedRuntime(void * runtime)389 virtual void SetSharedRuntime(void* runtime) {}
GetSharedRuntime()390 virtual void* GetSharedRuntime()
391 {
392 return nullptr;
393 }
394
IsFRSCardContainer()395 bool IsFRSCardContainer() const
396 {
397 return isFRSCardContainer_;
398 }
399
SetIsFRSCardContainer(bool isFRSCardContainer)400 void SetIsFRSCardContainer(bool isFRSCardContainer)
401 {
402 isFRSCardContainer_ = isFRSCardContainer;
403 }
404
IsDynamicRender()405 bool IsDynamicRender() const
406 {
407 return isDynamicRender_;
408 }
409
SetIsDynamicRender(bool isDynamicRender)410 void SetIsDynamicRender(bool isDynamicRender)
411 {
412 isDynamicRender_ = isDynamicRender;
413 }
414
GetRegisterComponents()415 virtual std::vector<std::string> GetRegisterComponents() { return {}; };
416
SetPageUrlChecker(const RefPtr<PageUrlChecker> & pageUrlChecker)417 void SetPageUrlChecker(const RefPtr<PageUrlChecker>& pageUrlChecker)
418 {
419 pageUrlChecker_ = pageUrlChecker;
420 }
421
GetPageUrlChecker()422 const RefPtr<PageUrlChecker>& GetPageUrlChecker()
423 {
424 return pageUrlChecker_;
425 }
426
SetNavigationRoute(const RefPtr<NG::NavigationRoute> & navigationRoute)427 void SetNavigationRoute(const RefPtr<NG::NavigationRoute>& navigationRoute)
428 {
429 navigationRoute_ = navigationRoute;
430 }
431
GetNavigationRoute()432 RefPtr<NG::NavigationRoute> GetNavigationRoute() const
433 {
434 return navigationRoute_;
435 }
436
IsDialogContainer()437 virtual bool IsDialogContainer() const
438 {
439 return false;
440 }
441
SetWindowScale(float windowScale)442 virtual void SetWindowScale(float windowScale) {}
443
GetWindowScale()444 virtual float GetWindowScale() const
445 {
446 return 1.0f;
447 }
448
449 virtual void NotifyConfigurationChange(bool, const ConfigurationChange& configurationChange = { false, false }) {}
HotReload()450 virtual void HotReload() {}
451
SetIsModule(bool isModule)452 void SetIsModule(bool isModule)
453 {
454 isModule_ = isModule;
455 }
456
IsModule()457 bool IsModule() const
458 {
459 return isModule_;
460 }
461
SetDistributedUI(std::shared_ptr<NG::DistributedUI> & distributedUI)462 void SetDistributedUI(std::shared_ptr<NG::DistributedUI>& distributedUI)
463 {
464 distributedUI_ = distributedUI;
465 }
466
GetDistributedUI()467 std::shared_ptr<NG::DistributedUI>& GetDistributedUI()
468 {
469 return distributedUI_;
470 }
471
IsLauncherContainer()472 virtual bool IsLauncherContainer()
473 {
474 return false;
475 }
476
IsScenceBoardWindow()477 virtual bool IsScenceBoardWindow()
478 {
479 return false;
480 }
481
IsUIExtensionWindow()482 virtual bool IsUIExtensionWindow()
483 {
484 return false;
485 }
486
IsSceneBoardEnabled()487 virtual bool IsSceneBoardEnabled()
488 {
489 return false;
490 }
491
GetCurPointerEventInfo(int32_t & pointerId,int32_t & globalX,int32_t & globalY,int32_t & sourceType,int32_t & sourceTool,StopDragCallback && stopDragCallback)492 virtual bool GetCurPointerEventInfo(
493 int32_t& pointerId, int32_t& globalX, int32_t& globalY, int32_t& sourceType,
494 int32_t& sourceTool, StopDragCallback&& stopDragCallback)
495 {
496 return false;
497 }
498
GetCurPointerEventSourceType(int32_t & sourceType)499 virtual bool GetCurPointerEventSourceType(int32_t& sourceType)
500 {
501 return false;
502 }
503
504 virtual bool RequestAutoFill(const RefPtr<NG::FrameNode>& node, AceAutoFillType autoFillType,
505 bool isNewPassWord, bool& isPopup, uint32_t& autoFillSessionId, bool isNative = true)
506 {
507 return false;
508 }
509
IsNeedToCreatePopupWindow(const AceAutoFillType & autoFillType)510 virtual bool IsNeedToCreatePopupWindow(const AceAutoFillType& autoFillType)
511 {
512 return false;
513 }
514
515 virtual bool RequestAutoSave(const RefPtr<NG::FrameNode>& node, const std::function<void()>& onFinish = nullptr,
516 const std::function<void()>& onUIExtNodeBindingCompleted = nullptr, bool isNative = true,
517 int32_t instanceId = -1)
518 {
519 return false;
520 }
521
522
GetNavigationController(const std::string & navigationId)523 virtual std::shared_ptr<NavigationController> GetNavigationController(const std::string& navigationId)
524 {
525 return nullptr;
526 }
527
LessThanAPIVersion(PlatformVersion version)528 static bool LessThanAPIVersion(PlatformVersion version)
529 {
530 return PipelineBase::GetCurrentContext() &&
531 PipelineBase::GetCurrentContext()->GetMinPlatformVersion() < static_cast<int32_t>(version);
532 }
533
GreatOrEqualAPIVersion(PlatformVersion version)534 static bool GreatOrEqualAPIVersion(PlatformVersion version)
535 {
536 return PipelineBase::GetCurrentContext() &&
537 PipelineBase::GetCurrentContext()->GetMinPlatformVersion() >= static_cast<int32_t>(version);
538 }
539
LessThanAPITargetVersion(PlatformVersion version)540 static bool LessThanAPITargetVersion(PlatformVersion version)
541 {
542 auto container = Current();
543 CHECK_NULL_RETURN(container, false);
544 auto apiTargetVersion = container->GetApiTargetVersion();
545 return apiTargetVersion < static_cast<int32_t>(version);
546 }
547
GreatOrEqualAPITargetVersion(PlatformVersion version)548 static bool GreatOrEqualAPITargetVersion(PlatformVersion version)
549 {
550 auto container = Current();
551 if (!container) {
552 auto apiTargetVersion = AceApplicationInfo::GetInstance().GetApiTargetVersion() % 1000;
553 return apiTargetVersion >= static_cast<int32_t>(version);
554 }
555 auto apiTargetVersion = container->GetApiTargetVersion();
556 return apiTargetVersion >= static_cast<int32_t>(version);
557 }
558
SetAppBar(const RefPtr<NG::AppBarView> & appBar)559 void SetAppBar(const RefPtr<NG::AppBarView>& appBar)
560 {
561 appBar_ = appBar;
562 }
563
GetAppBar()564 RefPtr<NG::AppBarView> GetAppBar() const
565 {
566 return appBar_;
567 }
568
TerminateUIExtension()569 virtual void TerminateUIExtension() {}
570
571 template<ContainerType type>
572 static int32_t GenerateId();
573 static void SetFontScale(int32_t instanceId, float fontScale);
574 static void SetFontWeightScale(int32_t instanceId, float fontScale);
575
GetApiTargetVersion()576 int32_t GetApiTargetVersion() const
577 {
578 return apiTargetVersion_;
579 }
580
SetApiTargetVersion(int32_t apiTargetVersion)581 void SetApiTargetVersion(int32_t apiTargetVersion)
582 {
583 apiTargetVersion_ = apiTargetVersion % 1000;
584 }
585
GetUIContentType()586 UIContentType GetUIContentType() const
587 {
588 return uIContentType_;
589 }
590
SetUIContentType(UIContentType uIContentType)591 void SetUIContentType(UIContentType uIContentType)
592 {
593 uIContentType_ = uIContentType;
594 }
595
IsFreeMultiWindow()596 virtual bool IsFreeMultiWindow() const
597 {
598 return false;
599 }
600
601 private:
602 static bool IsIdAvailable(int32_t id);
603
604 protected:
605 std::chrono::time_point<std::chrono::high_resolution_clock> createTime_;
606 bool firstUpdateData_ = true;
607 std::string cardHapPath_;
608 bool useNewPipeline_ = false;
609 std::mutex stateMutex_;
610 Frontend::State state_ = Frontend::State::UNDEFINE;
611 bool isFRSCardContainer_ = false;
612 bool isDynamicRender_ = false;
613
614 private:
615 std::string bundleName_;
616 std::string moduleName_;
617 std::string bundlePath_;
618 std::string filesDataPath_;
619 std::string tempDir_;
620 bool usePartialUpdate_ = false;
621 Settings settings_;
622 RefPtr<PageUrlChecker> pageUrlChecker_;
623 RefPtr<NG::NavigationRoute> navigationRoute_;
624 bool isModule_ = false;
625 std::shared_ptr<NG::DistributedUI> distributedUI_;
626 RefPtr<NG::AppBarView> appBar_;
627 int32_t apiTargetVersion_ = 0;
628 // Define the type of UI Content, for example, Security UIExtension.
629 UIContentType uIContentType_ = UIContentType::UNDEFINED;
630 ACE_DISALLOW_COPY_AND_MOVE(Container);
631 };
632
633 template<ContainerType type>
GenerateId()634 int32_t Container::GenerateId()
635 {
636 static std::atomic<int32_t> gInstanceId;
637 int32_t id;
638 do {
639 id = type * CONTAINER_ID_DIVIDE_SIZE + gInstanceId.fetch_add(1) % CONTAINER_ID_DIVIDE_SIZE;
640 } while (!IsIdAvailable(id));
641 return id;
642 }
643
644 template<>
645 int32_t Container::GenerateId<PLUGIN_SUBCONTAINER>();
646
647 } // namespace OHOS::Ace
648
649 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_CONTAINER_H
650