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_proxy.h"
17
18 #include "message_parcel_warp.h"
19 #include "pasteboard_error.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;
PasteboardEntryGetterProxy(const sptr<IRemoteObject> & object)26 PasteboardEntryGetterProxy::PasteboardEntryGetterProxy(const sptr<IRemoteObject> &object)
27 : IRemoteProxy<IPasteboardEntryGetter>(object)
28 {
29 }
30
MakeRequest(uint32_t recordId,PasteDataEntry & value,MessageParcel & request)31 int32_t PasteboardEntryGetterProxy::MakeRequest(uint32_t recordId, PasteDataEntry &value, MessageParcel &request)
32 {
33 if (!request.WriteInterfaceToken(GetDescriptor())) {
34 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "write descriptor failed");
35 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
36 }
37 if (!request.WriteUint32(recordId)) {
38 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "write recordId failed");
39 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
40 }
41 std::vector<uint8_t> sendEntryTLV(0);
42 if (!value.Encode(sendEntryTLV)) {
43 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "marshall entry value failed");
44 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
45 }
46 if (!request.WriteInt64(sendEntryTLV.size())) {
47 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "write entry tlv raw data size failed");
48 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
49 }
50 MessageParcelWarp messageRequest;
51 size_t tlvSize = sendEntryTLV.size();
52 if (!messageRequest.WriteRawData(request, sendEntryTLV.data(), tlvSize)) {
53 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "write entry tlv raw data failed size:%{public}zu", tlvSize);
54 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
55 }
56 return static_cast<int32_t>(PasteboardError::E_OK);
57 }
58
GetRecordValueByType(uint32_t recordId,PasteDataEntry & value)59 int32_t PasteboardEntryGetterProxy::GetRecordValueByType(uint32_t recordId, PasteDataEntry &value)
60 {
61 MessageParcel request;
62 auto res = MakeRequest(recordId, value, request);
63 if (res != static_cast<int32_t>(PasteboardError::E_OK)) {
64 return res;
65 }
66 MessageParcel reply;
67 MessageOption option;
68 int result = Remote()->SendRequest(
69 static_cast<int>(PasteboardEntryGetterInterfaceCode::GET_RECORD_VALUE_BY_TYPE), request, reply, option);
70 if (result != ERR_OK) {
71 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "send request failed, error:%{public}d", result);
72 return result;
73 }
74 res = reply.ReadInt32();
75 int64_t rawDataSize = reply.ReadInt64();
76 MessageParcelWarp messageReply;
77 if (rawDataSize <= 0 || rawDataSize > messageReply.GetRawDataSize()) {
78 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "read entry tlv raw data size failed");
79 return static_cast<int32_t>(PasteboardError::DESERIALIZATION_ERROR);
80 }
81 const uint8_t *rawData = reinterpret_cast<const uint8_t *>(messageReply.ReadRawData(reply, rawDataSize));
82 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(rawData != nullptr,
83 static_cast<int32_t>(PasteboardError::DESERIALIZATION_ERROR),
84 PASTEBOARD_MODULE_SERVICE, "read entry tlv raw data failed, size=%{public}" PRId64, rawDataSize);
85 std::vector<uint8_t> recvEntryTlv(rawData, rawData + rawDataSize);
86 PasteDataEntry entryValue;
87 if (!entryValue.Decode(recvEntryTlv)) {
88 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "unmarshall entry value failed");
89 return static_cast<int32_t>(PasteboardError::DESERIALIZATION_ERROR);
90 }
91 value = entryValue;
92 return res;
93 }
94 } // namespace MiscServices
95 } // namespace OHOS