1 /*
2 * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 //! This module implements the stub of the Asset service.
17
18 use asset_common::{AutoCounter, CallingInfo, OwnerType, ProcessInfo, ProcessInfoDetail};
19 use ipc::{parcel::MsgParcel, remote::RemoteStub, IpcResult, IpcStatusCode};
20
21 use asset_definition::{AssetError, Result};
22 use asset_ipc::{deserialize_map, serialize_maps, IpcCode, IPC_SUCCESS, SA_NAME};
23 use asset_log::{loge, logi};
24 use asset_plugin::asset_plugin::AssetPlugin;
25 use asset_sdk::{
26 log_throw_error,
27 plugin_interface::{
28 EventType, ExtDbMap, PARAM_NAME_APP_INDEX, PARAM_NAME_BUNDLE_NAME, PARAM_NAME_IS_HAP, PARAM_NAME_USER_ID,
29 },
30 ErrCode, Tag, Value,
31 };
32
33 use crate::{unload_handler::DELAYED_UNLOAD_TIME_IN_SEC, unload_sa, AssetService};
34
35 const REDIRECT_START_CODE: u32 = 200;
36
37 impl RemoteStub for AssetService {
on_remote_request( &self, code: u32, data: &mut ipc::parcel::MsgParcel, reply: &mut ipc::parcel::MsgParcel, ) -> i3238 fn on_remote_request(
39 &self,
40 code: u32,
41 data: &mut ipc::parcel::MsgParcel,
42 reply: &mut ipc::parcel::MsgParcel,
43 ) -> i32 {
44 let _counter_user = AutoCounter::new();
45 self.system_ability.cancel_idle();
46 unload_sa(DELAYED_UNLOAD_TIME_IN_SEC as u64);
47
48 if code >= REDIRECT_START_CODE {
49 return on_extension_request(self, code, data, reply);
50 }
51
52 match on_remote_request(self, code, data, reply) {
53 Ok(_) => IPC_SUCCESS as i32,
54 Err(e) => e as i32,
55 }
56 }
57
descriptor(&self) -> &'static str58 fn descriptor(&self) -> &'static str {
59 SA_NAME
60 }
61 }
62
on_app_request(process_info: &ProcessInfo, calling_info: &CallingInfo) -> Result<()>63 fn on_app_request(process_info: &ProcessInfo, calling_info: &CallingInfo) -> Result<()> {
64 let app_index = match &process_info.process_info_detail {
65 ProcessInfoDetail::Hap(hap_info) => hap_info.app_index,
66 ProcessInfoDetail::Native(_) => 0,
67 };
68 let mut params = ExtDbMap::new();
69
70 // to get the real user id to operate Asset
71 params.insert(PARAM_NAME_USER_ID, Value::Number(calling_info.user_id() as u32));
72 params.insert(PARAM_NAME_BUNDLE_NAME, Value::Bytes(process_info.process_name.clone()));
73 params.insert(PARAM_NAME_IS_HAP, Value::Bool(process_info.owner_type == OwnerType::Hap));
74 params.insert(PARAM_NAME_APP_INDEX, Value::Number(app_index as u32));
75
76 if let Ok(load) = AssetPlugin::get_instance().load_plugin() {
77 match load.process_event(EventType::OnAppCall, ¶ms) {
78 Ok(()) => return Ok(()),
79 Err(code) => {
80 return log_throw_error!(ErrCode::BmsError, "[FATAL]process on app call event failed, code: {}", code)
81 },
82 }
83 }
84 Ok(())
85 }
86
on_remote_request(stub: &AssetService, code: u32, data: &mut MsgParcel, reply: &mut MsgParcel) -> IpcResult<()>87 fn on_remote_request(stub: &AssetService, code: u32, data: &mut MsgParcel, reply: &mut MsgParcel) -> IpcResult<()> {
88 match data.read_interface_token() {
89 Ok(interface_token) if interface_token == stub.descriptor() => {},
90 _ => {
91 loge!("[FATAL][SA]Invalid interface token.");
92 return Err(IpcStatusCode::Failed);
93 },
94 }
95 let ipc_code = IpcCode::try_from(code).map_err(asset_err_handle)?;
96
97 let map = deserialize_map(data).map_err(asset_err_handle)?;
98
99 let process_info = ProcessInfo::build().map_err(asset_err_handle)?;
100 let calling_info = CallingInfo::build(map.get(&Tag::UserId).cloned(), &process_info);
101 on_app_request(&process_info, &calling_info).map_err(asset_err_handle)?;
102
103 match ipc_code {
104 IpcCode::Add => reply_handle(stub.add(&calling_info, &map), reply),
105 IpcCode::Remove => reply_handle(stub.remove(&calling_info, &map), reply),
106 IpcCode::Update => {
107 let update_map = deserialize_map(data).map_err(asset_err_handle)?;
108 reply_handle(stub.update(&calling_info, &map, &update_map), reply)
109 },
110 IpcCode::PreQuery => match stub.pre_query(&calling_info, &map) {
111 Ok(res) => {
112 reply_handle(Ok(()), reply)?;
113 reply.write::<Vec<u8>>(&res)
114 },
115 Err(e) => reply_handle(Err(e), reply),
116 },
117 IpcCode::Query => match stub.query(&calling_info, &map) {
118 Ok(res) => {
119 reply_handle(Ok(()), reply)?;
120 serialize_maps(&res, reply).map_err(asset_err_handle)
121 },
122 Err(e) => reply_handle(Err(e), reply),
123 },
124 IpcCode::PostQuery => reply_handle(stub.post_query(&calling_info, &map), reply),
125 }
126 }
127
on_extension_request(_stub: &AssetService, code: u32, data: &mut MsgParcel, reply: &mut MsgParcel) -> i32128 fn on_extension_request(_stub: &AssetService, code: u32, data: &mut MsgParcel, reply: &mut MsgParcel) -> i32 {
129 if let Ok(load) = AssetPlugin::get_instance().load_plugin() {
130 match load.redirect_request(code, data, reply) {
131 Ok(()) => {
132 logi!("process redirect request success.");
133 return IPC_SUCCESS as i32;
134 },
135 Err(code) => {
136 loge!("process redirect request failed, code: {}", code);
137 return code as i32;
138 },
139 }
140 }
141 IpcStatusCode::Failed as i32
142 }
143
asset_err_handle(e: AssetError) -> IpcStatusCode144 fn asset_err_handle(e: AssetError) -> IpcStatusCode {
145 loge!("[IPC]Asset error code = {}, msg is {}", e.code, e.msg);
146 IpcStatusCode::InvalidValue
147 }
148
reply_handle(ret: Result<()>, reply: &mut MsgParcel) -> IpcResult<()>149 fn reply_handle(ret: Result<()>, reply: &mut MsgParcel) -> IpcResult<()> {
150 match ret {
151 Ok(_) => reply.write::<u32>(&IPC_SUCCESS),
152 Err(e) => {
153 reply.write::<u32>(&(e.code as u32))?;
154 reply.write::<String>(&e.msg)
155 },
156 }
157 }
158