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