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
16 #define MLOG_TAG "CustomRestore"
17
18 #include "media_library_custom_restore.h"
19
20 #include "datashare_helper.h"
21 #include "iservice_registry.h"
22 #include "media_log.h"
23 #include "medialibrary_custom_restore_observer_manager.h"
24 #include "medialibrary_errno.h"
25 #include "system_ability_definition.h"
26 #include "userfilemgr_uri.h"
27 #include "user_inner_ipc_client.h"
28 #include "medialibrary_business_code.h"
29 #include "restore_vo.h"
30 #include "stop_restore_vo.h"
31
32 namespace OHOS {
33 namespace Media {
34 using ChangeInfo = DataShare::DataShareObserver::ChangeInfo;
35 const std::string CustomRestore::NOTIFY_URI_PREFIX = "file://media/custom_restore/";
36 const std::string ALBUM_PATH_PREFIX = "/Pictures/";
37
CustomRestore(string keyPath,bool isDeduplication)38 CustomRestore::CustomRestore(string keyPath, bool isDeduplication)
39 {
40 albumLpath_ = ALBUM_PATH_PREFIX + keyPath;
41 keyPath_ = keyPath;
42 isDeduplication_ = isDeduplication;
43 }
44
CustomRestore(string albumLpath,string keyPath,bool isDeduplication)45 CustomRestore::CustomRestore(string albumLpath, string keyPath, bool isDeduplication)
46 {
47 albumLpath_ = albumLpath;
48 keyPath_ = keyPath;
49 isDeduplication_ = isDeduplication;
50 }
51
Init(string bundleName,string appName,string appId,int32_t tokenId)52 void CustomRestore::Init(string bundleName, string appName, string appId, int32_t tokenId)
53 {
54 MEDIA_DEBUG_LOG("CustomRestore init");
55 bundleName_ = bundleName;
56 appName_ = appName;
57 appId_ = appId;
58 tokenId_ = tokenId;
59 InitDataShareHelper();
60 }
61
InitDataShareHelper()62 void CustomRestore::InitDataShareHelper()
63 {
64 auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
65 if (saManager == nullptr) {
66 MEDIA_ERR_LOG("get system ability mgr failed.");
67 return;
68 }
69 auto remoteObj = saManager->GetSystemAbility(STORAGE_MANAGER_MANAGER_ID);
70 if (remoteObj == nullptr) {
71 MEDIA_ERR_LOG("GetSystemAbility Service failed.");
72 return;
73 }
74 if (sDataShareHelper_ == nullptr && remoteObj != nullptr) {
75 sDataShareHelper_ = DataShare::DataShareHelper::Creator(remoteObj, MEDIALIBRARY_DATA_URI);
76 }
77 }
78
Restore()79 int32_t CustomRestore::Restore()
80 {
81 MEDIA_DEBUG_LOG("CustomRestore Restore");
82 if (sDataShareHelper_ == nullptr) {
83 MEDIA_ERR_LOG("sDataShareHelper_ is null.");
84 return E_DATASHARE_IS_NULL;
85 }
86 CHECK_AND_RETURN_RET_LOG(!albumLpath_.empty(), E_INVALID_VALUES, "albumLpath is empty.");
87 CHECK_AND_RETURN_RET_LOG(!keyPath_.empty(), E_INVALID_VALUES, "keyPath is empty.");
88 CHECK_AND_RETURN_RET_LOG(!bundleName_.empty(), E_INVALID_VALUES, "bundleName is empty.");
89 CHECK_AND_RETURN_RET_LOG(!appName_.empty(), E_INVALID_VALUES, "appName is empty.");
90
91 RestoreReqBody reqBody;
92 reqBody.albumLpath = albumLpath_;
93 reqBody.keyPath = keyPath_;
94 reqBody.bundleName = bundleName_;
95 reqBody.appName = appName_;
96 reqBody.appId = appId_;
97 reqBody.isDeduplication = isDeduplication_;
98 int32_t result = IPC::UserInnerIPCClient()
99 .SetDataShareHelper(sDataShareHelper_)
100 .Call(static_cast<uint32_t>(MediaLibraryBusinessCode::INNER_CUSTOM_RESTORE), reqBody);
101 MEDIA_DEBUG_LOG("CustomRestore Restore end. %{public}d", result);
102 return result;
103 }
104
StopRestore()105 int32_t CustomRestore::StopRestore()
106 {
107 MEDIA_DEBUG_LOG("CustomRestore StopRestore");
108 CHECK_AND_RETURN_RET_LOG(sDataShareHelper_ != nullptr, E_DATASHARE_IS_NULL, "sDataShareHelper_ is null.");
109
110 StopRestoreReqBody reqBody;
111 reqBody.keyPath = keyPath_;
112 int32_t result = IPC::UserInnerIPCClient()
113 .SetDataShareHelper(sDataShareHelper_)
114 .Call(static_cast<uint32_t>(MediaLibraryBusinessCode::INNER_CUSTOM_RESTORE_CANCEL), reqBody);
115 MEDIA_DEBUG_LOG("CustomRestore StopRestore end. %{public}d", result);
116 return result;
117 }
118
RegisterCustomRestoreCallback(std::shared_ptr<CustomRestoreCallback> callback)119 int32_t CustomRestore::RegisterCustomRestoreCallback(std::shared_ptr<CustomRestoreCallback> callback)
120 {
121 MEDIA_DEBUG_LOG("CustomRestore RegisterCallback");
122 if (callback == nullptr) {
123 MEDIA_ERR_LOG("CustomRestore RegisterCallback callback is null.");
124 return E_CALLBACK_IS_NULL;
125 }
126 if (sDataShareHelper_ == nullptr) {
127 MEDIA_ERR_LOG("CustomRestore RegisterCallback sDataShareHelper_ is null.");
128 return E_DATASHARE_IS_NULL;
129 }
130 std::shared_ptr<CustomRestoreNotifyObserver> notifyObserver =
131 std::make_shared<CustomRestoreNotifyObserver>(callback);
132 CHECK_AND_RETURN_RET_LOG(notifyObserver != nullptr, E_OBSERVER_IS_NULL,
133 "CustomRestore RegisterCallback notifyObserver is null.");
134
135 Uri customRestoreUri(NOTIFY_URI_PREFIX + keyPath_);
136 sDataShareHelper_->RegisterObserverExt(customRestoreUri, notifyObserver, true);
137 CustomRestoreObserverManager::GetInstance().AttachObserver(callback, notifyObserver);
138 return E_OK;
139 }
140
UnregisterCustomRestoreCallback(std::shared_ptr<CustomRestoreCallback> callback)141 int32_t CustomRestore::UnregisterCustomRestoreCallback(std::shared_ptr<CustomRestoreCallback> callback)
142 {
143 MEDIA_DEBUG_LOG("CustomRestore UnRegisterCallback");
144 if (callback == nullptr) {
145 MEDIA_ERR_LOG("CustomRestore UnRegisterCallback callback is null.");
146 return E_CALLBACK_IS_NULL;
147 }
148
149 CHECK_AND_RETURN_RET_LOG(sDataShareHelper_ != nullptr, E_DATASHARE_IS_NULL,
150 "CustomRestore UnregisterCallback sDataShareHelper_ is null.");
151 std::shared_ptr<CustomRestoreNotifyObserver> notifyObserver =
152 CustomRestoreObserverManager::GetInstance().QueryObserver(callback);
153 CHECK_AND_RETURN_RET_LOG(notifyObserver != nullptr, E_OBSERVER_IS_NULL,
154 "CustomRestore UnRegisterCallback notifyObserver is null.");
155
156 Uri customRestoreUri(NOTIFY_URI_PREFIX + keyPath_);
157 sDataShareHelper_->UnregisterObserverExt(customRestoreUri, notifyObserver);
158 CustomRestoreObserverManager::GetInstance().DetachObserver(callback);
159 return E_OK;
160 }
161
162 } // namespace Media
163 } // namespace OHOS