1 /*
2 * Copyright (c) 2021 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 "js_plugin_callback_mgr.h"
16 #include "core/components/plugin/plugin_component_manager.h"
17 #include "hilog_wrapper.h"
18
19 namespace OHOS::Ace::Napi {
JSPluginCallbackMgr()20 JSPluginCallbackMgr::JSPluginCallbackMgr()
21 {
22 }
23
~JSPluginCallbackMgr()24 JSPluginCallbackMgr::~JSPluginCallbackMgr()
25 {
26 std::lock_guard<std::mutex> lock(mutex_);
27 for (auto iter = eventList_.begin(); iter != eventList_.end(); iter++) {
28 if (iter->second != nullptr) {
29 PluginComponentManager::GetInstance()->UnregisterCallBack(iter->second->GetWant());
30 }
31 }
32 eventList_.clear();
33 }
34
Instance(void)35 JSPluginCallbackMgr& JSPluginCallbackMgr::Instance(void)
36 {
37 static JSPluginCallbackMgr gJSPluginCallbackMgr;
38 return gJSPluginCallbackMgr;
39 }
40
RegisterOnEvent(napi_env env,CallBackType eventType,const AAFwk::Want & want,ACECallbackInfo & cbInfo)41 bool JSPluginCallbackMgr::RegisterOnEvent(napi_env env, CallBackType eventType, const AAFwk::Want& want,
42 ACECallbackInfo& cbInfo)
43 {
44 std::lock_guard<std::mutex> lock(mutex_);
45 for (auto iter = eventList_.begin(); iter != eventList_.end(); iter++) {
46 if (iter->second->OnEventStrictEquals(eventType, want, cbInfo)) {
47 return false;
48 }
49 }
50
51 std::shared_ptr<JSPluginCallback> pPluginComponentCallback =
52 std::make_shared<JSPluginCallback>(eventType, cbInfo);
53 if (pPluginComponentCallback != nullptr) {
54 pPluginComponentCallback->SetWant(want);
55 eventList_.insert(std::make_pair(pPluginComponentCallback->GetID(), pPluginComponentCallback));
56 PluginComponentManager::GetInstance()->RegisterCallBack(want, pPluginComponentCallback, eventType);
57 return true;
58 } else {
59 return false;
60 }
61 }
62
RegisterRequestEvent(napi_env env,const AAFwk::Want & want,ACECallbackInfo & cbInfo,const std::shared_ptr<AceJSPluginRequestParam> & param)63 bool JSPluginCallbackMgr::RegisterRequestEvent(napi_env env, const AAFwk::Want& want, ACECallbackInfo& cbInfo,
64 const std::shared_ptr<AceJSPluginRequestParam>& param)
65 {
66 HILOG_INFO("%{public}s called.", __func__);
67 std::lock_guard<std::mutex> lock(mutex_);
68 if (param == nullptr) {
69 return false;
70 }
71 for (auto iter = eventList_.begin(); iter != eventList_.end(); iter++) {
72 if (iter->second->RequestStrictEquals(CallBackType::RequestCallBack, want, cbInfo, param)) {
73 return false;
74 }
75 }
76
77 std::shared_ptr<JSPluginCallback> pPluginComponentCallback =
78 std::make_shared<JSPluginCallback>(CallBackType::RequestCallBack, cbInfo);
79 if (pPluginComponentCallback != nullptr) {
80 pPluginComponentCallback->SetWant(want);
81 eventList_.insert(std::make_pair(pPluginComponentCallback->GetID(), pPluginComponentCallback));
82 PluginComponentManager::GetInstance()->RegisterCallBack(want, pPluginComponentCallback,
83 CallBackType::RequestCallBack);
84 return true;
85 } else {
86 return false;
87 }
88 }
89
UnRegisterEvent(size_t key)90 void JSPluginCallbackMgr::UnRegisterEvent(size_t key)
91 {
92 HILOG_INFO("%{public}s called. ", __func__);
93 std::lock_guard<std::mutex> lock(mutex_);
94 auto iter = eventList_.find(key);
95 if (iter != eventList_.end()) {
96 eventList_.erase(iter);
97 }
98 }
99
UnregisterCallBack(napi_env env,const AAFwk::Want & want)100 void JSPluginCallbackMgr::UnregisterCallBack(napi_env env, const AAFwk::Want& want)
101 {
102 PluginComponentManager::GetInstance()->UnregisterCallBack(want);
103 }
104 } // namespace OHOS::Ace::Napi