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 android.util.Log 21 import androidx.work.Data 22 import androidx.work.OneTimeWorkRequest 23 import androidx.work.OneTimeWorkRequestBuilder 24 import androidx.work.WorkerParameters 25 import com.android.statementservice.database.DomainGroups 26 import com.android.statementservice.utils.AndroidUtils 27 import com.android.statementservice.utils.StatementUtils 28 import kotlinx.coroutines.coroutineScope 29 30 class SingleV1RequestWorker(appContext: Context, params: WorkerParameters) : 31 BaseRequestWorker(appContext, params) { 32 33 companion object { 34 private val TAG = SingleV1RequestWorker::class.java.simpleName 35 private const val DEBUG = false 36 37 private const val PACKAGE_NAME_KEY = "packageName" 38 private const val HOST_KEY = "host" 39 const val HOST_SUCCESS_PREFIX = "hostSuccess:" 40 const val HOST_FAILURE_PREFIX = "hostFailure:" 41 buildRequestnull42 fun buildRequest( 43 packageName: String, 44 host: String, 45 block: OneTimeWorkRequest.Builder.() -> Unit = {} 46 ) = OneTimeWorkRequestBuilder<SingleV1RequestWorker>() 47 .setInputData( 48 Data.Builder() 49 .putString(PACKAGE_NAME_KEY, packageName) 50 .putString(HOST_KEY, host) 51 .build() 52 ) 53 .apply(block) 54 .build() 55 } 56 <lambda>null57 override suspend fun doWork() = coroutineScope { 58 if (!AndroidUtils.isReceiverV1Enabled(appContext)) { 59 return@coroutineScope Result.success() 60 } 61 62 val packageName = params.inputData.getString(PACKAGE_NAME_KEY)!! 63 val host = params.inputData.getString(HOST_KEY)!! 64 65 database.clear(packageName, host) 66 67 val (result, status, statement) = verifier.verifyHost(host, packageName, params.network) 68 69 if (DEBUG) { 70 Log.d( 71 TAG, "Domain verification v1 request for $packageName: " + 72 "host = $host, status = $status" 73 ) 74 } 75 76 // Coerce failure results into success so that final collection task gets a chance to run 77 when (result) { 78 is Result.Success -> { 79 val deContext = appContext.createDeviceProtectedStorageContext() 80 val sp = deContext?.getSharedPreferences(packageName, Context.MODE_PRIVATE) 81 sp?.edit()?.putInt("$HOST_SUCCESS_PREFIX$host", status.value)?.apply() 82 val groups = statement?.dynamicAppLinkComponents.orEmpty().map { 83 StatementUtils.createUriRelativeFilterGroup(it) 84 } 85 database.insertDomainGroups(DomainGroups(packageName, host, groups)) 86 Result.success() 87 } 88 is Result.Failure -> { 89 val deContext = appContext.createDeviceProtectedStorageContext() 90 val sp = deContext?.getSharedPreferences(packageName, Context.MODE_PRIVATE) 91 sp?.edit()?.putInt("$HOST_FAILURE_PREFIX$host", status.value)?.apply() 92 Result.success() 93 } 94 else -> result 95 } 96 } 97 } 98