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