1 /*
2 * Copyright (c) 2024-2025 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 "asset_recv_callback_proxy.h"
17
18 #include "accesstoken_kit.h"
19 #include "asset/asset_callback_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 {
26 namespace Storage {
27 namespace DistributedFile {
28 using namespace std;
29 using namespace OHOS::Storage;
30 using namespace OHOS::FileManagement;
31
OnStart(const std::string & srcNetworkId,const std::string & destNetworkId,const std::string & sessionId,const std::string & destBundleName)32 int32_t AssetRecvCallbackProxy::OnStart(const std::string &srcNetworkId,
33 const std::string &destNetworkId,
34 const std::string &sessionId,
35 const std::string &destBundleName)
36 {
37 MessageParcel data;
38 MessageParcel reply;
39 MessageOption option;
40 if (!data.WriteInterfaceToken(GetDescriptor())) {
41 LOGE("Failed to write interface token");
42 return E_BROKEN_IPC;
43 }
44 if (!data.WriteString(srcNetworkId)) {
45 LOGE("Failed to send srcNetworkId");
46 return E_INVAL_ARG;
47 }
48 if (!data.WriteString(destNetworkId)) {
49 LOGE("Failed to send destNetworkId");
50 return E_INVAL_ARG;
51 }
52 if (!data.WriteString(sessionId)) {
53 LOGE("Failed to send sessionId");
54 return E_INVAL_ARG;
55 }
56 if (!data.WriteString(destBundleName)) {
57 LOGE("Failed to send destBundleName");
58 return E_INVAL_ARG;
59 }
60 auto remote = Remote();
61 if (remote == nullptr) {
62 LOGE("remote is nullptr");
63 return E_BROKEN_IPC;
64 }
65 int32_t ret = remote->SendRequest(
66 static_cast<uint32_t>(AssetCallbackInterfaceCode::ASSET_CALLBACK_ON_START), data, reply, option);
67 if (ret != E_OK) {
68 LOGE("Failed to send out the request, errno:%{public}d", errno);
69 return E_BROKEN_IPC;
70 }
71 return reply.ReadInt32();
72 }
73
OnRecvProgress(const std::string & srcNetworkId,const sptr<AssetObj> & assetObj,uint64_t totalBytes,uint64_t processBytes)74 int32_t AssetRecvCallbackProxy::OnRecvProgress(const std::string &srcNetworkId,
75 const sptr<AssetObj> &assetObj,
76 uint64_t totalBytes,
77 uint64_t processBytes)
78 {
79 MessageParcel data;
80 MessageParcel reply;
81 MessageOption option;
82 if (!data.WriteInterfaceToken(GetDescriptor())) {
83 LOGE("Failed to write interface token");
84 return E_BROKEN_IPC;
85 }
86 if (!data.WriteString(srcNetworkId)) {
87 LOGE("Failed to send srcNetworkId");
88 return E_INVAL_ARG;
89 }
90
91 if (!data.WriteParcelable(assetObj)) {
92 LOGE("Failed to send the assetInfoObj");
93 return E_INVAL_ARG;
94 }
95
96 if (!data.WriteUint64(totalBytes)) {
97 LOGE("Failed to send totalBytes");
98 return E_INVAL_ARG;
99 }
100
101 if (!data.WriteUint64(processBytes)) {
102 LOGE("Failed to send processBytes");
103 return E_INVAL_ARG;
104 }
105 auto remote = Remote();
106 if (remote == nullptr) {
107 LOGE("remote is nullptr");
108 return E_BROKEN_IPC;
109 }
110 int32_t ret = remote->SendRequest(
111 static_cast<uint32_t>(AssetCallbackInterfaceCode::ASSET_CALLBACK_ON_PROGRESS), data, reply, option);
112 if (ret != E_OK) {
113 LOGE("Failed to send out the request, errno:%{public}d", errno);
114 return E_BROKEN_IPC;
115 }
116 return reply.ReadInt32();
117 }
118
OnFinished(const std::string & srcNetworkId,const sptr<AssetObj> & assetObj,int32_t result)119 int32_t AssetRecvCallbackProxy::OnFinished(const std::string &srcNetworkId,
120 const sptr<AssetObj> &assetObj,
121 int32_t result)
122 {
123 MessageParcel data;
124 MessageParcel reply;
125 MessageOption option;
126 if (!data.WriteInterfaceToken(GetDescriptor())) {
127 LOGE("Failed to write interface token");
128 return E_BROKEN_IPC;
129 }
130 if (!data.WriteString(srcNetworkId)) {
131 LOGE("Failed to send srcNetworkId");
132 return E_INVAL_ARG;
133 }
134
135 if (!data.WriteParcelable(assetObj)) {
136 LOGE("Failed to send the assetInfoObj");
137 return E_INVAL_ARG;
138 }
139
140 if (!data.WriteInt32(result)) {
141 LOGE("Failed to send result");
142 return E_INVAL_ARG;
143 }
144 auto remote = Remote();
145 if (remote == nullptr) {
146 LOGE("remote is nullptr");
147 return E_BROKEN_IPC;
148 }
149 int32_t ret = remote->SendRequest(
150 static_cast<uint32_t>(AssetCallbackInterfaceCode::ASSET_CALLBACK_ON_FINISHED), data, reply, option);
151 if (ret != E_OK) {
152 LOGE("Failed to send out the request, errno:%{public}d", errno);
153 return E_BROKEN_IPC;
154 }
155 return reply.ReadInt32();
156 }
157
158 } // namespace DistributedFile
159 } // namespace Storage
160 } // namespace OHOS