• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #include "hisysevent_manager.h"
17 
18 #include "hilog/log.h"
19 #include "hisysevent_base_manager.h"
20 #include "ret_code.h"
21 
22 namespace OHOS {
23 namespace HiviewDFX {
24 namespace {
25 constexpr HiLogLabel LABEL = { LOG_CORE, 0xD002D08, "HISYSEVENT_MANAGER" };
26 }
27 
28 std::unordered_map<std::shared_ptr<HiSysEventListener>,
29         std::shared_ptr<HiSysEventBaseListener>> HiSysEventManager::listenerToBaseMap;
30 
AddListener(std::shared_ptr<HiSysEventListener> listener,std::vector<ListenerRule> & rules)31 int32_t HiSysEventManager::AddListener(std::shared_ptr<HiSysEventListener> listener,
32     std::vector<ListenerRule>& rules)
33 {
34     if (listener == nullptr) {
35         HiLog::Warn(LABEL, "add a null listener is not allowed.");
36         return ERR_LISTENER_NOT_EXIST;
37     }
38     auto baseListener = listenerToBaseMap[listener];
39     if (baseListener == nullptr) {
40         baseListener = std::make_shared<HiSysEventBaseListener>(listener);
41         listenerToBaseMap[listener] = baseListener;
42     }
43     return HiSysEventBaseManager::AddListener(baseListener, rules);
44 }
45 
RemoveListener(std::shared_ptr<HiSysEventListener> listener)46 int32_t HiSysEventManager::RemoveListener(std::shared_ptr<HiSysEventListener> listener)
47 {
48     if (listener == nullptr) {
49         HiLog::Warn(LABEL, "remov a null listener is not allowed.");
50         return ERR_LISTENER_NOT_EXIST;
51     }
52     auto baseListener = listenerToBaseMap[listener];
53     if (baseListener == nullptr) {
54         HiLog::Warn(LABEL, "no need to remove a listener which has not been added.");
55         return ERR_LISTENER_NOT_EXIST;
56     }
57     auto ret = HiSysEventBaseManager::RemoveListener(baseListener);
58     if (ret == IPC_CALL_SUCCEED) {
59         HiLog::Debug(LABEL, "remove listener from local cache.");
60         listenerToBaseMap.erase(listener);
61     }
62     return ret;
63 }
64 
Query(struct QueryArg & arg,std::vector<QueryRule> & rules,std::shared_ptr<HiSysEventQueryCallback> callback)65 int32_t HiSysEventManager::Query(struct QueryArg& arg, std::vector<QueryRule>& rules,
66     std::shared_ptr<HiSysEventQueryCallback> callback)
67 {
68     auto baseQueryCallback = std::make_shared<HiSysEventBaseQueryCallback>(callback);
69     return HiSysEventBaseManager::Query(arg, rules, baseQueryCallback);
70 }
71 
SetDebugMode(std::shared_ptr<HiSysEventListener> listener,bool mode)72 int32_t HiSysEventManager::SetDebugMode(std::shared_ptr<HiSysEventListener> listener, bool mode)
73 {
74     if (listener == nullptr) {
75         HiLog::Warn(LABEL, "set debug mode on a null listener is not allowed.");
76         return ERR_LISTENER_NOT_EXIST;
77     }
78     auto baseListener = listenerToBaseMap[listener];
79     if (baseListener == nullptr) {
80         HiLog::Warn(LABEL, "no need to set debug mode on a listener which has not been added.");
81         return ERR_LISTENER_NOT_EXIST;
82     }
83     return HiSysEventBaseManager::SetDebugMode(baseListener, mode);
84 }
85 } // namespace HiviewDFX
86 } // namespace OHOS
87