1 /*
2 * Copyright (c) 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
16 #include "dm_native_event.h"
17 #include "jsi.h"
18 #include "dm_log.h"
19 #include "js_async_work.h"
20
21 using namespace OHOS::DistributedHardware;
22 using namespace std;
23
24 namespace OHOS {
25 namespace ACELite {
26 std::map<std::string, std::shared_ptr<DmEventListener>> DmNativeEvent::eventMap_;
27
DmNativeEvent()28 DmNativeEvent::DmNativeEvent()
29 {
30 LOGI("DmNativeEvent::DmNativeEvent() in");
31 }
32
DmNativeEvent(JSIValue thisVar)33 DmNativeEvent::DmNativeEvent(JSIValue thisVar)
34 {
35 LOGI("DmNativeEvent::DmNativeEvent(JSIValue thisVar) in");
36 }
37
~DmNativeEvent()38 DmNativeEvent::~DmNativeEvent()
39 {
40 LOGI("DmNativeEvent::~DmNativeEvent() in");
41 }
42
On(std::string & eventType,JSIValue handle,JSIValue thisVal)43 void DmNativeEvent::On(std::string &eventType, JSIValue handle, JSIValue thisVal)
44 {
45 LOGI("DmNativeEvent::On in for event: %s", eventType.c_str());
46 std::shared_ptr<DmEventListener> listener= std::make_shared<DmEventListener>();
47
48 listener->eventType = eventType;
49 listener->handlerRef = JSI::AcquireValue(handle);
50
51 listener->thisVarRef_ = JSI::AcquireValue(thisVal);
52 eventMap_[eventType] = listener;
53 }
54
Off(std::string & eventType)55 void DmNativeEvent::Off(std::string &eventType)
56 {
57 LOGI("DmNativeEvent Off in for event: %s", eventType.c_str());
58 auto iter = eventMap_.find(eventType);
59 if (iter == eventMap_.end()) {
60 LOGE("eventType %s not find", eventType.c_str());
61 return;
62 }
63 std::shared_ptr<DmEventListener> listener = iter->second;
64 JSI::ReleaseValue(listener->handlerRef);
65
66 JSI::ReleaseValue(listener->thisVarRef_);
67 eventMap_.erase(eventType);
68 }
69
OnEvent(const std::string & eventType,uint8_t argsSize,const JSIValue * data)70 void DmNativeEvent::OnEvent(const std::string &eventType, uint8_t argsSize, const JSIValue *data)
71 {
72 LOGI("OnEvent for %s", eventType.c_str());
73
74 auto iter = eventMap_.find(eventType);
75 if (iter == eventMap_.end()) {
76 LOGE("eventType %s not find", eventType.c_str());
77 return;
78 }
79 auto listener = iter->second;
80 if (!JSI::ValueIsFunction(listener->handlerRef)) {
81 LOGI("OnEvent for %s handlerRef is null", eventType.c_str());
82 return;
83 }
84
85 FuncParams* params = new FuncParams();
86 params->handlerRef = listener->handlerRef;
87 params->thisVarRef_ = listener->thisVarRef_;
88 params->args = data;
89 params->argsSize = argsSize;
90
91 LOGI("OnEventAsyncWorkFunc for %s in", eventType.c_str());
92 JsAsyncWork::DispatchAsyncWork(OnEventAsyncWorkFunc, reinterpret_cast<void *>(params));
93 }
94
OnEventAsyncWorkFunc(void * data)95 void DmNativeEvent::OnEventAsyncWorkFunc(void *data)
96 {
97 LOGI("OnEventAsyncWorkFunc in ");
98 FuncParams* params = reinterpret_cast<FuncParams *>(data);
99 JSI::CallFunction(params->handlerRef, params->thisVarRef_, params->args, params->argsSize);
100 }
101 }
102 }