1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <android/content/AttributionSourceState.h>
20 #include <error/BinderResult.h>
21 
22 #include "IPermissionProvider.h"
23 
24 namespace com::android::media::permission {
25 
26 using ::android::content::AttributionSourceState;
27 
28 class ValidatedAttributionSourceState {
29   public:
30     /**
31      * Validates an attribution source from within the context of a binder transaction.
32      * Overwrites the uid/pid and validates the packageName.
33      * Returns EX_SECURITY on package validation fail.
34      */
35     static ::android::error::BinderResult<ValidatedAttributionSourceState> createFromBinderContext(
36             AttributionSourceState attr, const IPermissionProvider& provider);
37 
38     /**
39      * Creates a ValidatedAttributionSourceState in cases where the source is passed from a
40      * trusted entity which already performed validation.
41      */
createFromTrustedSource(AttributionSourceState attr)42     static ValidatedAttributionSourceState createFromTrustedSource(AttributionSourceState attr) {
43         return ValidatedAttributionSourceState(attr);
44     }
45 
46     /**
47      * Create a ValidatedAttribubtionSourceState in cases where the uid/pid is trusted, but the
48      * packages have not been validated. Proper use of the previous two methods should avoid the
49      * necessity of this, but it is useful for migration purposes as well as testing this class.
50      * Returns EX_SECURITY on package validation fail.
51      */
52     static ::android::error::BinderResult<ValidatedAttributionSourceState>
53     createFromTrustedUidNoPackage(AttributionSourceState attr, const IPermissionProvider& provider);
54 
AttributionSourceState()55     operator AttributionSourceState() const { return state_; }
56 
57     operator const AttributionSourceState&() const { return state_; }
58 
unwrapInto()59     AttributionSourceState unwrapInto() && { return std::move(state_); }
60 
61     bool operator==(const ValidatedAttributionSourceState& other) const {
62         return operator==(other.state_);
63     }
64 
65     bool operator==(const AttributionSourceState& other) const { return state_ == other; }
66 
67   private:
ValidatedAttributionSourceState(AttributionSourceState attr)68     ValidatedAttributionSourceState(AttributionSourceState attr) : state_(attr) {}
69 
70     AttributionSourceState state_;
71 };
72 }  // namespace com::android::media::permission
73