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_ADAPTER_OHOS_CPP_ACE_CONTAINER_H 17 #define FOUNDATION_ACE_ADAPTER_OHOS_CPP_ACE_CONTAINER_H 18 19 #include <cstddef> 20 #include <memory> 21 #include <mutex> 22 23 #include "native_engine/native_reference.h" 24 #include "native_engine/native_value.h" 25 26 #include "adapter/ohos/entrance/ace_ability.h" 27 #include "adapter/ohos/entrance/platform_event_callback.h" 28 #include "base/resource/asset_manager.h" 29 #include "base/thread/task_executor.h" 30 #include "base/utils/noncopyable.h" 31 #include "core/common/ace_view.h" 32 #include "core/common/container.h" 33 #include "core/common/js_message_dispatcher.h" 34 #include "core/pipeline/pipeline_context.h" 35 #include "base/memory/ace_type.h" 36 37 namespace OHOS::Ace::Platform { 38 using UIEnvCallback = std::function<void(const OHOS::Ace::RefPtr<OHOS::Ace::PipelineContext>& context)>; 39 using SharePanelCallback = std::function<void(const std::string& bundleName, const std::string& abilityName)>; 40 class ACE_FORCE_EXPORT AceContainer : public Container, public JsMessageDispatcher { 41 DECLARE_ACE_TYPE(AceContainer, Container, JsMessageDispatcher); 42 43 public: 44 AceContainer(int32_t instanceId, FrontendType type, 45 std::shared_ptr<OHOS::AppExecFwk::Ability> aceAbility, std::unique_ptr<PlatformEventCallback> callback, 46 bool useCurrentEventRunner = false, bool useNewPipeline = false); 47 AceContainer(int32_t instanceId, FrontendType type, 48 std::weak_ptr<OHOS::AbilityRuntime::Context> runtimeContext, 49 std::weak_ptr<OHOS::AppExecFwk::AbilityInfo> abilityInfo, std::unique_ptr<PlatformEventCallback> callback, 50 bool useCurrentEventRunner = false, bool isSubContainer = false, bool useNewPipeline = false); 51 ~AceContainer() override; 52 53 void Initialize() override; 54 55 void Destroy() override; 56 57 void DestroyView() override; 58 59 static bool Register(); 60 GetInstanceId()61 int32_t GetInstanceId() const override 62 { 63 if (aceView_) { 64 return aceView_->GetInstanceId(); 65 } 66 return -1; 67 } 68 GetFrontend()69 RefPtr<Frontend> GetFrontend() const override 70 { 71 std::lock_guard<std::mutex> lock(frontendMutex_); 72 return frontend_; 73 } 74 SetCardFrontend(WeakPtr<Frontend> frontend,int64_t cardId)75 void SetCardFrontend(WeakPtr<Frontend> frontend, int64_t cardId) override 76 { 77 std::lock_guard<std::mutex> lock(cardFrontMutex_); 78 cardFrontendMap_.try_emplace(cardId, frontend); 79 } 80 GetCardFrontend(int64_t cardId)81 WeakPtr<Frontend> GetCardFrontend(int64_t cardId) const override 82 { 83 std::lock_guard<std::mutex> lock(cardFrontMutex_); 84 auto it = cardFrontendMap_.find(cardId); 85 if (it != cardFrontendMap_.end()) { 86 return it->second; 87 } 88 return nullptr; 89 } 90 SetCardPipeline(WeakPtr<PipelineBase> pipeline,int64_t cardId)91 void SetCardPipeline(WeakPtr<PipelineBase> pipeline, int64_t cardId) override 92 { 93 std::lock_guard<std::mutex> lock(cardPipelineMutex_); 94 cardPipelineMap_.try_emplace(cardId, pipeline); 95 } 96 GetCardPipeline(int64_t cardId)97 WeakPtr<PipelineBase> GetCardPipeline(int64_t cardId) const override 98 { 99 std::lock_guard<std::mutex> lock(cardPipelineMutex_); 100 auto it = cardPipelineMap_.find(cardId); 101 if (it == cardPipelineMap_.end()) { 102 return nullptr; 103 } 104 return it->second; 105 } 106 GetTaskExecutor()107 RefPtr<TaskExecutor> GetTaskExecutor() const override 108 { 109 return taskExecutor_; 110 } 111 SetAssetManager(const RefPtr<AssetManager> & assetManager)112 void SetAssetManager(const RefPtr<AssetManager>& assetManager) 113 { 114 assetManager_ = assetManager; 115 if (frontend_) { 116 frontend_->SetAssetManager(assetManager); 117 } 118 } 119 GetAssetManager()120 RefPtr<AssetManager> GetAssetManager() const override 121 { 122 return assetManager_; 123 } 124 GetPlatformResRegister()125 RefPtr<PlatformResRegister> GetPlatformResRegister() const override 126 { 127 return resRegister_; 128 } 129 GetPipelineContext()130 RefPtr<PipelineBase> GetPipelineContext() const override 131 { 132 std::lock_guard<std::mutex> lock(pipelineMutex_); 133 return pipelineContext_; 134 } 135 GetViewWidth()136 int32_t GetViewWidth() const override 137 { 138 return aceView_ ? aceView_->GetWidth() : 0; 139 } 140 GetViewHeight()141 int32_t GetViewHeight() const override 142 { 143 return aceView_ ? aceView_->GetHeight() : 0; 144 } 145 GetViewPosX()146 int32_t GetViewPosX() const override 147 { 148 return aceView_ ? aceView_->GetPosX() : 0; 149 } 150 GetViewPosY()151 int32_t GetViewPosY() const override 152 { 153 return aceView_ ? aceView_->GetPosY() : 0; 154 } 155 GetAceView()156 AceView* GetAceView() const 157 { 158 return aceView_; 159 } 160 GetView()161 void* GetView() const override 162 { 163 return static_cast<void*>(aceView_); 164 } 165 SetWindowModal(WindowModal windowModal)166 void SetWindowModal(WindowModal windowModal) 167 { 168 windowModal_ = windowModal; 169 } 170 SetInstallationFree(bool installationFree)171 void SetInstallationFree(bool installationFree) 172 { 173 installationFree_ = installationFree; 174 } 175 SetSharePanelCallback(SharePanelCallback && callback)176 void SetSharePanelCallback(SharePanelCallback&& callback) 177 { 178 sharePanelCallback_ = std::move(callback); 179 } 180 SetColorScheme(ColorScheme colorScheme)181 void SetColorScheme(ColorScheme colorScheme) 182 { 183 colorScheme_ = colorScheme; 184 } 185 GetResourceConfiguration()186 ResourceConfiguration GetResourceConfiguration() const 187 { 188 return resourceInfo_.GetResourceConfiguration(); 189 } 190 SetResourceConfiguration(const ResourceConfiguration & config)191 void SetResourceConfiguration(const ResourceConfiguration& config) 192 { 193 resourceInfo_.SetResourceConfiguration(config); 194 } 195 GetPackagePathStr()196 std::string GetPackagePathStr() const 197 { 198 return resourceInfo_.GetPackagePath(); 199 } 200 SetPackagePathStr(const std::string & packagePath)201 void SetPackagePathStr(const std::string& packagePath) 202 { 203 resourceInfo_.SetPackagePath(packagePath); 204 } 205 GetHapPath()206 std::string GetHapPath() const override 207 { 208 return resourceInfo_.GetHapPath(); 209 } 210 211 void SetHapPath(const std::string& hapPath); 212 213 void Dispatch( 214 const std::string& group, std::vector<uint8_t>&& data, int32_t id, bool replyToComponent) const override; 215 DispatchSync(const std::string & group,std::vector<uint8_t> && data,uint8_t ** resData,int64_t & position)216 void DispatchSync( 217 const std::string& group, std::vector<uint8_t>&& data, uint8_t** resData, int64_t& position) const override 218 {} 219 220 void DispatchPluginError(int32_t callbackId, int32_t errorCode, std::string&& errorMessage) const override; 221 222 bool Dump(const std::vector<std::string>& params, std::vector<std::string>& info) override; 223 224 bool DumpInfo(const std::vector<std::string>& params); 225 226 bool OnDumpInfo(const std::vector<std::string>& params); 227 228 void TriggerGarbageCollection() override; 229 230 void DumpHeapSnapshot(bool isPrivate) override; 231 232 void SetLocalStorage(NativeReference* storage, NativeReference* context); 233 OnFinish()234 void OnFinish() 235 { 236 if (platformEventCallback_) { 237 platformEventCallback_->OnFinish(); 238 } 239 } 240 OnStartAbility(const std::string & address)241 void OnStartAbility(const std::string& address) 242 { 243 if (platformEventCallback_) { 244 platformEventCallback_->OnStartAbility(address); 245 } 246 } 247 GeneratePageId()248 int32_t GeneratePageId() 249 { 250 return pageId_++; 251 } 252 GetHostClassName()253 std::string GetHostClassName() const override 254 { 255 return ""; 256 } 257 SetSharedRuntime(void * runtime)258 void SetSharedRuntime(void* runtime) override 259 { 260 sharedRuntime_ = runtime; 261 } 262 SetPageProfile(const std::string & pageProfile)263 void SetPageProfile(const std::string& pageProfile) 264 { 265 pageProfile_ = pageProfile; 266 } 267 IsSubContainer()268 bool IsSubContainer() const override 269 { 270 return isSubContainer_; 271 } 272 IsFormRender()273 bool IsFormRender() const 274 { 275 return isFormRender_; 276 } 277 GetSharedRuntime()278 void* GetSharedRuntime() override 279 { 280 return sharedRuntime_; 281 } 282 SetParentId(int32_t parentId)283 void SetParentId(int32_t parentId) 284 { 285 parentId_ = parentId; 286 } 287 GetParentId()288 int32_t GetParentId() const 289 { 290 return parentId_; 291 } 292 SetFocusWindowId(uint32_t focusWindowId)293 void SetFocusWindowId(uint32_t focusWindowId) 294 { 295 if (pipelineContext_) { 296 pipelineContext_->SetFocusWindowId(focusWindowId); 297 } 298 } 299 300 static void CreateContainer(int32_t instanceId, FrontendType type, const std::string& instanceName, 301 std::shared_ptr<OHOS::AppExecFwk::Ability> aceAbility, std::unique_ptr<PlatformEventCallback> callback, 302 bool useCurrentEventRunner = false, bool useNewPipeline = false); 303 304 static void DestroyContainer(int32_t instanceId, const std::function<void()>& destroyCallback = nullptr); 305 static bool RunPage(int32_t instanceId, int32_t pageId, const std::string& content, const std::string& params); 306 static bool PushPage(int32_t instanceId, const std::string& content, const std::string& params); 307 static bool OnBackPressed(int32_t instanceId); 308 static void OnShow(int32_t instanceId); 309 static void OnHide(int32_t instanceId); 310 static void OnActive(int32_t instanceId); 311 static void OnInactive(int32_t instanceId); 312 static void OnNewWant(int32_t instanceId, const std::string& data); 313 static bool OnStartContinuation(int32_t instanceId); 314 static std::string OnSaveData(int32_t instanceId); 315 static bool OnRestoreData(int32_t instanceId, const std::string& data); 316 static void OnCompleteContinuation(int32_t instanceId, int result); 317 static void OnRemoteTerminated(int32_t instanceId); 318 static void OnConfigurationUpdated(int32_t instanceId, const std::string& configuration); 319 static void OnNewRequest(int32_t instanceId, const std::string& data); 320 static void AddAssetPath(int32_t instanceId, const std::string& packagePath, const std::string& hapPath, 321 const std::vector<std::string>& paths); 322 static void AddLibPath(int32_t instanceId, const std::vector<std::string>& libPath); 323 static void SetView(AceView* view, double density, int32_t width, int32_t height, 324 sptr<OHOS::Rosen::Window> rsWindow, UIEnvCallback callback = nullptr); 325 static void SetViewNew( 326 AceView* view, double density, int32_t width, int32_t height, sptr<OHOS::Rosen::Window> rsWindow); 327 static void SetUIWindow(int32_t instanceId, sptr<OHOS::Rosen::Window> uiWindow); 328 static sptr<OHOS::Rosen::Window> GetUIWindow(int32_t instanceId); 329 static OHOS::AppExecFwk::Ability* GetAbility(int32_t instanceId); 330 static void SetFontScale(int32_t instanceId, float fontScale); 331 static void SetWindowStyle(int32_t instanceId, WindowModal windowModal, ColorScheme colorScheme); 332 static std::string RestoreRouterStack(int32_t instanceId, const std::string& contentInfo); 333 static std::string GetContentInfo(int32_t instanceId); 334 335 static RefPtr<AceContainer> GetContainer(int32_t instanceId); 336 static bool UpdatePage(int32_t instanceId, int32_t pageId, const std::string& content); 337 338 // ArkTsCard 339 static std::shared_ptr<Rosen::RSSurfaceNode> GetFormSurfaceNode(int32_t instanceId); 340 SetWindowName(const std::string & name)341 void SetWindowName(const std::string& name) 342 { 343 windowName_ = name; 344 } 345 GetWindowName()346 std::string& GetWindowName() 347 { 348 return windowName_; 349 } 350 SetWindowId(uint32_t windowId)351 void SetWindowId(uint32_t windowId) override 352 { 353 windowId_ = windowId; 354 } 355 GetWindowId()356 uint32_t GetWindowId() const override 357 { 358 return windowId_; 359 } 360 WindowIsShow()361 bool WindowIsShow() const override 362 { 363 if (!uiWindow_) { 364 return false; 365 } 366 return uiWindow_->GetWindowState() == Rosen::WindowState::STATE_SHOWN; 367 } 368 369 void SetWindowPos(int32_t left, int32_t top); 370 SetIsSubContainer(bool isSubContainer)371 void SetIsSubContainer(bool isSubContainer) 372 { 373 isSubContainer_ = isSubContainer; 374 } 375 SetIsFormRender(bool isFormRender)376 void SetIsFormRender(bool isFormRender) 377 { 378 isFormRender_ = isFormRender; 379 } 380 381 void InitializeSubContainer(int32_t parentContainerId); 382 static void SetDialogCallback(int32_t instanceId, FrontendDialogCallback callback); 383 384 std::shared_ptr<OHOS::AbilityRuntime::Context> GetAbilityContextByModule(const std::string& bundle, 385 const std::string& module); 386 387 void UpdateConfiguration(const std::string& colorMode, const std::string& inputDevice, 388 const std::string& languageTag, const std::string& configuration); 389 390 void NotifyConfigurationChange( 391 bool needReloadTransition, const OnConfigurationChange& configurationChange = {false, false}) override; 392 void HotReload() override; 393 IsUseStageModel()394 bool IsUseStageModel() const override 395 { 396 return useStageModel_; 397 } 398 GetCardFrontendMap(std::unordered_map<int64_t,WeakPtr<Frontend>> & cardFrontendMap)399 void GetCardFrontendMap(std::unordered_map<int64_t, WeakPtr<Frontend>>& cardFrontendMap) const override 400 { 401 cardFrontendMap = cardFrontendMap_; 402 } 403 404 void SetToken(sptr<IRemoteObject>& token); 405 sptr<IRemoteObject> GetToken(); 406 GetWebHapPath()407 std::string GetWebHapPath() const override 408 { 409 return webHapPath_; 410 } 411 412 NG::SafeAreaInsets GetViewSafeAreaByType(OHOS::Rosen::AvoidAreaType type); 413 414 // ArkTSCard 415 void UpdateFormData(const std::string& data); 416 void UpdateFormSharedImage(const std::map<std::string, sptr<OHOS::AppExecFwk::FormAshmem>>& imageDataMap); 417 void ReloadForm(); 418 419 void GetNamesOfSharedImage(std::vector<std::string>& picNameArray); 420 void UpdateSharedImage(std::vector<std::string>& picNameArray, std::vector<int32_t>& byteLenArray, 421 std::vector<int32_t>& fileDescriptorArray); 422 void GetImageDataFromAshmem( 423 const std::string& picName, Ashmem& ashmem, const RefPtr<PipelineBase>& pipelineContext, int len); 424 425 bool IsLauncherContainer() override; 426 bool IsScenceBoardWindow() override; 427 bool IsSceneBoardEnabled() override; 428 429 void SetCurPointerEvent(const std::shared_ptr<MMI::PointerEvent>& currentEvent); 430 bool GetCurPointerEventInfo(int32_t pointerId, int32_t& globalX, int32_t& globalY, int32_t& sourceType, 431 StopDragCallback&& stopDragCallback) override; 432 433 private: 434 void InitializeFrontend(); 435 void InitializeCallback(); 436 void InitializeTask(); 437 void InitWindowCallback(); 438 439 void AttachView(std::shared_ptr<Window> window, AceView* view, double density, int32_t width, int32_t height, 440 uint32_t windowId, UIEnvCallback callback = nullptr); 441 void SetUIWindowInner(sptr<OHOS::Rosen::Window> uiWindow); 442 sptr<OHOS::Rosen::Window> GetUIWindowInner() const; 443 std::weak_ptr<OHOS::AppExecFwk::Ability> GetAbilityInner() const; 444 445 void RegisterStopDragCallback(int32_t pointerId, StopDragCallback&& stopDragCallback); 446 447 int32_t instanceId_ = 0; 448 AceView* aceView_ = nullptr; 449 RefPtr<TaskExecutor> taskExecutor_; 450 RefPtr<AssetManager> assetManager_; 451 RefPtr<PlatformResRegister> resRegister_; 452 RefPtr<PipelineBase> pipelineContext_; 453 RefPtr<Frontend> frontend_; 454 std::unordered_map<int64_t, WeakPtr<Frontend>> cardFrontendMap_; 455 std::unordered_map<int64_t, WeakPtr<PipelineBase>> cardPipelineMap_; 456 457 FrontendType type_ = FrontendType::JS; 458 std::unique_ptr<PlatformEventCallback> platformEventCallback_; 459 WindowModal windowModal_ { WindowModal::NORMAL }; 460 ColorScheme colorScheme_ { ColorScheme::FIRST_VALUE }; 461 ResourceInfo resourceInfo_; 462 std::weak_ptr<OHOS::AppExecFwk::Ability> aceAbility_; 463 std::weak_ptr<OHOS::AbilityRuntime::Context> runtimeContext_; 464 std::weak_ptr<OHOS::AppExecFwk::AbilityInfo> abilityInfo_; 465 void* sharedRuntime_ = nullptr; 466 std::string pageProfile_; 467 int32_t pageId_ = 0; 468 bool useCurrentEventRunner_ = false; 469 sptr<OHOS::Rosen::Window> uiWindow_ = nullptr; 470 std::string windowName_; 471 uint32_t windowId_ = OHOS::Rosen::INVALID_WINDOW_ID; 472 sptr<IRemoteObject> token_; 473 474 bool isSubContainer_ = false; 475 bool isFormRender_ = false; 476 int32_t parentId_ = 0; 477 bool useStageModel_ = false; 478 479 mutable std::mutex frontendMutex_; 480 mutable std::mutex pipelineMutex_; 481 482 mutable std::mutex cardFrontMutex_; 483 mutable std::mutex cardPipelineMutex_; 484 mutable std::mutex cardTokensMutex_; 485 486 std::string webHapPath_; 487 488 bool installationFree_ = false; 489 SharePanelCallback sharePanelCallback_ = nullptr; 490 491 std::atomic_flag isDumping_ = ATOMIC_FLAG_INIT; 492 493 // For custom drag event 494 std::mutex pointerEventMutex_; 495 std::shared_ptr<MMI::PointerEvent> currentPointerEvent_; 496 std::unordered_map<int32_t, std::list<StopDragCallback>> stopDragCallbackMap_; 497 ACE_DISALLOW_COPY_AND_MOVE(AceContainer); 498 }; 499 500 } // namespace OHOS::Ace::Platform 501 502 #endif // FOUNDATION_ACE_ADAPTER_OHOS_CPP_ACE_CONTAINER_H 503