1 /*
2  * Copyright (C) 2020 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.statementservice.domain.worker
18 
19 import android.content.Context
20 import androidx.work.Data
21 import androidx.work.OneTimeWorkRequest
22 import androidx.work.OneTimeWorkRequestBuilder
23 import androidx.work.WorkerParameters
24 import com.android.statementservice.utils.AndroidUtils
25 import com.android.statementservice.utils.StatementUtils
26 import kotlinx.coroutines.coroutineScope
27 import java.util.UUID
28 
29 class SingleV2RequestWorker(appContext: Context, params: WorkerParameters) :
30     BaseRequestWorker(appContext, params) {
31 
32     companion object {
33         private const val DOMAIN_SET_ID_KEY = "domainSetId"
34         private const val PACKAGE_NAME_KEY = "packageName"
35         private const val HOST_KEY = "host"
36 
buildRequestnull37         fun buildRequest(
38             domainSetId: UUID,
39             packageName: String,
40             host: String,
41             block: OneTimeWorkRequest.Builder.() -> Unit = {}
42         ) = OneTimeWorkRequestBuilder<SingleV2RequestWorker>()
43             .setInputData(
44                 Data.Builder()
45                     .putString(DOMAIN_SET_ID_KEY, domainSetId.toString())
46                     .putString(PACKAGE_NAME_KEY, packageName)
47                     .putString(HOST_KEY, host)
48                     .build()
49             )
50             .apply(block)
51             .build()
52     }
53 
<lambda>null54     override suspend fun doWork() = coroutineScope {
55         if (!AndroidUtils.isReceiverV2Enabled(appContext)) {
56             return@coroutineScope Result.success()
57         }
58 
59         val domainSetId = params.inputData.getString(DOMAIN_SET_ID_KEY)!!.let(UUID::fromString)
60         val packageName = params.inputData.getString(PACKAGE_NAME_KEY)!!
61         val host = params.inputData.getString(HOST_KEY)!!
62 
63         val (result, status, statement) = verifier.verifyHost(host, packageName, params.network)
64 
65         verificationManager.setDomainVerificationStatus(domainSetId, setOf(host), status.value)
66         val groups = statement?.dynamicAppLinkComponents.orEmpty().map {
67             StatementUtils.createUriRelativeFilterGroup(it)
68         }
69         updateUriRelativeFilterGroups(packageName, mapOf(host to groups))
70 
71         result
72     }
73 }
74