1 /* 2 * Copyright (c) 2021 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_PA_BACKEND_H 17 #define FOUNDATION_ACE_ADAPTER_OHOS_ENTRANCE_PA_ENGINE_PA_BACKEND_H 18 19 #include <string> 20 #include <unordered_map> 21 22 #include "abs_shared_result_set.h" 23 #include "data_ability_predicates.h" 24 #include "form_provider_info.h" 25 #include "iremote_object.h" 26 #include "values_bucket.h" 27 #include "want.h" 28 29 #include "adapter/ohos/entrance/pa_engine/backend_delegate_impl.h" 30 #include "adapter/ohos/entrance/pa_engine/engine/common/js_backend_engine.h" 31 #include "base/memory/ace_type.h" 32 #include "base/utils/string_utils.h" 33 #include "core/common/backend.h" 34 35 namespace OHOS::Ace { 36 37 class PaBackend : public Backend { 38 DECLARE_ACE_TYPE(PaBackend, Backend); 39 40 public: 41 PaBackend() = default; 42 ~PaBackend() override; 43 44 bool Initialize(BackendType type, const RefPtr<TaskExecutor>& taskExecutor) override; 45 46 void LoadEngine(const char* libName, int32_t instanceId) override; 47 48 void UpdateState(Backend::State state) override; 49 50 void SetJsMessageDispatcher(const RefPtr<JsMessageDispatcher>& dispatcher) const override; 51 GetType()52 BackendType GetType() override 53 { 54 return type_; 55 } 56 57 void MethodChannel(const std::string& methodName, std::string& jsonStr) override; 58 59 void RunPa(const std::string& url) override; 60 61 void OnCommand(const std::string& intent, int startId) override; 62 63 void TransferJsResponseData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const override; 64 65 void TransferJsPluginGetError(int32_t callbackId, int32_t errorCode, std::string&& errorMessage) const override; 66 67 void TransferJsEventData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const override; 68 69 void LoadPluginJsCode(std::string&& jsCode) const override; 70 71 void LoadPluginJsByteCode(std::vector<uint8_t>&& jsCode, std::vector<int32_t>&& jsCodeLen) const override; 72 SetJsEngine(const RefPtr<JsBackendEngine> & jsBackEngine)73 void SetJsEngine(const RefPtr<JsBackendEngine>& jsBackEngine) 74 { 75 jsBackendEngine_ = jsBackEngine; 76 } 77 78 void SetAssetManager(const RefPtr<AssetManager>& assetManager) override; 79 80 void RunPa(const std::string& url, const OHOS::AAFwk::Want& want); 81 void OnCreate(const OHOS::AAFwk::Want& want); 82 void OnDelete(const int64_t formId); 83 void OnTriggerEvent(const int64_t formId, const std::string& message); 84 void OnUpdate(const int64_t formId); 85 void OnCastTemptoNormal(const int64_t formId); 86 void OnVisibilityChanged(const std::map<int64_t, int32_t>& formEventsMap); 87 int32_t OnAcquireFormState(const OHOS::AAFwk::Want& want); 88 sptr<IRemoteObject> OnConnect(const OHOS::AAFwk::Want& want); 89 void OnDisConnect(const OHOS::AAFwk::Want& want); 90 91 int32_t Insert(const Uri& uri, const OHOS::NativeRdb::ValuesBucket& value); 92 std::shared_ptr<OHOS::NativeRdb::AbsSharedResultSet> Query(const Uri& uri, const std::vector<std::string>& columns, 93 const OHOS::NativeRdb::DataAbilityPredicates& predicates); 94 int32_t Update(const Uri& uri, const OHOS::NativeRdb::ValuesBucket& value, 95 const OHOS::NativeRdb::DataAbilityPredicates& predicates); 96 int32_t Delete(const Uri& uri, const OHOS::NativeRdb::DataAbilityPredicates& predicates); 97 98 int32_t BatchInsert(const Uri& uri, const std::vector<OHOS::NativeRdb::ValuesBucket>& values); 99 std::string GetType(const Uri& uri); 100 std::vector<std::string> GetFileTypes(const Uri& uri, const std::string& mimeTypeFilter); 101 int32_t OpenFile(const Uri& uri, const std::string& mode); 102 int32_t OpenRawFile(const Uri& uri, const std::string& mode); 103 Uri NormalizeUri(const Uri& uri); 104 Uri DenormalizeUri(const Uri& uri); 105 std::shared_ptr<AppExecFwk::PacMap> Call(const Uri& uri, 106 const std::string& method, const std::string& arg, const AppExecFwk::PacMap& pacMap); 107 bool OnShare(int64_t formId, OHOS::AAFwk::WantParams &wantParams); 108 GetFormData()109 AppExecFwk::FormProviderData GetFormData() const 110 { 111 if (jsBackendEngine_) { 112 return jsBackendEngine_->GetFormData(); 113 } else { 114 LOGE("PA: PaBackend::jsBackendEngine_ is null"); 115 return AppExecFwk::FormProviderData(""); 116 } 117 } 118 119 void OnCommand(const OHOS::AAFwk::Want &want, int startId); 120 void DumpHeapSnapshot(bool isPrivate) override; 121 122 private: 123 void InitializeBackendDelegate(const RefPtr<TaskExecutor>& taskExecutor); 124 BackendType type_ = BackendType::SERVICE; 125 126 RefPtr<BackendDelegateImpl> delegate_; 127 RefPtr<JsBackendEngine> jsBackendEngine_; 128 }; 129 130 } // namespace OHOS::Ace 131 132 #endif // FOUNDATION_ACE_ADAPTER_OHOS_ENTRANCE_PA_ENGINE_PA_BACKEND_H 133