c0bc2637创建于 15 天前历史提交
/*
 * Copyright (c) 2024-2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//! This module defines the interface of the Asset Rust SDK.

use ipc::parcel::MsgParcel;
use std::any::Any;
use std::collections::HashMap;
use ylong_runtime::task::JoinHandle;

use std::sync::{Arc, Mutex};

use asset_sdk::{AssetError, Value};

/// Defines a type alias `ExtDbMap` as a `HashMap` with keys of type `&'static str` and values of type `Value`.
pub type ExtDbMap = HashMap<&'static str, Value>;

/// An enumeration representing different event types related to specific operations.
#[derive(Default, Hash, PartialEq, Eq, Clone)]
pub enum EventType {
    /// Sync operate.
    #[default]
    Sync = 0,

    /// Clean cloud flag.
    CleanCloudFlag = 1,

    /// Delete cloud data.
    DeleteCloudData,

    /// Device upgrade event.
    OnDeviceUpgrade,

    /// App upgrade event.
    OnAppRestore,

    /// User unlock envent.
    OnUserUnlocked,

    /// App call event.
    OnAppCall,

    /// Package clear event.
    OnPackageClear,

    /// User removed.
    OnUserRemoved,

    /// Query the result of synchronization.
    QuerySyncResult,

    /// Wrap data.
    WrapData,

    /// Check permission.
    IsPermissionEnabled,

    /// Update cloud sync status.
    UpdateCloudSyncStatus,

    /// Report HA event.
    ReportHAEvent,
}

/// param name for bundle name
pub const PARAM_NAME_BUNDLE_NAME: &str = "BundleName";

/// param name for user id
pub const PARAM_NAME_USER_ID: &str = "osAccount";

/// param name for app index
pub const PARAM_NAME_APP_INDEX: &str = "AppIndex";

/// param name for owner type
pub const PARAM_NAME_OWNER_TYPE: &str = "OwnerType";

/// param name for owner info
pub const PARAM_NAME_OWNER_INFO: &str = "OwnerInfo";

/// param name for caller
pub const PARAM_NAME_CALLER: &str = "caller";

/// param name for developer id
pub const PARAM_NAME_DEVELOPER_ID: &str = "DeveloperId";

/// param name for group id
pub const PARAM_NAME_GROUP_ID: &str = "asset_group_id";

/// param name for attribute encryption type
pub const PARAM_NAME_REQUIRE_ATTR_ENCRYPTED: &str = "asset_require_attr_encrypted";

/// param name for accessibility
pub const PARAM_NAME_ACCESSIBILITY: &str = "asset_accessibility";

/// param name for error code
pub const PARAM_NAME_ERROR_CODE: &str = "error_code";

/// param name for result code
pub const PARAM_NAME_RESULT_CODE: &str = "ResultCode";

/// param name for total count
pub const PARAM_NAME_TOTAL_COUNT: &str = "TotalCount";

/// param name for failed count
pub const PARAM_NAME_FAILED_COUNT: &str = "FailedCount";

/// param name for hap type
pub const PARAM_NAME_IS_HAP: &str = "IsHap";

/// param name for decrypt key alias
pub const PARAM_NAME_DECRYPT_KEY_ALIAS: &str = "DecryptKeyAlias";

/// param name for encrypt key alias
pub const PARAM_NAME_ENCRYPT_KEY_ALIAS: &str = "EncryptKeyAlias";

/// param name for cipher
pub const PARAM_NAME_CIPHER: &str = "Cipher";

/// param name for aad
pub const PARAM_NAME_AAD: &str = "AAD";

/// param name for return offset
pub const RETURN_OFFSET: &str = "ReturnOffset";

/// param name for return limit
pub const RETURN_LIMIT: &str = "ReturnLimit";

/// param name for event id
pub const PARAM_NAME_EVENT_ID: &str = "event_id";

/// param name for function name
pub const PARAM_NAME_FUNCTION_NAME: &str = "function_name";

/// param name for begin time
pub const PARAM_NAME_BEGIN_TIME: &str = "begin_time";

/// param name for diff time
pub const PARAM_NAME_DIFF_TIME: &str = "diff_time";

/// param name for extra info
pub const PARAM_NAME_EXTRA_INFO: &str = "extra_info";

/// param name for call stack
pub const PARAM_NAME_CALL_STACK: &str = "asset_call_stack";

/// param name for alias
pub const PARAM_NAME_ALIAS: &str = "asset_alias";

/// param name for sync type
pub const PARAM_NAME_SYNC_TYPE: &str = "asset_sync_type";

/// param name for require password set
pub const PARAM_NAME_REQUIRE_PASSWORD_SET: &str = "asset_require_password_set";

/// param name for auth type
pub const PARAM_NAME_AUTH_TYPE: &str = "asset_auth_type";

/// param name for operation type
pub const PARAM_NAME_OP_TYPE: &str = "asset_operation_type";

/// param name for return type
pub const PARAM_NAME_RETURN_TYPE: &str = "asset_return_type";

/// param name for wrap type
pub const PARAM_NAME_WRAP_TYPE: &str = "asset_wrap_type";

/// param name for is persistent
pub const PARAM_NAME_IS_PERSISTENT: &str = "asset_is_persistent";

/// param name for is conflict resolution
pub const PARAM_NAME_CONFLICT_RESOLUTION: &str = "asset_conflict_resolution";

/// An enumeration representing different plugin types.
#[derive(Default, Hash, PartialEq, Eq, Clone)]
pub enum PluginType {
    /// Default plugin.
    #[default]
    Asset = 0,
}

/// Defines an interface for an asset plugin context, which outlines the basic methods for
/// an asset plugin to operate on an asset database.
pub trait IAssetPluginCtx: Any + Sync + Send + std::panic::RefUnwindSafe {
    /// Initializes the plugin before usage.
    fn init(&mut self, user_id: i32) -> Result<(), u32>;

    /// Create adapt cloud table for certain asset db.
    fn create_adapt_cloud_table_for_specific_db(&self, db_info: &ExtDbMap, is_ce: bool) -> Result<(), u32>;

    /// Adds an asset to de db.
    fn add(&self, attributes: &ExtDbMap) -> Result<i32, u32>;

    /// Adds an asset to ce cb.
    fn ce_add(&self, attributes: &ExtDbMap) -> Result<i32, u32>;

    /// Adds an asset to db in asset and adapt table.
    fn add_cloud_adapt_data(
        &self,
        attributes: &ExtDbMap,
        adapt_attributes: &ExtDbMap,
        is_ce: bool,
        need_lock: bool,
    ) -> Result<i32, u32>;

    /// Adds an asset with replace to de db.
    fn replace(&self, condition: &ExtDbMap, attributes: &ExtDbMap) -> std::result::Result<(), u32>;

    /// Adds an asset with replace to ce db.
    fn ce_replace(&self, condition: &ExtDbMap, attributes: &ExtDbMap) -> std::result::Result<(), u32>;

    /// Queries de db.
    fn query(&self, attributes: &ExtDbMap) -> Result<Vec<ExtDbMap>, u32>;

    /// Queries ce db.
    fn ce_query(&self, attributes: &ExtDbMap) -> Result<Vec<ExtDbMap>, u32>;

    /// Query target data.
    fn query_target_data(
        &self,
        db_name: &str,
        columns: &[&'static str],
        sql_where: &str,
        limit: u32,
        offset: u32,
        is_ce: bool,
    ) -> Result<Vec<ExtDbMap>, u32>;

    /// Query db with attributes to a certain db. Normal, Group, CE.
    fn query_certain_db(
        &self,
        db_info: &ExtDbMap,
        attributes: &ExtDbMap,
        query_options: &ExtDbMap,
        is_ce: bool,
        is_filter_sync: bool,
    ) -> Result<Vec<ExtDbMap>, u32>;

    /// Query db with attributes to a certain db. Normal, CE.
    fn query_certain_db_with_connect_table(
        &self,
        db_info: &ExtDbMap,
        attributes: &ExtDbMap,
        is_ce: bool,
        need_lock: bool,
    ) -> Result<Vec<ExtDbMap>, u32>;

    /// Removes an asset from de db.
    fn remove(&self, attributes: &ExtDbMap) -> Result<i32, u32>;

    /// Removes an asset from ce db.
    fn ce_remove(&self, attributes: &ExtDbMap) -> Result<i32, u32>;

    /// Removes an asset from a certain db. Normal, Group, CE.
    fn remove_certain_db(&self, db_info: &ExtDbMap, attributes: &ExtDbMap, is_ce: bool) -> Result<i32, u32>;

    /// Removes assets from de db with specific condition.
    fn remove_with_specific_cond(&self, specific_cond: &str, condition_value: &[Value]) -> Result<i32, u32>;

    /// Removes assets from ce db with specific condition.
    fn ce_remove_with_specific_cond(&self, specific_cond: &str, condition_value: &[Value]) -> Result<i32, u32>;

    /// Remove an asset to db in asset and adapt table.
    fn remove_cloud_adapt_data(
        &self,
        db_info: &ExtDbMap,
        attributes: Option<&ExtDbMap>,
        adapt_attributes: Option<&ExtDbMap>,
        is_ce: bool,
        need_lock: bool,
    ) -> Result<i32, u32>;

    /// Updates the attributes of an asset in de db.
    fn update(&self, attributes: &ExtDbMap, attrs_to_update: &ExtDbMap) -> Result<i32, u32>;

    /// Updates the attributes of an asset in ce db.
    fn ce_update(&self, attributes: &ExtDbMap, attrs_to_update: &ExtDbMap) -> Result<i32, u32>;

    /// Get certain db lock.
    fn get_certain_db_lock(
        &self,
        db_info: &ExtDbMap,
        is_ce: bool,
    ) -> Result<Arc<Mutex<i32>>, u32>;

    /// Returns the storage path for de db.
    fn get_storage_path(&self) -> String;

    /// Increase count
    fn increase_count(&self);

    /// Decrease count
    fn decrease_count(&self);

    /// Add task
    fn add_task(&self, handle: JoinHandle<()>);

    /// check ce upgrade
    fn is_ce_upgrade(&self, user_id: i32) -> bool;

    /// store key-value into DataShare
    fn store_key_value(&self, user_id: i32, column_key: &str, column_value: i32) -> bool;
    
    /// query value in DataShare
    fn query_value(&self, user_id: i32, column_key: &str) -> Result<i32, AssetError>;

    /// Check whether the device is locked.
    fn is_device_locked(&self) -> Result<bool, u32>;
}

/// Defines a trait `IAssetPlugin` that specifies the required functionality for an asset plugin implementation.
pub trait IAssetPlugin: Any + Sync + Send + std::panic::RefUnwindSafe {
    /// Initialize the plugin.
    fn init(&self, ctx: Box<dyn IAssetPluginCtx>) -> Result<(), u32>;

    /// Uninitialize the plugin.
    fn uninit(&self);

    /// Process the event.
    fn process_event(&self, event_type: EventType, params: &mut ExtDbMap) -> Result<(), u32>;

    /// Redirect request.
    fn redirect_request(&self, code: u32, data: &mut MsgParcel, reply: &mut MsgParcel) -> Result<(), i32>;

    /// On SA Extension.
    fn on_sa_extension(&self, extension: String, data: &mut MsgParcel, reply: &mut MsgParcel) -> Result<(), i32>;
}