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 16 use super::config::{Action, Version}; 17 use super::info::State; 18 use super::reason::Reason; 19 use crate::FileSpec; 20 21 // NotifyData's callback arg 22 #[derive(Copy, Clone, Debug, PartialEq, Eq)] 23 pub(crate) enum SubscribeType { 24 Complete = 0, 25 Fail, 26 HeaderReceive, 27 Pause, 28 Progress, 29 Remove, 30 Resume, 31 // Response, 32 FaultOccur = 8, 33 } 34 35 #[derive(Clone, Debug, PartialEq, Eq)] 36 pub(crate) enum WaitingCause { 37 TaskQueue = 0, 38 Network, 39 AppState, 40 UserState, 41 } 42 43 #[derive(Debug, Clone)] 44 pub(crate) struct NotifyData { 45 pub(crate) bundle: String, 46 pub(crate) progress: Progress, 47 pub(crate) action: Action, 48 pub(crate) version: Version, 49 pub(crate) each_file_status: Vec<EachFileStatus>, 50 pub(crate) task_id: u32, 51 pub(crate) uid: u64, 52 } 53 54 #[repr(C)] 55 #[derive(Clone, Debug)] 56 pub(crate) struct CommonProgress { 57 pub(crate) state: u8, 58 pub(crate) index: usize, 59 pub(crate) total_processed: usize, 60 } 61 62 #[derive(Debug, Clone)] 63 pub(crate) struct Progress { 64 pub(crate) common_data: CommonProgress, 65 /// Total size of the files. 66 pub(crate) sizes: Vec<i64>, 67 /// Each progress size of the files. 68 pub(crate) processed: Vec<usize>, 69 pub(crate) extras: HashMap<String, String>, 70 } 71 72 #[derive(Debug, Clone, PartialEq, Eq)] 73 #[repr(C)] 74 pub(crate) struct EachFileStatus { 75 pub(crate) path: String, 76 pub(crate) reason: Reason, 77 pub(crate) message: String, 78 } 79 80 impl EachFileStatus { create_each_file_status( file_specs: &[FileSpec], index: usize, reason: Reason, ) -> Vec<EachFileStatus>81 pub(crate) fn create_each_file_status( 82 file_specs: &[FileSpec], 83 index: usize, 84 reason: Reason, 85 ) -> Vec<EachFileStatus> { 86 let mut vec = Vec::new(); 87 for (i, file_spec) in file_specs.iter().enumerate() { 88 let code = if i >= index { reason } else { Reason::Default }; 89 let each_file_status = EachFileStatus { 90 path: file_spec.path.clone(), 91 reason: code, 92 message: code.to_str().into(), 93 }; 94 vec.push(each_file_status); 95 } 96 vec 97 } 98 } 99 100 impl Progress { new(sizes: Vec<i64>) -> Self101 pub(crate) fn new(sizes: Vec<i64>) -> Self { 102 let len = sizes.len(); 103 Progress { 104 common_data: CommonProgress { 105 state: State::Initialized.repr, 106 index: 0, 107 total_processed: 0, 108 }, 109 sizes, 110 processed: vec![0; len], 111 extras: HashMap::<String, String>::new(), 112 } 113 } 114 is_finish(&self) -> bool115 pub(crate) fn is_finish(&self) -> bool { 116 self.sizes.iter().all(|a| *a != -1) 117 && self.processed.iter().sum::<usize>() == self.sizes.iter().sum::<i64>() as usize 118 } 119 } 120