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_send_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;
AssetSendCallbackStub()26 AssetSendCallbackStub::AssetSendCallbackStub()
27 {
28     opToInterfaceMap_[static_cast<uint32_t>(AssetCallbackInterfaceCode::ASSET_CALLBACK_ON_SEND_RESULT)] =
29         &AssetSendCallbackStub::HandleOnSendResult;
30 }
31 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)32 int32_t AssetSendCallbackStub::OnRemoteRequest(uint32_t code,
33                                                MessageParcel &data,
34                                                MessageParcel &reply,
35                                                MessageOption &option)
36 {
37     if (data.ReadInterfaceToken() != GetDescriptor()) {
38         return ASSET_SEND_CALLBACK_DESCRIPTOR_IS_EMPTY;
39     }
40     auto interfaceIndex = opToInterfaceMap_.find(code);
41     if (interfaceIndex == opToInterfaceMap_.end() || !interfaceIndex->second) {
42         LOGE("Cannot response request %{public}d: unknown tranction", code);
43         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
44     }
45     return (this->*(interfaceIndex->second))(data, reply);
46 }
47 
HandleOnSendResult(MessageParcel & data,MessageParcel & reply)48 int32_t AssetSendCallbackStub::HandleOnSendResult(MessageParcel &data, MessageParcel &reply)
49 {
50     sptr<AssetObj> assetObj = data.ReadParcelable<AssetObj>();
51     if (!assetObj) {
52         LOGE("object of AssetObj is nullptr");
53         return E_INVAL_ARG;
54     }
55 
56     int32_t result;
57     if (!data.ReadInt32(result)) {
58         LOGE("read result failed");
59         return E_INVAL_ARG;
60     }
61     int32_t res = OnSendResult(assetObj, result);
62     if (res != E_OK) {
63         LOGE("OnFinished call failed");
64         return E_BROKEN_IPC;
65     }
66     reply.WriteInt32(res);
67     return res;
68 }
69 } // namespace DistributedFile
70 } // namespace Storage
71 } // namespace OHOS