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 menue config from default input method.
79      *
80      * This function is used to get smart menue config from default input method.
81      *.
82      * @return string.
83      * @since 12
84      */
85     IMF_API std::string GetSmartMenuCfg();
86     int32_t ReceivePrivateCommand(
87         const std::unordered_map<std::string, PrivateDataValue> &privateCommand) override;
88     int32_t NotifyPanelStatus(const SysPanelStatus &sysPanelStatus);
89     void OnConnectCmdReady(const sptr<IRemoteObject> &agentObject);
90     IMF_API int32_t GetDefaultImeCfg(std::shared_ptr<Property> &property);
91 
92 private:
93     ImeSystemCmdChannel();
94     ~ImeSystemCmdChannel();
95     int32_t RunConnectSystemCmd();
96     sptr<IInputMethodSystemAbility> GetSystemAbilityProxy();
97     void OnRemoteSaDied(const wptr<IRemoteObject> &object);
98     void SetSystemCmdListener(const sptr<OnSystemCmdListener> &listener);
99     sptr<IInputMethodAgent> GetSystemCmdAgent();
100     sptr<OnSystemCmdListener> GetSystemCmdListener();
101     void ClearSystemCmdAgent();
102     void GetExtensionInfo(std::vector<ExtensionAbilityInfo> extensionInfos, ExtensionAbilityInfo &extInfo);
103     void OnSystemCmdAgentDied(const wptr<IRemoteObject> &remote);
104 
105     static std::mutex instanceLock_;
106     static sptr<ImeSystemCmdChannel> instance_;
107 
108     std::mutex abilityLock_;
109     sptr<IInputMethodSystemAbility> systemAbility_ = nullptr;
110     sptr<InputDeathRecipient> deathRecipient_;
111 
112     std::mutex systemCmdListenerLock_;
113     sptr<OnSystemCmdListener> systemCmdListener_ = nullptr;
114 
115     std::mutex systemAgentLock_;
116     sptr<IInputMethodAgent> systemAgent_ = nullptr;
117     sptr<InputDeathRecipient> agentDeathRecipient_;
118     std::atomic_bool isSystemCmdConnect_{ false };
119 
120     std::mutex systemChannelMutex_;
121     sptr<ISystemCmdChannel> systemChannelStub_;
122 };
123 } // namespace MiscServices
124 } // namespace OHOS
125 #endif // INPUTMETHOD_CONTROLLER_IME_SYSTEM_CHANNEL_H