1/*
2 * Copyright (c) 2023 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_observer_stub.h"
17
18#include "i_pasteboard_observer.h"
19#include "pasteboard_error.h"
20#include "pasteboard_serv_ipc_interface_code.h"
21
22using namespace OHOS::Security::PasteboardServ;
23namespace OHOS {
24namespace MiscServices {
25PasteboardObserverStub::PasteboardObserverStub()
26{
27    memberFuncMap_[static_cast<uint32_t>(PasteboardObserverInterfaceCode::ON_PASTE_BOARD_CHANGE)] =
28        &PasteboardObserverStub::OnPasteboardChangedStub;
29    memberFuncMap_[static_cast<uint32_t>(PasteboardObserverInterfaceCode::ON_PASTE_BOARD_EVENT)] =
30        &PasteboardObserverStub::OnPasteboardEventStub;
31}
32
33int32_t PasteboardObserverStub::OnRemoteRequest(
34    uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
35{
36    PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start##code = %{public}u", code);
37    std::u16string myDescripter = PasteboardObserverStub::GetDescriptor();
38    std::u16string remoteDescripter = data.ReadInterfaceToken();
39    if (myDescripter != remoteDescripter) {
40        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "end##descriptor checked fail");
41        return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
42    }
43    pid_t p = IPCSkeleton::GetCallingPid();
44    pid_t p1 = IPCSkeleton::GetCallingUid();
45    PASTEBOARD_HILOGI(
46        PASTEBOARD_MODULE_SERVICE, "CallingPid = %{public}d, CallingUid = %{public}d, code = %{public}u", p, p1, code);
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    int ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
55    PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end##ret = %{public}d", ret);
56    return ret;
57}
58
59int32_t PasteboardObserverStub::OnPasteboardChangedStub(MessageParcel &data, MessageParcel &reply)
60{
61    PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
62    OnPasteboardChanged();
63    PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
64    return ERR_OK;
65}
66
67int32_t PasteboardObserverStub::OnPasteboardEventStub(MessageParcel &data, MessageParcel &reply)
68{
69    PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
70    std::string bundleName = data.ReadString();
71    int32_t status = data.ReadInt32();
72    OnPasteboardEvent(bundleName, status);
73    PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
74    return ERR_OK;
75}
76
77PasteboardObserverStub::~PasteboardObserverStub()
78{
79    memberFuncMap_.clear();
80}
81} // namespace MiscServices
82} // namespace OHOS
83