• 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);
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 
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);
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 == 0) {
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 }, { 2, getWindowSizeParams });
149     };
150     eventHandler->PostTask(task, event);
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