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 #include "ipc_skeleton.h"
18 #include "pasteboard_hilog.h"
19 #include "pasteboard_serv_ipc_interface_code.h"
20
21 #define MAX_RAWDATA_SIZE (128 * 1024 * 1024)
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 int32_t rawDataSize = data.ReadInt32();
62 if (rawDataSize <= 0 || rawDataSize > MAX_RAWDATA_SIZE) {
63 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "invalid raw data size");
64 return ERR_INVALID_VALUE;
65 }
66 const uint8_t *rawData = reinterpret_cast<const uint8_t *>(data.ReadRawData(rawDataSize));
67 if (rawData == nullptr) {
68 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "read entry tlv raw data fail");
69 return ERR_INVALID_VALUE;
70 }
71 std::vector<uint8_t> recvEntryTlv(rawData, rawData + rawDataSize);
72 PasteDataEntry entryValue;
73 bool ret = entryValue.Decode(recvEntryTlv);
74 if (!ret) {
75 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "unmarshall entry value failed");
76 return ERR_INVALID_VALUE;
77 }
78 auto result = GetRecordValueByType(recordId, entryValue);
79 if (!reply.WriteInt32(result)) {
80 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to write result:%{public}d", result);
81 return ERR_INVALID_VALUE;
82 }
83 std::vector<uint8_t> sendEntryTLV(0);
84 ret = entryValue.Encode(sendEntryTLV);
85 if (!ret) {
86 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "marshall entry value failed");
87 return ERR_INVALID_VALUE;
88 }
89 if (!reply.WriteInt32(sendEntryTLV.size())) {
90 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "write entry tlv raw data size failed");
91 return ERR_INVALID_VALUE;
92 }
93 if (!reply.WriteRawData(sendEntryTLV.data(), sendEntryTLV.size())) {
94 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "write entry tlv raw data failed");
95 return ERR_INVALID_VALUE;
96 }
97 return ERR_OK;
98 }
99 } // namespace MiscServices
100 } // namespace OHOS