• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include "cloud_download_callback_client.h"
17 #include "cloud_sync_manager_impl.h"
18 #include "cloud_sync_callback_client.h"
19 #include "cloud_sync_service_proxy.h"
20 #include "dfs_error.h"
21 #include "system_ability_definition.h"
22 #include "iservice_registry.h"
23 #include "utils_log.h"
24 
25 namespace OHOS::FileManagement::CloudSync {
26 using namespace std;
27 constexpr int32_t MIN_USER_ID = 100;
GetInstance()28 CloudSyncManagerImpl &CloudSyncManagerImpl::GetInstance()
29 {
30     static CloudSyncManagerImpl instance;
31     return instance;
32 }
33 
RegisterCallback(const std::shared_ptr<CloudSyncCallback> callback)34 int32_t CloudSyncManagerImpl::RegisterCallback(const std::shared_ptr<CloudSyncCallback> callback)
35 {
36     if (!callback) {
37         LOGE("callback is null");
38         return E_INVAL_ARG;
39     }
40     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
41     if (!CloudSyncServiceProxy) {
42         LOGE("proxy is null");
43         return E_SA_LOAD_FAILED;
44     }
45     auto ret =
46         CloudSyncServiceProxy->RegisterCallbackInner(sptr(new (std::nothrow) CloudSyncCallbackClient(callback)));
47     {
48         unique_lock<mutex> lock(callbackMutex_);
49         callback_ = callback;
50     }
51     SubscribeListener();
52     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
53     LOGI("RegisterCallback ret %{public}d", ret);
54     return ret;
55 }
56 
UnRegisterCallback()57 int32_t CloudSyncManagerImpl::UnRegisterCallback()
58 {
59     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
60     if (!CloudSyncServiceProxy) {
61         LOGE("proxy is null");
62         return E_SA_LOAD_FAILED;
63     }
64 
65     auto ret = CloudSyncServiceProxy->UnRegisterCallbackInner();
66     if (!ret) {
67         {
68             unique_lock<mutex> lock(callbackMutex_);
69             callback_ = nullptr;
70         }
71         SubscribeListener();
72     }
73     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
74     LOGI("UnRegisterCallback ret %{public}d", ret);
75     return ret;
76 }
77 
StartSync()78 int32_t CloudSyncManagerImpl::StartSync()
79 {
80     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
81     if (!CloudSyncServiceProxy) {
82         LOGE("proxy is null");
83         return E_SA_LOAD_FAILED;
84     }
85     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
86     return CloudSyncServiceProxy->StartSyncInner(false);
87 }
88 
GetSyncTime(int64_t & syncTime)89 int32_t CloudSyncManagerImpl::GetSyncTime(int64_t &syncTime)
90 {
91     LOGI("GetSyncTime Start");
92     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
93     if (!CloudSyncServiceProxy) {
94         LOGE("proxy is null");
95         return E_SA_LOAD_FAILED;
96     }
97     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
98     return CloudSyncServiceProxy->GetSyncTimeInner(syncTime);
99 }
100 
StartSync(bool forceFlag,const std::shared_ptr<CloudSyncCallback> callback)101 int32_t CloudSyncManagerImpl::StartSync(bool forceFlag, const std::shared_ptr<CloudSyncCallback> callback)
102 {
103     if (!callback) {
104         LOGE("callback is null");
105         return E_INVAL_ARG;
106     }
107     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
108     if (!CloudSyncServiceProxy) {
109         LOGE("proxy is null");
110         return E_SA_LOAD_FAILED;
111     }
112 
113     if (!isFirstCall_.test()) {
114         LOGI("Register callback");
115         auto ret =
116             CloudSyncServiceProxy->RegisterCallbackInner(sptr(new (std::nothrow) CloudSyncCallbackClient(callback)));
117         if (ret) {
118             LOGE("Register callback failed");
119             isFirstCall_.clear();
120             return ret;
121         }
122         callback_ = callback;
123         SubscribeListener();
124         SetDeathRecipient(CloudSyncServiceProxy->AsObject());
125     }
126 
127     return CloudSyncServiceProxy->StartSyncInner(forceFlag);
128 }
129 
TriggerSync(const std::string & bundleName,const int32_t & userId)130 int32_t CloudSyncManagerImpl::TriggerSync(const std::string &bundleName, const int32_t &userId)
131 {
132     if (bundleName.empty() || userId < MIN_USER_ID) {
133         LOGE("Trigger Sync parameter is invalid");
134         return E_INVAL_ARG;
135     }
136     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
137     if (!CloudSyncServiceProxy) {
138         LOGE("proxy is null");
139         return E_SA_LOAD_FAILED;
140     }
141     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
142     return CloudSyncServiceProxy->TriggerSyncInner(bundleName, userId);
143 }
144 
StopSync()145 int32_t CloudSyncManagerImpl::StopSync()
146 {
147     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
148     if (!CloudSyncServiceProxy) {
149         LOGE("proxy is null");
150         return E_SA_LOAD_FAILED;
151     }
152     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
153     return CloudSyncServiceProxy->StopSyncInner();
154 }
155 
ChangeAppSwitch(const std::string & accoutId,const std::string & bundleName,bool status)156 int32_t CloudSyncManagerImpl::ChangeAppSwitch(const std::string &accoutId, const std::string &bundleName, bool status)
157 {
158     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
159     if (!CloudSyncServiceProxy) {
160         LOGE("proxy is null");
161         return E_SA_LOAD_FAILED;
162     }
163 
164     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
165 
166     int32_t ret = CloudSyncServiceProxy->ChangeAppSwitch(accoutId, bundleName, status);
167     LOGI("ChangeAppSwitch ret %{public}d", ret);
168     return ret;
169 }
170 
NotifyDataChange(const std::string & accoutId,const std::string & bundleName)171 int32_t CloudSyncManagerImpl::NotifyDataChange(const std::string &accoutId, const std::string &bundleName)
172 {
173     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
174     if (!CloudSyncServiceProxy) {
175         LOGE("proxy is null");
176         return E_SA_LOAD_FAILED;
177     }
178 
179     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
180 
181     int32_t ret = CloudSyncServiceProxy->NotifyDataChange(accoutId, bundleName);
182     LOGI("NotifyDataChange ret %{public}d", ret);
183     return ret;
184 }
185 
NotifyEventChange(int32_t userId,const std::string & eventId,const std::string & extraData)186 int32_t CloudSyncManagerImpl::NotifyEventChange(
187     int32_t userId, const std::string &eventId, const std::string &extraData)
188 {
189     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
190     if (!CloudSyncServiceProxy) {
191         LOGE("proxy is null");
192         return E_SA_LOAD_FAILED;
193     }
194 
195     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
196 
197     int32_t ret = CloudSyncServiceProxy->NotifyEventChange(userId, eventId, extraData);
198     LOGI("NotifyDataChange ret %{public}d", ret);
199     return ret;
200 }
201 
StartDownloadFile(const std::string & uri)202 int32_t CloudSyncManagerImpl::StartDownloadFile(const std::string &uri)
203 {
204     LOGI("StartDownloadFile start");
205     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
206     if (!CloudSyncServiceProxy) {
207         LOGE("proxy is null");
208         return E_SA_LOAD_FAILED;
209     }
210     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
211     int32_t ret = CloudSyncServiceProxy->StartDownloadFile(uri);
212     LOGI("StartDownloadFile ret %{public}d", ret);
213     return ret;
214 }
215 
StartFileCache(const std::string & uri)216 int32_t CloudSyncManagerImpl::StartFileCache(const std::string &uri)
217 {
218     LOGI("StartDownloadCache start");
219     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
220     if (!CloudSyncServiceProxy) {
221         LOGE("proxy is null");
222         return E_SA_LOAD_FAILED;
223     }
224     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
225     int32_t ret = CloudSyncServiceProxy->StartFileCache(uri);
226     LOGI("StartDownloadCache ret %{public}d", ret);
227     return ret;
228 }
229 
StopDownloadFile(const std::string & uri)230 int32_t CloudSyncManagerImpl::StopDownloadFile(const std::string &uri)
231 {
232     LOGI("StopDownloadFile start");
233     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
234     if (!CloudSyncServiceProxy) {
235         LOGE("proxy is null");
236         return E_SA_LOAD_FAILED;
237     }
238     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
239     int32_t ret = CloudSyncServiceProxy->StopDownloadFile(uri);
240     LOGI("StopDownloadFile ret %{public}d", ret);
241     return ret;
242 }
243 
RegisterDownloadFileCallback(const std::shared_ptr<CloudDownloadCallback> downloadCallback)244 int32_t CloudSyncManagerImpl::RegisterDownloadFileCallback(
245     const std::shared_ptr<CloudDownloadCallback> downloadCallback)
246 {
247     LOGI("RegisterDownloadFileCallback start");
248     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
249     if (!CloudSyncServiceProxy) {
250         LOGE("proxy is null");
251         return E_SA_LOAD_FAILED;
252     }
253     int32_t ret = CloudSyncServiceProxy->RegisterDownloadFileCallback(
254         sptr(new (std::nothrow) CloudDownloadCallbackClient(downloadCallback)));
255     LOGI("RegisterDownloadFileCallback ret %{public}d", ret);
256     downloadCallback_ = downloadCallback;
257     SubscribeListener();
258     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
259     return ret;
260 }
261 
UnregisterDownloadFileCallback()262 int32_t CloudSyncManagerImpl::UnregisterDownloadFileCallback()
263 {
264     LOGI("UnregisterDownloadFileCallback start");
265     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
266     if (!CloudSyncServiceProxy) {
267         LOGE("proxy is null");
268         return E_SA_LOAD_FAILED;
269     }
270     int32_t ret = CloudSyncServiceProxy->UnregisterDownloadFileCallback();
271     LOGI("UnregisterDownloadFileCallback ret %{public}d", ret);
272     if (ret == E_OK) {
273         downloadCallback_ = nullptr;
274         SubscribeListener();
275     }
276     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
277     return ret;
278 }
SetDeathRecipient(const sptr<IRemoteObject> & remoteObject)279 void CloudSyncManagerImpl::SetDeathRecipient(const sptr<IRemoteObject> &remoteObject)
280 {
281     if (!isFirstCall_.test_and_set()) {
282         auto deathCallback = [this](const wptr<IRemoteObject> &obj) {
283             LOGE("service died.");
284             CloudSyncServiceProxy::InvaildInstance();
285             if (callback_) {
286                 callback_->OnSyncStateChanged(CloudSyncState::COMPLETED, ErrorType::NO_ERROR);
287             }
288             isFirstCall_.clear();
289         };
290         deathRecipient_ = sptr(new SvcDeathRecipient(deathCallback));
291         remoteObject->AddDeathRecipient(deathRecipient_);
292     }
293 }
294 
EnableCloud(const std::string & accoutId,const SwitchDataObj & switchData)295 int32_t CloudSyncManagerImpl::EnableCloud(const std::string &accoutId,
296                                           const SwitchDataObj &switchData)
297 {
298     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
299     if (!CloudSyncServiceProxy) {
300         LOGE("proxy is null");
301         return E_SA_LOAD_FAILED;
302     }
303 
304     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
305 
306     return CloudSyncServiceProxy->EnableCloud(accoutId, switchData);
307 }
308 
DisableCloud(const std::string & accoutId)309 int32_t CloudSyncManagerImpl::DisableCloud(const std::string &accoutId)
310 {
311     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
312     if (!CloudSyncServiceProxy) {
313         LOGE("proxy is null");
314         return E_SA_LOAD_FAILED;
315     }
316 
317     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
318     return CloudSyncServiceProxy->DisableCloud(accoutId);
319 }
320 
Clean(const std::string & accountId,const CleanOptions & cleanOptions)321 int32_t CloudSyncManagerImpl::Clean(const std::string &accountId, const CleanOptions &cleanOptions)
322 {
323     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
324     if (!CloudSyncServiceProxy) {
325         LOGE("proxy is null");
326         return E_SA_LOAD_FAILED;
327     }
328 
329     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
330 
331     return CloudSyncServiceProxy->Clean(accountId, cleanOptions);
332 }
333 
CleanCache(const std::string & uri)334 int32_t CloudSyncManagerImpl::CleanCache(const std::string &uri)
335 {
336     LOGI("CleanCache Start");
337     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
338     if (!CloudSyncServiceProxy) {
339         LOGE("proxy is null");
340         return E_SA_LOAD_FAILED;
341     }
342     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
343     return CloudSyncServiceProxy->CleanCacheInner(uri);
344 }
345 
SubscribeListener()346 void CloudSyncManagerImpl::SubscribeListener()
347 {
348     unique_lock<mutex> lock(subscribeMutex_);
349     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
350     if (samgr == nullptr) {
351         LOGE("Samgr is nullptr");
352         return;
353     }
354     if (listener_ != nullptr) {
355         auto ret = samgr->UnSubscribeSystemAbility(FILEMANAGEMENT_CLOUD_SYNC_SERVICE_SA_ID, listener_);
356         LOGI("unsubscribed to systemAbility ret %{public}d", ret);
357     }
358     if (callback_ != nullptr || downloadCallback_ != nullptr) {
359         listener_ = new SystemAbilityStatusChange(callback_, downloadCallback_);
360         auto ret = samgr->SubscribeSystemAbility(FILEMANAGEMENT_CLOUD_SYNC_SERVICE_SA_ID, listener_);
361         LOGI("subscribed to systemAbility ret %{public}d", ret);
362     } else {
363         listener_ = nullptr;
364     }
365 }
366 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)367 void CloudSyncManagerImpl::SystemAbilityStatusChange::OnAddSystemAbility(int32_t systemAbilityId,
368     const std::string &deviceId)
369 {
370     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
371     if (!CloudSyncServiceProxy) {
372         LOGE("proxy is null");
373         return;
374     }
375     if (downloadCallback_) {
376         CloudSyncServiceProxy->RegisterDownloadFileCallback(
377             sptr(new (std::nothrow) CloudDownloadCallbackClient(downloadCallback_)));
378         CloudSyncManagerImpl::GetInstance().SetDeathRecipient(CloudSyncServiceProxy->AsObject());
379     }
380     if (callback_) {
381         CloudSyncServiceProxy->RegisterCallbackInner(sptr(new (std::nothrow) CloudSyncCallbackClient(callback_)));
382         CloudSyncManagerImpl::GetInstance().SetDeathRecipient(CloudSyncServiceProxy->AsObject());
383     }
384     return;
385 }
386 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)387 void CloudSyncManagerImpl::SystemAbilityStatusChange::OnRemoveSystemAbility(int32_t systemAbilityId,
388     const std::string &deviceId)
389 {
390     return;
391 }
392 } // namespace OHOS::FileManagement::CloudSync
393