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
18pub use asset_definition::Value;
19use ipc::parcel::MsgParcel;
20use std::any::Any;
21use std::collections::HashMap;
22
23/// Defines a type alias `ExtDbMap` as a `HashMap` with keys of type `&'static str` and values of type `Value`.
24pub 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)]
28pub 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
59pub const PARAM_NAME_BUNDLE_NAME: &str = "BundleName";
60
61/// param name for user id
62pub const PARAM_NAME_USER_ID: &str = "UserId";
63
64/// param name for app index
65pub const PARAM_NAME_APP_INDEX: &str = "AppIndex";
66
67/// param name for owner type
68pub const PARAM_NAME_IS_HAP: &str = "IsHap";
69
70/// An enumeration representing different plugin types.
71#[derive(Default, Hash, PartialEq, Eq, Clone)]
72pub 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.
80pub trait IAssetPluginCtx: Any + Sync + Send + std::panic::RefUnwindSafe {
81    /// Initializes the plugin before usage.
82    fn init(&mut self, user_id: i32) -> Result<(), u32>;
83
84    /// Adds an asset to de db.
85    fn add(&mut self, attributes: &ExtDbMap) -> Result<i32, u32>;
86
87    /// Adds an asset to ce cb.
88    fn ce_add(&mut self, attributes: &ExtDbMap) -> Result<i32, u32>;
89
90    /// Adds an asset with replace to de db.
91    fn replace(&mut self, condition: &ExtDbMap, attributes: &ExtDbMap) -> std::result::Result<(), u32>;
92
93    /// Adds an asset with replace to ce db.
94    fn ce_replace(&mut self, condition: &ExtDbMap, attributes: &ExtDbMap) -> std::result::Result<(), u32>;
95
96    /// Queries de db.
97    fn query(&mut self, attributes: &ExtDbMap) -> Result<Vec<ExtDbMap>, u32>;
98
99    /// Queries ce db.
100    fn ce_query(&mut self, attributes: &ExtDbMap) -> Result<Vec<ExtDbMap>, u32>;
101
102    /// Removes an asset from de db.
103    fn remove(&mut self, attributes: &ExtDbMap) -> Result<i32, u32>;
104
105    /// Removes an asset from ce db.
106    fn ce_remove(&mut self, attributes: &ExtDbMap) -> Result<i32, u32>;
107
108    /// Removes assets from de db with specific condition.
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.
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.
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.
118    fn ce_update(&mut self, attributes: &ExtDbMap, attrs_to_update: &ExtDbMap) -> Result<i32, u32>;
119
120    /// Returns the storage path for de db.
121    fn get_storage_path(&self) -> String;
122
123    /// Increase count
124    fn increase_count(&mut self);
125
126    /// Decrease count
127    fn decrease_count(&mut self);
128}
129
130/// Defines a trait `IAssetPlugin` that specifies the required functionality for an asset plugin implementation.
131pub trait IAssetPlugin: Any + Sync + Send + std::panic::RefUnwindSafe {
132    /// Initialize the plugin.
133    fn init(&self, ctx: Box<dyn IAssetPluginCtx>) -> Result<(), u32>;
134
135    /// Uninitialize the plugin.
136    fn uninit(&self);
137
138    /// Process the event.
139    fn process_event(&self, event_type: EventType, params: &ExtDbMap) -> Result<(), u32>;
140
141    /// Redirect request.
142    fn redirect_request(&self, code: u32, data: &mut MsgParcel, reply: &mut MsgParcel) -> Result<(), i32>;
143}
144