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