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