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 "inputmethod_extension.h" 20 #include "js_runtime.h" 21 #include "native_engine/native_value.h" 22 23 namespace OHOS { 24 namespace AbilityRuntime { 25 /** 26 * @brief Basic inputmethod components. 27 */ 28 class JsInputMethodExtension : public InputMethodExtension, 29 public std::enable_shared_from_this<JsInputMethodExtension> { 30 public: 31 JsInputMethodExtension(JsRuntime &jsRuntime); 32 virtual ~JsInputMethodExtension() override; 33 static JsInputMethodExtension *jsInputMethodExtension; 34 /** 35 * @brief Create JsInputMethodExtension. 36 * 37 * @param runtime The runtime. 38 * @return The JsInputMethodExtension instance. 39 */ 40 static JsInputMethodExtension *Create(const std::unique_ptr<Runtime> &runtime); 41 42 /** 43 * @brief Init the extension. 44 * 45 * @param record the extension record. 46 * @param application the application info. 47 * @param handler the extension handler. 48 * @param token the remote token. 49 */ 50 virtual void Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &record, 51 const std::shared_ptr<AppExecFwk::OHOSApplication> &application, 52 std::shared_ptr<AppExecFwk::AbilityHandler> &handler, const sptr<IRemoteObject> &token) override; 53 54 /** 55 * @brief Called when this extension is started. You must override this function if you want to perform some 56 * initialization operations during extension startup. 57 * 58 * This function can be called only once in the entire lifecycle of an extension. 59 * @param Want Indicates the {@link Want} structure containing startup information about the extension. 60 */ 61 virtual void OnStart(const AAFwk::Want &want) override; 62 63 /** 64 * @brief Called when this InputMethod extension is connected for the first time. 65 * 66 * You can override this function to implement your own processing logic. 67 * 68 * @param want Indicates the {@link Want} structure containing connection information about the InputMethod 69 * extension. 70 * 71 * @return Returns a pointer to the <b>sid</b> of the connected InputMethod extension. 72 */ 73 virtual sptr<IRemoteObject> OnConnect(const AAFwk::Want &want) override; 74 75 /** 76 * @brief Called when all abilities connected to this InputMethod extension are disconnected. 77 * 78 * You can override this function to implement your own processing logic. 79 * 80 */ 81 virtual void OnDisconnect(const AAFwk::Want &want) override; 82 83 /** 84 * @brief Called back when InputMethod is started. 85 * This method can be called only by InputMethod. You can use the StartAbility(ohos.aafwk.content.Want) method 86 * to start InputMethod. Then the system calls back the current method to use the transferred want parameter 87 * to execute its own logic. 88 * 89 * @param want Indicates the want of InputMethod to start. 90 * @param restart Indicates the startup mode. The value true indicates that InputMethod is restarted after being 91 * destroyed, and the value false indicates a normal startup. 92 * @param startId Indicates the number of times the InputMethod extension has been started. The startId is 93 * incremented. 94 * by 1 every time the extension is started. For example, if the extension has been started for six times, the 95 * value of startId is 6. 96 */ 97 virtual void OnCommand(const AAFwk::Want &want, bool restart, int startId) override; 98 99 /** 100 * @brief Called when this extension enters the <b>STATE_STOP</b> state. 101 * 102 * The extension in the <b>STATE_STOP</b> is being destroyed. 103 * You can override this function to implement your own processing logic. 104 */ 105 virtual void OnStop() override; 106 107 private: 108 NativeValue *CallObjectMethod(const char *name, NativeValue *const *argv = nullptr, size_t argc = 0); 109 110 void BindContext(NativeEngine &engine, NativeObject *obj); 111 112 void GetSrcPath(std::string &srcPath); 113 114 JsRuntime &jsRuntime_; 115 std::unique_ptr<NativeReference> jsObj_; 116 }; 117 } // namespace AbilityRuntime 118 } // namespace OHOS 119 #endif // FOUNDATION_ABILITYRUNTIME_OHOS_JS_INPUTMETHOD_EXTENSION_H