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 #include "js_quick_fix_manager.h"
17
18 #include "hilog_wrapper.h"
19 #include "js_application_quick_fix_info.h"
20 #include "js_runtime_utils.h"
21 #include "napi_common_util.h"
22 #include "quick_fix_error_utils.h"
23 #include "quick_fix_manager_client.h"
24
25 namespace OHOS {
26 namespace AbilityRuntime {
27 namespace {
28 constexpr size_t ARGC_ZERO = 0;
29 constexpr size_t ARGC_ONE = 1;
30 constexpr size_t ARGC_TWO = 2;
31 const char *QUICK_FIX_MANAGER_NAME = "JsQuickFixMgr";
32 } // namespace
33
34 class JsQuickFixManager {
35 public:
36 JsQuickFixManager() = default;
37 ~JsQuickFixManager() = default;
38
Finalizer(NativeEngine * engine,void * data,void * hint)39 static void Finalizer(NativeEngine *engine, void *data, void *hint)
40 {
41 HILOG_DEBUG("function called.");
42 std::unique_ptr<JsQuickFixManager>(static_cast<JsQuickFixManager*>(data));
43 }
44
ApplyQuickFix(NativeEngine * engine,NativeCallbackInfo * info)45 static NativeValue *ApplyQuickFix(NativeEngine *engine, NativeCallbackInfo *info)
46 {
47 JsQuickFixManager *me = CheckParamsAndGetThis<JsQuickFixManager>(engine, info);
48 return (me != nullptr) ? me->OnApplyQuickFix(*engine, *info) : nullptr;
49 }
50
GetApplyedQuickFixInfo(NativeEngine * engine,NativeCallbackInfo * info)51 static NativeValue *GetApplyedQuickFixInfo(NativeEngine *engine, NativeCallbackInfo *info)
52 {
53 JsQuickFixManager *me = CheckParamsAndGetThis<JsQuickFixManager>(engine, info);
54 return (me != nullptr) ? me->OnGetApplyedQuickFixInfo(*engine, *info) : nullptr;
55 }
56
RevokeQuickFix(NativeEngine * engine,NativeCallbackInfo * info)57 static NativeValue *RevokeQuickFix(NativeEngine *engine, NativeCallbackInfo *info)
58 {
59 JsQuickFixManager *me = CheckParamsAndGetThis<JsQuickFixManager>(engine, info);
60 return (me != nullptr) ? me->OnRevokeQuickFix(*engine, *info) : nullptr;
61 }
62
Throw(NativeEngine & engine,int32_t errCode)63 static bool Throw(NativeEngine &engine, int32_t errCode)
64 {
65 auto externalErrCode = AAFwk::QuickFixErrorUtil::GetErrorCode(errCode);
66 auto errMsg = AAFwk::QuickFixErrorUtil::GetErrorMessage(errCode);
67 NativeValue *error = engine.CreateError(CreateJsValue(engine, externalErrCode), CreateJsValue(engine, errMsg));
68 return engine.Throw(error);
69 }
70
CreateJsErrorByErrorCode(NativeEngine & engine,int32_t errCode)71 static NativeValue *CreateJsErrorByErrorCode(NativeEngine &engine, int32_t errCode)
72 {
73 auto externalErrCode = AAFwk::QuickFixErrorUtil::GetErrorCode(errCode);
74 auto errMsg = AAFwk::QuickFixErrorUtil::GetErrorMessage(errCode);
75 return CreateJsError(engine, externalErrCode, errMsg);
76 }
77
78 private:
OnGetApplyedQuickFixInfo(NativeEngine & engine,NativeCallbackInfo & info)79 NativeValue *OnGetApplyedQuickFixInfo(NativeEngine &engine, NativeCallbackInfo &info)
80 {
81 HILOG_DEBUG("function called.");
82 if (info.argc != ARGC_ONE && info.argc != ARGC_TWO) {
83 HILOG_ERROR("The number of parameter is invalid.");
84 Throw(engine, AAFwk::ERR_QUICKFIX_PARAM_INVALID);
85 return engine.CreateUndefined();
86 }
87
88 std::string bundleName;
89 if (!OHOS::AppExecFwk::UnwrapStringFromJS2(reinterpret_cast<napi_env>(&engine),
90 reinterpret_cast<napi_value>(info.argv[0]), bundleName)) {
91 HILOG_ERROR("The bundleName is invalid.");
92 Throw(engine, AAFwk::ERR_QUICKFIX_PARAM_INVALID);
93 return engine.CreateUndefined();
94 }
95
96 auto complete = [bundleName](NativeEngine &engine, AsyncTask &task, int32_t status) {
97 AppExecFwk::ApplicationQuickFixInfo quickFixInfo;
98 auto errCode = DelayedSingleton<AAFwk::QuickFixManagerClient>::GetInstance()->GetApplyedQuickFixInfo(
99 bundleName, quickFixInfo);
100 if (errCode == 0) {
101 task.ResolveWithNoError(engine, CreateJsApplicationQuickFixInfo(engine, quickFixInfo));
102 } else {
103 task.Reject(engine, CreateJsErrorByErrorCode(engine, errCode));
104 }
105 };
106
107 NativeValue *lastParam = (info.argc == ARGC_ONE) ? nullptr : info.argv[1];
108 NativeValue *result = nullptr;
109 AsyncTask::Schedule("JsQuickFixManager::OnGetApplyedQuickFixInfo", engine,
110 CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
111 HILOG_DEBUG("function finished.");
112 return result;
113 }
114
OnApplyQuickFix(NativeEngine & engine,NativeCallbackInfo & info)115 NativeValue *OnApplyQuickFix(NativeEngine &engine, NativeCallbackInfo &info)
116 {
117 HILOG_DEBUG("function called.");
118 if (info.argc != ARGC_ONE && info.argc != ARGC_TWO) {
119 HILOG_ERROR("The number of parameter is invalid.");
120 Throw(engine, AAFwk::ERR_QUICKFIX_PARAM_INVALID);
121 return engine.CreateUndefined();
122 }
123
124 std::vector<std::string> hapQuickFixFiles;
125 if (!OHOS::AppExecFwk::UnwrapArrayStringFromJS(reinterpret_cast<napi_env>(&engine),
126 reinterpret_cast<napi_value>(info.argv[0]), hapQuickFixFiles)) {
127 HILOG_ERROR("Hap quick fix files is invalid.");
128 Throw(engine, AAFwk::ERR_QUICKFIX_PARAM_INVALID);
129 return engine.CreateUndefined();
130 }
131
132 auto complete = [hapQuickFixFiles](NativeEngine &engine, AsyncTask &task, int32_t status) {
133 auto errcode = DelayedSingleton<AAFwk::QuickFixManagerClient>::GetInstance()->ApplyQuickFix(
134 hapQuickFixFiles);
135 if (errcode == 0) {
136 task.ResolveWithNoError(engine, engine.CreateUndefined());
137 } else {
138 task.Reject(engine, CreateJsErrorByErrorCode(engine, errcode));
139 }
140 };
141
142 NativeValue *lastParam = (info.argc == 1) ? nullptr : info.argv[1];
143 NativeValue *result = nullptr;
144 AsyncTask::Schedule("JsQuickFixManager::OnApplyQuickFix", engine,
145 CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
146 HILOG_DEBUG("function finished.");
147 return result;
148 }
149
OnRevokeQuickFix(NativeEngine & engine,NativeCallbackInfo & info)150 NativeValue *OnRevokeQuickFix(NativeEngine &engine, NativeCallbackInfo &info)
151 {
152 HILOG_DEBUG("called.");
153 if (info.argc == ARGC_ZERO) {
154 HILOG_ERROR("The number of parameter is invalid.");
155 Throw(engine, AAFwk::ERR_QUICKFIX_PARAM_INVALID);
156 return engine.CreateUndefined();
157 }
158
159 std::string bundleName;
160 if (!ConvertFromJsValue(engine, info.argv[ARGC_ZERO], bundleName)) {
161 HILOG_ERROR("The bundleName is invalid.");
162 Throw(engine, AAFwk::ERR_QUICKFIX_PARAM_INVALID);
163 return engine.CreateUndefined();
164 }
165
166 std::shared_ptr<int32_t> errCode = std::make_shared<int32_t>(AAFwk::ERR_OK);
167 auto execute = [retval = errCode, bundleName] () {
168 auto quickFixMgr = DelayedSingleton<AAFwk::QuickFixManagerClient>::GetInstance();
169 if (quickFixMgr == nullptr) {
170 *retval = AAFwk::ERR_QUICKFIX_INTERNAL_ERROR;
171 HILOG_ERROR("Get quick fix mgr is nullptr.");
172 return;
173 }
174
175 *retval = quickFixMgr->RevokeQuickFix(bundleName);
176 HILOG_DEBUG("Revoke quick fix execute retval is {%{public}d}.", *retval);
177 };
178
179 auto complete = [retval = errCode](NativeEngine &engine, AsyncTask &task, int32_t status) {
180 HILOG_DEBUG("Revoke quick fix complete called.");
181 if (*retval != AAFwk::ERR_OK) {
182 HILOG_ERROR("Revoke quick fix execution failed. retval is %{public}d", *retval);
183 task.Reject(engine, CreateJsErrorByErrorCode(engine, *retval));
184 return;
185 }
186 HILOG_DEBUG("Revoke quick fix complete called ok.");
187 task.ResolveWithNoError(engine, engine.CreateUndefined());
188 };
189
190 NativeValue *lastParam = (info.argc == ARGC_ONE) ? nullptr : info.argv[ARGC_ONE];
191 NativeValue *result = nullptr;
192 AsyncTask::Schedule("JsQuickFixManager::OnRevokeQuickFix", engine,
193 CreateAsyncTaskWithLastParam(engine, lastParam, std::move(execute), std::move(complete), &result));
194 HILOG_DEBUG("Function finished.");
195 return result;
196 }
197 };
198
CreateJsQuickFixManager(NativeEngine * engine,NativeValue * exportObj)199 NativeValue *CreateJsQuickFixManager(NativeEngine *engine, NativeValue *exportObj)
200 {
201 HILOG_DEBUG("function called.");
202 if (engine == nullptr || exportObj == nullptr) {
203 HILOG_ERROR("Input parameter is invalid.");
204 return nullptr;
205 }
206
207 NativeObject *object = ConvertNativeValueTo<NativeObject>(exportObj);
208 if (object == nullptr) {
209 HILOG_ERROR("object is nullptr.");
210 return nullptr;
211 }
212
213 std::unique_ptr<JsQuickFixManager> quickFixManager = std::make_unique<JsQuickFixManager>();
214 object->SetNativePointer(quickFixManager.release(), JsQuickFixManager::Finalizer, nullptr);
215
216 BindNativeFunction(*engine, *object, "applyQuickFix", QUICK_FIX_MANAGER_NAME, JsQuickFixManager::ApplyQuickFix);
217 BindNativeFunction(*engine, *object, "getApplicationQuickFixInfo", QUICK_FIX_MANAGER_NAME,
218 JsQuickFixManager::GetApplyedQuickFixInfo);
219 BindNativeFunction(*engine, *object, "revokeQuickFix", QUICK_FIX_MANAGER_NAME, JsQuickFixManager::RevokeQuickFix);
220 return engine->CreateUndefined();
221 }
222 } // namespace AbilityRuntime
223 } // namespace OHOS
224