1 /*
2  * Copyright (c) 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_component_snapshot.h"
17 
18 #include "interfaces/napi/kits/utils/napi_utils.h"
19 #include "js_native_api.h"
20 #include "js_native_api_types.h"
21 #include "napi/native_common.h"
22 #include "node_api_types.h"
23 #include "base/utils/utils.h"
24 #ifdef PIXEL_MAP_SUPPORTED
25 #include "pixel_map.h"
26 #include "pixel_map_napi.h"
27 #endif
28 
29 #include "node_api.h"
30 
31 #include "bridge/common/utils/utils.h"
32 #include "core/common/ace_engine.h"
33 
34 #include "frameworks/bridge/common/utils/engine_helper.h"
35 
36 namespace OHOS::Ace::Napi {
37 namespace {
38 
39 struct SnapshotAsyncCtx {
40     napi_env env = nullptr;
41     napi_deferred deferred = nullptr;
42     napi_ref callbackRef = nullptr;
43     std::shared_ptr<Media::PixelMap> pixmap = nullptr;
44     int32_t errCode = -1;
45     int32_t instanceId = -1;
46 };
47 
OnComplete(SnapshotAsyncCtx * asyncCtx,std::function<void ()> finishCallback)48 void OnComplete(SnapshotAsyncCtx* asyncCtx, std::function<void()> finishCallback)
49 {
50     auto container = AceEngine::Get().GetContainer(asyncCtx->instanceId);
51     if (!container) {
52         LOGW("container is null. %{public}d", asyncCtx->instanceId);
53         return;
54     }
55 
56     auto taskExecutor = container->GetTaskExecutor();
57     if (!taskExecutor) {
58         LOGW("taskExecutor is null.");
59         return;
60     }
61     taskExecutor->PostTask(
62         [asyncCtx, finishCallback]() {
63             std::unique_ptr<SnapshotAsyncCtx> ctx(asyncCtx);
64             napi_handle_scope scope = nullptr;
65             napi_open_handle_scope(ctx->env, &scope);
66 
67             // callback result format: [Error, PixelMap]
68             napi_value result[2] = { nullptr };
69             napi_get_undefined(ctx->env, &result[0]);
70             napi_get_undefined(ctx->env, &result[1]);
71 
72             if (ctx->errCode == ERROR_CODE_NO_ERROR) {
73 #ifdef PIXEL_MAP_SUPPORTED
74                 // convert pixelMap to napi value
75                 result[1] = Media::PixelMapNapi::CreatePixelMap(ctx->env, ctx->pixmap);
76 #endif
77             }
78             napi_create_int32(ctx->env, ctx->errCode, &result[0]);
79 
80             if (ctx->deferred) {
81                 // promise
82                 if (ctx->errCode == ERROR_CODE_NO_ERROR) {
83                     napi_resolve_deferred(ctx->env, ctx->deferred, result[1]);
84                 } else {
85                     napi_reject_deferred(ctx->env, ctx->deferred, result[0]);
86                 }
87             } else {
88                 // callback
89                 napi_value ret = nullptr;
90                 napi_value napiCallback = nullptr;
91                 napi_get_reference_value(ctx->env, ctx->callbackRef, &napiCallback);
92                 napi_call_function(ctx->env, nullptr, napiCallback, 2, result, &ret);
93                 napi_delete_reference(ctx->env, ctx->callbackRef);
94             }
95 
96             napi_close_handle_scope(ctx->env, scope);
97             if (finishCallback) {
98                 finishCallback();
99             }
100         },
101         TaskExecutor::TaskType::JS, "ArkUIComponentSnapshotComplete");
102 }
103 } // namespace
104 
JsComponentSnapshot(napi_env env,napi_callback_info info)105 JsComponentSnapshot::JsComponentSnapshot(napi_env env, napi_callback_info info) : env_(env), argc_(ARGC_MAX)
106 {
107     napi_value thisVar = nullptr;
108     void* data = nullptr;
109 
110     // get arguments from JS
111     napi_get_cb_info(env, info, &argc_, argv_, &thisVar, &data);
112 }
113 
CheckArgs(napi_valuetype firstArgType)114 bool JsComponentSnapshot::CheckArgs(napi_valuetype firstArgType)
115 
116 {
117     size_t minArgc = 1;
118     std::string errorMsg;
119     if (argc_ < minArgc) {
120         errorMsg = "The number of parameters must be greater than or equal to 1.";
121         LOGE("%{public}s", errorMsg.c_str());
122         NapiThrow(env_, errorMsg, ERROR_CODE_PARAM_INVALID);
123 
124         return false;
125     }
126     if (argc_ > ARGC_MAX) {
127         errorMsg = "The largest number of parameters is 2.";
128         LOGE("%{public}s", errorMsg.c_str());
129         NapiThrow(env_, errorMsg, ERROR_CODE_PARAM_INVALID);
130         return false;
131     }
132     napi_valuetype type = napi_undefined;
133     napi_typeof(env_, argv_[0], &type);
134     if (type != firstArgType) {
135         errorMsg = "parameter id is not of type string";
136         LOGE("%{public}s", errorMsg.c_str());
137         NapiThrow(env_, errorMsg, ERROR_CODE_PARAM_INVALID);
138         return false;
139     }
140     return true;
141 }
142 
CreateCallback(napi_value * result)143 auto JsComponentSnapshot::CreateCallback(napi_value* result)
144 {
145     auto* asyncCtx = new SnapshotAsyncCtx;
146     napi_valuetype type = napi_undefined;
147     // parse JsCallback
148     if (argc_ >= 2) {
149         napi_typeof(env_, argv_[1], &type);
150         if (type == napi_function) {
151             napi_create_reference(env_, argv_[1], 1, &asyncCtx->callbackRef);
152         }
153     }
154     // init promise
155     if (!asyncCtx->callbackRef) {
156         napi_create_promise(env_, &asyncCtx->deferred, result);
157     } else {
158         napi_get_undefined(env_, result);
159     }
160 
161     asyncCtx->env = env_;
162     asyncCtx->instanceId = Container::CurrentIdSafely();
163 
164     return [asyncCtx](std::shared_ptr<Media::PixelMap> pixmap, int32_t errCode, std::function<void()> finishCallback) {
165         asyncCtx->pixmap = std::move(pixmap);
166         asyncCtx->errCode = errCode;
167         OnComplete(asyncCtx, finishCallback);
168     };
169 }
170 
GetArgv(int32_t idx)171 napi_value JsComponentSnapshot::GetArgv(int32_t idx)
172 {
173     if (idx >= ARGC_MAX) {
174         return nullptr;
175     }
176     return argv_[idx];
177 }
178 
ParseParamForBuilder(NG::SnapshotParam & param)179 void JsComponentSnapshot::ParseParamForBuilder(NG::SnapshotParam& param)
180 {
181     // parse second param for builder interface
182     if (argc_ >= 2) {
183         napi_valuetype type = napi_undefined;
184         napi_typeof(env_, argv_[1], &type);
185         if (type != napi_function) {
186             ParseParam(1, param);
187         }
188     }
189     // parse third param for builder interface
190     if (argc_ >= 3) {
191         ParseParam(2, param);
192     }
193     // parse fourth param for builder interface
194     if (argc_ >= 4) {
195         ParseParam(3, param);
196     }
197     // parse fifth param for builder interface
198     if (argc_ == 5) {
199         ParseParam(4, param);
200     }
201 }
202 
ParseParamForGet(NG::SnapshotOptions & options)203 void JsComponentSnapshot::ParseParamForGet(NG::SnapshotOptions& options)
204 {
205     // parse options param for Promise
206     if (argc_ >= 2) {
207         napi_valuetype type = napi_undefined;
208         napi_typeof(env_, argv_[1], &type);
209         if (type != napi_function) {
210             ParseOptions(1, options);
211         }
212     }
213     // parse options param for Callback
214     if (argc_ == 3) {
215         ParseOptions(2, options);
216     }
217 }
218 
ParseParam(int32_t idx,NG::SnapshotParam & param)219 void JsComponentSnapshot::ParseParam(int32_t idx, NG::SnapshotParam& param)
220 {
221     if (static_cast<int32_t>(argc_) <= idx) {
222         return;
223     }
224     napi_valuetype type = napi_undefined;
225     napi_typeof(env_, argv_[idx], &type);
226     // parse delay param
227     if (type == napi_number) {
228         int32_t delayTime = 0;
229         napi_get_value_int32(env_, argv_[idx], &delayTime);
230         if (delayTime >= 0) {
231             param.delay = delayTime;
232         }
233     }
234     // parse checkImageStatus param
235     if (type == napi_boolean) {
236         bool checkImageStatus = 0;
237         napi_get_value_bool(env_, argv_[idx], &checkImageStatus);
238         param.checkImageStatus = checkImageStatus;
239     }
240     // parse SnapshotOptions param
241     if (type == napi_object) {
242         NG::SnapshotOptions options;
243         ParseOptions(idx, options);
244         param.options = options;
245     }
246 }
247 
ParseOptions(int32_t idx,NG::SnapshotOptions & options)248 void JsComponentSnapshot::ParseOptions(int32_t idx, NG::SnapshotOptions& options)
249 {
250     if (static_cast<int32_t>(argc_) <= idx) {
251         return;
252     }
253     napi_valuetype type = napi_undefined;
254     napi_typeof(env_, argv_[idx], &type);
255     if (type != napi_object) {
256         return;
257     }
258 
259     bool result = false;
260     napi_has_named_property(env_, argv_[idx], "scale", &result);
261     if (result) {
262         napi_value scaleNapi = nullptr;
263         napi_get_named_property(env_, argv_[idx], "scale", &scaleNapi);
264         double scale = 0.0;
265         napi_get_value_double(env_, scaleNapi, &scale);
266         if (GreatNotEqual(scale, 0.0)) {
267             options.scale = static_cast<float>(scale);
268         }
269     }
270 
271     result = false;
272     napi_has_named_property(env_, argv_[idx], "waitUntilRenderFinished", &result);
273     if (result) {
274         napi_value waitUntilRenderFinishedNapi = nullptr;
275         napi_get_named_property(env_, argv_[idx], "waitUntilRenderFinished", &waitUntilRenderFinishedNapi);
276         bool waitUntilRenderFinished = false;
277         napi_get_value_bool(env_, waitUntilRenderFinishedNapi, &waitUntilRenderFinished);
278         options.waitUntilRenderFinished = waitUntilRenderFinished;
279     }
280 }
281 
JSSnapshotGet(napi_env env,napi_callback_info info)282 static napi_value JSSnapshotGet(napi_env env, napi_callback_info info)
283 {
284     napi_escapable_handle_scope scope = nullptr;
285     napi_open_escapable_handle_scope(env, &scope);
286 
287     JsComponentSnapshot helper(env, info);
288 
289     napi_value result = nullptr;
290 
291     if (!helper.CheckArgs(napi_valuetype::napi_string)) {
292         TAG_LOGW(AceLogTag::ACE_COMPONENT_SNAPSHOT, "Parsing the first argument failed, not of string type.");
293         napi_close_escapable_handle_scope(env, scope);
294         return result;
295     }
296 
297     // parse id
298     std::string componentId;
299     napi_valuetype valueType = napi_null;
300     GetNapiString(env, helper.GetArgv(0), componentId, valueType);
301 
302     auto delegate = EngineHelper::GetCurrentDelegateSafely();
303     if (!delegate) {
304         TAG_LOGW(AceLogTag::ACE_COMPONENT_SNAPSHOT,
305             "Can't get delegate of ace_engine. param: %{public}s",
306             componentId.c_str());
307         auto callback = helper.CreateCallback(&result);
308         callback(nullptr, ERROR_CODE_INTERNAL_ERROR, nullptr);
309         return result;
310     }
311 
312     NG::SnapshotOptions options;
313     helper.ParseParamForGet(options);
314 
315     delegate->GetSnapshot(componentId, helper.CreateCallback(&result), options);
316 
317     napi_escape_handle(env, scope, result, &result);
318     napi_close_escapable_handle_scope(env, scope);
319     return result;
320 }
321 
JSSnapshotFromBuilder(napi_env env,napi_callback_info info)322 static napi_value JSSnapshotFromBuilder(napi_env env, napi_callback_info info)
323 {
324     napi_escapable_handle_scope scope = nullptr;
325     napi_open_escapable_handle_scope(env, &scope);
326 
327     JsComponentSnapshot helper(env, info);
328     if (!helper.CheckArgs(napi_valuetype::napi_function)) {
329         TAG_LOGW(AceLogTag::ACE_COMPONENT_SNAPSHOT, "Parsing the first argument failed, not of function type.");
330         napi_close_escapable_handle_scope(env, scope);
331         return nullptr;
332     }
333 
334     napi_value result = nullptr;
335     auto delegate = EngineHelper::GetCurrentDelegateSafely();
336     if (!delegate) {
337         TAG_LOGW(AceLogTag::ACE_COMPONENT_SNAPSHOT, "Can't get delegate of ace_engine. ");
338         auto callback = helper.CreateCallback(&result);
339         callback(nullptr, ERROR_CODE_INTERNAL_ERROR, nullptr);
340         return nullptr;
341     }
342 
343     // create builder closure
344     auto builder = [build = helper.GetArgv(0), env] { napi_call_function(env, nullptr, build, 0, nullptr, nullptr); };
345 
346     NG::SnapshotParam param;
347     helper.ParseParamForBuilder(param);
348 
349     delegate->CreateSnapshot(builder, helper.CreateCallback(&result), true, param);
350 
351     napi_escape_handle(env, scope, result, &result);
352     napi_close_escapable_handle_scope(env, scope);
353     return result;
354 }
355 
JSSnapshotGetSync(napi_env env,napi_callback_info info)356 static napi_value JSSnapshotGetSync(napi_env env, napi_callback_info info)
357 {
358     napi_escapable_handle_scope scope = nullptr;
359     napi_open_escapable_handle_scope(env, &scope);
360 
361     JsComponentSnapshot helper(env, info);
362 
363     napi_value result = nullptr;
364 
365     if (!helper.CheckArgs(napi_valuetype::napi_string)) {
366         TAG_LOGW(AceLogTag::ACE_COMPONENT_SNAPSHOT, "Parsing the first argument failed, not of string type.");
367         napi_close_escapable_handle_scope(env, scope);
368         return result;
369     }
370 
371     // parse id
372     std::string componentId;
373     napi_valuetype valueType = napi_null;
374     GetNapiString(env, helper.GetArgv(0), componentId, valueType);
375 
376     auto delegate = EngineHelper::GetCurrentDelegateSafely();
377     if (!delegate) {
378         TAG_LOGW(AceLogTag::ACE_COMPONENT_SNAPSHOT,
379             "Can't get delegate of ace_engine. param: %{public}s",
380             componentId.c_str());
381         NapiThrow(env, "Delegate is null", ERROR_CODE_INTERNAL_ERROR);
382         napi_close_escapable_handle_scope(env, scope);
383         return result;
384     }
385 
386     NG::SnapshotOptions options;
387     helper.ParseParamForGet(options);
388 
389     auto pair = delegate->GetSyncSnapshot(componentId,  options);
390 
391     switch (pair.first) {
392         case ERROR_CODE_NO_ERROR :
393 #ifdef PIXEL_MAP_SUPPORTED
394             result = Media::PixelMapNapi::CreatePixelMap(env, pair.second);
395 #endif
396             break;
397         case ERROR_CODE_INTERNAL_ERROR :
398             napi_get_null(env, &result);
399             NapiThrow(env, "Internal error!", ERROR_CODE_INTERNAL_ERROR);
400             break;
401         case ERROR_CODE_COMPONENT_SNAPSHOT_TIMEOUT :
402             napi_get_null(env, &result);
403             NapiThrow(env, "ComponentSnapshot timeout!", ERROR_CODE_COMPONENT_SNAPSHOT_TIMEOUT);
404             break;
405     }
406     napi_escape_handle(env, scope, result, &result);
407     napi_close_escapable_handle_scope(env, scope);
408     return result;
409 }
410 
ComponentSnapshotExport(napi_env env,napi_value exports)411 static napi_value ComponentSnapshotExport(napi_env env, napi_value exports)
412 {
413     napi_property_descriptor snapshotDesc[] = {
414         DECLARE_NAPI_FUNCTION("get", JSSnapshotGet),
415         DECLARE_NAPI_FUNCTION("createFromBuilder", JSSnapshotFromBuilder),
416         DECLARE_NAPI_FUNCTION("getSync", JSSnapshotGetSync),
417     };
418     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(snapshotDesc) / sizeof(snapshotDesc[0]), snapshotDesc));
419 
420     return exports;
421 }
422 
423 static napi_module snapshotModule = {
424     .nm_version = 1,
425     .nm_flags = 0,
426     .nm_filename = nullptr,
427     .nm_register_func = ComponentSnapshotExport,
428     .nm_modname = "arkui.componentSnapshot",
429     .nm_priv = ((void*)0),
430     .reserved = { 0 },
431 };
432 
ComponentSnapshotRegister()433 extern "C" __attribute__((constructor)) void ComponentSnapshotRegister()
434 {
435     napi_module_register(&snapshotModule);
436 }
437 } // namespace OHOS::Ace::Napi
438