1 /*
2  * Copyright (c) 2022 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 FOUNDATION_ABILITYRUNTIME_OHOS_JS_INPUTMETHOD_EXTENSION_H
17 #define FOUNDATION_ABILITYRUNTIME_OHOS_JS_INPUTMETHOD_EXTENSION_H
18 
19 #include "configuration.h"
20 #include "display_manager.h"
21 #include "inputmethod_extension.h"
22 #include "js_runtime.h"
23 #include "system_ability_status_change_stub.h"
24 
25 namespace OHOS {
26 namespace AbilityRuntime {
27 /**
28  * @brief Basic inputmethod components.
29  */
30 class JsInputMethodExtension : public InputMethodExtension {
31 public:
32     JsInputMethodExtension(JsRuntime &jsRuntime);
33     virtual ~JsInputMethodExtension() override;
34     static JsInputMethodExtension *jsInputMethodExtension;
35     /**
36      * @brief Create JsInputMethodExtension.
37      *
38      * @param runtime The runtime.
39      * @return The JsInputMethodExtension instance.
40      */
41     static JsInputMethodExtension *Create(const std::unique_ptr<Runtime> &runtime);
42 
43     /**
44      * @brief Init the extension.
45      *
46      * @param record the extension record.
47      * @param application the application info.
48      * @param handler the extension handler.
49      * @param token the remote token.
50      */
51     virtual void Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &record,
52         const std::shared_ptr<AppExecFwk::OHOSApplication> &application,
53         std::shared_ptr<AppExecFwk::AbilityHandler> &handler, const sptr<IRemoteObject> &token) override;
54 
55     /**
56      * @brief Called when this extension is started. You must override this function if you want to perform some
57      *        initialization operations during extension startup.
58      *
59      * This function can be called only once in the entire lifecycle of an extension.
60      * @param Want Indicates the {@link Want} structure containing startup information about the extension.
61      */
62     virtual void OnStart(const AAFwk::Want &want) override;
63 
64     /**
65      * @brief Called when this InputMethod extension is connected for the first time.
66      *
67      * You can override this function to implement your own processing logic.
68      *
69      * @param want Indicates the {@link Want} structure containing connection information about the InputMethod
70      *        extension.
71      *
72      * @return Returns a pointer to the <b>sid</b> of the connected InputMethod extension.
73      */
74     virtual sptr<IRemoteObject> OnConnect(const AAFwk::Want &want) override;
75 
76     /**
77      * @brief Called when all abilities connected to this InputMethod extension are disconnected.
78      *
79      * You can override this function to implement your own processing logic.
80      *
81      */
82     virtual void OnDisconnect(const AAFwk::Want &want) override;
83 
84     /**
85      * @brief Called back when InputMethod is started.
86      * This method can be called only by InputMethod. You can use the StartAbility(ohos.aafwk.content.Want) method
87      *        to start InputMethod. Then the system calls back the current method to use the transferred want parameter
88      *        to execute its own logic.
89      *
90      * @param want Indicates the want of InputMethod to start.
91      * @param restart Indicates the startup mode. The value true indicates that InputMethod is restarted after being
92      *        destroyed, and the value false indicates a normal startup.
93      * @param startId Indicates the number of times the InputMethod extension has been started. The startId is
94      *        incremented.
95      * by 1 every time the extension is started. For example, if the extension has been started for six times, the
96      * value of startId is 6.
97      */
98     virtual void OnCommand(const AAFwk::Want &want, bool restart, int startId) override;
99 
100     /**
101      * @brief Called when this extension enters the <b>STATE_STOP</b> state.
102      *
103      * The extension in the <b>STATE_STOP</b> is being destroyed.
104      * You can override this function to implement your own processing logic.
105      */
106     virtual void OnStop() override;
107 
108     /**
109      * @brief Called when the system configuration is updated.
110      *
111      * @param configuration Indicates the updated configuration information.
112      */
113     virtual void OnConfigurationUpdated(const AppExecFwk::Configuration& config) override;
114 
115     /**
116      * @brief Called when configuration changed, including system configuration and window configuration.
117      *
118      */
119     void ConfigurationUpdated();
120 
121 private:
122     napi_value CallObjectMethod(const char *name, const napi_value *argv = nullptr, size_t argc = 0);
123 
124     void BindContext(napi_env env, napi_value obj);
125 
126     void GetSrcPath(std::string &srcPath);
127 
128     void ListenWindowManager();
129 
130     JsRuntime &jsRuntime_;
131     std::unique_ptr<NativeReference> jsObj_;
132     std::shared_ptr<NativeReference> shellContextRef_ = nullptr;
133     std::shared_ptr<AbilityHandler> handler_ = nullptr;
134 
135 protected:
136     class JsInputMethodExtensionDisplayListener : public Rosen::DisplayManager::IDisplayListener {
137     public:
JsInputMethodExtensionDisplayListener(const std::weak_ptr<JsInputMethodExtension> & extension)138         explicit JsInputMethodExtensionDisplayListener(const std::weak_ptr<JsInputMethodExtension> &extension)
139         {
140             jsInputMethodExtension_ = extension;
141         }
142 
OnCreate(Rosen::DisplayId displayId)143         void OnCreate(Rosen::DisplayId displayId) override
144         {
145             auto inputMethodSptr = jsInputMethodExtension_.lock();
146             if (inputMethodSptr != nullptr) {
147                 inputMethodSptr->OnCreate(displayId);
148             }
149         }
150 
OnDestroy(Rosen::DisplayId displayId)151         void OnDestroy(Rosen::DisplayId displayId) override
152         {
153             auto inputMethodSptr = jsInputMethodExtension_.lock();
154             if (inputMethodSptr != nullptr) {
155                 inputMethodSptr->OnDestroy(displayId);
156             }
157         }
158 
OnChange(Rosen::DisplayId displayId)159         void OnChange(Rosen::DisplayId displayId) override
160         {
161             auto inputMethodSptr = jsInputMethodExtension_.lock();
162             if (inputMethodSptr != nullptr) {
163                 inputMethodSptr->OnChange(displayId);
164             }
165         }
166 
167     private:
168         std::weak_ptr<JsInputMethodExtension> jsInputMethodExtension_;
169     };
170 
171     void OnCreate(Rosen::DisplayId displayId);
172     void OnDestroy(Rosen::DisplayId displayId);
173     void OnChange(Rosen::DisplayId displayId);
174 
175 private:
176     class SystemAbilityStatusChangeListener : public OHOS::SystemAbilityStatusChangeStub {
177     public:
SystemAbilityStatusChangeListener(sptr<JsInputMethodExtensionDisplayListener> displayListener)178         SystemAbilityStatusChangeListener(sptr<JsInputMethodExtensionDisplayListener> displayListener)
179             : listener_(displayListener) {};
180         virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)181         virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override {}
182 
183     private:
184         sptr<JsInputMethodExtensionDisplayListener> listener_ = nullptr;
185     };
186 
187     sptr<JsInputMethodExtensionDisplayListener> displayListener_ = nullptr;
188 };
189 } // namespace AbilityRuntime
190 } // namespace OHOS
191 #endif // FOUNDATION_ABILITYRUNTIME_OHOS_JS_INPUTMETHOD_EXTENSION_H