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 #include "js_keyboard_delegate.h" 16 #include "input_method_controller.h" 17 #include "napi/native_api.h" 18 #include "napi/native_node_api.h" 19 #include "event_handler.h" 20 #include "event_runner.h" 21 #include "string_ex.h" 22 #include "js_keyboard_delegate_listener.h" 23 #include "input_method_ability.h" 24 #include "global.h" 25 namespace OHOS { 26 namespace MiscServices { 27 using namespace AbilityRuntime; 28 constexpr size_t ARGC_TWO = 2; JsKeyboardDelegate(NativeEngine * engine)29 JsKeyboardDelegate::JsKeyboardDelegate(NativeEngine* engine) 30 { 31 IMSA_HILOGI("JsKeyboardDelegate::Constructor is called"); 32 auto mainHandler = GetMainHandler(); 33 kdListener_ = new JsKeyboardDelegateListener(engine, mainHandler); 34 InputMethodAbility::GetInstance()->setKdListener(kdListener_); 35 } 36 Finalizer(NativeEngine * engine,void * data,void * hint)37 void JsKeyboardDelegate::Finalizer(NativeEngine* engine, void* data, void* hint) 38 { 39 IMSA_HILOGI("JsKeyboardDelegate::Finalizer is called"); 40 std::unique_ptr<JsKeyboardDelegate>(static_cast<JsKeyboardDelegate*>(data)); 41 } 42 GetMainHandler()43 std::shared_ptr<AppExecFwk::EventHandler> JsKeyboardDelegate::GetMainHandler() 44 { 45 if (!mainHandler_) { 46 mainHandler_ = 47 std::make_shared<AppExecFwk::EventHandler>(AppExecFwk::EventRunner::GetMainEventRunner()); 48 } 49 return mainHandler_; 50 } 51 RegisterCallback(NativeEngine * engine,NativeCallbackInfo * info)52 NativeValue* JsKeyboardDelegate::RegisterCallback(NativeEngine* engine, NativeCallbackInfo* info) 53 { 54 JsKeyboardDelegate* me = CheckParamsAndGetThis<JsKeyboardDelegate>(engine, info); 55 return (me != nullptr) ? me->OnRegisterCallback(*engine, *info) : nullptr; 56 } 57 UnRegisterCallback(NativeEngine * engine,NativeCallbackInfo * info)58 NativeValue* JsKeyboardDelegate::UnRegisterCallback(NativeEngine* engine, NativeCallbackInfo* info) 59 { 60 JsKeyboardDelegate* me = CheckParamsAndGetThis<JsKeyboardDelegate>(engine, info); 61 return (me != nullptr) ? me->OnUnRegisterCallback(*engine, *info) : nullptr; 62 } 63 OnRegisterCallback(NativeEngine & engine,NativeCallbackInfo & info)64 NativeValue* JsKeyboardDelegate::OnRegisterCallback(NativeEngine& engine, NativeCallbackInfo& info) 65 { 66 IMSA_HILOGI("JsKeyboardDelegate::OnRegisterCallback is called!"); 67 if (info.argc != ARGC_TWO) { 68 IMSA_HILOGI("JsKeyboardDelegate::OnRegisterCallback Params not match"); 69 return engine.CreateUndefined(); 70 } 71 72 std::string cbType; 73 if (!ConvertFromJsValue(engine, info.argv[0], cbType)) { 74 IMSA_HILOGI("JsKeyboardDelegate::OnRegisterCallback Failed to convert parameter to callbackType"); 75 return engine.CreateUndefined(); 76 } 77 78 NativeValue* value = info.argv[1]; 79 kdListener_->RegisterListenerWithType(engine, cbType, value); 80 return engine.CreateUndefined(); 81 } 82 OnUnRegisterCallback(NativeEngine & engine,NativeCallbackInfo & info)83 NativeValue* JsKeyboardDelegate::OnUnRegisterCallback(NativeEngine& engine, NativeCallbackInfo& info) 84 { 85 IMSA_HILOGI("JsKeyboardDelegate::OnUnRegisterCallback is called!"); 86 if (info.argc == 0) { 87 IMSA_HILOGI("JsKeyboardDelegate::OnUnRegisterCallback Params not match"); 88 return engine.CreateUndefined(); 89 } 90 91 std::string cbType; 92 if (!ConvertFromJsValue(engine, info.argv[0], cbType)) { 93 IMSA_HILOGI("JsKeyboardDelegate::OnUnRegisterCallback Failed to convert parameter to callbackType"); 94 return engine.CreateUndefined(); 95 } 96 97 std::lock_guard<std::mutex> lock(mtx_); 98 99 if (info.argc == 1) { 100 kdListener_->UnregisterAllListenerWithType(cbType); 101 } else { 102 NativeValue* value = info.argv[1]; 103 if (!value->IsCallable()) { 104 IMSA_HILOGI("JsKeyboardDelegate::OnUnregisterWindowManagerCallback info->argv[1] is not callable"); 105 return engine.CreateUndefined(); 106 } 107 kdListener_->UnregisterListenerWithType(cbType, value); 108 } 109 return engine.CreateUndefined(); 110 } 111 } 112 }