Signed-off-by: ma-shaoyin <mashaoyin1@huawei.com>

Changes to be committed:
This commit is contained in:
ma-shaoyin 2024-08-30 12:44:23 +08:00
parent 0e979d8e4b
commit b61ba60dd2
2 changed files with 15 additions and 10 deletions

View File

@ -28,6 +28,7 @@ namespace OHOS {
namespace AbilityRuntime {
napi_value CreateJsInputMethodExtensionContext(napi_env env, std::shared_ptr<InputMethodExtensionContext> context);
// need create in main thread.
class JSInputMethodExtensionConnection : public AbilityConnectCallback {
public:
explicit JSInputMethodExtensionConnection(napi_env env);
@ -35,16 +36,20 @@ public:
void OnAbilityConnectDone(const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject,
int resultCode) override;
void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override;
// this function need to execute in main thread.
void CallJsFailed(int32_t errorCode);
void SetJsConnectionObject(napi_value jsConnectionObject);
private:
void HandleOnAbilityConnectDone(const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject,
int resultCode);
void HandleOnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode);
void SetJsConnectionObject(napi_value jsConnectionObject);
void CallJsFailed(int32_t errorCode);
void CleanUpJsObject();
void ReleaseConnection();
private:
napi_env env_;
napi_ref jsConnectionObject_ = nullptr;
std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
};
struct ConnectionKey {
@ -61,7 +66,6 @@ struct key_compare {
static std::map<ConnectionKey, sptr<JSInputMethodExtensionConnection>, key_compare> connects_;
static int64_t serialNumber_ = 0;
static std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
static std::mutex g_connectMapMtx;
} // namespace AbilityRuntime
} // namespace OHOS

View File

@ -457,8 +457,6 @@ napi_value CreateJsInputMethodExtensionContext(napi_env env, std::shared_ptr<Inp
napi_value objValue = CreateJsExtensionContext(env, context);
std::unique_ptr<JsInputMethodExtensionContext> jsContext = std::make_unique<JsInputMethodExtensionContext>(context);
napi_wrap(env, objValue, jsContext.release(), JsInputMethodExtensionContext::Finalizer, nullptr, nullptr);
// make handler
handler_ = std::make_shared<AppExecFwk::EventHandler>(AppExecFwk::EventRunner::GetMainEventRunner());
const char *moduleName = "JsInputMethodExtensionContext";
BindNativeFunction(env, objValue, "startAbility", moduleName, JsInputMethodExtensionContext::StartAbility);
@ -474,7 +472,8 @@ napi_value CreateJsInputMethodExtensionContext(napi_env env, std::shared_ptr<Inp
return objValue;
}
JSInputMethodExtensionConnection::JSInputMethodExtensionConnection(napi_env env) : env_(env)
JSInputMethodExtensionConnection::JSInputMethodExtensionConnection(napi_env env) :
env_(env), handler_(std::make_shared<AppExecFwk::EventHandler>(AppExecFwk::EventRunner::GetMainEventRunner()))
{
}
@ -597,7 +596,7 @@ void JSInputMethodExtensionConnection::HandleOnAbilityDisconnectDone(const AppEx
if (item != connects_.end()) {
// match bundleName && abilityName
if (item->second != nullptr) {
item->second->CleanUpJsObject();
item->second->ReleaseConnection();
}
connects_.erase(item);
IMSA_HILOGI("OnAbilityDisconnectDone erase connects_.size: %{public}zu.", connects_.size());
@ -645,11 +644,13 @@ void JSInputMethodExtensionConnection::CallJsFailed(int32_t errorCode)
IMSA_HILOGI("CallJsFailed end.");
}
void JSInputMethodExtensionConnection::CleanUpJsObject()
void JSInputMethodExtensionConnection::ReleaseConnection()
{
IMSA_HILOGD("CleanUpJsObject");
IMSA_HILOGD("ReleaseConnection");
if (jsConnectionObject_ != nullptr) {
napi_delete_reference(env_, jsConnectionObject_);
env_ = nullptr;
jsConnectionObject_ == nullptr;
}
}
} // namespace AbilityRuntime