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 "pasteboard_entry_getter_stub.h"
17
18 #include "ipc_skeleton.h"
19 #include "message_parcel_warp.h"
20 #include "pasteboard_hilog.h"
21 #include "pasteboard_serv_ipc_interface_code.h"
22
23 namespace OHOS {
24 namespace MiscServices {
25 using namespace OHOS::Security::PasteboardServ;
PasteboardEntryGetterStub()26 PasteboardEntryGetterStub::PasteboardEntryGetterStub()
27 {
28 memberFuncMap_[static_cast<uint32_t>(PasteboardEntryGetterInterfaceCode::GET_RECORD_VALUE_BY_TYPE)] =
29 &PasteboardEntryGetterStub::OnGetRecordValueByType;
30 }
31
~PasteboardEntryGetterStub()32 PasteboardEntryGetterStub::~PasteboardEntryGetterStub()
33 {
34 memberFuncMap_.clear();
35 }
36
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)37 int PasteboardEntryGetterStub::OnRemoteRequest(
38 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
39 {
40 PASTEBOARD_HILOGI(
41 PASTEBOARD_MODULE_CLIENT, "code:%{public}u, callingPid:%{public}d", code, IPCSkeleton::GetCallingPid());
42 std::u16string localDescriptor = PasteboardEntryGetterStub::GetDescriptor();
43 std::u16string remoteDescriptor = data.ReadInterfaceToken();
44 if (remoteDescriptor != localDescriptor) {
45 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "remote descriptor is not equal to local descriptor");
46 return ERR_INVALID_VALUE;
47 }
48 auto itFunc = memberFuncMap_.find(code);
49 if (itFunc != memberFuncMap_.end()) {
50 auto memberFunc = itFunc->second;
51 if (memberFunc != nullptr) {
52 return (this->*memberFunc)(data, reply);
53 }
54 }
55 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
56 }
57
OnGetRecordValueByType(MessageParcel & data,MessageParcel & reply)58 int32_t PasteboardEntryGetterStub::OnGetRecordValueByType(MessageParcel &data, MessageParcel &reply)
59 {
60 uint32_t recordId = data.ReadUint32();
61 int64_t rawDataSize = data.ReadInt64();
62 MessageParcelWarp messageData;
63 if (rawDataSize <= 0 || rawDataSize > messageData.GetRawDataSize()) {
64 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "invalid raw data size");
65 return ERR_INVALID_VALUE;
66 }
67 const uint8_t *rawData = reinterpret_cast<const uint8_t *>(messageData.ReadRawData(data, rawDataSize));
68 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(rawData != nullptr, ERR_INVALID_VALUE,
69 PASTEBOARD_MODULE_CLIENT, "read entry tlv raw data failed, size=%{public}" PRId64, rawDataSize);
70 std::vector<uint8_t> recvEntryTlv(rawData, rawData + rawDataSize);
71 PasteDataEntry entryValue;
72 bool ret = entryValue.Decode(recvEntryTlv);
73 if (!ret) {
74 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "unmarshall entry value failed");
75 return ERR_INVALID_VALUE;
76 }
77 auto result = GetRecordValueByType(recordId, entryValue);
78 if (!reply.WriteInt32(result)) {
79 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to write result:%{public}d", result);
80 return ERR_INVALID_VALUE;
81 }
82 std::vector<uint8_t> sendEntryTLV(0);
83 ret = entryValue.Encode(sendEntryTLV);
84 if (!ret) {
85 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "marshall entry value failed");
86 return ERR_INVALID_VALUE;
87 }
88 if (!reply.WriteInt64(sendEntryTLV.size())) {
89 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "write entry tlv raw data size failed");
90 return ERR_INVALID_VALUE;
91 }
92 MessageParcelWarp messageReply;
93 size_t tlvSize = sendEntryTLV.size();
94 if (!messageReply.WriteRawData(reply, sendEntryTLV.data(), tlvSize)) {
95 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "write entry tlv raw data failed size:%{public}zu", tlvSize);
96 return ERR_INVALID_VALUE;
97 }
98 return ERR_OK;
99 }
100 } // namespace MiscServices
101 } // namespace OHOS