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_BACKEND_DELEGATE_IMPL_H 17 #define FOUNDATION_ACE_ADAPTER_OHOS_ENTRANCE_PA_ENGINE_BACKEND_DELEGATE_IMPL_H 18 19 #include <future> 20 #include <mutex> 21 #include <unordered_map> 22 23 #include "abs_shared_result_set.h" 24 #include "data_ability_predicates.h" 25 #include "form_provider_info.h" 26 #include "iremote_object.h" 27 #include "napi_remote_object.h" 28 #include "pac_map.h" 29 #include "values_bucket.h" 30 #include "want.h" 31 32 #include "adapter/ohos/entrance/pa_engine/backend_delegate.h" 33 #include "base/memory/ace_type.h" 34 #include "base/thread/cancelable_callback.h" 35 #include "core/common/backend.h" 36 #include "core/common/js_message_dispatcher.h" 37 #include "core/pipeline/pipeline_context.h" 38 #include "frameworks/bridge/common/manifest/manifest_parser.h" 39 40 namespace OHOS::Ace { 41 42 using LoadJsCallback = std::function<void(const std::string& url, const OHOS::AAFwk::Want& want)>; 43 using JsMessageDispatcherSetterCallback = std::function<void(const RefPtr<JsMessageDispatcher>&)>; 44 using PaEventCallback = std::function<void(const std::string&, const std::string&)>; 45 using DestroyApplicationCallback = std::function<void(const std::string& packageName)>; 46 using InsertCallback = 47 std::function<int32_t(const Uri& uri, const OHOS::NativeRdb::ValuesBucket& value, const CallingInfo& callingInfo)>; 48 using CallCallback = std::function<std::shared_ptr<AppExecFwk::PacMap>(const std::string& method, 49 const std::string& arg, const AppExecFwk::PacMap& pacMap, const CallingInfo& callingInfo)>; 50 using QueryCallback = std::function<std::shared_ptr<OHOS::NativeRdb::AbsSharedResultSet>(const Uri& uri, 51 const std::vector<std::string>& columns, const OHOS::NativeRdb::DataAbilityPredicates& predicates, 52 const CallingInfo& callingInfo)>; 53 using UpdateCallback = std::function<int32_t(const Uri& uri, const OHOS::NativeRdb::ValuesBucket& value, 54 const OHOS::NativeRdb::DataAbilityPredicates& predicates, const CallingInfo& callingInfo)>; 55 using DeleteCallback = std::function<int32_t( 56 const Uri& uri, const OHOS::NativeRdb::DataAbilityPredicates& predicates, const CallingInfo& callingInfo)>; 57 using BatchInsertCallback = std::function<int32_t( 58 const Uri& uri, const std::vector<OHOS::NativeRdb::ValuesBucket>& values, const CallingInfo& callingInfo)>; 59 using GetTypeCallback = std::function<std::string(const Uri& uri, const CallingInfo& callingInfo)>; 60 using GetFileTypesCallback = std::function<std::vector<std::string>( 61 const Uri& uri, const std::string& mimeTypeFilter, const CallingInfo& callingInfo)>; 62 using OpenFileCallback = 63 std::function<int32_t(const Uri& uri, const std::string& mode, const CallingInfo& callingInfo)>; 64 using OpenRawFileCallback = 65 std::function<int32_t(const Uri& uri, const std::string& mode, const CallingInfo& callingInfo)>; 66 using NormalizeUriCallback = std::function<Uri(const Uri& uri, const CallingInfo& callingInfo)>; 67 using DenormalizeUriCallback = std::function<Uri(const Uri& uri, const CallingInfo& callingInfo)>; 68 using ConnectCallback = std::function<sptr<IRemoteObject>(const OHOS::AAFwk::Want& want)>; 69 using DisConnectCallback = std::function<void(const OHOS::AAFwk::Want& want)>; 70 using CreateFormCallback = std::function<void(const OHOS::AAFwk::Want& want)>; 71 using DeleteFormCallback = std::function<void(const int64_t formId)>; 72 using TriggerEventCallback = std::function<void(const int64_t formId, const std::string& message)>; 73 using UpdateFormCallback = std::function<void(const int64_t formId)>; 74 using CastTemptoNormalCallback = std::function<void(const int64_t formId)>; 75 using VisibilityChangedCallback = std::function<void(const std::map<int64_t, int32_t>& formEventsMap)>; 76 using AcquireStateCallback = std::function<int32_t(const OHOS::AAFwk::Want& want)>; 77 using CommandCallback = std::function<void(const OHOS::AAFwk::Want& want, int startId)>; 78 using CommandApplicationCallback = std::function<void(const std::string& intent, int startId)>; 79 using ShareFormCallback = std::function<bool(int64_t formId, OHOS::AAFwk::WantParams& wntParams)>; 80 using DumpHeapSnapshotCallback = std::function<void(bool isPrivate)>; 81 82 struct BackendDelegateImplBuilder { 83 RefPtr<TaskExecutor> taskExecutor; 84 LoadJsCallback loadCallback; 85 PaEventCallback asyncEventCallback; 86 PaEventCallback syncEventCallback; 87 JsMessageDispatcherSetterCallback transferCallback; 88 DestroyApplicationCallback destroyApplicationCallback; 89 InsertCallback insertCallback; 90 CallCallback callCallback; 91 QueryCallback queryCallback; 92 UpdateCallback updateCallback; 93 DeleteCallback deleteCallback; 94 BatchInsertCallback batchInsertCallback; 95 GetTypeCallback getTypeCallback; 96 GetFileTypesCallback getFileTypesCallback; 97 OpenFileCallback openFileCallback; 98 OpenRawFileCallback openRawFileCallback; 99 NormalizeUriCallback normalizeUriCallback; 100 DenormalizeUriCallback denormalizeUriCallback; 101 ConnectCallback connectCallback; 102 DisConnectCallback disConnectCallback; 103 CreateFormCallback createFormCallback; 104 DeleteFormCallback deleteFormCallback; 105 TriggerEventCallback triggerEventCallback; 106 UpdateFormCallback updateFormCallback; 107 CastTemptoNormalCallback castTemptoNormalCallback; 108 VisibilityChangedCallback visibilityChangedCallback; 109 AcquireStateCallback acquireStateCallback; 110 CommandCallback commandCallback; 111 CommandApplicationCallback commandApplicationCallback; 112 ShareFormCallback shareFormCallback; 113 DumpHeapSnapshotCallback dumpHeapSnapshotCallback; 114 BackendType type; 115 }; 116 117 class BackendDelegateImpl : public BackendDelegate { 118 DECLARE_ACE_TYPE(BackendDelegateImpl, BackendDelegate); 119 120 public: 121 explicit BackendDelegateImpl(const BackendDelegateImplBuilder& builder); 122 ~BackendDelegateImpl() override = default; 123 124 // JsBackend delegate functions. 125 void RunPa(const std::string& url, const OHOS::AAFwk::Want& want); 126 void SetJsMessageDispatcher(const RefPtr<JsMessageDispatcher>& dispatcher) const; 127 128 // BackendDelegate overrides. 129 void PostJsTask(std::function<void()>&& task) override; 130 131 void PostDelayedJsTask(std::function<void()>&& task, uint32_t delayTime) override; 132 133 SingleTaskExecutor GetAnimationJsTask() override; 134 135 void AddTaskObserver(std::function<void()>&& task) override; 136 void RemoveTaskObserver() override; 137 GetType()138 BackendType GetType() const override 139 { 140 return type_; 141 } 142 GetGroupJsBridge()143 const RefPtr<GroupJsBridge>& GetGroupJsBridge() override 144 { 145 return groupJsBridge_; 146 } 147 SetGroupJsBridge(const RefPtr<GroupJsBridge> & groupJsBridge)148 void SetGroupJsBridge(const RefPtr<GroupJsBridge>& groupJsBridge) 149 { 150 groupJsBridge_ = groupJsBridge; 151 } 152 153 void SetCallBackResult(const std::string& callBackId, const std::string& result) override; 154 155 bool GetAssetContent(const std::string& url, std::string& content) override; 156 bool GetAssetContent(const std::string& url, std::vector<uint8_t>& content) override; 157 bool GetResourceData(const std::string& fileUri, std::vector<uint8_t>& content, std::string& ami) override; 158 std::string GetAssetPath(const std::string& url) override; 159 160 // JsEventHandler delegate functions. 161 void FireAsyncEvent(const std::string& eventId, const std::string& param, const std::string& jsonArgs); 162 bool FireSyncEvent(const std::string& eventId, const std::string& param, const std::string& jsonArgs); 163 void FireSyncEvent( 164 const std::string& eventId, const std::string& param, const std::string& jsonArgs, std::string& result); 165 166 // special JsEventHandler 167 void OnApplicationDestroy(const std::string& packageName); 168 sptr<IRemoteObject> OnConnect(const OHOS::AAFwk::Want& want); 169 void OnDisConnect(const OHOS::AAFwk::Want& want); 170 void OnCommand(const OHOS::AAFwk::Want& want, int startId); 171 void OnApplicationCommand(const std::string& intent, int startId); 172 void DumpHeapSnapshot(bool isPrivate); 173 174 void TransferJsResponseData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const; 175 void TransferJsPluginGetError(int32_t callbackId, int32_t errorCode, std::string&& errorMessage) const; 176 void TransferJsEventData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const; 177 void LoadPluginJsCode(std::string&& jsCode) const; 178 void LoadPluginJsByteCode(std::vector<uint8_t>&& jsCode, std::vector<int32_t>&& jsCodeLen) const; 179 180 void MethodChannel(const std::string& methodName, std::string& jsonStr); 181 182 int32_t Insert(const Uri& uri, const OHOS::NativeRdb::ValuesBucket& value); 183 std::shared_ptr<AppExecFwk::PacMap> Call( 184 const Uri& uri, const std::string& method, const std::string& arg, const AppExecFwk::PacMap& pacMap); 185 std::shared_ptr<OHOS::NativeRdb::AbsSharedResultSet> Query(const Uri& uri, const std::vector<std::string>& columns, 186 const OHOS::NativeRdb::DataAbilityPredicates& predicates); 187 int32_t Update(const Uri& uri, const OHOS::NativeRdb::ValuesBucket& value, 188 const OHOS::NativeRdb::DataAbilityPredicates& predicates); 189 int32_t Delete(const Uri& uri, const OHOS::NativeRdb::DataAbilityPredicates& predicates); 190 191 int32_t BatchInsert(const Uri& uri, const std::vector<OHOS::NativeRdb::ValuesBucket>& values); 192 std::string GetType(const Uri& uri); 193 std::vector<std::string> GetFileTypes(const Uri& uri, const std::string& mimeTypeFilter); 194 int32_t OpenFile(const Uri& uri, const std::string& mode); 195 int32_t OpenRawFile(const Uri& uri, const std::string& mode); 196 Uri NormalizeUri(const Uri& uri); 197 Uri DenormalizeUri(const Uri& uri); 198 199 void OnCreate(const OHOS::AAFwk::Want& want); 200 void OnDelete(const int64_t formId); 201 void OnTriggerEvent(const int64_t formId, const std::string& message); 202 void OnUpdate(const int64_t formId); 203 void OnCastTemptoNormal(const int64_t formId); 204 void OnVisibilityChanged(const std::map<int64_t, int32_t>& formEventsMap); 205 int32_t OnAcquireFormState(const OHOS::AAFwk::Want& want); 206 bool OnShare(int64_t formId, OHOS::AAFwk::WantParams& wantParams); 207 208 private: 209 void LoadPa(const std::string& url, const OHOS::AAFwk::Want& want); 210 bool ParseFileUri( 211 const RefPtr<AssetManager>& assetManager, const std::string& fileUri, std::string& assetsFilePath); 212 213 void ParseManifest(); 214 int32_t callbackCnt_ = 0; 215 bool isStagingPageExist_ = false; 216 std::unordered_map<int32_t, std::string> jsCallBackResult_; 217 218 LoadJsCallback loadJs_; 219 JsMessageDispatcherSetterCallback dispatcherCallback_; 220 221 PaEventCallback asyncEvent_; 222 PaEventCallback syncEvent_; 223 224 InsertCallback insert_; 225 CallCallback call_; 226 QueryCallback query_; 227 UpdateCallback update_; 228 DeleteCallback delete_; 229 BatchInsertCallback batchInsert_; 230 GetTypeCallback getType_; 231 GetFileTypesCallback getFileTypes_; 232 OpenFileCallback openFile_; 233 OpenRawFileCallback openRawFile_; 234 NormalizeUriCallback normalizeUri_; 235 DenormalizeUriCallback denormalizeUri_; 236 237 DestroyApplicationCallback destroyApplication_; 238 CommandApplicationCallback commandApplication_; 239 ConnectCallback connectCallback_; 240 DisConnectCallback disConnectCallback_; 241 242 CreateFormCallback createCallback_; 243 DeleteFormCallback deleteCallback_; 244 TriggerEventCallback triggerEventCallback_; 245 UpdateFormCallback updateCallback_; 246 CastTemptoNormalCallback castTemptoNormalCallback_; 247 VisibilityChangedCallback visibilityChangedCallback_; 248 AcquireStateCallback acquireStateCallback_; 249 CommandCallback commandCallback_; 250 ShareFormCallback shareFormCallback_; 251 DumpHeapSnapshotCallback dumpHeapSnapshotCallback_; 252 253 RefPtr<Framework::ManifestParser> manifestParser_; 254 BackendType type_ = BackendType::SERVICE; 255 256 RefPtr<TaskExecutor> taskExecutor_; 257 RefPtr<GroupJsBridge> groupJsBridge_; 258 mutable std::mutex mutex_; 259 mutable std::once_flag onceFlag_; 260 261 std::mutex LoadPaMutex_; 262 std::condition_variable condition_; 263 }; 264 265 } // namespace OHOS::Ace 266 267 #endif // FOUNDATION_ACE_ADAPTER_OHOS_ENTRANCE_PA_ENGINE_BACKEND_DELEGATE_IMPL_H 268