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_CACHE_NAPI_H
17 #define OHOS_FILEMGMT_CLOUD_FILE_CACHE_NAPI_H
18 
19 #include <mutex>
20 #include <unordered_set>
21 #include "cloud_file_napi.h"
22 
23 namespace OHOS::FileManagement::CloudSync {
24 const std::string PROGRESS = "progress";
25 const std::string MULTI_PROGRESS = "multiProgress";
26 class CloudFileCacheNapi final : public LibN::NExporter {
27 public:
CloudFileCacheNapi(napi_env env,napi_value exports)28     CloudFileCacheNapi(napi_env env, napi_value exports) : NExporter(env, exports) {}
29     ~CloudFileCacheNapi() = default;
30 
31     bool Export() override;
32     bool ToExport(std::vector<napi_property_descriptor> props);
33     std::string GetClassName() override;
34     static napi_value Constructor(napi_env env, napi_callback_info info);
35     static napi_value StartFileCache(napi_env env, napi_callback_info info);
36     static napi_value StartBatchFileCache(napi_env env, napi_callback_info info);
37     static napi_value StopFileCache(napi_env env, napi_callback_info info);
38     static napi_value StopBatchFileCache(napi_env env, napi_callback_info info);
39     static napi_value On(napi_env env, napi_callback_info info);
40     static napi_value Off(napi_env env, napi_callback_info info);
41     static napi_value CleanCloudFileCache(napi_env env, napi_callback_info info);
42 
43 private:
44     inline static std::string className_ = "CloudFileCache";
45 };
46 struct RegisterInfoArg {
47     std::string eventType;
48     std::shared_ptr<CloudDownloadCallbackImpl> callback;
~RegisterInfoArgRegisterInfoArg49     ~RegisterInfoArg()
50     {
51         if (callback != nullptr) {
52             callback->DeleteReference();
53             callback = nullptr;
54         }
55     }
56 };
57 
58 class RegisterManager {
59 public:
60     RegisterManager() = default;
61     ~RegisterManager() = default;
62     bool AddRegisterInfo(std::shared_ptr<RegisterInfoArg> info);
63     bool RemoveRegisterInfo(const std::string &eventType);
64     bool HasEvent(const std::string &eventType);
65 
66 private:
67     std::mutex registerMutex_;
68     std::unordered_set<std::shared_ptr<RegisterInfoArg>> registerInfo_;
69 };
70 
71 struct FileCacheEntity {
72     RegisterManager registerMgr;
73 };
74 } // namespace OHOS::FileManagement::CloudSync
75 #endif // OHOS_FILEMGMT_CLOUD_FILE_DOWNLOAD_NAPI_H