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