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