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_BRIDGE_PLUGIN_FRONTEND_PLUGIN_FRONTEND_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_PLUGIN_FRONTEND_PLUGIN_FRONTEND_H 18 19 #include <string> 20 #include <unordered_map> 21 22 #include "base/memory/ace_type.h" 23 #include "base/utils/string_utils.h" 24 #include "core/common/ace_page.h" 25 #include "core/common/container.h" 26 #include "core/common/frontend.h" 27 #include "core/common/js_message_dispatcher.h" 28 #include "frameworks/bridge/js_frontend/engine/common/js_engine.h" 29 #include "frameworks/bridge/plugin_frontend/plugin_frontend_delegate.h" 30 31 namespace OHOS::Ace { 32 // PluginFrontend is the unique entrance from ACE backend to frontend. 33 // The relationship between AceActivity, AceContainer and PluginFrontend is 1:1:1. 34 // So PluginFrontend would be responsible for below things: 35 // - Create and initialize QuickJS engine. 36 // - Load pages of a JS app, and parse the manifest.json before loading main page. 37 // - Maintain the page stack of JS app by PluginFrontendDelegate. 38 // - Lifecycle of JS app (also AceActivity). 39 class PluginFrontend : public Frontend { 40 DECLARE_ACE_TYPE(PluginFrontend, Frontend); 41 42 public: 43 using onPluginUpdateWithValueParams = std::function<void(const std::string&)>; 44 45 PluginFrontend() = default; 46 ~PluginFrontend() override; 47 48 bool Initialize(FrontendType type, const RefPtr<TaskExecutor>& taskExecutor) override; 49 50 void Destroy() override; 51 52 void AttachPipelineContext(const RefPtr<PipelineBase>& context) override; 53 54 void SetAssetManager(const RefPtr<AssetManager>& assetManager) override; 55 56 UIContentErrorCode RunPage(const std::string& url, const std::string& params) override; 57 58 void ReplacePage(const std::string& url, const std::string& params) override; 59 60 void PushPage(const std::string& url, const std::string& params) override; 61 62 // Js frontend manages all pages self. AddPage(const RefPtr<AcePage> & page)63 void AddPage(const RefPtr<AcePage>& page) override {} 64 GetPage(int32_t pageId)65 RefPtr<AcePage> GetPage(int32_t pageId) const override 66 { 67 return nullptr; 68 } 69 70 void TriggerGarbageCollection() override; 71 72 void SendCallbackMessage(const std::string& callbackId, const std::string& data) const override; 73 // platform channel. 74 void SetJsMessageDispatcher(const RefPtr<JsMessageDispatcher>& dispatcher) const override; 75 void TransferComponentResponseData(int32_t callbackId, int32_t code, 76 std::vector<uint8_t>&& data) const override; 77 void TransferJsResponseData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const override; 78 #if defined(PREVIEW) 79 void TransferJsResponseDataPreview(int32_t callbackId, int32_t code, ResponseData responseData) const; 80 #endif 81 void TransferJsPluginGetError(int32_t callbackId, int32_t errorCode, std::string&& errorMessage) const override; 82 void TransferJsEventData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const override; 83 void LoadPluginJsCode(std::string&& jsCode) const override; 84 void LoadPluginJsByteCode(std::vector<uint8_t>&& jsCode, std::vector<int32_t>&& jsCodeLen) const override; 85 86 // application lifecycle. 87 void UpdateState(Frontend::State state) override; 88 89 // page lifecycle. 90 bool OnBackPressed() override; 91 void OnShow() override; 92 void OnHide() override; 93 void OnConfigurationUpdated(const std::string& data) override; 94 void OnSaveAbilityState(std::string& data) override; 95 void OnRestoreAbilityState(const std::string& data) override; 96 void OnNewWant(const std::string& data) override; 97 void OnActive() override; 98 void OnInactive() override; 99 bool OnStartContinuation() override; 100 void OnCompleteContinuation(int32_t code) override; 101 void OnSaveData(std::string& data) override; 102 void GetPluginsUsed(std::string& data) override; 103 bool OnRestoreData(const std::string& data) override; 104 void OnRemoteTerminated() override; 105 void OnNewRequest(const std::string& data) override; 106 void OnMemoryLevel(const int32_t level) override; 107 void SetColorMode(ColorMode colorMode) override; 108 void CallRouterBack() override; 109 void NotifyAppStorage(const std::string& key, const std::string& value) override; 110 111 void OnSurfaceChanged(int32_t width, int32_t height) override; 112 113 void OnLayoutCompleted(const std::string& componentId) override; 114 void OnDrawCompleted(const std::string& componentId) override; 115 116 void DumpFrontend() const override; 117 std::string GetPagePath() const override; 118 GetEventHandler()119 RefPtr<AceEventHandler> GetEventHandler() override 120 { 121 return handler_; 122 } 123 124 // judge frontend is foreground frontend. IsForeground()125 bool IsForeground() override 126 { 127 return foregroundFrontend_; 128 } 129 130 RefPtr<AccessibilityManager> GetAccessibilityManager() const override; 131 WindowConfig& GetWindowConfig() override; 132 133 // navigator component call router 134 void NavigatePage(uint8_t type, const PageTarget& target, const std::string& params) override; 135 136 void OnWindowDisplayModeChanged(bool isShownInMultiWindow, const std::string& data); 137 SetJsEngine(const RefPtr<Framework::JsEngine> & jsEngine)138 void SetJsEngine(const RefPtr<Framework::JsEngine>& jsEngine) 139 { 140 jsEngine_ = jsEngine; 141 } 142 SetNeedDebugBreakPoint(bool value)143 void SetNeedDebugBreakPoint(bool value) 144 { 145 if (jsEngine_) { 146 jsEngine_->SetNeedDebugBreakPoint(value); 147 } 148 } 149 SetDebugVersion(bool value)150 void SetDebugVersion(bool value) 151 { 152 if (jsEngine_) { 153 jsEngine_->SetDebugVersion(value); 154 } 155 } 156 SetInstanceName(const std::string & name)157 void SetInstanceName(const std::string& name) 158 { 159 if (jsEngine_) { 160 jsEngine_->SetInstanceName(name); 161 } 162 } 163 SetPluginBundleName(const std::string & pluginBundleName)164 void SetPluginBundleName(const std::string& pluginBundleName) 165 { 166 if (jsEngine_) { 167 jsEngine_->SetPluginBundleName(pluginBundleName); 168 } 169 } 170 SetPluginModuleName(const std::string & pluginModuleName)171 void SetPluginModuleName(const std::string& pluginModuleName) 172 { 173 if (jsEngine_) { 174 jsEngine_->SetPluginModuleName(pluginModuleName); 175 } 176 } 177 MarkIsSubWindow(bool isSubWindow)178 void MarkIsSubWindow(bool isSubWindow) 179 { 180 isSubWindow_ = isSubWindow; 181 } 182 183 void RebuildAllPages() override; 184 SetDensity(double density)185 void SetDensity(double density) 186 { 187 density_ = density; 188 } 189 ResetPageLoadState()190 void ResetPageLoadState() 191 { 192 pageLoaded_ = false; 193 } 194 195 void UpdatePlugin(const std::string& content); 196 SetDeclarativeOnUpdateWithValueParamsCallback(onPluginUpdateWithValueParams && callback)197 void SetDeclarativeOnUpdateWithValueParamsCallback(onPluginUpdateWithValueParams&& callback) 198 { 199 if (delegate_) { 200 delegate_->SetDeclarativeOnUpdateWithValueParamsCallback(std::move(callback)); 201 } 202 } 203 FireDeclarativeOnUpdateWithValueParamsCallback(const std::string & params)204 void FireDeclarativeOnUpdateWithValueParamsCallback(const std::string& params) const 205 { 206 if (delegate_) { 207 delegate_->FireDeclarativeOnUpdateWithValueParamsCallback(params); 208 } 209 } 210 211 private: 212 void InitializeFrontendDelegate(const RefPtr<TaskExecutor>& taskExecutor); 213 214 RefPtr<Framework::PluginFrontendDelegate> delegate_; 215 RefPtr<AceEventHandler> handler_; 216 RefPtr<Framework::JsEngine> jsEngine_; 217 RefPtr<AccessibilityManager> accessibilityManager_; 218 bool foregroundFrontend_ = false; 219 bool isSubWindow_ = false; 220 bool pageLoaded_ = false; 221 double density_ = 1.0; 222 }; 223 224 class PluginEventHandler : public AceEventHandler { 225 public: PluginEventHandler(const RefPtr<Framework::PluginFrontendDelegate> & delegate)226 explicit PluginEventHandler(const RefPtr<Framework::PluginFrontendDelegate>& delegate) : delegate_(delegate) 227 { 228 ACE_DCHECK(delegate_); 229 } PluginEventHandler()230 PluginEventHandler() {} 231 232 ~PluginEventHandler() override = default; 233 234 void HandleAsyncEvent(const EventMarker& eventMarker) override; 235 236 void HandleAsyncEvent(const EventMarker& eventMarker, int32_t param) override; 237 238 void HandleAsyncEvent(const EventMarker& eventMarker, const BaseEventInfo& info) override; 239 240 void HandleAsyncEvent(const EventMarker& eventMarker, const std::shared_ptr<BaseEventInfo>& info) override; 241 242 void HandleAsyncEvent(const EventMarker& eventMarker, const KeyEvent& info) override; 243 244 void HandleAsyncEvent(const EventMarker& eventMarker, const std::string& param) override; 245 246 void HandleSyncEvent(const EventMarker& eventMarker, bool& result) override; 247 248 void HandleSyncEvent(const EventMarker& eventMarker, const BaseEventInfo& info, bool& result) override; 249 250 void HandleSyncEvent(const EventMarker& eventMarker, const KeyEvent& info, bool& result) override; 251 252 void HandleSyncEvent(const EventMarker& eventMarker, const std::string& param, std::string& result) override; 253 254 void HandleSyncEvent(const EventMarker& eventMarker, const std::shared_ptr<BaseEventInfo>& info) override; 255 256 void HandleSyncEvent(const EventMarker& eventMarker, const std::string& componentId, const int32_t nodeId, 257 const bool isDestroy) override; 258 259 private: 260 RefPtr<Framework::PluginFrontendDelegate> delegate_; 261 }; 262 } // namespace OHOS::Ace 263 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_PLUGIN_FRONTEND_PLUGIN_FRONTEND_H 264