1 /* 2 * Copyright (c) 2021 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 <memory> 20 21 #include "ability_context.h" 22 #include "native_engine/native_reference.h" 23 #include "native_engine/native_value.h" 24 25 #include "adapter/ohos/entrance/ace_ability.h" 26 #include "adapter/ohos/entrance/platform_event_callback.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 34 namespace OHOS::Ace::Platform { 35 using UIEnvCallback = std::function<void(const OHOS::Ace::RefPtr<OHOS::Ace::PipelineContext>& context)>; 36 class ACE_FORCE_EXPORT AceContainer : public Container, public JsMessageDispatcher { 37 DECLARE_ACE_TYPE(AceContainer, Container, JsMessageDispatcher); 38 39 public: 40 AceContainer(int32_t instanceId, FrontendType type, bool isArkApp, 41 std::shared_ptr<OHOS::AppExecFwk::Ability> aceAbility, std::unique_ptr<PlatformEventCallback> callback, 42 bool useCurrentEventRunner = false); 43 AceContainer(int32_t instanceId, FrontendType type, bool isArkApp, 44 std::weak_ptr<OHOS::AbilityRuntime::Context> runtimeContext, 45 std::weak_ptr<OHOS::AppExecFwk::AbilityInfo> abilityInfo, std::unique_ptr<PlatformEventCallback> callback, 46 bool useCurrentEventRunner = false, bool isSubContainer = false); 47 ~AceContainer() override = default; 48 49 void Initialize() override; 50 51 void Destroy() override; 52 53 void DestroyView() override; 54 55 static bool Register(); 56 GetInstanceId()57 int32_t GetInstanceId() const override 58 { 59 if (aceView_) { 60 return aceView_->GetInstanceId(); 61 } 62 return 0; 63 } 64 GetFrontend()65 RefPtr<Frontend> GetFrontend() const override 66 { 67 return frontend_; 68 } 69 GetTaskExecutor()70 RefPtr<TaskExecutor> GetTaskExecutor() const override 71 { 72 return taskExecutor_; 73 } 74 SetAssetManager(RefPtr<AssetManager> assetManager)75 void SetAssetManager(RefPtr<AssetManager> assetManager) 76 { 77 assetManager_ = assetManager; 78 if (frontend_) { 79 frontend_->SetAssetManager(assetManager); 80 } 81 } 82 GetAssetManager()83 RefPtr<AssetManager> GetAssetManager() const override 84 { 85 return assetManager_; 86 } 87 GetPlatformResRegister()88 RefPtr<PlatformResRegister> GetPlatformResRegister() const override 89 { 90 return resRegister_; 91 } 92 GetPipelineContext()93 RefPtr<PipelineContext> GetPipelineContext() const override 94 { 95 return pipelineContext_; 96 } 97 GetViewWidth()98 int32_t GetViewWidth() const override 99 { 100 return aceView_ ? aceView_->GetWidth() : 0; 101 } 102 GetViewHeight()103 int32_t GetViewHeight() const override 104 { 105 return aceView_ ? aceView_->GetHeight() : 0; 106 } 107 GetAceView()108 AceView* GetAceView() const 109 { 110 return aceView_; 111 } 112 GetView()113 void* GetView() const override 114 { 115 return static_cast<void*>(aceView_); 116 } 117 SetWindowModal(WindowModal windowModal)118 void SetWindowModal(WindowModal windowModal) 119 { 120 windowModal_ = windowModal; 121 } 122 SetColorScheme(ColorScheme colorScheme)123 void SetColorScheme(ColorScheme colorScheme) 124 { 125 colorScheme_ = colorScheme; 126 } 127 GetResourceConfiguration()128 ResourceConfiguration GetResourceConfiguration() const 129 { 130 return resourceInfo_.GetResourceConfiguration(); 131 } 132 SetResourceConfiguration(const ResourceConfiguration & config)133 void SetResourceConfiguration(const ResourceConfiguration& config) 134 { 135 resourceInfo_.SetResourceConfiguration(config); 136 } 137 GetPackagePathStr()138 std::string GetPackagePathStr() const 139 { 140 return resourceInfo_.GetPackagePath(); 141 } 142 SetPackagePathStr(const std::string & packagePath)143 void SetPackagePathStr(const std::string& packagePath) 144 { 145 resourceInfo_.SetPackagePath(packagePath); 146 } 147 148 void Dispatch( 149 const std::string& group, std::vector<uint8_t>&& data, int32_t id, bool replyToComponent) const override; 150 DispatchSync(const std::string & group,std::vector<uint8_t> && data,uint8_t ** resData,long & position)151 void DispatchSync( 152 const std::string& group, std::vector<uint8_t>&& data, uint8_t** resData, long& position) const override 153 {} 154 155 void DispatchPluginError(int32_t callbackId, int32_t errorCode, std::string&& errorMessage) const override; 156 157 bool Dump(const std::vector<std::string>& params) override; 158 159 void TriggerGarbageCollection() override; 160 161 void SetContentStorage(NativeReference* storage, NativeReference* context); 162 OnFinish()163 void OnFinish() 164 { 165 if (platformEventCallback_) { 166 platformEventCallback_->OnFinish(); 167 } 168 } 169 GeneratePageId()170 int32_t GeneratePageId() 171 { 172 return pageId_++; 173 } 174 GetHostClassName()175 std::string GetHostClassName() const override 176 { 177 return ""; 178 } 179 SetSharedRuntime(void * runtime)180 void SetSharedRuntime(void* runtime) 181 { 182 sharedRuntime_ = runtime; 183 } 184 SetPageProfile(const std::string & pageProfile)185 void SetPageProfile(const std::string& pageProfile) 186 { 187 pageProfile_ = pageProfile; 188 } 189 IsSubContainer()190 bool IsSubContainer() const override 191 { 192 return isSubContainer_; 193 } 194 GetSharedRuntime()195 void* GetSharedRuntime() 196 { 197 return sharedRuntime_; 198 } 199 SetParentId(int32_t parentId)200 void SetParentId(int32_t parentId) 201 { 202 parentId_ = parentId; 203 } 204 GetParentId()205 int32_t GetParentId() 206 { 207 return parentId_; 208 } 209 210 static void CreateContainer(int32_t instanceId, FrontendType type, bool isArkApp, std::string instanceName, 211 std::shared_ptr<OHOS::AppExecFwk::Ability> aceAbility, std::unique_ptr<PlatformEventCallback> callback, 212 bool useCurrentEventRunner = false); 213 214 static void DestroyContainer(int32_t instanceId); 215 static bool RunPage(int32_t instanceId, int32_t pageId, const std::string& content, const std::string& params); 216 static bool PushPage(int32_t instanceId, const std::string& content, const std::string& params); 217 static bool OnBackPressed(int32_t instanceId); 218 static void OnShow(int32_t instanceId); 219 static void OnHide(int32_t instanceId); 220 static void OnActive(int32_t instanceId); 221 static void OnInactive(int32_t instanceId); 222 static bool OnStartContinuation(int32_t instanceId); 223 static std::string OnSaveData(int32_t instanceId); 224 static bool OnRestoreData(int32_t instanceId, const std::string& data); 225 static void OnCompleteContinuation(int32_t instanceId, int result); 226 static void OnRemoteTerminated(int32_t instanceId); 227 static void OnConfigurationUpdated(int32_t instanceId, const std::string& configuration); 228 static void OnNewRequest(int32_t instanceId, const std::string& data); 229 static void AddAssetPath(int32_t instanceId, const std::string& packagePath, const std::vector<std::string>& paths); 230 static void AddLibPath(int32_t instanceId, const std::string& libPath); 231 static void SetView(AceView* view, double density, int32_t width, int32_t height, int32_t windowId, 232 UIEnvCallback callback = nullptr); 233 static void SetUIWindow(int32_t instanceId, sptr<OHOS::Rosen::Window> uiWindow); 234 static sptr<OHOS::Rosen::Window> GetUIWindow(int32_t instanceId); 235 static OHOS::AppExecFwk::Ability* GetAbility(int32_t instanceId); 236 static void SetFontScale(int32_t instanceId, float fontScale); 237 static void SetWindowStyle(int32_t instanceId, WindowModal windowModal, ColorScheme colorScheme); 238 static std::string RestoreRouterStack(int32_t instanceId, const std::string& contentInfo); 239 static std::string GetContentInfo(int32_t instanceId); 240 241 static RefPtr<AceContainer> GetContainer(int32_t instanceId); 242 static bool UpdatePage(int32_t instanceId, int32_t pageId, const std::string& content); 243 SetWindowName(const std::string & name)244 void SetWindowName(const std::string& name) 245 { 246 windowName_ = name; 247 } 248 GetWindowName()249 std::string& GetWindowName() 250 { 251 return windowName_; 252 } 253 SetIsSubContainer(bool isSubContainer)254 void SetIsSubContainer(bool isSubContainer) 255 { 256 isSubContainer_ = isSubContainer; 257 } 258 259 void InitializeSubContainer(int32_t parentContainerId); 260 static void SetDialogCallback(int32_t instanceId, FrontendDialogCallback callback); 261 262 private: 263 void InitializeFrontend(); 264 void InitializeCallback(); 265 void InitializeTask(); 266 267 void AttachView(std::unique_ptr<Window> window, AceView* view, double density, int32_t width, int32_t height, 268 int32_t windowId, UIEnvCallback callback = nullptr); 269 void SetUIWindowInner(sptr<OHOS::Rosen::Window> uiWindow); 270 sptr<OHOS::Rosen::Window> GetUIWindowInner() const; 271 std::weak_ptr<OHOS::AppExecFwk::Ability> GetAbilityInner() const; 272 int32_t instanceId_ = 0; 273 AceView* aceView_ = nullptr; 274 RefPtr<TaskExecutor> taskExecutor_; 275 RefPtr<AssetManager> assetManager_; 276 RefPtr<PlatformResRegister> resRegister_; 277 RefPtr<PipelineContext> pipelineContext_; 278 RefPtr<Frontend> frontend_; 279 FrontendType type_ = FrontendType::JS; 280 bool isArkApp_ = false; 281 std::unique_ptr<PlatformEventCallback> platformEventCallback_; 282 WindowModal windowModal_ { WindowModal::NORMAL }; 283 ColorScheme colorScheme_ { ColorScheme::FIRST_VALUE }; 284 ResourceInfo resourceInfo_; 285 std::weak_ptr<OHOS::AppExecFwk::Ability> aceAbility_; 286 std::weak_ptr<OHOS::AbilityRuntime::Context> runtimeContext_; 287 std::weak_ptr<OHOS::AppExecFwk::AbilityInfo> abilityInfo_; 288 void* sharedRuntime_ = nullptr; 289 std::string pageProfile_; 290 int32_t pageId_ = 0; 291 bool useCurrentEventRunner_ = false; 292 sptr<OHOS::Rosen::Window> uiWindow_ = nullptr; 293 std::string windowName_; 294 295 bool isSubContainer_ = false; 296 int32_t parentId_ = 0; 297 bool useStageModel_ = false; 298 299 ACE_DISALLOW_COPY_AND_MOVE(AceContainer); 300 }; 301 302 } // namespace OHOS::Ace::Platform 303 304 #endif // FOUNDATION_ACE_ADAPTER_OHOS_CPP_ACE_CONTAINER_H 305