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/asset_recv_callback_stub.h"
17
18 #include "asset/asset_callback_interface_code.h"
19 #include "dfs_error.h"
20 #include "utils_log.h"
21
22 namespace OHOS {
23 namespace Storage {
24 namespace DistributedFile {
25 using namespace FileManagement;
AssetRecvCallbackStub()26 AssetRecvCallbackStub::AssetRecvCallbackStub()
27 {
28 opToInterfaceMap_[static_cast<uint32_t>(AssetCallbackInterfaceCode::ASSET_CALLBACK_ON_START)] =
29 &AssetRecvCallbackStub::HandleOnStart;
30 opToInterfaceMap_[static_cast<uint32_t>(AssetCallbackInterfaceCode::ASSET_CALLBACK_ON_PROGRESS)] =
31 &AssetRecvCallbackStub::HandleOnRecvProgress;
32 opToInterfaceMap_[static_cast<uint32_t>(AssetCallbackInterfaceCode::ASSET_CALLBACK_ON_FINISHED)] =
33 &AssetRecvCallbackStub::HandleOnFinished;
34 }
35
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)36 int32_t AssetRecvCallbackStub::OnRemoteRequest(uint32_t code,
37 MessageParcel &data,
38 MessageParcel &reply,
39 MessageOption &option)
40 {
41 if (data.ReadInterfaceToken() != GetDescriptor()) {
42 return ASSET_RECV_CALLBACK_DESCRIPTOR_IS_EMPTY;
43 }
44 auto interfaceIndex = opToInterfaceMap_.find(code);
45 if (interfaceIndex == opToInterfaceMap_.end() || !interfaceIndex->second) {
46 LOGE("Cannot response request %{public}d: unknown tranction", code);
47 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
48 }
49 return (this->*(interfaceIndex->second))(data, reply);
50 }
51
HandleOnStart(MessageParcel & data,MessageParcel & reply)52 int32_t AssetRecvCallbackStub::HandleOnStart(MessageParcel &data, MessageParcel &reply)
53 {
54 std::string srcNetworkId;
55 if (!data.ReadString(srcNetworkId)) {
56 LOGE("read srcNetworkId failed");
57 return E_INVAL_ARG;
58 }
59 std::string destNetworkId;
60 if (!data.ReadString(destNetworkId)) {
61 LOGE("read destNetworkId failed");
62 return E_INVAL_ARG;
63 }
64 std::string sessionId;
65 if (!data.ReadString(sessionId)) {
66 LOGE("read sessionId failed");
67 return E_INVAL_ARG;
68 }
69 std::string destBundleName;
70 if (!data.ReadString(destBundleName)) {
71 LOGE("read destBundleName failed");
72 return E_INVAL_ARG;
73 }
74 int32_t res = OnStart(srcNetworkId, destNetworkId, sessionId, destBundleName);
75 if (res != E_OK) {
76 LOGE("OnStart call failed");
77 return E_BROKEN_IPC;
78 }
79 reply.WriteInt32(res);
80 return res;
81 }
82
HandleOnRecvProgress(MessageParcel & data,MessageParcel & reply)83 int32_t AssetRecvCallbackStub::HandleOnRecvProgress(MessageParcel &data, MessageParcel &reply)
84 {
85 std::string srcNetworkId;
86 if (!data.ReadString(srcNetworkId)) {
87 LOGE("read srcNetworkId failed");
88 return E_INVAL_ARG;
89 }
90
91 sptr<AssetObj> assetObj = data.ReadParcelable<AssetObj>();
92 if (!assetObj) {
93 LOGE("object of AssetObj is nullptr");
94 return E_INVAL_ARG;
95 }
96
97 uint64_t totalBytes;
98 if (!data.ReadUint64(totalBytes)) {
99 LOGE("read totalBytes failed");
100 return E_INVAL_ARG;
101 }
102
103 uint64_t processBytes;
104 if (!data.ReadUint64(processBytes)) {
105 LOGE("read processBytes failed");
106 return E_INVAL_ARG;
107 }
108
109 int32_t res = OnRecvProgress(srcNetworkId, assetObj, totalBytes, processBytes);
110 if (res != E_OK) {
111 LOGE("OnRecvProgress call failed");
112 return E_BROKEN_IPC;
113 }
114
115 reply.WriteInt32(res);
116 return res;
117 }
118
HandleOnFinished(MessageParcel & data,MessageParcel & reply)119 int32_t AssetRecvCallbackStub::HandleOnFinished(MessageParcel &data, MessageParcel &reply)
120 {
121 std::string srcNetworkId;
122 if (!data.ReadString(srcNetworkId)) {
123 LOGE("read srcNetworkId failed");
124 return E_INVAL_ARG;
125 }
126
127 sptr<AssetObj> assetObj = data.ReadParcelable<AssetObj>();
128 if (!assetObj) {
129 LOGE("object of AssetObj is nullptr");
130 return E_INVAL_ARG;
131 }
132
133 int32_t result;
134 if (!data.ReadInt32(result)) {
135 LOGE("read result failed");
136 return E_INVAL_ARG;
137 }
138 int32_t res = OnFinished(srcNetworkId, assetObj, result);
139 if (res != E_OK) {
140 LOGE("OnFinished call failed");
141 return E_BROKEN_IPC;
142 }
143 reply.WriteInt32(res);
144 return res;
145 }
146 } // namespace DistributedFile
147 } // namespace Storage
148 } // namespace OHOS