• 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 }
53 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)54 int32_t PasteboardServiceStub::OnRemoteRequest(
55     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
56 {
57     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start##code = %{public}u", code);
58     std::u16string myDescripter = PasteboardServiceStub::GetDescriptor();
59     std::u16string remoteDescripter = data.ReadInterfaceToken();
60     if (myDescripter != remoteDescripter) {
61         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "end##descriptor checked fail");
62         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
63     }
64     pid_t p = IPCSkeleton::GetCallingPid();
65     pid_t p1 = IPCSkeleton::GetCallingUid();
66     PASTEBOARD_HILOGI(
67         PASTEBOARD_MODULE_SERVICE, "CallingPid = %{public}d, CallingUid = %{public}d, code = %{public}u", p, p1, code);
68     auto itFunc = memberFuncMap_.find(code);
69     if (itFunc != memberFuncMap_.end()) {
70         auto memberFunc = itFunc->second;
71         if (memberFunc != nullptr) {
72             return (this->*memberFunc)(data, reply);
73         }
74     }
75     int ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
76     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end##ret = %{public}d", ret);
77     return ret;
78 }
OnClear(MessageParcel & data,MessageParcel & reply)79 int32_t PasteboardServiceStub::OnClear(MessageParcel &data, MessageParcel &reply)
80 {
81     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
82     Clear();
83     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
84     return ERR_OK;
85 }
86 
OnGetPasteData(MessageParcel & data,MessageParcel & reply)87 int32_t PasteboardServiceStub::OnGetPasteData(MessageParcel &data, MessageParcel &reply)
88 {
89     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " start.");
90     PasteData pasteData{};
91     auto result = GetPasteData(pasteData);
92     HiViewAdapter::ReportUseBehaviour(pasteData, HiViewAdapter::PASTE_STATE, result);
93     std::vector<uint8_t> pasteDataTlv(0);
94     bool ret = pasteData.Encode(pasteDataTlv);
95     if (!ret) {
96         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to encode pastedata in TLV");
97         return ERR_INVALID_VALUE;
98     }
99     if (!reply.WriteInt32(pasteDataTlv.size())) {
100         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to write raw size");
101         return ERR_INVALID_VALUE;
102     }
103     if (!reply.WriteRawData(pasteDataTlv.data(), pasteDataTlv.size())) {
104         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to write raw data");
105         return ERR_INVALID_VALUE;
106     }
107     PasteUriHandler pasteUriHandler;
108     if (!pasteData.WriteUriFd(reply, pasteUriHandler, false)) {
109         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to write uri fd");
110         return ERR_INVALID_VALUE;
111     }
112     if (!reply.WriteInt32(result)) {
113         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to GetPasteData result");
114         return ERR_INVALID_VALUE;
115     }
116     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " end.");
117     return ERR_OK;
118 }
OnHasPasteData(MessageParcel & data,MessageParcel & reply)119 int32_t PasteboardServiceStub::OnHasPasteData(MessageParcel &data, MessageParcel &reply)
120 {
121     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " start.");
122     auto result = HasPasteData();
123     reply.WriteBool(result);
124     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " end.");
125     return ERR_OK;
126 }
127 
OnSetPasteData(MessageParcel & data,MessageParcel & reply)128 int32_t PasteboardServiceStub::OnSetPasteData(MessageParcel &data, MessageParcel &reply)
129 {
130     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " start.");
131     int32_t rawDataSize = data.ReadInt32();
132     if (rawDataSize <= 0) {
133         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to read raw size");
134         return ERR_INVALID_VALUE;
135     }
136     auto *rawData = (uint8_t *)data.ReadRawData(rawDataSize);
137     if (rawData == nullptr) {
138         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to get raw data");
139         return ERR_INVALID_VALUE;
140     }
141     std::vector<uint8_t> pasteDataTlv(rawData, rawData + rawDataSize);
142     auto pasteData = std::make_shared<PasteData>();
143     bool ret = pasteData->Decode(pasteDataTlv);
144     if (!ret) {
145         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to decode pastedata in TLV");
146         return ERR_INVALID_VALUE;
147     }
148     CopyUriHandler copyUriHandler;
149     if (!pasteData->ReadUriFd(data, copyUriHandler)) {
150         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to read uri fd");
151         return ERR_INVALID_VALUE;
152     }
153     int32_t result = SavePasteData(pasteData);
154     HiViewAdapter::ReportUseBehaviour(*pasteData, HiViewAdapter::COPY_STATE, result);
155     if (!reply.WriteInt32(result)) {
156         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to write SetPasteData result");
157         return ERR_INVALID_VALUE;
158     }
159     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " end.");
160     return ERR_OK;
161 }
OnAddPasteboardChangedObserver(MessageParcel & data,MessageParcel & reply)162 int32_t PasteboardServiceStub::OnAddPasteboardChangedObserver(MessageParcel &data, MessageParcel &reply)
163 {
164     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
165     sptr<IPasteboardChangedObserver> callback;
166     if (!IsObserverValid(data, callback)) {
167         return ERR_INVALID_VALUE;
168     }
169 
170     AddPasteboardChangedObserver(callback);
171     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
172     return ERR_OK;
173 }
OnRemovePasteboardChangedObserver(MessageParcel & data,MessageParcel & reply)174 int32_t PasteboardServiceStub::OnRemovePasteboardChangedObserver(MessageParcel &data, MessageParcel &reply)
175 {
176     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
177     sptr<IPasteboardChangedObserver> callback;
178     if (!IsObserverValid(data, callback)) {
179         return ERR_INVALID_VALUE;
180     }
181     RemovePasteboardChangedObserver(callback);
182     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
183     return ERR_OK;
184 }
185 
OnRemoveAllChangedObserver(MessageParcel & data,MessageParcel & reply)186 int32_t PasteboardServiceStub::OnRemoveAllChangedObserver(MessageParcel &data, MessageParcel &reply)
187 {
188     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
189     RemoveAllChangedObserver();
190     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
191     return ERR_OK;
192 }
193 
OnAddPasteboardEventObserver(MessageParcel & data,MessageParcel & reply)194 int32_t PasteboardServiceStub::OnAddPasteboardEventObserver(MessageParcel &data, MessageParcel &reply)
195 {
196     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
197     sptr<IPasteboardChangedObserver> callback;
198     if (!IsObserverValid(data, callback)) {
199         return ERR_INVALID_VALUE;
200     }
201 
202     AddPasteboardEventObserver(callback);
203     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
204     return ERR_OK;
205 }
206 
OnRemovePasteboardEventObserver(MessageParcel & data,MessageParcel & reply)207 int32_t PasteboardServiceStub::OnRemovePasteboardEventObserver(MessageParcel &data, MessageParcel &reply)
208 {
209     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
210     sptr<IPasteboardChangedObserver> callback;
211     if (!IsObserverValid(data, callback)) {
212         return ERR_INVALID_VALUE;
213     }
214     RemovePasteboardEventObserver(callback);
215     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
216     return ERR_OK;
217 }
218 
OnRemoveAllEventObserver(MessageParcel & data,MessageParcel & reply)219 int32_t PasteboardServiceStub::OnRemoveAllEventObserver(MessageParcel &data, MessageParcel &reply)
220 {
221     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
222     RemoveAllEventObserver();
223     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
224     return ERR_OK;
225 }
226 
IsObserverValid(MessageParcel & data,sptr<IPasteboardChangedObserver> & callback)227 inline bool PasteboardServiceStub::IsObserverValid(MessageParcel &data, sptr<IPasteboardChangedObserver> &callback)
228 {
229     sptr<IRemoteObject> obj = data.ReadRemoteObject();
230     if (obj == nullptr) {
231         PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "obj nullptr");
232         return false;
233     }
234     callback = iface_cast<IPasteboardChangedObserver>(obj);
235     if (callback == nullptr) {
236         PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "callback nullptr");
237         return false;
238     }
239     return true;
240 }
241 
~PasteboardServiceStub()242 PasteboardServiceStub::~PasteboardServiceStub()
243 {
244     memberFuncMap_.clear();
245 }
246 } // namespace MiscServices
247 } // namespace OHOS
248