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