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