blob: 3d8d6d3fdc5c66ce315a6c4a0040a780dfcfa977 [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.
17use crate::error::map_or_log_err;
18use crate::metrics_store::METRICS_STORE;
19use crate::permission::KeystorePerm;
20use crate::utils::{check_keystore_permission, watchdog as wd};
21use android_security_metrics::aidl::android::security::metrics::{
22 AtomID::AtomID,
23 IKeystoreMetrics::{BnKeystoreMetrics, IKeystoreMetrics},
24 KeystoreAtom::KeystoreAtom,
Hasini Gunasingheb7142972021-02-20 03:11:27 +000025};
Hasini Gunasinghe15891e62021-06-10 16:23:27 +000026use android_security_metrics::binder::{BinderFeatures, Interface, Result as BinderResult, Strong};
27use anyhow::{Context, Result};
Hasini Gunasinghe0aba68a2021-03-19 00:43:52 +000028
Hasini Gunasinghe15891e62021-06-10 16:23:27 +000029/// This struct is defined to implement IKeystoreMetrics AIDL interface.
30pub struct Metrics;
31
32impl Metrics {
33 /// Create a new instance of Keystore Metrics service.
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
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.
Janis Danisevskisa916d992021-10-19 15:46:09 -070044 check_keystore_permission(KeystorePerm::PullMetrics).context("In pull_metrics.")?;
Hasini Gunasinghe15891e62021-06-10 16:23:27 +000045 METRICS_STORE.get_atoms(atom_id)
Hasini Gunasinghe69f0bc52021-05-15 02:08:39 +000046 }
47}
48
Hasini Gunasinghe15891e62021-06-10 16:23:27 +000049impl Interface for Metrics {}
50
51impl IKeystoreMetrics for Metrics {
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)
Hasini Gunasingheb7142972021-02-20 03:11:27 +000055 }
56}