!1091 多前台适配

Merge pull request !1091 from fqwert/predownload
This commit is contained in:
openharmony_ci 2024-11-22 09:18:04 +00:00 committed by Gitee
commit a53d7eff02
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
9 changed files with 72 additions and 46 deletions

View File

@ -20,7 +20,7 @@
namespace OHOS::Request {
int GetTopUid(int &uid);
int GetForegroundAbilities(rust::vec<int> &uid);
rust::string GetCallingBundle(rust::u64 tokenId);
bool IsSystemAPI(uint64_t tokenId);
bool CheckPermission(uint64_t tokenId, rust::str permission);

View File

@ -21,32 +21,45 @@
#include "access_token.h"
#include "accesstoken_kit.h"
#include "app_mgr_client.h"
#include "app_mgr_proxy.h"
#include "common_event_data.h"
#include "common_event_manager.h"
#include "common_event_publish_info.h"
#include "cxx.h"
#include "int_wrapper.h"
#include "iservice_registry.h"
#include "log.h"
#include "string_wrapper.h"
#include "system_ability_definition.h"
#include "tokenid_kit.h"
#include "utils/mod.rs.h"
namespace OHOS::Request {
using namespace OHOS::Security::AccessToken;
using namespace OHOS::EventFwk;
using namespace OHOS::AppExecFwk;
int GetTopUid(int &uid)
int GetForegroundAbilities(rust::vec<int> &uid)
{
sptr<IRemoteObject> token;
auto ret = OHOS::AAFwk::AbilityManagerClient::GetInstance()->GetTopAbility(token);
auto abilities = std::vector<AppExecFwk::AppStateData>();
auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (sysm == nullptr) {
REQUEST_HILOGE("GetForegroundAbilities failed, sysm is nullptr");
}
auto remote = sysm->CheckSystemAbility(APP_MGR_SERVICE_ID);
if (remote == nullptr) {
REQUEST_HILOGE("GetForegroundAbilities failed, remote is nullptr");
}
auto proxy = AppMgrProxy(remote);
auto ret = proxy.GetForegroundApplications(abilities);
if (ret != 0) {
REQUEST_HILOGE("GetTopUid failed, ret: %{public}d", ret);
REQUEST_HILOGE("GetForegroundAbilities, ret: %{public}d", ret);
return ret;
}
auto info = OHOS::AppExecFwk::RunningProcessInfo();
AppExecFwk::AppMgrClient().GetRunningProcessInfoByToken(token, info);
uid = info.uid_;
for (auto ability : abilities) {
uid.push_back(ability.uid);
}
return 0;
}

View File

@ -411,7 +411,7 @@ impl Scheduler {
return Ok(false);
}
if !config.satisfy_foreground(self.state_handler.top_uid()) {
if !config.satisfy_foreground(self.state_handler.foreground_abilities()) {
info!(
"task {} started, waiting for app {}",
task_id, config.common_data.uid

View File

@ -30,11 +30,15 @@ impl SortedApps {
}
}
pub(crate) fn sort(&mut self, top_uid: Option<u64>, top_user: u64) {
pub(crate) fn sort(&mut self, foreground_abilities: &HashSet<u64>, top_user: u64) {
self.inner.sort_by(|a, b| {
(a.uid / 200000 == top_user)
.cmp(&(b.uid / 200000 == top_user))
.then((Some(a.uid) == top_uid).cmp(&(Some(b.uid) == top_uid)))
.then(
foreground_abilities
.contains(&a.uid)
.cmp(&(foreground_abilities.contains(&b.uid))),
)
})
}

View File

@ -58,7 +58,8 @@ impl Qos {
impl Qos {
// Reschedule qos queue and get directions.
pub(crate) fn reschedule(&mut self, state: &state::Handler) -> QosChanges {
self.apps.sort(state.top_uid(), state.top_user());
self.apps
.sort(state.foreground_abilities(), state.top_user());
let mut changes = QosChanges::new();
changes.download = Some(self.reschedule_inner(Action::Download));
changes.upload = Some(self.reschedule_inner(Action::Upload));

View File

@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::time::Duration;
use sql::SqlList;
@ -23,7 +23,7 @@ use crate::manage::network_manager::NetworkManager;
use crate::manage::task_manager::TaskManagerTx;
#[cfg(feature = "oh")]
#[cfg(not(test))]
use crate::utils::GetTopUid;
use crate::utils::GetForegroundAbilities;
mod recorder;
pub(crate) mod sql;
@ -48,26 +48,35 @@ impl Handler {
let (foreground_account, active_accounts) = NetworkManager::query_active_accounts();
#[allow(unused_mut)]
let mut top_uid = 0;
let mut foreground_abilities = vec![];
#[cfg(not(test))]
#[cfg(feature = "oh")]
{
for _ in 0..10 {
let res = GetTopUid(&mut top_uid);
if res != 0 || top_uid == 0 {
error!("Get top uid failed, res: {}", top_uid);
let res = GetForegroundAbilities(&mut foreground_abilities);
if res != 0 {
error!("Get top uid failed, res: {}", res);
std::thread::sleep(Duration::from_millis(500));
}
}
}
let top_uid = if top_uid == 0 {
let foreground_abilities = if foreground_abilities.is_empty() {
None
} else {
Some(top_uid as u64)
Some(
foreground_abilities
.into_iter()
.map(|a: i32| a as u64)
.collect(),
)
};
self.recorder
.init(network_info, top_uid, foreground_account, active_accounts)
self.recorder.init(
network_info,
foreground_abilities,
foreground_account,
active_accounts,
)
}
pub(crate) fn update_rss_level(&mut self, level: i32) -> Option<RssCapacity> {
@ -86,12 +95,9 @@ impl Handler {
}
pub(crate) fn update_top_uid(&mut self, top_uid: u64) -> Option<SqlList> {
if self.top_uid() == Some(top_uid) {
if self.foreground_abilities().contains(&top_uid) {
return None;
}
if let Some(uid) = self.top_uid() {
self.update_background(uid);
}
if let Some(handle) = self.background_timeout.remove(&top_uid) {
handle.cancel();
}
@ -99,7 +105,7 @@ impl Handler {
}
pub(crate) fn update_background(&mut self, uid: u64) -> Option<SqlList> {
if Some(uid) != self.top_uid() {
if !self.foreground_abilities().contains(&uid) {
return None;
}
let task_manager = self.task_manager.clone();
@ -110,7 +116,7 @@ impl Handler {
task_manager.trigger_background_timeout(uid);
}),
);
self.recorder.update_background();
self.recorder.update_background(uid);
None
}
@ -131,8 +137,8 @@ impl Handler {
Some(sql_list)
}
pub(crate) fn top_uid(&self) -> Option<u64> {
self.recorder.top_uid
pub(crate) fn foreground_abilities(&self) -> &HashSet<u64> {
&self.recorder.foreground_abilities
}
pub(crate) fn top_user(&self) -> u64 {

View File

@ -17,7 +17,7 @@ use crate::manage::network::NetworkState;
use crate::manage::scheduler::qos::RssCapacity;
pub(super) struct StateRecord {
pub(super) top_uid: Option<u64>,
pub(super) foreground_abilities: HashSet<u64>,
pub(super) top_user: u64,
pub(super) network: NetworkState,
pub(super) active_accounts: HashSet<u64>,
@ -27,7 +27,7 @@ pub(super) struct StateRecord {
impl StateRecord {
pub(crate) fn new() -> Self {
StateRecord {
top_uid: None,
foreground_abilities: HashSet::new(),
top_user: 0,
network: NetworkState::Offline,
active_accounts: HashSet::new(),
@ -38,18 +38,20 @@ impl StateRecord {
pub(super) fn init(
&mut self,
network: NetworkState,
top_uid: Option<u64>,
foreground_abilities: Option<Vec<u64>>,
foreground_account: u64,
active_accounts: HashSet<u64>,
) -> SqlList {
let mut sql_list = SqlList::new();
sql_list.add_network_change(&network);
sql_list.add_account_change(&active_accounts);
if let Some(top_uid) = top_uid {
sql_list.add_app_state_available(top_uid);
if let Some(foreground_abilities) = foreground_abilities {
for foreground_ability in foreground_abilities {
sql_list.add_app_state_available(foreground_ability);
self.foreground_abilities.insert(foreground_ability);
}
}
self.top_user = foreground_account;
self.top_uid = top_uid;
self.active_accounts = active_accounts;
self.network = network;
sql_list
@ -94,19 +96,18 @@ impl StateRecord {
info!("update top uid {}", uid);
let mut sql_list = SqlList::new();
sql_list.add_app_state_available(uid);
self.top_uid = Some(uid);
self.foreground_abilities.insert(uid);
Some(sql_list)
}
pub(crate) fn update_background(&mut self) {
if let Some(uid) = self.top_uid {
pub(crate) fn update_background(&mut self, uid: u64) {
if self.foreground_abilities.remove(&uid) {
info!("{} turn to background", uid);
}
self.top_uid = None;
}
pub(crate) fn update_background_timeout(&self, uid: u64) -> Option<SqlList> {
if self.top_uid == Some(uid) {
if self.foreground_abilities.contains(&uid) {
return None;
}
info!("{} background timeout", uid);

View File

@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::fs::File;
use std::os::fd::FromRawFd;
@ -142,8 +142,9 @@ impl TaskConfig {
}
}
pub(crate) fn satisfy_foreground(&self, top_uid: Option<u64>) -> bool {
self.common_data.mode == Mode::BackGround || Some(self.common_data.uid) == top_uid
pub(crate) fn satisfy_foreground(&self, foreground_abilities: &HashSet<u64>) -> bool {
self.common_data.mode == Mode::BackGround
|| foreground_abilities.contains(&self.common_data.uid)
}
}

View File

@ -27,7 +27,7 @@ pub(crate) use ffi::PublishStateChangeEvent;
cfg_oh! {
pub(crate) mod url_policy;
#[cfg(not(test))]
pub(crate) use ffi::GetTopUid;
pub(crate) use ffi::GetForegroundAbilities;
}
pub(crate) mod task_id_generator;
@ -130,7 +130,7 @@ mod ffi {
include!("request_utils.h");
fn PublishStateChangeEvent(bundleName: &str, taskId: u32, state: i32) -> bool;
fn GetTopUid(uid: &mut i32) -> i32;
fn GetForegroundAbilities(uid: &mut Vec<i32>) -> i32;
fn GetCallingBundle(token_id: u64) -> String;
fn IsSystemAPI(token_id: u64) -> bool;
fn CheckPermission(token_id: u64, permission: &str) -> bool;