• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ability_manager_interface.h"
20 #include "identity_checker_impl.h"
21 #include "ime_info_inquirer.h"
22 #include "input_method_system_ability_stub.h"
23 #include "inputmethod_dump.h"
24 #include "inputmethod_trace.h"
25 #include "system_ability.h"
26 #include "input_method_types.h"
27 #include "user_session_manager.h"
28 #include "input_type_manager.h"
29 
30 namespace OHOS {
31 namespace MiscServices {
32 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
33 
34 class InputMethodSystemAbility : public SystemAbility, public InputMethodSystemAbilityStub {
35     DECLARE_SYSTEM_ABILITY(InputMethodSystemAbility);
36 
37 public:
38     DISALLOW_COPY_AND_MOVE(InputMethodSystemAbility);
39     InputMethodSystemAbility(int32_t systemAbilityId, bool runOnCreate);
40     InputMethodSystemAbility();
41     ~InputMethodSystemAbility();
42     int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
43 
44     ErrCode StartInput(const InputClientInfoInner &inputClientInfoInner, std::vector<sptr<IRemoteObject>> &agents,
45         std::vector<BindImeInfo> &imeInfos) override;
46     ErrCode ShowCurrentInput(uint32_t type = static_cast<uint32_t>(ClientType::INNER_KIT)) override;
47     ErrCode HideCurrentInput() override;
48     ErrCode ShowInput(const sptr<IInputClient>& client, uint32_t type = static_cast<uint32_t>(ClientType::INNER_KIT),
49         int32_t requestKeyboardReason = 0) override;
50     ErrCode HideInput(const sptr<IInputClient>& client) override;
51     ErrCode StopInputSession() override;
52     ErrCode ReleaseInput(const sptr<IInputClient>& client, uint32_t sessionId) override;
53     ErrCode RequestShowInput() override;
54     ErrCode RequestHideInput(bool isFocusTriggered) override;
55     ErrCode GetDefaultInputMethod(Property &prop, bool isBrief) override;
56     ErrCode GetInputMethodConfig(ElementName &inputMethodConfig) override;
57     ErrCode GetCurrentInputMethod(Property& resultValue) override;
58     ErrCode GetCurrentInputMethodSubtype(SubProperty& resultValue) override;
59     ErrCode ListInputMethod(uint32_t status, std::vector<Property> &props) override;
60     ErrCode ListCurrentInputMethodSubtype(std::vector<SubProperty> &subProps) override;
61     ErrCode ListInputMethodSubtype(const std::string &bundleName, std::vector<SubProperty> &subProps) override;
62     ErrCode SwitchInputMethod(
63         const std::string &bundleName, const std::string &subName, uint32_t trigger) override;
64     ErrCode DisplayOptionalInputMethod() override;
65     ErrCode SetCoreAndAgent(const sptr<IInputMethodCore> &core, const sptr<IRemoteObject> &agent) override;
66     ErrCode InitConnect() override;
67     ErrCode UnRegisteredProxyIme(int32_t type, const sptr<IInputMethodCore> &core) override;
68     ErrCode PanelStatusChange(uint32_t status, const ImeWindowInfo &info) override;
69     ErrCode UpdateListenEventFlag(const InputClientInfoInner &clientInfoInner, uint32_t eventFlag) override;
70     ErrCode SetCallingWindow(uint32_t windowId, const sptr<IInputClient>& client) override;
71     ErrCode GetInputStartInfo(bool& isInputStart, uint32_t& callingWndId, int32_t& requestKeyboardReason) override;
72     ErrCode SendPrivateData(const Value &value) override;
73 
74     ErrCode IsCurrentIme(bool& resultValue) override;
75     ErrCode IsInputTypeSupported(int32_t type, bool& resultValue) override;
76     ErrCode IsCurrentImeByPid(int32_t pid, bool& resultValue) override;
77     ErrCode StartInputType(int32_t type) override;
78     ErrCode StartInputTypeAsync(int32_t type) override;
79     ErrCode ExitCurrentInputType() override;
80     ErrCode IsPanelShown(const PanelInfo &panelInfo, bool &isShown) override;
81     ErrCode GetSecurityMode(int32_t &security) override;
82     ErrCode ConnectSystemCmd(const sptr<IRemoteObject> &channel, sptr<IRemoteObject> &agent) override;
83     // Deprecated because of no permission check, kept for compatibility
84     ErrCode HideCurrentInputDeprecated() override;
85     ErrCode ShowCurrentInputDeprecated() override;
86     int Dump(int fd, const std::vector<std::u16string> &args) override;
87     void DumpAllMethod(int fd);
88     ErrCode IsDefaultIme() override;
89     ErrCode IsDefaultImeSet(bool& resultValue) override;
90     ErrCode EnableIme(const std::string &bundleName, const std::string &extensionName, int32_t status) override;
91     ErrCode GetInputMethodState(int32_t &status) override;
92     ErrCode IsSystemApp(bool& resultValue) override;
93     ErrCode RegisterProxyIme(
94         uint64_t displayId, const sptr<IInputMethodCore> &core, const sptr<IRemoteObject> &agent) override;
95     ErrCode UnregisterProxyIme(uint64_t displayId) override;
96     ErrCode IsRestrictedDefaultImeByDisplay(uint64_t displayId, bool &resultValue) override;
97     ErrCode IsCapacitySupport(int32_t capacity, bool &isSupport) override;
98     ErrCode BindImeMirror(const sptr<IInputMethodCore> &core, const sptr<IRemoteObject> &agent) override;
99     ErrCode UnbindImeMirror() override;
100     int32_t GetCallingUserId();
101 
102 protected:
103     void OnStart() override;
104     void OnStop() override;
105     int32_t OnIdle(const SystemAbilityOnDemandReason &idleReason) override;
106     int32_t OnExtension(const std::string &extension, MessageParcel &data, MessageParcel &reply) override;
107 
108 private:
109     int32_t Init();
110     void Initialize();
111 
112     std::thread workThreadHandler; /*!< thread handler of the WorkThread */
113     void RestartSessionIme(std::shared_ptr<PerUserSession> &session);
114     std::shared_ptr<PerUserSession> GetSessionFromMsg(const Message *msg);
115     int32_t PrepareForOperateKeyboard(std::shared_ptr<PerUserSession> &session);
116     int32_t SwitchByCondition(const Condition &condition,
117         const std::shared_ptr<ImeInfo> &info);
118     int32_t GetUserId(int32_t uid);
119     uint64_t GetCallingDisplayId(sptr<IRemoteObject> abilityToken = nullptr);
120     std::shared_ptr<IdentityChecker> identityChecker_ = nullptr;
121     int32_t PrepareInput(int32_t userId, InputClientInfo &clientInfo);
122     void WorkThread();
123     int32_t OnUserStarted(const Message *msg);
124     int32_t OnUserRemoved(const Message *msg);
125     int32_t OnUserStop(const Message *msg);
126     int32_t OnHideKeyboardSelf(const Message *msg);
127     void OnSysMemChanged();
128     bool IsNeedSwitch(int32_t userId, const std::string &bundleName, const std::string &subName);
129     int32_t CheckEnableAndSwitchPermission();
130     int32_t CheckSwitchPermission(int32_t userId, const SwitchInfo &switchInfo, SwitchTrigger trigger);
131     bool IsStartInputTypePermitted(int32_t userId);
132     int32_t OnSwitchInputMethod(int32_t userId, const SwitchInfo &switchInfo, SwitchTrigger trigger);
133     int32_t StartSwitch(int32_t userId, const SwitchInfo &switchInfo,
134         const std::shared_ptr<PerUserSession> &session);
135     int32_t OnStartInputType(int32_t userId, const SwitchInfo &switchInfo, bool isCheckPermission);
136     int32_t HandlePackageEvent(const Message *msg);
137     int32_t HandleUpdateLargeMemoryState(const Message *msg);
138     int32_t OnPackageRemoved(int32_t userId, const std::string &packageName);
139     void OnScreenUnlock(const Message *msg);
140     void OnScreenLock(const Message *msg);
141     int32_t OnDisplayOptionalInputMethod();
142     static sptr<AAFwk::IAbilityManager> GetAbilityManagerService();
143     void SubscribeCommonEvent();
144     int32_t Switch(int32_t userId, const std::string &bundleName, const std::shared_ptr<ImeInfo> &info);
145     int32_t SwitchExtension(int32_t userId, const std::shared_ptr<ImeInfo> &info);
146     int32_t SwitchSubType(int32_t userId, const std::shared_ptr<ImeInfo> &info);
147     int32_t SwitchInputType(int32_t userId, const SwitchInfo &switchInfo);
148     void GetValidSubtype(const std::string &subName, const std::shared_ptr<ImeInfo> &info);
149     ServiceRunningState state_;
150     void InitServiceHandler();
151     void UpdateUserInfo(int32_t userId);
152     void HandleWmsConnected(int32_t userId, int32_t screenId);
153     void HandleWmsDisconnected(int32_t userId, int32_t screenId);
154     void HandleScbStarted(int32_t userId, int32_t screenId);
155     void HandleUserSwitched(int32_t userId);
156     void HandleWmsStarted();
157     void HandleMemStarted();
158     void HandleDataShareReady();
159     void HandleOsAccountStarted();
160     void HandleFocusChanged(bool isFocused, uint64_t displayId, int32_t pid, int32_t uid);
161     void HandleImeCfgCapsState();
162     void HandlePasteboardStarted();
163     void StopImeInBackground();
164     int32_t InitAccountMonitor();
165     static std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_;
166     int32_t userId_;
167     bool stop_ = false;
168     void InitMonitors();
169     int32_t InitKeyEventMonitor();
170     bool InitWmsMonitor();
171     void InitSystemLanguageMonitor();
172     bool InitMemMgrMonitor();
173     void InitWmsConnectionMonitor();
174     void InitFocusChangedMonitor();
175     void InitWindowDisplayChangedMonitor();
176     bool InitPasteboardMonitor();
177     int32_t SwitchByCombinationKey(uint32_t state);
178     int32_t SwitchMode();
179     int32_t SwitchLanguage();
180     int32_t SwitchType();
181     int32_t GenerateClientInfo(int32_t userId, InputClientInfo &clientInfo);
182     void RegisterSecurityModeObserver();
183     int32_t CheckInputTypeOption(int32_t userId, InputClientInfo &inputClientInfo);
184     int32_t IsDefaultImeFromTokenId(int32_t userId, uint32_t tokenId);
185     void DealSwitchRequest();
186     bool IsCurrentIme(int32_t userId, uint32_t tokenId);
187     int32_t StartInputType(int32_t userId, InputType type);
188     // if switch input type need to switch ime, then no need to hide panel first.
189     void NeedHideWhenSwitchInputType(int32_t userId, InputType type, bool &needHide);
190     bool GetDeviceFunctionKeyState(int32_t functionKey, bool &isEnable);
191     bool ModifyImeCfgWithWrongCaps();
192     void HandleBundleScanFinished();
193     int32_t StartInputInner(InputClientInfo &inputClientInfo, std::vector<sptr<IRemoteObject>> &agents,
194         std::vector<BindImeInfo> &imeInfos);
195     int32_t ShowInputInner(sptr<IInputClient> client, int32_t requestKeyboardReason = 0);
196     int32_t ShowCurrentInputInner();
197     std::pair<int64_t, std::string> GetCurrentImeInfoForHiSysEvent(int32_t userId);
198     static InputType GetSecurityInputType(const InputClientInfo &inputClientInfo);
199     int32_t StartSecurityIme(int32_t &userId, InputClientInfo &inputClientInfo);
200     int32_t GetScreenLockIme(int32_t userId, std::string &ime);
201     int32_t GetAlternativeIme(int32_t userId, std::string &ime);
202 #ifdef IMF_ON_DEMAND_START_STOP_SA_ENABLE
203     int64_t GetTickCount();
204     void ResetDelayUnloadTask(uint32_t code = 0);
205     bool IsImeInUse();
206 #endif
207     std::mutex checkMutex_;
208     int32_t EnableIme(int32_t userId, const std::string &bundleName, const std::string &extensionName = "",
209         EnabledStatus status = EnabledStatus::BASIC_MODE);
210     void OnCurrentImeStatusChanged(int32_t userId, const std::string &bundleName, EnabledStatus newStatus);
211     void DataShareCallback(const std::string &key);
212     bool IsValidBundleName(const std::string &bundleName);
213     std::string GetRestoreBundleName(MessageParcel &data);
214     int32_t RestoreInputmethod(std::string &bundleName);
215     void IncreaseAttachCount();
216     void DecreaseAttachCount();
217     bool IsTmpIme(int32_t userId, uint32_t tokenId);
218     bool IsTmpImeSwitchSubtype(int32_t userId, uint32_t tokenId, const SwitchInfo &switchInfo);
219 
220     class AttachStateGuard {
221     public:
AttachStateGuard(InputMethodSystemAbility & ability)222         explicit AttachStateGuard(InputMethodSystemAbility &ability) : ability_(ability)
223         {
224             ability_.IncreaseAttachCount();
225         }
~AttachStateGuard()226         ~AttachStateGuard()
227         {
228             ability_.DecreaseAttachCount();
229         }
230 
231     private:
232         InputMethodSystemAbility &ability_;
233     };
234 
235     std::atomic<bool> isBundleScanFinished_ = false;
236     std::atomic<bool> isScbEnable_ = false;
237     std::mutex switchImeMutex_;
238     std::atomic<bool> switchTaskExecuting_ = false;
239     std::atomic<uint32_t> targetSwitchCount_ = 0;
240 };
241 } // namespace MiscServices
242 } // namespace OHOS
243 #endif // SERVICES_INCLUDE_INPUT_METHOD_SYSTEM_ABILITY_H
244