• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 std::any::Any;
20 use std::collections::HashMap;
21 use ipc::parcel::MsgParcel;
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 
55 /// param name for bundle name
56 pub const PARAM_NAME_BUNDLE_NAME: &str = "BundleName";
57 
58 /// param name for user id
59 pub const PARAM_NAME_USER_ID: &str = "UserId";
60 
61 /// param name for user id
62 pub const PARAM_NAME_APP_INDEX: &str = "AppIndex";
63 
64 /// param name for whether is hap
65 pub const PARAM_NAME_IS_HAP: &str = "IsHap";
66 
67 /// An enumeration representing different plugin types.
68 #[derive(Default, Hash, PartialEq, Eq, Clone)]
69 pub enum PluginType {
70     /// Default plugin.
71     #[default]
72     Asset = 0,
73 }
74 
75 /// Defines an interface for an asset plugin context, which outlines the basic methods for
76 /// an asset plugin to operate on an asset database.
77 pub trait IAssetPluginCtx: Any + Sync + Send + std::panic::RefUnwindSafe {
78     /// Initializes the plugin before usage.
init(&mut self, user_id: i32) -> Result<(), u32>79     fn init(&mut self, user_id: i32) -> Result<(), u32>;
80 
81     /// Adds an asset to the database.
add(&mut self, attributes: &ExtDbMap) -> Result<i32, u32>82     fn add(&mut self, attributes: &ExtDbMap) -> Result<i32, u32>;
83 
84     /// Add an asset with replace.
replace(&mut self, condition: &ExtDbMap, attributes: &ExtDbMap) -> std::result::Result<(), u32>85     fn replace(&mut self, condition: &ExtDbMap, attributes: &ExtDbMap) -> std::result::Result<(), u32>;
86 
87     /// Queries the asset database.
query(&mut self, attributes: &ExtDbMap) -> Result<Vec<ExtDbMap>, u32>88     fn query(&mut self, attributes: &ExtDbMap) -> Result<Vec<ExtDbMap>, u32>;
89 
90     /// Removes an asset from the database.
remove(&mut self, attributes: &ExtDbMap) -> Result<i32, u32>91     fn remove(&mut self, attributes: &ExtDbMap) -> Result<i32, u32>;
92 
93     /// Removes assets from the database with specific condition.
remove_with_specific_cond(&mut self, specific_cond: &str, condition_value: &[Value]) -> Result<i32, u32>94     fn remove_with_specific_cond(&mut self, specific_cond: &str, condition_value: &[Value]) -> Result<i32, u32>;
95 
96     /// Updates the attributes of an asset in the database.
update(&mut self, attributes: &ExtDbMap, attrs_to_update: &ExtDbMap) -> Result<i32, u32>97     fn update(&mut self, attributes: &ExtDbMap, attrs_to_update: &ExtDbMap) -> Result<i32, u32>;
98 
99     /// Begins a transaction for the asset database.
begin_transaction(&mut self) -> Result<(), u32>100     fn begin_transaction(&mut self) -> Result<(), u32>;
101 
102     /// Commits a transaction for the asset database.
commit_transaction(&mut self) -> Result<(), u32>103     fn commit_transaction(&mut self) -> Result<(), u32>;
104 
105     /// Rolls back a transaction for the asset database.
rollback_transaction(&mut self) -> Result<(), u32>106     fn rollback_transaction(&mut self) -> Result<(), u32>;
107 
108     /// Returns the storage path for the asset database.
get_storage_path(&self) -> String109     fn get_storage_path(&self) -> String;
110 
111     /// Increase count
increase_count(&mut self)112     fn increase_count(&mut self);
113 
114     /// Decrease count
decrease_count(&mut self)115     fn decrease_count(&mut self);
116 }
117 
118 /// Defines a trait `IAssetPlugin` that specifies the required functionality for an asset plugin implementation.
119 pub trait IAssetPlugin: Any + Sync + Send + std::panic::RefUnwindSafe {
120     /// Initialize the plugin.
init(&self, ctx: Box<dyn IAssetPluginCtx>) -> Result<(), u32>121     fn init(&self, ctx: Box<dyn IAssetPluginCtx>) -> Result<(), u32>;
122 
123     /// Uninitialize the plugin.
uninit(&self)124     fn uninit(&self);
125 
126     /// Process the event.
process_event(&self, event_type: EventType, params: &ExtDbMap) -> Result<(), u32>127     fn process_event(&self, event_type: EventType, params: &ExtDbMap) -> Result<(), u32>;
128 
129     /// Redirect request.
redirect_request(&self, code: u32, data: &mut MsgParcel, reply: &mut MsgParcel) -> Result<(), i32>130     fn redirect_request(&self, code: u32, data: &mut MsgParcel, reply: &mut MsgParcel) -> Result<(), i32>;
131 }
132