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