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 
16 #ifndef SERVICES_INCLUDE_INPUT_METHOD_SYSTEM_ABILITY_H
17 #define SERVICES_INCLUDE_INPUT_METHOD_SYSTEM_ABILITY_H
18 
19 #include <atomic>
20 #include <map>
21 #include <thread>
22 
23 #include "application_info.h"
24 #include "block_queue.h"
25 #include "bundle_mgr_proxy.h"
26 #include "element_name.h"
27 #include "enable_ime_data_parser.h"
28 #include "event_handler.h"
29 #include "identity_checker_impl.h"
30 #include "ime_info_inquirer.h"
31 #include "input_method_status.h"
32 #include "input_method_system_ability_stub.h"
33 #include "inputmethod_dump.h"
34 #include "inputmethod_trace.h"
35 #include "message.h"
36 #include "security_mode_parser.h"
37 #include "settings_data_utils.h"
38 #include "system_ability.h"
39 #include "input_method_types.h"
40 
41 namespace OHOS {
42 namespace MiscServices {
43 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
44 class InputMethodSystemAbility : public SystemAbility, public InputMethodSystemAbilityStub {
45     DECLARE_SYSTEM_ABILITY(InputMethodSystemAbility);
46 
47 public:
48     DISALLOW_COPY_AND_MOVE(InputMethodSystemAbility);
49     InputMethodSystemAbility(int32_t systemAbilityId, bool runOnCreate);
50     InputMethodSystemAbility();
51     ~InputMethodSystemAbility();
52 
53     int32_t StartInput(InputClientInfo &inputClientInfo, sptr<IRemoteObject> &agent) override;
54     int32_t ShowCurrentInput() override;
55     int32_t HideCurrentInput() override;
56     int32_t ShowInput(sptr<IInputClient> client) override;
57     int32_t HideInput(sptr<IInputClient> client) override;
58     int32_t StopInputSession() override;
59     int32_t ReleaseInput(sptr<IInputClient> client) override;
60     int32_t RequestShowInput() override;
61     int32_t RequestHideInput() override;
62     int32_t GetDefaultInputMethod(std::shared_ptr<Property> &prop, bool isBrief) override;
63     int32_t GetInputMethodConfig(OHOS::AppExecFwk::ElementName &inputMethodConfig) override;
64     std::shared_ptr<Property> GetCurrentInputMethod() override;
65     std::shared_ptr<SubProperty> GetCurrentInputMethodSubtype() override;
66     int32_t ListInputMethod(InputMethodStatus status, std::vector<Property> &props) override;
67     int32_t ListCurrentInputMethodSubtype(std::vector<SubProperty> &subProps) override;
68     int32_t ListInputMethodSubtype(const std::string &bundleName, std::vector<SubProperty> &subProps) override;
69     int32_t SwitchInputMethod(
70         const std::string &bundleName, const std::string &subName, SwitchTrigger trigger) override;
71     int32_t DisplayOptionalInputMethod() override;
72     int32_t SetCoreAndAgent(const sptr<IInputMethodCore> &core, const sptr<IRemoteObject> &agent) override;
73     int32_t InitConnect() override;
74     int32_t UnRegisteredProxyIme(UnRegisteredType type, const sptr<IInputMethodCore> &core) override;
75     int32_t PanelStatusChange(const InputWindowStatus &status, const ImeWindowInfo &info) override;
76     int32_t UpdateListenEventFlag(InputClientInfo &clientInfo, uint32_t eventFlag) override;
77     bool IsCurrentIme() override;
78     bool IsInputTypeSupported(InputType type) override;
79     bool IsCurrentImeByPid(int32_t pid) override;
80     int32_t StartInputType(InputType type) override;
81     int32_t ExitCurrentInputType() override;
82     int32_t IsPanelShown(const PanelInfo &panelInfo, bool &isShown) override;
83     int32_t GetSecurityMode(int32_t &security) override;
84     int32_t ConnectSystemCmd(const sptr<IRemoteObject> &channel, sptr<IRemoteObject> &agent) override;
85     // Deprecated because of no permission check, kept for compatibility
86     int32_t HideCurrentInputDeprecated() override;
87     int32_t ShowCurrentInputDeprecated() override;
88     int Dump(int fd, const std::vector<std::u16string> &args) override;
89     void DumpAllMethod(int fd);
90     int32_t IsDefaultIme() override;
91     bool IsDefaultImeSet() override;
92     bool EnableIme(const std::string &bundleName) override;
93 
94 protected:
95     void OnStart() override;
96     void OnStop() override;
97 
98 private:
99     int32_t Init();
100     void Initialize();
101 
102     std::thread workThreadHandler; /*!< thread handler of the WorkThread */
103     int32_t GetUserId(int32_t uid);
104     int32_t GetCallingUserId();
105     std::shared_ptr<IdentityChecker> identityChecker_ = nullptr;
106     int32_t PrepareInput(int32_t userId, InputClientInfo &clientInfo);
107     void WorkThread();
108     int32_t OnUserStarted(const Message *msg);
109     int32_t OnUserRemoved(const Message *msg);
110     int32_t OnUserStop(const Message *msg);
111     int32_t OnHideKeyboardSelf(const Message *msg);
112     bool IsNeedSwitch(int32_t userId, const std::string &bundleName, const std::string &subName);
113     int32_t CheckEnableAndSwitchPermission();
114     std::string SetSettingValues(const std::string &settingValue, const std::string &bundleName);
115     int32_t CheckSwitchPermission(int32_t userId, const SwitchInfo &switchInfo, SwitchTrigger trigger);
116     bool IsStartInputTypePermitted(int32_t userId);
117     int32_t OnSwitchInputMethod(int32_t userId, const SwitchInfo &switchInfo, SwitchTrigger trigger);
118     int32_t OnStartInputType(int32_t userId, const SwitchInfo &switchInfo, bool isCheckPermission);
119     int32_t HandlePackageEvent(const Message *msg);
120     int32_t OnPackageRemoved(int32_t userId, const std::string &packageName);
121     int32_t OnDisplayOptionalInputMethod();
122     void SubscribeCommonEvent();
123     int32_t Switch(int32_t userId, const std::string &bundleName, const std::shared_ptr<ImeInfo> &info);
124     int32_t SwitchExtension(int32_t userId, const std::shared_ptr<ImeInfo> &info);
125     int32_t SwitchSubType(int32_t userId, const std::shared_ptr<ImeInfo> &info);
126     int32_t SwitchInputType(int32_t userId, const SwitchInfo &switchInfo);
127     void GetValidSubtype(const std::string &subName, const std::shared_ptr<ImeInfo> &info);
128     ServiceRunningState state_;
129     void InitServiceHandler();
130     void UpdateUserInfo(int32_t userId);
131     void HandleWmsConnected(int32_t userId, int32_t screenId);
132     void HandleWmsDisconnected(int32_t userId, int32_t screenId);
133     void HandleScbStarted(int32_t userId, int32_t screenId);
134     void HandleUserSwitched(int32_t userId);
135     void HandleWmsStarted();
136     void HandleMemStarted();
137     void HandleOsAccountStarted();
138     void HandleFocusChanged(bool isFocused, int32_t pid, int32_t uid);
139     void StopImeInBackground();
140     int32_t InitAccountMonitor();
141     int32_t RegisterDataShareObserver();
142     static std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_;
143     int32_t userId_;
144     static constexpr const char *SELECT_DIALOG_ACTION = "action.system.inputmethodchoose";
145     static constexpr const char *SELECT_DIALOG_HAP = "com.ohos.inputmethodchoosedialog";
146     static constexpr const char *SELECT_DIALOG_ABILITY = "InputMethod";
147     bool stop_ = false;
148     void InitMonitors();
149     int32_t InitKeyEventMonitor();
150     bool InitWmsMonitor();
151     void InitSystemLanguageMonitor();
152     bool InitMemMgrMonitor();
153     void InitWmsConnectionMonitor();
154     void InitFocusChangedMonitor();
155     int32_t SwitchByCombinationKey(uint32_t state);
156     int32_t SwitchMode();
157     int32_t SwitchLanguage();
158     int32_t SwitchType();
159     int32_t GenerateClientInfo(int32_t userId, InputClientInfo &clientInfo);
160     void RegisterEnableImeObserver();
161     void RegisterSecurityModeObserver();
162     int32_t CheckInputTypeOption(int32_t userId, InputClientInfo &inputClientInfo);
163     int32_t IsDefaultImeFromTokenId(int32_t userId, uint32_t tokenId);
164     void DealSwitchRequest();
165     void DealSecurityChange();
166     void OnSecurityModeChange();
167     bool IsCurrentIme(int32_t userId);
168     int32_t StartInputType(int32_t userId, InputType type);
169     // if switch input type need to switch ime, then no need to hide panel first.
170     void NeedHideWhenSwitchInputType(int32_t userId, bool &needHide);
171 
172     std::mutex checkMutex_;
173     void DatashareCallback(const std::string &key);
174     std::atomic<bool> enableImeOn_ = false;
175     std::atomic<bool> enableSecurityMode_ = false;
176 
177     std::atomic<bool> isScbEnable_ = false;
178     std::mutex switchImeMutex_;
179     std::atomic<bool> switchTaskExecuting_ = false;
180     std::atomic<uint32_t> targetSwitchCount_ = 0;
181 
182     std::mutex modeChangeMutex_;
183     bool isChangeHandling_ = false;
184     bool hasPendingChanges_ = false;
185 };
186 } // namespace MiscServices
187 } // namespace OHOS
188 #endif // SERVICES_INCLUDE_INPUT_METHOD_SYSTEM_ABILITY_H
189