1 /*
2 * Copyright (C) 2025 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 "input_data_channel_proxy_wrap.h"
16
17 #include <cinttypes>
18 #include <string>
19
20 #include "global.h"
21 #include "input_method_tools.h"
22 #include "string_ex.h"
23 #include "variant_util.h"
24
25 namespace OHOS {
26 namespace MiscServices {
27 constexpr std::size_t MESSAGE_UNANSWERED_MAX_NUMBER = 1000;
InputDataChannelProxyWrap(const std::shared_ptr<InputDataChannelProxy> & channel,const sptr<IRemoteObject> & agentObject)28 InputDataChannelProxyWrap::InputDataChannelProxyWrap(
29 const std::shared_ptr<InputDataChannelProxy> &channel, const sptr<IRemoteObject> &agentObject)
30 {
31 auto now = std::chrono::steady_clock::now();
32 auto duration = now.time_since_epoch();
33 msgId_ = static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(duration).count());
34 msgId_ = msgId_ ? msgId_ : ++msgId_;
35 channel_ = channel;
36 agentObject_ = agentObject;
37 }
38
~InputDataChannelProxyWrap()39 InputDataChannelProxyWrap::~InputDataChannelProxyWrap()
40 {
41 }
42
InsertText(const std::string & text,const AsyncIpcCallBack & callback)43 int32_t InputDataChannelProxyWrap::InsertText(const std::string &text, const AsyncIpcCallBack &callback)
44 {
45 auto work = [agentObject = agentObject_, text](
46 uint64_t msgId, const std::shared_ptr<InputDataChannelProxy> &channel) -> int32_t {
47 return channel->InsertText(text, msgId, agentObject);
48 };
49 return Request(callback, work, callback == nullptr);
50 }
51
DeleteForward(int32_t length,const AsyncIpcCallBack & callback)52 int32_t InputDataChannelProxyWrap::DeleteForward(int32_t length, const AsyncIpcCallBack &callback)
53 {
54 auto work = [agentObject = agentObject_, length](
55 uint64_t msgId, const std::shared_ptr<InputDataChannelProxy> &channel) -> int32_t {
56 return channel->DeleteForward(length, msgId, agentObject);
57 };
58 return Request(callback, work, callback == nullptr);
59 }
60
DeleteBackward(int32_t length,const AsyncIpcCallBack & callback)61 int32_t InputDataChannelProxyWrap::DeleteBackward(int32_t length, const AsyncIpcCallBack &callback)
62 {
63 auto work = [agentObject = agentObject_, length](
64 uint64_t msgId, const std::shared_ptr<InputDataChannelProxy> &channel) -> int32_t {
65 return channel->DeleteBackward(length, msgId, agentObject);
66 };
67 return Request(callback, work, callback == nullptr);
68 }
69
GetTextBeforeCursor(int32_t number,std::string & text,const AsyncIpcCallBack & callback)70 int32_t InputDataChannelProxyWrap::GetTextBeforeCursor(
71 int32_t number, std::string &text, const AsyncIpcCallBack &callback)
72 {
73 auto work = [agentObject = agentObject_, number](
74 uint64_t msgId, const std::shared_ptr<InputDataChannelProxy> &channel) -> int32_t {
75 return channel->GetTextBeforeCursor(number, msgId, agentObject);
76 };
77 SyncOutput output = nullptr;
78 if (callback == nullptr) {
79 output = [&text](const ResponseData &data) -> void { VariantUtil::GetValue(data, text); };
80 }
81 return Request(callback, work, callback == nullptr, output);
82 }
83
GetTextAfterCursor(int32_t number,std::string & text,const AsyncIpcCallBack & callback)84 int32_t InputDataChannelProxyWrap::GetTextAfterCursor(
85 int32_t number, std::string &text, const AsyncIpcCallBack &callback)
86 {
87 auto work = [agentObject = agentObject_, number, text](
88 uint64_t msgId, const std::shared_ptr<InputDataChannelProxy> &channel) -> int32_t {
89 return channel->GetTextAfterCursor(number, msgId, agentObject);
90 };
91 SyncOutput output = nullptr;
92 if (callback == nullptr) {
93 output = [&text](const ResponseData &data) -> void { VariantUtil::GetValue(data, text); };
94 }
95 return Request(callback, work, callback == nullptr, output);
96 }
97
SendFunctionKey(int32_t funcKey,const AsyncIpcCallBack & callback)98 int32_t InputDataChannelProxyWrap::SendFunctionKey(int32_t funcKey, const AsyncIpcCallBack &callback)
99 {
100 auto work = [agentObject = agentObject_, funcKey](
101 uint64_t msgId, const std::shared_ptr<InputDataChannelProxy> &channel) -> int32_t {
102 return channel->SendFunctionKey(funcKey, msgId, agentObject);
103 };
104 return Request(callback, work, callback == nullptr);
105 }
106
MoveCursor(int32_t keyCode,const AsyncIpcCallBack & callback)107 int32_t InputDataChannelProxyWrap::MoveCursor(int32_t keyCode, const AsyncIpcCallBack &callback)
108 {
109 auto work = [agentObject = agentObject_, keyCode](
110 uint64_t msgId, const std::shared_ptr<InputDataChannelProxy> &channel) -> int32_t {
111 return channel->MoveCursor(keyCode, msgId, agentObject);
112 };
113 return Request(callback, work, callback == nullptr);
114 }
115
SelectByRange(int32_t start,int32_t end,const AsyncIpcCallBack & callback)116 int32_t InputDataChannelProxyWrap::SelectByRange(int32_t start, int32_t end, const AsyncIpcCallBack &callback)
117 {
118 auto work = [agentObject = agentObject_, start, end](
119 uint64_t msgId, const std::shared_ptr<InputDataChannelProxy> &channel) -> int32_t {
120 return channel->SelectByRange(start, end, msgId, agentObject);
121 };
122 return Request(callback, work, callback == nullptr);
123 }
124
SelectByMovement(int32_t direction,int32_t cursorMoveSkip,const AsyncIpcCallBack & callback)125 int32_t InputDataChannelProxyWrap::SelectByMovement(
126 int32_t direction, int32_t cursorMoveSkip, const AsyncIpcCallBack &callback)
127 {
128 auto work = [agentObject = agentObject_, direction, cursorMoveSkip](
129 uint64_t msgId, const std::shared_ptr<InputDataChannelProxy> &channel) -> int32_t {
130 return channel->SelectByMovement(direction, cursorMoveSkip, msgId, agentObject);
131 };
132 return Request(callback, work, callback == nullptr);
133 }
134
HandleExtendAction(int32_t action,const AsyncIpcCallBack & callback)135 int32_t InputDataChannelProxyWrap::HandleExtendAction(int32_t action, const AsyncIpcCallBack &callback)
136 {
137 auto work = [agentObject = agentObject_, action](
138 uint64_t msgId, const std::shared_ptr<InputDataChannelProxy> &channel) -> int32_t {
139 return channel->HandleExtendAction(action, msgId, agentObject);
140 };
141 return Request(callback, work, callback == nullptr);
142 }
143
GetTextIndexAtCursor(int32_t & index,const AsyncIpcCallBack & callback)144 int32_t InputDataChannelProxyWrap::GetTextIndexAtCursor(int32_t &index, const AsyncIpcCallBack &callback)
145 {
146 auto work = [agentObject = agentObject_](
147 uint64_t msgId, const std::shared_ptr<InputDataChannelProxy> &channel) -> int32_t {
148 return channel->GetTextIndexAtCursor(msgId, agentObject);
149 };
150 SyncOutput output = nullptr;
151 if (callback == nullptr) {
152 output = [&index](const ResponseData &data) -> void { VariantUtil::GetValue(data, index); };
153 }
154 return Request(callback, work, callback == nullptr, output);
155 }
156
SetPreviewText(const std::string & text,const RangeInner & range,const AsyncIpcCallBack & callback)157 int32_t InputDataChannelProxyWrap::SetPreviewText(
158 const std::string &text, const RangeInner &range, const AsyncIpcCallBack &callback)
159 {
160 auto work = [agentObject = agentObject_, text, range](
161 uint64_t msgId, const std::shared_ptr<InputDataChannelProxy> &channel) -> int32_t {
162 return channel->SetPreviewText(text, range, msgId, agentObject);
163 };
164 return Request(callback, work, callback == nullptr);
165 }
166
FinishTextPreview(const AsyncIpcCallBack & callback)167 int32_t InputDataChannelProxyWrap::FinishTextPreview(const AsyncIpcCallBack &callback)
168 {
169 auto work = [agentObject = agentObject_](
170 uint64_t msgId, const std::shared_ptr<InputDataChannelProxy> &channel) -> int32_t {
171 return channel->FinishTextPreview(msgId, agentObject);
172 };
173 return Request(callback, work, callback == nullptr);
174 }
175
Request(const AsyncIpcCallBack & callback,const ChannelWork & work,bool isSync,const SyncOutput & output)176 int32_t InputDataChannelProxyWrap::Request(
177 const AsyncIpcCallBack &callback, const ChannelWork &work, bool isSync, const SyncOutput &output)
178 {
179 auto channel = GetDataChannel();
180 if (channel == nullptr) {
181 IMSA_HILOGE("data channel is nullptr!");
182 return ErrorCode::ERROR_IMA_CHANNEL_NULLPTR;
183 }
184 auto handler = AddRspHandler(callback, isSync);
185 if (handler == nullptr) {
186 IMSA_HILOGE("add rsp handler failed.");
187 return ErrorCode::ERROR_IMA_DATA_CHANNEL_ABNORMAL;
188 }
189 auto ret = work(handler->msgId_, channel);
190 if (ret != ErrorCode::NO_ERROR) {
191 IMSA_HILOGE("work error: %{public}d.", ret);
192 DeleteRspHandler(handler->msgId_);
193 return ErrorCode::ERROR_IMA_DATA_CHANNEL_ABNORMAL;
194 }
195 if (handler->syncBlockData_ != nullptr) {
196 return WaitResponse(handler, output);
197 }
198 return ErrorCode::NO_ERROR;
199 }
200
GetDataChannel()201 std::shared_ptr<InputDataChannelProxy> InputDataChannelProxyWrap::GetDataChannel()
202 {
203 std::lock_guard<std::mutex> lock(channelMutex_);
204 return channel_;
205 }
206
GenerateMsgId()207 uint64_t InputDataChannelProxyWrap::GenerateMsgId()
208 {
209 return ++msgId_ ? msgId_ : ++msgId_;
210 }
211
AddRspHandler(const AsyncIpcCallBack & callback,bool isSync)212 std::shared_ptr<ResponseHandler> InputDataChannelProxyWrap::AddRspHandler(const AsyncIpcCallBack &callback, bool isSync)
213 {
214 std::lock_guard<std::mutex> lock(rspMutex_);
215 if (rspHandlers_.size() >= MESSAGE_UNANSWERED_MAX_NUMBER) {
216 IMSA_HILOGW("too many unanswered, data channel abnormal");
217 return nullptr;
218 }
219 auto msgId = GenerateMsgId();
220 auto handler = std::make_shared<ResponseHandler>(msgId, isSync, callback);
221 rspHandlers_.insert({ msgId, handler });
222 return handler;
223 }
224
ClearRspHandlers()225 int32_t InputDataChannelProxyWrap::ClearRspHandlers()
226 {
227 std::lock_guard<std::mutex> lock(rspMutex_);
228 ResponseInfo rspInfo = { ErrorCode::ERROR_IMA_DATA_CHANNEL_ABNORMAL, std::monostate{} };
229 for (const auto &handler : rspHandlers_) {
230 if (handler.second == nullptr) {
231 continue;
232 }
233 if (handler.second->syncBlockData_ != nullptr) {
234 handler.second->syncBlockData_->SetValue(rspInfo);
235 continue;
236 }
237 if (handler.second->asyncCallback_ != nullptr) {
238 handler.second->asyncCallback_(rspInfo.dealRet_, rspInfo.data_);
239 }
240 }
241 rspHandlers_.clear();
242 return ErrorCode::NO_ERROR;
243 }
244
HandleResponse(uint64_t msgId,const ResponseInfo & rspInfo)245 int32_t InputDataChannelProxyWrap::HandleResponse(uint64_t msgId, const ResponseInfo &rspInfo)
246 {
247 std::lock_guard<std::mutex> lock(rspMutex_);
248 auto it = rspHandlers_.find(msgId);
249 if (it == rspHandlers_.end()) {
250 return ErrorCode::NO_ERROR;
251 }
252 if (it->second == nullptr) {
253 rspHandlers_.erase(it);
254 return ErrorCode::NO_ERROR;
255 }
256 if (it->second->syncBlockData_ != nullptr) {
257 it->second->syncBlockData_->SetValue(rspInfo);
258 }
259 if (it->second->asyncCallback_ != nullptr) {
260 it->second->asyncCallback_(rspInfo.dealRet_, rspInfo.data_);
261 }
262 rspHandlers_.erase(it);
263 return ErrorCode::NO_ERROR;
264 }
265
WaitResponse(const std::shared_ptr<ResponseHandler> & handler,const SyncOutput & output)266 int32_t InputDataChannelProxyWrap::WaitResponse(
267 const std::shared_ptr<ResponseHandler> &handler, const SyncOutput &output)
268 {
269 if (handler == nullptr || handler->syncBlockData_ == nullptr) {
270 return ErrorCode::ERROR_IMA_DATA_CHANNEL_ABNORMAL;
271 }
272 ResponseInfo rspInfo = handler->syncBlockData_->GetValueWithoutTimeout();
273 IMSA_HILOGD("rsp info id: %{public}" PRIu64 " ret: %{public}d", handler->msgId_, rspInfo.dealRet_);
274 if (rspInfo.dealRet_ != ErrorCode::NO_ERROR) {
275 return rspInfo.dealRet_;
276 }
277 if (output != nullptr) {
278 output(rspInfo.data_);
279 }
280 return ErrorCode::NO_ERROR;
281 }
282
DeleteRspHandler(uint64_t msgId)283 int32_t InputDataChannelProxyWrap::DeleteRspHandler(uint64_t msgId)
284 {
285 std::lock_guard<std::mutex> lock(rspMutex_);
286 auto it = rspHandlers_.find(msgId);
287 if (it != rspHandlers_.end()) {
288 rspHandlers_.erase(it);
289 }
290 return ErrorCode::NO_ERROR;
291 }
292 } // namespace MiscServices
293 } // namespace OHOS