blob: b8848093abda0737a02e8f5b2aba7a28ca2b080d [file] [log] [blame]
Hasini Gunasingheb7142972021-02-20 03:11:27 +00001// 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
Hasini Gunasinghe15891e62021-06-10 16:23:27 +000015//! 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.
David Drysdaledb7ddde2024-06-07 16:22:49 +010017use crate::error::into_logged_binder;
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +000018use crate::ks_err;
Hasini Gunasinghe15891e62021-06-10 16:23:27 +000019use crate::metrics_store::METRICS_STORE;
20use crate::permission::KeystorePerm;
21use crate::utils::{check_keystore_permission, watchdog as wd};
22use android_security_metrics::aidl::android::security::metrics::{
23 AtomID::AtomID,
24 IKeystoreMetrics::{BnKeystoreMetrics, IKeystoreMetrics},
25 KeystoreAtom::KeystoreAtom,
Hasini Gunasingheb7142972021-02-20 03:11:27 +000026};
Hasini Gunasinghe15891e62021-06-10 16:23:27 +000027use android_security_metrics::binder::{BinderFeatures, Interface, Result as BinderResult, Strong};
28use anyhow::{Context, Result};
Hasini Gunasinghe0aba68a2021-03-19 00:43:52 +000029
Hasini Gunasinghe15891e62021-06-10 16:23:27 +000030/// This struct is defined to implement IKeystoreMetrics AIDL interface.
31pub struct Metrics;
32
33impl Metrics {
34 /// Create a new instance of Keystore Metrics service.
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
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.
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +000045 check_keystore_permission(KeystorePerm::PullMetrics).context(ks_err!())?;
Hasini Gunasinghe15891e62021-06-10 16:23:27 +000046 METRICS_STORE.get_atoms(atom_id)
Hasini Gunasinghe69f0bc52021-05-15 02:08:39 +000047 }
48}
49
Hasini Gunasinghe15891e62021-06-10 16:23:27 +000050impl Interface for Metrics {}
51
52impl IKeystoreMetrics for Metrics {
53 fn pullMetrics(&self, atom_id: AtomID) -> BinderResult<Vec<KeystoreAtom>> {
David Drysdale703bcc12024-11-26 14:15:03 +000054 let _wp = wd::watch_millis_with("IKeystoreMetrics::pullMetrics", 500, atom_id);
David Drysdaledb7ddde2024-06-07 16:22:49 +010055 self.pull_metrics(atom_id).map_err(into_logged_binder)
Hasini Gunasingheb7142972021-02-20 03:11:27 +000056 }
57}