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 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/base/distributed_ui.h" 36 #include "core/components_ng/pattern/navigator/navigator_event_hub.h" 37 #include "core/pipeline/pipeline_base.h" 38 39 namespace OHOS::Ace { 40 41 using PageTask = std::function<void()>; 42 using TouchEventCallback = std::function<void(const TouchEvent&, const std::function<void()>&)>; 43 using KeyEventCallback = std::function<bool(const KeyEvent&)>; 44 using MouseEventCallback = std::function<void(const MouseEvent&, const std::function<void()>&)>; 45 using AxisEventCallback = std::function<void(const AxisEvent&, const std::function<void()>&)>; 46 using RotationEventCallBack = std::function<bool(const RotationEvent&)>; 47 using CardViewPositionCallBack = std::function<void(int id, float offsetX, float offsetY)>; 48 using DragEventCallBack = std::function<void(int32_t x, int32_t y, const DragEventAction& action)>; 49 using StopDragCallback = std::function<void()>; 50 51 constexpr int32_t INSTANCE_ID_UNDEFINED = -1; 52 constexpr int32_t INSTANCE_ID_PLATFORM = -2; 53 constexpr int32_t MIN_PLUGIN_SUBCONTAINER_ID = 2000000; 54 55 class ACE_FORCE_EXPORT Container : public virtual AceType { 56 DECLARE_ACE_TYPE(Container, AceType); 57 58 public: 59 Container() = default; 60 ~Container() override = default; 61 62 virtual void Initialize() = 0; 63 64 virtual void Destroy() = 0; 65 DestroyView()66 virtual void DestroyView() {} 67 68 // Get the instance id of this container 69 virtual int32_t GetInstanceId() const = 0; 70 71 // Get the ability name of this container 72 virtual std::string GetHostClassName() const = 0; 73 74 // Get the frontend of container 75 virtual RefPtr<Frontend> GetFrontend() const = 0; 76 77 // Get task executor. 78 virtual RefPtr<TaskExecutor> GetTaskExecutor() const = 0; 79 80 // Get assert manager. 81 virtual RefPtr<AssetManager> GetAssetManager() const = 0; 82 83 // Get platform resource register. 84 virtual RefPtr<PlatformResRegister> GetPlatformResRegister() const = 0; 85 86 // Get the pipelineContext of container. 87 virtual RefPtr<PipelineBase> GetPipelineContext() const = 0; 88 89 // Dump container. 90 virtual bool Dump(const std::vector<std::string>& params, std::vector<std::string>& info); 91 92 // Get the width/height of the view 93 virtual int32_t GetViewWidth() const = 0; 94 virtual int32_t GetViewHeight() const = 0; 95 virtual int32_t GetViewPosX() const = 0; 96 virtual int32_t GetViewPosY() const = 0; 97 98 virtual uint32_t GetWindowId() const = 0; SetWindowId(uint32_t windowId)99 virtual void SetWindowId(uint32_t windowId) {} WindowIsShow()100 virtual bool WindowIsShow() const 101 { 102 return false; 103 } 104 105 virtual void* GetView() const = 0; 106 107 // Trigger garbage collection TriggerGarbageCollection()108 virtual void TriggerGarbageCollection() {} 109 DumpHeapSnapshot(bool isPrivate)110 virtual void DumpHeapSnapshot(bool isPrivate) {} 111 NotifyFontNodes()112 virtual void NotifyFontNodes() {} 113 NotifyAppStorage(const std::string & key,const std::string & value)114 virtual void NotifyAppStorage(const std::string& key, const std::string& value) {} 115 SetCardFrontend(WeakPtr<Frontend> frontend,int64_t cardId)116 virtual void SetCardFrontend(WeakPtr<Frontend> frontend, int64_t cardId) {} 117 GetCardFrontend(int64_t cardId)118 virtual WeakPtr<Frontend> GetCardFrontend(int64_t cardId) const 119 { 120 return nullptr; 121 } 122 SetCardPipeline(WeakPtr<PipelineBase>,int64_t cardId)123 virtual void SetCardPipeline(WeakPtr<PipelineBase>, int64_t cardId) {} 124 GetCardPipeline(int64_t cardId)125 virtual WeakPtr<PipelineBase> GetCardPipeline(int64_t cardId) const 126 { 127 return nullptr; 128 } 129 130 // Get MultiModal ptr. GetMutilModalPtr()131 virtual uintptr_t GetMutilModalPtr() const 132 { 133 return 0; 134 } 135 ProcessScreenOnEvents()136 virtual void ProcessScreenOnEvents() {} 137 ProcessScreenOffEvents()138 virtual void ProcessScreenOffEvents() {} 139 GetHapPath()140 virtual std::string GetHapPath() const 141 { 142 return {}; 143 } 144 GetWebHapPath()145 virtual std::string GetWebHapPath() const 146 { 147 return {}; 148 } 149 SetCreateTime(std::chrono::time_point<std::chrono::high_resolution_clock> time)150 void SetCreateTime(std::chrono::time_point<std::chrono::high_resolution_clock> time) 151 { 152 createTime_ = time; 153 } 154 IsFirstUpdate()155 bool IsFirstUpdate() const 156 { 157 return firstUpdateData_; 158 } 159 AlreadyFirstUpdate()160 void AlreadyFirstUpdate() 161 { 162 firstUpdateData_ = false; 163 } 164 SetModuleName(const std::string & moduleName)165 void SetModuleName(const std::string& moduleName) 166 { 167 moduleName_ = moduleName; 168 } 169 GetModuleName()170 std::string GetModuleName() const 171 { 172 return moduleName_; 173 } 174 IsMainWindow()175 virtual bool IsMainWindow() const 176 { 177 return false; 178 } 179 IsSubContainer()180 virtual bool IsSubContainer() const 181 { 182 return false; 183 } 184 GetCardHapPath()185 const std::string& GetCardHapPath() const 186 { 187 return cardHapPath_; 188 } 189 190 bool UpdateState(const Frontend::State& state); 191 GetSettings()192 Settings& GetSettings() 193 { 194 return settings_; 195 } 196 SetBundlePath(const std::string & path)197 void SetBundlePath(const std::string& path) 198 { 199 bundlePath_ = path; 200 } 201 GetBundlePath()202 const std::string& GetBundlePath() const 203 { 204 return bundlePath_; 205 } 206 SetFilesDataPath(const std::string & path)207 void SetFilesDataPath(const std::string& path) 208 { 209 filesDataPath_ = path; 210 } 211 GetFilesDataPath()212 const std::string& GetFilesDataPath() const 213 { 214 return filesDataPath_; 215 } 216 SetViewFirstUpdating(std::chrono::time_point<std::chrono::high_resolution_clock> time)217 virtual void SetViewFirstUpdating(std::chrono::time_point<std::chrono::high_resolution_clock> time) {} 218 UpdateResourceConfiguration(const std::string & jsonStr)219 virtual void UpdateResourceConfiguration(const std::string& jsonStr) {} 220 221 static int32_t CurrentId(); 222 static RefPtr<Container> Current(); 223 static RefPtr<Container> GetActive(); 224 static RefPtr<TaskExecutor> CurrentTaskExecutor(); 225 static void UpdateCurrent(int32_t id); 226 SetUseNewPipeline()227 void SetUseNewPipeline() 228 { 229 useNewPipeline_ = true; 230 } 231 IsUseNewPipeline()232 bool IsUseNewPipeline() const 233 { 234 return useNewPipeline_; 235 } 236 IsCurrentUseNewPipeline()237 static bool IsCurrentUseNewPipeline() 238 { 239 auto container = Current(); 240 return container ? container->useNewPipeline_ : false; 241 } 242 243 // SetCurrentUsePartialUpdate is called when initial render on a page 244 // starts, see zyz_view_register loadDocument() implementation IsCurrentUsePartialUpdate()245 static bool IsCurrentUsePartialUpdate() 246 { 247 auto container = Current(); 248 return container ? container->usePartialUpdate_ : false; 249 } 250 251 static void SetCurrentUsePartialUpdate(bool useIt = false) 252 { 253 auto container = Current(); 254 if (container) { 255 container->usePartialUpdate_ = useIt; 256 } 257 } 258 GetWindow()259 Window* GetWindow() const 260 { 261 auto context = GetPipelineContext(); 262 return context ? context->GetWindow() : nullptr; 263 } 264 IsUseStageModel()265 virtual bool IsUseStageModel() const 266 { 267 return false; 268 } 269 GetCardFrontendMap(std::unordered_map<int64_t,WeakPtr<Frontend>> & cardFrontendMap)270 virtual void GetCardFrontendMap(std::unordered_map<int64_t, WeakPtr<Frontend>>& cardFrontendMap) const {} 271 SetSharedRuntime(void * runtime)272 virtual void SetSharedRuntime(void* runtime) {} GetSharedRuntime()273 virtual void* GetSharedRuntime() 274 { 275 return nullptr; 276 } 277 IsFRSCardContainer()278 bool IsFRSCardContainer() const 279 { 280 return isFRSCardContainer_; 281 } 282 SetIsFRSCardContainer(bool isFRSCardContainer)283 void SetIsFRSCardContainer(bool isFRSCardContainer) 284 { 285 isFRSCardContainer_ = isFRSCardContainer; 286 } 287 SetPageUrlChecker(const RefPtr<PageUrlChecker> & pageUrlChecker)288 void SetPageUrlChecker(const RefPtr<PageUrlChecker>& pageUrlChecker) 289 { 290 pageUrlChecker_ = pageUrlChecker; 291 } 292 GetPageUrlChecker()293 const RefPtr<PageUrlChecker>& GetPageUrlChecker() 294 { 295 return pageUrlChecker_; 296 } 297 IsDialogContainer()298 virtual bool IsDialogContainer() const 299 { 300 return false; 301 } 302 303 virtual void NotifyConfigurationChange(bool, const OnConfigurationChange& configurationChange = {false, false}) {} HotReload()304 virtual void HotReload() {} 305 SetIsModule(bool isModule)306 void SetIsModule(bool isModule) 307 { 308 isModule_ = isModule; 309 } 310 IsModule()311 bool IsModule() const 312 { 313 return isModule_; 314 } 315 SetDistributedUI(std::shared_ptr<NG::DistributedUI> & distributedUI)316 void SetDistributedUI(std::shared_ptr<NG::DistributedUI>& distributedUI) 317 { 318 distributedUI_ = distributedUI; 319 } 320 GetDistributedUI()321 std::shared_ptr<NG::DistributedUI>& GetDistributedUI() 322 { 323 return distributedUI_; 324 } 325 IsLauncherContainer()326 virtual bool IsLauncherContainer() 327 { 328 return false; 329 } 330 IsScenceBoardWindow()331 virtual bool IsScenceBoardWindow() 332 { 333 return false; 334 } 335 IsSceneBoardEnabled()336 virtual bool IsSceneBoardEnabled() 337 { 338 return false; 339 } 340 GetCurPointerEventInfo(int32_t pointerId,int32_t & globalX,int32_t & globalY,int32_t & sourceType,StopDragCallback && stopDragCallback)341 virtual bool GetCurPointerEventInfo( 342 int32_t pointerId, int32_t& globalX, int32_t& globalY, int32_t& sourceType, StopDragCallback&& stopDragCallback) 343 { 344 return false; 345 } 346 LessThanAPIVersion(PlatformVersion version)347 static bool LessThanAPIVersion(PlatformVersion version) 348 { 349 if (PipelineBase::GetCurrentContext() && 350 PipelineBase::GetCurrentContext()->GetMinPlatformVersion() < static_cast<int32_t>(version)) { 351 return true; 352 } 353 return false; 354 } 355 356 protected: 357 std::chrono::time_point<std::chrono::high_resolution_clock> createTime_; 358 bool firstUpdateData_ = true; 359 std::string cardHapPath_; 360 bool useNewPipeline_ = false; 361 std::mutex stateMutex_; 362 Frontend::State state_ = Frontend::State::UNDEFINE; 363 bool isFRSCardContainer_ = false; 364 365 private: 366 std::string moduleName_; 367 std::string bundlePath_; 368 std::string filesDataPath_; 369 bool usePartialUpdate_ = false; 370 Settings settings_; 371 RefPtr<PageUrlChecker> pageUrlChecker_; 372 bool isModule_ = false; 373 std::shared_ptr<NG::DistributedUI> distributedUI_; 374 ACE_DISALLOW_COPY_AND_MOVE(Container); 375 }; 376 377 } // namespace OHOS::Ace 378 379 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_CONTAINER_H 380