diff --git a/frameworks/inputmethod_ability/include/input_method_ability.h b/frameworks/inputmethod_ability/include/input_method_ability.h index e93b84f..2969381 100644 --- a/frameworks/inputmethod_ability/include/input_method_ability.h +++ b/frameworks/inputmethod_ability/include/input_method_ability.h @@ -44,8 +44,8 @@ namespace MiscServices { static sptr GetInstance(); sptr OnConnect(); bool InsertText(const std::string text); - void setImeListener(sptr &imeListener); - void setKdListener(sptr &kdListener); + void setImeListener(sptr imeListener); + void setKdListener(sptr kdListener); void DeleteForward(int32_t length); void DeleteBackward(int32_t length); void HideKeyboardSelf(); diff --git a/frameworks/inputmethod_ability/src/input_method_ability.cpp b/frameworks/inputmethod_ability/src/input_method_ability.cpp index 5003cc1..b70441b 100644 --- a/frameworks/inputmethod_ability/src/input_method_ability.cpp +++ b/frameworks/inputmethod_ability/src/input_method_ability.cpp @@ -117,7 +117,7 @@ namespace MiscServices { SetCoreAndAgent(); } - void InputMethodAbility::setImeListener(sptr &imeListener) + void InputMethodAbility::setImeListener(sptr imeListener) { IMSA_HILOGI("InputMethodAbility::setImeListener"); if (!imeListener_) { @@ -125,7 +125,7 @@ namespace MiscServices { } } - void InputMethodAbility::setKdListener(sptr &kdListener) + void InputMethodAbility::setKdListener(sptr kdListener) { IMSA_HILOGI("InputMethodAbility::setKdListener"); if (!kdListener_) { diff --git a/frameworks/inputmethod_controller/src/input_method_system_ability_proxy.cpp b/frameworks/inputmethod_controller/src/input_method_system_ability_proxy.cpp index a9a034e..80ea79f 100644 --- a/frameworks/inputmethod_controller/src/input_method_system_ability_proxy.cpp +++ b/frameworks/inputmethod_controller/src/input_method_system_ability_proxy.cpp @@ -440,7 +440,7 @@ namespace MiscServices { int32_t InputMethodSystemAbilityProxy::SwitchInputMethod(InputMethodProperty* target) { - IMSA_HILOGI("InputMethodSystemAbilityProxy::switchInputMethod"); + IMSA_HILOGI("InputMethodSystemAbilityProxy::SwitchInputMethod"); MessageParcel data, reply; MessageOption option; @@ -454,8 +454,9 @@ namespace MiscServices { return false; } delete target; - auto ret = Remote()->SendRequest(SWITCH_INPUT_METHOD, data, reply, option); - ret = reply.ReadInt32(); + Remote()->SendRequest(SWITCH_INPUT_METHOD, data, reply, option); + int32_t ret = reply.ReadInt32(); + IMSA_HILOGE("InputMethodSystemAbilityProxy::switchInputMethod ret = %{public}d", ret); return ret; } } // namespace MiscServices diff --git a/interfaces/kits/js/napi/inputmethod/src/js_input_method_registry.cpp b/interfaces/kits/js/napi/inputmethod/src/js_input_method_registry.cpp index 1d333a8..a885001 100644 --- a/interfaces/kits/js/napi/inputmethod/src/js_input_method_registry.cpp +++ b/interfaces/kits/js/napi/inputmethod/src/js_input_method_registry.cpp @@ -109,9 +109,10 @@ namespace MiscServices { bool isSwitchSuccess = false; if (!InputMethodController::GetInstance()->SwitchInputMethod(target)) { + IMSA_HILOGE("JsInputMethodRegistry::OnSwitchInputMethod success!"); isSwitchSuccess = true; } else { - IMSA_HILOGE("JsInputMethodRegistry::OnSwitchInputMethod isSwitchSuccess is false !"); + IMSA_HILOGE("JsInputMethodRegistry::OnSwitchInputMethod failed!"); } NativeValue *result = CreateJsValue(engine, isSwitchSuccess); diff --git a/interfaces/kits/js/napi/inputmethodengine/include/js_input_method_engine.h b/interfaces/kits/js/napi/inputmethodengine/include/js_input_method_engine.h index 716c809..f333055 100644 --- a/interfaces/kits/js/napi/inputmethodengine/include/js_input_method_engine.h +++ b/interfaces/kits/js/napi/inputmethodengine/include/js_input_method_engine.h @@ -26,14 +26,14 @@ namespace OHOS { class JsInputMethodEngine { public: JsInputMethodEngine(NativeEngine* engine); - ~JsInputMethodEngine() = default; + ~JsInputMethodEngine(); static void Finalizer(NativeEngine* engine, void* data, void* hint); static NativeValue* RegisterCallback(NativeEngine* engine, NativeCallbackInfo* info); static NativeValue* UnRegisterCallback(NativeEngine* engine, NativeCallbackInfo* info); private: - sptr imeListener_; std::mutex mtx_; + sptr imeListener_; NativeValue* OnRegisterCallback(NativeEngine& engine, NativeCallbackInfo& info); NativeValue* OnUnRegisterCallback(NativeEngine& engine, NativeCallbackInfo& info); std::shared_ptr GetMainHandler(); diff --git a/interfaces/kits/js/napi/inputmethodengine/include/js_input_method_engine_listener.h b/interfaces/kits/js/napi/inputmethodengine/include/js_input_method_engine_listener.h index e994ae4..0e00b83 100644 --- a/interfaces/kits/js/napi/inputmethodengine/include/js_input_method_engine_listener.h +++ b/interfaces/kits/js/napi/inputmethodengine/include/js_input_method_engine_listener.h @@ -32,7 +32,7 @@ namespace MiscServices { explicit JsInputMethodEngineListener(NativeEngine* engine) : engine_(engine) {} JsInputMethodEngineListener(NativeEngine* engine, std::shared_ptr &handler) : engine_(engine), mainHandler_(handler) {} - virtual ~JsInputMethodEngineListener() = default; + virtual ~JsInputMethodEngineListener(); void RegisterListenerWithType(NativeEngine& engine, std::string type, NativeValue* value); void UnregisterListenerWithType(std::string type, NativeValue* value); void UnregisterAllListenerWithType(std::string type); @@ -42,14 +42,15 @@ namespace MiscServices { void OnSetCallingWindow(uint32_t windowId); private: + std::mutex mMutex; + static std::recursive_mutex mapMutex; void AddCallback(std::string type, NativeValue* jsListenerObject); void CallJsMethod(std::string methodName, NativeValue* const* argv = nullptr, size_t argc = 0); bool CallJsMethodReturnBool(std::string methodName, NativeValue* const* argv = nullptr, size_t argc = 0); void RemoveCallback(NativeValue* jsListenerObject); bool IfCallbackRegistered(std::string type, NativeValue* jsListenerObject); NativeEngine* engine_ = nullptr; - std::mutex mMutex; - std::map>> jsCbMap_; + std::map>> jsCbMap_; std::shared_ptr mainHandler_ = nullptr; }; } // namespace MiscServices diff --git a/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine.cpp b/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine.cpp index 236073e..abfc08d 100644 --- a/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine.cpp +++ b/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine.cpp @@ -34,6 +34,8 @@ namespace MiscServices { InputMethodAbility::GetInstance()->setImeListener(imeListener_); } + JsInputMethodEngine::~JsInputMethodEngine() {} + void JsInputMethodEngine::Finalizer(NativeEngine* engine, void* data, void* hint) { IMSA_HILOGI("JsInputMethodEngine::Finalizer is called"); diff --git a/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine_listener.cpp b/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine_listener.cpp index bbc1b05..2252e2b 100644 --- a/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine_listener.cpp +++ b/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine_listener.cpp @@ -20,6 +20,7 @@ namespace OHOS { namespace MiscServices { using namespace AbilityRuntime; + std::recursive_mutex JsInputMethodEngineListener::mapMutex{}; void JsInputMethodEngineListener::RegisterListenerWithType(NativeEngine& engine, std::string type, NativeValue* value) { @@ -28,72 +29,62 @@ namespace MiscServices { IMSA_HILOGI("JsInputMethodEngineListener::RegisterListenerWithType callback already registered!"); return; } - std::unique_ptr callbackRef; - callbackRef.reset(engine.CreateReference(value, 1)); - AddCallback(type, value); } + JsInputMethodEngineListener::~JsInputMethodEngineListener() + { + IMSA_HILOGE("JsInputMethodEngineListener::~JsInputMethodEngineListener"); + } + void JsInputMethodEngineListener::AddCallback(std::string type, NativeValue* jsListenerObject) { - IMSA_HILOGI("JsInputMethodEngineListener::AddCallback is called"); + IMSA_HILOGI("JsInputMethodEngineListener::AddCallback is called : %{public}s", type.c_str()); std::lock_guard lock(mMutex); - std::unique_ptr callbackRef; - callbackRef.reset(engine_->CreateReference(jsListenerObject, 1)); - jsCbMap_[type].push_back(std::move(callbackRef)); + std::shared_ptr callbackRef(engine_->CreateReference(jsListenerObject, 1)); + if (callbackRef == nullptr) { + IMSA_HILOGI("JsInputMethodEngineListener::AddCallback fail, callbackRef is nullptr."); + return; + } + std::lock_guard mapLock(mapMutex); + jsCbMap_[type].push_back(callbackRef); IMSA_HILOGI("JsInputMethodEngineListener::AddCallback success"); IMSA_HILOGI("jsCbMap_ size: %{public}d, and type[%{public}s] size: %{public}d!", static_cast(jsCbMap_.size()), type.c_str(), static_cast(jsCbMap_[type].size())); - return; } void JsInputMethodEngineListener::UnregisterAllListenerWithType(std::string type) { - IMSA_HILOGI("JsInputMethodEngineListener::UnregisterAllListenerWithType"); + IMSA_HILOGI("JsInputMethodEngineListener::UnregisterAllListenerWithType : %{public}s.", type.c_str()); // should do type check + std::lock_guard mapLock(mapMutex); if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) { IMSA_HILOGI("methodName %{public}s not registerted!", type.c_str()); return; } - for (auto it = jsCbMap_[type].begin(); it != jsCbMap_[type].end();) { - jsCbMap_[type].erase(it); - } - // one type with multi jscallback, erase type when there is no callback in one type - if (jsCbMap_[type].empty()) { - jsCbMap_.erase(type); - } - return; + jsCbMap_.erase(type); } void JsInputMethodEngineListener::UnregisterListenerWithType(std::string type, NativeValue* value) { - IMSA_HILOGI("JsInputMethodEngineListener::UnregisterListenerWithType"); + IMSA_HILOGI("JsInputMethodEngineListener::UnregisterListenerWithType : %{public}s.", type.c_str()); // should do type check + std::lock_guard mapLock(mapMutex); if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) { IMSA_HILOGI("methodName %{public}s not registerted!", type.c_str()); return; } - for (auto it = jsCbMap_[type].begin(); it != jsCbMap_[type].end();) { - if (value->StrictEquals((*it)->Get())) { - jsCbMap_[type].erase(it); - break; - } - } - // one type with multi jscallback, erase type when there is no callback in one type - if (jsCbMap_[type].empty()) { - jsCbMap_.erase(type); - } - return; + jsCbMap_.erase(type); } bool JsInputMethodEngineListener::IfCallbackRegistered(std::string type, NativeValue* jsListenerObject) { IMSA_HILOGI("JsInputMethodEngineListener::IfCallbackRegistered"); + std::lock_guard mapLock(mapMutex); if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) { IMSA_HILOGI("methodName %{public}s not registertd!", type.c_str()); return false; } - for (auto iter = jsCbMap_[type].begin(); iter != jsCbMap_[type].end(); iter++) { if (jsListenerObject->StrictEquals((*iter)->Get())) { IMSA_HILOGI("JsInputMethodEngineListener::IfCallbackRegistered callback already registered!"); @@ -105,19 +96,22 @@ namespace MiscServices { void JsInputMethodEngineListener::CallJsMethod(std::string methodName, NativeValue* const* argv, size_t argc) { - IMSA_HILOGI("JsInputMethodEngineListener::CallJsMethod"); + IMSA_HILOGI("JsInputMethodEngineListener::CallJsMethod : %{public}s", methodName.c_str()); if (!engine_) { IMSA_HILOGI("engine_ nullptr"); return; } - + std::lock_guard mapLock(mapMutex); if (jsCbMap_.empty() || jsCbMap_.find(methodName) == jsCbMap_.end()) { IMSA_HILOGI("methodName %{public}s not registertd!", methodName.c_str()); return; } - - for (auto iter = jsCbMap_[methodName].begin(); iter != jsCbMap_[methodName].end(); iter++) { - engine_->CallFunction(engine_->CreateUndefined(), (*iter)->Get(), argv, argc); + for (auto iter : jsCbMap_[methodName]) { + if (iter == nullptr) { + IMSA_HILOGE("JsInputMethodEngineListener::CallJsMethod iter is null!"); + return; + } + engine_->CallFunction(engine_->CreateUndefined(), iter->Get(), argv, argc); } } @@ -129,15 +123,16 @@ namespace MiscServices { IMSA_HILOGI("engine_ nullptr"); return false; } - + std::lock_guard mapLock(mapMutex); if (jsCbMap_.empty() || jsCbMap_.find(methodName) == jsCbMap_.end()) { - IMSA_HILOGI("methodName %{public}s not registertd!", methodName.c_str()); + IMSA_HILOGI("methodName %{public}s not registered!", methodName.c_str()); return false; } bool result = false; - for (auto iter = jsCbMap_[methodName].begin(); iter != jsCbMap_[methodName].end(); iter++) { - NativeValue* nativeValue = engine_->CallFunction(engine_->CreateUndefined(), (*iter)->Get(), argv, argc); + for (auto iter : jsCbMap_[methodName]) { + NativeValue *nativeValue = + engine_->CallFunction(engine_->CreateUndefined(), iter->Get(), argv, argc); bool ret = false; if (ConvertFromJsValue(*engine_, nativeValue, ret) && ret) { result = true; diff --git a/services/include/input_method_system_ability_stub.h b/services/include/input_method_system_ability_stub.h index 20f3386..98bcc32 100644 --- a/services/include/input_method_system_ability_stub.h +++ b/services/include/input_method_system_ability_stub.h @@ -37,7 +37,7 @@ namespace MiscServices { void HideCurrentInput(MessageParcel& data) override; void displayOptionalInputMethod(MessageParcel& data) override; virtual int32_t listInputMethodByUserId(int32_t userId, std::vector *properties) = 0; - int32_t SwitchInputMethod(MessageParcel &data, MessageParcel &reply); + int32_t SwitchInputMethod(MessageParcel &data); protected: int32_t getUserId(int32_t uid); diff --git a/services/include/message.h b/services/include/message.h index c981690..902e62f 100644 --- a/services/include/message.h +++ b/services/include/message.h @@ -25,7 +25,7 @@ namespace OHOS { namespace MiscServices { class Message { public: - int32_t msgId_; // message id + int32_t msgId_{}; // message id MessageParcel *msgContent_ = nullptr; // message content MessageParcel *msgReply_ = nullptr; // message reply Message(int32_t msgId, MessageParcel *msgContent); diff --git a/services/src/input_method_system_ability.cpp b/services/src/input_method_system_ability.cpp index 38b8543..862ccf0 100644 --- a/services/src/input_method_system_ability.cpp +++ b/services/src/input_method_system_ability.cpp @@ -290,9 +290,10 @@ namespace MiscServices { want.SetElementName(imeId.substr(0, pos), imeId.substr(pos + 1)); int32_t result = abms->StartAbility(want); if (result) { - IMSA_HILOGE("InputMethodSystemAbility::StartInputService fail. result = %{public}d", result); + IMSA_HILOGE("InputMethodSystemAbility::StartInputService fail."); isStartSuccess = false; } else { + IMSA_HILOGE("InputMethodSystemAbility::StartInputService success."); isStartSuccess = true; } } @@ -302,7 +303,7 @@ namespace MiscServices { auto callback = [this, imeId]() { StartInputService(imeId); }; serviceHandler_->PostTask(callback, INIT_INTERVAL); } - return isStartSuccess; + return isStartSuccess; } void InputMethodSystemAbility::StopInputService(std::string imeId) @@ -633,8 +634,7 @@ namespace MiscServices { MessageParcel *data = msg->msgContent_; int32_t userId = data->ReadInt32(); InputMethodProperty *target = InputMethodProperty::Unmarshalling(*data); - auto ret = OnSwitchInputMethod(userId, target); - msg->msgReply_->WriteInt32(ret); + OnSwitchInputMethod(userId, target); break; } default: { @@ -990,7 +990,7 @@ namespace MiscServices { } std::string defaultIme = ParaHandle::GetDefaultIme(userId_); - std::string targetIme = ""; + std::string targetIme; std::string imeId = Str16ToStr8(target->mPackageName) + "/" + Str16ToStr8(target->mAbilityName); targetIme += imeId; IMSA_HILOGI("InputMethodSystemAbility::OnSwitchInputMethod DefaultIme : %{public}s, TargetIme : %{public}s", diff --git a/services/src/input_method_system_ability_stub.cpp b/services/src/input_method_system_ability_stub.cpp index 3ca4210..8746a65 100644 --- a/services/src/input_method_system_ability_stub.cpp +++ b/services/src/input_method_system_ability_stub.cpp @@ -171,7 +171,8 @@ namespace MiscServices { break; } case SWITCH_INPUT_METHOD: { - SwitchInputMethod(data, reply); + int32_t ret = SwitchInputMethod(data); + reply.WriteInt32(ret); break; } default: { @@ -308,13 +309,13 @@ namespace MiscServices { MessageHandler::Instance()->SendMessage(msg); } - int32_t InputMethodSystemAbilityStub::SwitchInputMethod(MessageParcel &data, MessageParcel &reply) + int32_t InputMethodSystemAbilityStub::SwitchInputMethod(MessageParcel &data) { IMSA_HILOGI("InputMethodSystemAbilityStub::switchInputMethod"); int32_t uid = IPCSkeleton::GetCallingUid(); int32_t userId = getUserId(uid); - MessageParcel *parcel = new MessageParcel(); + auto *parcel = new MessageParcel(); InputMethodProperty *target = InputMethodProperty::Unmarshalling(data); parcel->WriteInt32(userId); if (!target->Marshalling(*parcel)) { @@ -324,7 +325,7 @@ namespace MiscServices { return ErrorCode::ERROR_IME_PROPERTY_MARSHALL; } delete target; - Message *msg = new Message(MSG_ID_SWITCH_INPUT_METHOD, parcel, &reply); + auto *msg = new Message(MSG_ID_SWITCH_INPUT_METHOD, parcel); MessageHandler::Instance()->SendMessage(msg); return ErrorCode::NO_ERROR; }