1 /*
2 * Copyright (c) 2023 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 "panel_listener_impl.h"
17
18 #include "js_callback_handler.h"
19 #include "js_utils.h"
20
21 namespace OHOS {
22 namespace MiscServices {
23 std::shared_ptr<PanelListenerImpl> PanelListenerImpl::instance_{ nullptr };
24 std::mutex PanelListenerImpl::listenerMutex_;
25 constexpr uint8_t SIE_CHANGE_PARAM_COUNT = 2;
GetInstance()26 std::shared_ptr<PanelListenerImpl> PanelListenerImpl::GetInstance()
27 {
28 if (instance_ == nullptr) {
29 std::lock_guard<std::mutex> lock(listenerMutex_);
30 if (instance_ == nullptr) {
31 instance_ = std::make_shared<PanelListenerImpl>();
32 }
33 }
34 return instance_;
35 }
36
~PanelListenerImpl()37 PanelListenerImpl::~PanelListenerImpl() {}
38
Subscribe(uint32_t windowId,const std::string & type,std::shared_ptr<JSCallbackObject> cbObject)39 void PanelListenerImpl::Subscribe(uint32_t windowId, const std::string &type,
40 std::shared_ptr<JSCallbackObject> cbObject)
41 {
42 callbacks_.Compute(windowId,
43 [cbObject, &type](auto windowId, std::map<std::string, std::shared_ptr<JSCallbackObject>> &cbs) {
44 auto [it, insert] = cbs.try_emplace(type, cbObject);
45 if (insert) {
46 IMSA_HILOGI("start to subscribe type: %{public}s of windowId: %{public}u.", type.c_str(), windowId);
47 } else {
48 IMSA_HILOGD("type: %{public}s of windowId: %{public}u already subscribed.", type.c_str(), windowId);
49 }
50 return !cbs.empty();
51 });
52 }
53
RemoveInfo(const std::string & type,uint32_t windowId)54 void PanelListenerImpl::RemoveInfo(const std::string &type, uint32_t windowId)
55 {
56 callbacks_.ComputeIfPresent(windowId,
57 [&type](auto windowId, std::map<std::string, std::shared_ptr<JSCallbackObject>> &cbs) {
58 cbs.erase(type);
59 return !cbs.empty();
60 });
61 }
62
OnPanelStatus(uint32_t windowId,bool isShow)63 void PanelListenerImpl::OnPanelStatus(uint32_t windowId, bool isShow)
64 {
65 std::string type = isShow ? "show" : "hide";
66 auto eventHandler = GetEventHandler();
67 if (eventHandler == nullptr) {
68 IMSA_HILOGE("eventHandler is nullptr!");
69 return;
70 }
71 std::shared_ptr<JSCallbackObject> callBack = GetCallback(windowId, type);
72 if (callBack == nullptr) {
73 IMSA_HILOGE("callBack is nullptr!");
74 return;
75 }
76 auto entry = std::make_shared<UvEntry>(callBack);
77 IMSA_HILOGI("windowId = %{public}u, type = %{public}s", windowId, type.c_str());
78 auto task = [entry]() {
79 JsCallbackHandler::Traverse({ entry->cbCopy });
80 };
81 eventHandler->PostTask(task, type, 0, AppExecFwk::EventQueue::Priority::VIP);
82 }
83
OnSizeChange(uint32_t windowId,const WindowSize & size)84 void PanelListenerImpl::OnSizeChange(uint32_t windowId, const WindowSize &size)
85 {
86 std::string type = "sizeChange";
87 auto eventHandler = GetEventHandler();
88 if (eventHandler == nullptr) {
89 IMSA_HILOGE("eventHandler is nullptr!");
90 return;
91 }
92 std::shared_ptr<JSCallbackObject> callBack = GetCallback(windowId, type);
93 if (callBack == nullptr) {
94 return;
95 }
96 auto entry = std::make_shared<UvEntry>(callBack);
97 entry->size = size;
98 IMSA_HILOGI("OnSizeChange start. windowId:%{public}u, w:%{public}u, h:%{public}u", windowId, size.width,
99 size.height);
100 auto task = [entry]() {
101 auto gitWindowSizeParams = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool {
102 if (argc == 0) {
103 return false;
104 }
105 napi_value windowSize = JsWindowSize::Write(env, entry->size);
106 // 0 means the first param of callback.
107 args[0] = { windowSize };
108 return true;
109 };
110 JsCallbackHandler::Traverse({ entry->cbCopy }, { 1, gitWindowSizeParams });
111 };
112 eventHandler->PostTask(task, type, 0, AppExecFwk::EventQueue::Priority::VIP);
113 }
114
OnSizeChange(uint32_t windowId,const WindowSize & size,const PanelAdjustInfo & keyboardArea,const std::string & event)115 void PanelListenerImpl::OnSizeChange(
116 uint32_t windowId, const WindowSize &size, const PanelAdjustInfo &keyboardArea, const std::string &event)
117 {
118 std::string type = "sizeUpdate";
119 auto eventHandler = GetEventHandler();
120 if (eventHandler == nullptr) {
121 IMSA_HILOGE("eventHandler is nullptr!");
122 return;
123 }
124 std::shared_ptr<JSCallbackObject> callBack = GetCallback(windowId, event);
125 if (callBack == nullptr) {
126 IMSA_HILOGE("callback is nullptr");
127 return;
128 }
129 auto entry = std::make_shared<UvEntry>(callBack);
130 entry->size = size;
131 entry->keyboardArea = keyboardArea;
132 IMSA_HILOGI("%{public}s start. windowId:%{public}u, windowSize[%{public}u/%{public}u], "
133 "keyboardArea:[%{public}d/%{public}d/%{public}d/%{public}d]",
134 event.c_str(), windowId, size.width, size.height, keyboardArea.top, keyboardArea.bottom, keyboardArea.left,
135 keyboardArea.right);
136 auto task = [entry]() {
137 auto getWindowSizeParams = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool {
138 if (argc < SIE_CHANGE_PARAM_COUNT) {
139 return false;
140 }
141 napi_value windowSize = JsWindowSize::Write(env, entry->size);
142 napi_value jsKeyboardArea = JsKeyboardArea::Write(env, entry->keyboardArea);
143 args[0] = { windowSize };
144 args[1] = { jsKeyboardArea };
145 return true;
146 };
147 // 2 means 'sizeChange' has 2 params
148 JsCallbackHandler::Traverse({ entry->cbCopy }, { SIE_CHANGE_PARAM_COUNT, getWindowSizeParams });
149 };
150 eventHandler->PostTask(task, event, 0, AppExecFwk::EventQueue::Priority::VIP);
151 }
152
SetEventHandler(std::shared_ptr<AppExecFwk::EventHandler> handler)153 void PanelListenerImpl::SetEventHandler(std::shared_ptr<AppExecFwk::EventHandler> handler)
154 {
155 std::unique_lock<decltype(eventHandlerMutex_)> lock(eventHandlerMutex_);
156 handler_ = handler;
157 }
158
GetEventHandler()159 std::shared_ptr<AppExecFwk::EventHandler> PanelListenerImpl::GetEventHandler()
160 {
161 std::shared_lock<decltype(eventHandlerMutex_)> lock(eventHandlerMutex_);
162 return handler_;
163 }
164
GetCallback(uint32_t windowId,const std::string & type)165 std::shared_ptr<JSCallbackObject> PanelListenerImpl::GetCallback(uint32_t windowId, const std::string &type)
166 {
167 std::shared_ptr<JSCallbackObject> callBack = nullptr;
168 callbacks_.ComputeIfPresent(windowId, [&type, &callBack](uint32_t id, auto callbacks) {
169 auto it = callbacks.find(type);
170 if (it == callbacks.end()) {
171 return !callbacks.empty();
172 }
173 callBack = it->second;
174 return !callbacks.empty();
175 });
176 return callBack;
177 }
178
Write(napi_env env,const WindowSize & nativeObject)179 napi_value JsWindowSize::Write(napi_env env, const WindowSize &nativeObject)
180 {
181 napi_value jsObject = nullptr;
182 napi_create_object(env, &jsObject);
183 bool ret = JsUtil::Object::WriteProperty(env, jsObject, "width", nativeObject.width);
184 ret = ret && JsUtil::Object::WriteProperty(env, jsObject, "height", nativeObject.height);
185 return ret ? jsObject : JsUtil::Const::Null(env);
186 }
187
Read(napi_env env,napi_value jsObject,WindowSize & nativeObject)188 bool JsWindowSize::Read(napi_env env, napi_value jsObject, WindowSize &nativeObject)
189 {
190 auto ret = JsUtil::Object::ReadProperty(env, jsObject, "width", nativeObject.width);
191 ret = ret && JsUtil::Object::ReadProperty(env, jsObject, "height", nativeObject.height);
192 return ret;
193 }
194
Write(napi_env env,const PanelAdjustInfo & nativeObject)195 napi_value JsKeyboardArea::Write(napi_env env, const PanelAdjustInfo &nativeObject)
196 {
197 napi_value jsObject = nullptr;
198 napi_create_object(env, &jsObject);
199 bool ret = JsUtil::Object::WriteProperty(env, jsObject, "top", nativeObject.top);
200 ret = ret && JsUtil::Object::WriteProperty(env, jsObject, "bottom", nativeObject.bottom);
201 ret = ret && JsUtil::Object::WriteProperty(env, jsObject, "left", nativeObject.left);
202 ret = ret && JsUtil::Object::WriteProperty(env, jsObject, "right", nativeObject.right);
203 return ret ? jsObject : JsUtil::Const::Null(env);
204 }
205
Read(napi_env env,napi_value jsObject,PanelAdjustInfo & nativeObject)206 bool JsKeyboardArea::Read(napi_env env, napi_value jsObject, PanelAdjustInfo &nativeObject)
207 {
208 bool ret = JsUtil::Object::ReadProperty(env, jsObject, "top", nativeObject.top);
209 ret = ret && JsUtil::Object::ReadProperty(env, jsObject, "bottom", nativeObject.bottom);
210 ret = ret && JsUtil::Object::ReadProperty(env, jsObject, "left", nativeObject.left);
211 ret = ret && JsUtil::Object::ReadProperty(env, jsObject, "right", nativeObject.right);
212 return ret;
213 }
214 } // namespace MiscServices
215 } // namespace OHOS
216