1 /*
2 * Copyright (c) 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 #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_ONE = 1;
29 constexpr size_t ARGC_TWO = 2;
30 const char *QUICK_FIX_MANAGER_NAME = "JsQuickFixMgr";
31 } // namespace
32
33 class JsQuickFixManager {
34 public:
35 JsQuickFixManager() = default;
36 ~JsQuickFixManager() = default;
37
Finalizer(NativeEngine * engine,void * data,void * hint)38 static void Finalizer(NativeEngine *engine, void *data, void *hint)
39 {
40 HILOG_DEBUG("function called.");
41 std::unique_ptr<JsQuickFixManager>(static_cast<JsQuickFixManager*>(data));
42 }
43
ApplyQuickFix(NativeEngine * engine,NativeCallbackInfo * info)44 static NativeValue *ApplyQuickFix(NativeEngine *engine, NativeCallbackInfo *info)
45 {
46 JsQuickFixManager *me = CheckParamsAndGetThis<JsQuickFixManager>(engine, info);
47 return (me != nullptr) ? me->OnApplyQuickFix(*engine, *info) : nullptr;
48 }
49
GetApplyedQuickFixInfo(NativeEngine * engine,NativeCallbackInfo * info)50 static NativeValue *GetApplyedQuickFixInfo(NativeEngine *engine, NativeCallbackInfo *info)
51 {
52 JsQuickFixManager *me = CheckParamsAndGetThis<JsQuickFixManager>(engine, info);
53 return (me != nullptr) ? me->OnGetApplyedQuickFixInfo(*engine, *info) : nullptr;
54 }
55
Throw(NativeEngine & engine,int32_t errCode)56 static bool Throw(NativeEngine &engine, int32_t errCode)
57 {
58 auto externalErrCode = AAFwk::QuickFixErrorUtil::GetErrorCode(errCode);
59 auto errMsg = AAFwk::QuickFixErrorUtil::GetErrorMessage(errCode);
60 NativeValue *error = engine.CreateError(CreateJsValue(engine, externalErrCode), CreateJsValue(engine, errMsg));
61 return engine.Throw(error);
62 }
63
CreateJsErrorByErrorCode(NativeEngine & engine,int32_t errCode)64 static NativeValue *CreateJsErrorByErrorCode(NativeEngine &engine, int32_t errCode)
65 {
66 auto externalErrCode = AAFwk::QuickFixErrorUtil::GetErrorCode(errCode);
67 auto errMsg = AAFwk::QuickFixErrorUtil::GetErrorMessage(errCode);
68 return CreateJsError(engine, externalErrCode, errMsg);
69 }
70
71 private:
OnGetApplyedQuickFixInfo(NativeEngine & engine,NativeCallbackInfo & info)72 NativeValue *OnGetApplyedQuickFixInfo(NativeEngine &engine, NativeCallbackInfo &info)
73 {
74 HILOG_DEBUG("function called.");
75 if (info.argc != ARGC_ONE && info.argc != ARGC_TWO) {
76 HILOG_ERROR("The number of parameter is invalid.");
77 Throw(engine, AAFwk::ERR_QUICKFIX_PARAM_INVALID);
78 return engine.CreateUndefined();
79 }
80
81 std::string bundleName;
82 if (!OHOS::AppExecFwk::UnwrapStringFromJS2(reinterpret_cast<napi_env>(&engine),
83 reinterpret_cast<napi_value>(info.argv[0]), bundleName)) {
84 HILOG_ERROR("The bundleName is invalid.");
85 Throw(engine, AAFwk::ERR_QUICKFIX_PARAM_INVALID);
86 return engine.CreateUndefined();
87 }
88
89 auto complete = [bundleName](NativeEngine &engine, AsyncTask &task, int32_t status) {
90 AppExecFwk::ApplicationQuickFixInfo quickFixInfo;
91 auto errCode = DelayedSingleton<AAFwk::QuickFixManagerClient>::GetInstance()->GetApplyedQuickFixInfo(
92 bundleName, quickFixInfo);
93 if (errCode == 0) {
94 task.ResolveWithNoError(engine, CreateJsApplicationQuickFixInfo(engine, quickFixInfo));
95 } else {
96 task.Reject(engine, CreateJsErrorByErrorCode(engine, errCode));
97 }
98 };
99
100 NativeValue *lastParam = (info.argc == ARGC_ONE) ? nullptr : info.argv[1];
101 NativeValue *result = nullptr;
102 AsyncTask::Schedule("JsQuickFixManager::OnGetApplyedQuickFixInfo", engine,
103 CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
104 HILOG_DEBUG("function finished.");
105 return result;
106 }
107
OnApplyQuickFix(NativeEngine & engine,NativeCallbackInfo & info)108 NativeValue *OnApplyQuickFix(NativeEngine &engine, NativeCallbackInfo &info)
109 {
110 HILOG_DEBUG("function called.");
111 if (info.argc != ARGC_ONE && info.argc != ARGC_TWO) {
112 HILOG_ERROR("The number of parameter is invalid.");
113 Throw(engine, AAFwk::ERR_QUICKFIX_PARAM_INVALID);
114 return engine.CreateUndefined();
115 }
116
117 std::vector<std::string> hapQuickFixFiles;
118 if (!OHOS::AppExecFwk::UnwrapArrayStringFromJS(reinterpret_cast<napi_env>(&engine),
119 reinterpret_cast<napi_value>(info.argv[0]), hapQuickFixFiles)) {
120 HILOG_ERROR("Hap quick fix files is invalid.");
121 Throw(engine, AAFwk::ERR_QUICKFIX_PARAM_INVALID);
122 return engine.CreateUndefined();
123 }
124
125 auto complete = [hapQuickFixFiles](NativeEngine &engine, AsyncTask &task, int32_t status) {
126 auto errcode = DelayedSingleton<AAFwk::QuickFixManagerClient>::GetInstance()->ApplyQuickFix(
127 hapQuickFixFiles);
128 if (errcode == 0) {
129 task.ResolveWithNoError(engine, engine.CreateUndefined());
130 } else {
131 task.Reject(engine, CreateJsErrorByErrorCode(engine, errcode));
132 }
133 };
134
135 NativeValue *lastParam = (info.argc == 1) ? nullptr : info.argv[1];
136 NativeValue *result = nullptr;
137 AsyncTask::Schedule("JsQuickFixManager::OnApplyQuickFix", engine,
138 CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
139 HILOG_DEBUG("function finished.");
140 return result;
141 }
142 };
143
CreateJsQuickFixManager(NativeEngine * engine,NativeValue * exportObj)144 NativeValue *CreateJsQuickFixManager(NativeEngine *engine, NativeValue *exportObj)
145 {
146 HILOG_DEBUG("function called.");
147 if (engine == nullptr || exportObj == nullptr) {
148 HILOG_ERROR("Input parameter is invalid.");
149 return nullptr;
150 }
151
152 NativeObject *object = ConvertNativeValueTo<NativeObject>(exportObj);
153 if (object == nullptr) {
154 HILOG_ERROR("object is nullptr.");
155 return nullptr;
156 }
157
158 std::unique_ptr<JsQuickFixManager> quickFixManager = std::make_unique<JsQuickFixManager>();
159 object->SetNativePointer(quickFixManager.release(), JsQuickFixManager::Finalizer, nullptr);
160
161 BindNativeFunction(*engine, *object, "applyQuickFix", QUICK_FIX_MANAGER_NAME, JsQuickFixManager::ApplyQuickFix);
162 BindNativeFunction(*engine, *object, "getApplicationQuickFixInfo", QUICK_FIX_MANAGER_NAME,
163 JsQuickFixManager::GetApplyedQuickFixInfo);
164 return engine->CreateUndefined();
165 }
166 } // namespace AbilityRuntime
167 } // namespace OHOS
168