1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "ani_plugin_callback_mgr.h"
16 #include "core/components/plugin/plugin_component_manager.h"
17
18 namespace OHOS::Ace::Ani {
ANIPluginCallbackMgr()19 ANIPluginCallbackMgr::ANIPluginCallbackMgr() {}
20
~ANIPluginCallbackMgr()21 ANIPluginCallbackMgr::~ANIPluginCallbackMgr()
22 {
23 std::lock_guard<std::mutex> lock(mutex_);
24 for (auto iter = eventList_.begin(); iter != eventList_.end(); iter++) {
25 if (iter->second != nullptr) {
26 PluginComponentManager::GetInstance()->UnregisterCallBack(iter->second->GetWant());
27 }
28 }
29 eventList_.clear();
30 asyncJSCallbackInfo_ = nullptr;
31 }
32
Instance(void)33 ANIPluginCallbackMgr& ANIPluginCallbackMgr::Instance(void)
34 {
35 static ANIPluginCallbackMgr gANIPluginCallbackMgr;
36 return gANIPluginCallbackMgr;
37 }
38
RegisterOnEvent(ani_env * env,CallBackType eventType,const AAFwk::Want & want,ACECallbackInfo & cbInfo)39 bool ANIPluginCallbackMgr::RegisterOnEvent(
40 ani_env* env, CallBackType eventType, const AAFwk::Want& want, ACECallbackInfo& cbInfo)
41 {
42 std::lock_guard<std::mutex> lock(mutex_);
43 for (auto iter = eventList_.begin(); iter != eventList_.end(); iter++) {
44 if (iter->second->OnEventStrictEquals(eventType, want, cbInfo)) {
45 return false;
46 }
47 }
48
49 std::shared_ptr<PluginComponentCallBack> pPluginComponentCallback =
50 std::make_shared<ANIPluginCallback>(eventType, cbInfo, asyncJSCallbackInfo_);
51 std::shared_ptr<ANIPluginCallback> jsPluginCallback =
52 std::static_pointer_cast<ANIPluginCallback>(pPluginComponentCallback);
53 jsPluginCallback->SetWant(want);
54 eventList_.insert(std::make_pair(jsPluginCallback->GetID(), jsPluginCallback));
55 PluginComponentManager::GetInstance()->RegisterCallBack(want, pPluginComponentCallback, eventType);
56 LOGI("plugin-ani %{public}s register %{public}d event finish", want.GetBundle().c_str(), eventType);
57 return true;
58 }
59
RegisterRequestEvent(ani_env * env,const AAFwk::Want & want,ACECallbackInfo & cbInfo,const std::shared_ptr<AceJSPluginRequestParam> & param)60 bool ANIPluginCallbackMgr::RegisterRequestEvent(ani_env* env, const AAFwk::Want& want, ACECallbackInfo& cbInfo,
61 const std::shared_ptr<AceJSPluginRequestParam>& param)
62 {
63 std::lock_guard<std::mutex> lock(mutex_);
64 if (param == nullptr) {
65 return false;
66 }
67 for (auto iter = eventList_.begin(); iter != eventList_.end(); iter++) {
68 if (iter->second->RequestStrictEquals(CallBackType::RequestCallBack, want, cbInfo, param)) {
69 return false;
70 }
71 }
72
73 std::shared_ptr<ANIPluginCallback> pPluginComponentCallback =
74 std::make_shared<ANIPluginCallback>(CallBackType::RequestCallBack, cbInfo, asyncJSCallbackInfo_);
75 if (pPluginComponentCallback != nullptr) {
76 pPluginComponentCallback->SetWant(want);
77 pPluginComponentCallback->SetRequestParam(param);
78 eventList_.insert(std::make_pair(pPluginComponentCallback->GetID(), pPluginComponentCallback));
79 PluginComponentManager::GetInstance()->RegisterCallBack(
80 want, pPluginComponentCallback, CallBackType::RequestCallBack);
81 return true;
82 } else {
83 return false;
84 }
85 }
86
UnRegisterEvent(size_t key)87 void ANIPluginCallbackMgr::UnRegisterEvent(size_t key)
88 {
89 std::lock_guard<std::mutex> lock(mutex_);
90 auto iter = eventList_.find(key);
91 if (iter != eventList_.end()) {
92 eventList_.erase(iter);
93 }
94 }
95 } // namespace OHOS::Ace::Ani