• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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_pasteboard_common.h"
17 #include "pasteboard_hilog.h"
18 #include "pasteboard_progress_signal_napi.h"
19 
20 using namespace OHOS::MiscServices;
21 using namespace OHOS::Media;
22 
23 namespace OHOS {
24 namespace MiscServicesNapi {
25 static thread_local napi_ref g_progressSignal = nullptr;
26 constexpr size_t MAX_ARGS = 4;
27 
ProgressSignalNapi()28 ProgressSignalNapi::ProgressSignalNapi() : env_(nullptr) {}
29 
~ProgressSignalNapi()30 ProgressSignalNapi::~ProgressSignalNapi() {}
31 
Cancel(napi_env env,napi_callback_info info)32 napi_value ProgressSignalNapi::Cancel(napi_env env, napi_callback_info info)
33 {
34     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "Cancel is called!");
35     ProgressSignalClient::GetInstance().Cancel();
36     napi_value result = nullptr;
37     napi_get_null(env, &result);
38     return result;
39 }
40 
Destructor(napi_env env,void * nativeObject,void * finalize_hint)41 void ProgressSignalNapi::Destructor(napi_env env, void *nativeObject, void *finalize_hint)
42 {
43     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "Destructor");
44     ProgressSignalNapi *obj = static_cast<ProgressSignalNapi *>(nativeObject);
45     delete obj;
46 }
47 
New(napi_env env,napi_callback_info info)48 napi_value ProgressSignalNapi::New(napi_env env, napi_callback_info info)
49 {
50     size_t argc = MAX_ARGS;
51     napi_value argv[MAX_ARGS] = { 0 };
52     napi_value thisVar = nullptr;
53     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
54     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "proc.");
55 
56     // get native object
57     ProgressSignalNapi *obj = new (std::nothrow) ProgressSignalNapi();
58     if (obj == nullptr) {
59         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "New obj is null.");
60         return nullptr;
61     }
62     obj->env_ = env;
63     ASSERT_CALL(env, napi_wrap(env, thisVar, obj, ProgressSignalNapi::Destructor,
64                        nullptr, // finalize_hint
65                        nullptr), obj);
66     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "end.");
67     return thisVar;
68 }
69 
ProgressSignalNapiInit(napi_env env,napi_value exports)70 napi_value ProgressSignalNapi::ProgressSignalNapiInit(napi_env env, napi_value exports)
71 {
72     napi_status status = napi_ok;
73     napi_property_descriptor descriptors[] = {
74         DECLARE_NAPI_FUNCTION("cancel", Cancel),
75     };
76 
77     napi_value constructor;
78     napi_define_class(env, "ProgressSignal", NAPI_AUTO_LENGTH, New, nullptr,
79         sizeof(descriptors) / sizeof(napi_property_descriptor), descriptors, &constructor);
80     if (status != napi_ok) {
81         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Failed to define class at ProgressSignalNapiInit");
82         return nullptr;
83     }
84 
85     napi_create_reference(env, constructor, 1, &g_progressSignal);
86     status = napi_set_named_property(env, exports, "ProgressSignal", constructor);
87     if (status != napi_ok) {
88         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Set property failed when ProgressSignalNapiInit");
89         return nullptr;
90     }
91     return exports;
92 }
93 } // namespace MiscServicesNapi
94 } // namespace OHOS
95