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.data.adselection; 18 19 import android.content.Context; 20 21 import androidx.room.AutoMigration; 22 import androidx.room.Database; 23 import androidx.room.RoomDatabase; 24 import androidx.room.TypeConverters; 25 26 import com.android.adservices.data.common.FledgeRoomConverters; 27 import com.android.adservices.service.common.compat.FileCompatUtils; 28 import com.android.adservices.shared.common.ApplicationContextSingleton; 29 30 import com.google.errorprone.annotations.concurrent.GuardedBy; 31 32 /** Room based database for Ad Selection. */ 33 @Database( 34 entities = { 35 DBAdSelection.class, 36 DBBuyerDecisionLogic.class, 37 DBAdSelectionOverride.class, 38 DBAdSelectionFromOutcomesOverride.class, 39 DBRegisteredAdInteraction.class, 40 DBBuyerDecisionOverride.class, 41 DBReportingData.class, 42 DBAdSelectionInitialization.class, 43 DBAdSelectionResult.class, 44 DBReportingComputationInfo.class, 45 DBConsentedDebugConfiguration.class 46 }, 47 version = AdSelectionDatabase.DATABASE_VERSION, 48 autoMigrations = { 49 @AutoMigration(from = 1, to = 2), 50 @AutoMigration(from = 2, to = 3), 51 @AutoMigration(from = 3, to = 4), 52 @AutoMigration(from = 4, to = 5), 53 @AutoMigration(from = 5, to = 6), 54 @AutoMigration(from = 6, to = 7), 55 @AutoMigration(from = 7, to = 8), 56 @AutoMigration(from = 8, to = 9), 57 @AutoMigration(from = 9, to = 10), 58 @AutoMigration(from = 10, to = 11) 59 }) 60 @TypeConverters({FledgeRoomConverters.class}) 61 public abstract class AdSelectionDatabase extends RoomDatabase { 62 private static final Object SINGLETON_LOCK = new Object(); 63 64 public static final int DATABASE_VERSION = 11; 65 // TODO(b/230653780): Should we separate the DB. 66 public static final String DATABASE_NAME = 67 FileCompatUtils.getAdservicesFilename("adselection.db"); 68 69 @GuardedBy("SINGLETON_LOCK") 70 private static volatile AdSelectionDatabase sSingleton; 71 72 /** Returns an instance of the AdSelectionDatabase given a context. */ getInstance()73 public static AdSelectionDatabase getInstance() { 74 // Initialization pattern recommended on page 334 of "Effective Java" 3rd edition 75 AdSelectionDatabase singleReadResult = sSingleton; 76 if (singleReadResult != null) { 77 return singleReadResult; 78 } 79 synchronized (SINGLETON_LOCK) { 80 if (sSingleton == null) { 81 Context context = ApplicationContextSingleton.get(); 82 sSingleton = 83 FileCompatUtils.roomDatabaseBuilderHelper( 84 context, AdSelectionDatabase.class, DATABASE_NAME) 85 .fallbackToDestructiveMigration() 86 .build(); 87 } 88 return sSingleton; 89 } 90 } 91 92 /** 93 * @return a Dao to access entities in AdSelection database. 94 */ adSelectionEntryDao()95 public abstract AdSelectionEntryDao adSelectionEntryDao(); 96 97 /** 98 * @return a Dao to access ConsentedDebugConfiguration entities in AdSelection database. 99 */ consentedDebugConfigurationDao()100 public abstract ConsentedDebugConfigurationDao consentedDebugConfigurationDao(); 101 } 102