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 "clouddisk_sync_helper.h"
17
18 #include "utils_log.h"
19 #include "dfs_error.h"
20 #include "common_timer_errors.h"
21 namespace OHOS {
22 namespace FileManagement {
23 namespace CloudDisk {
24 using namespace std;
25 constexpr int32_t MIN_USER_ID = 100;
26
GetInstance()27 CloudDiskSyncHelper& CloudDiskSyncHelper::GetInstance()
28 {
29 static CloudDiskSyncHelper instance_;
30 return instance_;
31 }
32
CloudDiskSyncHelper()33 CloudDiskSyncHelper::CloudDiskSyncHelper() : timer_(std::make_unique<Utils::Timer>("CloudDiskTriggerSync"))
34 {
35 timer_->Setup();
36 }
37
~CloudDiskSyncHelper()38 CloudDiskSyncHelper::~CloudDiskSyncHelper()
39 {
40 if (timer_) {
41 timer_->Shutdown(true);
42 timer_ = nullptr;
43 }
44 }
45
RegisterTriggerSync(const std::string & bundleName,const int32_t & userId)46 void CloudDiskSyncHelper::RegisterTriggerSync(const std::string &bundleName, const int32_t &userId)
47 {
48 if (timer_ == nullptr || bundleName.empty() || userId < MIN_USER_ID) {
49 LOGE("TriggerSync parameter is invalid");
50 return;
51 }
52 LOGD("begin trigger sync, bundleName = %{public}s, userId = %{public}d", bundleName.c_str(), userId);
53 UnregisterRepeatingTriggerSync(bundleName, userId);
54 string keyId = to_string(userId) + bundleName;
55 function<void()> callback = [this, bundleName, userId] { OnTriggerSyncCallback(bundleName, userId); };
56 uint32_t timerId = timer_->Register(callback, SYNC_INTERVAL, true);
57 if (timerId == Utils::TIMER_ERR_DEAL_FAILED) {
58 LOGE("Register timer failed");
59 return;
60 }
61 shared_ptr<TriggerInfo> triggerInfoPtr = make_shared<TriggerInfo>();
62 triggerInfoPtr->timerId = timerId;
63 triggerInfoPtr->callback = callback;
64 lock_guard<mutex> lock(triggerMapMutex_);
65 triggerInfoMap_.emplace(keyId, triggerInfoPtr);
66 }
67
UnregisterRepeatingTriggerSync(const std::string & bundleName,const int32_t & userId)68 void CloudDiskSyncHelper::UnregisterRepeatingTriggerSync(const std::string &bundleName, const int32_t &userId)
69 {
70 if (timer_ == nullptr || bundleName.empty() || userId < MIN_USER_ID) {
71 LOGE("UnregisterRepeatingTrigger parameter is invalid");
72 return;
73 }
74 string keyId = to_string(userId) + bundleName;
75 bool isSuccess = false;
76 lock_guard<mutex> lock(triggerMapMutex_);
77 auto iterator = triggerInfoMap_.find(keyId);
78 if (iterator != triggerInfoMap_.end()) {
79 LOGD("bundleName: %{public}s, userId: %{public}d is exist", bundleName.c_str(), userId);
80 auto triggerInfoPtr = iterator->second;
81 timer_->Unregister(triggerInfoPtr->timerId);
82 triggerInfoMap_.erase(keyId);
83 isSuccess = true;
84 }
85 LOGD("Unregister repeating trigger result is %{public}d", isSuccess);
86 }
87
OnTriggerSyncCallback(const std::string & bundleName,const int32_t & userId)88 void CloudDiskSyncHelper::OnTriggerSyncCallback(const std::string &bundleName, const int32_t &userId)
89 {
90 string keyId = to_string(userId) + bundleName;
91 {
92 lock_guard<mutex> lock(triggerMapMutex_);
93 triggerInfoMap_.erase(keyId);
94 }
95 LOGI("cloud sync manager trigger sync, bundleName: %{public}s, userId: %{public}d", bundleName.c_str(), userId);
96 int32_t ret = CloudSync::CloudSyncManagerLite::GetInstance().TriggerSync(bundleName, userId);
97 if (ret != 0) {
98 LOGE("cloud sync manager trigger sync err %{public}d", ret);
99 }
100 }
101 } // namespace CloudDisk
102 } // namespace FileManagement
103 } // namespace OHOS