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_service_stub.h"
17
18 #include "hiview_adapter.h"
19 #include "pasteboard_error.h"
20 #include "pasteboard_hilog.h"
21 #include "pasteboard_serv_ipc_interface_code.h"
22
23 #define MAX_RAWDATA_SIZE (128 * 1024 * 1024)
24
25 using namespace OHOS::Security::PasteboardServ;
26 namespace OHOS {
27 namespace MiscServices {
PasteboardServiceStub()28 PasteboardServiceStub::PasteboardServiceStub()
29 {
30 memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::GET_PASTE_DATA)] =
31 &PasteboardServiceStub::OnGetPasteData;
32 memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::HAS_PASTE_DATA)] =
33 &PasteboardServiceStub::OnHasPasteData;
34 memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::SET_PASTE_DATA)] =
35 &PasteboardServiceStub::OnSetPasteData;
36 memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::CLEAR_ALL)] = &PasteboardServiceStub::OnClear;
37 memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::SUBSCRIBE_OBSERVER)] =
38 &PasteboardServiceStub::OnSubscribeObserver;
39 memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::UNSUBSCRIBE_OBSERVER)] =
40 &PasteboardServiceStub::OnUnsubscribeObserver;
41 memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::UNSUBSCRIBE_ALL_OBSERVER)] =
42 &PasteboardServiceStub::OnUnsubscribeAllObserver;
43 memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::IS_REMOTE_DATA)] =
44 &PasteboardServiceStub::OnIsRemoteData;
45 memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::GET_DATA_SOURCE)] =
46 &PasteboardServiceStub::OnGetDataSource;
47 memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::HAS_DATA_TYPE)] =
48 &PasteboardServiceStub::OnHasDataType;
49 memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::DETECT_PATTERNS)] =
50 &PasteboardServiceStub::OnDetectPatterns;
51 memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::SET_GLOBAL_SHARE_OPTION)] =
52 &PasteboardServiceStub::OnSetGlobalShareOption;
53 memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::REMOVE_GLOBAL_SHARE_OPTION)] =
54 &PasteboardServiceStub::OnRemoveGlobalShareOption;
55 memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::GET_GLOBAL_SHARE_OPTION)] =
56 &PasteboardServiceStub::OnGetGlobalShareOption;
57 memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::SET_APP_SHARE_OPTIONS)] =
58 &PasteboardServiceStub::OnSetAppShareOptions;
59 memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::REMOVE_APP_SHARE_OPTIONS)] =
60 &PasteboardServiceStub::OnRemoveAppShareOptions;
61 memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::PASTE_START)] =
62 &PasteboardServiceStub::OnPasteStart;
63 memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::PASTE_COMPLETE)] =
64 &PasteboardServiceStub::OnPasteComplete;
65 memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::REGISTER_CLIENT_DEATH_OBSERVER)] =
66 &PasteboardServiceStub::OnRegisterClientDeathObserver;
67 memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::GET_RECORD_VALUE)] =
68 &PasteboardServiceStub::OnGetRecordValueByType;
69 memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::GET_MIME_TYPES)] =
70 &PasteboardServiceStub::OnGetMimeTypes;
71 memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::GET_REMOTE_DEVICE_NAME)] =
72 &PasteboardServiceStub::OnGetRemoteDeviceName;
73 memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::SHOW_PROGRESS)] =
74 &PasteboardServiceStub::OnShowProgress;
75 memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::GET_CHANGE_COUNT)] =
76 &PasteboardServiceStub::OnGetChangeCount;
77 memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::SUBSCRIBE_ENTITY_OBSERVER)] =
78 &PasteboardServiceStub::OnSubscribeEntityObserver;
79 memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::UNSUBSCRIBE_ENTITY_OBSERVER)] =
80 &PasteboardServiceStub::OnUnsubscribeEntityObserver;
81 }
82
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)83 int32_t PasteboardServiceStub::OnRemoteRequest(
84 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
85 {
86 std::u16string myDescriptor = PasteboardServiceStub::GetDescriptor();
87 std::u16string remoteDescriptor = data.ReadInterfaceToken();
88 if (myDescriptor != remoteDescriptor) {
89 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "end##descriptor checked fail");
90 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
91 }
92 pid_t pid = IPCSkeleton::GetCallingPid();
93 pid_t uid = IPCSkeleton::GetCallingUid();
94 if (code != static_cast<uint32_t>(PasteboardServiceInterfaceCode::HAS_PASTE_DATA)) {
95 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "pid:%{public}d, uid:%{public}d, cmd:%{public}u", pid, uid, code);
96 }
97 auto itFunc = memberFuncMap_.find(code);
98 if (itFunc != memberFuncMap_.end()) {
99 auto memberFunc = itFunc->second;
100 if (memberFunc != nullptr) {
101 return (this->*memberFunc)(data, reply);
102 }
103 }
104 int ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
105 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end##ret = %{public}d", ret);
106 return ret;
107 }
OnClear(MessageParcel & data,MessageParcel & reply)108 int32_t PasteboardServiceStub::OnClear(MessageParcel &data, MessageParcel &reply)
109 {
110 Clear();
111 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
112 return ERR_OK;
113 }
114
OnGetRecordValueByType(MessageParcel & data,MessageParcel & reply)115 int32_t PasteboardServiceStub::OnGetRecordValueByType(MessageParcel &data, MessageParcel &reply)
116 {
117 uint32_t dataId = 0;
118 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(data.ReadUint32(dataId), ERR_INVALID_VALUE, PASTEBOARD_MODULE_SERVICE,
119 "read uint32 failed");
120 uint32_t recordId = 0;
121 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(data.ReadUint32(recordId), ERR_INVALID_VALUE, PASTEBOARD_MODULE_SERVICE,
122 "read uint32 failed");
123 PasteDataEntry entryValue;
124 int32_t rawDataSize = data.ReadInt32();
125 if (rawDataSize <= 0 || rawDataSize > MAX_RAWDATA_SIZE) {
126 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "invalid raw data size");
127 return ERR_INVALID_VALUE;
128 }
129 const uint8_t *rawData = reinterpret_cast<const uint8_t *>(data.ReadRawData(rawDataSize));
130 if (rawData == nullptr) {
131 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "fail to get raw data");
132 return ERR_INVALID_VALUE;
133 }
134 std::vector<uint8_t> receiveTlv(rawData, rawData + rawDataSize);
135 bool ret = entryValue.Decode(receiveTlv);
136 if (!ret) {
137 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "fail to decode paste data entry");
138 return ERR_INVALID_VALUE;
139 }
140 auto result = GetRecordValueByType(dataId, recordId, entryValue);
141 if (!reply.WriteInt32(result)) {
142 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to write result:%{public}d", result);
143 return ERR_INVALID_VALUE;
144 }
145 std::vector<uint8_t> entryValueTLV(0);
146 ret = entryValue.Encode(entryValueTLV);
147 if (!ret) {
148 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "fail encode entry value");
149 return ERR_INVALID_VALUE;
150 }
151 if (!reply.WriteInt32(entryValueTLV.size())) {
152 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "fail write data size");
153 return ERR_INVALID_VALUE;
154 }
155 if (!reply.WriteRawData(entryValueTLV.data(), entryValueTLV.size())) {
156 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "fail write raw data");
157 return ERR_INVALID_VALUE;
158 }
159 return ERR_OK;
160 }
161
OnGetPasteData(MessageParcel & data,MessageParcel & reply)162 int32_t PasteboardServiceStub::OnGetPasteData(MessageParcel &data, MessageParcel &reply)
163 {
164 std::string pasteId = data.ReadString();
165 PasteData pasteData{};
166 pasteData.SetPasteId(pasteId);
167 int32_t syncTime = 0;
168 auto result = GetPasteData(pasteData, syncTime);
169 HiViewAdapter::ReportUseBehaviour(pasteData, HiViewAdapter::PASTE_STATE, result);
170 std::vector<uint8_t> pasteDataTlv(0);
171 bool ret = pasteData.Encode(pasteDataTlv);
172 if (!ret) {
173 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to encode pastedata in TLV");
174 return ERR_INVALID_VALUE;
175 }
176 if (!reply.WriteInt32(pasteDataTlv.size())) {
177 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to write raw size");
178 return ERR_INVALID_VALUE;
179 }
180 if (!reply.WriteRawData(pasteDataTlv.data(), pasteDataTlv.size())) {
181 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to write raw data");
182 return ERR_INVALID_VALUE;
183 }
184 if (!reply.WriteInt32(syncTime)) {
185 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to GetPasteData syncTime");
186 return ERR_INVALID_VALUE;
187 }
188 if (!reply.WriteInt32(result)) {
189 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to GetPasteData result");
190 return ERR_INVALID_VALUE;
191 }
192 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " end.");
193 return ERR_OK;
194 }
OnHasPasteData(MessageParcel & data,MessageParcel & reply)195 int32_t PasteboardServiceStub::OnHasPasteData(MessageParcel &data, MessageParcel &reply)
196 {
197 auto result = HasPasteData();
198 reply.WriteBool(result);
199 return ERR_OK;
200 }
201
UnmarshalPasteData(MessageParcel & data,MessageParcel & reply)202 std::shared_ptr<PasteData> PasteboardServiceStub::UnmarshalPasteData(MessageParcel &data, MessageParcel &reply)
203 {
204 int32_t rawDataSize = data.ReadInt32();
205 if (rawDataSize <= 0 || rawDataSize > MAX_RAWDATA_SIZE) {
206 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Invalid raw data size");
207 return nullptr;
208 }
209 auto *rawData = (uint8_t *)data.ReadRawData(rawDataSize);
210 if (rawData == nullptr) {
211 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to get raw data");
212 return nullptr;
213 }
214 std::vector<uint8_t> pasteDataTlv(rawData, rawData + rawDataSize);
215 auto pasteData = std::make_shared<PasteData>();
216 bool ret = pasteData->Decode(pasteDataTlv);
217 if (!ret) {
218 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to decode pastedata in TLV");
219 return nullptr;
220 }
221 return pasteData;
222 }
223
OnSetPasteData(MessageParcel & data,MessageParcel & reply)224 int32_t PasteboardServiceStub::OnSetPasteData(MessageParcel &data, MessageParcel &reply)
225 {
226 auto pasteData = UnmarshalPasteData(data, reply);
227 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(pasteData != nullptr, ERR_INVALID_VALUE, PASTEBOARD_MODULE_SERVICE,
228 "Failed to Unmarshal PasteData");
229 sptr<IPasteboardDelayGetter> delayGetter = nullptr;
230 if (pasteData->IsDelayData()) {
231 sptr<IRemoteObject> obj = data.ReadRemoteObject();
232 if (obj == nullptr) {
233 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "delay getter is nullptr");
234 return ERR_INVALID_VALUE;
235 }
236 delayGetter = iface_cast<IPasteboardDelayGetter>(obj);
237 if (delayGetter == nullptr) {
238 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "delay getter is nullptr");
239 return ERR_INVALID_VALUE;
240 }
241 }
242 sptr<IPasteboardEntryGetter> entryGetter = nullptr;
243 if (pasteData->IsDelayRecord()) {
244 sptr<IRemoteObject> obj = data.ReadRemoteObject();
245 if (obj == nullptr) {
246 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "entry getter is nullptr");
247 return ERR_INVALID_VALUE;
248 }
249 entryGetter = iface_cast<IPasteboardEntryGetter>(obj);
250 if (entryGetter == nullptr) {
251 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "entry getter is nullptr");
252 return ERR_INVALID_VALUE;
253 }
254 }
255 auto result = SetPasteData(*pasteData, delayGetter, entryGetter);
256 HiViewAdapter::ReportUseBehaviour(*pasteData, HiViewAdapter::COPY_STATE, result);
257 if (!reply.WriteInt32(result)) {
258 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to write SetPasteData result");
259 return ERR_INVALID_VALUE;
260 }
261 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " end, ret is %{public}d.", result);
262 return ERR_OK;
263 }
264
OnSubscribeObserver(MessageParcel & data,MessageParcel & reply)265 int32_t PasteboardServiceStub::OnSubscribeObserver(MessageParcel &data, MessageParcel &reply)
266 {
267 uint32_t type = 0;
268 sptr<IPasteboardChangedObserver> callback;
269 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(IsObserverValid(data, type, callback), ERR_INVALID_VALUE,
270 PASTEBOARD_MODULE_SERVICE, "Observer invalid");
271
272 SubscribeObserver(static_cast<PasteboardObserverType>(type), callback);
273 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
274 return ERR_OK;
275 }
OnUnsubscribeObserver(MessageParcel & data,MessageParcel & reply)276 int32_t PasteboardServiceStub::OnUnsubscribeObserver(MessageParcel &data, MessageParcel &reply)
277 {
278 uint32_t type = 0;
279 sptr<IPasteboardChangedObserver> callback;
280 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(IsObserverValid(data, type, callback), ERR_INVALID_VALUE,
281 PASTEBOARD_MODULE_SERVICE, "Observer invalid");
282 UnsubscribeObserver(static_cast<PasteboardObserverType>(type), callback);
283 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
284 return ERR_OK;
285 }
286
OnUnsubscribeAllObserver(MessageParcel & data,MessageParcel & reply)287 int32_t PasteboardServiceStub::OnUnsubscribeAllObserver(MessageParcel &data, MessageParcel &reply)
288 {
289 uint32_t type = 0;
290 if (!data.ReadUint32(type)) {
291 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Read type failed.");
292 return ERR_INVALID_VALUE;
293 }
294 UnsubscribeAllObserver(static_cast<PasteboardObserverType>(type));
295 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
296 return ERR_OK;
297 }
298
IsObserverValid(MessageParcel & data,uint32_t & type,sptr<IPasteboardChangedObserver> & callback)299 bool PasteboardServiceStub::IsObserverValid(
300 MessageParcel &data, uint32_t &type, sptr<IPasteboardChangedObserver> &callback)
301 {
302 if (!data.ReadUint32(type)) {
303 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Read type failed.");
304 return false;
305 }
306 sptr<IRemoteObject> obj = data.ReadRemoteObject();
307 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(obj != nullptr, false, PASTEBOARD_MODULE_SERVICE, "obj nullptr");
308 callback = iface_cast<IPasteboardChangedObserver>(obj);
309 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(callback != nullptr, false, PASTEBOARD_MODULE_SERVICE, "callback nullptr");
310 return true;
311 }
312
OnIsRemoteData(MessageParcel & data,MessageParcel & reply)313 int32_t PasteboardServiceStub::OnIsRemoteData(MessageParcel &data, MessageParcel &reply)
314 {
315 auto result = IsRemoteData();
316 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(reply.WriteBool(result), ERR_INVALID_VALUE, PASTEBOARD_MODULE_SERVICE,
317 "Failed to write result");
318 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
319 return ERR_OK;
320 }
321
OnGetDataSource(MessageParcel & data,MessageParcel & reply)322 int32_t PasteboardServiceStub::OnGetDataSource(MessageParcel &data, MessageParcel &reply)
323 {
324 std::string bundleName;
325 auto ret = GetDataSource(bundleName);
326 if (bundleName.empty() || bundleName.length() > MAX_BUNDLE_NAME_LENGTH) {
327 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to get bundleName");
328 return ERR_INVALID_VALUE;
329 }
330 if (!reply.WriteString(bundleName)) {
331 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to writeName result");
332 return ERR_INVALID_VALUE;
333 }
334 if (!reply.WriteInt32(ret)) {
335 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to OnGetResourceApp result");
336 return ERR_INVALID_VALUE;
337 }
338 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end, ret is %{public}d.", ret);
339 return ERR_OK;
340 }
341
OnGetMimeTypes(MessageParcel & data,MessageParcel & reply)342 int32_t PasteboardServiceStub::OnGetMimeTypes(MessageParcel &data, MessageParcel &reply)
343 {
344 std::vector<std::string> mimeTypes = GetMimeTypes();
345 if (!reply.WriteUint32(static_cast<uint32_t>(mimeTypes.size()))) {
346 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Write size failed.");
347 return ERR_INVALID_VALUE;
348 }
349 for (const std::string &type : mimeTypes) {
350 if (!reply.WriteString(type)) {
351 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Write mime type failed.");
352 return ERR_INVALID_VALUE;
353 }
354 }
355 return ERR_OK;
356 }
357
OnGetChangeCount(MessageParcel & data,MessageParcel & reply)358 int32_t PasteboardServiceStub::OnGetChangeCount(MessageParcel &data, MessageParcel &reply)
359 {
360 uint32_t changeCount = 0;
361 int32_t ret = GetChangeCount(changeCount);
362 if (!reply.WriteUint32(changeCount)) {
363 return ERR_INVALID_VALUE;
364 }
365 if (!reply.WriteInt32(ret)) {
366 return ERR_INVALID_VALUE;
367 }
368 return ERR_OK;
369 }
370
OnSubscribeEntityObserver(MessageParcel & data,MessageParcel & reply)371 int32_t PasteboardServiceStub::OnSubscribeEntityObserver(MessageParcel &data, MessageParcel &reply)
372 {
373 uint32_t type = 0;
374 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(
375 data.ReadUint32(type), ERR_INVALID_VALUE, PASTEBOARD_MODULE_SERVICE, "Failed to read data");
376 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(type < static_cast<uint32_t>(EntityType::MAX), ERR_INVALID_VALUE,
377 PASTEBOARD_MODULE_SERVICE, "Failed to read data");
378 EntityType entityType = static_cast<EntityType>(type);
379 uint32_t expectedDataLength = 0;
380 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(
381 data.ReadUint32(expectedDataLength), ERR_INVALID_VALUE, PASTEBOARD_MODULE_SERVICE, "Failed to read data");
382 sptr<IRemoteObject> callback = data.ReadRemoteObject();
383 sptr<IEntityRecognitionObserver> observer = iface_cast<IEntityRecognitionObserver>(callback);
384 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(
385 observer != nullptr, ERR_INVALID_VALUE, PASTEBOARD_MODULE_SERVICE, "Failed to read data");
386 int32_t ret = SubscribeEntityObserver(entityType, expectedDataLength, observer);
387 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(reply.WriteInt32(ret),
388 static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR), PASTEBOARD_MODULE_SERVICE, "Write result failed");
389 return ERR_OK;
390 }
391
OnUnsubscribeEntityObserver(MessageParcel & data,MessageParcel & reply)392 int32_t PasteboardServiceStub::OnUnsubscribeEntityObserver(MessageParcel &data, MessageParcel &reply)
393 {
394 uint32_t type = 0;
395 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(
396 data.ReadUint32(type), ERR_INVALID_VALUE, PASTEBOARD_MODULE_SERVICE, "Failed to read data");
397 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(type < static_cast<uint32_t>(EntityType::MAX), ERR_INVALID_VALUE,
398 PASTEBOARD_MODULE_SERVICE, "Failed to read data");
399 EntityType entityType = static_cast<EntityType>(type);
400 uint32_t expectedDataLength = 0;
401 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(
402 data.ReadUint32(expectedDataLength), ERR_INVALID_VALUE, PASTEBOARD_MODULE_SERVICE, "Failed to read data");
403 sptr<IRemoteObject> callback = data.ReadRemoteObject();
404 sptr<IEntityRecognitionObserver> observer = iface_cast<IEntityRecognitionObserver>(callback);
405 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(
406 observer != nullptr, ERR_INVALID_VALUE, PASTEBOARD_MODULE_SERVICE, "Failed to read data");
407 int32_t ret = UnsubscribeEntityObserver(entityType, expectedDataLength, observer);
408 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(reply.WriteInt32(ret),
409 static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR), PASTEBOARD_MODULE_SERVICE, "Write result failed");
410 return ERR_OK;
411 }
412
OnGetRemoteDeviceName(MessageParcel & data,MessageParcel & reply)413 int32_t PasteboardServiceStub::OnGetRemoteDeviceName(MessageParcel &data, MessageParcel &reply)
414 {
415 std::string name;
416 bool isRemote;
417 auto ret = GetRemoteDeviceName(name, isRemote);
418 if (name.empty()) {
419 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to get remote device name");
420 return ERR_INVALID_VALUE;
421 }
422 if (!reply.WriteString(name) || !reply.WriteBool(isRemote)) {
423 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to write name result");
424 return ERR_INVALID_VALUE;
425 }
426 if (!reply.WriteInt32(ret)) {
427 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to write result");
428 return ERR_INVALID_VALUE;
429 }
430 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end, ret is %{public}d.", ret);
431 return ERR_OK;
432 }
433
OnShowProgress(MessageParcel & data,MessageParcel & reply)434 int32_t PasteboardServiceStub::OnShowProgress(MessageParcel &data, MessageParcel &reply)
435 {
436 std::string progressKey = data.ReadString();
437 sptr<IRemoteObject> callback = data.ReadRemoteObject();
438 if (callback == nullptr) {
439 return ERR_INVALID_VALUE;
440 }
441 ShowProgress(progressKey, callback);
442 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
443 return ERR_OK;
444 }
445
OnHasDataType(MessageParcel & data,MessageParcel & reply)446 int32_t PasteboardServiceStub::OnHasDataType(MessageParcel &data, MessageParcel &reply)
447 {
448 std::string mimeType = data.ReadString();
449 auto ret = HasDataType(mimeType);
450 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(reply.WriteBool(ret), ERR_INVALID_VALUE, PASTEBOARD_MODULE_SERVICE,
451 "Failed to write ret");
452 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
453 return ERR_OK;
454 }
455
OnDetectPatterns(MessageParcel & data,MessageParcel & reply)456 int32_t PasteboardServiceStub::OnDetectPatterns(MessageParcel &data, MessageParcel &reply)
457 {
458 uint32_t size = 0;
459 if (!data.ReadUint32(size)) {
460 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Read size failed.");
461 return ERR_INVALID_VALUE;
462 }
463 size_t readAbleSize = data.GetReadableBytes();
464 if (size > readAbleSize || size > static_cast<uint32_t>(Pattern::COUNT)) {
465 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Read oversize failed.");
466 return ERR_INVALID_VALUE;
467 }
468 std::set<Pattern> patternsToCheck;
469 for (uint32_t i = 0; i < size; i++) {
470 uint32_t pattern;
471 if (!data.ReadUint32(pattern)) {
472 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Read pattern failed.");
473 return ERR_INVALID_VALUE;
474 }
475 patternsToCheck.insert(static_cast<Pattern>(pattern));
476 }
477 std::set<Pattern> existedPatterns = DetectPatterns(patternsToCheck);
478 if (!reply.WriteUint32(static_cast<uint32_t>(existedPatterns.size()))) {
479 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Write size failed.");
480 return ERR_INVALID_VALUE;
481 }
482 for (const auto &pattern : existedPatterns) {
483 if (!reply.WriteUint32(static_cast<uint32_t>(pattern))) {
484 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Write pattern failed.");
485 return ERR_INVALID_VALUE;
486 }
487 }
488 return ERR_OK;
489 }
490
OnSetGlobalShareOption(MessageParcel & data,MessageParcel & reply)491 int32_t PasteboardServiceStub::OnSetGlobalShareOption(MessageParcel &data, MessageParcel &reply)
492 {
493 uint32_t size = 0;
494 if (!data.ReadUint32(size)) {
495 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Read size failed.");
496 return ERR_INVALID_VALUE;
497 }
498 size_t readAbleSize = data.GetReadableBytes();
499 if (size > readAbleSize || size > MAX_SET_GLOBAL_SHARE_OPTION_SIZE) {
500 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Read oversize failed.");
501 return ERR_INVALID_VALUE;
502 }
503 std::map<uint32_t, ShareOption> globalShareOptions;
504 for (uint32_t i = 0; i < size; i++) {
505 uint32_t tokenId;
506 if (!data.ReadUint32(tokenId)) {
507 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Read tokenId failed.");
508 return ERR_INVALID_VALUE;
509 }
510 int32_t shareOption;
511 if (!data.ReadInt32(shareOption)) {
512 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Read shareOption failed.");
513 return ERR_INVALID_VALUE;
514 }
515 globalShareOptions[tokenId] = static_cast<ShareOption>(shareOption);
516 }
517 int32_t result = SetGlobalShareOption(globalShareOptions);
518 if (!reply.WriteInt32(result)) {
519 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Write result failed.");
520 return ERR_INVALID_VALUE;
521 }
522 return ERR_OK;
523 }
524
OnRemoveGlobalShareOption(MessageParcel & data,MessageParcel & reply)525 int32_t PasteboardServiceStub::OnRemoveGlobalShareOption(MessageParcel &data, MessageParcel &reply)
526 {
527 std::vector<uint32_t> tokenIds;
528 if (!data.ReadUInt32Vector(&tokenIds)) {
529 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Read tokenIds failed.");
530 return ERR_INVALID_VALUE;
531 }
532 int32_t result = RemoveGlobalShareOption(tokenIds);
533 if (!reply.WriteInt32(result)) {
534 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Write result failed.");
535 return ERR_INVALID_VALUE;
536 }
537 return ERR_OK;
538 }
539
OnGetGlobalShareOption(MessageParcel & data,MessageParcel & reply)540 int32_t PasteboardServiceStub::OnGetGlobalShareOption(MessageParcel &data, MessageParcel &reply)
541 {
542 std::vector<uint32_t> tokenIds;
543 if (!data.ReadUInt32Vector(&tokenIds)) {
544 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Read tokenIds failed.");
545 return ERR_INVALID_VALUE;
546 }
547 std::map<uint32_t, ShareOption> globalShareOptions = GetGlobalShareOption(tokenIds);
548 if (!reply.WriteUint32(static_cast<uint32_t>(globalShareOptions.size()))) {
549 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Write size failed.");
550 return ERR_INVALID_VALUE;
551 }
552 for (const auto &[tokenId, shareOption] : globalShareOptions) {
553 if (!reply.WriteUint32(tokenId)) {
554 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Write tokenId failed.");
555 return ERR_INVALID_VALUE;
556 }
557 if (!reply.WriteInt32(static_cast<int32_t>(shareOption))) {
558 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Write shareOption failed.");
559 return ERR_INVALID_VALUE;
560 }
561 }
562 return ERR_OK;
563 }
564
OnSetAppShareOptions(MessageParcel & data,MessageParcel & reply)565 int32_t PasteboardServiceStub::OnSetAppShareOptions(MessageParcel &data, MessageParcel &reply)
566 {
567 int32_t shareOptions;
568 if (!data.ReadInt32(shareOptions)) {
569 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Read share options failed.");
570 return static_cast<int32_t>(PasteboardError::DESERIALIZATION_ERROR);
571 }
572 auto result = SetAppShareOptions(static_cast<ShareOption>(shareOptions));
573 if (!reply.WriteInt32(result)) {
574 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Write result failed.");
575 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
576 }
577 return ERR_OK;
578 }
579
OnRemoveAppShareOptions(MessageParcel & data,MessageParcel & reply)580 int32_t PasteboardServiceStub::OnRemoveAppShareOptions(MessageParcel &data, MessageParcel &reply)
581 {
582 auto result = RemoveAppShareOptions();
583 if (!reply.WriteInt32(result)) {
584 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Write result failed.");
585 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
586 }
587 return ERR_OK;
588 }
589
~PasteboardServiceStub()590 PasteboardServiceStub::~PasteboardServiceStub()
591 {
592 memberFuncMap_.clear();
593 }
594
OnPasteStart(MessageParcel & data,MessageParcel & reply)595 int32_t PasteboardServiceStub::OnPasteStart(MessageParcel &data, MessageParcel &reply)
596 {
597 std::string pasteId = data.ReadString();
598 PasteStart(pasteId);
599 return ERR_OK;
600 }
601
OnPasteComplete(MessageParcel & data,MessageParcel & reply)602 int32_t PasteboardServiceStub::OnPasteComplete(MessageParcel &data, MessageParcel &reply)
603 {
604 std::string deviceId = data.ReadString();
605 std::string pasteId = data.ReadString();
606 PasteComplete(deviceId, pasteId);
607 return ERR_OK;
608 }
609
OnRegisterClientDeathObserver(MessageParcel & data,MessageParcel & reply)610 int32_t PasteboardServiceStub::OnRegisterClientDeathObserver(MessageParcel &data, MessageParcel &reply)
611 {
612 sptr<IRemoteObject> pasteboardClientDeathObserverProxy = data.ReadRemoteObject();
613 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(pasteboardClientDeathObserverProxy != nullptr, ERR_INVALID_VALUE,
614 PASTEBOARD_MODULE_SERVICE, "Read remote object failed");
615 int32_t status = RegisterClientDeathObserver(std::move(pasteboardClientDeathObserverProxy));
616 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(reply.WriteInt32(static_cast<int32_t>(status)), ERR_INVALID_VALUE,
617 PASTEBOARD_MODULE_SERVICE, "Write status failed");
618 return ERR_OK;
619 }
620 } // namespace MiscServices
621 } // namespace OHOS