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 #ifndef OHOS_FILEMGMT_CLOUD_FILE_NAPI_H
17 #define OHOS_FILEMGMT_CLOUD_FILE_NAPI_H
18 
19 #include "cloud_download_callback.h"
20 #include "filemgmt_libn.h"
21 
22 namespace OHOS::FileManagement::CloudSync {
23 class CloudDownloadCallbackImpl;
24 class CloudFileNapi : public LibN::NExporter {
25 public:
CloudFileNapi(napi_env env,napi_value exports)26     CloudFileNapi(napi_env env, napi_value exports) : NExporter(env, exports) {}
27     ~CloudFileNapi() = default;
28 
29     bool Export() override;
30     bool ToExport(std::vector<napi_property_descriptor> props);
31     virtual void SetClassName(std::string classname);
32     std::string GetClassName() override;
33     static napi_value Constructor(napi_env env, napi_callback_info info);
34     static napi_value Start(napi_env env, napi_callback_info info);
35     static napi_value On(napi_env env, napi_callback_info info);
36     static napi_value Off(napi_env env, napi_callback_info info);
37     static napi_value Stop(napi_env env, napi_callback_info info);
38 
39 private:
40     static inline std::shared_ptr<CloudDownloadCallbackImpl> callback_;
41     inline static std::string className_ = "CloudFileNapi";
42 };
43 
44 class CloudDownloadCallbackImpl : public CloudDownloadCallback,
45                                   public std::enable_shared_from_this<CloudDownloadCallbackImpl> {
46 public:
47     CloudDownloadCallbackImpl(napi_env env, napi_value fun, bool isBatch = false);
48     ~CloudDownloadCallbackImpl() = default;
49     void OnDownloadProcess(const DownloadProgressObj &progress) override;
50     void DeleteReference();
51 
52     class UvChangeMsg {
53     public:
UvChangeMsg(std::shared_ptr<CloudDownloadCallbackImpl> CloudDownloadCallbackIn,DownloadProgressObj downloadProgress,bool isBatch)54         UvChangeMsg(std::shared_ptr<CloudDownloadCallbackImpl> CloudDownloadCallbackIn,
55                     DownloadProgressObj downloadProgress,
56                     bool isBatch)
57             : CloudDownloadCallback_(CloudDownloadCallbackIn), downloadProgress_(downloadProgress), isBatch_(isBatch)
58         {
59         }
~UvChangeMsg()60         ~UvChangeMsg() {}
61         std::weak_ptr<CloudDownloadCallbackImpl> CloudDownloadCallback_;
62         DownloadProgressObj downloadProgress_;
63         bool isBatch_;
64     };
65 
66 private:
67     static void OnComplete(UvChangeMsg *msg);
68     napi_env env_;
69     napi_ref cbOnRef_ = nullptr;
70     bool isBatch_;
71 };
72 } // namespace OHOS::FileManagement::CloudSync
73 #endif // OHOS_FILEMGMT_CLOUD_FILE_NAPI_H