• 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(napi_env env, CallBackType eventType, const AAFwk::Want& want,
43     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<JSPluginCallback> pPluginComponentCallback =
53         std::make_shared<JSPluginCallback>(eventType, cbInfo, asyncJSCallbackInfo_);
54     if (pPluginComponentCallback != nullptr) {
55         pPluginComponentCallback->SetWant(want);
56         eventList_.insert(std::make_pair(pPluginComponentCallback->GetID(), pPluginComponentCallback));
57         PluginComponentManager::GetInstance()->RegisterCallBack(want, pPluginComponentCallback, eventType);
58         return true;
59     } else {
60         return false;
61     }
62 }
63 
RegisterRequestEvent(napi_env env,const AAFwk::Want & want,ACECallbackInfo & cbInfo,const std::shared_ptr<AceJSPluginRequestParam> & param)64 bool JSPluginCallbackMgr::RegisterRequestEvent(napi_env env, const AAFwk::Want& want, ACECallbackInfo& cbInfo,
65     const std::shared_ptr<AceJSPluginRequestParam>& param)
66 {
67     HILOG_INFO("%{public}s called.", __func__);
68     std::lock_guard<std::mutex> lock(mutex_);
69     if (param == nullptr) {
70         return false;
71     }
72     for (auto iter = eventList_.begin(); iter != eventList_.end(); iter++) {
73         if (iter->second->RequestStrictEquals(CallBackType::RequestCallBack, want, cbInfo, param)) {
74             return false;
75         }
76     }
77 
78     std::shared_ptr<JSPluginCallback> pPluginComponentCallback =
79         std::make_shared<JSPluginCallback>(CallBackType::RequestCallBack, cbInfo, asyncJSCallbackInfo_);
80     if (pPluginComponentCallback != nullptr) {
81         pPluginComponentCallback->SetWant(want);
82         pPluginComponentCallback->SetRequestParam(param);
83         eventList_.insert(std::make_pair(pPluginComponentCallback->GetID(), pPluginComponentCallback));
84         PluginComponentManager::GetInstance()->RegisterCallBack(want, pPluginComponentCallback,
85             CallBackType::RequestCallBack);
86         return true;
87     } else {
88         return false;
89     }
90 }
91 
RegisterRequestEvent(napi_env env,const AAFwk::Want & want,ACEAsyncJSCallbackInfo * jsCallbackInfo,const std::shared_ptr<AceJSPluginRequestParam> & param)92 bool JSPluginCallbackMgr::RegisterRequestEvent(napi_env env, const AAFwk::Want& want,
93     ACEAsyncJSCallbackInfo* jsCallbackInfo, const std::shared_ptr<AceJSPluginRequestParam>& param)
94 {
95     asyncJSCallbackInfo_ = jsCallbackInfo;
96     if (jsCallbackInfo) {
97         return RegisterRequestEvent(env, want, jsCallbackInfo->cbInfo, param);
98     } else {
99         return false;
100     }
101 }
102 
UnRegisterEvent(size_t key)103 void JSPluginCallbackMgr::UnRegisterEvent(size_t key)
104 {
105     HILOG_INFO("%{public}s called. ", __func__);
106     std::lock_guard<std::mutex> lock(mutex_);
107     auto iter = eventList_.find(key);
108     if (iter != eventList_.end()) {
109         eventList_.erase(iter);
110     }
111 }
112 
UnregisterCallBack(napi_env env,const AAFwk::Want & want)113 void JSPluginCallbackMgr::UnregisterCallBack(napi_env env, const AAFwk::Want& want)
114 {
115     PluginComponentManager::GetInstance()->UnregisterCallBack(want);
116 }
117 }  // namespace OHOS::Ace::Napi