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,SyncTriggerType triggerType)33bool SyncStateManager::CheckAndSetPending(bool forceFlag, SyncTriggerType triggerType) 34 { 35 std::unique_lock<std::shared_mutex> lck(syncMutex_); 36 if (state_ != SyncState::SYNCING && 37 state_ != SyncState::CLEANING && 38 state_ != SyncState::DISABLE_CLOUD) { 39 state_ = SyncState::SYNCING; 40 nextAction_ = Action::STOP; 41 isForceSync_ = forceFlag; 42 return false; 43 } 44 45 if (nextAction_ == Action::CHECK) { 46 return true; 47 } 48 if (forceFlag) { 49 nextAction_ = Action::FORCE_START; 50 } else if (triggerType == SyncTriggerType::TASK_TRIGGER) { 51 nextAction_ = Action::CHECK; 52 } else { 53 nextAction_ = Action::START; 54 } 55 return true; 56 } 57 SetDisableCloudFlag()58void SyncStateManager::SetDisableCloudFlag() 59 { 60 std::unique_lock<std::shared_mutex> lck(syncMutex_); 61 state_ = SyncState::DISABLE_CLOUD; 62 nextAction_ = Action::STOP; 63 } 64 SetCleaningFlag()65void SyncStateManager::SetCleaningFlag() 66 { 67 std::unique_lock<std::shared_mutex> lck(syncMutex_); 68 state_ = SyncState::CLEANING; 69 nextAction_ = Action::STOP; 70 } 71 GetStopSyncFlag()72bool SyncStateManager::GetStopSyncFlag() 73 { 74 return stopSyncFlag_; 75 } 76 SetStopSyncFlag()77void SyncStateManager::SetStopSyncFlag() 78 { 79 std::unique_lock<std::shared_mutex> lck(syncMutex_); 80 nextAction_ = Action::STOP; 81 stopSyncFlag_ = true; 82 } 83 GetSyncState() const84SyncState SyncStateManager::GetSyncState() const 85 { 86 return state_; 87 } 88 GetForceFlag() const89bool SyncStateManager::GetForceFlag() const 90 { 91 return isForceSync_; 92 } 93 } // namespace OHOS::FileManagement::CloudSync