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 22 namespace OHOS::FileManagement::CloudSync { 23 enum class SyncState : int32_t { 24 INIT = 0, 25 SYNCING, 26 SYNC_FAILED, 27 SYNC_SUCCEED, 28 }; 29 30 enum class Action : int32_t { 31 STOP = 0, 32 START, 33 FORCE_START, 34 }; 35 36 class SyncStateManager { 37 public: 38 Action UpdateSyncState(SyncState newState); 39 bool CheckAndSetPending(bool forceFlag); 40 bool GetStopSyncFlag(); 41 void SetStopSyncFlag(); 42 SyncState GetSyncState() const; 43 bool GetForceFlag() const; 44 45 private: 46 mutable std::shared_mutex syncMutex_; 47 SyncState state_{SyncState::INIT}; 48 Action nextAction_{Action::STOP}; 49 std::atomic_bool isForceSync_{false}; 50 std::atomic_bool stopSyncFlag_{false}; 51 }; 52 53 } // namespace OHOS::FileManagement::CloudSync 54 55 #endif // OHOS_FILEMGMT_SYNC_STATE_MANAGER_H