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 
16 #include "security_collector_run_manager.h"
17 #include "security_collector_define.h"
18 #include "security_collector_log.h"
19 #include "data_collection.h"
20 #include "event_info.h"
21 #include "sg_collect_client.h"
22 
23 namespace OHOS::Security::SecurityCollector {
SecurityCollectorRunManager()24 SecurityCollectorRunManager::SecurityCollectorRunManager()
25 {
26 }
27 
GetExtraInfo()28 std::string SecurityCollectorRunManager::CollectorListenner::GetExtraInfo()
29 {
30     if (subscriber_) {
31         return subscriber_->GetSecurityCollectorSubscribeInfo().GetEvent().extra;
32     }
33     return {};
34 }
35 
OnNotify(const Event & event)36 void SecurityCollectorRunManager::CollectorListenner::OnNotify(const Event &event)
37 {
38     LOGI("eventid:%{public}" PRId64 " report by collector, store to db", event.eventId);
39     auto info = std::make_shared<SecurityGuard::EventInfo>(event.eventId, event.version, event.content);
40     (void)SecurityGuard::NativeDataCollectKit::ReportSecurityInfo(info);
41 }
42 
StartCollector(const std::shared_ptr<SecurityCollectorSubscriber> & subscriber)43 bool SecurityCollectorRunManager::StartCollector(const std::shared_ptr<SecurityCollectorSubscriber> &subscriber)
44 {
45     std::lock_guard<std::mutex> lock(collectorRunMutex_);
46     if (subscriber == nullptr) {
47         LOGE("subscriber is null");
48         return false;
49     }
50     std::string appName = subscriber->GetAppName();
51     int64_t eventId = subscriber->GetSecurityCollectorSubscribeInfo().GetEvent().eventId;
52     LOGI("appName:%{public}s, eventId:%{public}" PRId64 "", appName.c_str(), eventId);
53     if (collectorRunManager_.find(eventId) != collectorRunManager_.end()) {
54         LOGE("collector already start");
55         return false;
56     }
57 
58     auto collectorListenner = std::make_shared<SecurityCollectorRunManager::CollectorListenner>(subscriber);
59     LOGI("start collector, eventId:%{public}" PRId64 "", eventId);
60     if (!DataCollection::GetInstance().StartCollectors(std::vector<int64_t>{eventId}, collectorListenner)) {
61         LOGE("failed to start collectors");
62         return false;
63     }
64     collectorRunManager_.emplace(eventId, subscriber);
65     return true;
66 }
67 
StopCollector(const std::shared_ptr<SecurityCollectorSubscriber> & subscriber)68 bool SecurityCollectorRunManager::StopCollector(const std::shared_ptr<SecurityCollectorSubscriber> &subscriber)
69 {
70     std::lock_guard<std::mutex> lock(collectorRunMutex_);
71     if (subscriber == nullptr) {
72         LOGE("subscriber is null");
73         return false;
74     }
75     std::string appName = subscriber->GetAppName();
76     int64_t eventId = subscriber->GetSecurityCollectorSubscribeInfo().GetEvent().eventId;
77     LOGI("appName:%{public}s, eventId:%{public}" PRId64 "", appName.c_str(), eventId);
78     if (collectorRunManager_.find(eventId) == collectorRunManager_.end()) {
79         LOGE("collector no start");
80         return false;
81     }
82 
83     if (collectorRunManager_[eventId]->GetAppName() != appName) {
84         LOGE("collector starter is %{public}s, but stoper is %{public}s",
85             collectorRunManager_[eventId]->GetAppName().c_str(), appName.c_str());
86         return false;
87     }
88     LOGI("Scheduling stop collector, eventId:%{public}" PRId64 "", eventId);
89     if (!DataCollection::GetInstance().StopCollectors(std::vector<int64_t>{eventId})) {
90         LOGE("failed to stop collectors");
91         return false;
92     }
93     collectorRunManager_.erase(eventId);
94     return true;
95 }
96 }