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