1 /* 2 * Copyright (c) 2022-2024 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 OHOS_FILEMGMT_BACKUP_EXT_BACKUP_JS_H 17 #define OHOS_FILEMGMT_BACKUP_EXT_BACKUP_JS_H 18 19 #include <memory> 20 #include <string_view> 21 #include <tuple> 22 #include <vector> 23 24 #include "b_resources/b_constants.h" 25 #include "ext_backup.h" 26 #include "js_runtime.h" 27 #include "native_reference.h" 28 #include "native_value.h" 29 #include "unique_fd.h" 30 #include "want.h" 31 32 namespace OHOS::FileManagement::Backup { 33 using InputArgsParser = std::function<bool(napi_env, std::vector<napi_value> &)>; 34 using ResultValueParser = std::function<bool(napi_env, napi_value)>; 35 36 struct CallJsParam { 37 std::mutex backupOperateMutex; 38 std::condition_variable backupOperateCondition; 39 std::atomic<bool> isReady {false}; 40 std::string funcName; 41 AbilityRuntime::JsRuntime *jsRuntime; 42 NativeReference *jsObj; 43 InputArgsParser argParser; 44 ResultValueParser retParser; 45 CallJsParamCallJsParam46 CallJsParam(const std::string &funcNameIn, 47 AbilityRuntime::JsRuntime *jsRuntimeIn, 48 NativeReference *jsObjIn, 49 InputArgsParser &argParserIn, 50 ResultValueParser &retParserIn) 51 : funcName(funcNameIn), jsRuntime(jsRuntimeIn), jsObj(jsObjIn), argParser(argParserIn), retParser(retParserIn) 52 { 53 } 54 }; 55 56 struct CallbackInfo { 57 std::function<void(ErrCode, std::string)> callback; CallbackInfoCallbackInfo58 CallbackInfo(std::function<void(ErrCode, std::string)> callbackIn) : callback(callbackIn) {} 59 }; 60 61 struct CallbackInfoBackup { 62 std::function<void(ErrCode, const std::string)> callbackParam; CallbackInfoBackupCallbackInfoBackup63 CallbackInfoBackup(std::function<void(ErrCode, const std::string)> param) 64 : callbackParam(param) 65 { 66 } 67 }; 68 69 struct CallbackInfoEx { 70 std::function<void(ErrCode, const std::string)> callbackParam; CallbackInfoExCallbackInfoEx71 CallbackInfoEx(std::function<void(ErrCode, const std::string)> param) : callbackParam(param) 72 { 73 } 74 }; 75 76 struct OnProcessCallBackInfo { 77 std::function<void(ErrCode, const std::string)> onProcessCallback; OnProcessCallBackInfoOnProcessCallBackInfo78 OnProcessCallBackInfo(std::function<void(ErrCode, const std::string)> param) : onProcessCallback(param) 79 { 80 } 81 }; 82 83 class ExtBackupJs : public ExtBackup { 84 public: 85 /** 86 * @brief Init the extension. 87 * 88 * @param record the extension record. 89 * @param application the application info. 90 * @param handler the extension handler. 91 * @param token the remote token. 92 */ 93 void Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &record, 94 const std::shared_ptr<AppExecFwk::OHOSApplication> &application, 95 std::shared_ptr<AppExecFwk::AbilityHandler> &handler, 96 const sptr<IRemoteObject> &token) override; 97 98 public: 99 /** 100 * @brief Create ExtBackupJs. 101 * 102 * @param runtime The runtime. 103 * @return The ExtBackupJs instance. 104 */ 105 static ExtBackupJs *Create(const std::unique_ptr<AbilityRuntime::Runtime> &runtime); 106 107 ErrCode OnBackup(std::function<void(ErrCode, std::string)> callback, 108 std::function<void(ErrCode, const std::string)> callbackEx) override; 109 110 /** 111 * @brief Call the app's OnRestore. 112 * 113 * @param callbackEx The callbackEx. 114 * @param callback The callBack. 115 */ 116 ErrCode OnRestore(std::function<void(ErrCode, std::string)> callback, 117 std::function<void(ErrCode, const std::string)> callbackEx) override; 118 119 /** 120 * @brief get app backup detail 121 * 122 * @param callback The callBack. 123 */ 124 ErrCode GetBackupInfo(std::function<void(ErrCode, const std::string)> callback) override; 125 126 /** 127 * @brief Called Notification containing extended information 128 * 129 * @param result The result. 130 */ 131 ErrCode InvokeAppExtMethod(ErrCode, const std::string) override; 132 133 /** 134 * @brief get app onProcess info 135 * 136 * @param callback The callBack. 137 */ 138 ErrCode OnProcess(std::function<void(ErrCode, const std::string)> callback) override; 139 140 public: 141 explicit ExtBackupJs(AbilityRuntime::JsRuntime &jsRuntime); 142 ~ExtBackupJs(); 143 private: 144 int CallJsMethod(const std::string &funcName, 145 AbilityRuntime::JsRuntime &jsRuntime, 146 NativeReference *jsObj, 147 InputArgsParser argParser, 148 ResultValueParser retParser); 149 std::tuple<ErrCode, napi_value> CallObjectMethod(std::string_view name, const std::vector<napi_value> &argv = {}); 150 std::function<bool(napi_env env, std::vector<napi_value> &argv)> ParseRestoreExInfo(); 151 std::function<bool(napi_env env, std::vector<napi_value> &argv)> ParseRestoreInfo(); 152 153 std::function<bool(napi_env env, std::vector<napi_value> &argv)> ParseBackupExInfo(); 154 155 ErrCode CallJSRestoreEx(); 156 ErrCode CallJSRestore(); 157 ErrCode CallJsOnBackupEx(); 158 ErrCode CallJsOnBackup(); 159 160 void ExportJsContext(void); 161 162 AbilityRuntime::JsRuntime &jsRuntime_; 163 std::unique_ptr<NativeReference> jsObj_; 164 std::shared_ptr<CallbackInfoBackup> callbackInfoBackup_; 165 std::shared_ptr<CallbackInfoEx> callbackInfoEx_; 166 std::shared_ptr<CallbackInfo> callbackInfo_; 167 std::shared_ptr<OnProcessCallBackInfo> onProcessCallback_; 168 std::condition_variable callJsCon_; 169 std::mutex callJsMutex_; 170 std::atomic<bool> callExtDefaultFunc_ {false}; // extension default method, onBackup or onRestore 171 std::atomic<bool> callJsExMethodDone_ {false}; 172 }; 173 } // namespace OHOS::FileManagement::Backup 174 175 #endif // OHOS_FILEMGMT_BACKUP_EXT_BACKUP_JS_H 176