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 /// param name for return offset 71 pub const RETURN_OFFSET: &str = "ReturnOffset"; 72 73 /// param name for return limit 74 pub const RETURN_LIMIT: &str = "ReturnLimit"; 75 76 /// An enumeration representing different plugin types. 77 #[derive(Default, Hash, PartialEq, Eq, Clone)] 78 pub enum PluginType { 79 /// Default plugin. 80 #[default] 81 Asset = 0, 82 } 83 84 /// Defines an interface for an asset plugin context, which outlines the basic methods for 85 /// an asset plugin to operate on an asset database. 86 pub trait IAssetPluginCtx: Any + Sync + Send + std::panic::RefUnwindSafe { 87 /// Initializes the plugin before usage. init(&mut self, user_id: i32) -> Result<(), u32>88 fn init(&mut self, user_id: i32) -> Result<(), u32>; 89 90 /// Adds an asset to de db. add(&mut self, attributes: &ExtDbMap) -> Result<i32, u32>91 fn add(&mut self, attributes: &ExtDbMap) -> Result<i32, u32>; 92 93 /// Adds an asset to ce cb. ce_add(&mut self, attributes: &ExtDbMap) -> Result<i32, u32>94 fn ce_add(&mut self, attributes: &ExtDbMap) -> Result<i32, u32>; 95 96 /// Adds an asset with replace to de db. replace(&mut self, condition: &ExtDbMap, attributes: &ExtDbMap) -> std::result::Result<(), u32>97 fn replace(&mut self, condition: &ExtDbMap, attributes: &ExtDbMap) -> std::result::Result<(), u32>; 98 99 /// Adds an asset with replace to ce db. ce_replace(&mut self, condition: &ExtDbMap, attributes: &ExtDbMap) -> std::result::Result<(), u32>100 fn ce_replace(&mut self, condition: &ExtDbMap, attributes: &ExtDbMap) -> std::result::Result<(), u32>; 101 102 /// Queries de db. query(&mut self, attributes: &ExtDbMap) -> Result<Vec<ExtDbMap>, u32>103 fn query(&mut self, attributes: &ExtDbMap) -> Result<Vec<ExtDbMap>, u32>; 104 105 /// Queries ce db. ce_query(&mut self, attributes: &ExtDbMap) -> Result<Vec<ExtDbMap>, u32>106 fn ce_query(&mut self, attributes: &ExtDbMap) -> Result<Vec<ExtDbMap>, u32>; 107 108 /// Query db with attributes to a certain db. Normal, Group, CE. query_certain_db( &mut self, db_info: &ExtDbMap, attributes: &ExtDbMap, query_options: &ExtDbMap, is_ce: bool ) -> Result<Vec<ExtDbMap>, u32>109 fn query_certain_db( 110 &mut self, db_info: &ExtDbMap, attributes: &ExtDbMap, query_options: &ExtDbMap, is_ce: bool 111 ) -> Result<Vec<ExtDbMap>, u32>; 112 113 /// Removes an asset from de db. remove(&mut self, attributes: &ExtDbMap) -> Result<i32, u32>114 fn remove(&mut self, attributes: &ExtDbMap) -> Result<i32, u32>; 115 116 /// Removes an asset from ce db. ce_remove(&mut self, attributes: &ExtDbMap) -> Result<i32, u32>117 fn ce_remove(&mut self, attributes: &ExtDbMap) -> Result<i32, u32>; 118 119 /// Removes an asset from a certain db. Normal, Group, CE. remove_certain_db(&mut self, db_info: &ExtDbMap, attributes: &ExtDbMap, is_ce: bool) -> Result<i32, u32>120 fn remove_certain_db(&mut self, db_info: &ExtDbMap, attributes: &ExtDbMap, is_ce: bool) -> Result<i32, u32>; 121 122 /// Removes assets from de db with specific condition. remove_with_specific_cond(&mut self, specific_cond: &str, condition_value: &[Value]) -> Result<i32, u32>123 fn remove_with_specific_cond(&mut self, specific_cond: &str, condition_value: &[Value]) -> Result<i32, u32>; 124 125 /// Removes assets from ce db with specific condition. ce_remove_with_specific_cond(&mut self, specific_cond: &str, condition_value: &[Value]) -> Result<i32, u32>126 fn ce_remove_with_specific_cond(&mut self, specific_cond: &str, condition_value: &[Value]) -> Result<i32, u32>; 127 128 /// Updates the attributes of an asset in de db. update(&mut self, attributes: &ExtDbMap, attrs_to_update: &ExtDbMap) -> Result<i32, u32>129 fn update(&mut self, attributes: &ExtDbMap, attrs_to_update: &ExtDbMap) -> Result<i32, u32>; 130 131 /// Updates the attributes of an asset in ce db. ce_update(&mut self, attributes: &ExtDbMap, attrs_to_update: &ExtDbMap) -> Result<i32, u32>132 fn ce_update(&mut self, attributes: &ExtDbMap, attrs_to_update: &ExtDbMap) -> Result<i32, u32>; 133 134 /// Returns the storage path for de db. get_storage_path(&self) -> String135 fn get_storage_path(&self) -> String; 136 137 /// Increase count increase_count(&mut self)138 fn increase_count(&mut self); 139 140 /// Decrease count decrease_count(&mut self)141 fn decrease_count(&mut self); 142 } 143 144 /// Defines a trait `IAssetPlugin` that specifies the required functionality for an asset plugin implementation. 145 pub trait IAssetPlugin: Any + Sync + Send + std::panic::RefUnwindSafe { 146 /// Initialize the plugin. init(&self, ctx: Box<dyn IAssetPluginCtx>) -> Result<(), u32>147 fn init(&self, ctx: Box<dyn IAssetPluginCtx>) -> Result<(), u32>; 148 149 /// Uninitialize the plugin. uninit(&self)150 fn uninit(&self); 151 152 /// Process the event. process_event(&self, event_type: EventType, params: &ExtDbMap) -> Result<(), u32>153 fn process_event(&self, event_type: EventType, params: &ExtDbMap) -> Result<(), u32>; 154 155 /// Redirect request. redirect_request(&self, code: u32, data: &mut MsgParcel, reply: &mut MsgParcel) -> Result<(), i32>156 fn redirect_request(&self, code: u32, data: &mut MsgParcel, reply: &mut MsgParcel) -> Result<(), i32>; 157 } 158