• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023-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  * Description: supply cast session manager listener for napi interface.
15  * Author: zhangjingnan
16  * Create: 2022-7-11
17  */
18 
19 #ifndef NAPI_CAST_SESSION_MANAGER_LISTENER_H
20 #define NAPI_CAST_SESSION_MANAGER_LISTENER_H
21 
22 #include <memory>
23 #include "napi/native_api.h"
24 #include "napi/native_node_api.h"
25 #include <list>
26 #include "cast_engine_common.h"
27 #include "i_cast_session_manager_listener.h"
28 #include "napi_callback.h"
29 #include "napi_castengine_utils.h"
30 
31 namespace OHOS {
32 namespace CastEngine {
33 namespace CastEngineClient {
34 class NapiCastSessionManagerListener : public ICastSessionManagerListener {
35 public:
36     using NapiArgsGetter = std::function<void(napi_env env, int &argc, napi_value *argv)>;
37     enum {
38         EVENT_SERVICE_DIED,
39         EVENT_DEVICE_FOUND,
40         EVENT_SESSION_CREATE,
41         EVENT_DEVICE_OFFLINE,
42         EVENT_TYPE_MAX
43     };
NapiCastSessionManagerListener()44     NapiCastSessionManagerListener() {};
45     ~NapiCastSessionManagerListener() override;
46 
47     void OnDeviceFound(const std::vector<CastRemoteDevice> &deviceList) override;
48     void OnDeviceOffline(const std::string &deviceId) override;
49     void OnSessionCreated(const std::shared_ptr<ICastSession> &castSession) override;
50     void OnServiceDied() override;
51 
52     napi_status AddCallback(napi_env env, int32_t event, napi_value callback);
53     napi_status RemoveCallback(napi_env env, int32_t event, napi_value callback);
54 
55 private:
56     void HandleEvent(int32_t event, NapiArgsGetter getter);
57     napi_status ClearCallback(napi_env env);
58 
59     std::mutex lock_;
60     std::shared_ptr<NapiCallback> callback_;
61     std::list<napi_ref> callbacks_[EVENT_TYPE_MAX] {};
62 };
63 } // namespace CastEngineClient
64 } // namespace CastEngine
65 } // namespace OHOS
66 #endif