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 #include <if_system_ability_manager.h>
16 #include <ipc_skeleton.h>
17 #include <iservice_registry.h>
18 #include <string_ex.h>
19 #include "system_ability_definition.h"
20 #include "pasteboard_observer.h"
21 #include "pasteboard_common.h"
22 #include "pasteboard_client.h"
23
24 namespace OHOS {
25 namespace MiscServices {
26 sptr<IPasteboardService> PasteboardClient::pasteboardServiceProxy_;
27 std::mutex PasteboardClient::instanceLock_;
28
PasteboardClient()29 PasteboardClient::PasteboardClient() {};
~PasteboardClient()30 PasteboardClient::~PasteboardClient()
31 {
32 if (pasteboardServiceProxy_ != nullptr) {
33 auto remoteObject = pasteboardServiceProxy_->AsObject();
34 if (remoteObject != nullptr) {
35 remoteObject->RemoveDeathRecipient(deathRecipient_);
36 }
37 }
38 }
39
CreateHtmlTextRecord(const std::string & htmlText)40 std::shared_ptr<PasteDataRecord> PasteboardClient::CreateHtmlTextRecord(const std::string &htmlText)
41 {
42 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "New text record: %{public}s", htmlText.c_str());
43 return PasteDataRecord::NewHtmlRecord(htmlText);
44 }
45
CreateWantRecord(std::shared_ptr<OHOS::AAFwk::Want> want)46 std::shared_ptr<PasteDataRecord> PasteboardClient::CreateWantRecord(std::shared_ptr<OHOS::AAFwk::Want> want)
47 {
48 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "New want record");
49 return PasteDataRecord::NewWantRecord(std::move(want));
50 }
51
CreatePlainTextRecord(const std::string & text)52 std::shared_ptr<PasteDataRecord> PasteboardClient::CreatePlainTextRecord(const std::string &text)
53 {
54 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "New text record: %{public}s", text.c_str());
55 return PasteDataRecord::NewPlaintTextRecord(text);
56 }
57
CreateUriRecord(const OHOS::Uri & uri)58 std::shared_ptr<PasteDataRecord> PasteboardClient::CreateUriRecord(const OHOS::Uri &uri)
59 {
60 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "New uri record");
61 return PasteDataRecord::NewUriRecord(uri);
62 }
63
CreateHtmlData(const std::string & htmlText)64 std::shared_ptr<PasteData> PasteboardClient::CreateHtmlData(const std::string &htmlText)
65 {
66 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "New htmlText data: %{public}s", htmlText.c_str());
67 auto pasteData = std::make_shared<PasteData>();
68 pasteData->AddHtmlRecord(htmlText);
69 return pasteData;
70 }
71
CreateWantData(std::shared_ptr<OHOS::AAFwk::Want> want)72 std::shared_ptr<PasteData> PasteboardClient::CreateWantData(std::shared_ptr<OHOS::AAFwk::Want> want)
73 {
74 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "New want data");
75 auto pasteData = std::make_shared<PasteData>();
76 pasteData->AddWantRecord(std::move(want));
77 return pasteData;
78 }
79
CreatePlainTextData(const std::string & text)80 std::shared_ptr<PasteData> PasteboardClient::CreatePlainTextData(const std::string &text)
81 {
82 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "New plain data: %{public}s", text.c_str());
83 auto pasteData = std::make_shared<PasteData>();
84 pasteData->AddTextRecord(text);
85 return pasteData;
86 }
87
CreateUriData(const OHOS::Uri & uri)88 std::shared_ptr<PasteData> PasteboardClient::CreateUriData(const OHOS::Uri &uri)
89 {
90 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "New uri data");
91 auto pasteData = std::make_shared<PasteData>();
92 pasteData->AddUriRecord(uri);
93 return pasteData;
94 }
95
Clear()96 void PasteboardClient::Clear()
97 {
98 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "start.");
99 if (pasteboardServiceProxy_ == nullptr) {
100 PASTEBOARD_HILOGW(PASTEBOARD_MODULE_CLIENT, "Redo ConnectService");
101 ConnectService();
102 }
103
104 if (pasteboardServiceProxy_ == nullptr) {
105 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "GetPasteData quit.");
106 return;
107 }
108 pasteboardServiceProxy_->Clear();
109 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "end.");
110 return;
111 }
112
GetPasteData(PasteData & pasteData)113 bool PasteboardClient::GetPasteData(PasteData& pasteData)
114 {
115 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "start.");
116 if (pasteboardServiceProxy_ == nullptr) {
117 PASTEBOARD_HILOGW(PASTEBOARD_MODULE_CLIENT, "Redo ConnectService");
118 ConnectService();
119 }
120
121 if (pasteboardServiceProxy_ == nullptr) {
122 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "GetPasteData quit.");
123 return false;
124 }
125 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "end.");
126 return pasteboardServiceProxy_->GetPasteData(pasteData);
127 }
128
HasPasteData()129 bool PasteboardClient::HasPasteData()
130 {
131 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "start.");
132 if (pasteboardServiceProxy_ == nullptr) {
133 PASTEBOARD_HILOGW(PASTEBOARD_MODULE_CLIENT, "Redo ConnectService");
134 ConnectService();
135 }
136
137 if (pasteboardServiceProxy_ == nullptr) {
138 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "HasPasteData quit ");
139 return false;
140 }
141 auto result = pasteboardServiceProxy_->HasPasteData();
142 return result;
143 }
144
SetPasteData(PasteData & pasteData)145 void PasteboardClient::SetPasteData(PasteData& pasteData)
146 {
147 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "start.");
148 if (pasteboardServiceProxy_ == nullptr) {
149 PASTEBOARD_HILOGW(PASTEBOARD_MODULE_CLIENT, "Redo ConnectService");
150 ConnectService();
151 }
152
153 if (pasteboardServiceProxy_ == nullptr) {
154 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "SetPasteData quit.");
155 return;
156 }
157 pasteboardServiceProxy_->SetPasteData(pasteData);
158 }
159
AddPasteboardChangedObserver(std::shared_ptr<PasteboardObserver> callback)160 void PasteboardClient::AddPasteboardChangedObserver(std::shared_ptr<PasteboardObserver> callback)
161 {
162 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "start.");
163 if (callback == nullptr) {
164 PASTEBOARD_HILOGW(PASTEBOARD_MODULE_CLIENT, "input nullptr.");
165 return;
166 }
167 if (pasteboardServiceProxy_ == nullptr) {
168 PASTEBOARD_HILOGW(PASTEBOARD_MODULE_CLIENT, "Redo ConnectService");
169 ConnectService();
170 }
171
172 if (pasteboardServiceProxy_ == nullptr) {
173 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "AddPasteboardChangedObserver quit.");
174 return;
175 }
176
177 auto remoteObject = callback->AsObject();
178 sptr<IPasteboardChangedObserver> observerPtr = iface_cast<IPasteboardChangedObserver>(remoteObject);
179 pasteboardServiceProxy_->AddPasteboardChangedObserver(observerPtr);
180 return;
181 }
182
RemovePasteboardChangedObserver(std::shared_ptr<PasteboardObserver> callback)183 void PasteboardClient::RemovePasteboardChangedObserver(std::shared_ptr<PasteboardObserver> callback)
184 {
185 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "start.");
186 if (pasteboardServiceProxy_ == nullptr) {
187 PASTEBOARD_HILOGW(PASTEBOARD_MODULE_CLIENT, "Redo ConnectService");
188 ConnectService();
189 }
190
191 if (pasteboardServiceProxy_ == nullptr) {
192 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "quit.");
193 return;
194 }
195 if (callback == nullptr) {
196 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "remove all.");
197 pasteboardServiceProxy_->RemoveAllChangedObserver();
198 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "end.");
199 return;
200 }
201 auto remoteObject = callback->AsObject();
202 sptr<IPasteboardChangedObserver> observerPtr = iface_cast<IPasteboardChangedObserver>(remoteObject);
203 pasteboardServiceProxy_->RemovePasteboardChangedObserver(observerPtr);
204 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "end.");
205 return;
206 }
207
ConnectService()208 void PasteboardClient::ConnectService()
209 {
210 std::lock_guard<std::mutex> lock(instanceLock_);
211 if (pasteboardServiceProxy_ != nullptr) {
212 return ;
213 }
214 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "start.");
215 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
216 if (sam == nullptr) {
217 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Getting SystemAbilityManager failed.");
218 pasteboardServiceProxy_ = nullptr;
219 return;
220 }
221 sptr<IRemoteObject> remoteObject = sam->CheckSystemAbility(PASTEBOARD_SERVICE_ID);
222 if (remoteObject == nullptr) {
223 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "GetSystemAbility failed!");
224 return;
225 }
226
227 deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new PasteboardSaDeathRecipient());
228 if (deathRecipient_ == nullptr) {
229 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Getting deathRecipient_ failed.");
230 return;
231 }
232 if ((remoteObject->IsProxyObject()) && (!remoteObject->AddDeathRecipient(deathRecipient_))) {
233 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Add death recipient to paste service failed.");
234 return;
235 }
236
237 pasteboardServiceProxy_ = iface_cast<IPasteboardService>(remoteObject);
238 if (pasteboardServiceProxy_ == nullptr) {
239 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Get PasteboardServiceProxy from SA failed.");
240 return ;
241 }
242 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "Getting PasteboardServiceProxy succeeded.");
243 }
244
OnRemoteSaDied(const wptr<IRemoteObject> & remote)245 void PasteboardClient::OnRemoteSaDied(const wptr<IRemoteObject> &remote)
246 {
247 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "start.");
248 ConnectService();
249 }
250
PasteboardSaDeathRecipient()251 PasteboardSaDeathRecipient::PasteboardSaDeathRecipient()
252 {
253 }
254
OnRemoteDied(const wptr<IRemoteObject> & object)255 void PasteboardSaDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &object)
256 {
257 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "PasteboardSaDeathRecipient on remote systemAbility died.");
258 PasteboardClient::GetInstance()->OnRemoteSaDied(object);
259 }
260 } // MiscServices
261 } // OHOS