1 /*
2  * Copyright (c) 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 defines the interface of the Asset Rust SDK.
17 
18 pub use asset_definition::Value;
19 use ipc::parcel::MsgParcel;
20 use std::any::Any;
21 use std::collections::HashMap;
22 
23 /// Defines a type alias `ExtDbMap` as a `HashMap` with keys of type `&'static str` and values of type `Value`.
24 pub type ExtDbMap = HashMap<&'static str, Value>;
25 
26 /// An enumeration representing different event types related to specific operations.
27 #[derive(Default, Hash, PartialEq, Eq, Clone)]
28 pub enum EventType {
29     /// Sync operate.
30     #[default]
31     Sync = 0,
32 
33     /// Clean cloud flag.
34     CleanCloudFlag = 1,
35 
36     /// Delete cloud data.
37     DeleteCloudData,
38 
39     /// Device upgrade event.
40     OnDeviceUpgrade,
41 
42     /// App upgrade event.
43     OnAppRestore,
44 
45     /// User unlock envent.
46     OnUserUnlocked,
47 
48     /// App call event.
49     OnAppCall,
50 
51     /// Package clear event.
52     OnPackageClear,
53 
54     /// User removed.
55     OnUserRemoved,
56 }
57 
58 /// param name for bundle name
59 pub const PARAM_NAME_BUNDLE_NAME: &str = "BundleName";
60 
61 /// param name for user id
62 pub const PARAM_NAME_USER_ID: &str = "UserId";
63 
64 /// param name for app index
65 pub const PARAM_NAME_APP_INDEX: &str = "AppIndex";
66 
67 /// param name for owner type
68 pub const PARAM_NAME_IS_HAP: &str = "IsHap";
69 
70 /// An enumeration representing different plugin types.
71 #[derive(Default, Hash, PartialEq, Eq, Clone)]
72 pub enum PluginType {
73     /// Default plugin.
74     #[default]
75     Asset = 0,
76 }
77 
78 /// Defines an interface for an asset plugin context, which outlines the basic methods for
79 /// an asset plugin to operate on an asset database.
80 pub trait IAssetPluginCtx: Any + Sync + Send + std::panic::RefUnwindSafe {
81     /// Initializes the plugin before usage.
init(&mut self, user_id: i32) -> Result<(), u32>82     fn init(&mut self, user_id: i32) -> Result<(), u32>;
83 
84     /// Adds an asset to de db.
add(&mut self, attributes: &ExtDbMap) -> Result<i32, u32>85     fn add(&mut self, attributes: &ExtDbMap) -> Result<i32, u32>;
86 
87     /// Adds an asset to ce cb.
ce_add(&mut self, attributes: &ExtDbMap) -> Result<i32, u32>88     fn ce_add(&mut self, attributes: &ExtDbMap) -> Result<i32, u32>;
89 
90     /// Adds an asset with replace to de db.
replace(&mut self, condition: &ExtDbMap, attributes: &ExtDbMap) -> std::result::Result<(), u32>91     fn replace(&mut self, condition: &ExtDbMap, attributes: &ExtDbMap) -> std::result::Result<(), u32>;
92 
93     /// Adds an asset with replace to ce db.
ce_replace(&mut self, condition: &ExtDbMap, attributes: &ExtDbMap) -> std::result::Result<(), u32>94     fn ce_replace(&mut self, condition: &ExtDbMap, attributes: &ExtDbMap) -> std::result::Result<(), u32>;
95 
96     /// Queries de db.
query(&mut self, attributes: &ExtDbMap) -> Result<Vec<ExtDbMap>, u32>97     fn query(&mut self, attributes: &ExtDbMap) -> Result<Vec<ExtDbMap>, u32>;
98 
99     /// Queries ce db.
ce_query(&mut self, attributes: &ExtDbMap) -> Result<Vec<ExtDbMap>, u32>100     fn ce_query(&mut self, attributes: &ExtDbMap) -> Result<Vec<ExtDbMap>, u32>;
101 
102     /// Removes an asset from de db.
remove(&mut self, attributes: &ExtDbMap) -> Result<i32, u32>103     fn remove(&mut self, attributes: &ExtDbMap) -> Result<i32, u32>;
104 
105     /// Removes an asset from ce db.
ce_remove(&mut self, attributes: &ExtDbMap) -> Result<i32, u32>106     fn ce_remove(&mut self, attributes: &ExtDbMap) -> Result<i32, u32>;
107 
108     /// Removes assets from de db with specific condition.
remove_with_specific_cond(&mut self, specific_cond: &str, condition_value: &[Value]) -> Result<i32, u32>109     fn remove_with_specific_cond(&mut self, specific_cond: &str, condition_value: &[Value]) -> Result<i32, u32>;
110 
111     /// Removes assets from ce db with specific condition.
ce_remove_with_specific_cond(&mut self, specific_cond: &str, condition_value: &[Value]) -> Result<i32, u32>112     fn ce_remove_with_specific_cond(&mut self, specific_cond: &str, condition_value: &[Value]) -> Result<i32, u32>;
113 
114     /// Updates the attributes of an asset in de db.
update(&mut self, attributes: &ExtDbMap, attrs_to_update: &ExtDbMap) -> Result<i32, u32>115     fn update(&mut self, attributes: &ExtDbMap, attrs_to_update: &ExtDbMap) -> Result<i32, u32>;
116 
117     /// Updates the attributes of an asset in ce db.
ce_update(&mut self, attributes: &ExtDbMap, attrs_to_update: &ExtDbMap) -> Result<i32, u32>118     fn ce_update(&mut self, attributes: &ExtDbMap, attrs_to_update: &ExtDbMap) -> Result<i32, u32>;
119 
120     /// Returns the storage path for de db.
get_storage_path(&self) -> String121     fn get_storage_path(&self) -> String;
122 
123     /// Increase count
increase_count(&mut self)124     fn increase_count(&mut self);
125 
126     /// Decrease count
decrease_count(&mut self)127     fn decrease_count(&mut self);
128 }
129 
130 /// Defines a trait `IAssetPlugin` that specifies the required functionality for an asset plugin implementation.
131 pub trait IAssetPlugin: Any + Sync + Send + std::panic::RefUnwindSafe {
132     /// Initialize the plugin.
init(&self, ctx: Box<dyn IAssetPluginCtx>) -> Result<(), u32>133     fn init(&self, ctx: Box<dyn IAssetPluginCtx>) -> Result<(), u32>;
134 
135     /// Uninitialize the plugin.
uninit(&self)136     fn uninit(&self);
137 
138     /// Process the event.
process_event(&self, event_type: EventType, params: &ExtDbMap) -> Result<(), u32>139     fn process_event(&self, event_type: EventType, params: &ExtDbMap) -> Result<(), u32>;
140 
141     /// Redirect request.
redirect_request(&self, code: u32, data: &mut MsgParcel, reply: &mut MsgParcel) -> Result<(), i32>142     fn redirect_request(&self, code: u32, data: &mut MsgParcel, reply: &mut MsgParcel) -> Result<(), i32>;
143 }
144