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 crate::info::State;
15 use crate::service::client::ClientManagerEntry;
16 use crate::task::notify::{NotifyData, SubscribeType, WaitingCause};
17 use crate::task::reason::Reason;
18 pub(crate) struct Notifier;
19
20 impl Notifier {
complete(client_manager: &ClientManagerEntry, notify_data: NotifyData)21 pub(crate) fn complete(client_manager: &ClientManagerEntry, notify_data: NotifyData) {
22 #[cfg(feature = "oh")]
23 let _ = publish_state_change_event(
24 notify_data.bundle.as_str(),
25 notify_data.task_id,
26 State::Completed.repr as i32,
27 notify_data.uid,
28 );
29 client_manager.send_notify_data(SubscribeType::Complete, notify_data)
30 }
31
fail(client_manager: &ClientManagerEntry, notify_data: NotifyData)32 pub(crate) fn fail(client_manager: &ClientManagerEntry, notify_data: NotifyData) {
33 #[cfg(feature = "oh")]
34 let _ = publish_state_change_event(
35 notify_data.bundle.as_str(),
36 notify_data.task_id,
37 State::Failed.repr as i32,
38 notify_data.uid,
39 );
40 client_manager.send_notify_data(SubscribeType::Fail, notify_data)
41 }
42
faults(tid: u32, client_manager: &ClientManagerEntry, reason: Reason)43 pub(crate) fn faults(tid: u32, client_manager: &ClientManagerEntry, reason: Reason) {
44 client_manager.send_faults(tid, SubscribeType::FaultOccur, reason)
45 }
46
pause(client_manager: &ClientManagerEntry, notify_data: NotifyData)47 pub(crate) fn pause(client_manager: &ClientManagerEntry, notify_data: NotifyData) {
48 client_manager.send_notify_data(SubscribeType::Pause, notify_data)
49 }
50
resume(client_manager: &ClientManagerEntry, notify_data: NotifyData)51 pub(crate) fn resume(client_manager: &ClientManagerEntry, notify_data: NotifyData) {
52 client_manager.send_notify_data(SubscribeType::Resume, notify_data)
53 }
54
header_receive(client_manager: &ClientManagerEntry, notify_data: NotifyData)55 pub(crate) fn header_receive(client_manager: &ClientManagerEntry, notify_data: NotifyData) {
56 client_manager.send_notify_data(SubscribeType::HeaderReceive, notify_data)
57 }
58
progress(client_manager: &ClientManagerEntry, notify_data: NotifyData)59 pub(crate) fn progress(client_manager: &ClientManagerEntry, notify_data: NotifyData) {
60 let total_processed = notify_data.progress.common_data.total_processed;
61 let file_total_size: i64 = notify_data.progress.sizes.iter().sum();
62 if total_processed == 0 && file_total_size < 0 {
63 return;
64 }
65 client_manager.send_notify_data(SubscribeType::Progress, notify_data)
66 }
67
remove(client_manager: &ClientManagerEntry, notify_data: NotifyData)68 pub(crate) fn remove(client_manager: &ClientManagerEntry, notify_data: NotifyData) {
69 let task_id = notify_data.task_id;
70 client_manager.send_notify_data(SubscribeType::Remove, notify_data);
71 client_manager.notify_task_finished(task_id);
72 }
73
waiting(client_manager: &ClientManagerEntry, task_id: u32, cause: WaitingCause)74 pub(crate) fn waiting(client_manager: &ClientManagerEntry, task_id: u32, cause: WaitingCause) {
75 client_manager.send_wait_reason(task_id, cause);
76 }
77 }
78
79 #[cfg(feature = "oh")]
publish_state_change_event( bundle_name: &str, task_id: u32, state: i32, uid: u64, ) -> Result<(), ()>80 pub(crate) fn publish_state_change_event(
81 bundle_name: &str,
82 task_id: u32,
83 state: i32,
84 uid: u64,
85 ) -> Result<(), ()> {
86 match crate::utils::PublishStateChangeEvent(bundle_name, task_id, state, uid as i32) {
87 true => Ok(()),
88 false => Err(()),
89 }
90 }
91 #[allow(unused)]
92 #[cfg(test)]
93 mod ut_notifier {
94 include!("../../tests/ut/manage/ut_notifier.rs");
95 }
96