• 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 #ifndef UPDATE_SESSION_MGR_H
17 #define UPDATE_SESSION_MGR_H
18 
19 #include <map>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 
24 #include "js_native_api_types.h"
25 
26 #include "base_session.h"
27 #include "event_classify_info.h"
28 #include "event_info.h"
29 
30 namespace OHOS::UpdateEngine {
31 class SessionManager {
32 public:
33     SessionManager(napi_env env, napi_ref thisReference);
34     ~SessionManager();
35 
36     void AddSession(std::shared_ptr<BaseSession> session);
37     void RemoveSession(uint32_t sessionId);
38 
39     int32_t ProcessUnsubscribe(const std::string &eventType, size_t argc, napi_value arg);
40     void Unsubscribe(const EventClassifyInfo &eventClassifyInfo, napi_value handle);
41 
42     BaseSession *FindSessionByHandle(napi_env env, const std::string &eventType, napi_value arg);
43     BaseSession *FindSessionByHandle(napi_env env, const EventClassifyInfo &eventClassifyInfo, napi_value arg);
44 
45     void Emit(const EventClassifyInfo &eventClassifyInfo, const EventInfo &eventInfo);
46 
47 private:
48     void PublishToJS(const EventClassifyInfo &eventClassifyInfo, const EventInfo &eventInfo);
49     bool GetFirstSessionId(uint32_t &sessionId);
50     bool GetNextSessionId(uint32_t &sessionId);
51 
52 private:
53     napi_env env_ {};
54     napi_ref thisReference_ {};
55     std::map<uint32_t, std::shared_ptr<BaseSession>> sessions_;
56     std::recursive_mutex sessionMutex_;
57 };
58 } // namespace OHOS::UpdateEngine
59 #endif // UPDATE_SESSION_MGR_H