• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 const HAP_OWNER_TYPES: [OwnerType; 2] = [OwnerType::Hap, OwnerType::HapGroup];
38 
39 impl RemoteStub for AssetService {
on_remote_request( &self, code: u32, data: &mut ipc::parcel::MsgParcel, reply: &mut ipc::parcel::MsgParcel, ) -> i3240     fn on_remote_request(
41         &self,
42         code: u32,
43         data: &mut ipc::parcel::MsgParcel,
44         reply: &mut ipc::parcel::MsgParcel,
45     ) -> i32 {
46         let _counter_user = AutoCounter::new();
47         logi!("[INFO]Start cancel idle");
48         self.system_ability.cancel_idle();
49         unload_sa(DELAYED_UNLOAD_TIME_IN_SEC as u64);
50 
51         if code >= REDIRECT_START_CODE {
52             return on_extension_request(self, code, data, reply);
53         }
54 
55         match on_remote_request(self, code, data, reply) {
56             Ok(_) => IPC_SUCCESS as i32,
57             Err(e) => e as i32,
58         }
59     }
60 
descriptor(&self) -> &'static str61     fn descriptor(&self) -> &'static str {
62         SA_NAME
63     }
64 }
65 
on_app_request(process_info: &ProcessInfo, calling_info: &CallingInfo) -> Result<()>66 fn on_app_request(process_info: &ProcessInfo, calling_info: &CallingInfo) -> Result<()> {
67     let app_index = match &process_info.process_info_detail {
68         ProcessInfoDetail::Hap(hap_info) => hap_info.app_index,
69         ProcessInfoDetail::Native(_) => 0,
70     };
71     let mut params = ExtDbMap::new();
72 
73     // to get the real user id to operate Asset
74     params.insert(PARAM_NAME_USER_ID, Value::Number(calling_info.user_id() as u32));
75     params.insert(PARAM_NAME_BUNDLE_NAME, Value::Bytes(process_info.process_name.clone()));
76     params.insert(PARAM_NAME_IS_HAP, Value::Bool(HAP_OWNER_TYPES.contains(&process_info.owner_type)));
77     params.insert(PARAM_NAME_APP_INDEX, Value::Number(app_index as u32));
78 
79     if let Ok(load) = AssetPlugin::get_instance().load_plugin() {
80         match load.process_event(EventType::OnAppCall, &params) {
81             Ok(()) => return Ok(()),
82             Err(code) => {
83                 return log_throw_error!(ErrCode::BmsError, "[FATAL]process on app call event failed, code: {}", code)
84             },
85         }
86     }
87     Ok(())
88 }
89 
on_remote_request(stub: &AssetService, code: u32, data: &mut MsgParcel, reply: &mut MsgParcel) -> IpcResult<()>90 fn on_remote_request(stub: &AssetService, code: u32, data: &mut MsgParcel, reply: &mut MsgParcel) -> IpcResult<()> {
91     match data.read_interface_token() {
92         Ok(interface_token) if interface_token == stub.descriptor() => {},
93         _ => {
94             loge!("[FATAL][SA]Invalid interface token.");
95             return Err(IpcStatusCode::Failed);
96         },
97     }
98     let ipc_code = IpcCode::try_from(code).map_err(asset_err_handle)?;
99 
100     let map = deserialize_map(data).map_err(asset_err_handle)?;
101     let process_info = ProcessInfo::build(map.get(&Tag::GroupId)).map_err(asset_err_handle)?;
102     let calling_info = CallingInfo::build(map.get(&Tag::UserId).cloned(), &process_info);
103     on_app_request(&process_info, &calling_info).map_err(asset_err_handle)?;
104 
105     match ipc_code {
106         IpcCode::Add => reply_handle(stub.add(&calling_info, &map), reply),
107         IpcCode::Remove => reply_handle(stub.remove(&calling_info, &map), reply),
108         IpcCode::Update => {
109             let update_map = deserialize_map(data).map_err(asset_err_handle)?;
110             reply_handle(stub.update(&calling_info, &map, &update_map), reply)
111         },
112         IpcCode::PreQuery => match stub.pre_query(&calling_info, &map) {
113             Ok(res) => {
114                 reply_handle(Ok(()), reply)?;
115                 reply.write::<Vec<u8>>(&res)
116             },
117             Err(e) => reply_handle(Err(e), reply),
118         },
119         IpcCode::Query => match stub.query(&calling_info, &map) {
120             Ok(res) => {
121                 reply_handle(Ok(()), reply)?;
122                 serialize_maps(&res, reply).map_err(asset_err_handle)
123             },
124             Err(e) => reply_handle(Err(e), reply),
125         },
126         IpcCode::PostQuery => reply_handle(stub.post_query(&calling_info, &map), reply),
127     }
128 }
129 
on_extension_request(_stub: &AssetService, code: u32, data: &mut MsgParcel, reply: &mut MsgParcel) -> i32130 fn on_extension_request(_stub: &AssetService, code: u32, data: &mut MsgParcel, reply: &mut MsgParcel) -> i32 {
131     if let Ok(load) = AssetPlugin::get_instance().load_plugin() {
132         match load.redirect_request(code, data, reply) {
133             Ok(()) => {
134                 logi!("process redirect request success.");
135                 return IPC_SUCCESS as i32;
136             },
137             Err(code) => {
138                 loge!("process redirect request failed, code: {}", code);
139                 return code as i32;
140             },
141         }
142     }
143     IpcStatusCode::Failed as i32
144 }
145 
asset_err_handle(e: AssetError) -> IpcStatusCode146 fn asset_err_handle(e: AssetError) -> IpcStatusCode {
147     loge!("[IPC]Asset error code = {}, msg is {}", e.code, e.msg);
148     IpcStatusCode::InvalidValue
149 }
150 
reply_handle(ret: Result<()>, reply: &mut MsgParcel) -> IpcResult<()>151 fn reply_handle(ret: Result<()>, reply: &mut MsgParcel) -> IpcResult<()> {
152     match ret {
153         Ok(_) => reply.write::<u32>(&IPC_SUCCESS),
154         Err(e) => {
155             reply.write::<u32>(&(e.code as u32))?;
156             reply.write::<String>(&e.msg)
157         },
158     }
159 }
160