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 
RegisterCallbackInner(const sptr<IRemoteObject> & remoteObject,const std::string & bundleName)142 int32_t CloudSyncServiceProxy::RegisterCallbackInner(const sptr<IRemoteObject> &remoteObject,
143                                                      const std::string &bundleName)
144 {
145     return E_OK;
146 }
147 
StartSyncInner(bool forceFlag,const std::string & bundleName)148 int32_t CloudSyncServiceProxy::StartSyncInner(bool forceFlag, const std::string &bundleName)
149 {
150     return E_OK;
151 }
152 
GetSyncTimeInner(int64_t & syncTime,const std::string & bundleName)153 int32_t CloudSyncServiceProxy::GetSyncTimeInner(int64_t &syncTime, const std::string &bundleName)
154 {
155     return E_OK;
156 }
157 
CleanCacheInner(const std::string & uri)158 int32_t CloudSyncServiceProxy::CleanCacheInner(const std::string &uri)
159 {
160     return E_OK;
161 }
162 
StopSyncInner(const std::string & bundleName,bool forceFlag)163 int32_t CloudSyncServiceProxy::StopSyncInner(const std::string &bundleName, bool forceFlag)
164 {
165     return E_OK;
166 }
167 
ResetCursor(const std::string & bundleName)168 int32_t CloudSyncServiceProxy::ResetCursor(const std::string &bundleName)
169 {
170     return E_OK;
171 }
172 
ChangeAppSwitch(const std::string & accoutId,const std::string & bundleName,bool status)173 int32_t CloudSyncServiceProxy::ChangeAppSwitch(const std::string &accoutId,
174                                                const std::string &bundleName,
175                                                bool status)
176 {
177     return E_OK;
178 }
179 
Clean(const std::string & accountId,const CleanOptions & cleanOptions)180 int32_t CloudSyncServiceProxy::Clean(const std::string &accountId, const CleanOptions &cleanOptions)
181 {
182     return E_OK;
183 }
184 
EnableCloud(const std::string & accoutId,const SwitchDataObj & switchData)185 int32_t CloudSyncServiceProxy::EnableCloud(const std::string &accoutId, const SwitchDataObj &switchData)
186 {
187     return E_OK;
188 }
189 
DisableCloud(const std::string & accoutId)190 int32_t CloudSyncServiceProxy::DisableCloud(const std::string &accoutId)
191 {
192     return E_OK;
193 }
194 
NotifyDataChange(const std::string & accoutId,const std::string & bundleName)195 int32_t CloudSyncServiceProxy::NotifyDataChange(const std::string &accoutId, const std::string &bundleName)
196 {
197     return E_OK;
198 }
199 
NotifyEventChange(int32_t userId,const std::string & eventId,const std::string & extraData)200 int32_t CloudSyncServiceProxy::NotifyEventChange(
201     int32_t userId, const std::string &eventId, const std::string &extraData)
202 {
203     return E_OK;
204 }
205 
StartDownloadFile(const std::string & uri)206 int32_t CloudSyncServiceProxy::StartDownloadFile(const std::string &uri)
207 {
208     return E_OK;
209 }
210 
StartFileCacheWriteParcel(MessageParcel & data,const std::vector<std::string> & pathVec,std::bitset<FIELD_KEY_MAX_SIZE> & fieldkey,bool & isCallbackValid,const sptr<IRemoteObject> & downloadCallback)211 int32_t CloudSyncServiceProxy::StartFileCacheWriteParcel(MessageParcel &data,
212                                                          const std::vector<std::string> &pathVec,
213                                                          std::bitset<FIELD_KEY_MAX_SIZE> &fieldkey,
214                                                          bool &isCallbackValid,
215                                                          const sptr<IRemoteObject> &downloadCallback)
216 {
217     return E_OK;
218 }
219 
StartFileCache(const std::vector<std::string> & uriVec,int64_t & downloadId,std::bitset<FIELD_KEY_MAX_SIZE> fieldkey,bool isCallbackValid,const sptr<IRemoteObject> & downloadCallback)220 int32_t CloudSyncServiceProxy::StartFileCache(const std::vector<std::string> &uriVec,
221                                               int64_t &downloadId, std::bitset<FIELD_KEY_MAX_SIZE> fieldkey,
222                                               bool isCallbackValid,
223                                               const sptr<IRemoteObject> &downloadCallback)
224 {
225     return E_OK;
226 }
227 
StopDownloadFile(const std::string & uri,bool needClean)228 int32_t CloudSyncServiceProxy::StopDownloadFile(const std::string &uri, bool needClean)
229 {
230     return E_OK;
231 }
232 
StopFileCache(const int64_t & downloadId,bool needClean)233 int32_t CloudSyncServiceProxy::StopFileCache(const int64_t &downloadId,  bool needClean)
234 {
235     return E_OK;
236 }
237 
RegisterDownloadFileCallback(const sptr<IRemoteObject> & downloadCallback)238 int32_t CloudSyncServiceProxy::RegisterDownloadFileCallback(const sptr<IRemoteObject> &downloadCallback)
239 {
240     return E_OK;
241 }
242 
UnregisterDownloadFileCallback()243 int32_t CloudSyncServiceProxy::UnregisterDownloadFileCallback()
244 {
245     return E_OK;
246 }
247 
UploadAsset(const int32_t userId,const std::string & request,std::string & result)248 int32_t CloudSyncServiceProxy::UploadAsset(const int32_t userId, const std::string &request, std::string &result)
249 {
250     return E_OK;
251 }
252 
DownloadFile(const int32_t userId,const std::string & bundleName,AssetInfoObj & assetInfoObj)253 int32_t CloudSyncServiceProxy::DownloadFile(const int32_t userId,
254                                             const std::string &bundleName,
255                                             AssetInfoObj &assetInfoObj)
256 {
257     return E_OK;
258 }
259 
DownloadFiles(const int32_t userId,const std::string & bundleName,const std::vector<AssetInfoObj> & assetInfoObj,std::vector<bool> & assetResultMap)260 int32_t CloudSyncServiceProxy::DownloadFiles(const int32_t userId,
261                                              const std::string &bundleName,
262                                              const std::vector<AssetInfoObj> &assetInfoObj,
263                                              std::vector<bool> &assetResultMap)
264 {
265     return E_OK;
266 }
267 
DownloadAsset(const uint64_t taskId,const int32_t userId,const std::string & bundleName,const std::string & networkId,AssetInfoObj & assetInfoObj)268 int32_t CloudSyncServiceProxy::DownloadAsset(const uint64_t taskId,
269                                              const int32_t userId,
270                                              const std::string &bundleName,
271                                              const std::string &networkId,
272                                              AssetInfoObj &assetInfoObj)
273 {
274     return E_OK;
275 }
276 
RegisterDownloadAssetCallback(const sptr<IRemoteObject> & remoteObject)277 int32_t CloudSyncServiceProxy::RegisterDownloadAssetCallback(const sptr<IRemoteObject> &remoteObject)
278 {
279     return E_OK;
280 }
281 
DeleteAsset(const int32_t userId,const std::string & uri)282 int32_t CloudSyncServiceProxy::DeleteAsset(const int32_t userId, const std::string &uri)
283 {
284     return E_OK;
285 }
286 } // namespace OHOS::FileManagement::CloudSync
287