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_sync_asset_manager_impl.h"
17
18 #include <cinttypes>
19
20 #include "cloud_sync_service_proxy.h"
21 #include "dfs_error.h"
22 #include "download_asset_callback_client.h"
23 #include "utils_log.h"
24
25 namespace OHOS::FileManagement::CloudSync {
GetInstance()26 CloudSyncAssetManagerImpl &CloudSyncAssetManagerImpl::GetInstance()
27 {
28 static CloudSyncAssetManagerImpl instance;
29 return instance;
30 }
31
UploadAsset(const int32_t userId,const std::string & request,std::string & result)32 int32_t CloudSyncAssetManagerImpl::UploadAsset(const int32_t userId,
33 const std::string &request,
34 std::string &result)
35 {
36 auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
37 if (!CloudSyncServiceProxy) {
38 LOGE("proxy is null");
39 return E_SA_LOAD_FAILED;
40 }
41
42 SetDeathRecipient(CloudSyncServiceProxy->AsObject());
43 int32_t ret = CloudSyncServiceProxy->UploadAsset(userId, request, result);
44 LOGD("UploadAsset ret %{public}d", ret);
45 return ret;
46 }
47
DownloadFile(const int32_t userId,const std::string & bundleName,AssetInfo & assetInfo)48 int32_t CloudSyncAssetManagerImpl::DownloadFile(const int32_t userId,
49 const std::string &bundleName,
50 AssetInfo &assetInfo)
51 {
52 auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
53 if (!CloudSyncServiceProxy) {
54 LOGE("proxy is null");
55 return E_SA_LOAD_FAILED;
56 }
57
58 SetDeathRecipient(CloudSyncServiceProxy->AsObject());
59 AssetInfoObj assetInfoObj(assetInfo);
60 int32_t ret = CloudSyncServiceProxy->DownloadFile(userId, bundleName, assetInfoObj);
61 LOGI("DownloadFile ret %{public}d", ret);
62 return ret;
63 }
64
DownloadFiles(const int32_t userId,const std::string & bundleName,const std::vector<AssetInfo> & assetInfo,std::vector<bool> & assetResultMap)65 int32_t CloudSyncAssetManagerImpl::DownloadFiles(const int32_t userId,
66 const std::string &bundleName,
67 const std::vector<AssetInfo> &assetInfo,
68 std::vector<bool> &assetResultMap)
69 {
70 auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
71 if (!CloudSyncServiceProxy) {
72 LOGE("proxy is null");
73 return E_SA_LOAD_FAILED;
74 }
75
76 SetDeathRecipient(CloudSyncServiceProxy->AsObject());
77 std::vector<AssetInfoObj> assetInfoObj;
78 for (const auto &info : assetInfo) {
79 AssetInfoObj obj(info);
80 assetInfoObj.emplace_back(obj);
81 }
82 int32_t ret = CloudSyncServiceProxy->DownloadFiles(userId, bundleName, assetInfoObj, assetResultMap);
83 LOGI("DownloadFile ret %{public}d", ret);
84 return ret;
85 }
86
DownloadFile(const int32_t userId,const std::string & bundleName,const std::string & networkId,AssetInfo & assetInfo,ResultCallback resultCallback)87 int32_t CloudSyncAssetManagerImpl::DownloadFile(const int32_t userId,
88 const std::string &bundleName,
89 const std::string &networkId,
90 AssetInfo &assetInfo,
91 ResultCallback resultCallback)
92 {
93 auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
94 if (!CloudSyncServiceProxy) {
95 LOGE("proxy is null");
96 return E_SA_LOAD_FAILED;
97 }
98
99 {
100 std::lock_guard<std::mutex> lock(callbackInitMutex_);
101 if (downloadAssetCallback_ == nullptr) {
102 downloadAssetCallback_ = sptr(new (std::nothrow) DownloadAssetCallbackClient());
103 }
104 if (downloadAssetCallback_ == nullptr) {
105 LOGE("have no enough memory");
106 return E_MEMORY;
107 }
108 }
109
110 if (!isCallbackRegistered_.test_and_set()) {
111 LOGI("register callback");
112 CloudSyncServiceProxy->RegisterDownloadAssetCallback(downloadAssetCallback_);
113 }
114 SetDeathRecipient(CloudSyncServiceProxy->AsObject());
115 auto taskId = downloadAssetCallback_->GetTaskId();
116 downloadAssetCallback_->AddDownloadTaskCallback(taskId, resultCallback);
117 AssetInfoObj assetInfoObj(assetInfo);
118 int32_t ret = CloudSyncServiceProxy->DownloadAsset(taskId, userId, bundleName, networkId, assetInfoObj);
119 LOGI("DownloadFile ret %{public}d, taskId:%{public}" PRIu64 "", ret, taskId);
120 return ret;
121 }
122
DeleteAsset(const int32_t userId,const std::string & uri)123 int32_t CloudSyncAssetManagerImpl::DeleteAsset(const int32_t userId, const std::string &uri)
124 {
125 auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
126 if (!CloudSyncServiceProxy) {
127 LOGE("proxy is null");
128 return E_SA_LOAD_FAILED;
129 }
130
131 SetDeathRecipient(CloudSyncServiceProxy->AsObject());
132 int32_t ret = CloudSyncServiceProxy->DeleteAsset(userId, uri);
133 LOGI("DeleteAsset ret %{public}d", ret);
134 return ret;
135 }
136
SetDeathRecipient(const sptr<IRemoteObject> & remoteObject)137 void CloudSyncAssetManagerImpl::SetDeathRecipient(const sptr<IRemoteObject> &remoteObject)
138 {
139 if (!isFirstCall_.test_and_set()) {
140 auto deathCallback = [this](const wptr<IRemoteObject> &obj) {
141 LOGE("service died. Died remote obj");
142 CloudSyncServiceProxy::InvaildInstance();
143 isCallbackRegistered_.clear();
144 isFirstCall_.clear();
145 };
146 deathRecipient_ = sptr(new SvcDeathRecipient(deathCallback));
147 remoteObject->AddDeathRecipient(deathRecipient_);
148 }
149 }
150 } // namespace OHOS::FileManagement::CloudSync
151