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 #include "app_event_processor_mgr.h"
16
17 #include "app_event_store.h"
18 #include "app_event_observer_mgr.h"
19 #include "hiappevent_verify.h"
20 #include "processor_config_loader.h"
21
22 namespace OHOS {
23 namespace HiviewDFX {
24 namespace HiAppEvent {
AddProcessor(const ReportConfig & config)25 int64_t AppEventProcessorMgr::AddProcessor(const ReportConfig& config)
26 {
27 if (!IsApp()) {
28 return ErrorCode::ERROR_NOT_APP;
29 }
30 ReportConfig realConfig = config;
31 if (int ret = VerifyReportConfig(realConfig); ret != 0) {
32 return ret;
33 }
34 if (int ret = AppEventObserverMgr::GetInstance().Load(realConfig.name); ret != 0) {
35 return ret;
36 }
37 if (!realConfig.configName.empty()) {
38 HiAppEvent::ProcessorConfigLoader loader;
39 if (loader.LoadProcessorConfig(realConfig.name, realConfig.configName)) {
40 realConfig = loader.GetReportConfig();
41 }
42 }
43 return AppEventObserverMgr::GetInstance().AddProcessor(realConfig.name, realConfig);
44 }
45
RemoveProcessor(int64_t processorId)46 int AppEventProcessorMgr::RemoveProcessor(int64_t processorId)
47 {
48 if (!IsApp()) {
49 return ErrorCode::ERROR_NOT_APP;
50 }
51 return AppEventObserverMgr::GetInstance().RemoveObserver(processorId);
52 }
53
RegisterProcessor(const std::string & name,std::shared_ptr<AppEventProcessor> processor)54 int AppEventProcessorMgr::RegisterProcessor(const std::string& name, std::shared_ptr<AppEventProcessor> processor)
55 {
56 if (!IsApp()) {
57 return ErrorCode::ERROR_NOT_APP;
58 }
59 return AppEventObserverMgr::GetInstance().RegisterProcessor(name, processor);
60 }
61
UnregisterProcessor(const std::string & name)62 int AppEventProcessorMgr::UnregisterProcessor(const std::string& name)
63 {
64 if (!IsApp()) {
65 return ErrorCode::ERROR_NOT_APP;
66 }
67 if (int ret = AppEventObserverMgr::GetInstance().RemoveObserver(name); ret < 0) {
68 return ret;
69 }
70 return AppEventObserverMgr::GetInstance().UnregisterProcessor(name);
71 }
72
SetProcessorConfig(int64_t processorSeq,const ReportConfig & config)73 int AppEventProcessorMgr::SetProcessorConfig(int64_t processorSeq, const ReportConfig& config)
74 {
75 if (!IsApp()) {
76 return ErrorCode::ERROR_NOT_APP;
77 }
78 return AppEventObserverMgr::GetInstance().SetReportConfig(processorSeq, config);
79 }
80
GetProcessorConfig(int64_t processorSeq,ReportConfig & config)81 int AppEventProcessorMgr::GetProcessorConfig(int64_t processorSeq, ReportConfig& config)
82 {
83 if (!IsApp()) {
84 return ErrorCode::ERROR_NOT_APP;
85 }
86 return AppEventObserverMgr::GetInstance().GetReportConfig(processorSeq, config);
87 }
88
GetProcessorSeqs(const std::string & name,std::vector<int64_t> & processorSeqs)89 int AppEventProcessorMgr::GetProcessorSeqs(const std::string& name, std::vector<int64_t>& processorSeqs)
90 {
91 if (!IsApp()) {
92 return ErrorCode::ERROR_NOT_APP;
93 }
94 processorSeqs.clear(); // prevent repeated invoking scenarios
95 return AppEventStore::GetInstance().QueryObserverSeqs(name, processorSeqs);
96 }
97 } // namespace HiAppEvent
98 } // namespace HiviewDFX
99 } // namespace OHOS
100