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