1 /* 2 * Copyright (c) 2021-2022 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_FRONTEND_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_FRONTEND_H 18 19 #include <string> 20 #include <utility> 21 22 #include "base/memory/ace_type.h" 23 #include "base/utils/macros.h" 24 #include "base/utils/resource_configuration.h" 25 #include "bridge/common/utils/source_map.h" 26 #include "core/common/js_message_dispatcher.h" 27 #include "core/common/router_recover_record.h" 28 #include "core/event/ace_event_handler.h" 29 #include "interfaces/inner_api/ace/constants.h" 30 31 using FrontendDialogCallback = std::function<void(const std::string& event, const std::string& param)>; 32 33 typedef struct napi_value__* napi_value; 34 35 namespace OHOS::Ace { 36 37 class AcePage; 38 class PipelineBase; 39 class AssetManager; 40 class TaskExecutor; 41 class AccessibilityManager; 42 enum class ContentInfoType; 43 44 #ifndef WEARABLE_PRODUCT 45 constexpr int32_t DEFAULT_DESIGN_WIDTH = 720; 46 #else 47 constexpr int32_t DEFAULT_DESIGN_WIDTH = 454; 48 #endif 49 50 // Window config of frontend. 51 struct WindowConfig { 52 // NOT runtime real design width, this is config value set by user. 53 // Runtime design width should be considered together with autoDesignWidth. 54 int32_t designWidth = DEFAULT_DESIGN_WIDTH; 55 bool autoDesignWidth = false; 56 bool boxWrap = false; 57 double designWidthScale = 0.0; 58 GetDesignWidthScaleWindowConfig59 double GetDesignWidthScale(const double viewWidth) 60 { 61 if (NearEqual(designWidthScale, 0.0)) { 62 if (designWidth <= 0) { 63 LOGI("designWidth <= 0"); 64 designWidth = DEFAULT_DESIGN_WIDTH; 65 } 66 return viewWidth / designWidth; 67 } 68 return designWidthScale; 69 } 70 UpdateDesignWidthScaleWindowConfig71 void UpdateDesignWidthScale(const double viewWidth) 72 { 73 if (designWidth <= 0) { 74 LOGI("designWidth <= 0"); 75 designWidth = DEFAULT_DESIGN_WIDTH; 76 } 77 designWidthScale = viewWidth / designWidth; 78 } 79 }; 80 81 enum class FrontendType { 82 JSON, JS, JS_CARD, DECLARATIVE_JS, JS_PLUGIN, ETS_CARD, DECLARATIVE_CJ, ARK_TS, 83 DYNAMIC_HYBRID_STATIC, STATIC_HYBRID_DYNAMIC 84 }; 85 86 struct PageTarget; 87 88 class ACE_FORCE_EXPORT Frontend : public AceType { 89 DECLARE_ACE_TYPE(Frontend, AceType); 90 91 public: 92 Frontend() = default; 93 ~Frontend() override; 94 95 enum State : uint8_t { ON_CREATE = 0, ON_DESTROY, ON_SHOW, ON_HIDE, ON_ACTIVE, ON_INACTIVE, UNDEFINE }; 96 static std::string stateToString(int state); 97 98 static RefPtr<Frontend> Create(); 99 static RefPtr<Frontend> CreateDefault(); 100 101 virtual bool Initialize(FrontendType type, const RefPtr<TaskExecutor>& taskExecutor) = 0; 102 103 virtual void Destroy() = 0; 104 105 virtual void AttachPipelineContext(const RefPtr<PipelineBase>& context) = 0; 106 107 virtual void SetAssetManager(const RefPtr<AssetManager>& assetManager) = 0; 108 109 virtual void AddPage(const RefPtr<AcePage>& page) = 0; 110 111 virtual RefPtr<AcePage> GetPage(int32_t pageId) const = 0; 112 113 // Get the currently running JS page information in NG structure. GetCurrentPageUrl()114 virtual std::string GetCurrentPageUrl() const 115 { 116 return ""; 117 } 118 119 // Get the currently running JS page information in NG structure. GetCurrentPageSourceMap()120 virtual RefPtr<Framework::RevSourceMap> GetCurrentPageSourceMap() const 121 { 122 return nullptr; 123 } 124 125 // Get the currently running JS page information in NG structure. GetFaAppSourceMap()126 virtual RefPtr<Framework::RevSourceMap> GetFaAppSourceMap() const 127 { 128 return nullptr; 129 } 130 131 // Get the stage mode sourceMap. GetStageSourceMap(std::unordered_map<std::string,RefPtr<Framework::RevSourceMap>> & sourceMap)132 virtual void GetStageSourceMap(std::unordered_map<std::string, RefPtr<Framework::RevSourceMap>>& sourceMap) const {} 133 RunPage(const std::string & content,const std::string & params)134 virtual UIContentErrorCode RunPage(const std::string& content, const std::string& params) 135 { 136 return UIContentErrorCode::NO_ERRORS; 137 } RunPage(const std::shared_ptr<std::vector<uint8_t>> & content,const std::string & params)138 virtual UIContentErrorCode RunPage(const std::shared_ptr<std::vector<uint8_t>>& content, const std::string& params) 139 { 140 return UIContentErrorCode::NO_ERRORS; 141 } RunDynamicPage(const std::string & content,const std::string & params,const std::string & entryPoint)142 virtual UIContentErrorCode RunDynamicPage( 143 const std::string& content, const std::string& params, const std::string& entryPoint) 144 { 145 return UIContentErrorCode::NO_ERRORS; 146 } 147 RunPageByNamedRouter(const std::string & name,const std::string & params)148 virtual UIContentErrorCode RunPageByNamedRouter(const std::string& name, const std::string& params) 149 { 150 return UIContentErrorCode::NO_ERRORS; 151 } 152 RunIntentPage()153 virtual UIContentErrorCode RunIntentPage() 154 { 155 return UIContentErrorCode::NO_ERRORS; 156 } 157 SetRouterIntentInfo(const std::string & intentInfoSerialized,bool isColdStart,const std::function<void ()> && loadPageCallback)158 virtual UIContentErrorCode SetRouterIntentInfo(const std::string& intentInfoSerialized, bool isColdStart, 159 const std::function<void()>&& loadPageCallback) 160 { 161 return UIContentErrorCode::NO_ERRORS; 162 } 163 GetTopNavDestinationInfo(bool onlyFullScreen,bool needParam)164 virtual std::string GetTopNavDestinationInfo(bool onlyFullScreen, bool needParam) 165 { 166 return ""; 167 } 168 169 virtual void ReplacePage(const std::string& url, const std::string& params) = 0; 170 171 virtual void PushPage(const std::string& url, const std::string& params) = 0; 172 173 #if defined(ACE_STATIC) 174 // For ArkTS1.2 PushExtender(const std::string & url,const std::string & params,bool recoverable,std::function<void ()> && finishCallback)175 virtual void* PushExtender( 176 const std::string& url, const std::string& params, bool recoverable, std::function<void()>&& finishCallback) 177 { 178 return nullptr; 179 }; ReplaceExtender(const std::string & url,const std::string & params,bool recoverable,std::function<void ()> && enterFinishCallback,std::function<void ()> && exitFinishCallback)180 virtual void* ReplaceExtender(const std::string& url, const std::string& params, bool recoverable, 181 std::function<void()>&& enterFinishCallback, std::function<void()>&& exitFinishCallback) 182 { 183 return nullptr; 184 }; RunPageExtender(const std::string & url,const std::string & params,bool recoverable,std::function<void ()> && finishCallback)185 virtual void* RunPageExtender( 186 const std::string& url, const std::string& params, bool recoverable, std::function<void()>&& finishCallback) 187 { 188 return nullptr; 189 }; BackExtender(const std::string & url,const std::string & params)190 virtual void BackExtender(const std::string& url, const std::string& params) {}; ClearExtender()191 virtual void ClearExtender() {}; ShowAlertBeforeBackPageExtender(const std::string & url)192 virtual void ShowAlertBeforeBackPageExtender(const std::string& url) {}; HideAlertBeforeBackPageExtender()193 virtual void HideAlertBeforeBackPageExtender() {}; 194 #endif 195 196 // Gets front-end event handler to handle ace event. 197 virtual RefPtr<AceEventHandler> GetEventHandler() = 0; 198 199 // Get window config of front end, which is used to calculate the pixel ratio of the real device. 200 virtual WindowConfig& GetWindowConfig() = 0; 201 GetLock()202 std::unique_lock<std::recursive_mutex> GetLock() const 203 { 204 return std::unique_lock<std::recursive_mutex>(mutex_); 205 } 206 GetType()207 FrontendType GetType() const 208 { 209 return type_; 210 } 211 212 RefPtr<TaskExecutor> GetTaskExecutor() const; 213 214 // inform the frontend that onCreate or onDestroy 215 virtual void UpdateState(State) = 0; 216 217 // dump frontend info 218 virtual void DumpFrontend() const = 0; 219 220 virtual std::string GetPagePath() const = 0; 221 222 // send the message by js callback 223 virtual void SendCallbackMessage(const std::string& callbackId, const std::string& data) const = 0; 224 225 // set the message transfer to js instance 226 virtual void SetJsMessageDispatcher(const RefPtr<JsMessageDispatcher>& transfer) const = 0; 227 228 // transfer data back from platform side to component side 229 virtual void TransferComponentResponseData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const = 0; 230 231 // transfer data back from platform side to js side 232 virtual void TransferJsResponseData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const = 0; 233 234 // transfer error message get in plugin from platform side to js side 235 virtual void TransferJsPluginGetError(int32_t callbackId, int32_t errorCode, std::string&& errorMessage) const = 0; 236 237 // transfer event data from platform side to js side 238 virtual void TransferJsEventData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const = 0; 239 240 // get system plugin used in application GetPluginsUsed(std::string & data)241 virtual void GetPluginsUsed(std::string& data) {} 242 243 // get js code from plugin and load in js engine 244 virtual void LoadPluginJsCode(std::string&& jsCode) const = 0; 245 246 virtual void LoadPluginJsByteCode(std::vector<uint8_t>&& jsCode, std::vector<int32_t>&& jsCodeLen) const = 0; 247 248 // when this is foreground frontend 249 virtual bool IsForeground() = 0; 250 251 // get accessibility manager handler. 252 virtual RefPtr<AccessibilityManager> GetAccessibilityManager() const = 0; 253 254 // when back pressed 255 virtual bool OnBackPressed() = 0; 256 257 // when interface show up 258 virtual void OnShow() = 0; 259 260 // when interface hide 261 virtual void OnHide() = 0; 262 263 // when configuration update OnConfigurationUpdated(const std::string & data)264 virtual void OnConfigurationUpdated(const std::string& data) {} 265 266 virtual void OnSaveAbilityState(std::string& data) = 0; 267 268 virtual void OnMemoryLevel(int32_t level) = 0; 269 270 virtual void OnRestoreAbilityState(const std::string& data) = 0; 271 272 // when front on active 273 virtual void OnActive() = 0; 274 275 // when front on inactive 276 virtual void OnInactive() = 0; 277 278 // when front on asks a user whether to start the migration 279 virtual bool OnStartContinuation() = 0; 280 281 // when front on a local ability migration is complete 282 virtual void OnCompleteContinuation(int32_t code) = 0; 283 284 // interface to save the user data 285 virtual void OnSaveData(std::string& data) = 0; 286 287 // interface to restores the user data on the remote device 288 virtual bool OnRestoreData(const std::string& data) = 0; 289 290 virtual void OnRemoteTerminated() = 0; 291 292 // start the ability when it's running 293 virtual void OnNewRequest(const std::string& data) = 0; 294 295 virtual void OnNewWant(const std::string& data) = 0; 296 297 // call router back 298 virtual void CallRouterBack() = 0; 299 300 virtual void OnSurfaceChanged(int32_t width, int32_t height) = 0; 301 302 virtual void OnLayoutCompleted(const std::string& componentId) = 0; 303 virtual void OnDrawCompleted(const std::string& componentId) = 0; 304 virtual void OnDrawChildrenCompleted(const std::string& componentId) = 0; 305 virtual bool IsDrawChildrenCallbackFuncExist(const std::string& componentId) = 0; 306 TriggerGarbageCollection()307 virtual void TriggerGarbageCollection() {} 308 DumpHeapSnapshot(bool isPrivate)309 virtual void DumpHeapSnapshot(bool isPrivate) {} 310 DestroyHeapProfiler()311 virtual void DestroyHeapProfiler() {} 312 ForceFullGC()313 virtual void ForceFullGC() {} 314 NotifyUIIdle()315 virtual void NotifyUIIdle() {} 316 RebuildAllPages()317 virtual void RebuildAllPages() {} 318 SetColorMode(ColorMode colorMode)319 virtual void SetColorMode(ColorMode colorMode) {} 320 321 // navigator component call router NavigatePage(uint8_t type,const PageTarget & target,const std::string & params)322 virtual void NavigatePage(uint8_t type, const PageTarget& target, const std::string& params) {} 323 324 // restore RestoreRouterStack(const std::string & contentInfo,ContentInfoType type)325 virtual std::pair<RouterRecoverRecord, UIContentErrorCode> RestoreRouterStack( 326 const std::string& contentInfo, ContentInfoType type) 327 { 328 return std::make_pair(RouterRecoverRecord(), UIContentErrorCode::NO_ERRORS); 329 } 330 GetContentInfo(ContentInfoType type)331 virtual std::string GetContentInfo(ContentInfoType type) const 332 { 333 return ""; 334 } 335 336 // only declarative frontend need to support GetRouterSize()337 virtual int32_t GetRouterSize() const 338 { 339 return -1; 340 } 341 NotifyAppStorage(const std::string & key,const std::string & value)342 virtual void NotifyAppStorage(const std::string& key, const std::string& value) {} 343 GetContextValue()344 virtual napi_value GetContextValue() 345 { 346 napi_value value = nullptr; 347 return value; 348 } 349 BuilderNodeFunc(std::string functionName,const std::vector<int32_t> & nodeIds)350 virtual bool BuilderNodeFunc(std::string functionName, const std::vector<int32_t>& nodeIds) 351 { 352 return false; 353 } 354 GetFrameNodeValueByNodeId(int32_t nodeId)355 virtual napi_value GetFrameNodeValueByNodeId(int32_t nodeId) 356 { 357 return nullptr; 358 } 359 #ifdef PREVIEW RunNativeEngineLoop()360 virtual void RunNativeEngineLoop() {} 361 #endif 362 363 // Disallow pop last page DisallowPopLastPage()364 void DisallowPopLastPage() 365 { 366 disallowPopLastPage_ = true; 367 } 368 SetDialogCallback(FrontendDialogCallback callback)369 virtual void SetDialogCallback(FrontendDialogCallback callback) 370 { 371 dialogCallback_ = std::move(callback); 372 } 373 FlushReload()374 virtual void FlushReload() {} 375 // flush frontend for HotReload feature in NG HotReload()376 virtual void HotReload() {} 377 GetState()378 State GetState() const 379 { 380 return state_; 381 } 382 SetErrorEventHandler(std::function<void (const std::string &,const std::string &)> && errorCallback)383 virtual void SetErrorEventHandler(std::function<void(const std::string&, const std::string&)>&& errorCallback) {} 384 GetPagePathByUrl(const std::string & url)385 virtual std::string GetPagePathByUrl(const std::string& url) const 386 { 387 return ""; 388 } GetEnv()389 virtual void* GetEnv() { return nullptr; } 390 391 protected: 392 virtual bool MaybeRelease() override; 393 FrontendType type_ = FrontendType::JS; 394 RefPtr<TaskExecutor> taskExecutor_; 395 bool disallowPopLastPage_ = false; 396 FrontendDialogCallback dialogCallback_ = nullptr; 397 State state_ = State::UNDEFINE; 398 mutable std::recursive_mutex mutex_; 399 mutable std::mutex destructMutex_; 400 }; 401 402 } // namespace OHOS::Ace 403 404 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_FRONTEND_H 405