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_FRAMEWORK_JAVASCRIPT_BRIDGE_ENGINE_JSI_JSI_ENGINE_H
17 #define FOUNDATION_ACE_FRAMEWORK_JAVASCRIPT_BRIDGE_ENGINE_JSI_JSI_ENGINE_H
18 
19 #include <string>
20 
21 #include "native_engine/impl/ark/ark_native_engine.h"
22 
23 #include "core/common/js_message_dispatcher.h"
24 #include "frameworks/bridge/js_frontend/engine/common/js_engine.h"
25 #include "frameworks/bridge/js_frontend/engine/jsi/js_runtime.h"
26 #include "frameworks/bridge/js_frontend/engine/jsi/js_value.h"
27 #include "frameworks/bridge/js_frontend/engine/jsi/jsi_group_js_bridge.h"
28 #include "frameworks/bridge/js_frontend/frontend_delegate.h"
29 #include "frameworks/bridge/js_frontend/js_ace_page.h"
30 
31 namespace OHOS::Ace::Framework {
32 class JsiEngineInstance final : public AceType, public JsEngineInstance {
DECLARE_ACE_TYPE(JsiEngineInstance,AceType)33     DECLARE_ACE_TYPE(JsiEngineInstance, AceType)
34 public:
35     explicit JsiEngineInstance(const RefPtr<FrontendDelegate>& delegate, int32_t instanceId)
36         : frontendDelegate_(delegate), instanceId_(instanceId)
37     {}
38     ~JsiEngineInstance() override;
39     static std::map<const std::string, std::string> dataMap_;
40     RefPtr<JsAcePage> GetRunningPage() const;
41     void SetRunningPage(const RefPtr<JsAcePage>& page);
42     RefPtr<JsAcePage> GetStagingPage() const;
43     void SetStagingPage(const RefPtr<JsAcePage>& page);
44     void ResetStagingPage(const RefPtr<JsAcePage>& page);
45     void SetJsMessageDispatcher(const WeakPtr<JsMessageDispatcher>& dispatcher);
46     WeakPtr<JsMessageDispatcher> GetJsMessageDispatcher() const;
47     RefPtr<FrontendDelegate> GetFrontendDelegate() const;
48     bool FireJsEvent(const std::string& eventStr);
49     void CallJs(const std::string& callbackId, const std::string& args, bool keepAlive = false, bool isGlobal = false);
50     shared_ptr<JsRuntime> GetJsRuntime() const;
51     bool InitJsEnv(bool debugger_mode, const std::unordered_map<std::string, void*>& extraNativeObject);
52     // add Console object to worker
53     void RegisterConsoleModule(ArkNativeEngine* engine);
54 
55     void FlushCommandBuffer(void* context, const std::string& command) override;
56 
SetDebugMode(bool isDebugMode)57     void SetDebugMode(bool isDebugMode)
58     {
59         isDebugMode_ = isDebugMode;
60     }
61 
62     void SetDebuggerPostTask(std::string& library_path);
63 
GetDelegate()64     RefPtr<FrontendDelegate> GetDelegate() const
65     {
66         return frontendDelegate_;
67     }
68 #if defined(PREVIEW)
69     bool CallCurlFunction(const OHOS::Ace::RequestData& requestData, int32_t callbackId);
70 #endif
SetArkNativeEngine(ArkNativeEngine * nativeEngine)71     void SetArkNativeEngine(ArkNativeEngine* nativeEngine)
72     {
73         nativeEngine_ = nativeEngine;
74     }
75 
GetArkNativeEngine()76     ArkNativeEngine const *GetArkNativeEngine() const
77     {
78         return static_cast<ArkNativeEngine*>(nativeEngine_);
79     }
80 
81     void RegisterFaPlugin(); // load ReatureAbility plugin
82 
83 private:
84     void RegisterAceModule();             // add ace object to global
85     void RegisterConsoleModule();         // add Console object to global
86     void RegisterSyscapModule();          // add Syscap object to global
87     void RegisterDocumentModule();        // add dom object to global
88     void RegisterPerfUtilModule();        // add perfutil object to global
89     void RegisterHiViewModule();          // add hiView object to global
90     void RegisterI18nPluralRulesModule(); // add i18nPluralRules object to global
91     void InitGroupJsBridge();
92     bool IsDragEvent(const std::string& param);
93 
GetInstanceId()94     int32_t GetInstanceId() const
95     {
96         return instanceId_;
97     }
98 
99     shared_ptr<JsRuntime> runtime_;
100     RefPtr<JsAcePage> runningPage_;
101     RefPtr<JsAcePage> stagingPage_;
102     WeakPtr<JsMessageDispatcher> dispatcher_;
103     RefPtr<FrontendDelegate> frontendDelegate_;
104     int32_t instanceId_ = 0;
105     mutable std::mutex mutex_;
106     bool isDebugMode_ = true;
107 };
108 
109 class JsiEngine : public JsEngine {
110     DECLARE_ACE_TYPE(JsiEngine, JsEngine);
111 
112 public:
JsiEngine(int32_t instanceId)113     explicit JsiEngine(int32_t instanceId) : instanceId_(instanceId) {}
114     ~JsiEngine() override;
115 
116     bool Initialize(const RefPtr<FrontendDelegate>& delegate) override;
117 
118     void LoadJs(const std::string& url, const RefPtr<JsAcePage>& page, bool isMainPage) override;
119 
120     void UpdateRunningPage(const RefPtr<JsAcePage>& page) override;
121 
122     void UpdateStagingPage(const RefPtr<JsAcePage>& page) override;
123 
124     void ResetStagingPage() override;
125 
126     void SetJsMessageDispatcher(const RefPtr<JsMessageDispatcher>& dispatcher) override;
127 
128     void DestroyPageInstance(int32_t pageId) override;
129 
130     void DestroyApplication(const std::string& packageName) override;
131 
132     void FireAsyncEvent(const std::string& eventId, const std::string& param) override;
133 
134     void FireSyncEvent(const std::string& eventId, const std::string& param) override;
135 
136     void FireExternalEvent(const std::string& componentId, const uint32_t nodeId, const bool isDestroy) override;
137 
138     void TimerCallback(const std::string& callbackId, const std::string& delay, bool isInterval) override;
139 
140     void MediaQueryCallback(const std::string& callbackId, const std::string& args) override;
141 
142     void RequestAnimationCallback(const std::string& callbackId, uint64_t timeStamp) override;
143 
144     void JsCallback(const std::string& callbackId, const std::string& args) override;
145 
146     bool OnStartContinuation() override;
147 
148     void OnCompleteContinuation(int32_t code) override;
149 
150     void OnRemoteTerminated() override;
151 
152     void OnSaveData(std::string& data) override;
153 
154     bool OnRestoreData(const std::string& data) override;
155 
156     void RunGarbageCollection() override;
157 
158     void DumpHeapSnapshot(bool isPrivate) override;
159 
160     void DestroyHeapProfiler() override;
161 
162     void ForceFullGC() override;
163 
164     void NotifyUIIdle() override;
165 
166     std::string GetStacktraceMessage() override;
167 
168     RefPtr<GroupJsBridge> GetGroupJsBridge() override;
169 
GetFrontend()170     RefPtr<FrontendDelegate> GetFrontend() override
171     {
172         return engineInstance_->GetDelegate();
173     }
174 
175 private:
176     void SetPostTask(NativeEngine* nativeEngine);
177     void GetLoadOptions(std::string& optionStr, bool isMainPage, bool hasAppCode);
178     void RegisterWorker();
179     void RegisterInitWorkerFunc();
180     void RegisterOffWorkerFunc();
181     void RegisterAssetFunc();
182     bool ExecuteAbc(const std::string& fileName);
183     bool CallAppFunc(const std::string& appFuncName);
184     bool CallAppFunc(const std::string& appFuncName, std::vector<shared_ptr<JsValue>>& argv);
185 
186     int32_t instanceId_ = 0;
187     RefPtr<JsiEngineInstance> engineInstance_;
188 };
189 } // namespace OHOS::Ace::Framework
190 
191 #endif // FOUNDATION_ACE_FRAMEWORK_JAVASCRIPT_BRIDGE_ENGINE_JSI_JSI_ENGINE_H
192