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_JS_FRONTEND_ENGINE_COMMON_JS_ENGINE_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_ENGINE_COMMON_JS_ENGINE_H 18 19 #include <string> 20 #include <unordered_map> 21 22 #include "core/common/frontend.h" 23 #include "core/common/js_message_dispatcher.h" 24 #include "frameworks/bridge/js_frontend/frontend_delegate.h" 25 26 class NativeEngine; 27 class NativeReference; 28 29 namespace OHOS::Ace::Framework { 30 31 class JsAcePage; 32 using PixelMapNapiEntry = void* (*)(void*, void*); 33 struct JsModule { 34 const std::string moduleName; 35 const std::string methods; 36 }; 37 38 struct JsComponent { 39 const std::string componentName; 40 const std::string methods; 41 }; 42 43 class JsEngineInstance { 44 public: 45 JsEngineInstance() = default; 46 virtual ~JsEngineInstance() = default; 47 48 virtual void FlushCommandBuffer(void* context, const std::string& command); GetNativeEngine()49 NativeEngine* GetNativeEngine() 50 { 51 return nativeEngine_; 52 } SetNativeEngine(NativeEngine * nativeEngine)53 void SetNativeEngine(NativeEngine* nativeEngine) 54 { 55 nativeEngine_ = nativeEngine; 56 } 57 58 protected: 59 NativeEngine* nativeEngine_ = nullptr; 60 }; 61 62 class JsEngine : public AceType { 63 DECLARE_ACE_TYPE(JsEngine, AceType); 64 65 public: 66 JsEngine() = default; 67 ~JsEngine() override = default; 68 69 void RegisterSingleComponent(std::string& command, const std::string& componentName, const std::string& methods); 70 71 void RegisterSingleModule(std::string& command, const std::string& moduleName, const std::string& methods); 72 73 void RegisterModules(std::string& command); 74 75 void RegisterComponents(std::string& command); 76 77 // Initialize the JS engine. 78 virtual bool Initialize(const RefPtr<FrontendDelegate>& delegate) = 0; 79 80 // Destroy the JS engine resource. Destroy()81 virtual void Destroy() {} 82 83 // Load script in JS engine, and execute in corresponding context. 84 virtual void LoadJs(const std::string& url, const RefPtr<JsAcePage>& page, bool isMainPage) = 0; 85 // Load ets card script in JS engine, and execute in corresponding context. LoadCard(const std::string & url,int64_t cardId)86 virtual bool LoadCard(const std::string& url, int64_t cardId) 87 { 88 return false; 89 } 90 91 // Load the app.js file of the FA model in NG structure.. LoadFaAppSource()92 virtual bool LoadFaAppSource() 93 { 94 return false; 95 } 96 97 // Load the je file of the page in NG structure.. 98 virtual bool LoadPageSource(const std::string& /*url*/, 99 const std::function<void(const std::string&, int32_t)>& errorCallback = nullptr) 100 { 101 return false; 102 } 103 104 // Update running page 105 virtual void UpdateRunningPage(const RefPtr<JsAcePage>& page) = 0; 106 107 // Update staging page 108 virtual void UpdateStagingPage(const RefPtr<JsAcePage>& page) = 0; 109 110 // Reset loading page 111 virtual void ResetStagingPage() = 0; 112 113 virtual void SetJsMessageDispatcher(const RefPtr<JsMessageDispatcher>& dispatcher) = 0; 114 115 // Fire AsyncEvent on JS 116 virtual void FireAsyncEvent(const std::string& eventId, const std::string& param) = 0; 117 118 // Fire SyncEvent on JS 119 virtual void FireSyncEvent(const std::string& eventId, const std::string& param) = 0; 120 121 // Fire external event on JS thread 122 virtual void FireExternalEvent(const std::string& componentId, uint32_t nodeId, bool isDestroy = false) = 0; 123 124 // Timer callback on JS 125 virtual void TimerCallback(const std::string& callbackId, const std::string& delay, bool isInterval) = 0; 126 127 // Destroy page instance on JS 128 virtual void DestroyPageInstance(int32_t pageId) = 0; 129 130 // destroy application instance according packageName 131 virtual void DestroyApplication(const std::string& packageName) = 0; 132 133 // update application State according packageName UpdateApplicationState(const std::string & packageName,Frontend::State state)134 virtual void UpdateApplicationState(const std::string& packageName, Frontend::State state) {} 135 OnWindowDisplayModeChanged(bool isShownInMultiWindow,const std::string & data)136 virtual void OnWindowDisplayModeChanged(bool isShownInMultiWindow, const std::string& data) {} 137 OnNewWant(const std::string & data)138 virtual void OnNewWant(const std::string& data) {} 139 OnSaveAbilityState(std::string & data)140 virtual void OnSaveAbilityState(std::string& data) {} 141 OnRestoreAbilityState(const std::string & data)142 virtual void OnRestoreAbilityState(const std::string& data) {} 143 OnConfigurationUpdated(const std::string & data)144 virtual void OnConfigurationUpdated(const std::string& data) {} 145 OnActive()146 virtual void OnActive() {} 147 OnInactive()148 virtual void OnInactive() {} 149 OnMemoryLevel(const int32_t code)150 virtual void OnMemoryLevel(const int32_t code) {} 151 OnStartContinuation()152 virtual bool OnStartContinuation() 153 { 154 return false; 155 } 156 OnCompleteContinuation(int32_t code)157 virtual void OnCompleteContinuation(int32_t code) {} 158 OnRemoteTerminated()159 virtual void OnRemoteTerminated() {} 160 OnSaveData(std::string & data)161 virtual void OnSaveData(std::string& data) {} 162 OnRestoreData(const std::string & data)163 virtual bool OnRestoreData(const std::string& data) 164 { 165 return false; 166 } 167 MediaQueryCallback(const std::string & callbackId,const std::string & args)168 virtual void MediaQueryCallback(const std::string& callbackId, const std::string& args) 169 { 170 if (mediaUpdateCallback_) { 171 mediaUpdateCallback_(this); 172 } 173 } 174 175 virtual void RequestAnimationCallback(const std::string& callbackId, uint64_t timeStamp) = 0; 176 177 virtual void JsCallback(const std::string& callbackId, const std::string& args) = 0; 178 SetErrorEventHandler(std::function<void (const std::string &,const std::string &)> && errorCallback)179 virtual void SetErrorEventHandler( 180 std::function<void(const std::string&, const std::string&)>&& errorCallback) {} 181 182 virtual void RunGarbageCollection() = 0; 183 RunFullGarbageCollection()184 virtual void RunFullGarbageCollection() {} 185 DumpHeapSnapshot(bool isPrivate)186 virtual void DumpHeapSnapshot(bool isPrivate) {} 187 ClearCache()188 virtual void ClearCache() {} 189 GetStacktraceMessage()190 virtual std::string GetStacktraceMessage() 191 { 192 return ""; 193 } 194 NotifyAppStorage(const std::string & key,const std::string & value)195 virtual void NotifyAppStorage(const std::string& key, const std::string& value) {} 196 197 virtual RefPtr<GroupJsBridge> GetGroupJsBridge() = 0; 198 GetFrontend()199 virtual ACE_EXPORT FrontendDelegate* GetFrontend() 200 { 201 return nullptr; 202 } 203 SetLocalStorage(int32_t instanceId,NativeReference * storage)204 virtual void SetLocalStorage(int32_t instanceId, NativeReference* storage) {} 205 SetContext(int32_t instanceId,NativeReference * context)206 virtual void SetContext(int32_t instanceId, NativeReference* context) {} 207 IsDebugVersion()208 bool IsDebugVersion() const 209 { 210 return isDebugVersion_; 211 } 212 SetDebugVersion(bool value)213 void SetDebugVersion(bool value) 214 { 215 isDebugVersion_ = value; 216 } 217 NeedDebugBreakPoint()218 bool NeedDebugBreakPoint() const 219 { 220 return needDebugBreakPoint_; 221 } 222 SetNeedDebugBreakPoint(bool value)223 void SetNeedDebugBreakPoint(bool value) 224 { 225 needDebugBreakPoint_ = value; 226 } 227 GetInstanceName()228 const std::string& GetInstanceName() const 229 { 230 return instanceName_; 231 } 232 SetInstanceName(const std::string & name)233 void SetInstanceName(const std::string& name) 234 { 235 instanceName_ = name; 236 } 237 AddExtraNativeObject(const std::string & key,void * value)238 void AddExtraNativeObject(const std::string& key, void* value) 239 { 240 extraNativeObject_[key] = value; 241 } 242 GetExtraNativeObject()243 const std::unordered_map<std::string, void*>& GetExtraNativeObject() const 244 { 245 return extraNativeObject_; 246 } 247 SetForceUpdate(bool needUpdate)248 void SetForceUpdate(bool needUpdate) 249 { 250 needUpdate_ = needUpdate; 251 } 252 GetNativeEngine()253 NativeEngine* GetNativeEngine() 254 { 255 return nativeEngine_; 256 } 257 RegisterMediaUpdateCallback(std::function<void (JsEngine *)> cb)258 void ACE_EXPORT RegisterMediaUpdateCallback(std::function<void(JsEngine*)> cb) 259 { 260 mediaUpdateCallback_ = std::move(cb); 261 } 262 UnregisterMediaUpdateCallback()263 void ACE_EXPORT UnregisterMediaUpdateCallback() 264 { 265 mediaUpdateCallback_ = nullptr; 266 } 267 268 virtual void RunNativeEngineLoop(); 269 #if !defined(PREVIEW) 270 static PixelMapNapiEntry GetPixelMapNapiEntry(); 271 #endif 272 273 #if defined(PREVIEW) GetNewComponentWithJsCode(const std::string & jsCode,const std::string & viewID)274 virtual RefPtr<Component> GetNewComponentWithJsCode(const std::string& jsCode, const std::string& viewID) 275 { 276 return nullptr; 277 } 278 ExecuteJsForFastPreview(const std::string & jsCode,const std::string & viewID)279 virtual bool ExecuteJsForFastPreview(const std::string& jsCode, const std::string& viewID) 280 { 281 return true; 282 } 283 ReplaceJSContent(const std::string & url,const std::string componentName)284 virtual void ReplaceJSContent(const std::string& url, const std::string componentName) 285 { 286 LOGE("V8 does not support replaceJSContent"); 287 return; 288 } 289 InitializeModuleSearcher(const std::string & bundleName,const std::string & moduleName,const std::string assetPath,bool isBundle)290 virtual void InitializeModuleSearcher(const std::string& bundleName, const std::string& moduleName, 291 const std::string assetPath, bool isBundle) 292 { 293 LOGE("V8 and QuickJs does not support InitializeModuleSearcher"); 294 } 295 #endif 296 FlushReload()297 virtual void FlushReload() {} 298 299 protected: 300 NativeEngine* nativeEngine_ = nullptr; 301 std::function<void(JsEngine*)> mediaUpdateCallback_; 302 bool needUpdate_ = false; 303 304 private: 305 // weather the app has debugger.so. 306 bool isDebugVersion_ = false; 307 308 // if debug, '-D' means need debug breakpoint, by default, do not enter breakpoint. 309 bool needDebugBreakPoint_ = false; 310 311 std::string instanceName_; 312 313 std::unordered_map<std::string, void*> extraNativeObject_; 314 }; 315 316 } // namespace OHOS::Ace::Framework 317 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_ENGINE_COMMON_JS_ENGINE_H 318