• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 #define MLOG_TAG "CustomRestoreObserverManager"
16 
17 #include "medialibrary_custom_restore_observer_manager.h"
18 
19 #include "dataobs_mgr_client.h"
20 #include "media_log.h"
21 
22 namespace OHOS::Media {
OnChange(const ChangeInfo & changeInfo)23 void CustomRestoreNotifyObserver::OnChange(const ChangeInfo &changeInfo)
24 {
25     MEDIA_DEBUG_LOG("CustomRestoreCallback OnChange");
26     CHECK_AND_RETURN_LOG(customRestoreCallback_ != nullptr, "CustomRestoreCallback is nullptr");
27     CHECK_AND_RETURN_LOG(!changeInfo.valueBuckets_.empty(), "changeInfo.valueBuckets_ is empty");
28 
29     ChangeInfo::VBucket vBucket = changeInfo.valueBuckets_[0];
30     CHECK_AND_RETURN_LOG(!vBucket.empty(), "vBucket is empty");
31 
32     RestoreResult restoreResult;
33     restoreResult.stage = std::get<std::string>(vBucket["stage"]);
34     restoreResult.errCode = static_cast<int32_t>(std::get<int64_t>(vBucket["errCode"]));
35     restoreResult.progress = static_cast<int32_t>(std::get<int64_t>(vBucket["progress"]));
36     restoreResult.uriType = static_cast<int32_t>(std::get<int64_t>(vBucket["uriType"]));
37     restoreResult.uri = std::get<std::string>(vBucket["uri"]);
38     RestoreInfo restoreInfo;
39     restoreInfo.totalNum = static_cast<int32_t>(std::get<int64_t>(vBucket["totalNum"]));
40     restoreInfo.successNum = static_cast<int32_t>(std::get<int64_t>(vBucket["successNum"]));
41     restoreInfo.failedNum = static_cast<int32_t>(std::get<int64_t>(vBucket["failedNum"]));
42     restoreInfo.sameNum = static_cast<int32_t>(std::get<int64_t>(vBucket["sameNum"]));
43     restoreInfo.cancelNum = static_cast<int32_t>(std::get<int64_t>(vBucket["cancelNum"]));
44     restoreResult.restoreInfo = restoreInfo;
45     MEDIA_DEBUG_LOG("CustomRestoreCallback OnRestoreResult");
46     customRestoreCallback_->OnRestoreResult(restoreResult);
47 }
48 
GetInstance()49 CustomRestoreObserverManager &CustomRestoreObserverManager::GetInstance()
50 {
51     static CustomRestoreObserverManager instance;
52     return instance;
53 }
54 
QueryObserver(std::shared_ptr<CustomRestoreCallback> callback)55 std::shared_ptr<CustomRestoreNotifyObserver> CustomRestoreObserverManager::QueryObserver(
56     std::shared_ptr<CustomRestoreCallback> callback)
57 {
58     MEDIA_DEBUG_LOG("QueryObserver callback");
59     if (callback == nullptr) {
60         MEDIA_ERR_LOG("QueryObserver callback is nullptr");
61         return nullptr;
62     }
63     std::shared_ptr<CustomRestoreNotifyObserver> result;
64     if (!callbackMap_.Find(callback, result)) {
65         MEDIA_ERR_LOG("QueryObserver not find");
66         return nullptr;
67     }
68     return result;
69 }
70 
AttachObserver(std::shared_ptr<CustomRestoreCallback> callback,std::shared_ptr<CustomRestoreNotifyObserver> notifyObserver)71 bool CustomRestoreObserverManager::AttachObserver(std::shared_ptr<CustomRestoreCallback> callback,
72     std::shared_ptr<CustomRestoreNotifyObserver> notifyObserver)
73 {
74     MEDIA_DEBUG_LOG("AttachObserver callback");
75     if (callback == nullptr) {
76         MEDIA_ERR_LOG("AttachObserver callback is nullptr");
77         return false;
78     }
79     if (notifyObserver == nullptr) {
80         MEDIA_ERR_LOG("AttachObserver notifyObserver is nullptr");
81         return false;
82     }
83     callbackMap_.Insert(callback, notifyObserver);
84     return true;
85 }
86 
DetachObserver(std::shared_ptr<CustomRestoreCallback> callback)87 bool CustomRestoreObserverManager::DetachObserver(std::shared_ptr<CustomRestoreCallback> callback)
88 {
89     MEDIA_DEBUG_LOG("DetachObserver callback");
90     if (callback == nullptr) {
91         MEDIA_ERR_LOG("DetachObserver callback is nullptr");
92         return false;
93     }
94     callbackMap_.Erase(callback);
95     return true;
96 }
97 
98 } // namespace OHOS::Media
99