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