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_PREVIEW_ACE_CONTAINER_H 17 #define FOUNDATION_ACE_ADAPTER_PREVIEW_ACE_CONTAINER_H 18 19 #include <memory> 20 #include <mutex> 21 #include <string> 22 #include <vector> 23 24 #include "adapter/preview/entrance/ace_run_args.h" 25 #include "adapter/preview/entrance/ace_view_preview.h" 26 #include "adapter/preview/external/ability/stage/stage_pkg_context_info.h" 27 #include "adapter/preview/osal/fetch_manager.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/common/platform_bridge.h" 35 #include "frameworks/bridge/js_frontend/engine/common/js_engine.h" 36 #include "core/event/crown_event.h" 37 38 #include <refbase.h> 39 40 namespace OHOS::Rosen { 41 class Window; 42 } 43 44 namespace OHOS::Ace::Platform { 45 46 namespace { 47 // Different with mobile, we don't support multi-instances in Windows, because we only want 48 // preview UI effect, it doesn't make sense to create multi ability in one process. 49 constexpr int32_t ACE_INSTANCE_ID = 0; 50 } // namespace 51 52 using UIEnvCallback = std::function<void(const OHOS::Ace::RefPtr<PipelineContext>& context)>; 53 using OnRouterChangeCallback = bool (*)(const std::string currentRouterPath); 54 55 // AceContainer is the instance have its own pipeline and thread models, it can contains multiple pages. 56 class AceContainer : public Container, public JsMessageDispatcher { 57 DECLARE_ACE_TYPE(AceContainer, Container, JsMessageDispatcher); 58 59 public: 60 static void CreateContainer( 61 int32_t instanceId, FrontendType type, bool useNewPipeline, bool useCurrentEventRunner = false); 62 static void DestroyContainer(int32_t instanceId); 63 64 static void AddAssetPath(int32_t instanceId, const std::string& packagePath, const std::vector<std::string>& paths); 65 static void SetResourcesPathAndThemeStyle(int32_t instanceId, const std::string& systemResourcesPath, 66 const std::string& hmsResourcesPath, const std::string& appResourcesPath, const int32_t& themeId, 67 const ColorMode& colorMode); 68 69 #ifndef ENABLE_ROSEN_BACKEND 70 static void SetView(AceViewPreview* view, double density, int32_t width, int32_t height); 71 #else 72 static void SetView(AceViewPreview* view, sptr<Rosen::Window> rsWindow, double density, int32_t width, 73 int32_t height, UIEnvCallback callback); 74 #endif 75 76 static UIContentErrorCode RunPage( 77 int32_t instanceId, const std::string& url, const std::string& params, bool isNamedRouter = false); 78 static RefPtr<AceContainer> GetContainerInstance(int32_t instanceId); 79 static void AddRouterChangeCallback(int32_t instanceId, const OnRouterChangeCallback& onRouterChangeCallback); 80 static void NativeOnConfigurationUpdated(int32_t instanceId); 81 82 AceContainer(int32_t instanceId, FrontendType type, bool useNewPipeline, bool useCurrentEventRunner = false); 83 ~AceContainer() override = default; 84 85 void Initialize() override; 86 87 void Destroy() override; 88 89 void DestroyView() override; 90 GetInstanceId()91 int32_t GetInstanceId() const override 92 { 93 return instanceId_; 94 } 95 GetHostClassName()96 std::string GetHostClassName() const override 97 { 98 return ""; 99 } 100 GetFrontend()101 RefPtr<Frontend> GetFrontend() const override 102 { 103 return frontend_; 104 } 105 GetMessageBridge()106 RefPtr<PlatformBridge> GetMessageBridge() const 107 { 108 return messageBridge_; 109 } 110 GetTaskExecutor()111 RefPtr<TaskExecutor> GetTaskExecutor() const override 112 { 113 return taskExecutor_; 114 } 115 GetAssetManager()116 RefPtr<AssetManager> GetAssetManager() const override 117 { 118 return assetManager_; 119 } 120 GetPlatformResRegister()121 RefPtr<PlatformResRegister> GetPlatformResRegister() const override 122 { 123 return resRegister_; 124 } 125 GetPipelineContext()126 RefPtr<PipelineBase> GetPipelineContext() const override 127 { 128 return pipelineContext_; 129 } 130 GetViewWidth()131 int32_t GetViewWidth() const override 132 { 133 return aceView_ ? aceView_->GetWidth() : 0; 134 } 135 GetViewHeight()136 int32_t GetViewHeight() const override 137 { 138 return aceView_ ? aceView_->GetHeight() : 0; 139 } 140 GetViewPosX()141 int32_t GetViewPosX() const override 142 { 143 return 0; 144 } 145 GetViewPosY()146 int32_t GetViewPosY() const override 147 { 148 return 0; 149 } 150 GetWindowId()151 uint32_t GetWindowId() const override 152 { 153 return 0; 154 } 155 SetWindowId(uint32_t windowId)156 void SetWindowId(uint32_t windowId) override {} 157 WindowIsShow()158 bool WindowIsShow() const override 159 { 160 return true; 161 } 162 GetAceView()163 RefPtr<AceView> GetAceView() const override 164 { 165 return aceView_; 166 } 167 GetView()168 void* GetView() const override 169 { 170 return static_cast<void*>(AceType::RawPtr(aceView_)); 171 } 172 SetWindowModal(WindowModal windowModal)173 void SetWindowModal(WindowModal windowModal) 174 { 175 windowModal_ = windowModal; 176 } 177 SetColorScheme(ColorScheme colorScheme)178 void SetColorScheme(ColorScheme colorScheme) 179 { 180 colorScheme_ = colorScheme; 181 } 182 GetType()183 FrontendType GetType() const 184 { 185 return type_; 186 } 187 GetResourceConfiguration()188 ResourceConfiguration GetResourceConfiguration() const override 189 { 190 return resourceInfo_.GetResourceConfiguration(); 191 } 192 SetResourceConfiguration(const ResourceConfiguration & config)193 void SetResourceConfiguration(const ResourceConfiguration& config) 194 { 195 resourceInfo_.SetResourceConfiguration(config); 196 } 197 198 void UpdateResourceConfiguration(const std::string& jsonStr) override; 199 200 void FetchResponse(const ResponseData responseData, const int32_t callbackId) const; 201 202 void CallCurlFunction(const RequestData requestData, const int32_t callbackId) const override; 203 204 void Dispatch( 205 const std::string& group, std::vector<uint8_t>&& data, int32_t id, bool replyToComponent) const override; 206 DispatchSync(const std::string & group,std::vector<uint8_t> && data,uint8_t ** resData,int64_t & position)207 void DispatchSync( 208 const std::string& group, std::vector<uint8_t>&& data, uint8_t** resData, int64_t& position) const override 209 {} 210 211 void DispatchPluginError(int32_t callbackId, int32_t errorCode, std::string&& errorMessage) const override; 212 213 void UpdateDeviceConfig(const DeviceConfig& deviceConfig); 214 215 void LoadDocument(const std::string& url, const std::string& componentName); 216 217 void RunNativeEngineLoop(); 218 219 void SetStageCardConfig(const std::string& pageProfile, const std::string& selectUrl); 220 221 void SetPkgContextInfo(const RefPtr<StagePkgContextInfo>& PkgContextInfo); 222 SetPageProfile(const std::string & pageProfile)223 void SetPageProfile(const std::string& pageProfile) 224 { 225 pageProfile_ = pageProfile; 226 } 227 228 void InitializeAppConfig(const std::string& assetPath, const std::string& bundleName, 229 const std::string& moduleName, const std::string& compileMode); 230 SetCardFrontend(WeakPtr<Frontend> frontend,int64_t cardId)231 void SetCardFrontend(WeakPtr<Frontend> frontend, int64_t cardId) override 232 { 233 std::lock_guard<std::mutex> lock(cardFrontMutex_); 234 cardFrontendMap_.try_emplace(cardId, frontend); 235 } 236 GetCardFrontend(int64_t cardId)237 WeakPtr<Frontend> GetCardFrontend(int64_t cardId) const override 238 { 239 std::lock_guard<std::mutex> lock(cardFrontMutex_); 240 auto it = cardFrontendMap_.find(cardId); 241 if (it != cardFrontendMap_.end()) { 242 return it->second; 243 } 244 return nullptr; 245 } 246 GetCardFrontendMap(std::unordered_map<int64_t,WeakPtr<Frontend>> & cardFrontendMap)247 void GetCardFrontendMap(std::unordered_map<int64_t, WeakPtr<Frontend>>& cardFrontendMap) const override 248 { 249 cardFrontendMap = cardFrontendMap_; 250 } 251 SetCardPipeline(WeakPtr<PipelineBase> pipeline,int64_t cardId)252 void SetCardPipeline(WeakPtr<PipelineBase> pipeline, int64_t cardId) override 253 { 254 std::lock_guard<std::mutex> lock(cardPipelineMutex_); 255 cardPipelineMap_.try_emplace(cardId, pipeline); 256 } 257 GetCardPipeline(int64_t cardId)258 WeakPtr<PipelineBase> GetCardPipeline(int64_t cardId) const override 259 { 260 std::lock_guard<std::mutex> lock(cardPipelineMutex_); 261 auto it = cardPipelineMap_.find(cardId); 262 if (it == cardPipelineMap_.end()) { 263 return nullptr; 264 } 265 return it->second; 266 } 267 268 static std::string GetContentInfo(int32_t instanceId, ContentInfoType type); SetSharedRuntime(void * runtime)269 void SetSharedRuntime(void* runtime) override 270 { 271 sharedRuntime_ = runtime; 272 } SetInstallationFree(bool installationFree)273 void SetInstallationFree(bool installationFree) 274 { 275 installationFree_ = installationFree; 276 } 277 SetLabelId(int32_t labelId)278 void SetLabelId(int32_t labelId) 279 { 280 labelId_ = labelId; 281 } SetComponentModeFlag(bool isComponentMode)282 static void SetComponentModeFlag(bool isComponentMode) 283 { 284 isComponentMode_ = isComponentMode; 285 } 286 SetContainerSdkPath(const std::string & containerSdkPath)287 void SetContainerSdkPath(const std::string& containerSdkPath) 288 { 289 containerSdkPath_ = containerSdkPath; 290 } 291 292 void NotifyConfigurationChange(bool, const ConfigurationChange& configurationChange) override; 293 294 // Support to execute the ets code mocked by developer SetMockModuleList(const std::map<std::string,std::string> & mockJsonInfo)295 void SetMockModuleList(const std::map<std::string, std::string>& mockJsonInfo) 296 { 297 mockJsonInfo_ = mockJsonInfo; 298 } 299 SetBundleName(const std::string & bundleName)300 void SetBundleName(const std::string& bundleName) 301 { 302 bundleName_ = bundleName; 303 } 304 SetModuleName(const std::string & moduleName)305 void SetModuleName(const std::string& moduleName) 306 { 307 moduleName_ = moduleName; 308 } 309 RegisterCrownEventCallback(CrownEventCallback && callback)310 void RegisterCrownEventCallback(CrownEventCallback&& callback) 311 { 312 ACE_DCHECK(callback); 313 crownEventCallback_ = std::move(callback); 314 } 315 316 private: 317 void InitializeFrontend(); 318 void InitializeCallback(); 319 void SetHspBufferTrackerCallback(); 320 // Support to execute the ets code mocked by developer 321 void SetMockModuleListToJsEngine(); 322 323 #ifndef ENABLE_ROSEN_BACKEND 324 void AttachView( 325 std::shared_ptr<Window> window, AceViewPreview* view, double density, int32_t width, int32_t height); 326 #else 327 void AttachView(std::shared_ptr<Window> window, AceViewPreview* view, double density, int32_t width, int32_t height, 328 UIEnvCallback callback); 329 #endif 330 331 RefPtr<AceViewPreview> aceView_ = nullptr; 332 int32_t instanceId_; 333 RefPtr<TaskExecutor> taskExecutor_; 334 RefPtr<AssetManager> assetManager_; 335 RefPtr<PlatformResRegister> resRegister_; 336 RefPtr<PipelineBase> pipelineContext_; 337 RefPtr<Frontend> frontend_; 338 RefPtr<PlatformBridge> messageBridge_; 339 FrontendType type_ { FrontendType::JSON }; 340 WindowModal windowModal_ { WindowModal::NORMAL }; 341 ColorScheme colorScheme_ { ColorScheme::SCHEME_LIGHT }; 342 ResourceInfo resourceInfo_; 343 static std::once_flag onceFlag_; 344 std::string pageProfile_; 345 std::unordered_map<int64_t, WeakPtr<Frontend>> cardFrontendMap_; 346 std::unordered_map<int64_t, WeakPtr<PipelineBase>> cardPipelineMap_; 347 mutable std::mutex cardFrontMutex_; 348 mutable std::mutex cardPipelineMutex_; 349 void* sharedRuntime_ = nullptr; 350 std::string bundleName_; 351 std::string moduleName_; 352 RefPtr<StagePkgContextInfo> PkgContextInfo_; 353 354 CrownEventCallback crownEventCallback_; 355 356 // Support to execute the ets code mocked by developer 357 std::map<std::string, std::string> mockJsonInfo_; 358 359 // app bar to use 360 bool installationFree_ = false; 361 int32_t labelId_; 362 static bool isComponentMode_; 363 std::string containerSdkPath_; 364 friend class WindowFreeContainer; 365 366 ACE_DISALLOW_COPY_AND_MOVE(AceContainer); 367 }; 368 369 } // namespace OHOS::Ace::Platform 370 371 #endif // FOUNDATION_ACE_ADAPTER_PREVIEW_ACE_CONTAINER_H 372