• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 package com.android.adservices.service.measurement.util;
18 
19 import android.content.Context;
20 import android.net.Uri;
21 import android.os.Build;
22 
23 import com.android.adservices.LogUtil;
24 import com.android.adservices.data.enrollment.EnrollmentDao;
25 import com.android.adservices.service.Flags;
26 import com.android.adservices.service.common.AppManifestConfigHelper;
27 import com.android.adservices.service.enrollment.EnrollmentData;
28 
29 import java.util.Optional;
30 
31 /** Enrollment utilities for measurement. */
32 public final class Enrollment {
33 
34     public static final String FAKE_ENROLLMENT = "fake_enrollment";
35 
Enrollment()36     private Enrollment() { }
37 
38     /**
39      * Returns an {@code Optional<String>} of the ad-tech enrollment record ID.
40      *
41      * @param registrationUri the ad-tech URL used to register a source or trigger.
42      * @param packageName Package mame of the registrant
43      * @param enrollmentDao an instance of {@code EnrollmentDao}.
44      * @param context a valid {@code Context} object
45      * @param flags a valid {@code Flags} object
46      * @return enrollmentId if enrollment id exists and all validations pass otherwise empty
47      */
getValidEnrollmentId( Uri registrationUri, String packageName, EnrollmentDao enrollmentDao, Context context, Flags flags)48     public static Optional<String> getValidEnrollmentId(
49             Uri registrationUri,
50             String packageName,
51             EnrollmentDao enrollmentDao,
52             Context context,
53             Flags flags) {
54         Uri uriWithoutParams = registrationUri.buildUpon().clearQuery().fragment(null).build();
55 
56         EnrollmentData enrollmentData =
57                 enrollmentDao.getEnrollmentDataFromMeasurementUrl(uriWithoutParams);
58         if (enrollmentData == null) {
59             LogUtil.w(
60                     "Enrollment check failed, Reason: Enrollment Id Not Found, "
61                             + "Registration URI: %s",
62                     registrationUri);
63             return Optional.empty();
64         }
65         if (flags.isEnrollmentBlocklisted(enrollmentData.getEnrollmentId())) {
66             LogUtil.w(
67                     "Enrollment check failed, Reason: Enrollment Id in blocklist, "
68                             + "Registration URI: %s, Enrollment Id: %s",
69                     registrationUri, enrollmentData.getEnrollmentId());
70             return Optional.empty();
71         }
72         // TODO(b/269798827): Enable for R.
73         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S
74                 && !AppManifestConfigHelper.isAllowedAttributionAccess(
75                         context, packageName, enrollmentData.getEnrollmentId())) {
76             LogUtil.w(
77                     "Enrollment check failed, Reason: Enrollment Id missing from "
78                             + "App Manifest AdTech allowlist, "
79                             + "Registration URI: %s, Enrollment Id: %s",
80                     registrationUri, enrollmentData.getEnrollmentId());
81             return Optional.empty();
82         }
83         return Optional.of(enrollmentData.getEnrollmentId());
84     }
85 
86     /**
87      * Returns an {@code Optional<Uri>} of the ad-tech server URL that accepts attribution reports.
88      *
89      * @param enrollmentId the enrollment record ID.
90      * @param enrollmentDao an instance of {@code EnrollmentDao}.
91      */
maybeGetReportingOrigin(String enrollmentId, EnrollmentDao enrollmentDao)92     public static Optional<Uri> maybeGetReportingOrigin(String enrollmentId,
93             EnrollmentDao enrollmentDao) {
94         EnrollmentData enrollmentData = enrollmentDao.getEnrollmentData(enrollmentId);
95         if (enrollmentData != null && enrollmentData.getAttributionReportingUrl().size() > 0) {
96             return Optional.of(Uri.parse(enrollmentData.getAttributionReportingUrl().get(0)));
97         }
98         return Optional.empty();
99     }
100 }
101