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 "utils_log.h"
22
23 namespace OHOS::FileManagement::CloudSync {
24 using namespace std;
GetInstance()25 CloudSyncManagerImpl &CloudSyncManagerImpl::GetInstance()
26 {
27 static CloudSyncManagerImpl instance;
28 return instance;
29 }
30
RegisterCallback(const std::shared_ptr<CloudSyncCallback> callback)31 int32_t CloudSyncManagerImpl::RegisterCallback(const std::shared_ptr<CloudSyncCallback> callback)
32 {
33 if (!callback) {
34 LOGE("callback is null");
35 return E_INVAL_ARG;
36 }
37 auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
38 if (!CloudSyncServiceProxy) {
39 LOGE("proxy is null");
40 return E_SA_LOAD_FAILED;
41 }
42
43 auto ret =
44 CloudSyncServiceProxy->RegisterCallbackInner(sptr(new (std::nothrow) CloudSyncCallbackClient(callback)));
45 {
46 unique_lock<mutex> lock(callbackMutex_);
47 callback_ = callback;
48 }
49 SetDeathRecipient(CloudSyncServiceProxy->AsObject());
50 LOGI("RegisterCallback ret %{public}d", ret);
51 return ret;
52 }
53
UnRegisterCallback()54 int32_t CloudSyncManagerImpl::UnRegisterCallback()
55 {
56 auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
57 if (!CloudSyncServiceProxy) {
58 LOGE("proxy is null");
59 return E_SA_LOAD_FAILED;
60 }
61
62 auto ret = CloudSyncServiceProxy->UnRegisterCallbackInner();
63 if (!ret) {
64 {
65 unique_lock<mutex> lock(callbackMutex_);
66 callback_ = nullptr;
67 }
68 }
69 LOGI("UnRegisterCallback ret %{public}d", ret);
70 return ret;
71 }
72
StartSync()73 int32_t CloudSyncManagerImpl::StartSync()
74 {
75 auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
76 if (!CloudSyncServiceProxy) {
77 LOGE("proxy is null");
78 return E_SA_LOAD_FAILED;
79 }
80 return CloudSyncServiceProxy->StartSyncInner(false);
81 }
82
StartSync(bool forceFlag,const std::shared_ptr<CloudSyncCallback> callback)83 int32_t CloudSyncManagerImpl::StartSync(bool forceFlag, const std::shared_ptr<CloudSyncCallback> callback)
84 {
85 if (!callback) {
86 LOGE("callback is null");
87 return E_INVAL_ARG;
88 }
89 auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
90 if (!CloudSyncServiceProxy) {
91 LOGE("proxy is null");
92 return E_SA_LOAD_FAILED;
93 }
94
95 if (!isFirstCall_.test_and_set()) {
96 LOGI("Register callback");
97 auto ret =
98 CloudSyncServiceProxy->RegisterCallbackInner(sptr(new (std::nothrow) CloudSyncCallbackClient(callback)));
99 if (ret) {
100 LOGE("Register callback failed");
101 isFirstCall_.clear();
102 return ret;
103 }
104 callback_ = callback;
105 SetDeathRecipient(CloudSyncServiceProxy->AsObject());
106 }
107
108 return CloudSyncServiceProxy->StartSyncInner(forceFlag);
109 }
110
StopSync()111 int32_t CloudSyncManagerImpl::StopSync()
112 {
113 auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
114 if (!CloudSyncServiceProxy) {
115 LOGE("proxy is null");
116 return E_SA_LOAD_FAILED;
117 }
118 return CloudSyncServiceProxy->StopSyncInner();
119 }
120
ChangeAppSwitch(const std::string & accoutId,const std::string & bundleName,bool status)121 int32_t CloudSyncManagerImpl::ChangeAppSwitch(const std::string &accoutId, const std::string &bundleName, bool status)
122 {
123 auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
124 if (!CloudSyncServiceProxy) {
125 LOGE("proxy is null");
126 return E_SA_LOAD_FAILED;
127 }
128 int32_t ret = CloudSyncServiceProxy->ChangeAppSwitch(accoutId, bundleName, status);
129 LOGI("ChangeAppSwitch ret %{public}d", ret);
130 return ret;
131 }
132
NotifyDataChange(const std::string & accoutId,const std::string & bundleName)133 int32_t CloudSyncManagerImpl::NotifyDataChange(const std::string &accoutId, const std::string &bundleName)
134 {
135 auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
136 if (!CloudSyncServiceProxy) {
137 LOGE("proxy is null");
138 return E_SA_LOAD_FAILED;
139 }
140 int32_t ret = CloudSyncServiceProxy->NotifyDataChange(accoutId, bundleName);
141 LOGI("NotifyDataChange ret %{public}d", ret);
142 return ret;
143 }
144
StartDownloadFile(const std::string & uri)145 int32_t CloudSyncManagerImpl::StartDownloadFile(const std::string &uri)
146 {
147 LOGI("StartDownloadFile start");
148 auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
149 if (!CloudSyncServiceProxy) {
150 LOGE("proxy is null");
151 return E_SA_LOAD_FAILED;
152 }
153 int32_t ret = CloudSyncServiceProxy->StartDownloadFile(uri);
154 LOGI("StartDownloadFile ret %{public}d", ret);
155 return ret;
156 }
157
StopDownloadFile(const std::string & uri)158 int32_t CloudSyncManagerImpl::StopDownloadFile(const std::string &uri)
159 {
160 LOGI("StopDownloadFile start");
161 auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
162 if (!CloudSyncServiceProxy) {
163 LOGE("proxy is null");
164 return E_SA_LOAD_FAILED;
165 }
166 int32_t ret = CloudSyncServiceProxy->StopDownloadFile(uri);
167 LOGI("StopDownloadFile ret %{public}d", ret);
168 return ret;
169 }
170
RegisterDownloadFileCallback(const std::shared_ptr<CloudDownloadCallback> downloadCallback)171 int32_t CloudSyncManagerImpl::RegisterDownloadFileCallback(
172 const std::shared_ptr<CloudDownloadCallback> downloadCallback)
173 {
174 LOGI("RegisterDownloadFileCallback start");
175 auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
176 if (!CloudSyncServiceProxy) {
177 LOGE("proxy is null");
178 return E_SA_LOAD_FAILED;
179 }
180 int32_t ret = CloudSyncServiceProxy->RegisterDownloadFileCallback(
181 sptr(new (std::nothrow) CloudDownloadCallbackClient(downloadCallback)));
182 LOGI("RegisterDownloadFileCallback ret %{public}d", ret);
183 return ret;
184 }
185
UnregisterDownloadFileCallback()186 int32_t CloudSyncManagerImpl::UnregisterDownloadFileCallback()
187 {
188 LOGI("UnregisterDownloadFileCallback start");
189 auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
190 if (!CloudSyncServiceProxy) {
191 LOGE("proxy is null");
192 return E_SA_LOAD_FAILED;
193 }
194 int32_t ret = CloudSyncServiceProxy->UnregisterDownloadFileCallback();
195 LOGI("UnregisterDownloadFileCallback ret %{public}d", ret);
196 return ret;
197 }
SetDeathRecipient(const sptr<IRemoteObject> & remoteObject)198 void CloudSyncManagerImpl::SetDeathRecipient(const sptr<IRemoteObject> &remoteObject)
199 {
200 auto deathCallback = [this](const wptr<IRemoteObject> &obj) {
201 LOGE("service died.");
202 CloudSyncServiceProxy::InvaildInstance();
203 if (callback_) {
204 callback_->OnSyncStateChanged(CloudSyncState::COMPLETED, ErrorType::NO_ERROR);
205 callback_ = nullptr;
206 }
207 isFirstCall_.clear();
208 };
209 deathRecipient_ = sptr(new SvcDeathRecipient(deathCallback));
210 remoteObject->AddDeathRecipient(deathRecipient_);
211 }
212
EnableCloud(const std::string & accoutId,const SwitchDataObj & switchData)213 int32_t CloudSyncManagerImpl::EnableCloud(const std::string &accoutId,
214 const SwitchDataObj &switchData)
215 {
216 auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
217 if (!CloudSyncServiceProxy) {
218 LOGE("proxy is null");
219 return E_SA_LOAD_FAILED;
220 }
221
222 return CloudSyncServiceProxy->EnableCloud(accoutId, switchData);
223 }
224
DisableCloud(const std::string & accoutId)225 int32_t CloudSyncManagerImpl::DisableCloud(const std::string &accoutId)
226 {
227 auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
228 if (!CloudSyncServiceProxy) {
229 LOGE("proxy is null");
230 return E_SA_LOAD_FAILED;
231 }
232
233 return CloudSyncServiceProxy->DisableCloud(accoutId);
234 }
235
Clean(const std::string & accountId,const CleanOptions & cleanOptions)236 int32_t CloudSyncManagerImpl::Clean(const std::string &accountId, const CleanOptions &cleanOptions)
237 {
238 auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
239 if (!CloudSyncServiceProxy) {
240 LOGE("proxy is null");
241 return E_SA_LOAD_FAILED;
242 }
243 return CloudSyncServiceProxy->Clean(accountId, cleanOptions);
244 }
245 } // namespace OHOS::FileManagement::CloudSync
246