!1071 [Bug]: 上传下载适配特殊应用退出时关闭其任务 --5.0.1Release

Merge pull request !1071 from ZhangJianxin/5.0.1R
This commit is contained in:
openharmony_ci 2024-11-15 13:49:09 +00:00 committed by Gitee
commit 0a55f743b4
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
8 changed files with 77 additions and 17 deletions

View File

@ -22,15 +22,17 @@
#include <string>
#include "application_state_observer_stub.h"
#include "c_string_wrapper.h"
namespace OHOS::Request {
class ApplicationStateObserver {
public:
~ApplicationStateObserver();
using RegCallBack = std::function<void(int32_t uid, int32_t state, int32_t pid)>;
using ProcessCallBack = std::function<void(int32_t uid, int32_t state, int32_t pid, CStringWrapper bundleName)>;
static ApplicationStateObserver &GetInstance();
bool RegisterAppStateChanged(RegCallBack &&callback);
void RegisterProcessStateChanged(RegCallBack &&callback);
void RegisterProcessDied(ProcessCallBack &&callback);
public:
class AppProcessState : public AppExecFwk::ApplicationStateObserverStub {
@ -47,12 +49,12 @@ public:
public:
void RunAppStateCallback(int32_t uid, int32_t state, int32_t pid);
void RunProcessStateCallback(int32_t uid, int32_t state, int32_t pid);
void RunProcessDiedCallback(int32_t uid, int32_t state, int32_t pid, const std::string &bundleName);
ApplicationStateObserver &appStateObserver_;
};
ApplicationStateObserver();
RegCallBack appStateCallback_ = nullptr;
RegCallBack processCallback_ = nullptr;
ProcessCallBack processCallback_ = nullptr;
};
} // namespace OHOS::Request
@ -61,8 +63,9 @@ extern "C" {
#endif
typedef void (*APPStateCallback)(int32_t, int32_t, int32_t);
typedef void (*ProcessStateCallback)(int32_t, int32_t, int32_t, CStringWrapper);
void RegisterAPPStateCallback(APPStateCallback fun);
void RegisterProcessStateCallback(APPStateCallback fun);
void RegisterProcessDiedCallback(ProcessStateCallback fun);
#ifdef __cplusplus
}

View File

@ -103,7 +103,7 @@ impl RequestAbility {
AppStateListener::register();
},
|_, _| {
error!("app Manager service died");
error!("app manager service died");
},
);

View File

@ -74,7 +74,7 @@ bool ApplicationStateObserver::RegisterAppStateChanged(RegCallBack &&callback)
return false;
}
void ApplicationStateObserver::RegisterProcessStateChanged(RegCallBack &&callback)
void ApplicationStateObserver::RegisterProcessDied(ProcessCallBack &&callback)
{
processCallback_ = callback;
}
@ -105,7 +105,8 @@ void ApplicationStateObserver::AppProcessState::OnProcessDied(const AppExecFwk::
{
REQUEST_HILOGD("OnProcessDied uid=%{public}d, bundleName=%{public}s, state=%{public}d, pid=%{public}d",
processData.uid, processData.bundleName.c_str(), static_cast<int32_t>(processData.state), processData.pid);
RunProcessStateCallback(processData.uid, static_cast<int32_t>(processData.state), processData.pid);
RunProcessDiedCallback(
processData.uid, static_cast<int32_t>(processData.state), processData.pid, processData.bundleName);
}
void ApplicationStateObserver::AppProcessState::RunAppStateCallback(int32_t uid, int32_t state, int32_t pid)
@ -117,13 +118,15 @@ void ApplicationStateObserver::AppProcessState::RunAppStateCallback(int32_t uid,
appStateObserver_.appStateCallback_(uid, state, pid);
}
void ApplicationStateObserver::AppProcessState::RunProcessStateCallback(int32_t uid, int32_t state, int32_t pid)
void ApplicationStateObserver::AppProcessState::RunProcessDiedCallback(
int32_t uid, int32_t state, int32_t pid, const std::string &bundleName)
{
if (appStateObserver_.processCallback_ == nullptr) {
REQUEST_HILOGE("processStateObserver callback is nullptr");
return;
}
appStateObserver_.processCallback_(uid, state, pid);
CStringWrapper name = WrapperCString(bundleName);
appStateObserver_.processCallback_(uid, state, pid, name);
}
} // namespace OHOS::Request
@ -134,8 +137,8 @@ void RegisterAPPStateCallback(APPStateCallback fun)
REQUEST_HILOGD("running RegisterAPPStateCallback");
}
void RegisterProcessStateCallback(APPStateCallback fun)
void RegisterProcessDiedCallback(ProcessStateCallback fun)
{
ApplicationStateObserver::GetInstance().RegisterProcessStateChanged(fun);
REQUEST_HILOGD("running RegisterProcessStateCallback");
}
ApplicationStateObserver::GetInstance().RegisterProcessDied(fun);
REQUEST_HILOGD("running RegisterProcessDiedCallback");
}

View File

@ -16,6 +16,7 @@ use std::sync::Once;
use super::task_manager::TaskManagerTx;
use crate::service::client::ClientManagerEntry;
use crate::utils::c_wrapper::CStringWrapper;
pub(crate) struct AppStateListener {
client_manager: ClientManagerEntry,
@ -35,7 +36,7 @@ impl AppStateListener {
});
});
RegisterAPPStateCallback(app_state_change_callback);
RegisterProcessStateCallback(process_state_change_callback);
RegisterProcessDiedCallback(process_died_callback);
}
}
@ -43,7 +44,7 @@ impl AppStateListener {
if ONCE.is_completed() {
unsafe {
RegisterAPPStateCallback(app_state_change_callback);
RegisterProcessStateCallback(process_state_change_callback);
RegisterProcessDiedCallback(process_died_callback);
}
}
}
@ -67,11 +68,22 @@ extern "C" fn app_state_change_callback(uid: i32, state: i32, _pid: i32) {
}
}
extern "C" fn process_state_change_callback(uid: i32, state: i32, pid: i32) {
extern "C" fn process_died_callback(uid: i32, state: i32, pid: i32, bundle_name: CStringWrapper) {
debug!(
"Receives process change, uid {} pid {} state {}",
uid, pid, state
);
let name = bundle_name.to_string();
if name.starts_with("com.") && name.ends_with(".hmos.hiviewx") {
unsafe {
APP_STATE_LISTENER
.assume_init_ref()
.task_manager
.notify_special_process_terminate(uid as u64);
}
info!("hiviewx terminate. {:?}, {:?}", uid, pid);
}
if state == 5 {
info!("Receives process died, uid {} pid {}", uid, pid);
unsafe {
@ -86,5 +98,5 @@ extern "C" fn process_state_change_callback(uid: i32, state: i32, pid: i32) {
#[cfg(feature = "oh")]
extern "C" {
fn RegisterAPPStateCallback(f: extern "C" fn(i32, i32, i32));
fn RegisterProcessStateCallback(f: extern "C" fn(i32, i32, i32));
fn RegisterProcessDiedCallback(f: extern "C" fn(i32, i32, i32, CStringWrapper));
}

View File

@ -155,6 +155,7 @@ pub(crate) enum StateEvent {
ForegroundApp(u64),
Background(u64),
BackgroundTimeout(u64),
SpecialTerminate(u64),
}
pub(crate) struct ConstructMessage {

View File

@ -118,6 +118,13 @@ impl Handler {
self.recorder.update_background_timeout(uid)
}
pub(crate) fn special_process_terminate(&mut self, uid: u64) -> Option<SqlList> {
info!("hiviewx terminate handle. {:?}", uid);
let mut sql_list = SqlList::new();
sql_list.add_special_process_terminate(uid);
Some(sql_list)
}
pub(crate) fn top_uid(&self) -> Option<u64> {
self.recorder.top_uid
}

View File

@ -18,9 +18,12 @@ use crate::info::State;
use crate::manage::network::{NetworkInfo, NetworkState, NetworkType};
use crate::task::reason::Reason;
const INITIALIZED: u8 = State::Initialized.repr;
const RUNNING: u8 = State::Running.repr;
const RETRYING: u8 = State::Retrying.repr;
const WAITING: u8 = State::Waiting.repr;
const PAUSED: u8 = State::Paused.repr;
const STOPPED: u8 = State::Stopped.repr;
const FAILED: u8 = State::Failed.repr;
const APP_BACKGROUND_OR_TERMINATE: u8 = Reason::AppBackgroundOrTerminate.repr;
@ -77,6 +80,10 @@ impl SqlList {
pub(crate) fn add_app_state_unavailable(&mut self, uid: u64) {
self.sqls.push(app_state_unavailable(uid));
}
pub(crate) fn add_special_process_terminate(&mut self, uid: u64) {
self.sqls.push(special_process_terminate(uid));
}
}
impl Iterator for SqlList {
@ -278,6 +285,25 @@ pub(super) fn network_available(info: &NetworkInfo) -> String {
sql
}
pub(crate) fn special_process_terminate(uid: u64) -> String {
format!(
"UPDATE request_task
SET
state = {FAILED},
reason = {APP_BACKGROUND_OR_TERMINATE}
WHERE
uid = {uid}
AND (
state = {INITIALIZED}
OR state = {RUNNING}
OR state = {RETRYING}
OR state = {WAITING}
OR state = {PAUSED}
OR state = {STOPPED}
);",
)
}
#[cfg(feature = "oh")]
#[cfg(test)]
mod test {

View File

@ -223,6 +223,10 @@ impl TaskManager {
StateEvent::BackgroundTimeout(uid) => self
.scheduler
.on_state_change(Handler::update_background_timeout, uid),
StateEvent::SpecialTerminate(uid) => {
self.scheduler
.on_state_change(Handler::special_process_terminate, uid);
}
}
}
@ -376,6 +380,10 @@ impl TaskManagerTx {
let _ = self.send_event(TaskManagerEvent::State(StateEvent::BackgroundTimeout(uid)));
}
pub(crate) fn notify_special_process_terminate(&self, uid: u64) {
let _ = self.send_event(TaskManagerEvent::State(StateEvent::SpecialTerminate(uid)));
}
pub(crate) fn show(&self, uid: u64, task_id: u32) -> Option<TaskInfo> {
let (tx, rx) = oneshot::channel();
let event = QueryEvent::Show(task_id, uid, tx);