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_CLOUD_DISK_SERVICE_SYNC_HELPER_H
17 #define OHOS_CLOUD_DISK_SERVICE_SYNC_HELPER_H
18 
19 #include <mutex>
20 
21 #include <timer.h>
22 #include <unordered_map>
23 #include <functional>
24 
25 #include "cloud_sync_manager_lite.h"
26 
27 namespace OHOS {
28 namespace FileManagement {
29 namespace CloudDisk {
30 constexpr int32_t SYNC_INTERVAL = 5000;
31 constexpr int32_t ARGS_SIZE = 2;
32 enum TRIGGER_SYNC_ARGS : int32_t {
33     ARG_USER_ID = 0,
34     ARG_BUNDLE_NAME
35 };
36 class CloudDiskSyncHelper final {
37 public:
38     using TriggerSyncCallback = std::function<void()>;
39     static CloudDiskSyncHelper& GetInstance();
40     CloudDiskSyncHelper(const CloudDiskSyncHelper&) = delete;
41     CloudDiskSyncHelper& operator=(const CloudDiskSyncHelper&) = delete;
42     void RegisterTriggerSync(const std::string &bundleName, const int32_t &userId);
43 
44 private:
45     CloudDiskSyncHelper();
46     ~CloudDiskSyncHelper();
47     void UnregisterRepeatingTriggerSync(const std::string &bundleName, const int32_t &userId);
48     void OnTriggerSyncCallback(const std::string &bundleName, const int32_t &userId);
49 
50     struct TriggerInfo {
51         uint32_t timerId{0};
52         TriggerSyncCallback callback;
53     };
54     std::unique_ptr<Utils::Timer> timer_;
55     std::mutex triggerMapMutex_;
56     std::unordered_map<std::string, std::shared_ptr<TriggerInfo>> triggerInfoMap_;
57 };
58 } // namespace CloudDisk
59 } // namespace FileManagement
60 } // namespace OHOS
61 
62 #endif // OHOS_CLOUD_DISK_SERVICE_SYNC_HELPER_H