From 9ff3ca2202fba262b9df2092d471723fc8bfbb6e Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Tue, 19 Jul 2022 11:55:12 +0800 Subject: [PATCH 1/9] fix SwitchInputMethod error Signed-off-by: zhaolinglan --- .../include/input_method_ability.h | 4 +- .../src/input_method_ability.cpp | 4 +- .../src/input_method_system_ability_proxy.cpp | 7 +- .../src/js_input_method_registry.cpp | 3 +- .../include/js_input_method_engine.h | 4 +- .../include/js_input_method_engine_listener.h | 7 +- .../src/js_input_method_engine.cpp | 2 + .../src/js_input_method_engine_listener.cpp | 73 +++++++++---------- .../input_method_system_ability_stub.h | 2 +- services/include/message.h | 2 +- services/src/input_method_system_ability.cpp | 10 +-- .../src/input_method_system_ability_stub.cpp | 9 ++- 12 files changed, 64 insertions(+), 63 deletions(-) diff --git a/frameworks/inputmethod_ability/include/input_method_ability.h b/frameworks/inputmethod_ability/include/input_method_ability.h index e93b84f7..2969381f 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 5003cc12..b70441b2 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 a9a034e6..80ea79f4 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 1d333a8a..a885001a 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 716c8092..f333055e 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 e994ae4a..0e00b836 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 236073e9..abfc08d3 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 bbc1b05a..2252e2bf 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 20f33863..98bcc321 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 c9816904..902e62f7 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 38b85430..862ccf02 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 3ca42104..8746a65e 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; } From 7f526b8f71971eb2902fccaa944d4d37616c0000 Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Tue, 19 Jul 2022 14:20:30 +0800 Subject: [PATCH 2/9] modification for codecheck Signed-off-by: zhaolinglan --- .../js/napi/inputmethodengine/src/js_input_method_engine.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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 abfc08d3..f382251a 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,7 +34,10 @@ namespace MiscServices { InputMethodAbility::GetInstance()->setImeListener(imeListener_); } - JsInputMethodEngine::~JsInputMethodEngine() {} + JsInputMethodEngine::~JsInputMethodEngine() + { + IMSA_HILOGE("JsInputMethodEngine::~JsInputMethodEngine"); + } void JsInputMethodEngine::Finalizer(NativeEngine* engine, void* data, void* hint) { From 40bf72f4d3b4c335d146e7b765cfd2024bd6d269 Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Tue, 19 Jul 2022 14:36:33 +0800 Subject: [PATCH 3/9] modification for codecheck Signed-off-by: zhaolinglan --- .../src/js_input_method_engine_listener.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 2252e2bf..6cab6634 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 @@ -34,7 +34,7 @@ namespace MiscServices { JsInputMethodEngineListener::~JsInputMethodEngineListener() { - IMSA_HILOGE("JsInputMethodEngineListener::~JsInputMethodEngineListener"); + IMSA_HILOGE("JsInputMethodEngineListener::~JsInputMethodEngineListener"); } void JsInputMethodEngineListener::AddCallback(std::string type, NativeValue* jsListenerObject) @@ -43,8 +43,8 @@ namespace MiscServices { std::lock_guard lock(mMutex); std::shared_ptr callbackRef(engine_->CreateReference(jsListenerObject, 1)); if (callbackRef == nullptr) { - IMSA_HILOGI("JsInputMethodEngineListener::AddCallback fail, callbackRef is nullptr."); - return; + IMSA_HILOGI("JsInputMethodEngineListener::AddCallback fail, callbackRef is nullptr."); + return; } std::lock_guard mapLock(mapMutex); jsCbMap_[type].push_back(callbackRef); From 5a2dabc8b5449fd88d272a22fea187745826c34d Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Tue, 19 Jul 2022 15:00:26 +0800 Subject: [PATCH 4/9] modification for codecheck Signed-off-by: zhaolinglan --- .../inputmethodengine/src/js_input_method_engine_listener.cpp | 2 +- services/include/message.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 6cab6634..3db3b7d3 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,7 +20,7 @@ namespace OHOS { namespace MiscServices { using namespace AbilityRuntime; - std::recursive_mutex JsInputMethodEngineListener::mapMutex{}; + std::recursive_mutex JsInputMethodEngineListener::mapMutex {}; void JsInputMethodEngineListener::RegisterListenerWithType(NativeEngine& engine, std::string type, NativeValue* value) { diff --git a/services/include/message.h b/services/include/message.h index 902e62f7..fbe73e0a 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); From 2c1a5e9f772d273f08c7594350b46bcacb27edcc Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Tue, 19 Jul 2022 19:53:36 +0800 Subject: [PATCH 5/9] modify some code Signed-off-by: zhaolinglan --- .../src/input_method_system_ability_proxy.cpp | 12 ++++---- .../include/js_input_method_engine.h | 2 +- .../include/js_input_method_engine_listener.h | 4 +-- .../src/js_input_method_engine.cpp | 5 ---- .../src/js_input_method_engine_listener.cpp | 30 ++++++++----------- services/include/message.h | 4 +-- services/src/input_method_system_ability.cpp | 8 ++++- .../src/input_method_system_ability_stub.cpp | 2 +- services/src/message.cpp | 4 --- 9 files changed, 32 insertions(+), 39 deletions(-) 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 80ea79f4..d111a8ad 100644 --- a/frameworks/inputmethod_controller/src/input_method_system_ability_proxy.cpp +++ b/frameworks/inputmethod_controller/src/input_method_system_ability_proxy.cpp @@ -445,18 +445,20 @@ namespace MiscServices { MessageOption option; if (!data.WriteInterfaceToken(GetDescriptor())) { - return false; + return ERROR_EX_PARCELABLE; } if (!target->Marshalling(data)) { IMSA_HILOGE("InputMethodSystemAbilityProxy::switchInputMethod Failed to marshall target to data!"); delete target; - return false; + return ERROR_IME_PROPERTY_MARSHALL; } delete target; - Remote()->SendRequest(SWITCH_INPUT_METHOD, data, reply, option); - int32_t ret = reply.ReadInt32(); - IMSA_HILOGE("InputMethodSystemAbilityProxy::switchInputMethod ret = %{public}d", ret); + auto ret = Remote()->SendRequest(SWITCH_INPUT_METHOD, data, reply, option); + if (ret != NO_ERROR) { + return ERROR_STATUS_FAILED_TRANSACTION; + } + ret = reply.ReadInt32(); return ret; } } // namespace MiscServices 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 f333055e..ba3ef113 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,7 +26,7 @@ namespace OHOS { class JsInputMethodEngine { public: JsInputMethodEngine(NativeEngine* engine); - ~JsInputMethodEngine(); + ~JsInputMethodEngine() = default; static void Finalizer(NativeEngine* engine, void* data, void* hint); static NativeValue* RegisterCallback(NativeEngine* engine, NativeCallbackInfo* info); static NativeValue* UnRegisterCallback(NativeEngine* engine, NativeCallbackInfo* info); 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 0e00b836..6ee5e523 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(); + virtual ~JsInputMethodEngineListener() = default; void RegisterListenerWithType(NativeEngine& engine, std::string type, NativeValue* value); void UnregisterListenerWithType(std::string type, NativeValue* value); void UnregisterAllListenerWithType(std::string type); @@ -43,7 +43,7 @@ namespace MiscServices { private: std::mutex mMutex; - static std::recursive_mutex mapMutex; + 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); 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 f382251a..236073e9 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,11 +34,6 @@ namespace MiscServices { InputMethodAbility::GetInstance()->setImeListener(imeListener_); } - JsInputMethodEngine::~JsInputMethodEngine() - { - IMSA_HILOGE("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 3db3b7d3..a8c74039 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,7 +20,6 @@ namespace OHOS { namespace MiscServices { using namespace AbilityRuntime; - std::recursive_mutex JsInputMethodEngineListener::mapMutex {}; void JsInputMethodEngineListener::RegisterListenerWithType(NativeEngine& engine, std::string type, NativeValue* value) { @@ -32,21 +31,18 @@ namespace MiscServices { AddCallback(type, value); } - JsInputMethodEngineListener::~JsInputMethodEngineListener() - { - IMSA_HILOGE("JsInputMethodEngineListener::~JsInputMethodEngineListener"); - } - void JsInputMethodEngineListener::AddCallback(std::string type, NativeValue* jsListenerObject) { IMSA_HILOGI("JsInputMethodEngineListener::AddCallback is called : %{public}s", type.c_str()); - std::lock_guard lock(mMutex); - std::shared_ptr callbackRef(engine_->CreateReference(jsListenerObject, 1)); - if (callbackRef == nullptr) { - IMSA_HILOGI("JsInputMethodEngineListener::AddCallback fail, callbackRef is nullptr."); - return; + { + std::lock_guard lock(mMutex); + 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); + std::lock_guard lk(mapMutex); jsCbMap_[type].push_back(callbackRef); IMSA_HILOGI("JsInputMethodEngineListener::AddCallback success"); IMSA_HILOGI("jsCbMap_ size: %{public}d, and type[%{public}s] size: %{public}d!", @@ -57,7 +53,7 @@ namespace MiscServices { { IMSA_HILOGI("JsInputMethodEngineListener::UnregisterAllListenerWithType : %{public}s.", type.c_str()); // should do type check - std::lock_guard mapLock(mapMutex); + std::lock_guard lk(mapMutex); if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) { IMSA_HILOGI("methodName %{public}s not registerted!", type.c_str()); return; @@ -69,7 +65,7 @@ namespace MiscServices { { IMSA_HILOGI("JsInputMethodEngineListener::UnregisterListenerWithType : %{public}s.", type.c_str()); // should do type check - std::lock_guard mapLock(mapMutex); + std::lock_guard lk(mapMutex); if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) { IMSA_HILOGI("methodName %{public}s not registerted!", type.c_str()); return; @@ -80,7 +76,7 @@ namespace MiscServices { bool JsInputMethodEngineListener::IfCallbackRegistered(std::string type, NativeValue* jsListenerObject) { IMSA_HILOGI("JsInputMethodEngineListener::IfCallbackRegistered"); - std::lock_guard mapLock(mapMutex); + std::lock_guard lk(mapMutex); if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) { IMSA_HILOGI("methodName %{public}s not registertd!", type.c_str()); return false; @@ -101,7 +97,7 @@ namespace MiscServices { IMSA_HILOGI("engine_ nullptr"); return; } - std::lock_guard mapLock(mapMutex); + std::lock_guard lk(mapMutex); if (jsCbMap_.empty() || jsCbMap_.find(methodName) == jsCbMap_.end()) { IMSA_HILOGI("methodName %{public}s not registertd!", methodName.c_str()); return; @@ -123,7 +119,7 @@ namespace MiscServices { IMSA_HILOGI("engine_ nullptr"); return false; } - std::lock_guard mapLock(mapMutex); + std::lock_guard lk(mapMutex); if (jsCbMap_.empty() || jsCbMap_.find(methodName) == jsCbMap_.end()) { IMSA_HILOGI("methodName %{public}s not registered!", methodName.c_str()); return false; diff --git a/services/include/message.h b/services/include/message.h index fbe73e0a..9fa011d0 100644 --- a/services/include/message.h +++ b/services/include/message.h @@ -25,11 +25,9 @@ namespace OHOS { namespace MiscServices { class Message { public: - int32_t msgId_ {}; // message id + int32_t msgId_ {0}; // message id MessageParcel *msgContent_ = nullptr; // message content - MessageParcel *msgReply_ = nullptr; // message reply Message(int32_t msgId, MessageParcel *msgContent); - Message(int32_t msgId, MessageParcel *msgContent, MessageParcel *msgReply); explicit Message(const Message& msg); Message& operator =(const Message& msg); ~Message(); diff --git a/services/src/input_method_system_ability.cpp b/services/src/input_method_system_ability.cpp index 862ccf02..2175e971 100644 --- a/services/src/input_method_system_ability.cpp +++ b/services/src/input_method_system_ability.cpp @@ -1002,7 +1002,13 @@ namespace MiscServices { if (!StartInputService(targetIme)) { return ErrorCode::ERROR_IME_START_FAILED; } - ParaHandle::SetDefaultIme(userId_, targetIme); + bool setResult = ParaHandle::SetDefaultIme(userId_, targetIme); + if (setResult) { + IMSA_HILOGI("SetDefaultIme Successfully."); + } else { + IMSA_HILOGI("SetDefaultIme Failed."); + return ErrorCode::ERROR_STATUS_PERMISSION_DENIED; + } } else { IMSA_HILOGI("InputMethodSystemAbility::OnSwitchInputMethod DefaultIme and TargetIme are the same one!"); } diff --git a/services/src/input_method_system_ability_stub.cpp b/services/src/input_method_system_ability_stub.cpp index 8746a65e..d9eb6e65 100644 --- a/services/src/input_method_system_ability_stub.cpp +++ b/services/src/input_method_system_ability_stub.cpp @@ -325,7 +325,7 @@ namespace MiscServices { return ErrorCode::ERROR_IME_PROPERTY_MARSHALL; } delete target; - auto *msg = new Message(MSG_ID_SWITCH_INPUT_METHOD, parcel); + auto *msg = new (std::nothrow) Message(MSG_ID_SWITCH_INPUT_METHOD, parcel); MessageHandler::Instance()->SendMessage(msg); return ErrorCode::NO_ERROR; } diff --git a/services/src/message.cpp b/services/src/message.cpp index 1713e454..e39f48cb 100644 --- a/services/src/message.cpp +++ b/services/src/message.cpp @@ -30,10 +30,6 @@ namespace MiscServices { } } - Message::Message(int32_t msgId, MessageParcel *msgContent, MessageParcel *msgReply) : Message(msgId, msgContent) - { - msgReply_ = msgReply; - } /*! Constructor \param msg a source message */ From e2d6bc660239ead8d46f83712a5f6e9630e8e6d4 Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Tue, 19 Jul 2022 20:16:58 +0800 Subject: [PATCH 6/9] modify for build error Signed-off-by: zhaolinglan --- .../src/js_input_method_engine_listener.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) 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 a8c74039..62c1af5e 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 @@ -34,13 +34,11 @@ namespace MiscServices { void JsInputMethodEngineListener::AddCallback(std::string type, NativeValue* jsListenerObject) { IMSA_HILOGI("JsInputMethodEngineListener::AddCallback is called : %{public}s", type.c_str()); - { - std::lock_guard lock(mMutex); - std::shared_ptr callbackRef(engine_->CreateReference(jsListenerObject, 1)); - if (callbackRef == nullptr) { - IMSA_HILOGI("JsInputMethodEngineListener::AddCallback fail, callbackRef is nullptr."); - return; - } + std::lock_guard lock(mMutex); + std::shared_ptr callbackRef(engine_->CreateReference(jsListenerObject, 1)); + if (callbackRef == nullptr) { + IMSA_HILOGI("JsInputMethodEngineListener::AddCallback fail, callbackRef is nullptr."); + return; } std::lock_guard lk(mapMutex); jsCbMap_[type].push_back(callbackRef); From 7d7c8666859fb319ec9cbf6e7569fd9ca98f92ea Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Wed, 20 Jul 2022 19:58:10 +0800 Subject: [PATCH 7/9] modify some code at 7/20 19:58 Signed-off-by: zhaolinglan --- .../include/input_method_controller.h | 2 +- .../include/input_method_system_ability_proxy.h | 2 +- .../src/input_method_controller.cpp | 2 +- .../src/input_method_system_ability_proxy.cpp | 8 +++----- .../inputmethod/src/js_input_method_registry.cpp | 4 ++-- .../src/js_input_method_engine_listener.cpp | 11 +++++------ services/src/input_method_system_ability.cpp | 12 ++++++------ 7 files changed, 19 insertions(+), 22 deletions(-) diff --git a/frameworks/inputmethod_controller/include/input_method_controller.h b/frameworks/inputmethod_controller/include/input_method_controller.h index 99af3ca3..59de7bac 100644 --- a/frameworks/inputmethod_controller/include/input_method_controller.h +++ b/frameworks/inputmethod_controller/include/input_method_controller.h @@ -75,7 +75,7 @@ namespace MiscServices { int32_t GetInputPattern(); void HideCurrentInput(); void SetCallingWindow(uint32_t windowId); - int32_t SwitchInputMethod(InputMethodProperty *target); + int32_t SwitchInputMethod(const InputMethodProperty &target); private: InputMethodController(); diff --git a/frameworks/inputmethod_controller/include/input_method_system_ability_proxy.h b/frameworks/inputmethod_controller/include/input_method_system_ability_proxy.h index e1fd29c2..4575ab45 100644 --- a/frameworks/inputmethod_controller/include/input_method_system_ability_proxy.h +++ b/frameworks/inputmethod_controller/include/input_method_system_ability_proxy.h @@ -56,7 +56,7 @@ namespace MiscServices { int32_t listInputMethodEnabled(std::vector *properties) override; int32_t listInputMethod(std::vector *properties) override; int32_t listKeyboardType(const std::u16string& imeId, std::vector *types) override; - int32_t SwitchInputMethod(InputMethodProperty* target); + int32_t SwitchInputMethod(const InputMethodProperty &target); private: static inline BrokerDelegator delegator_; diff --git a/frameworks/inputmethod_controller/src/input_method_controller.cpp b/frameworks/inputmethod_controller/src/input_method_controller.cpp index 02f525af..f0ec0b83 100644 --- a/frameworks/inputmethod_controller/src/input_method_controller.cpp +++ b/frameworks/inputmethod_controller/src/input_method_controller.cpp @@ -444,7 +444,7 @@ using namespace MessageID; agent->SetCallingWindow(windowId); } - int32_t InputMethodController::SwitchInputMethod(InputMethodProperty *target) + int32_t InputMethodController::SwitchInputMethod(const InputMethodProperty &target) { IMSA_HILOGI("InputMethodController::SwitchInputMethod"); if (!mImms) { 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 d111a8ad..0d2ef505 100644 --- a/frameworks/inputmethod_controller/src/input_method_system_ability_proxy.cpp +++ b/frameworks/inputmethod_controller/src/input_method_system_ability_proxy.cpp @@ -438,7 +438,7 @@ namespace MiscServices { return NO_ERROR; } - int32_t InputMethodSystemAbilityProxy::SwitchInputMethod(InputMethodProperty* target) + int32_t InputMethodSystemAbilityProxy::SwitchInputMethod(const InputMethodProperty &target) { IMSA_HILOGI("InputMethodSystemAbilityProxy::SwitchInputMethod"); MessageParcel data, reply; @@ -448,14 +448,12 @@ namespace MiscServices { return ERROR_EX_PARCELABLE; } - if (!target->Marshalling(data)) { + if (!target.Marshalling(data)) { IMSA_HILOGE("InputMethodSystemAbilityProxy::switchInputMethod Failed to marshall target to data!"); - delete target; return ERROR_IME_PROPERTY_MARSHALL; } - delete target; auto ret = Remote()->SendRequest(SWITCH_INPUT_METHOD, data, reply, option); - if (ret != NO_ERROR) { + if (ret != 0) { return ERROR_STATUS_FAILED_TRANSACTION; } ret = reply.ReadInt32(); 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 a885001a..f7aeb2e9 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 @@ -96,14 +96,14 @@ namespace MiscServices { return engine.CreateUndefined(); } - InputMethodProperty *target = new InputMethodProperty(); + InputMethodProperty target; NativeObject *object = ConvertNativeValueTo(info.argv[0]); if (object == nullptr) { IMSA_HILOGE("JsInputMethodRegistry::OnSwitchInputMethod Failed to get object"); return engine.CreateUndefined(); } - if (!GetInputMethodPropertyFromJs(engine, object, *target)) { + if (!GetInputMethodPropertyFromJs(engine, object, target)) { return engine.CreateUndefined(); } 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 62c1af5e..b134ad1e 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 @@ -37,19 +37,18 @@ namespace MiscServices { std::lock_guard lock(mMutex); std::shared_ptr callbackRef(engine_->CreateReference(jsListenerObject, 1)); if (callbackRef == nullptr) { - IMSA_HILOGI("JsInputMethodEngineListener::AddCallback fail, callbackRef is nullptr."); + IMSA_HILOGI("JsInputMethodEngineListener::AddCallback fail, callbackRef is nullptr"); return; } std::lock_guard lk(mapMutex); jsCbMap_[type].push_back(callbackRef); - IMSA_HILOGI("JsInputMethodEngineListener::AddCallback success"); - IMSA_HILOGI("jsCbMap_ size: %{public}d, and type[%{public}s] size: %{public}d!", + IMSA_HILOGI("AddCallback success! jsCbMap_ size: %{public}d, and type[%{public}s] size: %{public}d", static_cast(jsCbMap_.size()), type.c_str(), static_cast(jsCbMap_[type].size())); } void JsInputMethodEngineListener::UnregisterAllListenerWithType(std::string type) { - IMSA_HILOGI("JsInputMethodEngineListener::UnregisterAllListenerWithType : %{public}s.", type.c_str()); + IMSA_HILOGI("JsInputMethodEngineListener::UnregisterAllListenerWithType : %{public}s", type.c_str()); // should do type check std::lock_guard lk(mapMutex); if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) { @@ -102,8 +101,8 @@ namespace MiscServices { } for (auto iter : jsCbMap_[methodName]) { if (iter == nullptr) { - IMSA_HILOGE("JsInputMethodEngineListener::CallJsMethod iter is null!"); - return; + IMSA_HILOGE("JsInputMethodEngineListener::CallJsMethod iter is null"); + continue; } engine_->CallFunction(engine_->CreateUndefined(), iter->Get(), argv, argc); } diff --git a/services/src/input_method_system_ability.cpp b/services/src/input_method_system_ability.cpp index 2175e971..fd857798 100644 --- a/services/src/input_method_system_ability.cpp +++ b/services/src/input_method_system_ability.cpp @@ -290,7 +290,7 @@ 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."); + IMSA_HILOGE("InputMethodSystemAbility::StartInputService failed, result = %{public}d", result); isStartSuccess = false; } else { IMSA_HILOGE("InputMethodSystemAbility::StartInputService success."); @@ -990,9 +990,7 @@ namespace MiscServices { } std::string defaultIme = ParaHandle::GetDefaultIme(userId_); - std::string targetIme; - std::string imeId = Str16ToStr8(target->mPackageName) + "/" + Str16ToStr8(target->mAbilityName); - targetIme += imeId; + std::string targetIme = Str16ToStr8(target->mPackageName) + "/" + Str16ToStr8(target->mAbilityName); IMSA_HILOGI("InputMethodSystemAbility::OnSwitchInputMethod DefaultIme : %{public}s, TargetIme : %{public}s", defaultIme.c_str(), targetIme.c_str()); if (defaultIme != targetIme) { @@ -1004,9 +1002,11 @@ namespace MiscServices { } bool setResult = ParaHandle::SetDefaultIme(userId_, targetIme); if (setResult) { - IMSA_HILOGI("SetDefaultIme Successfully."); + IMSA_HILOGI("InputMethodSystemAbility::OnSwitchInputMethod SetDefaultIme Successfully."); } else { - IMSA_HILOGI("SetDefaultIme Failed."); + IMSA_HILOGI("InputMethodSystemAbility::OnSwitchInputMethod SetDefaultIme Failed. setResult = " + "%{public}d", + setResult); return ErrorCode::ERROR_STATUS_PERMISSION_DENIED; } } else { From bc1daf879cc9995a83693aa0f6cf213c114ac91a Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Thu, 21 Jul 2022 08:37:54 +0800 Subject: [PATCH 8/9] add a nothrow Signed-off-by: zhaolinglan --- services/src/input_method_system_ability_stub.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/src/input_method_system_ability_stub.cpp b/services/src/input_method_system_ability_stub.cpp index d9eb6e65..63e0b5dc 100644 --- a/services/src/input_method_system_ability_stub.cpp +++ b/services/src/input_method_system_ability_stub.cpp @@ -315,7 +315,7 @@ namespace MiscServices { int32_t uid = IPCSkeleton::GetCallingUid(); int32_t userId = getUserId(uid); - auto *parcel = new MessageParcel(); + auto *parcel = new (std::nothrow) MessageParcel(); InputMethodProperty *target = InputMethodProperty::Unmarshalling(data); parcel->WriteInt32(userId); if (!target->Marshalling(*parcel)) { From 33dcbbae4c720d344eede851662702bc5708a3bf Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Thu, 21 Jul 2022 09:57:52 +0800 Subject: [PATCH 9/9] modify two files at 7/21 09:57 Signed-off-by: zhaolinglan --- .../src/js_input_method_engine_listener.cpp | 12 ++++++++++-- services/src/input_method_system_ability_stub.cpp | 6 ++++++ 2 files changed, 16 insertions(+), 2 deletions(-) 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 b134ad1e..305c6bb8 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 @@ -64,10 +64,18 @@ namespace MiscServices { // should do type check std::lock_guard lk(mapMutex); if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) { - IMSA_HILOGI("methodName %{public}s not registerted!", type.c_str()); + IMSA_HILOGI("methodName %{public}s not registered!", type.c_str()); return; } - jsCbMap_.erase(type); + for (auto it = jsCbMap_[type].begin(); it != jsCbMap_[type].end();) { + if (value->StrictEquals((*it)->Get())) { + jsCbMap_[type].erase(it); + break; + } + } + if (jsCbMap_[type].empty()) { + jsCbMap_.erase(type); + } } bool JsInputMethodEngineListener::IfCallbackRegistered(std::string type, NativeValue* jsListenerObject) diff --git a/services/src/input_method_system_ability_stub.cpp b/services/src/input_method_system_ability_stub.cpp index 63e0b5dc..029aecb4 100644 --- a/services/src/input_method_system_ability_stub.cpp +++ b/services/src/input_method_system_ability_stub.cpp @@ -316,6 +316,9 @@ namespace MiscServices { int32_t userId = getUserId(uid); auto *parcel = new (std::nothrow) MessageParcel(); + if (parcel == nullptr) { + return ErrorCode::ERROR_EX_NULL_POINTER; + } InputMethodProperty *target = InputMethodProperty::Unmarshalling(data); parcel->WriteInt32(userId); if (!target->Marshalling(*parcel)) { @@ -326,6 +329,9 @@ namespace MiscServices { } delete target; auto *msg = new (std::nothrow) Message(MSG_ID_SWITCH_INPUT_METHOD, parcel); + if (msg == nullptr) { + return ErrorCode::ERROR_EX_NULL_POINTER; + } MessageHandler::Instance()->SendMessage(msg); return ErrorCode::NO_ERROR; }