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 kotlinx.coroutines.coroutineScope
26 import java.util.UUID
27 
28 class SingleV2RequestWorker(appContext: Context, params: WorkerParameters) :
29     BaseRequestWorker(appContext, params) {
30 
31     companion object {
32         private const val DOMAIN_SET_ID_KEY = "domainSetId"
33         private const val PACKAGE_NAME_KEY = "packageName"
34         private const val HOST_KEY = "host"
35 
buildRequestnull36         fun buildRequest(
37             domainSetId: UUID,
38             packageName: String,
39             host: String,
40             block: OneTimeWorkRequest.Builder.() -> Unit = {}
41         ) = OneTimeWorkRequestBuilder<SingleV2RequestWorker>()
42             .setInputData(
43                 Data.Builder()
44                     .putString(DOMAIN_SET_ID_KEY, domainSetId.toString())
45                     .putString(PACKAGE_NAME_KEY, packageName)
46                     .putString(HOST_KEY, host)
47                     .build()
48             )
49             .apply(block)
50             .build()
51     }
52 
<lambda>null53     override suspend fun doWork() = coroutineScope {
54         if (!AndroidUtils.isReceiverV2Enabled(appContext)) {
55             return@coroutineScope Result.success()
56         }
57 
58         val domainSetId = params.inputData.getString(DOMAIN_SET_ID_KEY)!!.let(UUID::fromString)
59         val packageName = params.inputData.getString(PACKAGE_NAME_KEY)!!
60         val host = params.inputData.getString(HOST_KEY)!!
61 
62         val (result, status) = verifier.verifyHost(host, packageName, params.network)
63 
64         verificationManager.setDomainVerificationStatus(domainSetId, setOf(host), status.value)
65 
66         result
67     }
68 }
69