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 
GetMimeTypes()329 std::vector<std::string> PasteboardServiceProxy::GetMimeTypes()
330 {
331     MessageParcel data;
332     MessageParcel reply;
333     MessageOption option;
334     if (!data.WriteInterfaceToken(GetDescriptor())) {
335         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write parcelable");
336         return {};
337     }
338     int32_t result = Remote()->SendRequest(PasteboardServiceInterfaceCode::GET_MIME_TYPES, data, reply, option);
339     if (result != ERR_NONE) {
340         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "failed, error code is: %{public}d", result);
341         return {};
342     }
343     uint32_t size = 0;
344     if (!reply.ReadUint32(size)) {
345         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to read size of mime types");
346         return {};
347     }
348     std::vector<std::string> mimeTypes;
349     for (uint32_t i = 0; i < size; i++) {
350         std::string type;
351         if (!reply.ReadString(type)) {
352             PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to read mime type");
353             return {};
354         }
355         mimeTypes.push_back(type);
356     }
357     return mimeTypes;
358 }
359 
HasDataType(const std::string & mimeType)360 bool PasteboardServiceProxy::HasDataType(const std::string &mimeType)
361 {
362     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "start.");
363     MessageParcel data;
364     MessageParcel reply;
365     MessageOption option;
366     if (!data.WriteInterfaceToken(GetDescriptor())) {
367         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write parcelable");
368         return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
369     }
370     if (!data.WriteString(mimeType)) {
371         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write string");
372         return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
373     }
374     int32_t result = Remote()->SendRequest(PasteboardServiceInterfaceCode::HAS_DATA_TYPE, data, reply, option);
375     if (result != ERR_NONE) {
376         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "failed, error code is: %{public}d", result);
377         return result;
378     }
379 
380     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "end.");
381     return reply.ReadBool();
382 }
383 
DetectPatterns(const std::set<Pattern> & patternsToCheck)384 std::set<Pattern> PasteboardServiceProxy::DetectPatterns(const std::set<Pattern> &patternsToCheck)
385 {
386     MessageParcel data;
387     MessageParcel reply;
388     MessageOption option;
389     if (!data.WriteInterfaceToken(GetDescriptor())) {
390         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write parcelable");
391         return {};
392     }
393     if (!data.WriteUint32(static_cast<uint32_t>(patternsToCheck.size()))) {
394         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write size of patterns to check");
395         return {};
396     }
397     for (const auto &pattern : patternsToCheck) {
398         if (!data.WriteUint32(static_cast<uint32_t>(pattern))) {
399             PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write pattern to check");
400             return {};
401         }
402     }
403     int32_t result = Remote()->SendRequest(PasteboardServiceInterfaceCode::DETECT_PATTERNS, 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 existed patterns");
411         return {};
412     }
413     std::set<Pattern> existedPatterns;
414     for (uint32_t i = 0; i < size; i++) {
415         uint32_t pattern;
416         if (!reply.ReadUint32(pattern)) {
417             PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to read existed pattern");
418             return {};
419         }
420         existedPatterns.insert(static_cast<Pattern>(pattern));
421     }
422     return existedPatterns;
423 }
424 
SetGlobalShareOption(const std::map<uint32_t,ShareOption> & globalShareOptions)425 int32_t PasteboardServiceProxy::SetGlobalShareOption(const std::map<uint32_t, ShareOption> &globalShareOptions)
426 {
427     MessageParcel data;
428     MessageParcel reply;
429     MessageOption option;
430     if (!data.WriteInterfaceToken(GetDescriptor())) {
431         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "WriteInterfaceToken failed.");
432         return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
433     }
434     if (!data.WriteUint32(static_cast<uint32_t>(globalShareOptions.size()))) {
435         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Write size failed.");
436         return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
437     }
438     for (const auto &[tokenId, shareOption] : globalShareOptions) {
439         if (!data.WriteUint32(tokenId)) {
440             PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Write tokenId failed.");
441             return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
442         }
443         if (!data.WriteInt32(static_cast<int32_t>(shareOption))) {
444             PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Write shareOption failed.");
445             return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
446         }
447     }
448     int32_t result = Remote()->SendRequest(
449         PasteboardServiceInterfaceCode::SET_GLOBAL_SHARE_OPTION, data, reply, option);
450     if (result != ERR_NONE) {
451         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "SendRequest failed, error code: %{public}d.", result);
452         return result;
453     }
454     return reply.ReadInt32();
455 }
456 
RemoveGlobalShareOption(const std::vector<uint32_t> & tokenIds)457 int32_t PasteboardServiceProxy::RemoveGlobalShareOption(const std::vector<uint32_t> &tokenIds)
458 {
459     MessageParcel data;
460     MessageParcel reply;
461     MessageOption option;
462     if (!data.WriteInterfaceToken(GetDescriptor())) {
463         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "WriteInterfaceToken failed.");
464         return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
465     }
466     if (!data.WriteUInt32Vector(tokenIds)) {
467         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Write tokenIds failed.");
468         return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
469     }
470     int32_t result = Remote()->SendRequest(
471         PasteboardServiceInterfaceCode::REMOVE_GLOBAL_SHARE_OPTION, data, reply, option);
472     if (result != ERR_NONE) {
473         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "SendRequest failed, error code: %{public}d.", result);
474         return result;
475     }
476     return reply.ReadInt32();
477 }
478 
GetGlobalShareOption(const std::vector<uint32_t> & tokenIds)479 std::map<uint32_t, ShareOption> PasteboardServiceProxy::GetGlobalShareOption(const std::vector<uint32_t> &tokenIds)
480 {
481     MessageParcel data;
482     MessageParcel reply;
483     MessageOption option;
484     if (!data.WriteInterfaceToken(GetDescriptor())) {
485         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "WriteInterfaceToken failed.");
486         return {};
487     }
488     if (!data.WriteUInt32Vector(tokenIds)) {
489         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Write tokenIds failed.");
490         return {};
491     }
492     int32_t result = Remote()->SendRequest(
493         PasteboardServiceInterfaceCode::GET_GLOBAL_SHARE_OPTION, data, reply, option);
494     if (result != ERR_NONE) {
495         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "SendRequest failed, error code: %{public}d.", result);
496         return {};
497     }
498     uint32_t size = 0;
499     if (!reply.ReadUint32(size)) {
500         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Read size failed.");
501         return {};
502     }
503     size_t readAbleSize = reply.GetReadableBytes();
504     if (size > readAbleSize || size > MAX_GET_GLOBAL_SHARE_OPTION_SIZE) {
505         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Read oversize failed.");
506         return {};
507     }
508     std::map<uint32_t, ShareOption> globalShareOptions;
509     for (uint32_t i = 0; i < size; i++) {
510         uint32_t tokenId;
511         if (!reply.ReadUint32(tokenId)) {
512             PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Read tokenId failed.");
513             return {};
514         }
515         int32_t shareOption;
516         if (!reply.ReadInt32(shareOption)) {
517             PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Read shareOption failed.");
518             return {};
519         }
520         globalShareOptions[tokenId] = static_cast<ShareOption>(shareOption);
521     }
522     return globalShareOptions;
523 }
524 
SetAppShareOptions(const ShareOption & shareOptions)525 int32_t PasteboardServiceProxy::SetAppShareOptions(const ShareOption &shareOptions)
526 {
527     MessageParcel data;
528     MessageParcel reply;
529     MessageOption option;
530     if (!data.WriteInterfaceToken(GetDescriptor())) {
531         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Write interface token failed.");
532         return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
533     }
534     if (!data.WriteInt32(shareOptions)) {
535         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Write share options failed.");
536         return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
537     }
538     auto result = Remote()->SendRequest(PasteboardServiceInterfaceCode::SET_APP_SHARE_OPTIONS, data, reply, option);
539     if (result != ERR_NONE) {
540         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Send request failed, error code: %{public}d.", result);
541         return result;
542     }
543     return reply.ReadInt32();
544 }
545 
RemoveAppShareOptions()546 int32_t PasteboardServiceProxy::RemoveAppShareOptions()
547 {
548     MessageParcel data;
549     MessageParcel reply;
550     MessageOption option;
551     if (!data.WriteInterfaceToken(GetDescriptor())) {
552         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Write interface token failed.");
553         return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
554     }
555     auto result = Remote()->SendRequest(PasteboardServiceInterfaceCode::REMOVE_APP_SHARE_OPTIONS, data, reply, option);
556     if (result != ERR_NONE) {
557         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Send request failed, error code: %{public}d.", result);
558         return result;
559     }
560     return reply.ReadInt32();
561 }
562 
PasteStart(const std::string & pasteId)563 void PasteboardServiceProxy::PasteStart(const std::string &pasteId)
564 {
565     MessageParcel data;
566     MessageParcel reply;
567     MessageOption option;
568     if (!data.WriteInterfaceToken(GetDescriptor())) {
569         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write parcelable");
570         return;
571     }
572     if (!data.WriteString(pasteId)) {
573         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write pasteId");
574         return;
575     }
576     int32_t result = Remote()->SendRequest(PasteboardServiceInterfaceCode::PASTE_START, data, reply, option);
577     if (result != ERR_NONE) {
578         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "failed, error code is: %{public}d", result);
579     }
580 }
581 
PasteComplete(const std::string & deviceId,const std::string & pasteId)582 void PasteboardServiceProxy::PasteComplete(const std::string &deviceId, const std::string &pasteId)
583 {
584     MessageParcel data;
585     MessageParcel reply;
586     MessageOption option;
587     if (!data.WriteInterfaceToken(GetDescriptor())) {
588         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write parcelable");
589         return;
590     }
591     if (!data.WriteString(deviceId)) {
592         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write string");
593         return;
594     }
595     if (!data.WriteString(pasteId)) {
596         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write pasteId");
597         return;
598     }
599     int32_t result = Remote()->SendRequest(PasteboardServiceInterfaceCode::PASTE_COMPLETE, data, reply, option);
600     if (result != ERR_NONE) {
601         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "failed, error code is: %{public}d", result);
602     }
603 }
604 
RegisterClientDeathObserver(sptr<IRemoteObject> observer)605 int32_t PasteboardServiceProxy::RegisterClientDeathObserver(sptr<IRemoteObject> observer)
606 {
607     if (observer == nullptr) {
608         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "observer is nullptr");
609         return static_cast<int32_t>(PasteboardError::INVALID_PARAM_ERROR);
610     }
611     MessageParcel data;
612     MessageParcel reply;
613     MessageOption option;
614     if (!data.WriteInterfaceToken(GetDescriptor())) {
615         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Write interface token failed.");
616         return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
617     }
618     if (!data.WriteRemoteObject(observer)) {
619         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "remote observer failed.");
620         return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
621     }
622     auto result = Remote()->SendRequest(
623         PasteboardServiceInterfaceCode::REGISTER_CLIENT_DEATH_OBSERVER, data, reply, option);
624     if (result != ERR_NONE) {
625         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Send request failed, error code: %{public}d.", result);
626         return result;
627     }
628     return reply.ReadInt32();
629 }
630 
631 } // namespace MiscServices
632 } // namespace OHOS