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 "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)25 Action 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)33 bool SyncStateManager::CheckAndSetPending(bool forceFlag, SyncTriggerType triggerType)
34 {
35     std::unique_lock<std::shared_mutex> lck(syncMutex_);
36     if (!CheckCleaningFlag() &&
37         !CheckDisableCloudFlag() &&
38         state_ != SyncState::SYNCING) {
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 
CheckCleaningFlag()58 bool SyncStateManager::CheckCleaningFlag()
59 {
60     return syncSignal.test(static_cast<uint32_t>(SignalPos::CLEANING));
61 }
62 
SetCleaningFlag()63 void SyncStateManager::SetCleaningFlag()
64 {
65     std::unique_lock<std::shared_mutex> lck(syncMutex_);
66     syncSignal.set(static_cast<uint32_t>(SignalPos::CLEANING));
67     nextAction_ = Action::STOP;
68 }
69 
ClearCleaningFlag()70 Action SyncStateManager::ClearCleaningFlag()
71 {
72     std::unique_lock<std::shared_mutex> lck(syncMutex_);
73     syncSignal.reset(static_cast<uint32_t>(SignalPos::CLEANING));
74     return nextAction_;
75 }
76 
CheckDisableCloudFlag()77 bool SyncStateManager::CheckDisableCloudFlag()
78 {
79     return syncSignal.test(static_cast<uint32_t>(SignalPos::DISABLE_CLOUD));
80 }
81 
SetDisableCloudFlag()82 void SyncStateManager::SetDisableCloudFlag()
83 {
84     std::unique_lock<std::shared_mutex> lck(syncMutex_);
85     syncSignal.set(static_cast<uint32_t>(SignalPos::DISABLE_CLOUD));
86     nextAction_ = Action::STOP;
87 }
88 
ClearDisableCloudFlag()89 Action SyncStateManager::ClearDisableCloudFlag()
90 {
91     std::unique_lock<std::shared_mutex> lck(syncMutex_);
92     syncSignal.reset(static_cast<uint32_t>(SignalPos::DISABLE_CLOUD));
93     return nextAction_;
94 }
95 
GetStopSyncFlag()96 bool SyncStateManager::GetStopSyncFlag()
97 {
98     return stopSyncFlag_;
99 }
100 
SetStopSyncFlag()101 void SyncStateManager::SetStopSyncFlag()
102 {
103     std::unique_lock<std::shared_mutex> lck(syncMutex_);
104     nextAction_ = Action::STOP;
105     stopSyncFlag_ = true;
106 }
107 
GetSyncState() const108 SyncState SyncStateManager::GetSyncState() const
109 {
110     return state_;
111 }
112 
GetForceFlag() const113 bool SyncStateManager::GetForceFlag() const
114 {
115     return isForceSync_;
116 }
117 } // namespace OHOS::FileManagement::CloudSync