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 "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 = ServiceProxy::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 = ServiceProxy::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,int32_t connectTime)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 int32_t connectTime)
70 {
71 auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
72 if (!CloudSyncServiceProxy) {
73 LOGE("proxy is null");
74 return E_SA_LOAD_FAILED;
75 }
76
77 SetDeathRecipient(CloudSyncServiceProxy->AsObject());
78 std::vector<AssetInfoObj> assetInfoObj;
79 for (const auto &info : assetInfo) {
80 AssetInfoObj obj(info);
81 assetInfoObj.emplace_back(obj);
82 }
83 int32_t ret = CloudSyncServiceProxy->DownloadFiles(userId, bundleName, assetInfoObj, assetResultMap, connectTime);
84 LOGI("DownloadFile ret %{public}d", ret);
85 return ret;
86 }
87
DownloadFile(const int32_t userId,const std::string & bundleName,const std::string & networkId,AssetInfo & assetInfo,ResultCallback resultCallback)88 int32_t CloudSyncAssetManagerImpl::DownloadFile(const int32_t userId,
89 const std::string &bundleName,
90 const std::string &networkId,
91 AssetInfo &assetInfo,
92 ResultCallback resultCallback)
93 {
94 auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
95 if (!CloudSyncServiceProxy) {
96 LOGE("proxy is null");
97 return E_SA_LOAD_FAILED;
98 }
99
100 {
101 std::lock_guard<std::mutex> lock(callbackInitMutex_);
102 if (downloadAssetCallback_ == nullptr) {
103 downloadAssetCallback_ = sptr(new (std::nothrow) DownloadAssetCallbackClient());
104 }
105 if (downloadAssetCallback_ == nullptr) {
106 LOGE("have no enough memory");
107 return E_MEMORY;
108 }
109 }
110
111 if (!isCallbackRegistered_.test_and_set()) {
112 LOGI("register callback");
113 CloudSyncServiceProxy->RegisterDownloadAssetCallback(downloadAssetCallback_);
114 }
115 SetDeathRecipient(CloudSyncServiceProxy->AsObject());
116 auto taskId = downloadAssetCallback_->GetTaskId();
117 downloadAssetCallback_->AddDownloadTaskCallback(taskId, resultCallback);
118 AssetInfoObj assetInfoObj(assetInfo);
119 int32_t ret = CloudSyncServiceProxy->DownloadAsset(taskId, userId, bundleName, networkId, assetInfoObj);
120 LOGI("DownloadFile ret %{public}d, taskId:%{public}" PRIu64 "", ret, taskId);
121 return ret;
122 }
123
DeleteAsset(const int32_t userId,const std::string & uri)124 int32_t CloudSyncAssetManagerImpl::DeleteAsset(const int32_t userId, const std::string &uri)
125 {
126 auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
127 if (!CloudSyncServiceProxy) {
128 LOGE("proxy is null");
129 return E_SA_LOAD_FAILED;
130 }
131
132 SetDeathRecipient(CloudSyncServiceProxy->AsObject());
133 int32_t ret = CloudSyncServiceProxy->DeleteAsset(userId, uri);
134 LOGI("DeleteAsset ret %{public}d", ret);
135 return ret;
136 }
137
SetDeathRecipient(const sptr<IRemoteObject> & remoteObject)138 void CloudSyncAssetManagerImpl::SetDeathRecipient(const sptr<IRemoteObject> &remoteObject)
139 {
140 if (!isFirstCall_.test_and_set()) {
141 auto deathCallback = [this](const wptr<IRemoteObject> &obj) {
142 LOGE("service died. Died remote obj");
143 ServiceProxy::InvaildInstance();
144 isCallbackRegistered_.clear();
145 isFirstCall_.clear();
146 };
147 deathRecipient_ = sptr(new SvcDeathRecipient(deathCallback));
148 remoteObject->AddDeathRecipient(deathRecipient_);
149 }
150 }
151 } // namespace OHOS::FileManagement::CloudSync
152