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