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 #ifndef NWEB_WEB_DOWNLOAD_DELEGATE_H
17 #define NWEB_WEB_DOWNLOAD_DELEGATE_H
18 
19 #include <string>
20 
21 #include "napi/native_api.h"
22 #include "napi/native_common.h"
23 #include "napi/native_node_api.h"
24 
25 #include "web_download_item.h"
26 
27 namespace OHOS {
28 namespace NWeb {
29 class WebDownloadDelegate {
30 public:
31     static WebDownloadDelegate *FromNWebID(int32_t nwebId);
32 
33     explicit WebDownloadDelegate(napi_env env);
34     ~WebDownloadDelegate();
35 
36     void DownloadBeforeStart(WebDownloadItem *webDownloadItem);
37     void DownloadDidUpdate(WebDownloadItem *webDownloadItem);
38     void DownloadDidFail(WebDownloadItem *webDownloadItem);
39     void DownloadDidFinish(WebDownloadItem *webDownloadItem);
40 
41     void PutDownloadBeforeStart(napi_env, napi_value callback);
42     void PutDownloadDidUpdate(napi_env, napi_value callback);
43     void PutDownloadDidFinish(napi_env, napi_value callback);
44     void PutDownloadDidFail(napi_env, napi_value callback);
45 
46     int32_t GetNWebId() const;
47     void SetNWebId(int32_t nwebId);
48     napi_env GetEnv();
49 
50     void RemoveSelfRef();
51 
52     //  create this reference to make web download delegate not be deleted by gc.
53     napi_ref delegate_;
54 
55 private:
56     int32_t nwebId_ = -1;
57 
58     napi_ref download_before_start_callback_;
59     napi_ref download_did_update_callback_;
60     napi_ref download_did_finish_callback_;
61     napi_ref download_did_fail_callback_;
62     napi_env env_;
63 };
64 } // namespace NWeb
65 } // namespace OHOS
66 
67 #endif // NWEB_WEB_DOWNLOAD_DELEGATE_H