• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 #include "pasteboard_service_stub.h"
16 #include "pasteboard_common.h"
17 #include "i_pasteboard_service.h"
18 #include "paste_data.h"
19 
20 namespace OHOS {
21 namespace MiscServices {
PasteboardServiceStub()22 PasteboardServiceStub::PasteboardServiceStub()
23 {
24     memberFuncMap_[static_cast<uint32_t>(GET_PASTE_DATA)] = &PasteboardServiceStub::OnGetPasteData;
25     memberFuncMap_[static_cast<uint32_t>(HAS_PASTE_DATA)] = &PasteboardServiceStub::OnHasPasteData;
26     memberFuncMap_[static_cast<uint32_t>(SET_PASTE_DATA)] = &PasteboardServiceStub::OnSetPasteData;
27     memberFuncMap_[static_cast<uint32_t>(CLEAR_ALL)] = &PasteboardServiceStub::OnClear;
28     memberFuncMap_[static_cast<uint32_t>(ADD_OBSERVER)] =
29         &PasteboardServiceStub::OnAddPasteboardChangedObserver;
30     memberFuncMap_[static_cast<uint32_t>(DELETE_OBSERVER)] =
31         &PasteboardServiceStub::OnRemovePasteboardChangedObserver;
32     memberFuncMap_[static_cast<uint32_t>(DELETE_ALL_OBSERVER)] =
33         &PasteboardServiceStub::OnRemoveAllChangedObserver;
34 }
35 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)36 int32_t PasteboardServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
37     MessageOption &option)
38 {
39     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start##code = %{public}u", code);
40     std::u16string myDescripter = PasteboardServiceStub::GetDescriptor();
41     std::u16string remoteDescripter = data.ReadInterfaceToken();
42     if (myDescripter != remoteDescripter) {
43         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "end##descriptor checked fail");
44         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
45     }
46     pid_t p = IPCSkeleton::GetCallingPid();
47     pid_t p1 = IPCSkeleton::GetCallingUid();
48     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE,
49         "CallingPid = %{public}d, CallingUid = %{public}d, code = %{public}u",
50         p, p1, code);
51     auto itFunc = memberFuncMap_.find(code);
52     if (itFunc != memberFuncMap_.end()) {
53         auto memberFunc = itFunc->second;
54         if (memberFunc != nullptr) {
55             return (this->*memberFunc)(data, reply);
56         }
57     }
58     int ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
59     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end##ret = %{public}d", ret);
60     return ret;
61 }
OnClear(MessageParcel & data,MessageParcel & reply)62 int32_t PasteboardServiceStub::OnClear(MessageParcel &data, MessageParcel &reply)
63 {
64     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
65     Clear();
66     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
67     return ERR_OK;
68 }
69 
OnGetPasteData(MessageParcel & data,MessageParcel & reply)70 int32_t PasteboardServiceStub::OnGetPasteData(MessageParcel &data, MessageParcel &reply)
71 {
72     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " start.");
73     PasteData pasteData {};
74     auto hasPasteData = GetPasteData(pasteData);
75     if (!hasPasteData) {
76         PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " end.");
77         return ERR_INVALID_VALUE;
78     }
79     if (!reply.WriteParcelable(&pasteData)) {
80         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to write parcelable pasteData");
81         return ERR_INVALID_VALUE;
82     }
83     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " end.");
84     return ERR_OK;
85 }
OnHasPasteData(MessageParcel & data,MessageParcel & reply)86 int32_t PasteboardServiceStub::OnHasPasteData(MessageParcel &data, MessageParcel &reply)
87 {
88     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " start.");
89     auto result = HasPasteData();
90     reply.WriteBool(result);
91     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " end.");
92     return ERR_OK;
93 }
94 
OnSetPasteData(MessageParcel & data,MessageParcel & reply)95 int32_t PasteboardServiceStub::OnSetPasteData(MessageParcel &data, MessageParcel &reply)
96 {
97     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " start.");
98     std::unique_ptr<PasteData> pasteData(data.ReadParcelable<PasteData>());
99     if (!pasteData) {
100     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " start.");
101         return ERR_INVALID_VALUE;
102     }
103 
104     SetPasteData(*pasteData);
105     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " end.");
106     return ERR_OK;
107 }
OnAddPasteboardChangedObserver(MessageParcel & data,MessageParcel & reply)108 int32_t PasteboardServiceStub::OnAddPasteboardChangedObserver(MessageParcel &data, MessageParcel &reply)
109 {
110     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
111     sptr<IRemoteObject> obj = data.ReadRemoteObject();
112     if (obj == nullptr) {
113         PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "obj nullptr");
114         return ERR_INVALID_VALUE;
115     }
116     sptr<IPasteboardChangedObserver> callback = iface_cast<IPasteboardChangedObserver>(obj);
117     if (callback == nullptr) {
118         PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "callback nullptr");
119         return ERR_INVALID_VALUE;
120     }
121     AddPasteboardChangedObserver(callback);
122     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
123     return ERR_OK;
124 }
OnRemovePasteboardChangedObserver(MessageParcel & data,MessageParcel & reply)125 int32_t PasteboardServiceStub::OnRemovePasteboardChangedObserver(MessageParcel &data, MessageParcel &reply)
126 {
127     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
128     sptr<IRemoteObject> obj = data.ReadRemoteObject();
129     if (obj == nullptr) {
130         PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "obj nullptr");
131         return ERR_INVALID_VALUE;
132     }
133     sptr<IPasteboardChangedObserver> callback = iface_cast<IPasteboardChangedObserver>(obj);
134     if (callback == nullptr) {
135         PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "callback nullptr");
136         return ERR_INVALID_VALUE;
137     }
138     RemovePasteboardChangedObserver(callback);
139     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
140     return ERR_OK;
141 }
142 
OnRemoveAllChangedObserver(MessageParcel & data,MessageParcel & reply)143 int32_t PasteboardServiceStub::OnRemoveAllChangedObserver(MessageParcel &data, MessageParcel &reply)
144 {
145     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
146     RemoveAllChangedObserver();
147     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
148     return ERR_OK;
149 }
150 
~PasteboardServiceStub()151 PasteboardServiceStub::~PasteboardServiceStub()
152 {
153     memberFuncMap_.clear();
154 }
155 } // namespace MiscServices
156 } // namespace OHOS