• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_ENGINE_ACE_DIFF_ENGINE_H
17 #define FOUNDATION_ACE_FRAMEWORK_JAVASCRIPT_ENGINE_ACE_DIFF_ENGINE_H
18 
19 #include <unordered_map>
20 
21 #include "core/components/xcomponent/native_interface_xcomponent_impl.h"
22 #include "frameworks/bridge/declarative_frontend/engine/quickjs/qjs_declarative_engine_instance.h"
23 #include "frameworks/bridge/js_frontend/engine/common/js_engine.h"
24 #include "frameworks/bridge/js_frontend/engine/quickjs/qjs_group_js_bridge.h"
25 #include "frameworks/bridge/js_frontend/js_ace_page.h"
26 
27 namespace OHOS::Ace::Framework {
28 
29 class QJSDeclarativeEngine : public JsEngine {
DECLARE_ACE_TYPE(QJSDeclarativeEngine,JsEngine)30     DECLARE_ACE_TYPE(QJSDeclarativeEngine, JsEngine)
31 public:
32     explicit QJSDeclarativeEngine(int32_t instanceId) : instanceId_(instanceId) {}
33     ~QJSDeclarativeEngine() override;
34 
35     bool Initialize(const RefPtr<FrontendDelegate>& delegate) override;
36 
37     void LoadJs(const std::string& url, const RefPtr<JsAcePage>& page, bool isMainPage) override;
38 
39     // Load the je file of the page in NG structure..
40     bool LoadPageSource(const std::string& url,
41         const std::function<void(const std::string&, int32_t)>& errorCallback = nullptr) override;
42 
43     // Load the app.js file of the FA model in NG structure..
44     bool LoadFaAppSource() override;
45 
46     // Update running page
47     void UpdateRunningPage(const RefPtr<JsAcePage>& page) override;
48 
49     // Update staging page
50     void UpdateStagingPage(const RefPtr<JsAcePage>& page) override;
51 
52     // Reset staging page
53     void ResetStagingPage() override;
54 
55     void SetJsMessageDispatcher(const RefPtr<JsMessageDispatcher>& dispatcher) override;
56 
57     // Fire AsyncEvent on JS
58     void FireAsyncEvent(const std::string& eventId, const std::string& param) override;
59 
60     // Fire SyncEvent on JS
61     void FireSyncEvent(const std::string& eventId, const std::string& param) override;
62 
63     void FireExternalEvent(const std::string& componentId, const uint32_t nodeId, const bool isDestroy) override;
64 
65     // Timer callback
66     void TimerCallback(const std::string& callbackId, const std::string& delay, bool isInterval) override;
67 
68     void TimerCallJs(const std::string& callbackId, bool isInterval);
69 
70     // destroy page instance
71     void DestroyPageInstance(int32_t pageId) override;
72 
73     void OnConfigurationUpdated(const std::string& data) override;
74 
75     bool OnStartContinuation() override;
76 
77     void OnCompleteContinuation(int32_t code) override;
78 
79     void OnRemoteTerminated() override;
80 
81     void OnSaveData(std::string& data) override;
82 
83     bool OnRestoreData(const std::string& data) override;
84 
85     void MediaQueryCallback(const std::string& callbackId, const std::string& args) override;
86 
87     void RequestAnimationCallback(const std::string& callbackId, uint64_t timeStamp) override;
88 
89     void JsCallback(const std::string& callbackId, const std::string& args) override;
90 
91     void CallAppFunc(const std::string& appFuncName, int argc, JSValueConst* argv);
92 
93     void CallAppFunc(const std::string& appFuncName, int argc, JSValueConst* argv, JSValue& ret);
94 
95     // destroy application instance according packageName
96     void DestroyApplication(const std::string& packageName) override;
97 
98     void UpdateApplicationState(const std::string& packageName, Frontend::State state) override;
99 
100     void OnWindowDisplayModeChanged(bool isShownInMultiWindow, const std::string& data) override;
101 
102     void RunGarbageCollection() override;
103 
104     RefPtr<GroupJsBridge> GetGroupJsBridge() override;
105 
GetFrontend()106     FrontendDelegate* GetFrontend() override
107     {
108         return AceType::RawPtr(engineInstance_->GetDelegate());
109     }
110 
GetEngineInstance()111     RefPtr<QJSDeclarativeEngineInstance> GetEngineInstance()
112     {
113         return engineInstance_;
114     }
115 
116 #if defined(PREVIEW)
117     void ReplaceJSContent(const std::string& url, const std::string componentName) override;
118 
119     RefPtr<Component> GetNewComponentWithJsCode(const std::string& jsCode, const std::string& viewID) override;
120 #endif
121 
122     RefPtr<NativeXComponentImpl> nativeXComponentImpl_;
123     OH_NativeXComponent* nativeXComponent_ = nullptr;
124 
125 private:
126     void RegisterWorker();
127     void RegisterInitWorkerFunc();
128     void RegisterAssetFunc();
129     void SetPostTask(NativeEngine* nativeEngine);
130 
131     RefPtr<QJSDeclarativeEngineInstance> engineInstance_;
132     int32_t instanceId_ = 0;
133 
134     ACE_DISALLOW_COPY_AND_MOVE(QJSDeclarativeEngine);
135 };
136 } // namespace OHOS::Ace::Framework
137 
138 #endif // FOUNDATION_ACE_FRAMEWORK_JAVASCRIPT_ENGINE_ACE_DIFF_ENGINE_H
139