• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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     asyncJSCallbackInfo_ = nullptr;
34 }
35 
Instance(void)36 JSPluginCallbackMgr& JSPluginCallbackMgr::Instance(void)
37 {
38     static JSPluginCallbackMgr gJSPluginCallbackMgr;
39     return gJSPluginCallbackMgr;
40 }
41 
RegisterOnEvent(napi_env env,CallBackType eventType,const AAFwk::Want & want,ACECallbackInfo & cbInfo)42 bool JSPluginCallbackMgr::RegisterOnEvent(
43     napi_env env, CallBackType eventType, const AAFwk::Want& want, ACECallbackInfo& cbInfo)
44 {
45     std::lock_guard<std::mutex> lock(mutex_);
46     for (auto iter = eventList_.begin(); iter != eventList_.end(); iter++) {
47         if (iter->second->OnEventStrictEquals(eventType, want, cbInfo)) {
48             return false;
49         }
50     }
51 
52     std::shared_ptr<PluginComponentCallBack> pPluginComponentCallback =
53         std::make_shared<JSPluginCallback>(eventType, cbInfo, asyncJSCallbackInfo_);
54     std::shared_ptr<JSPluginCallback> jsPluginCallback =
55         std::static_pointer_cast<JSPluginCallback>(pPluginComponentCallback);
56     jsPluginCallback->SetWant(want);
57     eventList_.insert(std::make_pair(jsPluginCallback->GetID(), jsPluginCallback));
58     PluginComponentManager::GetInstance()->RegisterCallBack(want, pPluginComponentCallback, eventType);
59     return true;
60 }
61 
RegisterRequestEvent(napi_env env,const AAFwk::Want & want,ACECallbackInfo & cbInfo,const std::shared_ptr<AceJSPluginRequestParam> & param)62 bool JSPluginCallbackMgr::RegisterRequestEvent(napi_env env, const AAFwk::Want& want, ACECallbackInfo& cbInfo,
63     const std::shared_ptr<AceJSPluginRequestParam>& param)
64 {
65     HILOG_INFO("%{public}s called.", __func__);
66     std::lock_guard<std::mutex> lock(mutex_);
67     if (param == nullptr) {
68         return false;
69     }
70     for (auto iter = eventList_.begin(); iter != eventList_.end(); iter++) {
71         if (iter->second->RequestStrictEquals(CallBackType::RequestCallBack, want, cbInfo, param)) {
72             return false;
73         }
74     }
75 
76     std::shared_ptr<JSPluginCallback> pPluginComponentCallback =
77         std::make_shared<JSPluginCallback>(CallBackType::RequestCallBack, cbInfo, asyncJSCallbackInfo_);
78     if (pPluginComponentCallback != nullptr) {
79         pPluginComponentCallback->SetWant(want);
80         pPluginComponentCallback->SetRequestParam(param);
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 
RegisterRequestEvent(napi_env env,const AAFwk::Want & want,ACEAsyncJSCallbackInfo * jsCallbackInfo,const std::shared_ptr<AceJSPluginRequestParam> & param)90 bool JSPluginCallbackMgr::RegisterRequestEvent(napi_env env, const AAFwk::Want& want,
91     ACEAsyncJSCallbackInfo* jsCallbackInfo, const std::shared_ptr<AceJSPluginRequestParam>& param)
92 {
93     asyncJSCallbackInfo_ = jsCallbackInfo;
94     if (jsCallbackInfo) {
95         return RegisterRequestEvent(env, want, jsCallbackInfo->cbInfo, param);
96     } else {
97         return false;
98     }
99 }
100 
UnRegisterEvent(size_t key)101 void JSPluginCallbackMgr::UnRegisterEvent(size_t key)
102 {
103     HILOG_INFO("%{public}s called. ", __func__);
104     std::lock_guard<std::mutex> lock(mutex_);
105     auto iter = eventList_.find(key);
106     if (iter != eventList_.end()) {
107         eventList_.erase(iter);
108     }
109 }
110 
UnregisterCallBack(napi_env env,const AAFwk::Want & want)111 void JSPluginCallbackMgr::UnregisterCallBack(napi_env env, const AAFwk::Want& want)
112 {
113     PluginComponentManager::GetInstance()->UnregisterCallBack(want);
114 }
115 }  // namespace OHOS::Ace::Napi