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 
SetEventHandler(std::shared_ptr<AppExecFwk::EventHandler> handler)115 void PanelListenerImpl::SetEventHandler(std::shared_ptr<AppExecFwk::EventHandler> handler)
116 {
117     std::unique_lock<decltype(eventHandlerMutex_)> lock(eventHandlerMutex_);
118     handler_ = handler;
119 }
120 
GetEventHandler()121 std::shared_ptr<AppExecFwk::EventHandler> PanelListenerImpl::GetEventHandler()
122 {
123     std::shared_lock<decltype(eventHandlerMutex_)> lock(eventHandlerMutex_);
124     return handler_;
125 }
126 
GetCallback(uint32_t windowId,const std::string & type)127 std::shared_ptr<JSCallbackObject> PanelListenerImpl::GetCallback(uint32_t windowId, const std::string &type)
128 {
129     std::shared_ptr<JSCallbackObject> callBack = nullptr;
130     callbacks_.ComputeIfPresent(windowId, [&type, &callBack](uint32_t id, auto callbacks) {
131         auto it = callbacks.find(type);
132         if (it == callbacks.end()) {
133             return !callbacks.empty();
134         }
135         callBack = it->second;
136         return !callbacks.empty();
137     });
138     return callBack;
139 }
140 
Write(napi_env env,const WindowSize & nativeObject)141 napi_value JsWindowSize::Write(napi_env env, const WindowSize &nativeObject)
142 {
143     napi_value jsObject = nullptr;
144     napi_create_object(env, &jsObject);
145     bool ret = JsUtil::Object::WriteProperty(env, jsObject, "width", nativeObject.width);
146     ret = ret && JsUtil::Object::WriteProperty(env, jsObject, "height", nativeObject.height);
147     return ret ? jsObject : JsUtil::Const::Null(env);
148 }
149 
Read(napi_env env,napi_value jsObject,WindowSize & nativeObject)150 bool JsWindowSize::Read(napi_env env, napi_value jsObject, WindowSize &nativeObject)
151 {
152     auto ret = JsUtil::Object::ReadProperty(env, jsObject, "width", nativeObject.width);
153     ret = ret && JsUtil::Object::ReadProperty(env, jsObject, "height", nativeObject.height);
154     return ret;
155 }
156 } // namespace MiscServices
157 } // namespace OHOS