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_ADAPTER_OHOS_ENTRANCE_PA_ENGINE_ENGINE_JSI_JSI_PA_ENGINE_H 17 #define FOUNDATION_ACE_ADAPTER_OHOS_ENTRANCE_PA_ENGINE_ENGINE_JSI_JSI_PA_ENGINE_H 18 19 #include <vector> 20 21 #include "abs_shared_result_set.h" 22 #include "data_ability_predicates.h" 23 #include "event_handler.h" 24 #include "event_runner.h" 25 #include "napi/native_api.h" 26 #include "napi/native_node_api.h" 27 #include "native_engine/impl/ark/ark_native_engine.h" 28 #include "result_set.h" 29 #include "uri.h" 30 #include "values_bucket.h" 31 #include "want.h" 32 33 #include "adapter/ohos/entrance/pa_engine/engine/common/js_backend_engine.h" 34 #include "base/memory/ace_type.h" 35 #include "base/utils/noncopyable.h" 36 #include "core/common/js_message_dispatcher.h" 37 #include "frameworks/bridge/js_frontend/engine/jsi/js_runtime.h" 38 #include "js_backend_asset_manager.h" 39 #include "js_runtime.h" 40 41 namespace OHOS::Ace { 42 using namespace OHOS::Ace::Framework; 43 using RdbValueBucketNewInstance = napi_value (*)(napi_env env, OHOS::NativeRdb::ValuesBucket& valueBucket); 44 using RdbValueBucketGetNativeObject = OHOS::NativeRdb::ValuesBucket* (*)(napi_env env, napi_value& value); 45 using RdbResultSetProxyNewInstance = 46 napi_value (*)(napi_env env, std::shared_ptr<OHOS::NativeRdb::AbsSharedResultSet> resultSet); 47 using RdbResultSetProxyGetNativeObject = std::shared_ptr<OHOS::NativeRdb::AbsSharedResultSet> (*)(napi_env env, 48 napi_value arg); 49 using DataAbilityPredicatesNewInstance = napi_value (*)( 50 napi_env env, OHOS::NativeRdb::DataAbilityPredicates* predicates); 51 using DataAbilityPredicatesGetNativeObject = OHOS::NativeRdb::DataAbilityPredicates* (*)(const napi_env env, 52 const napi_value& arg); 53 54 class JsiPaEngine : public JsBackendEngine { 55 public: 56 using EventRunner = AppExecFwk::EventRunner; 57 using EventHandler = AppExecFwk::EventHandler; 58 JsiPaEngine(int32_t instanceId)59 explicit JsiPaEngine(int32_t instanceId) : instanceId_(instanceId) {} 60 ~JsiPaEngine() override; 61 62 bool Initialize(BackendType type, SrcLanguage language) override; 63 64 bool InitializeInner(BackendType type, SrcLanguage language) override; 65 66 void SetAssetManager(const RefPtr<AssetManager>& assetManager) override; 67 68 // Load and initialize a JS bundle into the JS Framework 69 void LoadJs(const std::string& url, const OHOS::AAFwk::Want& want) override; 70 71 // destroy application instance according packageName 72 void DestroyApplication(const std::string& packageName) override; 73 74 // data 75 int32_t Insert(const Uri& uri, const OHOS::NativeRdb::ValuesBucket& value, const CallingInfo& callingInfo) override; 76 std::shared_ptr<AppExecFwk::PacMap> Call(const std::string& method, const std::string& arg, 77 const AppExecFwk::PacMap& pacMap, const CallingInfo& callingInfo) override; 78 79 int32_t BatchInsert(const Uri& uri, const std::vector<OHOS::NativeRdb::ValuesBucket>& values, 80 const CallingInfo& callingInfo) override; 81 std::shared_ptr<OHOS::NativeRdb::AbsSharedResultSet> Query(const Uri& uri, const std::vector<std::string>& columns, 82 const OHOS::NativeRdb::DataAbilityPredicates& predicates, const CallingInfo& callingInfo) override; 83 int32_t Update(const Uri& uri, const OHOS::NativeRdb::ValuesBucket& value, 84 const OHOS::NativeRdb::DataAbilityPredicates& predicates, const CallingInfo& callingInfo) override; 85 int32_t Delete(const Uri& uri, const OHOS::NativeRdb::DataAbilityPredicates& predicates, 86 const CallingInfo& callingInfo) override; 87 88 std::string GetType(const Uri& uri, const CallingInfo& callingInfo) override; 89 std::vector<std::string> GetFileTypes( 90 const Uri& uri, const std::string& mimeTypeFilter, const CallingInfo& callingInfo) override; 91 int32_t OpenFile(const Uri& uri, const std::string& mode, const CallingInfo& callingInfo) override; 92 int32_t OpenRawFile(const Uri& uri, const std::string& mode, const CallingInfo& callingInfo) override; 93 Uri NormalizeUri(const Uri& uri, const CallingInfo& callingInfo) override; 94 Uri DenormalizeUri(const Uri& uri, const CallingInfo& callingInfo) override; 95 96 // service 97 sptr<IRemoteObject> OnConnectService(const OHOS::AAFwk::Want& want) override; 98 void OnDisconnectService(const OHOS::AAFwk::Want& want) override; 99 void OnCommand(const OHOS::AAFwk::Want& want, int startId) override; 100 101 // form 102 void OnCreate(const OHOS::AAFwk::Want& want) override; 103 void OnDelete(const int64_t formId) override; 104 void OnTriggerEvent(const int64_t formId, const std::string& message) override; 105 void OnUpdate(const int64_t formId) override; 106 void OnCastTemptoNormal(const int64_t formId) override; 107 void OnVisibilityChanged(const std::map<int64_t, int32_t>& formEventsMap) override; 108 int32_t OnAcquireFormState(const OHOS::AAFwk::Want& want) override; 109 bool OnShare(int64_t formId, OHOS::AAFwk::WantParams& wantParams) override; 110 111 void PostTask(const std::function<void()>& task, const std::string& name, int64_t delayTime = 0) override; 112 void PostSyncTask(const std::function<void()>& task, const std::string& name) override; 113 void RemoveTask(const std::string& name) override; 114 115 std::shared_ptr<JsRuntime> GetJsRuntime() const; 116 NativeEngine* GetNativeEngine() const; 117 GetBlockWaiting()118 bool GetBlockWaiting() const 119 { 120 return blockWaiting_; 121 } 122 SetBlockWaiting(bool blockWaiting)123 void SetBlockWaiting(bool blockWaiting) 124 { 125 blockWaiting_ = blockWaiting; 126 } 127 GetAsyncResult()128 shared_ptr<JsValue> GetAsyncResult() const 129 { 130 return asyncResult_; 131 } 132 SetAsyncResult(shared_ptr<JsValue> asyncResult)133 void SetAsyncResult(shared_ptr<JsValue> asyncResult) 134 { 135 asyncResult_ = asyncResult; 136 } 137 SetDebugMode(bool isDebugMode)138 void SetDebugMode(bool isDebugMode) 139 { 140 isDebugMode_ = isDebugMode; 141 } 142 GetDebugMode()143 bool GetDebugMode() 144 { 145 return isDebugMode_; 146 } 147 148 private: 149 void LoadLibrary(); 150 void UnloadLibrary(); 151 shared_ptr<JsValue> GetPaFunc(const std::string& funcName); 152 shared_ptr<JsValue> CallFunc(const shared_ptr<JsValue>& func); 153 shared_ptr<JsValue> CallFunc(const shared_ptr<JsValue>& func, const std::vector<shared_ptr<JsValue>>& argv); 154 shared_ptr<JsValue> CallFunc( 155 const shared_ptr<JsValue>& func, const std::vector<shared_ptr<JsValue>>& argv, const CallingInfo& callingInfo); 156 shared_ptr<JsValue> CallFuncWithDefaultThis( 157 const shared_ptr<JsValue>& func, const std::vector<shared_ptr<JsValue>>& argv); 158 159 shared_ptr<JsValue> CallAsyncFunc( 160 const shared_ptr<JsValue>& func, std::vector<shared_ptr<JsValue>>& argv, const CallingInfo& callingInfo); 161 shared_ptr<JsValue> NapiValueToJsValue(napi_value nativeValue); 162 shared_ptr<JsValue> WantToJsValue(const OHOS::AAFwk::Want& want); 163 void StartService(); 164 void StartData(); 165 void StartForm(const OHOS::AAFwk::Want& want); 166 167 bool InitJsEnv(bool debuggerMode, const std::unordered_map<std::string, void*>& extraNativeObject); 168 void RegisterPaModule(); 169 void RegisterConsoleModule(ArkNativeEngine* engine); 170 void RegisterUncaughtExceptionHandler(); 171 void EvaluateJsCode(); 172 void StartDebugMode(bool debuggerMode); 173 panda::ecmascript::EcmaVM* GetEcmaVm() const; 174 void InitJsRuntimeOptions(AbilityRuntime::Runtime::Options& options); 175 bool CreateJsRuntime(); 176 177 std::string ExcludeTag(const std::string& jsonString, const std::string& tagString); 178 std::string IncludeTag(const std::string& jsonString, const std::string& tagString); 179 std::shared_ptr<AppExecFwk::PacMap> BuildPacMap(const napi_env& env, const napi_value& arg); 180 void SetPacMapObject( 181 std::shared_ptr<AppExecFwk::PacMap> pacMap, const napi_env& env, std::string keyStr, napi_value value); 182 std::string UnwrapStringFromJS(napi_env env, napi_value param, const std::string& defaultValue = ""); 183 std::vector<std::string> ConvertStringVector(napi_env env, napi_value jsValue); 184 185 int32_t instanceId_ = 0; 186 std::unique_ptr<AbilityRuntime::JsRuntime> jsAbilityRuntime_ = nullptr; 187 std::shared_ptr<EventRunner> eventRunner_ = nullptr; 188 std::shared_ptr<EventHandler> eventHandler_ = nullptr; 189 std::shared_ptr<JsRuntime> runtime_ = nullptr; 190 RefPtr<JsBackendAssetManager> jsBackendAssetManager_ = nullptr; 191 BackendType type_ = BackendType::SERVICE; 192 SrcLanguage language_ = SrcLanguage::ETS; 193 bool blockWaiting_ = false; 194 shared_ptr<JsValue> asyncResult_ = nullptr; 195 bool isDebugMode_ = true; 196 197 void* libRdb_ = nullptr; 198 void* libDataAbility_ = nullptr; 199 RdbValueBucketNewInstance rdbValueBucketNewInstance_ = nullptr; 200 RdbValueBucketGetNativeObject rdbValueBucketGetNativeObject_ = nullptr; 201 RdbResultSetProxyNewInstance rdbResultSetProxyNewInstance_ = nullptr; 202 RdbResultSetProxyGetNativeObject rdbResultSetProxyGetNativeObject_ = nullptr; 203 DataAbilityPredicatesNewInstance dataAbilityPredicatesNewInstance_ = nullptr; 204 DataAbilityPredicatesGetNativeObject dataAbilityPredicatesGetNativeObject_ = nullptr; 205 206 ACE_DISALLOW_COPY_AND_MOVE(JsiPaEngine); 207 }; 208 209 } // namespace OHOS::Ace 210 211 #endif // FOUNDATION_ACE_ADAPTER_OHOS_ENTRANCE_PA_ENGINE_ENGINE_JSI_JSI_PA_ENGINE_H 212