• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::ADD_CHANGED_OBSERVER)] =
41         &PasteboardServiceStub::OnAddPasteboardChangedObserver;
42     memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::DELETE_CHANGED_OBSERVER)] =
43         &PasteboardServiceStub::OnRemovePasteboardChangedObserver;
44     memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::DELETE_ALL_CHANGED_OBSERVER)] =
45         &PasteboardServiceStub::OnRemoveAllChangedObserver;
46     memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::ADD_EVENT_OBSERVER)] =
47         &PasteboardServiceStub::OnAddPasteboardEventObserver;
48     memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::DELETE_EVENT_OBSERVER)] =
49         &PasteboardServiceStub::OnRemovePasteboardEventObserver;
50     memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::DELETE_ALL_EVENT_OBSERVER)] =
51         &PasteboardServiceStub::OnRemoveAllEventObserver;
52     memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::IS_REMOTE_DATA)] =
53             &PasteboardServiceStub::OnIsRemoteData;
54     memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::GET_DATA_SOURCE)] =
55             &PasteboardServiceStub::OnGetDataSource;
56     memberFuncMap_[static_cast<uint32_t>(PasteboardServiceInterfaceCode::HAS_DATA_TYPE)] =
57             &PasteboardServiceStub::OnHasDataType;
58 }
59 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)60 int32_t PasteboardServiceStub::OnRemoteRequest(
61     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
62 {
63     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start##code = %{public}u", code);
64     std::u16string myDescripter = PasteboardServiceStub::GetDescriptor();
65     std::u16string remoteDescripter = data.ReadInterfaceToken();
66     if (myDescripter != remoteDescripter) {
67         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "end##descriptor checked fail");
68         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
69     }
70     pid_t p = IPCSkeleton::GetCallingPid();
71     pid_t p1 = IPCSkeleton::GetCallingUid();
72     PASTEBOARD_HILOGI(
73         PASTEBOARD_MODULE_SERVICE, "CallingPid = %{public}d, CallingUid = %{public}d, code = %{public}u", p, p1, code);
74     auto itFunc = memberFuncMap_.find(code);
75     if (itFunc != memberFuncMap_.end()) {
76         auto memberFunc = itFunc->second;
77         if (memberFunc != nullptr) {
78             return (this->*memberFunc)(data, reply);
79         }
80     }
81     int ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
82     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end##ret = %{public}d", ret);
83     return ret;
84 }
OnClear(MessageParcel & data,MessageParcel & reply)85 int32_t PasteboardServiceStub::OnClear(MessageParcel &data, MessageParcel &reply)
86 {
87     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
88     Clear();
89     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
90     return ERR_OK;
91 }
92 
OnGetPasteData(MessageParcel & data,MessageParcel & reply)93 int32_t PasteboardServiceStub::OnGetPasteData(MessageParcel &data, MessageParcel &reply)
94 {
95     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " start.");
96     PasteData pasteData{};
97     auto result = GetPasteData(pasteData);
98     HiViewAdapter::ReportUseBehaviour(pasteData, HiViewAdapter::PASTE_STATE, result);
99     std::vector<uint8_t> pasteDataTlv(0);
100     bool ret = pasteData.Encode(pasteDataTlv);
101     if (!ret) {
102         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to encode pastedata in TLV");
103         return ERR_INVALID_VALUE;
104     }
105     if (!reply.WriteInt32(pasteDataTlv.size())) {
106         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to write raw size");
107         return ERR_INVALID_VALUE;
108     }
109     if (!reply.WriteRawData(pasteDataTlv.data(), pasteDataTlv.size())) {
110         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to write raw data");
111         return ERR_INVALID_VALUE;
112     }
113     PasteUriHandler pasteUriHandler;
114     if (!pasteData.WriteUriFd(reply, pasteUriHandler, false)) {
115         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to write uri fd");
116         return ERR_INVALID_VALUE;
117     }
118     if (!reply.WriteInt32(result)) {
119         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to GetPasteData result");
120         return ERR_INVALID_VALUE;
121     }
122     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " end.");
123     return ERR_OK;
124 }
OnHasPasteData(MessageParcel & data,MessageParcel & reply)125 int32_t PasteboardServiceStub::OnHasPasteData(MessageParcel &data, MessageParcel &reply)
126 {
127     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " start.");
128     auto result = HasPasteData();
129     reply.WriteBool(result);
130     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " end.");
131     return ERR_OK;
132 }
133 
OnSetPasteData(MessageParcel & data,MessageParcel & reply)134 int32_t PasteboardServiceStub::OnSetPasteData(MessageParcel &data, MessageParcel &reply)
135 {
136     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " start.");
137     int32_t rawDataSize = data.ReadInt32();
138     if (rawDataSize <= 0) {
139         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to read raw size");
140         return ERR_INVALID_VALUE;
141     }
142     auto *rawData = (uint8_t *)data.ReadRawData(rawDataSize);
143     if (rawData == nullptr) {
144         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to get raw data");
145         return ERR_INVALID_VALUE;
146     }
147     std::vector<uint8_t> pasteDataTlv(rawData, rawData + rawDataSize);
148     auto pasteData = std::make_shared<PasteData>();
149     bool ret = pasteData->Decode(pasteDataTlv);
150     if (!ret) {
151         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to decode pastedata in TLV");
152         return ERR_INVALID_VALUE;
153     }
154     CopyUriHandler copyUriHandler;
155     if (!pasteData->ReadUriFd(data, copyUriHandler)) {
156         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to read uri fd");
157         return ERR_INVALID_VALUE;
158     }
159     int32_t result = SavePasteData(pasteData);
160     HiViewAdapter::ReportUseBehaviour(*pasteData, HiViewAdapter::COPY_STATE, result);
161     if (!reply.WriteInt32(result)) {
162         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to write SetPasteData result");
163         return ERR_INVALID_VALUE;
164     }
165     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " end.");
166     return ERR_OK;
167 }
OnAddPasteboardChangedObserver(MessageParcel & data,MessageParcel & reply)168 int32_t PasteboardServiceStub::OnAddPasteboardChangedObserver(MessageParcel &data, MessageParcel &reply)
169 {
170     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
171     sptr<IPasteboardChangedObserver> callback;
172     if (!IsObserverValid(data, callback)) {
173         return ERR_INVALID_VALUE;
174     }
175 
176     AddPasteboardChangedObserver(callback);
177     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
178     return ERR_OK;
179 }
OnRemovePasteboardChangedObserver(MessageParcel & data,MessageParcel & reply)180 int32_t PasteboardServiceStub::OnRemovePasteboardChangedObserver(MessageParcel &data, MessageParcel &reply)
181 {
182     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
183     sptr<IPasteboardChangedObserver> callback;
184     if (!IsObserverValid(data, callback)) {
185         return ERR_INVALID_VALUE;
186     }
187     RemovePasteboardChangedObserver(callback);
188     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
189     return ERR_OK;
190 }
191 
OnRemoveAllChangedObserver(MessageParcel & data,MessageParcel & reply)192 int32_t PasteboardServiceStub::OnRemoveAllChangedObserver(MessageParcel &data, MessageParcel &reply)
193 {
194     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
195     RemoveAllChangedObserver();
196     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
197     return ERR_OK;
198 }
199 
OnAddPasteboardEventObserver(MessageParcel & data,MessageParcel & reply)200 int32_t PasteboardServiceStub::OnAddPasteboardEventObserver(MessageParcel &data, MessageParcel &reply)
201 {
202     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
203     sptr<IPasteboardChangedObserver> callback;
204     if (!IsObserverValid(data, callback)) {
205         return ERR_INVALID_VALUE;
206     }
207 
208     AddPasteboardEventObserver(callback);
209     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
210     return ERR_OK;
211 }
212 
OnRemovePasteboardEventObserver(MessageParcel & data,MessageParcel & reply)213 int32_t PasteboardServiceStub::OnRemovePasteboardEventObserver(MessageParcel &data, MessageParcel &reply)
214 {
215     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
216     sptr<IPasteboardChangedObserver> callback;
217     if (!IsObserverValid(data, callback)) {
218         return ERR_INVALID_VALUE;
219     }
220     RemovePasteboardEventObserver(callback);
221     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
222     return ERR_OK;
223 }
224 
OnRemoveAllEventObserver(MessageParcel & data,MessageParcel & reply)225 int32_t PasteboardServiceStub::OnRemoveAllEventObserver(MessageParcel &data, MessageParcel &reply)
226 {
227     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
228     RemoveAllEventObserver();
229     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
230     return ERR_OK;
231 }
232 
IsObserverValid(MessageParcel & data,sptr<IPasteboardChangedObserver> & callback)233 inline bool PasteboardServiceStub::IsObserverValid(MessageParcel &data, sptr<IPasteboardChangedObserver> &callback)
234 {
235     sptr<IRemoteObject> obj = data.ReadRemoteObject();
236     if (obj == nullptr) {
237         PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "obj nullptr");
238         return false;
239     }
240     callback = iface_cast<IPasteboardChangedObserver>(obj);
241     if (callback == nullptr) {
242         PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "callback nullptr");
243         return false;
244     }
245     return true;
246 }
247 
OnIsRemoteData(MessageParcel & data,MessageParcel & reply)248 int32_t PasteboardServiceStub::OnIsRemoteData(MessageParcel &data, MessageParcel &reply)
249 {
250     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
251     auto result = IsRemoteData();
252     reply.WriteBool(result);
253     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
254     return ERR_OK;
255 }
256 
OnGetDataSource(MessageParcel & data,MessageParcel & reply)257 int32_t PasteboardServiceStub::OnGetDataSource(MessageParcel &data, MessageParcel &reply)
258 {
259     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
260     std::string bundleName;
261     auto ret = GetDataSource(bundleName);
262     if (bundleName.empty() || bundleName.length() > MAX_BUNDLE_NAME_LENGTH) {
263         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to get bundleName");
264         return ERR_INVALID_VALUE;
265     }
266     if (!reply.WriteString(bundleName)) {
267         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to writeName result");
268         return ERR_INVALID_VALUE;
269     }
270     if (!reply.WriteInt32(ret)) {
271         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to OnGetResourceApp result");
272         return ERR_INVALID_VALUE;
273     }
274     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
275     return ERR_OK;
276 }
277 
OnHasDataType(MessageParcel & data,MessageParcel & reply)278 int32_t PasteboardServiceStub::OnHasDataType(MessageParcel &data, MessageParcel &reply)
279 {
280     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
281     std::string mimeType = data.ReadString();
282     auto ret = HasDataType(mimeType);
283     reply.WriteBool(ret);
284     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
285     return ERR_OK;
286 }
287 
~PasteboardServiceStub()288 PasteboardServiceStub::~PasteboardServiceStub()
289 {
290     memberFuncMap_.clear();
291 }
292 } // namespace MiscServices
293 } // namespace OHOS
294