1 /*
2  * Copyright (C) 2024 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 INPUTMETHOD_CONTROLLER_IME_SYSTEM_CHANNEL_H
17 #define INPUTMETHOD_CONTROLLER_IME_SYSTEM_CHANNEL_H
18 
19 #include "bundle_mgr_client.h"
20 #include "i_input_method_agent.h"
21 #include "i_input_method_system_ability.h"
22 #include "input_method_utils.h"
23 #include "ipc_skeleton.h"
24 #include "iremote_object.h"
25 #include "private_command_interface.h"
26 #include "refbase.h"
27 #include "visibility.h"
28 
29 namespace OHOS {
30 namespace MiscServices {
31 using namespace OHOS::AppExecFwk;
32 class OnSystemCmdListener : public virtual RefBase {
33 public:
ReceivePrivateCommand(const std::unordered_map<std::string, PrivateDataValue> &privateCommand)34     virtual void ReceivePrivateCommand(const std::unordered_map<std::string, PrivateDataValue> &privateCommand)
35     {
36     }
NotifyPanelStatus(const SysPanelStatus &sysPanelStatus)37     virtual void NotifyPanelStatus(const SysPanelStatus &sysPanelStatus)
38     {
39     }
40 };
41 using PrivateDataValue = std::variant<std::string, bool, int32_t>;
42 class ImeSystemCmdChannel : public RefBase, public PrivateCommandInterface {
43 public:
44     /**
45      * @brief Get the instance of ImeSystemCmdChannel.
46      *
47      * This function is used to get the instance of ImeSystemCmdChannel.
48      *
49      * @return The instance of ImeSystemCmdChannel.
50      * @since 12
51      */
52     IMF_API static sptr<ImeSystemCmdChannel> GetInstance();
53 
54     /**
55      * @brief Connect system channel, set listener and bind IMSA.
56      *
57      * This function is used to connect the system app and  input method.
58      *
59      * @param listener Indicates the listener in order to receive private command.
60      * @return Returns 0 for success, others for failure.
61      * @since 12
62      */
63     IMF_API int32_t ConnectSystemCmd(const sptr<OnSystemCmdListener> &listener);
64 
65     /**
66      * @brief Send private command to ime.
67      *
68      * This function is used to send private command to ime.
69      *
70      * @param privateCommand Indicates the private command which will be send.
71      * @return Returns 0 for success, others for failure.
72      * @since 12
73      */
74     IMF_API int32_t SendPrivateCommand(
75         const std::unordered_map<std::string, PrivateDataValue> &privateCommand) override;
76 
77     /**
78      * @brief Get smart menu config from default input method.
79      *
80      * This function is used to get smart menu config from default input method.
81      *
82      * @return string.
83      * @since 12
84      */
85     IMF_API std::string GetSmartMenuCfg();
86     int32_t ReceivePrivateCommand(const std::unordered_map<std::string, PrivateDataValue> &privateCommand) override;
87     int32_t NotifyPanelStatus(const SysPanelStatus &sysPanelStatus);
88     void OnConnectCmdReady(const sptr<IRemoteObject> &agentObject);
89     IMF_API int32_t GetDefaultImeCfg(std::shared_ptr<Property> &property);
90 
91 private:
92     ImeSystemCmdChannel();
93     ~ImeSystemCmdChannel();
94     int32_t RunConnectSystemCmd();
95     sptr<IInputMethodSystemAbility> GetSystemAbilityProxy();
96     void OnRemoteSaDied(const wptr<IRemoteObject> &object);
97     void SetSystemCmdListener(const sptr<OnSystemCmdListener> &listener);
98     sptr<IInputMethodAgent> GetSystemCmdAgent();
99     sptr<OnSystemCmdListener> GetSystemCmdListener();
100     void ClearSystemCmdAgent();
101     void GetExtensionInfo(std::vector<ExtensionAbilityInfo> extensionInfos, ExtensionAbilityInfo &extInfo);
102     void OnSystemCmdAgentDied(const wptr<IRemoteObject> &remote);
103 
104     static std::mutex instanceLock_;
105     static sptr<ImeSystemCmdChannel> instance_;
106 
107     std::mutex abilityLock_;
108     sptr<IInputMethodSystemAbility> systemAbility_ = nullptr;
109     sptr<InputDeathRecipient> deathRecipient_;
110 
111     std::mutex systemCmdListenerLock_;
112     sptr<OnSystemCmdListener> systemCmdListener_ = nullptr;
113 
114     std::mutex systemAgentLock_;
115     sptr<IInputMethodAgent> systemAgent_ = nullptr;
116     sptr<InputDeathRecipient> agentDeathRecipient_;
117     std::atomic_bool isSystemCmdConnect_{ false };
118 
119     std::mutex systemChannelMutex_;
120     sptr<ISystemCmdChannel> systemChannelStub_;
121 };
122 } // namespace MiscServices
123 } // namespace OHOS
124 #endif // INPUTMETHOD_CONTROLLER_IME_SYSTEM_CHANNEL_H