1 // Copyright 2021, The Android Open Source Project 2 // 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 //! This module implements the IKeystoreMetrics AIDL interface, which exposes the API method for the 16 //! proxy in the system server to pull the aggregated metrics in keystore. 17 use crate::error::map_or_log_err; 18 use crate::metrics_store::METRICS_STORE; 19 use crate::permission::KeystorePerm; 20 use crate::utils::{check_keystore_permission, watchdog as wd}; 21 use android_security_metrics::aidl::android::security::metrics::{ 22 AtomID::AtomID, 23 IKeystoreMetrics::{BnKeystoreMetrics, IKeystoreMetrics}, 24 KeystoreAtom::KeystoreAtom, 25 }; 26 use android_security_metrics::binder::{BinderFeatures, Interface, Result as BinderResult, Strong}; 27 use anyhow::{Context, Result}; 28 29 /// This struct is defined to implement IKeystoreMetrics AIDL interface. 30 pub struct Metrics; 31 32 impl Metrics { 33 /// Create a new instance of Keystore Metrics service. new_native_binder() -> Result<Strong<dyn IKeystoreMetrics>>34 pub fn new_native_binder() -> Result<Strong<dyn IKeystoreMetrics>> { 35 Ok(BnKeystoreMetrics::new_binder( 36 Self, 37 BinderFeatures { set_requesting_sid: true, ..BinderFeatures::default() }, 38 )) 39 } 40 pull_metrics(&self, atom_id: AtomID) -> Result<Vec<KeystoreAtom>>41 fn pull_metrics(&self, atom_id: AtomID) -> Result<Vec<KeystoreAtom>> { 42 // Check permission. Function should return if this failed. Therefore having '?' at the end 43 // is very important. 44 check_keystore_permission(KeystorePerm::pull_metrics()).context("In pull_metrics.")?; 45 METRICS_STORE.get_atoms(atom_id) 46 } 47 } 48 49 impl Interface for Metrics {} 50 51 impl IKeystoreMetrics for Metrics { pullMetrics(&self, atom_id: AtomID) -> BinderResult<Vec<KeystoreAtom>>52 fn pullMetrics(&self, atom_id: AtomID) -> BinderResult<Vec<KeystoreAtom>> { 53 let _wp = wd::watch_millis("IKeystoreMetrics::pullMetrics", 500); 54 map_or_log_err(self.pull_metrics(atom_id), Ok) 55 } 56 } 57