• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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 defines the interface of the Asset Rust SDK.
17 
18 pub use asset_definition::{AssetError, Value};
19 use ipc::parcel::MsgParcel;
20 use std::any::Any;
21 use std::collections::HashMap;
22 use ylong_runtime::task::JoinHandle;
23 
24 use std::sync::{Arc, Mutex};
25 
26 /// Defines a type alias `ExtDbMap` as a `HashMap` with keys of type `&'static str` and values of type `Value`.
27 pub type ExtDbMap = HashMap<&'static str, Value>;
28 
29 /// An enumeration representing different event types related to specific operations.
30 #[derive(Default, Hash, PartialEq, Eq, Clone)]
31 pub enum EventType {
32     /// Sync operate.
33     #[default]
34     Sync = 0,
35 
36     /// Clean cloud flag.
37     CleanCloudFlag = 1,
38 
39     /// Delete cloud data.
40     DeleteCloudData,
41 
42     /// Device upgrade event.
43     OnDeviceUpgrade,
44 
45     /// App upgrade event.
46     OnAppRestore,
47 
48     /// User unlock envent.
49     OnUserUnlocked,
50 
51     /// App call event.
52     OnAppCall,
53 
54     /// Package clear event.
55     OnPackageClear,
56 
57     /// User removed.
58     OnUserRemoved,
59 
60     /// Query the result of synchronization.
61     QuerySyncResult,
62 
63     /// Wrap data.
64     WrapData,
65 }
66 
67 /// param name for bundle name
68 pub const PARAM_NAME_BUNDLE_NAME: &str = "BundleName";
69 
70 /// param name for user id
71 pub const PARAM_NAME_USER_ID: &str = "UserId";
72 
73 /// param name for app index
74 pub const PARAM_NAME_APP_INDEX: &str = "AppIndex";
75 
76 /// param name for owner type
77 pub const PARAM_NAME_OWNER_TYPE: &str = "OwnerType";
78 
79 /// param name for owner info
80 pub const PARAM_NAME_OWNER_INFO: &str = "OwnerInfo";
81 
82 /// param name for developer id
83 pub const PARAM_NAME_DEVELOPER_ID: &str = "DeveloperId";
84 
85 /// param name for group id
86 pub const PARAM_NAME_GROUP_ID: &str = "GroupId";
87 
88 /// param name for attribute encryption type
89 pub const PARAM_NAME_REQUIRE_ATTR_ENCRYPTED: &str = "RequireAttrEncrypted";
90 
91 /// param name for accessibility
92 pub const PARAM_NAME_ACCESSIBILITY: &str = "Accessibility";
93 
94 /// param name for result code
95 pub const PARAM_NAME_RESULT_CODE: &str = "ResultCode";
96 
97 /// param name for total count
98 pub const PARAM_NAME_TOTAL_COUNT: &str = "TotalCount";
99 
100 /// param name for failed count
101 pub const PARAM_NAME_FAILED_COUNT: &str = "FailedCount";
102 
103 /// param name for hap type
104 pub const PARAM_NAME_IS_HAP: &str = "IsHap";
105 
106 /// param name for decrypt key alias
107 pub const PARAM_NAME_DECRYPT_KEY_ALIAS: &str = "DecryptKeyAlias";
108 
109 /// param name for encrypt key alias
110 pub const PARAM_NAME_ENCRYPT_KEY_ALIAS: &str = "EncryptKeyAlias";
111 
112 /// param name for cipher
113 pub const PARAM_NAME_CIPHER: &str = "Cipher";
114 
115 /// param name for aad
116 pub const PARAM_NAME_AAD: &str = "AAD";
117 
118 /// param name for return offset
119 pub const RETURN_OFFSET: &str = "ReturnOffset";
120 
121 /// param name for return limit
122 pub const RETURN_LIMIT: &str = "ReturnLimit";
123 
124 /// An enumeration representing different plugin types.
125 #[derive(Default, Hash, PartialEq, Eq, Clone)]
126 pub enum PluginType {
127     /// Default plugin.
128     #[default]
129     Asset = 0,
130 }
131 
132 /// Defines an interface for an asset plugin context, which outlines the basic methods for
133 /// an asset plugin to operate on an asset database.
134 pub trait IAssetPluginCtx: Any + Sync + Send + std::panic::RefUnwindSafe {
135     /// Initializes the plugin before usage.
init(&mut self, user_id: i32) -> Result<(), u32>136     fn init(&mut self, user_id: i32) -> Result<(), u32>;
137 
138     /// Create adapt cloud table for certain asset db.
create_adapt_cloud_table_for_specific_db(&self, db_info: &ExtDbMap, is_ce: bool) -> Result<(), u32>139     fn create_adapt_cloud_table_for_specific_db(&self, db_info: &ExtDbMap, is_ce: bool) -> Result<(), u32>;
140 
141     /// Adds an asset to de db.
add(&self, attributes: &ExtDbMap) -> Result<i32, u32>142     fn add(&self, attributes: &ExtDbMap) -> Result<i32, u32>;
143 
144     /// Adds an asset to ce cb.
ce_add(&self, attributes: &ExtDbMap) -> Result<i32, u32>145     fn ce_add(&self, attributes: &ExtDbMap) -> Result<i32, u32>;
146 
147     /// Adds an asset to db in asset and adapt table.
add_cloud_adapt_data( &self, attributes: &ExtDbMap, adapt_attributes: &ExtDbMap, is_ce: bool, need_lock: bool, ) -> Result<i32, u32>148     fn add_cloud_adapt_data(
149         &self,
150         attributes: &ExtDbMap,
151         adapt_attributes: &ExtDbMap,
152         is_ce: bool,
153         need_lock: bool,
154     ) -> Result<i32, u32>;
155 
156     /// Adds an asset with replace to de db.
replace(&self, condition: &ExtDbMap, attributes: &ExtDbMap) -> std::result::Result<(), u32>157     fn replace(&self, condition: &ExtDbMap, attributes: &ExtDbMap) -> std::result::Result<(), u32>;
158 
159     /// Adds an asset with replace to ce db.
ce_replace(&self, condition: &ExtDbMap, attributes: &ExtDbMap) -> std::result::Result<(), u32>160     fn ce_replace(&self, condition: &ExtDbMap, attributes: &ExtDbMap) -> std::result::Result<(), u32>;
161 
162     /// Queries de db.
query(&self, attributes: &ExtDbMap) -> Result<Vec<ExtDbMap>, u32>163     fn query(&self, attributes: &ExtDbMap) -> Result<Vec<ExtDbMap>, u32>;
164 
165     /// Queries ce db.
ce_query(&self, attributes: &ExtDbMap) -> Result<Vec<ExtDbMap>, u32>166     fn ce_query(&self, attributes: &ExtDbMap) -> Result<Vec<ExtDbMap>, u32>;
167 
168     /// Query target data.
query_target_data( &self, db_name: &str, columns: &[&'static str], sql_where: &str, limit: u32, offset: u32, is_ce: bool, ) -> Result<Vec<ExtDbMap>, u32>169     fn query_target_data(
170         &self,
171         db_name: &str,
172         columns: &[&'static str],
173         sql_where: &str,
174         limit: u32,
175         offset: u32,
176         is_ce: bool,
177     ) -> Result<Vec<ExtDbMap>, u32>;
178 
179     /// Query db with attributes to a certain db. Normal, Group, CE.
query_certain_db( &self, db_info: &ExtDbMap, attributes: &ExtDbMap, query_options: &ExtDbMap, is_ce: bool, is_filter_sync: bool, ) -> Result<Vec<ExtDbMap>, u32>180     fn query_certain_db(
181         &self,
182         db_info: &ExtDbMap,
183         attributes: &ExtDbMap,
184         query_options: &ExtDbMap,
185         is_ce: bool,
186         is_filter_sync: bool,
187     ) -> Result<Vec<ExtDbMap>, u32>;
188 
189     /// Query db with attributes to a certain db. Normal, CE.
query_certain_db_with_connect_table( &self, db_info: &ExtDbMap, attributes: &ExtDbMap, is_ce: bool, need_lock: bool, ) -> Result<Vec<ExtDbMap>, u32>190     fn query_certain_db_with_connect_table(
191         &self,
192         db_info: &ExtDbMap,
193         attributes: &ExtDbMap,
194         is_ce: bool,
195         need_lock: bool,
196     ) -> Result<Vec<ExtDbMap>, u32>;
197 
198     /// Removes an asset from de db.
remove(&self, attributes: &ExtDbMap) -> Result<i32, u32>199     fn remove(&self, attributes: &ExtDbMap) -> Result<i32, u32>;
200 
201     /// Removes an asset from ce db.
ce_remove(&self, attributes: &ExtDbMap) -> Result<i32, u32>202     fn ce_remove(&self, attributes: &ExtDbMap) -> Result<i32, u32>;
203 
204     /// Removes an asset from a certain db. Normal, Group, CE.
remove_certain_db(&self, db_info: &ExtDbMap, attributes: &ExtDbMap, is_ce: bool) -> Result<i32, u32>205     fn remove_certain_db(&self, db_info: &ExtDbMap, attributes: &ExtDbMap, is_ce: bool) -> Result<i32, u32>;
206 
207     /// Removes assets from de db with specific condition.
remove_with_specific_cond(&self, specific_cond: &str, condition_value: &[Value]) -> Result<i32, u32>208     fn remove_with_specific_cond(&self, specific_cond: &str, condition_value: &[Value]) -> Result<i32, u32>;
209 
210     /// Removes assets from ce db with specific condition.
ce_remove_with_specific_cond(&self, specific_cond: &str, condition_value: &[Value]) -> Result<i32, u32>211     fn ce_remove_with_specific_cond(&self, specific_cond: &str, condition_value: &[Value]) -> Result<i32, u32>;
212 
213     /// Removes assets from de db with aliases
batch_remove( &self, attributes: &ExtDbMap, aliases: &[Vec<u8>], require_attr_encrypted: bool, ) -> Result<(), AssetError>214     fn batch_remove(
215         &self,
216         attributes: &ExtDbMap,
217         aliases: &[Vec<u8>],
218         require_attr_encrypted: bool,
219     ) -> Result<(), AssetError>;
220 
221     /// Remove an asset to db in asset and adapt table.
remove_cloud_adapt_data( &self, db_info: &ExtDbMap, attributes: Option<&ExtDbMap>, adapt_attributes: Option<&ExtDbMap>, is_ce: bool, need_lock: bool, ) -> Result<i32, u32>222     fn remove_cloud_adapt_data(
223         &self,
224         db_info: &ExtDbMap,
225         attributes: Option<&ExtDbMap>,
226         adapt_attributes: Option<&ExtDbMap>,
227         is_ce: bool,
228         need_lock: bool,
229     ) -> Result<i32, u32>;
230 
231     /// Updates the attributes of an asset in de db.
update(&self, attributes: &ExtDbMap, attrs_to_update: &ExtDbMap) -> Result<i32, u32>232     fn update(&self, attributes: &ExtDbMap, attrs_to_update: &ExtDbMap) -> Result<i32, u32>;
233 
234     /// Updates the attributes of an asset in ce db.
ce_update(&self, attributes: &ExtDbMap, attrs_to_update: &ExtDbMap) -> Result<i32, u32>235     fn ce_update(&self, attributes: &ExtDbMap, attrs_to_update: &ExtDbMap) -> Result<i32, u32>;
236 
237     /// Get certain db lock.
get_certain_db_lock( &self, db_info: &ExtDbMap, is_ce: bool, ) -> Result<Arc<Mutex<i32>>, u32>238     fn get_certain_db_lock(
239         &self,
240         db_info: &ExtDbMap,
241         is_ce: bool,
242     ) -> Result<Arc<Mutex<i32>>, u32>;
243 
244     /// Returns the storage path for de db.
get_storage_path(&self) -> String245     fn get_storage_path(&self) -> String;
246 
247     /// Increase count
increase_count(&self)248     fn increase_count(&self);
249 
250     /// Decrease count
decrease_count(&self)251     fn decrease_count(&self);
252 
253     /// Add task
add_task(&self, handle: JoinHandle<()>)254     fn add_task(&self, handle: JoinHandle<()>);
255 }
256 
257 /// Defines a trait `IAssetPlugin` that specifies the required functionality for an asset plugin implementation.
258 pub trait IAssetPlugin: Any + Sync + Send + std::panic::RefUnwindSafe {
259     /// Initialize the plugin.
init(&self, ctx: Box<dyn IAssetPluginCtx>) -> Result<(), u32>260     fn init(&self, ctx: Box<dyn IAssetPluginCtx>) -> Result<(), u32>;
261 
262     /// Uninitialize the plugin.
uninit(&self)263     fn uninit(&self);
264 
265     /// Process the event.
process_event(&self, event_type: EventType, params: &mut ExtDbMap) -> Result<(), u32>266     fn process_event(&self, event_type: EventType, params: &mut ExtDbMap) -> Result<(), u32>;
267 
268     /// Redirect request.
redirect_request(&self, code: u32, data: &mut MsgParcel, reply: &mut MsgParcel) -> Result<(), i32>269     fn redirect_request(&self, code: u32, data: &mut MsgParcel, reply: &mut MsgParcel) -> Result<(), i32>;
270 
271     /// On SA Extension.
on_sa_extension(&self, extension: String, data: &mut MsgParcel, reply: &mut MsgParcel) -> Result<(), i32>272     fn on_sa_extension(&self, extension: String, data: &mut MsgParcel, reply: &mut MsgParcel) -> Result<(), i32>;
273 }
274