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 #ifndef OHOS_JS_SCREEN_LISTENER_H 17 #define OHOS_JS_SCREEN_LISTENER_H 18 19 #include <mutex> 20 #include "native_engine/native_engine.h" 21 #include "native_engine/native_value.h" 22 #include "screen_manager.h" 23 24 namespace OHOS { 25 namespace Rosen { 26 class JsScreenListener : public ScreenManager::IScreenListener { 27 public: 28 explicit JsScreenListener(napi_env env); 29 ~JsScreenListener() override; 30 void AddCallback(const std::string& type, napi_value jsListenerObject); 31 void RemoveAllCallback(); 32 void RemoveCallback(napi_env env, const std::string& type, napi_value jsListenerObject); 33 void OnConnect(ScreenId id) override; 34 void OnDisconnect(ScreenId id) override; 35 void OnChange(ScreenId id) override; 36 static void CleanEnv(void* obj); 37 38 private: 39 void CallJsMethod(const std::string& methodName, napi_value const * argv = nullptr, size_t argc = 0); 40 napi_env env_ = nullptr; 41 std::mutex mtx_; 42 wptr<JsScreenListener> weakRef_ = nullptr; 43 std::map<std::string, std::vector<std::unique_ptr<NativeReference>>> jsCallBack_; 44 napi_value CreateScreenIdArray(napi_env env, const std::vector<ScreenId>& data); 45 }; 46 const std::string EVENT_CONNECT = "connect"; 47 const std::string EVENT_DISCONNECT = "disconnect"; 48 const std::string EVENT_CHANGE = "change"; 49 } // namespace Rosen 50 } // namespace OHOS 51 #endif /* OHOS_JS_SCREEN_LISTENER_H */