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 "paste_uri_handler.h"
18bc03f14fSopenharmony_ci#include "pasteboard_delay_getter_stub.h"
19bc03f14fSopenharmony_ci#include "pasteboard_error.h"
20bc03f14fSopenharmony_ci
21bc03f14fSopenharmony_cinamespace OHOS {
22bc03f14fSopenharmony_cinamespace MiscServices {
23bc03f14fSopenharmony_ciconst PasteboardDelayGetterStub::Handler PasteboardDelayGetterStub::HANDLERS[TRANS_BUTT] = {
24bc03f14fSopenharmony_ci    &PasteboardDelayGetterStub::OnGetPasteData,
25bc03f14fSopenharmony_ci    &PasteboardDelayGetterStub::OnGetUnifiedData,
26bc03f14fSopenharmony_ci};
27bc03f14fSopenharmony_ci
28bc03f14fSopenharmony_ciint PasteboardDelayGetterStub::OnRemoteRequest(
29bc03f14fSopenharmony_ci    uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
30bc03f14fSopenharmony_ci{
31bc03f14fSopenharmony_ci    PASTEBOARD_HILOGI(
32bc03f14fSopenharmony_ci        PASTEBOARD_MODULE_SERVICE, "code:%{public}u, callingPid:%{public}d", code, IPCSkeleton::GetCallingPid());
33bc03f14fSopenharmony_ci    std::u16string localDescriptor = PasteboardDelayGetterStub::GetDescriptor();
34bc03f14fSopenharmony_ci    std::u16string remoteDescriptor = data.ReadInterfaceToken();
35bc03f14fSopenharmony_ci    if (remoteDescriptor != localDescriptor) {
36bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "remote descriptor is not equal to local descriptor");
37bc03f14fSopenharmony_ci        return -1;
38bc03f14fSopenharmony_ci    }
39bc03f14fSopenharmony_ci    if (TRANS_HEAD > code || code >= TRANS_BUTT || HANDLERS[code] == nullptr) {
40bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "not support code:%{public}u, max:%{public}d", code, TRANS_BUTT);
41bc03f14fSopenharmony_ci        return -1;
42bc03f14fSopenharmony_ci    }
43bc03f14fSopenharmony_ci    return (this->*HANDLERS[code])(data, reply);
44bc03f14fSopenharmony_ci}
45bc03f14fSopenharmony_ci
46bc03f14fSopenharmony_ciint32_t PasteboardDelayGetterStub::OnGetPasteData(MessageParcel &data, MessageParcel &reply)
47bc03f14fSopenharmony_ci{
48bc03f14fSopenharmony_ci    PasteData pasteData;
49bc03f14fSopenharmony_ci    std::string dataType = data.ReadString();
50bc03f14fSopenharmony_ci    GetPasteData(dataType, pasteData);
51bc03f14fSopenharmony_ci    std::vector<uint8_t> pasteDataTlv(0);
52bc03f14fSopenharmony_ci    bool ret = pasteData.Encode(pasteDataTlv);
53bc03f14fSopenharmony_ci    if (!ret) {
54bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "fail encode paste data");
55bc03f14fSopenharmony_ci        return ERR_INVALID_VALUE;
56bc03f14fSopenharmony_ci    }
57bc03f14fSopenharmony_ci    if (!reply.WriteInt32(pasteDataTlv.size())) {
58bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "fail write data size");
59bc03f14fSopenharmony_ci        return ERR_INVALID_VALUE;
60bc03f14fSopenharmony_ci    }
61bc03f14fSopenharmony_ci    if (!reply.WriteRawData(pasteDataTlv.data(), pasteDataTlv.size())) {
62bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "fail  write raw data");
63bc03f14fSopenharmony_ci        return ERR_INVALID_VALUE;
64bc03f14fSopenharmony_ci    }
65bc03f14fSopenharmony_ci    PasteUriHandler pasteUriHandler;
66bc03f14fSopenharmony_ci    if (!pasteData.WriteUriFd(reply, pasteUriHandler, false)) {
67bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "fail  write uri fd");
68bc03f14fSopenharmony_ci        return ERR_INVALID_VALUE;
69bc03f14fSopenharmony_ci    }
70bc03f14fSopenharmony_ci    return ERR_OK;
71bc03f14fSopenharmony_ci}
72bc03f14fSopenharmony_ci
73bc03f14fSopenharmony_ciint32_t PasteboardDelayGetterStub::OnGetUnifiedData(MessageParcel &data, MessageParcel &reply)
74bc03f14fSopenharmony_ci{
75bc03f14fSopenharmony_ci    return ERR_OK;
76bc03f14fSopenharmony_ci}
77bc03f14fSopenharmony_ci} // namespace MiscServices
78bc03f14fSopenharmony_ci} // namespace OHOS