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 #include "data_sync/sync_state_manager.h" 17 18 #include <type_traits> 19 20 #include "utils_log.h" 21 22 namespace OHOS::FileManagement::CloudSync { 23 using namespace std; 24 UpdateSyncState(SyncState newState)25Action SyncStateManager::UpdateSyncState(SyncState newState) 26 { 27 std::unique_lock<std::shared_mutex> lck(syncMutex_); 28 state_ = newState; 29 stopSyncFlag_ = false; 30 return nextAction_; 31 } 32 CheckAndSetPending(bool forceFlag)33bool SyncStateManager::CheckAndSetPending(bool forceFlag) 34 { 35 std::unique_lock<std::shared_mutex> lck(syncMutex_); 36 if (state_ != SyncState::SYNCING) { 37 state_ = SyncState::SYNCING; 38 nextAction_ = Action::STOP; 39 isForceSync_ = forceFlag; 40 return false; 41 } 42 43 if (forceFlag) { 44 nextAction_ = Action::FORCE_START; 45 } else { 46 nextAction_ = Action::START; 47 } 48 return true; 49 } 50 GetStopSyncFlag()51bool SyncStateManager::GetStopSyncFlag() 52 { 53 return stopSyncFlag_; 54 } 55 SetStopSyncFlag()56void SyncStateManager::SetStopSyncFlag() 57 { 58 std::unique_lock<std::shared_mutex> lck(syncMutex_); 59 nextAction_ = Action::STOP; 60 stopSyncFlag_ = true; 61 } 62 GetSyncState() const63SyncState SyncStateManager::GetSyncState() const 64 { 65 return state_; 66 } 67 GetForceFlag() const68bool SyncStateManager::GetForceFlag() const 69 { 70 return isForceSync_; 71 } 72 } // namespace OHOS::FileManagement::CloudSync