1 /*
2 * Copyright (c) 2024 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 #include "cloud_sync_service_proxy.h"
16
17 #include <sstream>
18
19 #include "cloud_file_sync_service_interface_code.h"
20 #include "dfs_error.h"
21 #include "iservice_registry.h"
22 #include "system_ability_definition.h"
23 #include "utils_log.h"
24
25 namespace OHOS::FileManagement::CloudSync {
26
27 constexpr int LOAD_SA_TIMEOUT_MS = 4000;
28
TriggerSyncInner(const std::string & bundleName,const int32_t & userId)29 int32_t CloudSyncServiceProxy::TriggerSyncInner(const std::string &bundleName, const int32_t &userId)
30 {
31 LOGI("Trigger Sync");
32 MessageParcel data;
33 MessageParcel reply;
34 MessageOption option;
35
36 if (!data.WriteInterfaceToken(GetDescriptor())) {
37 LOGE("Failed to write interface token");
38 return E_BROKEN_IPC;
39 }
40
41 if (!data.WriteString(bundleName)) {
42 LOGE("Failed to send the bundle name");
43 return E_INVAL_ARG;
44 }
45
46 if (!data.WriteInt32(userId)) {
47 LOGE("Failed to send the user id");
48 return E_INVAL_ARG;
49 }
50
51 auto remote = Remote();
52 if (!remote) {
53 LOGE("remote is nullptr");
54 return E_BROKEN_IPC;
55 }
56
57 int32_t ret = remote->SendRequest(
58 static_cast<uint32_t> (CloudFileSyncServiceInterfaceCode::SERVICE_CMD_TRIGGER_SYNC),
59 data, reply, option);
60 if (ret != E_OK) {
61 LOGE("Failed to send out the request, errno: %{public}d", ret);
62 return E_BROKEN_IPC;
63 }
64 LOGI("TriggerSyncInner Success");
65 return reply.ReadInt32();
66 }
67
GetInstance()68 sptr<ICloudSyncService> CloudSyncServiceProxy::GetInstance()
69 {
70 LOGI("GetInstance");
71 std::unique_lock<std::mutex> lock(instanceMutex_);
72 if (serviceProxy_ != nullptr) {
73 return serviceProxy_;
74 }
75
76 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
77 if (samgr == nullptr) {
78 LOGE("Samgr is nullptr");
79 return nullptr;
80 }
81 sptr<ServiceProxyLoadCallback> cloudSyncLoadCallback = new ServiceProxyLoadCallback();
82 if (cloudSyncLoadCallback == nullptr) {
83 LOGE("cloudSyncLoadCallback is nullptr");
84 return nullptr;
85 }
86 int32_t ret = samgr->LoadSystemAbility(FILEMANAGEMENT_CLOUD_SYNC_SERVICE_SA_ID, cloudSyncLoadCallback);
87 if (ret != E_OK) {
88 LOGE("Failed to load System Ability, systemAbilityId: %{public}d, ret code: %{public}d",
89 FILEMANAGEMENT_CLOUD_SYNC_SERVICE_SA_ID, ret);
90 return nullptr;
91 }
92 std::unique_lock<std::mutex> proxyLock(proxyMutex_);
93 auto waitStatus = cloudSyncLoadCallback->proxyConVar_.wait_for(
94 proxyLock, std::chrono::milliseconds(LOAD_SA_TIMEOUT_MS),
95 [cloudSyncLoadCallback]() { return cloudSyncLoadCallback->isLoadSuccess_.load(); });
96 if (!waitStatus) {
97 LOGE("Load CloudSync SA timeout");
98 return nullptr;
99 }
100 return serviceProxy_;
101 }
102
InvaildInstance()103 void CloudSyncServiceProxy::InvaildInstance()
104 {
105 LOGI("Invalid Instance");
106 std::unique_lock<std::mutex> lock(instanceMutex_);
107 serviceProxy_ = nullptr;
108 }
109
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)110 void CloudSyncServiceProxy::ServiceProxyLoadCallback::OnLoadSystemAbilitySuccess(
111 int32_t systemAbilityId,
112 const sptr<IRemoteObject> &remoteObject)
113 {
114 LOGI("Load CloudSync SA success, systemAbilityId: %{public}d, remote Obj result: %{private}s",
115 systemAbilityId, (remoteObject == nullptr ? "false" : "true"));
116 std::unique_lock<std::mutex> lock(proxyMutex_);
117 if (serviceProxy_ != nullptr) {
118 LOGE("CloudSync SA proxy has been loaded");
119 } else {
120 serviceProxy_ = iface_cast<ICloudSyncService>(remoteObject);
121 }
122 isLoadSuccess_.store(true);
123 proxyConVar_.notify_one();
124 }
125
OnLoadSystemAbilityFail(int32_t systemAbilityId)126 void CloudSyncServiceProxy::ServiceProxyLoadCallback::OnLoadSystemAbilityFail(
127 int32_t systemAbilityId)
128 {
129 LOGI("Load CloudSync SA failed, systemAbilityId: %{public}d", systemAbilityId);
130 std::unique_lock<std::mutex> lock(proxyMutex_);
131 serviceProxy_ = nullptr;
132 isLoadSuccess_.store(false);
133 proxyConVar_.notify_one();
134 }
135
UnRegisterCallbackInner(const std::string & bundleName)136 int32_t CloudSyncServiceProxy::UnRegisterCallbackInner(const std::string &bundleName)
137 {
138 return E_OK;
139 }
140
RegisterCallbackInner(const sptr<IRemoteObject> & remoteObject,const std::string & bundleName)141 int32_t CloudSyncServiceProxy::RegisterCallbackInner(const sptr<IRemoteObject> &remoteObject,
142 const std::string &bundleName)
143 {
144 return E_OK;
145 }
146
StartSyncInner(bool forceFlag,const std::string & bundleName)147 int32_t CloudSyncServiceProxy::StartSyncInner(bool forceFlag, const std::string &bundleName)
148 {
149 return E_OK;
150 }
151
GetSyncTimeInner(int64_t & syncTime,const std::string & bundleName)152 int32_t CloudSyncServiceProxy::GetSyncTimeInner(int64_t &syncTime, const std::string &bundleName)
153 {
154 return E_OK;
155 }
156
CleanCacheInner(const std::string & uri)157 int32_t CloudSyncServiceProxy::CleanCacheInner(const std::string &uri)
158 {
159 return E_OK;
160 }
161
BatchDentryFileInsert(const std::vector<DentryFileInfoObj> & fileInfo,std::vector<std::string> & failCloudId)162 int32_t CloudSyncServiceProxy::BatchDentryFileInsert(const std::vector<DentryFileInfoObj> &fileInfo,
163 std::vector<std::string> &failCloudId)
164 {
165 return E_OK;
166 }
167
StopSyncInner(const std::string & bundleName,bool forceFlag)168 int32_t CloudSyncServiceProxy::StopSyncInner(const std::string &bundleName, bool forceFlag)
169 {
170 return E_OK;
171 }
172
ResetCursor(const std::string & bundleName)173 int32_t CloudSyncServiceProxy::ResetCursor(const std::string &bundleName)
174 {
175 return E_OK;
176 }
177
ChangeAppSwitch(const std::string & accoutId,const std::string & bundleName,bool status)178 int32_t CloudSyncServiceProxy::ChangeAppSwitch(const std::string &accoutId,
179 const std::string &bundleName,
180 bool status)
181 {
182 return E_OK;
183 }
184
OptimizeStorage(const int32_t agingDays)185 int32_t CloudSyncServiceProxy::OptimizeStorage(const int32_t agingDays)
186 {
187 return E_OK;
188 }
189
Clean(const std::string & accountId,const CleanOptions & cleanOptions)190 int32_t CloudSyncServiceProxy::Clean(const std::string &accountId, const CleanOptions &cleanOptions)
191 {
192 return E_OK;
193 }
194
EnableCloud(const std::string & accoutId,const SwitchDataObj & switchData)195 int32_t CloudSyncServiceProxy::EnableCloud(const std::string &accoutId, const SwitchDataObj &switchData)
196 {
197 return E_OK;
198 }
199
DisableCloud(const std::string & accoutId)200 int32_t CloudSyncServiceProxy::DisableCloud(const std::string &accoutId)
201 {
202 return E_OK;
203 }
204
NotifyDataChange(const std::string & accoutId,const std::string & bundleName)205 int32_t CloudSyncServiceProxy::NotifyDataChange(const std::string &accoutId, const std::string &bundleName)
206 {
207 return E_OK;
208 }
209
NotifyEventChange(int32_t userId,const std::string & eventId,const std::string & extraData)210 int32_t CloudSyncServiceProxy::NotifyEventChange(
211 int32_t userId, const std::string &eventId, const std::string &extraData)
212 {
213 return E_OK;
214 }
215
StartDownloadFile(const std::string & uri)216 int32_t CloudSyncServiceProxy::StartDownloadFile(const std::string &uri)
217 {
218 return E_OK;
219 }
220
StartFileCacheWriteParcel(MessageParcel & data,const std::vector<std::string> & pathVec,bool & isCallbackValid,const sptr<IRemoteObject> & downloadCallback)221 int32_t CloudSyncServiceProxy::StartFileCacheWriteParcel(MessageParcel &data,
222 const std::vector<std::string> &pathVec,
223 bool &isCallbackValid,
224 const sptr<IRemoteObject> &downloadCallback)
225 {
226 return E_OK;
227 }
228
StartFileCache(const std::vector<std::string> & uriVec,int64_t & downloadId,bool isCallbackValid,const sptr<IRemoteObject> & downloadCallback)229 int32_t CloudSyncServiceProxy::StartFileCache(const std::vector<std::string> &uriVec,
230 int64_t &downloadId,
231 bool isCallbackValid,
232 const sptr<IRemoteObject> &downloadCallback)
233 {
234 return E_OK;
235 }
236
StopDownloadFile(const std::string & uri,bool needClean)237 int32_t CloudSyncServiceProxy::StopDownloadFile(const std::string &uri, bool needClean)
238 {
239 return E_OK;
240 }
241
StopFileCache(const int64_t & downloadId,bool needClean)242 int32_t CloudSyncServiceProxy::StopFileCache(const int64_t &downloadId, bool needClean)
243 {
244 return E_OK;
245 }
246
RegisterDownloadFileCallback(const sptr<IRemoteObject> & downloadCallback)247 int32_t CloudSyncServiceProxy::RegisterDownloadFileCallback(const sptr<IRemoteObject> &downloadCallback)
248 {
249 return E_OK;
250 }
251
UnregisterDownloadFileCallback()252 int32_t CloudSyncServiceProxy::UnregisterDownloadFileCallback()
253 {
254 return E_OK;
255 }
256
UploadAsset(const int32_t userId,const std::string & request,std::string & result)257 int32_t CloudSyncServiceProxy::UploadAsset(const int32_t userId, const std::string &request, std::string &result)
258 {
259 return E_OK;
260 }
261
DownloadFile(const int32_t userId,const std::string & bundleName,AssetInfoObj & assetInfoObj)262 int32_t CloudSyncServiceProxy::DownloadFile(const int32_t userId,
263 const std::string &bundleName,
264 AssetInfoObj &assetInfoObj)
265 {
266 return E_OK;
267 }
268
DownloadFiles(const int32_t userId,const std::string & bundleName,const std::vector<AssetInfoObj> & assetInfoObj,std::vector<bool> & assetResultMap)269 int32_t CloudSyncServiceProxy::DownloadFiles(const int32_t userId,
270 const std::string &bundleName,
271 const std::vector<AssetInfoObj> &assetInfoObj,
272 std::vector<bool> &assetResultMap)
273 {
274 return E_OK;
275 }
276
DownloadAsset(const uint64_t taskId,const int32_t userId,const std::string & bundleName,const std::string & networkId,AssetInfoObj & assetInfoObj)277 int32_t CloudSyncServiceProxy::DownloadAsset(const uint64_t taskId,
278 const int32_t userId,
279 const std::string &bundleName,
280 const std::string &networkId,
281 AssetInfoObj &assetInfoObj)
282 {
283 return E_OK;
284 }
285
DownloadThumb()286 int32_t CloudSyncServiceProxy::DownloadThumb()
287 {
288 return E_OK;
289 }
290
RegisterDownloadAssetCallback(const sptr<IRemoteObject> & remoteObject)291 int32_t CloudSyncServiceProxy::RegisterDownloadAssetCallback(const sptr<IRemoteObject> &remoteObject)
292 {
293 return E_OK;
294 }
295
DeleteAsset(const int32_t userId,const std::string & uri)296 int32_t CloudSyncServiceProxy::DeleteAsset(const int32_t userId, const std::string &uri)
297 {
298 return E_OK;
299 }
300 } // namespace OHOS::FileManagement::CloudSync
301