!364 修复js侧回调传入off导致crash问题

Merge pull request !364 from 陈海莹/master
This commit is contained in:
openharmony_ci
2022-02-28 07:20:05 +00:00
committed by Gitee
7 changed files with 28 additions and 48 deletions
@@ -232,7 +232,7 @@ NativeValue* JsWindowManager::OnCreateWindow(NativeEngine& engine, NativeCallbac
callback = (info.argc == ARGC_TWO) ? nullptr :
(info.argv[INDEX_TWO]->TypeOf() == NATIVE_FUNCTION ? info.argv[INDEX_TWO] : nullptr);
} else if (info.argc >= ARGC_THREE) {
nativeContext = info.argv[0];
nativeContext = info.argv[0]->TypeOf() == NATIVE_OBJECT ? info.argv[0] : nullptr;
nativeString = info.argv[ARGC_ONE];
nativeType = info.argv[ARGC_TWO];
callback = (info.argc == ARGC_THREE) ? nullptr :
@@ -342,16 +342,16 @@ NativeValue* JsWindowManager::OnMinimizeAll(NativeEngine& engine, NativeCallback
return result;
}
bool JsWindowManager::IfCallbackRegistered(std::string type, NativeValue* jsListenerObject)
bool JsWindowManager::IsCallbackRegistered(std::string type, NativeValue* jsListenerObject)
{
if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) {
WLOGFI("JsWindowManager::IfCallbackRegistered methodName %{public}s not registertd!", type.c_str());
WLOGFI("JsWindowManager::IsCallbackRegistered 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->first->Get())) {
WLOGFE("JsWindowManager::IfCallbackRegistered callback already registered!");
WLOGFE("JsWindowManager::IsCallbackRegistered callback already registered!");
return true;
}
}
@@ -361,7 +361,7 @@ bool JsWindowManager::IfCallbackRegistered(std::string type, NativeValue* jsList
void JsWindowManager::RegisterWmListenerWithType(NativeEngine& engine, std::string type, NativeValue* value)
{
// should do type check
if (IfCallbackRegistered(type, value)) {
if (IsCallbackRegistered(type, value)) {
WLOGFE("JsWindowManager::RegisterWmListenerWithType callback already registered!");
return;
}
@@ -39,7 +39,7 @@ public:
static NativeValue* SetWindowLayoutMode(NativeEngine* engine, NativeCallbackInfo* info);
private:
bool IfCallbackRegistered(std::string type, NativeValue* jsListenerObject);
bool IsCallbackRegistered(std::string type, NativeValue* jsListenerObject);
void RegisterWmListenerWithType(NativeEngine& engine, std::string type, NativeValue* value);
void UnregisterAllWmListenerWithType(std::string type);
void UnregisterWmListenerWithType(std::string type, NativeValue* value);
@@ -511,16 +511,16 @@ NativeValue* JsWindow::OnGetProperties(NativeEngine& engine, NativeCallbackInfo&
return result;
}
bool JsWindow::IfCallbackRegistered(std::string type, NativeValue* jsListenerObject)
bool JsWindow::IsCallbackRegistered(std::string type, NativeValue* jsListenerObject)
{
if (jsCallbackMap_.empty() || jsCallbackMap_.find(type) == jsCallbackMap_.end()) {
WLOGFI("JsWindow::IfCallbackRegistered methodName %{public}s not registertd!", type.c_str());
WLOGFI("JsWindow::IsCallbackRegistered methodName %{public}s not registertd!", type.c_str());
return false;
}
for (auto iter = jsCallbackMap_[type].begin(); iter != jsCallbackMap_[type].end(); iter++) {
if (jsListenerObject->StrictEquals((*iter)->Get())) {
WLOGFE("JsWindow::IfCallbackRegistered callback already registered!");
WLOGFE("JsWindow::IsCallbackRegistered callback already registered!");
return true;
}
}
@@ -529,7 +529,7 @@ bool JsWindow::IfCallbackRegistered(std::string type, NativeValue* jsListenerObj
void JsWindow::RegisterWindowListenerWithType(NativeEngine& engine, std::string type, NativeValue* value)
{
if (IfCallbackRegistered(type, value)) {
if (IsCallbackRegistered(type, value)) {
WLOGFE("JsWindow::RegisterWindowListenerWithType callback already registered!");
return;
}
@@ -568,8 +568,8 @@ void JsWindow::UnregisterAllWindowListenerWithType(std::string type)
}
jsListenerMap_[type]->RemoveAllCallback();
if (type.compare(WINDOW_SIZE_CHANGE_CB) == 0) {
sptr<IWindowChangeListener> thisListener(nullptr);
windowToken_->RegisterWindowChangeListener(thisListener);
sptr<IWindowChangeListener> thisListener(jsListenerMap_[type]);
windowToken_->UnregisterWindowChangeListener(thisListener);
WLOGFI("JsWindow::UnregisterAllWindowListenerWithType windowSizeChange success");
}
if (type.compare(SYSTEM_AVOID_AREA_CHANGE_CB) == 0) {
@@ -600,8 +600,8 @@ void JsWindow::UnregisterWindowListenerWithType(std::string type, NativeValue* v
// one type with multi jscallback, erase type when there is no callback in one type
if (jsCallbackMap_[type].empty()) {
if (type.compare(WINDOW_SIZE_CHANGE_CB) == 0) {
sptr<IWindowChangeListener> thisListener(nullptr);
windowToken_->RegisterWindowChangeListener(thisListener);
sptr<IWindowChangeListener> thisListener(jsListenerMap_[type]);
windowToken_->UnregisterWindowChangeListener (thisListener);
WLOGFI("JsWindow::UnregisterWindowListenerWithType windowSizeChange success");
}
if (type.compare(SYSTEM_AVOID_AREA_CHANGE_CB) == 0) {
@@ -56,7 +56,7 @@ public:
static NativeValue* GetColorSpace(NativeEngine* engine, NativeCallbackInfo* info);
private:
bool IfCallbackRegistered(std::string type, NativeValue* jsListenerObject);
bool IsCallbackRegistered(std::string type, NativeValue* jsListenerObject);
void RegisterWindowListenerWithType(NativeEngine& engine, std::string type, NativeValue* value);
void UnregisterWindowListenerWithType(std::string type, NativeValue* value);
void UnregisterAllWindowListenerWithType(std::string type);
@@ -46,11 +46,10 @@ void JsWindowListener::RemoveAllCallback()
void JsWindowListener::RemoveCallback(NativeValue* jsListenerObject)
{
std::lock_guard<std::mutex> lock(mtx_);
for (auto iter = jsCallBack_.begin(); iter != jsCallBack_.end();) {
for (auto iter = jsCallBack_.begin(); iter != jsCallBack_.end(); ++iter) {
if (jsListenerObject->StrictEquals((*iter)->Get())) {
jsCallBack_.erase(iter);
} else {
iter++;
iter = jsCallBack_.erase(iter);
break;
}
}
WLOGFI("JsWindowListener::RemoveCallback success jsCallBack_ size: %{public}d!",
@@ -61,12 +60,12 @@ void JsWindowListener::RemoveCallback(NativeValue* jsListenerObject)
void JsWindowListener::CallJsMethod(const char* methodName, NativeValue* const* argv, size_t argc)
{
WLOGFI("CallJsMethod methodName = %{public}s", methodName);
if (engine_ == nullptr) {
WLOGFE("engine_ nullptr");
if (engine_ == nullptr || jsCallBack_.empty()) {
WLOGFE("engine_ nullptr or jsCallBack_ is empty");
return;
}
for (auto iter = jsCallBack_.begin(); iter != jsCallBack_.end(); iter++) {
NativeValue* method = (*iter)->Get();
for (auto &item : jsCallBack_) {
NativeValue* method = item->Get();
if (method == nullptr) {
WLOGFE("Failed to get method callback from object");
continue;
@@ -78,12 +77,6 @@ void JsWindowListener::CallJsMethod(const char* methodName, NativeValue* const*
void JsWindowListener::OnSizeChange(Rect rect, WindowSizeChangeReason reason)
{
WLOGFI("JsWindowListener::OnSizeChange is called");
std::lock_guard<std::mutex> lock(mtx_);
if (jsCallBack_.empty()) {
WLOGFE("JsWindowListener::OnSizeChange windowSizeChanged not register!");
return;
}
// js callback should run in js thread
std::unique_ptr<AsyncTask::CompleteCallback> complete = std::make_unique<AsyncTask::CompleteCallback> (
[this, rect] (NativeEngine &engine, AsyncTask &task, int32_t status) {
@@ -112,12 +105,7 @@ void JsWindowListener::OnModeChange(WindowMode mode)
void JsWindowListener::OnSystemBarPropertyChange(DisplayId displayId, const SystemBarRegionTints& tints)
{
std::lock_guard<std::mutex> lock(mtx_);
WLOGFI("JsWindowListener::OnSystemBarPropertyChange is called");
if (jsCallBack_.empty()) {
WLOGFE("JsWindowListener::OnSystemBarPropertyChange systemBarTintChange not register!");
return;
}
// js callback should run in js thread
std::unique_ptr<AsyncTask::CompleteCallback> complete = std::make_unique<AsyncTask::CompleteCallback> (
[this, displayId, tints] (NativeEngine &engine, AsyncTask &task, int32_t status) {
@@ -141,13 +129,7 @@ void JsWindowListener::OnSystemBarPropertyChange(DisplayId displayId, const Syst
void JsWindowListener::OnAvoidAreaChanged(const std::vector<Rect> avoidAreas)
{
std::lock_guard<std::mutex> lock(mtx_);
WLOGFI("JsWindowListener::OnAvoidAreaChanged is called");
if (jsCallBack_.empty()) {
WLOGFE("JsWindowListener::OnAvoidAreaChanged systemAvoidAreaChange not register!");
return;
}
// js callback should run in js thread
std::unique_ptr<AsyncTask::CompleteCallback> complete = std::make_unique<AsyncTask::CompleteCallback> (
[this, avoidAreas] (NativeEngine &engine, AsyncTask &task, int32_t status) {
@@ -35,7 +35,6 @@ constexpr int32_t RGBA_LENGTH = 8;
enum class ApiWindowType : uint32_t {
TYPE_BASE,
TYPE_APP = TYPE_BASE,
TYPE_APP_SUB_WINDOW,
TYPE_SYSTEM_ALERT,
TYPE_INPUT_METHOD,
TYPE_STATUS_BAR,
@@ -48,7 +47,7 @@ enum class ApiWindowType : uint32_t {
};
const std::map<WindowType, ApiWindowType> NATIVE_JS_TO_WINDOW_TYPE_MAP {
{ WindowType::APP_SUB_WINDOW_BASE, ApiWindowType::TYPE_APP },
{ WindowType::WINDOW_TYPE_APP_SUB_WINDOW, ApiWindowType::TYPE_APP },
{ WindowType::WINDOW_TYPE_SYSTEM_ALARM_WINDOW, ApiWindowType::TYPE_SYSTEM_ALERT },
{ WindowType::WINDOW_TYPE_INPUT_METHOD_FLOAT, ApiWindowType::TYPE_INPUT_METHOD },
{ WindowType::WINDOW_TYPE_STATUS_BAR, ApiWindowType::TYPE_STATUS_BAR },
@@ -56,11 +55,10 @@ const std::map<WindowType, ApiWindowType> NATIVE_JS_TO_WINDOW_TYPE_MAP {
{ WindowType::WINDOW_TYPE_KEYGUARD, ApiWindowType::TYPE_KEYGUARD },
{ WindowType::WINDOW_TYPE_VOLUME_OVERLAY, ApiWindowType::TYPE_VOLUME_OVERLAY },
{ WindowType::WINDOW_TYPE_NAVIGATION_BAR, ApiWindowType::TYPE_NAVIGATION_BAR },
{ WindowType::WINDOW_TYPE_APP_SUB_WINDOW, ApiWindowType::TYPE_APP_SUB_WINDOW },
{ WindowType::WINDOW_TYPE_FLOAT, ApiWindowType::TYPE_FLOAT },
};
const std::map<ApiWindowType, WindowType> JS_TO_NATIVE_WINDOW_TYPE_MAP {
{ ApiWindowType::TYPE_APP, WindowType::APP_SUB_WINDOW_BASE },
{ ApiWindowType::TYPE_APP, WindowType::WINDOW_TYPE_APP_SUB_WINDOW },
{ ApiWindowType::TYPE_SYSTEM_ALERT, WindowType::WINDOW_TYPE_SYSTEM_ALARM_WINDOW},
{ ApiWindowType::TYPE_INPUT_METHOD, WindowType::WINDOW_TYPE_INPUT_METHOD_FLOAT },
{ ApiWindowType::TYPE_STATUS_BAR, WindowType::WINDOW_TYPE_STATUS_BAR },
@@ -68,7 +66,6 @@ const std::map<ApiWindowType, WindowType> JS_TO_NATIVE_WINDOW_TYPE_MAP {
{ ApiWindowType::TYPE_KEYGUARD, WindowType::WINDOW_TYPE_KEYGUARD },
{ ApiWindowType::TYPE_VOLUME_OVERLAY, WindowType::WINDOW_TYPE_VOLUME_OVERLAY },
{ ApiWindowType::TYPE_NAVIGATION_BAR, WindowType::WINDOW_TYPE_NAVIGATION_BAR },
{ ApiWindowType::TYPE_APP_SUB_WINDOW, WindowType::WINDOW_TYPE_APP_SUB_WINDOW },
{ ApiWindowType::TYPE_FLOAT, WindowType::WINDOW_TYPE_FLOAT }
};
enum class ApiWindowMode : uint32_t {
@@ -115,12 +115,13 @@ void JsWindowStage::LifeCycleCallBack(WindowStageEventType type)
std::unique_ptr<AsyncTask::CompleteCallback> complete = std::make_unique<AsyncTask::CompleteCallback>(
[=] (NativeEngine &engine, AsyncTask &task, int32_t status) {
NativeValue* argv[] = {CreateJsValue(*engine_, static_cast<uint32_t>(type))};
for (auto iter = eventCallbackMap_.begin(); iter != eventCallbackMap_.end(); iter++) {
if (iter->first == nullptr || iter->first->Get() == nullptr) {
for (auto &iter : eventCallbackMap_) {
NativeValue* method = (iter.first)->Get();
if (method == nullptr) {
WLOGFE("callback is null");
return;
}
engine_->CallFunction(engine_->CreateUndefined(), iter->first->Get(), argv, ArraySize(argv));
engine_->CallFunction(engine_->CreateUndefined(), method, argv, ArraySize(argv));
}
}
);