extract the calling info construct logic

Signed-off-by: zhangwenzhi <zhangwenzhi3@huawei.com>
This commit is contained in:
zhang-wenzhi821 2024-06-26 17:19:08 +08:00
parent cd29a4ae6b
commit 0b6897a1b3
8 changed files with 39 additions and 55 deletions

View File

@ -24,9 +24,9 @@ use system_ability_fwk::{
};
use ylong_runtime::{builder::RuntimeBuilder, time::sleep};
use asset_common::{CallingInfo, Counter, ProcessInfo};
use asset_common::{CallingInfo, Counter};
use asset_crypto_manager::crypto_manager::CryptoManager;
use asset_definition::{log_throw_error, AssetMap, ErrCode, Result, Tag};
use asset_definition::{log_throw_error, AssetMap, ErrCode, Result};
use asset_ipc::SA_ID;
use asset_log::{loge, logi};
use asset_plugin::asset_plugin::{AssetPlugin, AssetContext};
@ -138,25 +138,13 @@ struct AssetService {
}
macro_rules! execute {
($func:path, $args:expr, $args2:expr) => {{
($func:path, $calling_info:expr, $($args:expr),+) => {{
let func_name = hisysevent::function!();
let specific_user_id = $args.get(&Tag::UserId);
let calling_info = CallingInfo::build(specific_user_id.cloned(), $args2);
let start = Instant::now();
let _trace = TraceScope::trace(func_name);
// Create database directory if not exists.
asset_file_operator::create_user_db_dir(calling_info.user_id())?;
upload_system_event($func($args, &calling_info), &calling_info, start, func_name)
}};
($func:path, $args1:expr, $args2:expr, $args3:expr) => {{
let func_name = hisysevent::function!();
let specific_user_id = $args1.get(&Tag::UserId);
let calling_info = CallingInfo::build(specific_user_id.cloned(), $args3);
let start = Instant::now();
let _trace = TraceScope::trace(func_name);
// Create database directory if not exists.
asset_file_operator::create_user_db_dir(calling_info.user_id())?;
upload_system_event($func($args1, $args2, &calling_info), &calling_info, start, func_name)
asset_file_operator::create_user_db_dir($calling_info.user_id())?;
upload_system_event($func($calling_info, $($args),+), $calling_info, start, func_name)
}};
}
@ -165,27 +153,27 @@ impl AssetService {
Self { system_ability: handler }
}
fn add(&self, attributes: &AssetMap, process_info: &ProcessInfo) -> Result<()> {
execute!(operations::add, attributes, process_info)
fn add(&self, calling_info: &CallingInfo, attributes: &AssetMap) -> Result<()> {
execute!(operations::add, calling_info, attributes)
}
fn remove(&self, query: &AssetMap, process_info: &ProcessInfo) -> Result<()> {
execute!(operations::remove, query, process_info)
fn remove(&self, calling_info: &CallingInfo, query: &AssetMap) -> Result<()> {
execute!(operations::remove, calling_info, query)
}
fn update(&self, query: &AssetMap, attributes_to_update: &AssetMap, process_info: &ProcessInfo) -> Result<()> {
execute!(operations::update, query, attributes_to_update, process_info)
fn update(&self, calling_info: &CallingInfo, query: &AssetMap, attributes_to_update: &AssetMap) -> Result<()> {
execute!(operations::update, calling_info, query, attributes_to_update)
}
fn pre_query(&self, query: &AssetMap, process_info: &ProcessInfo) -> Result<Vec<u8>> {
execute!(operations::pre_query, query, process_info)
fn pre_query(&self, calling_info: &CallingInfo, query: &AssetMap) -> Result<Vec<u8>> {
execute!(operations::pre_query, calling_info, query)
}
fn query(&self, query: &AssetMap, process_info: &ProcessInfo) -> Result<Vec<AssetMap>> {
execute!(operations::query, query, process_info)
fn query(&self, calling_info: &CallingInfo, query: &AssetMap) -> Result<Vec<AssetMap>> {
execute!(operations::query, calling_info, query)
}
fn post_query(&self, query: &AssetMap, process_info: &ProcessInfo) -> Result<()> {
execute!(operations::post_query, query, process_info)
fn post_query(&self, calling_info: &CallingInfo, query: &AssetMap) -> Result<()> {
execute!(operations::post_query, calling_info, query)
}
}

View File

@ -185,7 +185,7 @@ fn local_add(attributes: &AssetMap, calling_info: &CallingInfo) -> Result<()> {
}
}
pub(crate) fn add(attributes: &AssetMap, calling_info: &CallingInfo) -> Result<()> {
pub(crate) fn add(calling_info: &CallingInfo, attributes: &AssetMap) -> Result<()> {
let local_res = local_add(attributes, calling_info);
common::inform_asset_ext(calling_info, attributes);

View File

@ -33,7 +33,7 @@ fn check_arguments(query: &AssetMap) -> Result<()> {
common::check_value_validity(query)
}
pub(crate) fn post_query(handle: &AssetMap, calling_info: &CallingInfo) -> Result<()> {
pub(crate) fn post_query(calling_info: &CallingInfo, handle: &AssetMap) -> Result<()> {
check_arguments(handle)?;
let challenge = handle.get_bytes_attr(&Tag::AuthChallenge)?;

View File

@ -68,7 +68,7 @@ fn query_key_attrs(calling_info: &CallingInfo, db_data: &DbMap) -> Result<(Acces
}
}
pub(crate) fn pre_query(query: &AssetMap, calling_info: &CallingInfo) -> Result<Vec<u8>> {
pub(crate) fn pre_query(calling_info: &CallingInfo, query: &AssetMap) -> Result<Vec<u8>> {
check_arguments(query)?;
let mut db_data = common::into_db_map(query);

View File

@ -175,7 +175,7 @@ fn check_arguments(attributes: &AssetMap) -> Result<()> {
common::check_system_permission(attributes)
}
pub(crate) fn query(query: &AssetMap, calling_info: &CallingInfo) -> Result<Vec<AssetMap>> {
pub(crate) fn query(calling_info: &CallingInfo, query: &AssetMap) -> Result<Vec<AssetMap>> {
check_arguments(query)?;
common::inform_asset_ext(calling_info, query);

View File

@ -47,7 +47,7 @@ fn check_arguments(attributes: &AssetMap) -> Result<()> {
common::check_system_permission(attributes)
}
pub(crate) fn remove(query: &AssetMap, calling_info: &CallingInfo) -> Result<()> {
pub(crate) fn remove(calling_info: &CallingInfo, query: &AssetMap) -> Result<()> {
check_arguments(query)?;
let mut db_data = common::into_db_map(query);

View File

@ -82,7 +82,7 @@ fn upgrade_to_latest_version(origin_db_data: &mut DbMap, update_db_data: &mut Db
update_db_data.insert_attr(column::VERSION, DB_DATA_VERSION);
}
pub(crate) fn update(query: &AssetMap, update: &AssetMap, calling_info: &CallingInfo) -> Result<()> {
pub(crate) fn update(calling_info: &CallingInfo, query: &AssetMap, update: &AssetMap) -> Result<()> {
check_arguments(query, update)?;
let mut query_db_data = common::into_db_map(query);

View File

@ -15,15 +15,15 @@
//! This module implements the stub of the Asset service.
use asset_common::{AutoCounter, OwnerType, ProcessInfo, ProcessInfoInner};
use asset_common::{AutoCounter, CallingInfo, OwnerType, ProcessInfo, ProcessInfoInner};
use ipc::{parcel::MsgParcel, remote::RemoteStub, IpcResult, IpcStatusCode};
use asset_definition::{AssetError, Result};
use asset_ipc::{deserialize_map, serialize_maps, IpcCode, IPC_SUCCESS, SA_NAME};
use asset_log::{loge, logi};
use asset_plugin::asset_plugin::AssetPlugin;
use asset_sdk::{log_throw_error, plugin_interface::{EventType, ExtDbMap, PARAM_NAME_APP_INDEX, PARAM_NAME_BUNDLE_NAME, PARAM_NAME_IS_HAP,
PARAM_NAME_USER_ID}, AssetMap, ErrCode, Value};
use asset_sdk::{log_throw_error, plugin_interface::{EventType, ExtDbMap, PARAM_NAME_APP_INDEX, PARAM_NAME_BUNDLE_NAME,
PARAM_NAME_IS_HAP, PARAM_NAME_USER_ID}, ErrCode, Tag, Value};
use crate::{unload_handler::DELAYED_UNLOAD_TIME_IN_SEC, unload_sa, AssetService};
@ -56,26 +56,21 @@ impl RemoteStub for AssetService {
}
}
fn on_app_request(code: &IpcCode, param_map: &AssetMap, process_info: &ProcessInfo) -> Result<()> {
fn on_app_request(process_info: &ProcessInfo, calling_info: &CallingInfo)
-> Result<()> {
let app_index = match &process_info.process_info_inner {
ProcessInfoInner::Hap(hap_info) => hap_info.app_index,
ProcessInfoInner::Native(_) => 0
};
let mut params = ExtDbMap::new();
params.insert(PARAM_NAME_USER_ID, Value::Number(process_info.user_id as u32));
// to get the real user id to operate Asset
params.insert(PARAM_NAME_USER_ID, Value::Number(calling_info.user_id() as u32));
params.insert(PARAM_NAME_BUNDLE_NAME, Value::Bytes(process_info.process_name.clone()));
params.insert(PARAM_NAME_IS_HAP, Value::Bool(process_info.owner_type == OwnerType::Hap));
params.insert(PARAM_NAME_APP_INDEX, Value::Number(app_index as u32));
if let Ok(load) = AssetPlugin::get_instance().load_plugin() {
if code == &IpcCode::Remove && param_map.is_empty() {
match load.process_event(EventType::OnPackageClear, &params) {
Ok(()) => return Ok(()),
Err(code) => return log_throw_error!(ErrCode::BmsError,
"[FATAL]process on package clear data failed, code: {}", code)
}
}
match load.process_event(EventType::OnAppCall, &params) {
Ok(()) => return Ok(()),
Err(code) => return log_throw_error!(ErrCode::BmsError,
@ -98,30 +93,31 @@ fn on_remote_request(stub: &AssetService, code: u32, data: &mut MsgParcel, reply
let map = deserialize_map(data).map_err(asset_err_handle)?;
let process_info = ProcessInfo::build().map_err(asset_err_handle)?;
on_app_request(&ipc_code, &map, &process_info).map_err(asset_err_handle)?;
let calling_info = CallingInfo::build(map.get(&Tag::UserId).cloned(), &process_info);
on_app_request(&process_info, &calling_info).map_err(asset_err_handle)?;
match ipc_code {
IpcCode::Add => reply_handle(stub.add(&map, &process_info), reply),
IpcCode::Remove => reply_handle(stub.remove(&map, &process_info), reply),
IpcCode::Add => reply_handle(stub.add(&calling_info, &map), reply),
IpcCode::Remove => reply_handle(stub.remove(&calling_info, &map), reply),
IpcCode::Update => {
let update_map = deserialize_map(data).map_err(asset_err_handle)?;
reply_handle(stub.update(&map, &update_map, &process_info), reply)
reply_handle(stub.update(&calling_info, &map, &update_map), reply)
},
IpcCode::PreQuery => match stub.pre_query(&map, &process_info) {
IpcCode::PreQuery => match stub.pre_query(&calling_info, &map) {
Ok(res) => {
reply_handle(Ok(()), reply)?;
reply.write::<Vec<u8>>(&res)
},
Err(e) => reply_handle(Err(e), reply),
},
IpcCode::Query => match stub.query(&map, &process_info) {
IpcCode::Query => match stub.query(&calling_info, &map) {
Ok(res) => {
reply_handle(Ok(()), reply)?;
serialize_maps(&res, reply).map_err(asset_err_handle)
},
Err(e) => reply_handle(Err(e), reply),
},
IpcCode::PostQuery => reply_handle(stub.post_query(&map, &process_info), reply),
IpcCode::PostQuery => reply_handle(stub.post_query(&calling_info, &map), reply),
}
}