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_proxy.h"
17
18 #include "pasteboard_error.h"
19 #include "pasteboard_hilog.h"
20 #include "pasteboard_serv_ipc_interface_code.h"
21
22 #define MAX_RAWDATA_SIZE (128 * 1024 * 1024)
23
24 using namespace OHOS::Security::PasteboardServ;
25 namespace OHOS {
26 namespace MiscServices {
PasteboardServiceProxy(const sptr<IRemoteObject> & object)27 PasteboardServiceProxy::PasteboardServiceProxy(const sptr<IRemoteObject> &object)
28 : IRemoteProxy<IPasteboardService>(object)
29 {
30 }
31
Clear()32 void PasteboardServiceProxy::Clear()
33 {
34 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "start.");
35 MessageParcel data;
36 MessageParcel reply;
37 MessageOption option;
38 if (!data.WriteInterfaceToken(GetDescriptor())) {
39 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write parcelable");
40 return;
41 }
42
43 int32_t result = Remote()->SendRequest(PasteboardServiceInterfaceCode::CLEAR_ALL, data, reply, option);
44 if (result != ERR_NONE) {
45 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "failed, error code is: %{public}d", result);
46 }
47 }
48
GetRecordValueByType(uint32_t dataId,uint32_t recordId,PasteDataEntry & value)49 int32_t PasteboardServiceProxy::GetRecordValueByType(uint32_t dataId, uint32_t recordId, PasteDataEntry &value)
50 {
51 MessageParcel data;
52 MessageParcel reply;
53 MessageOption option;
54 if (!data.WriteInterfaceToken(GetDescriptor())) {
55 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT,
56 "fail to write descriptor, dataId:%{public}d or recordId:%{public}d", dataId, recordId);
57 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
58 }
59 if (!data.WriteUint32(dataId) || !data.WriteUint32(recordId)) {
60 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT,
61 "fail to write dataId:%{public}d or recordId:%{public}d", dataId, recordId);
62 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
63 }
64 std::vector<uint8_t> sendTLV(0);
65 if (!value.Encode(sendTLV)) {
66 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "fail encode entry value");
67 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
68 }
69 if (!data.WriteInt32(sendTLV.size())) {
70 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "fail write data size");
71 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
72 }
73 if (!data.WriteRawData(sendTLV.data(), sendTLV.size())) {
74 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "fail write raw data");
75 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
76 }
77 int32_t result = Remote()->SendRequest(PasteboardServiceInterfaceCode::GET_RECORD_VALUE, data, reply, option);
78 if (result != ERR_NONE) {
79 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "failed, error code is:%{public}d", result);
80 return result;
81 }
82 int32_t res = reply.ReadInt32();
83 int32_t rawDataSize = reply.ReadInt32();
84 if (rawDataSize <= 0 || rawDataSize > MAX_RAWDATA_SIZE) {
85 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "invalid raw data size");
86 return static_cast<int32_t>(PasteboardError::DESERIALIZATION_ERROR) ;
87 }
88 const uint8_t *rawData = reinterpret_cast<const uint8_t *>(reply.ReadRawData(rawDataSize));
89 if (rawData == nullptr) {
90 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "fail to get raw data");
91 return static_cast<int32_t>(PasteboardError::DESERIALIZATION_ERROR);
92 }
93 std::vector<uint8_t> receiveTlv(rawData, rawData + rawDataSize);
94 PasteDataEntry entryValue;
95 if (!entryValue.Decode(receiveTlv)) {
96 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "fail to decode paste data entry");
97 return static_cast<int32_t>(PasteboardError::DESERIALIZATION_ERROR);
98 }
99 value = std::move(entryValue);
100 return res;
101 }
102
GetChangeCount(uint32_t & changeCount)103 int32_t PasteboardServiceProxy::GetChangeCount(uint32_t &changeCount)
104 {
105 MessageParcel data;
106 MessageParcel reply;
107 MessageOption option;
108 if (!data.WriteInterfaceToken(GetDescriptor())) {
109 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write token");
110 }
111 changeCount = 0;
112 int32_t result = Remote()->SendRequest(PasteboardServiceInterfaceCode::GET_CHANGE_COUNT, data, reply, option);
113 if (result != ERR_NONE) {
114 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Send request failed: ret=%{public}d", result);
115 return result;
116 }
117 if (!reply.ReadUint32(changeCount)) {
118 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to read changeCount!");
119 }
120 return reply.ReadInt32();
121 }
122
SubscribeEntityObserver(EntityType entityType,uint32_t expectedDataLength,const sptr<IEntityRecognitionObserver> & observer)123 int32_t PasteboardServiceProxy::SubscribeEntityObserver(
124 EntityType entityType, uint32_t expectedDataLength, const sptr<IEntityRecognitionObserver> &observer)
125 {
126 MessageParcel data;
127 MessageParcel reply;
128 MessageOption option;
129 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(data.WriteInterfaceToken(GetDescriptor()),
130 static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR), PASTEBOARD_MODULE_SERVICE,
131 "write descriptor failed!");
132 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(data.WriteUint32(static_cast<uint32_t>(entityType)),
133 static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR), PASTEBOARD_MODULE_SERVICE,
134 "write entityType failed!");
135 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(data.WriteUint32(expectedDataLength),
136 static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR), PASTEBOARD_MODULE_SERVICE,
137 "write expectedDataLength failed!");
138 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(data.WriteRemoteObject(observer->AsObject()),
139 static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR), PASTEBOARD_MODULE_SERVICE,
140 "write observer failed!");
141 int32_t result =
142 Remote()->SendRequest(PasteboardServiceInterfaceCode::SUBSCRIBE_ENTITY_OBSERVER, data, reply, option);
143 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(
144 result == ERR_NONE, result, PASTEBOARD_MODULE_SERVICE, "failed, error code is:%{public}d", result);
145 int32_t ret = 0;
146 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(reply.ReadInt32(ret),
147 static_cast<int32_t>(PasteboardError::DESERIALIZATION_ERROR), PASTEBOARD_MODULE_SERVICE, "read reply failed!");
148 return ret;
149 }
150
UnsubscribeEntityObserver(EntityType entityType,uint32_t expectedDataLength,const sptr<IEntityRecognitionObserver> & observer)151 int32_t PasteboardServiceProxy::UnsubscribeEntityObserver(
152 EntityType entityType, uint32_t expectedDataLength, const sptr<IEntityRecognitionObserver> &observer)
153 {
154 MessageParcel data;
155 MessageParcel reply;
156 MessageOption option;
157 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(data.WriteInterfaceToken(GetDescriptor()),
158 static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR), PASTEBOARD_MODULE_SERVICE,
159 "write descriptor failed!");
160 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(data.WriteUint32(static_cast<uint32_t>(entityType)),
161 static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR), PASTEBOARD_MODULE_SERVICE,
162 "write entityType failed!");
163 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(data.WriteUint32(expectedDataLength),
164 static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR), PASTEBOARD_MODULE_SERVICE,
165 "write expectedDataLength failed!");
166 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(data.WriteRemoteObject(observer->AsObject()),
167 static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR), PASTEBOARD_MODULE_SERVICE,
168 "write observer failed!");
169 int32_t result =
170 Remote()->SendRequest(PasteboardServiceInterfaceCode::UNSUBSCRIBE_ENTITY_OBSERVER, data, reply, option);
171 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(
172 result == ERR_NONE, result, PASTEBOARD_MODULE_SERVICE, "failed, error code is:%{public}d", result);
173 int32_t ret = 0;
174 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(reply.ReadInt32(ret),
175 static_cast<int32_t>(PasteboardError::DESERIALIZATION_ERROR), PASTEBOARD_MODULE_SERVICE, "read reply failed!");
176 return ret;
177 }
178
HasPasteData()179 bool PasteboardServiceProxy::HasPasteData()
180 {
181 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "start.");
182 MessageParcel data;
183 MessageParcel reply;
184 MessageOption option;
185
186 if (!data.WriteInterfaceToken(GetDescriptor())) {
187 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write parcelable");
188 return false;
189 }
190
191 int32_t result = Remote()->SendRequest(PasteboardServiceInterfaceCode::HAS_PASTE_DATA, data, reply, option);
192 if (result != ERR_NONE) {
193 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "failed, error code is: %{public}d", result);
194 return false;
195 }
196 auto has = reply.ReadBool();
197 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "end.");
198 return has;
199 }
200
SetPasteData(PasteData & pasteData,const sptr<IPasteboardDelayGetter> delayGetter,const sptr<IPasteboardEntryGetter> entryGetter)201 int32_t PasteboardServiceProxy::SetPasteData(PasteData &pasteData, const sptr<IPasteboardDelayGetter> delayGetter,
202 const sptr<IPasteboardEntryGetter> entryGetter)
203 {
204 MessageParcel data;
205 MessageParcel reply;
206 MessageOption option;
207 if (!data.WriteInterfaceToken(GetDescriptor())) {
208 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write parcelable");
209 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
210 }
211 if (pasteData.IsDelayData() && delayGetter == nullptr) {
212 pasteData.SetDelayData(false);
213 }
214 if (pasteData.IsDelayRecord() && entryGetter == nullptr) {
215 pasteData.SetDelayRecord(false);
216 }
217 std::vector<uint8_t> pasteDataTlv(0);
218 bool ret = pasteData.Encode(pasteDataTlv);
219 if (!ret) {
220 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to encode pastedata in TLV");
221 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
222 }
223 if (!data.WriteInt32(pasteDataTlv.size())) {
224 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write raw size");
225 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
226 }
227 if (!data.WriteRawData(pasteDataTlv.data(), pasteDataTlv.size())) {
228 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write raw data");
229 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
230 }
231 if (pasteData.IsDelayData() && !data.WriteRemoteObject(delayGetter->AsObject())) {
232 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "failed to write delay getter");
233 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
234 }
235 if (pasteData.IsDelayRecord() && !data.WriteRemoteObject(entryGetter->AsObject())) {
236 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "failed to write entry getter");
237 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
238 }
239 int32_t result = Remote()->SendRequest(PasteboardServiceInterfaceCode::SET_PASTE_DATA, data, reply, option);
240 if (result != ERR_NONE) {
241 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "failed, error code is: %{public}d", result);
242 return result;
243 }
244 return reply.ReadInt32();
245 }
246
GetPasteData(PasteData & pasteData,int32_t & syncTime)247 __attribute__ ((no_sanitize("cfi"))) int32_t PasteboardServiceProxy::GetPasteData(PasteData &pasteData,
248 int32_t &syncTime)
249 {
250 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "start.");
251 MessageParcel data;
252 MessageParcel reply;
253 MessageOption option;
254 if (!data.WriteInterfaceToken(GetDescriptor())) {
255 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write parcelable");
256 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
257 }
258 if (!data.WriteString(pasteData.GetPasteId())) {
259 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write pasteId");
260 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
261 }
262 int32_t result = Remote()->SendRequest(PasteboardServiceInterfaceCode::GET_PASTE_DATA, data, reply, option);
263 if (result != ERR_NONE) {
264 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "failed, error code is: %{public}d", result);
265 return result;
266 }
267 pasteData.SetPasteId("");
268 int32_t rawDataSize = reply.ReadInt32();
269 if (rawDataSize <= 0 || rawDataSize > MAX_RAWDATA_SIZE) {
270 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Invalid raw data size");
271 return static_cast<int32_t>(PasteboardError::DESERIALIZATION_ERROR);
272 }
273 auto *rawData = (uint8_t *)reply.ReadRawData(rawDataSize);
274 if (rawData == nullptr) {
275 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to get raw data");
276 return static_cast<int32_t>(PasteboardError::DESERIALIZATION_ERROR);
277 }
278 std::vector<uint8_t> pasteDataTlv(rawData, rawData + rawDataSize);
279 bool ret = pasteData.Decode(pasteDataTlv);
280 if (!ret) {
281 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to decode pastedata in TLV");
282 return static_cast<int32_t>(PasteboardError::DESERIALIZATION_ERROR);
283 }
284 syncTime = reply.ReadInt32();
285 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "end.");
286 return reply.ReadInt32();
287 }
288
SubscribeObserver(PasteboardObserverType type,const sptr<IPasteboardChangedObserver> & observer)289 void PasteboardServiceProxy::SubscribeObserver(PasteboardObserverType type,
290 const sptr<IPasteboardChangedObserver> &observer)
291 {
292 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "start.");
293 ProcessObserver(PasteboardServiceInterfaceCode::SUBSCRIBE_OBSERVER, type, observer);
294 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "end.");
295 }
296
UnsubscribeObserver(PasteboardObserverType type,const sptr<IPasteboardChangedObserver> & observer)297 void PasteboardServiceProxy::UnsubscribeObserver(PasteboardObserverType type,
298 const sptr<IPasteboardChangedObserver> &observer)
299 {
300 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "start.");
301 ProcessObserver(PasteboardServiceInterfaceCode::UNSUBSCRIBE_OBSERVER, type, observer);
302 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "end.");
303 }
UnsubscribeAllObserver(PasteboardObserverType type)304 void PasteboardServiceProxy::UnsubscribeAllObserver(PasteboardObserverType type)
305 {
306 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "start.");
307 MessageParcel data;
308 MessageParcel reply;
309 MessageOption option;
310 if (!data.WriteInterfaceToken(GetDescriptor())) {
311 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write parcelable");
312 return;
313 }
314 if (!data.WriteUint32(static_cast<uint32_t>(type))) {
315 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write parcelable");
316 return;
317 }
318 int32_t result =
319 Remote()->SendRequest(PasteboardServiceInterfaceCode::UNSUBSCRIBE_ALL_OBSERVER, data, reply, option);
320 if (result != ERR_NONE) {
321 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "failed, error code is: %{public}d", result);
322 }
323 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "end.");
324 }
325
ProcessObserver(uint32_t code,PasteboardObserverType type,const sptr<IPasteboardChangedObserver> & observer)326 void PasteboardServiceProxy::ProcessObserver(uint32_t code, PasteboardObserverType type,
327 const sptr<IPasteboardChangedObserver> &observer)
328 {
329 if (observer == nullptr) {
330 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "observer nullptr");
331 return;
332 }
333 MessageParcel data;
334 MessageParcel reply;
335 MessageOption option;
336 if (!data.WriteInterfaceToken(GetDescriptor())) {
337 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write descriptor to parcelable");
338 return;
339 }
340 if (!data.WriteUint32(static_cast<uint32_t>(type))) {
341 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write type to parcelable");
342 return;
343 }
344 if (!data.WriteRemoteObject(observer->AsObject())) {
345 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write observer to parcelable");
346 return;
347 }
348 int32_t result = Remote()->SendRequest(code, data, reply, option);
349 if (result != ERR_NONE) {
350 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "failed, error code is: %{public}d", result);
351 }
352 }
353
IsRemoteData()354 bool PasteboardServiceProxy::IsRemoteData()
355 {
356 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "start.");
357 MessageParcel data;
358 MessageParcel reply;
359 MessageOption option;
360 if (!data.WriteInterfaceToken(GetDescriptor())) {
361 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write parcelable");
362 return false;
363 }
364
365 int32_t ret = Remote()->SendRequest(PasteboardServiceInterfaceCode::IS_REMOTE_DATA, data, reply, option);
366 if (ret != ERR_NONE) {
367 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "failed, error code is: %{public}d", ret);
368 return false;
369 }
370 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "end.");
371 return reply.ReadBool();
372 }
373
GetDataSource(std::string & bundleName)374 int32_t PasteboardServiceProxy::GetDataSource(std::string &bundleName)
375 {
376 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "start.");
377 MessageParcel data;
378 MessageParcel reply;
379 MessageOption option;
380 if (!data.WriteInterfaceToken(GetDescriptor())) {
381 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write parcelable");
382 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
383 }
384 int32_t result = Remote()->SendRequest(PasteboardServiceInterfaceCode::GET_DATA_SOURCE, data, reply, option);
385 if (result != ERR_NONE) {
386 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "failed, error code is: %{public}d", result);
387 return result;
388 }
389 bundleName = reply.ReadString();
390 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "end.");
391 return reply.ReadInt32();
392 }
393
GetMimeTypes()394 std::vector<std::string> PasteboardServiceProxy::GetMimeTypes()
395 {
396 MessageParcel data;
397 MessageParcel reply;
398 MessageOption option;
399 if (!data.WriteInterfaceToken(GetDescriptor())) {
400 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write parcelable");
401 return {};
402 }
403 int32_t result = Remote()->SendRequest(PasteboardServiceInterfaceCode::GET_MIME_TYPES, data, reply, option);
404 if (result != ERR_NONE) {
405 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "failed, error code is: %{public}d", result);
406 return {};
407 }
408 uint32_t size = 0;
409 if (!reply.ReadUint32(size)) {
410 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to read size of mime types");
411 return {};
412 }
413 std::vector<std::string> mimeTypes;
414 for (uint32_t i = 0; i < size; i++) {
415 std::string type;
416 if (!reply.ReadString(type)) {
417 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to read mime type");
418 return {};
419 }
420 mimeTypes.push_back(type);
421 }
422 return mimeTypes;
423 }
424
HasDataType(const std::string & mimeType)425 bool PasteboardServiceProxy::HasDataType(const std::string &mimeType)
426 {
427 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "start.");
428 MessageParcel data;
429 MessageParcel reply;
430 MessageOption option;
431 if (!data.WriteInterfaceToken(GetDescriptor())) {
432 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write parcelable");
433 return false;
434 }
435 if (!data.WriteString(mimeType)) {
436 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write string");
437 return false;
438 }
439 int32_t result = Remote()->SendRequest(PasteboardServiceInterfaceCode::HAS_DATA_TYPE, data, reply, option);
440 if (result != ERR_NONE) {
441 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "failed, error code is: %{public}d", result);
442 return false;
443 }
444
445 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "end.");
446 return reply.ReadBool();
447 }
448
DetectPatterns(const std::set<Pattern> & patternsToCheck)449 std::set<Pattern> PasteboardServiceProxy::DetectPatterns(const std::set<Pattern> &patternsToCheck)
450 {
451 MessageParcel data;
452 MessageParcel reply;
453 MessageOption option;
454 if (!data.WriteInterfaceToken(GetDescriptor())) {
455 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write parcelable");
456 return {};
457 }
458 if (!data.WriteUint32(static_cast<uint32_t>(patternsToCheck.size()))) {
459 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write size of patterns to check");
460 return {};
461 }
462 for (const auto &pattern : patternsToCheck) {
463 if (!data.WriteUint32(static_cast<uint32_t>(pattern))) {
464 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write pattern to check");
465 return {};
466 }
467 }
468 int32_t result = Remote()->SendRequest(PasteboardServiceInterfaceCode::DETECT_PATTERNS, data, reply, option);
469 if (result != ERR_NONE) {
470 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "failed, error code is: %{public}d", result);
471 return {};
472 }
473 uint32_t size = 0;
474 if (!reply.ReadUint32(size)) {
475 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to read size of existed patterns");
476 return {};
477 }
478 std::set<Pattern> existedPatterns;
479 for (uint32_t i = 0; i < size; i++) {
480 uint32_t pattern;
481 if (!reply.ReadUint32(pattern)) {
482 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to read existed pattern");
483 return {};
484 }
485 existedPatterns.insert(static_cast<Pattern>(pattern));
486 }
487 return existedPatterns;
488 }
489
SetGlobalShareOption(const std::map<uint32_t,ShareOption> & globalShareOptions)490 int32_t PasteboardServiceProxy::SetGlobalShareOption(const std::map<uint32_t, ShareOption> &globalShareOptions)
491 {
492 MessageParcel data;
493 MessageParcel reply;
494 MessageOption option;
495 if (!data.WriteInterfaceToken(GetDescriptor())) {
496 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "WriteInterfaceToken failed.");
497 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
498 }
499 if (!data.WriteUint32(static_cast<uint32_t>(globalShareOptions.size()))) {
500 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Write size failed.");
501 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
502 }
503 for (const auto &[tokenId, shareOption] : globalShareOptions) {
504 if (!data.WriteUint32(tokenId)) {
505 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Write tokenId failed.");
506 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
507 }
508 if (!data.WriteInt32(static_cast<int32_t>(shareOption))) {
509 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Write shareOption failed.");
510 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
511 }
512 }
513 int32_t result = Remote()->SendRequest(
514 PasteboardServiceInterfaceCode::SET_GLOBAL_SHARE_OPTION, data, reply, option);
515 if (result != ERR_NONE) {
516 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "SendRequest failed, error code: %{public}d.", result);
517 return result;
518 }
519 return reply.ReadInt32();
520 }
521
RemoveGlobalShareOption(const std::vector<uint32_t> & tokenIds)522 int32_t PasteboardServiceProxy::RemoveGlobalShareOption(const std::vector<uint32_t> &tokenIds)
523 {
524 MessageParcel data;
525 MessageParcel reply;
526 MessageOption option;
527 if (!data.WriteInterfaceToken(GetDescriptor())) {
528 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "WriteInterfaceToken failed.");
529 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
530 }
531 if (!data.WriteUInt32Vector(tokenIds)) {
532 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Write tokenIds failed.");
533 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
534 }
535 int32_t result = Remote()->SendRequest(
536 PasteboardServiceInterfaceCode::REMOVE_GLOBAL_SHARE_OPTION, data, reply, option);
537 if (result != ERR_NONE) {
538 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "SendRequest failed, error code: %{public}d.", result);
539 return result;
540 }
541 return reply.ReadInt32();
542 }
543
GetGlobalShareOption(const std::vector<uint32_t> & tokenIds)544 std::map<uint32_t, ShareOption> PasteboardServiceProxy::GetGlobalShareOption(const std::vector<uint32_t> &tokenIds)
545 {
546 MessageParcel data;
547 MessageParcel reply;
548 MessageOption option;
549 if (!data.WriteInterfaceToken(GetDescriptor())) {
550 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "WriteInterfaceToken failed.");
551 return {};
552 }
553 if (!data.WriteUInt32Vector(tokenIds)) {
554 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Write tokenIds failed.");
555 return {};
556 }
557 int32_t result = Remote()->SendRequest(
558 PasteboardServiceInterfaceCode::GET_GLOBAL_SHARE_OPTION, data, reply, option);
559 if (result != ERR_NONE) {
560 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "SendRequest failed, error code: %{public}d.", result);
561 return {};
562 }
563 uint32_t size = 0;
564 if (!reply.ReadUint32(size)) {
565 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Read size failed.");
566 return {};
567 }
568 size_t readAbleSize = reply.GetReadableBytes();
569 if (size > readAbleSize || size > MAX_GET_GLOBAL_SHARE_OPTION_SIZE) {
570 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Read oversize failed.");
571 return {};
572 }
573 std::map<uint32_t, ShareOption> globalShareOptions;
574 for (uint32_t i = 0; i < size; i++) {
575 uint32_t tokenId;
576 if (!reply.ReadUint32(tokenId)) {
577 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Read tokenId failed.");
578 return {};
579 }
580 int32_t shareOption;
581 if (!reply.ReadInt32(shareOption)) {
582 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Read shareOption failed.");
583 return {};
584 }
585 globalShareOptions[tokenId] = static_cast<ShareOption>(shareOption);
586 }
587 return globalShareOptions;
588 }
589
SetAppShareOptions(const ShareOption & shareOptions)590 int32_t PasteboardServiceProxy::SetAppShareOptions(const ShareOption &shareOptions)
591 {
592 MessageParcel data;
593 MessageParcel reply;
594 MessageOption option;
595 if (!data.WriteInterfaceToken(GetDescriptor())) {
596 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Write interface token failed.");
597 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
598 }
599 if (!data.WriteInt32(shareOptions)) {
600 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Write share options failed.");
601 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
602 }
603 auto result = Remote()->SendRequest(PasteboardServiceInterfaceCode::SET_APP_SHARE_OPTIONS, data, reply, option);
604 if (result != ERR_NONE) {
605 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Send request failed, error code: %{public}d.", result);
606 return result;
607 }
608 return reply.ReadInt32();
609 }
610
RemoveAppShareOptions()611 int32_t PasteboardServiceProxy::RemoveAppShareOptions()
612 {
613 MessageParcel data;
614 MessageParcel reply;
615 MessageOption option;
616 if (!data.WriteInterfaceToken(GetDescriptor())) {
617 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Write interface token failed.");
618 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
619 }
620 auto result = Remote()->SendRequest(PasteboardServiceInterfaceCode::REMOVE_APP_SHARE_OPTIONS, data, reply, option);
621 if (result != ERR_NONE) {
622 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Send request failed, error code: %{public}d.", result);
623 return result;
624 }
625 return reply.ReadInt32();
626 }
627
PasteStart(const std::string & pasteId)628 void PasteboardServiceProxy::PasteStart(const std::string &pasteId)
629 {
630 MessageParcel data;
631 MessageParcel reply;
632 MessageOption option;
633 if (!data.WriteInterfaceToken(GetDescriptor())) {
634 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write parcelable");
635 return;
636 }
637 if (!data.WriteString(pasteId)) {
638 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write pasteId");
639 return;
640 }
641 int32_t result = Remote()->SendRequest(PasteboardServiceInterfaceCode::PASTE_START, data, reply, option);
642 if (result != ERR_NONE) {
643 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "failed, error code is: %{public}d", result);
644 }
645 }
646
PasteComplete(const std::string & deviceId,const std::string & pasteId)647 void PasteboardServiceProxy::PasteComplete(const std::string &deviceId, const std::string &pasteId)
648 {
649 MessageParcel data;
650 MessageParcel reply;
651 MessageOption option;
652 if (!data.WriteInterfaceToken(GetDescriptor())) {
653 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write parcelable");
654 return;
655 }
656 if (!data.WriteString(deviceId)) {
657 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write string");
658 return;
659 }
660 if (!data.WriteString(pasteId)) {
661 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write pasteId");
662 return;
663 }
664 int32_t result = Remote()->SendRequest(PasteboardServiceInterfaceCode::PASTE_COMPLETE, data, reply, option);
665 if (result != ERR_NONE) {
666 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "failed, error code is: %{public}d", result);
667 }
668 }
669
GetRemoteDeviceName(std::string & deviceName,bool & isRemote)670 int32_t PasteboardServiceProxy::GetRemoteDeviceName(std::string &deviceName, bool &isRemote)
671 {
672 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "start.");
673 MessageParcel data;
674 MessageParcel reply;
675 MessageOption option;
676 if (!data.WriteInterfaceToken(GetDescriptor())) {
677 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write parcelable");
678 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
679 }
680 int32_t result = Remote()->SendRequest(PasteboardServiceInterfaceCode::GET_REMOTE_DEVICE_NAME, data, reply, option);
681 if (result != ERR_NONE) {
682 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "failed, error code is: %{public}d", result);
683 return result;
684 }
685 deviceName = reply.ReadString();
686 isRemote = reply.ReadBool();
687 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "end.");
688 return reply.ReadInt32();
689 }
690
ShowProgress(const std::string & progressKey,const sptr<IRemoteObject> & observer)691 void PasteboardServiceProxy::ShowProgress(
692 const std::string &progressKey, const sptr<IRemoteObject> &observer)
693 {
694 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "start.");
695 MessageParcel data;
696 MessageParcel reply;
697 MessageOption option;
698 if (!data.WriteInterfaceToken(GetDescriptor())) {
699 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write parcelable");
700 return;
701 }
702 if (!data.WriteString(progressKey)) {
703 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write string");
704 return;
705 }
706 if (!data.WriteRemoteObject(observer)) {
707 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write observer");
708 return;
709 }
710 int32_t result = Remote()->SendRequest(PasteboardServiceInterfaceCode::SHOW_PROGRESS,
711 data, reply, option);
712 if (result != ERR_NONE) {
713 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "failed, error code is: %{public}d", result);
714 }
715 }
716
RegisterClientDeathObserver(sptr<IRemoteObject> observer)717 int32_t PasteboardServiceProxy::RegisterClientDeathObserver(sptr<IRemoteObject> observer)
718 {
719 if (observer == nullptr) {
720 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "observer is nullptr");
721 return static_cast<int32_t>(PasteboardError::INVALID_PARAM_ERROR);
722 }
723 MessageParcel data;
724 MessageParcel reply;
725 MessageOption option;
726 if (!data.WriteInterfaceToken(GetDescriptor())) {
727 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Write interface token failed.");
728 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
729 }
730 if (!data.WriteRemoteObject(observer)) {
731 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "remote observer failed.");
732 return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
733 }
734 auto result = Remote()->SendRequest(
735 PasteboardServiceInterfaceCode::REGISTER_CLIENT_DEATH_OBSERVER, data, reply, option);
736 if (result != ERR_NONE) {
737 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Send request failed, error code: %{public}d.", result);
738 return result;
739 }
740 return reply.ReadInt32();
741 }
742
743 } // namespace MiscServices
744 } // namespace OHOS