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