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 std::lock_guard<std::mutex> lock(eventmapMutex_);
53 eventMap_[eventType] = listener;
54 }
55
Off(std::string & eventType)56 void DmNativeEvent::Off(std::string &eventType)
57 {
58 LOGI("DmNativeEvent Off in for event: %s", eventType.c_str());
59 std::lock_guard<std::mutex> lock(eventmapMutex_);
60 auto iter = eventMap_.find(eventType);
61 if (iter == eventMap_.end()) {
62 LOGE("eventType %s not find", eventType.c_str());
63 return;
64 }
65 std::shared_ptr<DmEventListener> listener = iter->second;
66 JSI::ReleaseValue(listener->handlerRef);
67
68 JSI::ReleaseValue(listener->thisVarRef_);
69 eventMap_.erase(eventType);
70 }
71
OnEvent(const std::string & eventType,uint8_t argsSize,const JSIValue * data)72 void DmNativeEvent::OnEvent(const std::string &eventType, uint8_t argsSize, const JSIValue *data)
73 {
74 LOGI("OnEvent for %s", eventType.c_str());
75 std::shared_ptr<DmEventListener> listener = nullptr;
76 {
77 std::lock_guard<std::mutex> lock(eventmapMutex_);
78 auto iter = eventMap_.find(eventType);
79 if (iter == eventMap_.end()) {
80 LOGE("eventType %s not find", eventType.c_str());
81 return;
82 }
83 listener = iter->second;
84 }
85
86 if (!JSI::ValueIsFunction(listener->handlerRef)) {
87 LOGI("OnEvent for %s handlerRef is null", eventType.c_str());
88 return;
89 }
90
91 FuncParams* params = new FuncParams();
92 params->handlerRef = listener->handlerRef;
93 params->thisVarRef_ = listener->thisVarRef_;
94 params->args = data;
95 params->argsSize = argsSize;
96
97 LOGI("OnEventAsyncWorkFunc for %s in", eventType.c_str());
98 JsAsyncWork::DispatchAsyncWork(OnEventAsyncWorkFunc, reinterpret_cast<void *>(params));
99 }
100
OnEventAsyncWorkFunc(void * data)101 void DmNativeEvent::OnEventAsyncWorkFunc(void *data)
102 {
103 LOGI("OnEventAsyncWorkFunc in ");
104 FuncParams* params = reinterpret_cast<FuncParams *>(data);
105 JSI::CallFunction(params->handlerRef, params->thisVarRef_, params->args, params->argsSize);
106 }
107 }
108 }