1 /*
2 * Copyright (c) 2024 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_FINISHED)] =
31 &AssetRecvCallbackStub::HandleOnFinished;
32 }
33
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)34 int32_t AssetRecvCallbackStub::OnRemoteRequest(uint32_t code,
35 MessageParcel &data,
36 MessageParcel &reply,
37 MessageOption &option)
38 {
39 if (data.ReadInterfaceToken() != GetDescriptor()) {
40 return ASSET_RECV_CALLBACK_DESCRIPTOR_IS_EMPTY;
41 }
42 auto interfaceIndex = opToInterfaceMap_.find(code);
43 if (interfaceIndex == opToInterfaceMap_.end() || !interfaceIndex->second) {
44 LOGE("Cannot response request %{public}d: unknown tranction", code);
45 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
46 }
47 return (this->*(interfaceIndex->second))(data, reply);
48 }
49
HandleOnStart(MessageParcel & data,MessageParcel & reply)50 int32_t AssetRecvCallbackStub::HandleOnStart(MessageParcel &data, MessageParcel &reply)
51 {
52 std::string srcNetworkId;
53 if (!data.ReadString(srcNetworkId)) {
54 LOGE("read srcNetworkId failed");
55 return E_INVAL_ARG;
56 }
57 std::string destNetworkId;
58 if (!data.ReadString(destNetworkId)) {
59 LOGE("read destNetworkId failed");
60 return E_INVAL_ARG;
61 }
62 std::string sessionId;
63 if (!data.ReadString(sessionId)) {
64 LOGE("read sessionId failed");
65 return E_INVAL_ARG;
66 }
67 std::string destBundleName;
68 if (!data.ReadString(destBundleName)) {
69 LOGE("read destBundleName failed");
70 return E_INVAL_ARG;
71 }
72 int32_t res = OnStart(srcNetworkId, destNetworkId, sessionId, destBundleName);
73 if (res != E_OK) {
74 LOGE("OnStart call failed");
75 return E_BROKEN_IPC;
76 }
77 reply.WriteInt32(res);
78 return res;
79 }
80
HandleOnFinished(MessageParcel & data,MessageParcel & reply)81 int32_t AssetRecvCallbackStub::HandleOnFinished(MessageParcel &data, MessageParcel &reply)
82 {
83 std::string srcNetworkId;
84 if (!data.ReadString(srcNetworkId)) {
85 LOGE("read srcNetworkId failed");
86 return E_INVAL_ARG;
87 }
88
89 sptr<AssetObj> assetObj = data.ReadParcelable<AssetObj>();
90 if (!assetObj) {
91 LOGE("object of AssetObj is nullptr");
92 return E_INVAL_ARG;
93 }
94
95 int32_t result;
96 if (!data.ReadInt32(result)) {
97 LOGE("read result failed");
98 return E_INVAL_ARG;
99 }
100 int32_t res = OnFinished(srcNetworkId, assetObj, result);
101 if (res != E_OK) {
102 LOGE("OnFinished call failed");
103 return E_BROKEN_IPC;
104 }
105 reply.WriteInt32(res);
106 return res;
107 }
108 } // namespace DistributedFile
109 } // namespace Storage
110 } // namespace OHOS