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 "napi_web_download_delegate.h"
17
18 #include <cstring>
19 #include <js_native_api.h>
20 #include <js_native_api_types.h>
21 #include <napi/native_api.h>
22 #include <securec.h>
23
24 #include "nweb_log.h"
25 #include "web_download_delegate.h"
26
27 namespace OHOS {
28 namespace NWeb {
JS_DownloadBeforeStart(napi_env env,napi_callback_info cbinfo)29 napi_value NapiWebDownloadDelegate::JS_DownloadBeforeStart(napi_env env, napi_callback_info cbinfo)
30 {
31 WVLOG_D("[DOWNLOAD] NapiWebDownloadDelegate::JS_DownloadBeforeStart");
32 size_t argc = 1;
33 napi_value argv[1] = {0};
34 napi_value thisVar = nullptr;
35 void *data = nullptr;
36 WebDownloadDelegate *webDownloadDelegate = nullptr;
37 napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
38
39 napi_unwrap(env, thisVar, (void **)&webDownloadDelegate);
40 if (!webDownloadDelegate) {
41 WVLOG_E("[DOWNLOAD] webDownloadDelegate is null");
42 return thisVar;
43 }
44
45 napi_valuetype valueType = napi_undefined;
46 napi_typeof(env, argv[0], &valueType);
47
48 webDownloadDelegate->PutDownloadBeforeStart(env, argv[0]);
49 return thisVar;
50 }
51
JS_DownloadDidUpdate(napi_env env,napi_callback_info cbinfo)52 napi_value NapiWebDownloadDelegate::JS_DownloadDidUpdate(napi_env env, napi_callback_info cbinfo)
53 {
54 WVLOG_D("[DOWNLOAD] NapiWebDownloadDelegate::JS_DownloadDidUpdate");
55 size_t argc = 1;
56 napi_value argv[1] = {0};
57 napi_value thisVar = nullptr;
58 void *data = nullptr;
59 WebDownloadDelegate *webDownloadDelegate = nullptr;
60 napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
61
62 napi_unwrap(env, thisVar, (void **)&webDownloadDelegate);
63 if (!webDownloadDelegate) {
64 WVLOG_E("[DOWNLOAD] webDownloadDelegate is null");
65 return thisVar;
66 }
67 napi_valuetype valueType = napi_undefined;
68 napi_typeof(env, argv[0], &valueType);
69
70 webDownloadDelegate->PutDownloadDidUpdate(env, argv[0]);
71 return thisVar;
72 }
73
JS_DownloadDidFinish(napi_env env,napi_callback_info cbinfo)74 napi_value NapiWebDownloadDelegate::JS_DownloadDidFinish(napi_env env, napi_callback_info cbinfo)
75 {
76 WVLOG_D("[DOWNLOAD] NapiWebDownloadDelegate::JS_DownloadDidFinish");
77 size_t argc = 1;
78 napi_value argv[1] = {0};
79 napi_value thisVar = nullptr;
80 void *data = nullptr;
81 WebDownloadDelegate *webDownloadDelegate = nullptr;
82 napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
83
84 napi_unwrap(env, thisVar, (void **)&webDownloadDelegate);
85 if (!webDownloadDelegate) {
86 WVLOG_E("[DOWNLOAD] webDownloadDelegate is null");
87 return thisVar;
88 }
89 napi_valuetype valueType = napi_undefined;
90 napi_typeof(env, argv[0], &valueType);
91
92 webDownloadDelegate->PutDownloadDidFinish(env, argv[0]);
93 return thisVar;
94 }
95
JS_DownloadDidFail(napi_env env,napi_callback_info cbinfo)96 napi_value NapiWebDownloadDelegate::JS_DownloadDidFail(napi_env env, napi_callback_info cbinfo)
97 {
98 WVLOG_D("[DOWNLOAD] NapiWebDownloadDelegate::JS_DownloadDidFail");
99 size_t argc = 1;
100 napi_value argv[1] = {0};
101 napi_value thisVar = nullptr;
102 void *data = nullptr;
103 WebDownloadDelegate *webDownloadDelegate = nullptr;
104 napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
105
106 napi_unwrap(env, thisVar, (void **)&webDownloadDelegate);
107 if (!webDownloadDelegate) {
108 WVLOG_E("[DOWNLOAD] webDownloadDelegate is null");
109 return thisVar;
110 }
111 napi_valuetype valueType = napi_undefined;
112 napi_typeof(env, argv[0], &valueType);
113
114 webDownloadDelegate->PutDownloadDidFail(env, argv[0]);
115 return thisVar;
116 }
117
JS_Constructor(napi_env env,napi_callback_info cbinfo)118 napi_value NapiWebDownloadDelegate::JS_Constructor(napi_env env, napi_callback_info cbinfo)
119 {
120 WVLOG_D("[DOWNLOAD] NapiWebDownloadDelegate::JS_Constructor");
121 WebDownloadDelegate *delegate = new WebDownloadDelegate(env);
122 napi_value thisVar = nullptr;
123 napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, nullptr);
124 napi_wrap(
125 env, thisVar, delegate,
126 [](napi_env /* env */, void *data, void * /* hint */) {
127 WebDownloadDelegate *delegate = (WebDownloadDelegate *)data;
128 delete delegate;
129 },
130 nullptr, nullptr);
131
132 return thisVar;
133 }
134
Init(napi_env env,napi_value exports)135 napi_value NapiWebDownloadDelegate::Init(napi_env env, napi_value exports)
136 {
137 WVLOG_D("[DOWNLOAD] NapiWebDownloadDelegate::Init");
138 napi_property_descriptor properties[] = {
139 DECLARE_NAPI_FUNCTION("onBeforeDownload", JS_DownloadBeforeStart),
140 DECLARE_NAPI_FUNCTION("onDownloadUpdated", JS_DownloadDidUpdate),
141 DECLARE_NAPI_FUNCTION("onDownloadFinish", JS_DownloadDidFinish),
142 DECLARE_NAPI_FUNCTION("onDownloadFailed", JS_DownloadDidFail),
143 };
144 const std::string WEB_DOWNLOAD_DELEGATE = "WebDownloadDelegate";
145 napi_value webDownloadDelegateClass = nullptr;
146 napi_define_class(env, WEB_DOWNLOAD_DELEGATE.c_str(), WEB_DOWNLOAD_DELEGATE.length(), JS_Constructor, nullptr,
147 sizeof(properties) / sizeof(properties[0]), properties, &webDownloadDelegateClass);
148 napi_set_named_property(env, exports, WEB_DOWNLOAD_DELEGATE.c_str(), webDownloadDelegateClass);
149
150 return exports;
151 }
152 } // namespace NWeb
153 } // namespace OHOS