• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_proxy.h"
17 
18 #include "iremote_broker.h"
19 #include "pasteboard_error.h"
20 #include "pasteboard_hilog.h"
21 #include "paste_uri_handler.h"
22 #include "copy_uri_handler.h"
23 
24 namespace OHOS {
25 namespace MiscServices {
PasteboardServiceProxy(const sptr<IRemoteObject> & object)26 PasteboardServiceProxy::PasteboardServiceProxy(const sptr<IRemoteObject> &object)
27     : IRemoteProxy<IPasteboardService>(object)
28 {
29 }
30 
Clear()31 void PasteboardServiceProxy::Clear()
32 {
33     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "start.");
34     MessageParcel data, reply;
35     MessageOption option;
36     if (!data.WriteInterfaceToken(GetDescriptor())) {
37         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write parcelable");
38         return;
39     }
40 
41     int32_t result = Remote()->SendRequest(CLEAR_ALL, data, reply, option);
42     if (result != ERR_NONE) {
43         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "failed, error code is: %{public}d", result);
44     }
45 }
46 
AddPasteboardChangedObserver(const sptr<IPasteboardChangedObserver> & observer)47 void PasteboardServiceProxy::AddPasteboardChangedObserver(const sptr<IPasteboardChangedObserver> &observer)
48 {
49     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "start.");
50     if (observer == nullptr) {
51         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "observer nullptr");
52         return;
53     }
54     MessageParcel data, reply;
55     MessageOption option;
56     if (!data.WriteInterfaceToken(GetDescriptor())) {
57         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write parcelable");
58         return;
59     }
60     if (!data.WriteRemoteObject(observer->AsObject())) {
61         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write parcelable");
62         return;
63     }
64     int32_t result = Remote()->SendRequest(ADD_OBSERVER, data, reply, option);
65     if (result != ERR_NONE) {
66         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "failed, error code is: %{public}d", result);
67     }
68     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "end.");
69 }
70 
RemovePasteboardChangedObserver(const sptr<IPasteboardChangedObserver> & observer)71 void PasteboardServiceProxy::RemovePasteboardChangedObserver(const sptr<IPasteboardChangedObserver> &observer)
72 {
73     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "start.");
74     if (observer == nullptr) {
75         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "observer nullptr");
76         return;
77     }
78     MessageParcel data, reply;
79     MessageOption option;
80     if (!data.WriteInterfaceToken(GetDescriptor())) {
81         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write parcelable");
82         return;
83     }
84     if (!data.WriteRemoteObject(observer->AsObject())) {
85         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write parcelable");
86         return;
87     }
88     int32_t result = Remote()->SendRequest(DELETE_OBSERVER, data, reply, option);
89     if (result != ERR_NONE) {
90         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "failed, error code is: %{public}d", result);
91     }
92     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "end.");
93 }
RemoveAllChangedObserver()94 void PasteboardServiceProxy::RemoveAllChangedObserver()
95 {
96     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "start.");
97     MessageParcel data, reply;
98     MessageOption option;
99     if (!data.WriteInterfaceToken(GetDescriptor())) {
100         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write parcelable");
101         return;
102     }
103     int32_t result = Remote()->SendRequest(DELETE_ALL_OBSERVER, data, reply, option);
104     if (result != ERR_NONE) {
105         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "failed, error code is: %{public}d", result);
106     }
107     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "end.");
108 }
HasPasteData()109 bool PasteboardServiceProxy::HasPasteData()
110 {
111     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "start.");
112     MessageParcel data, reply;
113     MessageOption option;
114 
115     if (!data.WriteInterfaceToken(GetDescriptor())) {
116         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write parcelable");
117         return false;
118     }
119 
120     int32_t result = Remote()->SendRequest(HAS_PASTE_DATA, data, reply, option);
121     if (result != ERR_NONE) {
122         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "failed, error code is: %{public}d", result);
123         return false;
124     }
125     auto has = reply.ReadBool();
126     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "end.");
127     return has;
128 }
129 
SetPasteData(PasteData & pasteData)130 int32_t PasteboardServiceProxy::SetPasteData(PasteData &pasteData)
131 {
132     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "start.");
133     MessageParcel data, reply;
134     MessageOption option;
135     if (!data.WriteInterfaceToken(GetDescriptor())) {
136         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write parcelable");
137         return ERR_INVALID_VALUE;
138     }
139     std::vector<uint8_t> pasteDataTlv(0);
140     bool ret = pasteData.Encode(pasteDataTlv);
141     if (!ret) {
142         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to encode pastedata in TLV");
143         return ERR_INVALID_VALUE;
144     }
145     if (!data.WriteInt32(pasteDataTlv.size())) {
146         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write raw size");
147         return ERR_INVALID_VALUE;
148     }
149     if (!data.WriteRawData(pasteDataTlv.data(), pasteDataTlv.size())) {
150         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write raw data");
151         return ERR_INVALID_VALUE;
152     }
153     CopyUriHandler copyHandler;
154     if (!pasteData.WriteUriFd(data, copyHandler)) {
155         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write record uri fd");
156         return ERR_INVALID_VALUE;
157     }
158 
159     int32_t result = Remote()->SendRequest(SET_PASTE_DATA, data, reply, option);
160     if (result != ERR_NONE) {
161         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "failed, error code is: %{public}d", result);
162         return ERR_INVALID_OPERATION;
163     }
164     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "end.");
165     return reply.ReadInt32();
166 }
167 
GetPasteData(PasteData & pasteData)168 int32_t PasteboardServiceProxy::GetPasteData(PasteData &pasteData)
169 {
170     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "start.");
171     MessageParcel data, reply;
172     MessageOption option;
173     if (!data.WriteInterfaceToken(GetDescriptor())) {
174         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write parcelable");
175         return ERR_INVALID_VALUE;
176     }
177     int32_t result = Remote()->SendRequest(GET_PASTE_DATA, data, reply, option);
178     if (result != ERR_NONE) {
179         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "failed, error code is: %{public}d", result);
180         return ERR_INVALID_OPERATION;
181     }
182     int32_t rawDataSize = reply.ReadInt32();
183     if (rawDataSize <= 0) {
184         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to get raw size");
185         return ERR_INVALID_VALUE;
186     }
187     auto *rawData = (uint8_t *)reply.ReadRawData(rawDataSize);
188     if (rawData == nullptr) {
189         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to get raw data");
190         return ERR_INVALID_VALUE;
191     }
192     std::vector<uint8_t> pasteDataTlv(rawData, rawData + rawDataSize);
193     bool ret = pasteData.Decode(pasteDataTlv);
194     if (!ret) {
195         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to decode pastedata in TLV");
196         return ERR_INVALID_VALUE;
197     }
198     PasteUriHandler pasteHandler;
199     if (!pasteData.ReadUriFd(reply, pasteHandler)) {
200         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to write record uri fd");
201         return ERR_INVALID_VALUE;
202     }
203 
204     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "end.");
205     return reply.ReadInt32();
206 }
207 } // namespace MiscServices
208 } // namespace OHOS