1bc03f14fSopenharmony_ci/*
2bc03f14fSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3bc03f14fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4bc03f14fSopenharmony_ci * you may not use this file except in compliance with the License.
5bc03f14fSopenharmony_ci * You may obtain a copy of the License at
6bc03f14fSopenharmony_ci *
7bc03f14fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8bc03f14fSopenharmony_ci *
9bc03f14fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10bc03f14fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11bc03f14fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12bc03f14fSopenharmony_ci * See the License for the specific language governing permissions and
13bc03f14fSopenharmony_ci * limitations under the License.
14bc03f14fSopenharmony_ci */
15bc03f14fSopenharmony_ci
16bc03f14fSopenharmony_ci#include "ipc_skeleton.h"
17bc03f14fSopenharmony_ci#include "pasteboard_entry_getter_stub.h"
18bc03f14fSopenharmony_ci#include "pasteboard_error.h"
19bc03f14fSopenharmony_ci#include "pasteboard_hilog.h"
20bc03f14fSopenharmony_ci#include "pasteboard_serv_ipc_interface_code.h"
21bc03f14fSopenharmony_ci
22bc03f14fSopenharmony_cinamespace OHOS {
23bc03f14fSopenharmony_cinamespace MiscServices {
24bc03f14fSopenharmony_ciusing namespace OHOS::Security::PasteboardServ;
25bc03f14fSopenharmony_ciPasteboardEntryGetterStub::PasteboardEntryGetterStub()
26bc03f14fSopenharmony_ci{
27bc03f14fSopenharmony_ci    memberFuncMap_[static_cast<uint32_t>(PasteboardEntryGetterInterfaceCode::GET_RECORD_VALUE_BY_TYPE)] =
28bc03f14fSopenharmony_ci        &PasteboardEntryGetterStub::OnGetRecordValueByType;
29bc03f14fSopenharmony_ci}
30bc03f14fSopenharmony_ci
31bc03f14fSopenharmony_ciPasteboardEntryGetterStub::~PasteboardEntryGetterStub()
32bc03f14fSopenharmony_ci{
33bc03f14fSopenharmony_ci    memberFuncMap_.clear();
34bc03f14fSopenharmony_ci}
35bc03f14fSopenharmony_ci
36bc03f14fSopenharmony_ciint PasteboardEntryGetterStub::OnRemoteRequest(
37bc03f14fSopenharmony_ci    uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
38bc03f14fSopenharmony_ci{
39bc03f14fSopenharmony_ci    PASTEBOARD_HILOGI(
40bc03f14fSopenharmony_ci        PASTEBOARD_MODULE_CLIENT, "code:%{public}u, callingPid:%{public}d", code, IPCSkeleton::GetCallingPid());
41bc03f14fSopenharmony_ci    std::u16string localDescriptor = PasteboardEntryGetterStub::GetDescriptor();
42bc03f14fSopenharmony_ci    std::u16string remoteDescriptor = data.ReadInterfaceToken();
43bc03f14fSopenharmony_ci    if (remoteDescriptor != localDescriptor) {
44bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "remote descriptor is not equal to local descriptor");
45bc03f14fSopenharmony_ci        return -1;
46bc03f14fSopenharmony_ci    }
47bc03f14fSopenharmony_ci    auto itFunc = memberFuncMap_.find(code);
48bc03f14fSopenharmony_ci    if (itFunc != memberFuncMap_.end()) {
49bc03f14fSopenharmony_ci        auto memberFunc = itFunc->second;
50bc03f14fSopenharmony_ci        if (memberFunc != nullptr) {
51bc03f14fSopenharmony_ci            return (this->*memberFunc)(data, reply);
52bc03f14fSopenharmony_ci        }
53bc03f14fSopenharmony_ci    }
54bc03f14fSopenharmony_ci    return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
55bc03f14fSopenharmony_ci}
56bc03f14fSopenharmony_ci
57bc03f14fSopenharmony_ciint32_t PasteboardEntryGetterStub::OnGetRecordValueByType(MessageParcel &data, MessageParcel &reply)
58bc03f14fSopenharmony_ci{
59bc03f14fSopenharmony_ci    uint32_t recordId = data.ReadUint32();
60bc03f14fSopenharmony_ci    int32_t rawDataSize = data.ReadInt32();
61bc03f14fSopenharmony_ci    if (rawDataSize <= 0) {
62bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "read entry tlv raw data size fail");
63bc03f14fSopenharmony_ci        return ERR_INVALID_VALUE;
64bc03f14fSopenharmony_ci    }
65bc03f14fSopenharmony_ci    const uint8_t *rawData = reinterpret_cast<const uint8_t *>(data.ReadRawData(rawDataSize));
66bc03f14fSopenharmony_ci    if (rawData == nullptr) {
67bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "read entry tlv raw data fail");
68bc03f14fSopenharmony_ci        return ERR_INVALID_VALUE;
69bc03f14fSopenharmony_ci    }
70bc03f14fSopenharmony_ci    std::vector<uint8_t> recvEntryTlv(rawData, rawData + rawDataSize);
71bc03f14fSopenharmony_ci    PasteDataEntry entryValue;
72bc03f14fSopenharmony_ci    bool ret = entryValue.Unmarshalling(recvEntryTlv);
73bc03f14fSopenharmony_ci    if (!ret) {
74bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "unmarshall entry value failed");
75bc03f14fSopenharmony_ci        return ERR_INVALID_VALUE;
76bc03f14fSopenharmony_ci    }
77bc03f14fSopenharmony_ci    auto result = GetRecordValueByType(recordId, entryValue);
78bc03f14fSopenharmony_ci    if (!reply.WriteInt32(result)) {
79bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to write result:%{public}d", result);
80bc03f14fSopenharmony_ci        return ERR_INVALID_VALUE;
81bc03f14fSopenharmony_ci    }
82bc03f14fSopenharmony_ci    std::vector<uint8_t> sendEntryTLV(0);
83bc03f14fSopenharmony_ci    ret = entryValue.Marshalling(sendEntryTLV);
84bc03f14fSopenharmony_ci    if (!ret) {
85bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "marshall entry value failed");
86bc03f14fSopenharmony_ci        return ERR_INVALID_VALUE;
87bc03f14fSopenharmony_ci    }
88bc03f14fSopenharmony_ci    if (!reply.WriteInt32(sendEntryTLV.size())) {
89bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "write entry tlv raw data size failed");
90bc03f14fSopenharmony_ci        return ERR_INVALID_VALUE;
91bc03f14fSopenharmony_ci    }
92bc03f14fSopenharmony_ci    if (!reply.WriteRawData(sendEntryTLV.data(), sendEntryTLV.size())) {
93bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "write entry tlv raw data failed");
94bc03f14fSopenharmony_ci        return ERR_INVALID_VALUE;
95bc03f14fSopenharmony_ci    }
96bc03f14fSopenharmony_ci    return ERR_OK;
97bc03f14fSopenharmony_ci}
98bc03f14fSopenharmony_ci} // namespace MiscServices
99bc03f14fSopenharmony_ci} // namespace OHOS