1 // Copyright (C) 2023 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 use std::collections::HashMap;
15 use std::time::Duration;
16 
17 use sql::SqlList;
18 use ylong_runtime::task::JoinHandle;
19 
20 use super::qos::RssCapacity;
21 use crate::manage::network::NetworkState;
22 use crate::manage::network_manager::NetworkManager;
23 use crate::manage::task_manager::TaskManagerTx;
24 #[cfg(feature = "oh")]
25 #[cfg(not(test))]
26 use crate::utils::GetTopUid;
27 
28 mod recorder;
29 pub(crate) mod sql;
30 
31 pub(crate) struct Handler {
32     recorder: recorder::StateRecord,
33     background_timeout: HashMap<u64, JoinHandle<()>>,
34     task_manager: TaskManagerTx,
35 }
36 
37 impl Handler {
new(task_manager: TaskManagerTx) -> Self38     pub(crate) fn new(task_manager: TaskManagerTx) -> Self {
39         Handler {
40             recorder: recorder::StateRecord::new(),
41             background_timeout: HashMap::new(),
42             task_manager,
43         }
44     }
45 
init(&mut self) -> SqlList46     pub(crate) fn init(&mut self) -> SqlList {
47         let network_info = NetworkManager::query_network();
48         let (foreground_account, active_accounts) = NetworkManager::query_active_accounts();
49 
50         #[allow(unused_mut)]
51         let mut top_uid = 0;
52 
53         #[cfg(not(test))]
54         #[cfg(feature = "oh")]
55         {
56             for _ in 0..10 {
57                 let res = GetTopUid(&mut top_uid);
58                 if res != 0 || top_uid == 0 {
59                     error!("Get top uid failed, res: {}", top_uid);
60                     std::thread::sleep(Duration::from_millis(500));
61                 }
62             }
63         }
64         let top_uid = if top_uid == 0 {
65             None
66         } else {
67             Some(top_uid as u64)
68         };
69         self.recorder
70             .init(network_info, top_uid, foreground_account, active_accounts)
71     }
72 
update_rss_level(&mut self, level: i32) -> Option<RssCapacity>73     pub(crate) fn update_rss_level(&mut self, level: i32) -> Option<RssCapacity> {
74         self.recorder.update_rss_level(level)
75     }
76 
update_network(&mut self, _a: ()) -> Option<SqlList>77     pub(crate) fn update_network(&mut self, _a: ()) -> Option<SqlList> {
78         let network_info = NetworkManager::query_network();
79         self.recorder.update_network(network_info)
80     }
81 
update_account(&mut self, _a: ()) -> Option<SqlList>82     pub(crate) fn update_account(&mut self, _a: ()) -> Option<SqlList> {
83         let (foreground_account, active_accounts) = NetworkManager::query_active_accounts();
84         self.recorder
85             .update_accounts(foreground_account, active_accounts)
86     }
87 
update_top_uid(&mut self, top_uid: u64) -> Option<SqlList>88     pub(crate) fn update_top_uid(&mut self, top_uid: u64) -> Option<SqlList> {
89         if self.top_uid() == Some(top_uid) {
90             return None;
91         }
92         if let Some(uid) = self.top_uid() {
93             self.update_background(uid);
94         }
95         if let Some(handle) = self.background_timeout.remove(&top_uid) {
96             handle.cancel();
97         }
98         self.recorder.update_top_uid(top_uid)
99     }
100 
update_background(&mut self, uid: u64) -> Option<SqlList>101     pub(crate) fn update_background(&mut self, uid: u64) -> Option<SqlList> {
102         if Some(uid) != self.top_uid() {
103             return None;
104         }
105         let task_manager = self.task_manager.clone();
106         self.background_timeout.insert(
107             uid,
108             ylong_runtime::spawn(async move {
109                 ylong_runtime::time::sleep(Duration::from_secs(60)).await;
110                 task_manager.trigger_background_timeout(uid);
111             }),
112         );
113         self.recorder.update_background();
114         None
115     }
116 
update_background_timeout(&mut self, uid: u64) -> Option<SqlList>117     pub(crate) fn update_background_timeout(&mut self, uid: u64) -> Option<SqlList> {
118         self.recorder.update_background_timeout(uid)
119     }
120 
special_process_terminate(&mut self, uid: u64) -> Option<SqlList>121     pub(crate) fn special_process_terminate(&mut self, uid: u64) -> Option<SqlList> {
122         info!("hiviewx terminate handle. {:?}", uid);
123         let mut sql_list = SqlList::new();
124         sql_list.add_special_process_terminate(uid);
125         Some(sql_list)
126     }
127 
top_uid(&self) -> Option<u64>128     pub(crate) fn top_uid(&self) -> Option<u64> {
129         self.recorder.top_uid
130     }
131 
top_user(&self) -> u64132     pub(crate) fn top_user(&self) -> u64 {
133         self.recorder.top_user
134     }
135 
network(&self) -> &NetworkState136     pub(crate) fn network(&self) -> &NetworkState {
137         &self.recorder.network
138     }
139 }
140