• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #ifndef FRAMEWORKS_INPUTMETHOD_CONTROLLER_INCLUDE_INPUT_DATA_CHANNEL_PROXY_WRAP_H
17 #define FRAMEWORKS_INPUTMETHOD_CONTROLLER_INCLUDE_INPUT_DATA_CHANNEL_PROXY_WRAP_H
18 #include <atomic>
19 #include <condition_variable>
20 #include <map>
21 #include <memory>
22 #include <mutex>
23 #include <variant>
24 
25 #include "block_data.h"
26 #include "input_data_channel_proxy.h"
27 #include "input_method_utils.h"
28 
29 namespace OHOS {
30 namespace MiscServices {
31 using ChannelWork = std::function<int32_t(uint64_t msgId, const std::shared_ptr<InputDataChannelProxy> &channel)>;
32 using SyncOutput = std::function<void(const ResponseData &)>;
33 using AsyncIpcCallBack = std::function<void(int32_t, const ResponseData &)>;
34 struct ResponseInfo {
35     int32_t dealRet_{ ErrorCode::NO_ERROR };
36     ResponseData data_{ std::monostate{} };
37 };
38 struct ResponseHandler {
39     static constexpr uint32_t SYNC_REPLY_TIMEOUT = 3000; // unit ms
40     uint64_t msgId_ = 0;
41     AsyncIpcCallBack asyncCallback_ = nullptr;
42     std::shared_ptr<BlockData<ResponseInfo>> syncBlockData_ = nullptr;
ResponseHandlerResponseHandler43     ResponseHandler(uint64_t msgId, bool isSync, const AsyncIpcCallBack &callback)
44     {
45         msgId_ = msgId;
46         asyncCallback_ = callback;
47         if (isSync) {
48             syncBlockData_ = std::make_shared<BlockData<ResponseInfo>>(SYNC_REPLY_TIMEOUT);
49         }
50     }
51 };
52 class InputDataChannelProxyWrap {
53 public:
54     InputDataChannelProxyWrap(
55         const std::shared_ptr<InputDataChannelProxy> &channel, const sptr<IRemoteObject> &agentObject);
56     ~InputDataChannelProxyWrap();
57 
58     int32_t InsertText(const std::string &text, const AsyncIpcCallBack &callback = nullptr);
59     int32_t DeleteForward(int32_t length, const AsyncIpcCallBack &callback = nullptr);
60     int32_t DeleteBackward(int32_t length, const AsyncIpcCallBack &callback = nullptr);
61     int32_t GetTextBeforeCursor(int32_t number, std::string &text, const AsyncIpcCallBack &callback = nullptr);
62     int32_t GetTextAfterCursor(int32_t number, std::string &text, const AsyncIpcCallBack &callback = nullptr);
63     int32_t SendFunctionKey(int32_t funcKey, const AsyncIpcCallBack &callback = nullptr);
64     int32_t MoveCursor(int32_t keyCode, const AsyncIpcCallBack &callback = nullptr);
65     int32_t SelectByRange(int32_t start, int32_t end, const AsyncIpcCallBack &callback = nullptr);
66     int32_t SelectByMovement(int32_t direction, int32_t cursorMoveSkip, const AsyncIpcCallBack &callback = nullptr);
67     int32_t HandleExtendAction(int32_t action, const AsyncIpcCallBack &callback = nullptr);
68     int32_t GetTextIndexAtCursor(int32_t &index, const AsyncIpcCallBack &callback = nullptr);
69     int32_t SetPreviewText(
70         const std::string &text, const RangeInner &range, const AsyncIpcCallBack &callback = nullptr);
71     int32_t FinishTextPreview(const AsyncIpcCallBack &callback = nullptr);
72     int32_t ClearRspHandlers();
73 
74 public:
75     int32_t HandleResponse(uint64_t msgId, const ResponseInfo &rspInfo);
76     std::shared_ptr<InputDataChannelProxy> GetDataChannel();
77 
78 private:
79     std::shared_ptr<ResponseHandler> AddRspHandler(const AsyncIpcCallBack &callback, bool isSync);
80     int32_t WaitResponse(const std::shared_ptr<ResponseHandler> &rspHandler, const SyncOutput &output);
81     int32_t DeleteRspHandler(uint64_t msgId);
82     uint64_t GenerateMsgId();
83     int32_t Request(
84         const AsyncIpcCallBack &callback, const ChannelWork &work, bool isSync, const SyncOutput &output = nullptr);
85 
86 private:
87     uint64_t msgId_{ 0 };
88     std::mutex rspMutex_;
89     std::map<uint64_t, std::shared_ptr<ResponseHandler>> rspHandlers_;
90     std::mutex channelMutex_;
91     std::shared_ptr<InputDataChannelProxy> channel_ = nullptr;
92     sptr<IRemoteObject> agentObject_ = nullptr;
93 };
94 } // namespace MiscServices
95 } // namespace OHOS
96 #endif // FRAMEWORKS_INPUTMETHOD_CONTROLLER_INCLUDE_INPUT_DATA_CHANNEL_PROXY_WRAP_H
97