• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
JsScreenListener(NativeEngine * engine)28     explicit JsScreenListener(NativeEngine* engine) : engine_(engine) {}
29     ~JsScreenListener() override = default;
30     void AddCallback(const std::string& type, NativeValue* jsListenerObject);
31     void RemoveAllCallback();
32     void RemoveCallback(const std::string& type, NativeValue* jsListenerObject);
33     void OnConnect(ScreenId id) override;
34     void OnDisconnect(ScreenId id) override;
35     void OnChange(ScreenId id) override;
36 
37 private:
38     void CallJsMethod(const std::string& methodName, NativeValue* const* argv = nullptr, size_t argc = 0);
39     NativeEngine* engine_ = nullptr;
40     std::mutex mtx_;
41     std::map<std::string, std::vector<std::unique_ptr<NativeReference>>> jsCallBack_;
42     NativeValue* CreateScreenIdArray(NativeEngine& engine, const std::vector<ScreenId>& data);
43 };
44 const std::string EVENT_CONNECT = "connect";
45 const std::string EVENT_DISCONNECT = "disconnect";
46 const std::string EVENT_CHANGE = "change";
47 }  // namespace Rosen
48 }  // namespace OHOS
49 #endif /* OHOS_JS_SCREEN_LISTENER_H */