• 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 
16 #include "pasteboard_service_stub.h"
17 
18 #include "errors.h"
19 #include "paste_data.h"
20 #include "pasteboard_error.h"
21 #include "pasteboard_hilog.h"
22 #include "pasteboard_observer_proxy.h"
23 #include "paste_uri_handler.h"
24 #include "copy_uri_handler.h"
25 
26 namespace OHOS {
27 namespace MiscServices {
PasteboardServiceStub()28 PasteboardServiceStub::PasteboardServiceStub()
29 {
30     memberFuncMap_[static_cast<uint32_t>(GET_PASTE_DATA)] = &PasteboardServiceStub::OnGetPasteData;
31     memberFuncMap_[static_cast<uint32_t>(HAS_PASTE_DATA)] = &PasteboardServiceStub::OnHasPasteData;
32     memberFuncMap_[static_cast<uint32_t>(SET_PASTE_DATA)] = &PasteboardServiceStub::OnSetPasteData;
33     memberFuncMap_[static_cast<uint32_t>(CLEAR_ALL)] = &PasteboardServiceStub::OnClear;
34     memberFuncMap_[static_cast<uint32_t>(ADD_CHANGED_OBSERVER)] =
35         &PasteboardServiceStub::OnAddPasteboardChangedObserver;
36     memberFuncMap_[static_cast<uint32_t>(DELETE_CHANGED_OBSERVER)] =
37         &PasteboardServiceStub::OnRemovePasteboardChangedObserver;
38     memberFuncMap_[static_cast<uint32_t>(DELETE_ALL_CHANGED_OBSERVER)] =
39         &PasteboardServiceStub::OnRemoveAllChangedObserver;
40     memberFuncMap_[static_cast<uint32_t>(ADD_EVENT_OBSERVER)] =
41         &PasteboardServiceStub::OnAddPasteboardEventObserver;
42     memberFuncMap_[static_cast<uint32_t>(DELETE_EVENT_OBSERVER)] =
43         &PasteboardServiceStub::OnRemovePasteboardEventObserver;
44     memberFuncMap_[static_cast<uint32_t>(DELETE_ALL_EVENT_OBSERVER)] =
45         &PasteboardServiceStub::OnRemoveAllEventObserver;
46 }
47 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)48 int32_t PasteboardServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
49     MessageOption &option)
50 {
51     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start##code = %{public}u", code);
52     std::u16string myDescripter = PasteboardServiceStub::GetDescriptor();
53     std::u16string remoteDescripter = data.ReadInterfaceToken();
54     if (myDescripter != remoteDescripter) {
55         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "end##descriptor checked fail");
56         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
57     }
58     pid_t p = IPCSkeleton::GetCallingPid();
59     pid_t p1 = IPCSkeleton::GetCallingUid();
60     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "CallingPid = %{public}d, CallingUid = %{public}d, code = %{public}u",
61         p, p1, code);
62     auto itFunc = memberFuncMap_.find(code);
63     if (itFunc != memberFuncMap_.end()) {
64         auto memberFunc = itFunc->second;
65         if (memberFunc != nullptr) {
66             return (this->*memberFunc)(data, reply);
67         }
68     }
69     int ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
70     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end##ret = %{public}d", ret);
71     return ret;
72 }
OnClear(MessageParcel & data,MessageParcel & reply)73 int32_t PasteboardServiceStub::OnClear(MessageParcel &data, MessageParcel &reply)
74 {
75     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
76     Clear();
77     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
78     return ERR_OK;
79 }
80 
OnGetPasteData(MessageParcel & data,MessageParcel & reply)81 int32_t PasteboardServiceStub::OnGetPasteData(MessageParcel &data, MessageParcel &reply)
82 {
83     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " start.");
84     PasteData pasteData{};
85     auto result = GetPasteData(pasteData);
86     std::vector<uint8_t> pasteDataTlv(0);
87     bool ret = pasteData.Encode(pasteDataTlv);
88     if (!ret) {
89         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to encode pastedata in TLV");
90         return ERR_INVALID_VALUE;
91     }
92     if (!reply.WriteInt32(pasteDataTlv.size())) {
93         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to write raw size");
94         return ERR_INVALID_VALUE;
95     }
96     if (!reply.WriteRawData(pasteDataTlv.data(), pasteDataTlv.size())) {
97         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to write raw data");
98         return ERR_INVALID_VALUE;
99     }
100     PasteUriHandler pasteUriHandler;
101     if (!pasteData.WriteUriFd(reply, pasteUriHandler, false)) {
102         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to write uri fd");
103         return ERR_INVALID_VALUE;
104     }
105     if (!reply.WriteInt32(result)) {
106         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to GetPasteData result");
107         return ERR_INVALID_VALUE;
108     }
109     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " end.");
110     return ERR_OK;
111 }
OnHasPasteData(MessageParcel & data,MessageParcel & reply)112 int32_t PasteboardServiceStub::OnHasPasteData(MessageParcel &data, MessageParcel &reply)
113 {
114     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " start.");
115     auto result = HasPasteData();
116     reply.WriteBool(result);
117     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " end.");
118     return ERR_OK;
119 }
120 
OnSetPasteData(MessageParcel & data,MessageParcel & reply)121 int32_t PasteboardServiceStub::OnSetPasteData(MessageParcel &data, MessageParcel &reply)
122 {
123     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " start.");
124     int32_t rawDataSize = data.ReadInt32();
125     if (rawDataSize <= 0) {
126         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to read raw size");
127         return ERR_INVALID_VALUE;
128     }
129     auto *rawData = (uint8_t *)data.ReadRawData(rawDataSize);
130     if (rawData == nullptr) {
131         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to get raw data");
132         return ERR_INVALID_VALUE;
133     }
134     std::vector<uint8_t> pasteDataTlv(rawData, rawData + rawDataSize);
135     PasteData pasteData;
136     bool ret = pasteData.Decode(pasteDataTlv);
137     if (!ret) {
138         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to decode pastedata in TLV");
139         return ERR_INVALID_VALUE;
140     }
141     CopyUriHandler copyUriHandler;
142     if (!pasteData.ReadUriFd(data, copyUriHandler)) {
143         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to read uri fd");
144         return ERR_INVALID_VALUE;
145     }
146     int32_t result = SetPasteData(pasteData);
147     if (!reply.WriteInt32(result)) {
148         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to write SetPasteData result");
149         return ERR_INVALID_VALUE;
150     }
151     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " end.");
152     return ERR_OK;
153 }
OnAddPasteboardChangedObserver(MessageParcel & data,MessageParcel & reply)154 int32_t PasteboardServiceStub::OnAddPasteboardChangedObserver(MessageParcel &data, MessageParcel &reply)
155 {
156     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
157     sptr<IPasteboardChangedObserver> callback;
158     if (!IsObserverValid(data, callback)) {
159         return ERR_INVALID_VALUE;
160     }
161 
162     AddPasteboardChangedObserver(callback);
163     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
164     return ERR_OK;
165 }
OnRemovePasteboardChangedObserver(MessageParcel & data,MessageParcel & reply)166 int32_t PasteboardServiceStub::OnRemovePasteboardChangedObserver(MessageParcel &data, MessageParcel &reply)
167 {
168     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
169     sptr<IPasteboardChangedObserver> callback;
170     if (!IsObserverValid(data, callback)) {
171         return ERR_INVALID_VALUE;
172     }
173     RemovePasteboardChangedObserver(callback);
174     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
175     return ERR_OK;
176 }
177 
OnRemoveAllChangedObserver(MessageParcel & data,MessageParcel & reply)178 int32_t PasteboardServiceStub::OnRemoveAllChangedObserver(MessageParcel &data, MessageParcel &reply)
179 {
180     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
181     RemoveAllChangedObserver();
182     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
183     return ERR_OK;
184 }
185 
OnAddPasteboardEventObserver(MessageParcel & data,MessageParcel & reply)186 int32_t PasteboardServiceStub::OnAddPasteboardEventObserver(MessageParcel &data, MessageParcel &reply)
187 {
188     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
189     sptr<IPasteboardChangedObserver> callback;
190     if (!IsObserverValid(data, callback)) {
191         return ERR_INVALID_VALUE;
192     }
193 
194     AddPasteboardEventObserver(callback);
195     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
196     return ERR_OK;
197 }
198 
OnRemovePasteboardEventObserver(MessageParcel & data,MessageParcel & reply)199 int32_t PasteboardServiceStub::OnRemovePasteboardEventObserver(MessageParcel &data, MessageParcel &reply)
200 {
201     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
202     sptr<IPasteboardChangedObserver> callback;
203     if (!IsObserverValid(data, callback)) {
204         return ERR_INVALID_VALUE;
205     }
206     RemovePasteboardEventObserver(callback);
207     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
208     return ERR_OK;
209 }
210 
OnRemoveAllEventObserver(MessageParcel & data,MessageParcel & reply)211 int32_t PasteboardServiceStub::OnRemoveAllEventObserver(MessageParcel &data, MessageParcel &reply)
212 {
213     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
214     RemoveAllEventObserver();
215     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
216     return ERR_OK;
217 }
218 
IsObserverValid(MessageParcel & data,sptr<IPasteboardChangedObserver> & callback)219 inline bool PasteboardServiceStub::IsObserverValid(MessageParcel &data, sptr<IPasteboardChangedObserver> &callback)
220 {
221     sptr<IRemoteObject> obj = data.ReadRemoteObject();
222     if (obj == nullptr) {
223         PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "obj nullptr");
224         return false;
225     }
226     callback = iface_cast<IPasteboardChangedObserver>(obj);
227     if (callback == nullptr) {
228         PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "callback nullptr");
229         return false;
230     }
231     return true;
232 }
233 
~PasteboardServiceStub()234 PasteboardServiceStub::~PasteboardServiceStub()
235 {
236     memberFuncMap_.clear();
237 }
238 } // namespace MiscServices
239 } // namespace OHOS
240