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/plugin_frontend/plugin_frontend_delegate.h" 29 #include "frameworks/bridge/js_frontend/engine/common/js_engine.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 void RunPage(int32_t pageId, 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 DumpFrontend() const override; 114 std::string GetPagePath() const override; 115 GetEventHandler()116 RefPtr<AceEventHandler> GetEventHandler() override 117 { 118 return handler_; 119 }; 120 GetType()121 FrontendType GetType() override 122 { 123 return type_; 124 } 125 126 // judge frontend is foreground frontend. IsForeground()127 bool IsForeground() override 128 { 129 return foregroundFrontend_; 130 } 131 132 RefPtr<AccessibilityManager> GetAccessibilityManager() const override; 133 WindowConfig& GetWindowConfig() override; 134 135 // navigator component call router 136 void NavigatePage(uint8_t type, const PageTarget& target, const std::string& params) override; 137 138 void OnWindowDisplayModeChanged(bool isShownInMultiWindow, const std::string& data); 139 SetJsEngine(const RefPtr<Framework::JsEngine> & jsEngine)140 void SetJsEngine(const RefPtr<Framework::JsEngine>& jsEngine) 141 { 142 jsEngine_ = jsEngine; 143 } 144 SetNeedDebugBreakPoint(bool value)145 void SetNeedDebugBreakPoint(bool value) 146 { 147 if (jsEngine_) { 148 jsEngine_->SetNeedDebugBreakPoint(value); 149 } 150 } 151 SetDebugVersion(bool value)152 void SetDebugVersion(bool value) 153 { 154 if (jsEngine_) { 155 jsEngine_->SetDebugVersion(value); 156 } 157 } 158 SetInstanceName(const std::string & name)159 void SetInstanceName(const std::string& name) 160 { 161 if (jsEngine_) { 162 jsEngine_->SetInstanceName(name); 163 } 164 } 165 MarkIsSubWindow(bool isSubWindow)166 void MarkIsSubWindow(bool isSubWindow) 167 { 168 isSubWindow_ = isSubWindow; 169 } 170 171 void RebuildAllPages() override; 172 SetDensity(double density)173 void SetDensity(double density) 174 { 175 density_ = density; 176 } 177 ResetPageLoadState()178 void ResetPageLoadState() 179 { 180 pageLoaded_ = false; 181 } 182 183 void UpdatePlugin(const std::string& content); 184 SetDeclarativeOnUpdateWithValueParamsCallback(onPluginUpdateWithValueParams && callback)185 void SetDeclarativeOnUpdateWithValueParamsCallback(onPluginUpdateWithValueParams&& callback) 186 { 187 if (delegate_) { 188 delegate_->SetDeclarativeOnUpdateWithValueParamsCallback(std::move(callback)); 189 } 190 } 191 private: 192 void InitializeFrontendDelegate(const RefPtr<TaskExecutor>& taskExecutor); 193 194 FrontendType type_ = FrontendType::DECLARATIVE_JS; 195 RefPtr<Framework::PluginFrontendDelegate> delegate_; 196 RefPtr<AceEventHandler> handler_; 197 RefPtr<Framework::JsEngine> jsEngine_; 198 RefPtr<AccessibilityManager> accessibilityManager_; 199 bool foregroundFrontend_ = false; 200 bool isSubWindow_ = false; 201 bool pageLoaded_ = false; 202 double density_ = 1.0; 203 }; 204 205 class PluginEventHandler : public AceEventHandler { 206 public: PluginEventHandler(const RefPtr<Framework::PluginFrontendDelegate> & delegate)207 explicit PluginEventHandler(const RefPtr<Framework::PluginFrontendDelegate>& delegate) 208 : delegate_(delegate) 209 { 210 ACE_DCHECK(delegate_); 211 } PluginEventHandler()212 PluginEventHandler() {} 213 214 ~PluginEventHandler() override = default; 215 216 void HandleAsyncEvent(const EventMarker& eventMarker) override; 217 218 void HandleAsyncEvent(const EventMarker& eventMarker, int32_t param) override; 219 220 void HandleAsyncEvent(const EventMarker& eventMarker, const BaseEventInfo& info) override; 221 222 void HandleAsyncEvent(const EventMarker& eventMarker, const std::shared_ptr<BaseEventInfo>& info) override; 223 224 void HandleAsyncEvent(const EventMarker& eventMarker, const KeyEvent& info) override; 225 226 void HandleAsyncEvent(const EventMarker& eventMarker, const std::string& param) override; 227 228 void HandleSyncEvent(const EventMarker& eventMarker, bool& result) override; 229 230 void HandleSyncEvent(const EventMarker& eventMarker, const BaseEventInfo& info, bool& result) override; 231 232 void HandleSyncEvent(const EventMarker& eventMarker, const KeyEvent& info, bool& result) override; 233 234 void HandleSyncEvent(const EventMarker& eventMarker, const std::string& param, std::string& result) override; 235 236 void HandleSyncEvent(const EventMarker& eventMarker, const std::shared_ptr<BaseEventInfo>& info) override; 237 238 void HandleSyncEvent(const EventMarker& eventMarker, const std::string& componentId, const int32_t nodeId, 239 const bool isDestroy) override; 240 241 private: 242 RefPtr<Framework::PluginFrontendDelegate> delegate_; 243 }; 244 } // namespace OHOS::Ace 245 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_PLUGIN_FRONTEND_PLUGIN_FRONTEND_H 246