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_SYNC_STATE_MANAGER_H
17 #define OHOS_FILEMGMT_SYNC_STATE_MANAGER_H
18 
19 #include <atomic>
20 #include <shared_mutex>
21 #include <bitset>
22 #include "data_sync_const.h"
23 namespace OHOS::FileManagement::CloudSync {
24 enum class SyncState : int32_t {
25     INIT = 0,
26     SYNCING,
27     SYNC_FAILED,
28     SYNC_SUCCEED,
29 };
30 
31 enum class SignalPos: int32_t {
32     CLEANING = 0,
33     DISABLE_CLOUD,
34     END,
35 };
36 
37 enum class Action : int32_t {
38     STOP = 0,
39     START,
40     FORCE_START,
41     CHECK,
42 };
43 
44 class SyncStateManager {
45 public:
46     Action UpdateSyncState(SyncState newState);
47     bool CheckAndSetPending(bool forceFlag, SyncTriggerType triggerType);
48     void SetCleaningFlag();
49     Action ClearCleaningFlag();
50     bool CheckCleaningFlag();
51     bool GetStopSyncFlag();
52     void SetStopSyncFlag();
53     void SetDisableCloudFlag();
54     Action ClearDisableCloudFlag();
55     bool CheckDisableCloudFlag();
56     SyncState GetSyncState() const;
57     bool GetForceFlag() const;
58 
59 private:
60     mutable std::shared_mutex syncMutex_;
61     SyncState state_{SyncState::INIT};
62     Action nextAction_{Action::STOP};
63     //pos0: is_cleaning, pos1: is_disable_cloud
64     std::bitset<static_cast<unsigned long>(SignalPos::END)> syncSignal;
65     std::atomic_bool isForceSync_{false};
66     std::atomic_bool stopSyncFlag_{false};
67 };
68 
69 } // namespace OHOS::FileManagement::CloudSync
70 
71 #endif // OHOS_FILEMGMT_SYNC_STATE_MANAGER_H
72