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