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_base_manager.h"
17 
18 #include "hilog/log.h"
19 #include "hisysevent_delegate.h"
20 #include "ret_code.h"
21 
22 #undef LOG_DOMAIN
23 #define LOG_DOMAIN 0xD002D08
24 
25 #undef LOG_TAG
26 #define LOG_TAG "HISYSEVENT_BASE_MANAGER"
27 
28 namespace OHOS {
29 namespace HiviewDFX {
AddListener(std::shared_ptr<HiSysEventBaseListener> listener,std::vector<ListenerRule> & rules)30 int32_t HiSysEventBaseManager::AddListener(std::shared_ptr<HiSysEventBaseListener> listener,
31     std::vector<ListenerRule>& rules)
32 {
33     if (listener == nullptr) {
34         HILOG_WARN(LOG_CORE, "no need to add a listener which is null.");
35         return ERR_LISTENER_NOT_EXIST;
36     }
37     if (listener->listenerProxy == nullptr) {
38         listener->listenerProxy = new HiSysEventDelegate();
39     }
40     return listener->listenerProxy->AddListener(listener, rules);
41 }
42 
RemoveListener(std::shared_ptr<HiSysEventBaseListener> listener)43 int32_t HiSysEventBaseManager::RemoveListener(std::shared_ptr<HiSysEventBaseListener> listener)
44 {
45     if (listener == nullptr || listener->listenerProxy == nullptr) {
46         HILOG_WARN(LOG_CORE, "no need to remove a base listener which has not been added.");
47         return ERR_LISTENER_NOT_EXIST;
48     }
49     auto ret = listener->listenerProxy->RemoveListener(listener);
50     delete listener->listenerProxy;
51     listener->listenerProxy = nullptr;
52     return ret;
53 }
54 
Query(struct QueryArg & arg,std::vector<QueryRule> & rules,std::shared_ptr<HiSysEventBaseQueryCallback> callback)55 int32_t HiSysEventBaseManager::Query(struct QueryArg& arg, std::vector<QueryRule>& rules,
56     std::shared_ptr<HiSysEventBaseQueryCallback> callback)
57 {
58     auto proxy = std::make_unique<HiSysEventDelegate>();
59     if (proxy != nullptr) {
60         return proxy->Query(arg, rules, callback);
61     }
62     return ERR_LISTENER_NOT_EXIST;
63 }
64 
SetDebugMode(std::shared_ptr<HiSysEventBaseListener> listener,bool mode)65 int32_t HiSysEventBaseManager::SetDebugMode(std::shared_ptr<HiSysEventBaseListener> listener, bool mode)
66 {
67     if (listener == nullptr || listener->listenerProxy == nullptr) {
68         HILOG_WARN(LOG_CORE, "no need to set debug mode on a base listener which has not been added.");
69         return ERR_LISTENER_NOT_EXIST;
70     }
71     return listener->listenerProxy->SetDebugMode(listener, mode);
72 }
73 
Export(struct QueryArg & arg,std::vector<QueryRule> & rules)74 int64_t HiSysEventBaseManager::Export(struct QueryArg& arg, std::vector<QueryRule>& rules)
75 {
76     auto proxy = std::make_unique<HiSysEventDelegate>();
77     if (proxy != nullptr) {
78         return proxy->Export(arg, rules);
79     }
80     return ERR_LISTENER_NOT_EXIST;
81 }
82 
Subscribe(std::vector<QueryRule> & rules)83 int64_t HiSysEventBaseManager::Subscribe(std::vector<QueryRule>& rules)
84 {
85     auto proxy = std::make_unique<HiSysEventDelegate>();
86     if (proxy != nullptr) {
87         return proxy->Subscribe(rules);
88     }
89     return ERR_LISTENER_NOT_EXIST;
90 }
91 
Unsubscribe()92 int32_t HiSysEventBaseManager::Unsubscribe()
93 {
94     auto proxy = std::make_unique<HiSysEventDelegate>();
95     if (proxy != nullptr) {
96         return proxy->Unsubscribe();
97     }
98     return ERR_LISTENER_NOT_EXIST;
99 }
100 
101 } // namespace HiviewDFX
102 } // namespace OHOS
103