• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::ks_err;
19 use crate::metrics_store::METRICS_STORE;
20 use crate::permission::KeystorePerm;
21 use crate::utils::{check_keystore_permission, watchdog as wd};
22 use android_security_metrics::aidl::android::security::metrics::{
23     AtomID::AtomID,
24     IKeystoreMetrics::{BnKeystoreMetrics, IKeystoreMetrics},
25     KeystoreAtom::KeystoreAtom,
26 };
27 use android_security_metrics::binder::{BinderFeatures, Interface, Result as BinderResult, Strong};
28 use anyhow::{Context, Result};
29 
30 /// This struct is defined to implement IKeystoreMetrics AIDL interface.
31 pub struct Metrics;
32 
33 impl Metrics {
34     /// Create a new instance of Keystore Metrics service.
new_native_binder() -> Result<Strong<dyn IKeystoreMetrics>>35     pub fn new_native_binder() -> Result<Strong<dyn IKeystoreMetrics>> {
36         Ok(BnKeystoreMetrics::new_binder(
37             Self,
38             BinderFeatures { set_requesting_sid: true, ..BinderFeatures::default() },
39         ))
40     }
41 
pull_metrics(&self, atom_id: AtomID) -> Result<Vec<KeystoreAtom>>42     fn pull_metrics(&self, atom_id: AtomID) -> Result<Vec<KeystoreAtom>> {
43         // Check permission. Function should return if this failed. Therefore having '?' at the end
44         // is very important.
45         check_keystore_permission(KeystorePerm::PullMetrics).context(ks_err!())?;
46         METRICS_STORE.get_atoms(atom_id)
47     }
48 }
49 
50 impl Interface for Metrics {}
51 
52 impl IKeystoreMetrics for Metrics {
pullMetrics(&self, atom_id: AtomID) -> BinderResult<Vec<KeystoreAtom>>53     fn pullMetrics(&self, atom_id: AtomID) -> BinderResult<Vec<KeystoreAtom>> {
54         let _wp = wd::watch_millis("IKeystoreMetrics::pullMetrics", 500);
55         map_or_log_err(self.pull_metrics(atom_id), Ok)
56     }
57 }
58