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_FRAMEWORKS_CORE_COMMON_FRONTEND_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_FRONTEND_H 18 19 #include "base/memory/ace_type.h" 20 #include "base/utils/macros.h" 21 #include "core/accessibility/accessibility_manager.h" 22 #include "core/common/ace_page.h" 23 #include "core/common/js_message_dispatcher.h" 24 #include "core/event/ace_event_handler.h" 25 #include "core/pipeline/pipeline_context.h" 26 #include "frameworks/base/utils/resource_configuration.h" 27 28 using FrontendDialogCallback = std::function<void(const std::string& event, const std::string& param)>; 29 30 namespace OHOS::Ace { 31 32 #ifndef WEARABLE_PRODUCT 33 constexpr int32_t DEFAULT_DESIGN_WIDTH = 720; 34 #else 35 constexpr int32_t DEFAULT_DESIGN_WIDTH = 454; 36 #endif 37 38 // Window config of frontend. 39 struct WindowConfig { 40 // NOT runtime real design width, this is config value set by user. 41 // Runtime design width should be considered together with autoDesignWidth. 42 int32_t designWidth = DEFAULT_DESIGN_WIDTH; 43 bool autoDesignWidth = false; 44 bool boxWrap = false; 45 double designWidthScale = 0.0; 46 }; 47 48 enum class FrontendType { JSON, JS, JS_CARD, DECLARATIVE_JS, JS_PLUGIN }; 49 50 class ACE_EXPORT Frontend : public AceType { 51 DECLARE_ACE_TYPE(Frontend, AceType); 52 53 public: 54 Frontend() = default; 55 ~Frontend() override = default; 56 57 enum class State { ON_CREATE, ON_DESTROY, ON_SHOW, ON_HIDE }; 58 59 static RefPtr<Frontend> Create(); 60 static RefPtr<Frontend> CreateDefault(); 61 62 virtual bool Initialize(FrontendType type, const RefPtr<TaskExecutor>& taskExecutor) = 0; 63 64 virtual void Destroy() = 0; 65 66 virtual void AttachPipelineContext(const RefPtr<PipelineContext>& context) = 0; 67 68 virtual void SetAssetManager(const RefPtr<AssetManager>& assetManager) = 0; 69 70 virtual void AddPage(const RefPtr<AcePage>& page) = 0; 71 72 virtual RefPtr<AcePage> GetPage(int32_t pageId) const = 0; 73 74 virtual void RunPage(int32_t pageId, const std::string& content, const std::string& params) = 0; 75 76 virtual void ReplacePage(const std::string& url, const std::string& params) = 0; 77 78 virtual void PushPage(const std::string& url, const std::string& params) = 0; 79 80 // Gets front-end event handler to handle ace event. 81 virtual RefPtr<AceEventHandler> GetEventHandler() = 0; 82 83 // Get window config of front end, which is used to calculate the pixel ratio of the real device. 84 virtual WindowConfig& GetWindowConfig() = 0; 85 86 virtual FrontendType GetType() = 0; 87 88 // inform the frontend that onCreate or onDestroy 89 virtual void UpdateState(State) = 0; 90 91 // dump frontend info 92 virtual void DumpFrontend() const = 0; 93 94 // send the message by js callback 95 virtual void SendCallbackMessage(const std::string& callbackId, const std::string& data) const = 0; 96 97 // set the message transfer to js instance 98 virtual void SetJsMessageDispatcher(const RefPtr<JsMessageDispatcher>& transfer) const = 0; 99 100 // transfer data back from platform side to component side 101 virtual void TransferComponentResponseData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const = 0; 102 103 // transfer data back from platform side to js side 104 virtual void TransferJsResponseData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const = 0; 105 106 // transfer error message get in plugin from platform side to js side 107 virtual void TransferJsPluginGetError(int32_t callbackId, int32_t errorCode, std::string&& errorMessage) const = 0; 108 109 // transfer event data from platform side to js side 110 virtual void TransferJsEventData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const = 0; 111 112 // get system plugin used in application GetPluginsUsed(std::string & data)113 virtual void GetPluginsUsed(std::string& data) {}; 114 115 // get js code from plugin and load in js engine 116 virtual void LoadPluginJsCode(std::string&& jsCode) const = 0; 117 118 virtual void LoadPluginJsByteCode(std::vector<uint8_t>&& jsCode, std::vector<int32_t>&& jsCodeLen) const = 0; 119 120 // when this is foreground frontend 121 virtual bool IsForeground() = 0; 122 123 // get accessibility manager handler. 124 virtual RefPtr<AccessibilityManager> GetAccessibilityManager() const = 0; 125 126 // when back pressed 127 virtual bool OnBackPressed() = 0; 128 129 // when interface show up 130 virtual void OnShow() = 0; 131 132 // when interface hide 133 virtual void OnHide() = 0; 134 135 // when configuration update OnConfigurationUpdated(const std::string & data)136 virtual void OnConfigurationUpdated(const std::string& data) {}; 137 138 virtual void OnSaveAbilityState (std::string& data) = 0; 139 140 virtual void OnMemoryLevel(const int32_t level) = 0; 141 142 virtual void OnRestoreAbilityState (const std::string& data) = 0; 143 144 // when front on active 145 virtual void OnActive() = 0; 146 147 // when front on inactive 148 virtual void OnInactive() = 0; 149 150 // when front on asks a user whether to start the migration 151 virtual bool OnStartContinuation() = 0; 152 153 // when front on a local ability migration is complete 154 virtual void OnCompleteContinuation(int32_t code) = 0; 155 156 // interface to save the user data 157 virtual void OnSaveData(std::string& data) = 0; 158 159 // interface to restores the user data on the remote device 160 virtual bool OnRestoreData(const std::string& data) = 0; 161 162 virtual void OnRemoteTerminated() = 0; 163 164 // start the ability when it's running 165 virtual void OnNewRequest(const std::string& data) = 0; 166 167 virtual void OnNewWant(const std::string& data) = 0; 168 169 // call router back 170 virtual void CallRouterBack() = 0; 171 172 virtual void OnSurfaceChanged(int32_t width, int32_t height) = 0; 173 TriggerGarbageCollection()174 virtual void TriggerGarbageCollection() {}; 175 RebuildAllPages()176 virtual void RebuildAllPages() {}; 177 SetColorMode(ColorMode colorMode)178 virtual void SetColorMode(ColorMode colorMode) {}; 179 180 // navigator component call router NavigatePage(uint8_t type,const PageTarget & target,const std::string & params)181 virtual void NavigatePage(uint8_t type, const PageTarget& target, const std::string& params) {}; 182 183 // distribute RestoreRouterStack(const std::string & contentInfo)184 virtual std::string RestoreRouterStack(const std::string& contentInfo) 185 { 186 return ""; 187 } GetContentInfo()188 virtual std::string GetContentInfo() const 189 { 190 return ""; 191 } 192 NotifyAppStorage(const std::string & key,const std::string & value)193 virtual void NotifyAppStorage(const std::string& key, const std::string& value) {}; 194 #if defined(WINDOWS_PLATFORM) || defined(MAC_PLATFORM) RunNativeEngineLoop()195 virtual void RunNativeEngineLoop() {}; 196 #endif 197 198 // Disallow pop last page DisallowPopLastPage()199 void DisallowPopLastPage() 200 { 201 disallowPopLastPage_ = true; 202 } 203 SetDialogCallback(FrontendDialogCallback callback)204 virtual void SetDialogCallback(FrontendDialogCallback callback) 205 { 206 dialogCallback_ = callback; 207 } 208 209 protected: 210 bool disallowPopLastPage_ = false; 211 FrontendDialogCallback dialogCallback_ = nullptr; 212 }; 213 214 } // namespace OHOS::Ace 215 216 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_FRONTEND_H 217