• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "napi/native_api.h"
17 #include "napi/native_node_api.h"
18 #include "update_client.h"
19 
20 namespace updateClient {
21 const std::string CLASS_NAME  = "UpdateClient";
22 constexpr int32_t REF_COUNT = 20;
23 napi_ref thread_local g_reference = nullptr;
24 
UpdateClientJSConstructor(napi_env env,napi_callback_info info)25 napi_value UpdateClientJSConstructor(napi_env env, napi_callback_info info)
26 {
27     // Count of argument is 1
28     size_t argc = 1;
29     // Set second argument
30     napi_value args[1] = {0};
31     napi_value thisVar = nullptr;
32     void* data = nullptr;
33     napi_get_cb_info(env, info, &argc, args, &thisVar, &data);
34     UpdateClient* client = new UpdateClient(env, thisVar);
35     napi_wrap(env, thisVar, client,
36         [](napi_env env, void* data, void* hint) {
37             CLIENT_LOGI("UpdateClient Destructor");
38             UpdateClient* client = (UpdateClient*)data;
39             delete client;
40             client = nullptr;
41         },
42         nullptr, nullptr);
43     return thisVar;
44 }
45 
GetAndCreateJsUpdateClient(napi_env env,napi_callback_info info,napi_value & obj)46 UpdateClient *GetAndCreateJsUpdateClient(napi_env env, napi_callback_info info, napi_value &obj)
47 {
48     napi_value constructor = nullptr;
49     UpdateClient *client = nullptr;
50     napi_status status = napi_get_reference_value(env, g_reference, &constructor);
51     CLIENT_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Error get client");
52     status = napi_new_instance(env, constructor, 0, nullptr, &obj);
53     CLIENT_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Error get client");
54 
55     napi_unwrap(env, obj, (void**)&client);
56     return client;
57 }
58 
GetUpdateClient(napi_env env,napi_callback_info info)59 UpdateClient *GetUpdateClient(napi_env env, napi_callback_info info)
60 {
61     size_t argc = 1;
62     napi_value argv[1] = {0};
63     napi_value thisVar = nullptr;
64     void* data = nullptr;
65     napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
66 
67     UpdateClient *client = nullptr;
68     napi_unwrap(env, thisVar, (void**)&client);
69     return client;
70 }
71 
GetUpdater(napi_env env,napi_callback_info info)72 napi_value GetUpdater(napi_env env, napi_callback_info info)
73 {
74     UpdateClient* client = GetUpdateClient(env, info);
75     if (client != nullptr) {
76         return client->GetUpdater(env, info, 1);
77     }
78     napi_value obj = nullptr;
79     client = GetAndCreateJsUpdateClient(env, info, obj);
80     if (client != nullptr) {
81         napi_value result = client->GetUpdater(env, info, 1);
82         if (result != nullptr) {
83             return obj;
84         }
85     }
86     napi_remove_wrap(env, obj, (void**)&client);
87     delete client;
88     return nullptr;
89 }
90 
GetUpdaterForOther(napi_env env,napi_callback_info info)91 napi_value GetUpdaterForOther(napi_env env, napi_callback_info info)
92 {
93     UpdateClient* client = GetUpdateClient(env, info);
94     if (client != nullptr) {
95         return client->GetUpdaterForOther(env, info);
96     }
97     napi_value obj = nullptr;
98     client = GetAndCreateJsUpdateClient(env, info, obj);
99     if (client != nullptr) {
100         client->GetUpdaterForOther(env, info);
101     }
102     return obj;
103 }
104 
GetUpdaterFromOther(napi_env env,napi_callback_info info)105 napi_value GetUpdaterFromOther(napi_env env, napi_callback_info info)
106 {
107     UpdateClient* client = GetUpdateClient(env, info);
108     if (client != nullptr) {
109         return client->GetUpdaterFromOther(env, info);
110     }
111     napi_value obj = nullptr;
112     client = GetAndCreateJsUpdateClient(env, info, obj);
113     if (client != nullptr) {
114         client->GetUpdaterFromOther(env, info);
115     }
116     return obj;
117 }
118 
CheckNewVersion(napi_env env,napi_callback_info info)119 napi_value CheckNewVersion(napi_env env, napi_callback_info info)
120 {
121     UpdateClient* client = GetUpdateClient(env, info);
122     CLIENT_CHECK_NAPI_CALL(env, client != nullptr, return nullptr, "Error get client");
123     return client->CheckNewVersion(env, info);
124 }
125 
SetUpdatePolicy(napi_env env,napi_callback_info info)126 napi_value SetUpdatePolicy(napi_env env, napi_callback_info info)
127 {
128     UpdateClient* client = GetUpdateClient(env, info);
129     CLIENT_CHECK_NAPI_CALL(env, client != nullptr, return nullptr, "Error get client");
130     return client->SetUpdatePolicy(env, info);
131 }
132 
GetUpdatePolicy(napi_env env,napi_callback_info info)133 napi_value GetUpdatePolicy(napi_env env, napi_callback_info info)
134 {
135     UpdateClient* client = GetUpdateClient(env, info);
136     CLIENT_CHECK_NAPI_CALL(env, client != nullptr, return nullptr, "Error get client");
137     return client->GetUpdatePolicy(env, info);
138 }
139 
DownloadVersion(napi_env env,napi_callback_info info)140 napi_value DownloadVersion(napi_env env, napi_callback_info info)
141 {
142     UpdateClient* client = GetUpdateClient(env, info);
143     CLIENT_CHECK_NAPI_CALL(env, client != nullptr, return nullptr, "Error get client");
144     return client->DownloadVersion(env, info);
145 }
146 
CancelUpgrade(napi_env env,napi_callback_info info)147 napi_value CancelUpgrade(napi_env env, napi_callback_info info)
148 {
149     UpdateClient* client = GetUpdateClient(env, info);
150     CLIENT_CHECK_NAPI_CALL(env, client != nullptr, return nullptr, "Error get client");
151     return client->CancelUpgrade(env, info);
152 }
153 
UpgradeVersion(napi_env env,napi_callback_info info)154 napi_value UpgradeVersion(napi_env env, napi_callback_info info)
155 {
156     UpdateClient* client = GetUpdateClient(env, info);
157     CLIENT_CHECK_NAPI_CALL(env, client != nullptr, return nullptr, "Error get client");
158     return client->UpgradeVersion(env, info);
159 }
160 
GetNewVersionInfo(napi_env env,napi_callback_info info)161 napi_value GetNewVersionInfo(napi_env env, napi_callback_info info)
162 {
163     UpdateClient* client = GetUpdateClient(env, info);
164     CLIENT_CHECK_NAPI_CALL(env, client != nullptr, return nullptr, "Error get client");
165     return client->GetNewVersionInfo(env, info);
166 }
167 
GetUpgradeStatus(napi_env env,napi_callback_info info)168 napi_value GetUpgradeStatus(napi_env env, napi_callback_info info)
169 {
170     UpdateClient* client = GetUpdateClient(env, info);
171     CLIENT_CHECK_NAPI_CALL(env, client != nullptr, return nullptr, "Error get client");
172     return client->GetUpgradeStatus(env, info);
173 }
174 
UnsubscribeEvent(napi_env env,napi_callback_info info)175 napi_value UnsubscribeEvent(napi_env env, napi_callback_info info)
176 {
177     UpdateClient* client = GetUpdateClient(env, info);
178     CLIENT_CHECK_NAPI_CALL(env, client != nullptr, return nullptr, "Error get client");
179     return client->UnsubscribeEvent(env, info);
180 }
181 
SubscribeEvent(napi_env env,napi_callback_info info)182 napi_value SubscribeEvent(napi_env env, napi_callback_info info)
183 {
184     UpdateClient* client = GetUpdateClient(env, info);
185     CLIENT_CHECK_NAPI_CALL(env, client != nullptr, return nullptr, "Error get client");
186     return client->SubscribeEvent(env, info);
187 }
188 
ApplyNewVersion(napi_env env,napi_callback_info info)189 napi_value ApplyNewVersion(napi_env env, napi_callback_info info)
190 {
191     UpdateClient* client = GetUpdateClient(env, info);
192     CLIENT_CHECK_NAPI_CALL(env, client != nullptr, return nullptr, "Error get client");
193     return client->ApplyNewVersion(env, info);
194 }
195 
RebootAndClean(napi_env env,napi_callback_info info)196 napi_value RebootAndClean(napi_env env, napi_callback_info info)
197 {
198     UpdateClient* client = GetUpdateClient(env, info);
199     CLIENT_CHECK_NAPI_CALL(env, client != nullptr, return nullptr, "Error get client");
200     return client->RebootAndClean(env, info);
201 }
202 
VerifyUpdatePackage(napi_env env,napi_callback_info info)203 napi_value VerifyUpdatePackage(napi_env env, napi_callback_info info)
204 {
205     UpdateClient* client = GetUpdateClient(env, info);
206     CLIENT_CHECK_NAPI_CALL(env, client != nullptr, return nullptr, "Error get client");
207     return client->VerifyUpdatePackage(env, info);
208 }
209 
210 #ifdef UPDATER_UT
UpdateClientInit(napi_env env,napi_value exports)211 napi_value UpdateClientInit(napi_env env, napi_value exports)
212 #else
213 static napi_value UpdateClientInit(napi_env env, napi_value exports)
214 #endif
215 {
216     CLIENT_LOGI("UpdateClientInit");
217     // Registration function
218     napi_property_descriptor desc[] = {
219         DECLARE_NAPI_FUNCTION("getUpdater", GetUpdater),
220         DECLARE_NAPI_FUNCTION("getUpdaterForOther", GetUpdaterForOther),
221         DECLARE_NAPI_FUNCTION("getUpdaterFromOther", GetUpdaterFromOther),
222     };
223     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
224 
225     // Registration object
226     napi_property_descriptor descriptors[] = {
227         DECLARE_NAPI_FUNCTION("getUpdater", GetUpdater),
228         DECLARE_NAPI_FUNCTION("getUpdaterForOther", GetUpdaterForOther),
229         DECLARE_NAPI_FUNCTION("getUpdaterFromOther", GetUpdaterFromOther),
230 
231         DECLARE_NAPI_FUNCTION("checkNewVersion", CheckNewVersion),
232         DECLARE_NAPI_FUNCTION("getNewVersionInfo", GetNewVersionInfo),
233 
234         DECLARE_NAPI_FUNCTION("setUpdatePolicy", SetUpdatePolicy),
235         DECLARE_NAPI_FUNCTION("getUpdatePolicy", GetUpdatePolicy),
236         DECLARE_NAPI_FUNCTION("getUpgradeStatus", GetUpgradeStatus),
237 
238         DECLARE_NAPI_FUNCTION("cancel", CancelUpgrade),
239         DECLARE_NAPI_FUNCTION("download", DownloadVersion),
240         DECLARE_NAPI_FUNCTION("upgrade", UpgradeVersion),
241 
242         DECLARE_NAPI_FUNCTION("applyNewVersion", ApplyNewVersion),
243         DECLARE_NAPI_FUNCTION("rebootAndCleanUserData", RebootAndClean),
244         DECLARE_NAPI_FUNCTION("verifyUpdatePackage", VerifyUpdatePackage),
245 
246         DECLARE_NAPI_FUNCTION("on", SubscribeEvent),
247         DECLARE_NAPI_FUNCTION("off", UnsubscribeEvent)
248     };
249 
250     napi_value result = nullptr;
251     napi_define_class(env, CLASS_NAME.c_str(), CLASS_NAME.size(), UpdateClientJSConstructor,
252         nullptr, sizeof(descriptors) / sizeof(*descriptors), descriptors, &result);
253     napi_set_named_property(env, exports, CLASS_NAME.c_str(), result);
254     napi_status status = napi_create_reference(env, result, REF_COUNT, &g_reference);
255     CLIENT_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Failed to create_reference");
256     CLIENT_LOGI("UpdateClient g_reference %p", g_reference);
257     return exports;
258 }
259 
260 /*
261  * Module definition
262  */
263 static napi_module g_module = {
264     .nm_version = 1,
265     .nm_flags = 0,
266     .nm_filename = nullptr,
267     .nm_register_func = UpdateClientInit,
268     .nm_modname = "update",
269     .nm_priv = ((void*)0),
270     .reserved = { 0 }
271 };
272 
273 /*
274  * Module registration function
275  */
RegisterModule(void)276 extern "C" __attribute__((constructor)) void RegisterModule(void)
277 {
278     napi_module_register(&g_module);
279 }
280 }
281