1 /*
2 * Copyright (C) 2021 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 "errors.h"
19 #include "paste_data.h"
20 #include "pasteboard_error.h"
21 #include "pasteboard_hilog.h"
22 #include "pasteboard_observer_proxy.h"
23 #include "paste_uri_handler.h"
24 #include "copy_uri_handler.h"
25
26 namespace OHOS {
27 namespace MiscServices {
PasteboardServiceStub()28 PasteboardServiceStub::PasteboardServiceStub()
29 {
30 memberFuncMap_[static_cast<uint32_t>(GET_PASTE_DATA)] = &PasteboardServiceStub::OnGetPasteData;
31 memberFuncMap_[static_cast<uint32_t>(HAS_PASTE_DATA)] = &PasteboardServiceStub::OnHasPasteData;
32 memberFuncMap_[static_cast<uint32_t>(SET_PASTE_DATA)] = &PasteboardServiceStub::OnSetPasteData;
33 memberFuncMap_[static_cast<uint32_t>(CLEAR_ALL)] = &PasteboardServiceStub::OnClear;
34 memberFuncMap_[static_cast<uint32_t>(ADD_OBSERVER)] = &PasteboardServiceStub::OnAddPasteboardChangedObserver;
35 memberFuncMap_[static_cast<uint32_t>(DELETE_OBSERVER)] = &PasteboardServiceStub::OnRemovePasteboardChangedObserver;
36 memberFuncMap_[static_cast<uint32_t>(DELETE_ALL_OBSERVER)] = &PasteboardServiceStub::OnRemoveAllChangedObserver;
37 }
38
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)39 int32_t PasteboardServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
40 MessageOption &option)
41 {
42 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start##code = %{public}u", code);
43 std::u16string myDescripter = PasteboardServiceStub::GetDescriptor();
44 std::u16string remoteDescripter = data.ReadInterfaceToken();
45 if (myDescripter != remoteDescripter) {
46 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "end##descriptor checked fail");
47 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
48 }
49 pid_t p = IPCSkeleton::GetCallingPid();
50 pid_t p1 = IPCSkeleton::GetCallingUid();
51 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "CallingPid = %{public}d, CallingUid = %{public}d, code = %{public}u",
52 p, p1, code);
53 auto itFunc = memberFuncMap_.find(code);
54 if (itFunc != memberFuncMap_.end()) {
55 auto memberFunc = itFunc->second;
56 if (memberFunc != nullptr) {
57 return (this->*memberFunc)(data, reply);
58 }
59 }
60 int ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
61 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end##ret = %{public}d", ret);
62 return ret;
63 }
OnClear(MessageParcel & data,MessageParcel & reply)64 int32_t PasteboardServiceStub::OnClear(MessageParcel &data, MessageParcel &reply)
65 {
66 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
67 Clear();
68 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
69 return ERR_OK;
70 }
71
OnGetPasteData(MessageParcel & data,MessageParcel & reply)72 int32_t PasteboardServiceStub::OnGetPasteData(MessageParcel &data, MessageParcel &reply)
73 {
74 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " start.");
75 PasteData pasteData{};
76 auto result = GetPasteData(pasteData);
77 std::vector<uint8_t> pasteDataTlv(0);
78 bool ret = pasteData.Encode(pasteDataTlv);
79 if (!ret) {
80 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to encode pastedata in TLV");
81 return ERR_INVALID_VALUE;
82 }
83 if (!reply.WriteInt32(pasteDataTlv.size())) {
84 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to write raw size");
85 return ERR_INVALID_VALUE;
86 }
87 if (!reply.WriteRawData(pasteDataTlv.data(), pasteDataTlv.size())) {
88 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to write raw data");
89 return ERR_INVALID_VALUE;
90 }
91 PasteUriHandler pasteUriHandler;
92 if (!pasteData.WriteUriFd(reply, pasteUriHandler, false)) {
93 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to write uri fd");
94 return ERR_INVALID_VALUE;
95 }
96 if (!reply.WriteInt32(result)) {
97 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to GetPasteData result");
98 return ERR_INVALID_VALUE;
99 }
100 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " end.");
101 return ERR_OK;
102 }
OnHasPasteData(MessageParcel & data,MessageParcel & reply)103 int32_t PasteboardServiceStub::OnHasPasteData(MessageParcel &data, MessageParcel &reply)
104 {
105 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " start.");
106 auto result = HasPasteData();
107 reply.WriteBool(result);
108 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " end.");
109 return ERR_OK;
110 }
111
OnSetPasteData(MessageParcel & data,MessageParcel & reply)112 int32_t PasteboardServiceStub::OnSetPasteData(MessageParcel &data, MessageParcel &reply)
113 {
114 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " start.");
115 int32_t rawDataSize = data.ReadInt32();
116 if (rawDataSize <= 0) {
117 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to read raw size");
118 return ERR_INVALID_VALUE;
119 }
120 auto *rawData = (uint8_t *)data.ReadRawData(rawDataSize);
121 if (rawData == nullptr) {
122 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to get raw data");
123 return ERR_INVALID_VALUE;
124 }
125 std::vector<uint8_t> pasteDataTlv(rawData, rawData + rawDataSize);
126 PasteData pasteData;
127 bool ret = pasteData.Decode(pasteDataTlv);
128 if (!ret) {
129 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to decode pastedata in TLV");
130 return ERR_INVALID_VALUE;
131 }
132 CopyUriHandler copyUriHandler;
133 if (!pasteData.ReadUriFd(data, copyUriHandler)) {
134 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to read uri fd");
135 return ERR_INVALID_VALUE;
136 }
137 int32_t result = SetPasteData(pasteData);
138 if (!reply.WriteInt32(result)) {
139 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Failed to write SetPasteData result");
140 return ERR_INVALID_VALUE;
141 }
142 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, " end.");
143 return ERR_OK;
144 }
OnAddPasteboardChangedObserver(MessageParcel & data,MessageParcel & reply)145 int32_t PasteboardServiceStub::OnAddPasteboardChangedObserver(MessageParcel &data, MessageParcel &reply)
146 {
147 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
148 sptr<IRemoteObject> obj = data.ReadRemoteObject();
149 if (obj == nullptr) {
150 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "obj nullptr");
151 return ERR_INVALID_VALUE;
152 }
153 sptr<IPasteboardChangedObserver> callback = iface_cast<IPasteboardChangedObserver>(obj);
154 if (callback == nullptr) {
155 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "callback nullptr");
156 return ERR_INVALID_VALUE;
157 }
158 AddPasteboardChangedObserver(callback);
159 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
160 return ERR_OK;
161 }
OnRemovePasteboardChangedObserver(MessageParcel & data,MessageParcel & reply)162 int32_t PasteboardServiceStub::OnRemovePasteboardChangedObserver(MessageParcel &data, MessageParcel &reply)
163 {
164 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
165 sptr<IRemoteObject> obj = data.ReadRemoteObject();
166 if (obj == nullptr) {
167 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "obj nullptr");
168 return ERR_INVALID_VALUE;
169 }
170 sptr<IPasteboardChangedObserver> callback = iface_cast<IPasteboardChangedObserver>(obj);
171 if (callback == nullptr) {
172 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "callback nullptr");
173 return ERR_INVALID_VALUE;
174 }
175 RemovePasteboardChangedObserver(callback);
176 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
177 return ERR_OK;
178 }
179
OnRemoveAllChangedObserver(MessageParcel & data,MessageParcel & reply)180 int32_t PasteboardServiceStub::OnRemoveAllChangedObserver(MessageParcel &data, MessageParcel &reply)
181 {
182 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start.");
183 RemoveAllChangedObserver();
184 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "end.");
185 return ERR_OK;
186 }
187
~PasteboardServiceStub()188 PasteboardServiceStub::~PasteboardServiceStub()
189 {
190 memberFuncMap_.clear();
191 }
192 } // namespace MiscServices
193 } // namespace OHOS
194