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_manager.h"
17 
18 #include <js_native_api.h>
19 #include <js_native_api_types.h>
20 #include <napi/native_api.h>
21 #include <securec.h>
22 #include <cstring>
23 
24 #include "business_error.h"
25 #include "nweb_c_api.h"
26 #include "nweb_log.h"
27 #include "web_download_delegate.h"
28 #include "web_download_manager.h"
29 #include "web_errors.h"
30 
31 namespace OHOS {
32 namespace NWeb {
33 using namespace NWebError;
34 
35 // static
JS_SetDownloadDelegate(napi_env env,napi_callback_info info)36 napi_value NapiWebDownloadManager::JS_SetDownloadDelegate(napi_env env, napi_callback_info info)
37 {
38     WVLOG_D("[DOWNLOAD] NapiWebDownloadManager::JS_SetDownloadDelegate");
39     size_t argc = 1;
40     napi_value argv[1] = {0};
41     napi_value thisVar = nullptr;
42     void *data = nullptr;
43     napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
44 
45     napi_value obj = argv[0];
46     // check download delegate is object type
47     napi_valuetype objType = napi_undefined;
48     napi_typeof(env, argv[0], &objType);
49 
50     WebDownloadDelegate *delegate = nullptr;
51     napi_unwrap(env, obj, (void **)&delegate);
52     if (!delegate) {
53         WVLOG_E("[DOWNLOAD] NapiWebDownloadManager::JS_SetDownloadDelegate delegate is null");
54         return nullptr;
55     }
56     napi_create_reference(env, obj, 1, &delegate->delegate_);
57     WebDownloadManager::SetDownloadDelegate(delegate);
58     return nullptr;
59 }
60 
61 // static
JS_ResumeDownload(napi_env env,napi_callback_info info)62 napi_value NapiWebDownloadManager::JS_ResumeDownload(napi_env env, napi_callback_info info)
63 {
64     WVLOG_D("[DOWNLOAD] NapiWebDownloadManager::JS_ResumeDownload");
65     size_t argc = 1;
66     napi_value argv[1] = {0};
67     napi_value thisVar = nullptr;
68     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
69 
70     if (!WebDownloadManager::HasValidDelegate()) {
71         BusinessError::ThrowErrorByErrcode(env, NO_DOWNLOAD_DELEGATE_SET);
72         return nullptr;
73     }
74 
75     // check web download is object type
76     napi_valuetype objType = napi_undefined;
77     napi_typeof(env, argv[0], &objType);
78 
79     WebDownloadItem *webDownloadItem = nullptr;
80     napi_status status = napi_unwrap(env, argv[0], (void **)&webDownloadItem);
81     if (status != napi_ok || webDownloadItem == nullptr) {
82         WVLOG_E("[DOWNLOAD] unwrap webDownloadItem failed.");
83     }
84 
85     WebDownloadManager::ResumeDownload(webDownloadItem);
86     return nullptr;
87 }
88 
JS_Constructor(napi_env env,napi_callback_info info)89 napi_value NapiWebDownloadManager::JS_Constructor(napi_env env, napi_callback_info info)
90 {
91     WVLOG_D("[DOWNLOAD] NapiWebDownloadManager::JS_Constructor");
92     size_t argc = 1;
93     napi_value argv[1] = {0};
94     napi_value thisVar = nullptr;
95     void *data = nullptr;
96     napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
97 
98     return thisVar;
99 }
100 
Init(napi_env env,napi_value exports)101 napi_value NapiWebDownloadManager::Init(napi_env env, napi_value exports)
102 {
103     WVLOG_D("[DOWNLOAD] NapiWebDownloadManager::Init");
104     napi_property_descriptor properties[] = {
105         { "setDownloadDelegate", nullptr, JS_SetDownloadDelegate, nullptr, nullptr, nullptr, napi_static, nullptr },
106         { "resumeDownload", nullptr, JS_ResumeDownload, nullptr, nullptr, nullptr, napi_static, nullptr },
107     };
108     napi_value webDownloadManagerClass = nullptr;
109     napi_define_class(env, WEB_DOWNLOAD_MANAGER.c_str(), WEB_DOWNLOAD_MANAGER.length(), JS_Constructor, nullptr,
110         sizeof(properties) / sizeof(properties[0]), properties, &webDownloadManagerClass);
111     napi_set_named_property(env, exports, WEB_DOWNLOAD_MANAGER.c_str(), webDownloadManagerClass);
112 
113     return exports;
114 }
115 } // namespace NWeb
116 } // namespace OHOS
117