!47404 【mediaQuery】多实例场景添加ContainerScope保护

Merge pull request !47404 from 冯纪拓/multi_instance
This commit is contained in:
openharmony_ci 2024-11-05 03:22:54 +00:00 committed by Gitee
commit bb39e09e0d
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 23 additions and 15 deletions

View File

@ -27,9 +27,13 @@ template("napi_mediaquery_static") {
deps = []
external_deps = []
if (platform != "" && is_arkui_x) {
deps += [ "//plugins/libs/napi:napi_$platform" ]
deps += [
"$ace_root/frameworks/core:ace_container_scope_static",
"//plugins/libs/napi:napi_$platform",
]
} else {
external_deps += [ "napi:ace_napi" ]
deps += [ "$ace_root/frameworks/core:ace_container_scope" ]
}
if (defined(config.libace_target)) {

View File

@ -13,12 +13,6 @@
* limitations under the License.
*/
#include <algorithm>
#include <map>
#include <mutex>
#include <set>
#include <vector>
#include "napi/native_api.h"
#include "napi/native_common.h"
#include "napi/native_node_api.h"
@ -26,8 +20,6 @@
#include "base/utils/utils.h"
#include "bridge/common/media_query/media_queryer.h"
#include "bridge/common/utils/engine_helper.h"
#include "bridge/js_frontend/engine/common/js_engine.h"
#include "core/common/container.h"
namespace OHOS::Ace::Napi {
namespace {
@ -121,6 +113,7 @@ public:
{
MediaQueryer queryer;
for (auto& listener : copyListeners) {
OHOS::Ace::ContainerScope scope(listener->GetInstanceId());
auto json = MediaQueryInfo::GetMediaQueryJsonInfo();
listener->matches_ = queryer.MatchCondition(listener->media_, json);
std::set<napi_ref> delayDeleteCallbacks;
@ -158,12 +151,6 @@ public:
static napi_value On(napi_env env, napi_callback_info info)
{
auto jsEngine = EngineHelper::GetCurrentEngineSafely();
if (!jsEngine) {
return nullptr;
}
jsEngine->RegisterMediaUpdateCallback(NapiCallback);
napi_handle_scope scope = nullptr;
napi_open_handle_scope(env, &scope);
if (scope == nullptr) {
@ -181,6 +168,11 @@ public:
napi_close_handle_scope(env, scope);
return nullptr;
}
auto jsEngine = EngineHelper::GetEngine(listener->GetInstanceId());
if (!jsEngine) {
return nullptr;
}
jsEngine->RegisterMediaUpdateCallback(NapiCallback);
auto iter = listener->FindCbList(cb);
if (iter != listener->cbList_.end()) {
napi_close_handle_scope(env, scope);
@ -277,6 +269,16 @@ public:
napi_set_named_property(env, result, funName, funcValue);
}
void SetInstanceId(int32_t instanceId)
{
instanceId_ = instanceId;
}
int32_t GetInstanceId()
{
return instanceId_;
}
private:
void CleanListenerSet()
{
@ -355,6 +357,7 @@ private:
napi_env env_ = nullptr;
std::list<napi_ref> cbList_;
int32_t instanceId_;
static std::set<std::unique_ptr<MediaQueryListener>>* delayDeleteListenerSets_;
static napi_env delayDeleteEnv_;
static std::set<napi_ref>* delayDeleteCallbacks_;
@ -394,6 +397,7 @@ static napi_value JSMatchMediaSync(napi_env env, napi_callback_info info)
MediaQueryListener* listener = new MediaQueryListener(matchResult, conditionStr);
napi_value result = nullptr;
listener->NapiSerializer(env, result);
listener->SetInstanceId(Container::CurrentIdSafely());
return result;
}