1  /*
2  * Copyright (c) 2024 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 "message_option.h"
17  #include "message_parcel.h"
18  #include "pasteboard_entry_getter_proxy.h"
19  #include "pasteboard_error.h"
20  #include "pasteboard_hilog.h"
21  #include "pasteboard_serv_ipc_interface_code.h"
22  
23  namespace OHOS {
24  namespace MiscServices {
25  using namespace OHOS::Security::PasteboardServ;
PasteboardEntryGetterProxy(const sptr<IRemoteObject> & object)26  PasteboardEntryGetterProxy::PasteboardEntryGetterProxy(const sptr<IRemoteObject> &object)
27      : IRemoteProxy<IPasteboardEntryGetter>(object)
28  {
29  }
30  
MakeRequest(uint32_t recordId,PasteDataEntry & value,MessageParcel & request)31  int32_t PasteboardEntryGetterProxy::MakeRequest(uint32_t recordId, PasteDataEntry& value, MessageParcel& request)
32  {
33      if (!request.WriteInterfaceToken(GetDescriptor())) {
34          PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "write descriptor failed");
35          return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
36      }
37      if (!request.WriteUint32(recordId)) {
38          PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "write recordId failed");
39          return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
40      }
41      std::vector<uint8_t> sendEntryTLV(0);
42      if (!value.Marshalling(sendEntryTLV)) {
43          PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "marshall entry value failed");
44          return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
45      }
46      if (!request.WriteInt32(sendEntryTLV.size())) {
47          PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "write entry tlv raw data size failed");
48          return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
49      }
50      if (!request.WriteRawData(sendEntryTLV.data(), sendEntryTLV.size())) {
51          PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "write entry tlv raw data failed");
52          return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
53      }
54      return static_cast<int32_t>(PasteboardError::E_OK);
55  }
56  
GetRecordValueByType(uint32_t recordId,PasteDataEntry & value)57  int32_t PasteboardEntryGetterProxy::GetRecordValueByType(uint32_t recordId, PasteDataEntry& value)
58  {
59      MessageParcel request;
60      auto res = MakeRequest(recordId, value, request);
61      if (res != static_cast<int32_t>(PasteboardError::E_OK)) {
62          return res;
63      }
64      MessageParcel reply;
65      MessageOption option;
66      int result = Remote()->SendRequest(
67          static_cast<int>(PasteboardEntryGetterInterfaceCode::GET_RECORD_VALUE_BY_TYPE), request, reply, option);
68      if (result != ERR_OK) {
69          PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "send request failed, error:%{public}d", result);
70          return result;
71      }
72      res = reply.ReadInt32();
73      int32_t rawDataSize = reply.ReadInt32();
74      if (rawDataSize <= 0) {
75          PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "read entry tlv raw data size failed");
76          return static_cast<int32_t>(PasteboardError::DESERIALIZATION_ERROR);
77      }
78      const uint8_t *rawData = reinterpret_cast<const uint8_t *>(reply.ReadRawData(rawDataSize));
79      if (rawData == nullptr) {
80          PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "read entry tlv raw data failed");
81          return static_cast<int32_t>(PasteboardError::DESERIALIZATION_ERROR);
82      }
83      std::vector<uint8_t> recvEntryTlv(rawData, rawData + rawDataSize);
84      PasteDataEntry entryValue;
85      if (!entryValue.Unmarshalling(recvEntryTlv)) {
86          PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "unmarshall entry value failed");
87          return static_cast<int32_t>(PasteboardError::DESERIALIZATION_ERROR);
88      }
89      value = entryValue;
90      return res;
91  }
92  } // namespace MiscServices
93  } // namespace OHOS