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