1 /*
2 * Copyright (c) 2021-2022 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
17 #include "core/components/plugin/plugin_component_manager.h"
18
19 namespace OHOS::Ace::Napi {
JSPluginCallbackMgr()20 JSPluginCallbackMgr::JSPluginCallbackMgr() {}
21
~JSPluginCallbackMgr()22 JSPluginCallbackMgr::~JSPluginCallbackMgr()
23 {
24 std::lock_guard<std::mutex> lock(mutex_);
25 for (auto iter = eventList_.begin(); iter != eventList_.end(); iter++) {
26 if (iter->second != nullptr) {
27 PluginComponentManager::GetInstance()->UnregisterCallBack(iter->second->GetWant());
28 }
29 }
30 eventList_.clear();
31 asyncJSCallbackInfo_ = nullptr;
32 }
33
Instance(void)34 JSPluginCallbackMgr& JSPluginCallbackMgr::Instance(void)
35 {
36 static JSPluginCallbackMgr gJSPluginCallbackMgr;
37 return gJSPluginCallbackMgr;
38 }
39
RegisterOnEvent(napi_env env,CallBackType eventType,const AAFwk::Want & want,ACECallbackInfo & cbInfo)40 bool JSPluginCallbackMgr::RegisterOnEvent(
41 napi_env env, CallBackType eventType, const AAFwk::Want& want, ACECallbackInfo& cbInfo)
42 {
43 std::lock_guard<std::mutex> lock(mutex_);
44 for (auto iter = eventList_.begin(); iter != eventList_.end(); iter++) {
45 if (iter->second->OnEventStrictEquals(eventType, want, cbInfo)) {
46 return false;
47 }
48 }
49
50 std::shared_ptr<PluginComponentCallBack> pPluginComponentCallback =
51 std::make_shared<JSPluginCallback>(eventType, cbInfo, asyncJSCallbackInfo_);
52 std::shared_ptr<JSPluginCallback> jsPluginCallback =
53 std::static_pointer_cast<JSPluginCallback>(pPluginComponentCallback);
54 jsPluginCallback->SetWant(want);
55 eventList_.insert(std::make_pair(jsPluginCallback->GetID(), jsPluginCallback));
56 PluginComponentManager::GetInstance()->RegisterCallBack(want, pPluginComponentCallback, eventType);
57 return true;
58 }
59
RegisterRequestEvent(napi_env env,const AAFwk::Want & want,ACECallbackInfo & cbInfo,const std::shared_ptr<AceJSPluginRequestParam> & param)60 bool JSPluginCallbackMgr::RegisterRequestEvent(napi_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<JSPluginCallback> pPluginComponentCallback =
74 std::make_shared<JSPluginCallback>(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
RegisterRequestEvent(napi_env env,const AAFwk::Want & want,ACEAsyncJSCallbackInfo * jsCallbackInfo,const std::shared_ptr<AceJSPluginRequestParam> & param)87 bool JSPluginCallbackMgr::RegisterRequestEvent(napi_env env, const AAFwk::Want& want,
88 ACEAsyncJSCallbackInfo* jsCallbackInfo, const std::shared_ptr<AceJSPluginRequestParam>& param)
89 {
90 asyncJSCallbackInfo_ = jsCallbackInfo;
91 if (jsCallbackInfo) {
92 return RegisterRequestEvent(env, want, jsCallbackInfo->cbInfo, param);
93 } else {
94 return false;
95 }
96 }
97
UnRegisterEvent(size_t key)98 void JSPluginCallbackMgr::UnRegisterEvent(size_t key)
99 {
100 std::lock_guard<std::mutex> lock(mutex_);
101 auto iter = eventList_.find(key);
102 if (iter != eventList_.end()) {
103 eventList_.erase(iter);
104 }
105 }
106
UnregisterCallBack(napi_env env,const AAFwk::Want & want)107 void JSPluginCallbackMgr::UnregisterCallBack(napi_env env, const AAFwk::Want& want)
108 {
109 PluginComponentManager::GetInstance()->UnregisterCallBack(want);
110 }
111 } // namespace OHOS::Ace::Napi
112