• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 <string_view>
20 #include <tuple>
21 #include <vector>
22 
23 #include "b_resources/b_constants.h"
24 #include "ext_backup.h"
25 #include "js_runtime.h"
26 #include "native_reference.h"
27 #include "native_value.h"
28 #include "unique_fd.h"
29 #include "want.h"
30 
31 namespace OHOS::FileManagement::Backup {
32 using InputArgsParser = std::function<bool(NativeEngine &, std::vector<NativeValue *> &)>;
33 using ResultValueParser = std::function<bool(NativeEngine &, NativeValue *)>;
34 
35 struct CallJsParam {
36     std::mutex backupOperateMutex;
37     std::condition_variable backupOperateCondition;
38     bool isReady = false;
39     std::string funcName;
40     AbilityRuntime::JsRuntime *jsRuntime;
41     NativeReference *jsObj;
42     InputArgsParser argParser;
43     ResultValueParser retParser;
44 
CallJsParamCallJsParam45     CallJsParam(const std::string &funcNameIn,
46                 AbilityRuntime::JsRuntime *jsRuntimeIn,
47                 NativeReference *jsObjIn,
48                 InputArgsParser &argParserIn,
49                 ResultValueParser &retParserIn)
50         : funcName(funcNameIn), jsRuntime(jsRuntimeIn), jsObj(jsObjIn), argParser(argParserIn), retParser(retParserIn)
51     {
52     }
53 };
54 
55 class ExtBackupJs : public ExtBackup {
56 public:
57     /**
58      * @brief Init the extension.
59      *
60      * @param record the extension record.
61      * @param application the application info.
62      * @param handler the extension handler.
63      * @param token the remote token.
64      */
65     void Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &record,
66               const std::shared_ptr<AppExecFwk::OHOSApplication> &application,
67               std::shared_ptr<AppExecFwk::AbilityHandler> &handler,
68               const sptr<IRemoteObject> &token) override;
69 
70 public:
71     /**
72      * @brief Create ExtBackupJs.
73      *
74      * @param runtime The runtime.
75      * @return The ExtBackupJs instance.
76      */
77     static ExtBackupJs *Create(const std::unique_ptr<AbilityRuntime::Runtime> &runtime);
78 
79     /**
80      * @brief Call the app's OnBackup.
81      */
82     ErrCode OnBackup(void) override;
83 
84     /**
85      * @brief Call the app's OnRestore.
86      */
87     ErrCode OnRestore(void) override;
88 
89 public:
ExtBackupJs(AbilityRuntime::JsRuntime & jsRuntime)90     explicit ExtBackupJs(AbilityRuntime::JsRuntime &jsRuntime) : jsRuntime_(jsRuntime) {}
~ExtBackupJs()91     ~ExtBackupJs()
92     {
93         jsRuntime_.FreeNativeReference(std::move(jsObj_));
94     }
95 
96 private:
97     int CallJsMethod(const std::string &funcName,
98                      AbilityRuntime::JsRuntime &jsRuntime,
99                      NativeReference *jsObj,
100                      InputArgsParser argParser,
101                      ResultValueParser retParser);
102     std::tuple<ErrCode, NativeValue *> CallObjectMethod(std::string_view name,
103                                                         const std::vector<NativeValue *> &argv = {});
104 
105     AbilityRuntime::JsRuntime &jsRuntime_;
106     std::unique_ptr<NativeReference> jsObj_;
107 };
108 } // namespace OHOS::FileManagement::Backup
109 
110 #endif // OHOS_FILEMGMT_BACKUP_EXT_BACKUP_JS_H