1 /* 2 * Copyright (C) 2023 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.annotation.NonNull; 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 29 import java.util.Objects; 30 /** Room based database for Ad Selection Debug Reporting. */ 31 @Database( 32 entities = {DBAdSelectionDebugReport.class}, 33 version = AdSelectionDebugReportingDatabase.DATABASE_VERSION) 34 @TypeConverters({FledgeRoomConverters.class}) 35 public abstract class AdSelectionDebugReportingDatabase extends RoomDatabase { 36 private static final Object SINGLETON_LOCK = new Object(); 37 38 public static final int DATABASE_VERSION = 1; 39 40 public static final String DATABASE_NAME = 41 FileCompatUtils.getAdservicesFilename("adselection_debug_reporting.db"); 42 43 private static volatile AdSelectionDebugReportingDatabase sSingleton = null; 44 45 /** Returns an instance of the AdSelectionDatabase given a context. */ getInstance(@onNull Context context)46 public static AdSelectionDebugReportingDatabase getInstance(@NonNull Context context) { 47 Objects.requireNonNull(context, "Context must be provided."); 48 // Initialization pattern recommended on page 334 of "Effective Java" 3rd edition 49 AdSelectionDebugReportingDatabase singleReadResult = sSingleton; 50 if (singleReadResult != null) { 51 return singleReadResult; 52 } 53 synchronized (SINGLETON_LOCK) { 54 if (sSingleton == null) { 55 sSingleton = 56 FileCompatUtils.roomDatabaseBuilderHelper( 57 context, 58 AdSelectionDebugReportingDatabase.class, 59 DATABASE_NAME) 60 .fallbackToDestructiveMigration() 61 .build(); 62 } 63 return sSingleton; 64 } 65 } 66 67 /** 68 * @return a Dao to access entities in AdSelection database. 69 */ getAdSelectionDebugReportDao()70 public abstract AdSelectionDebugReportDao getAdSelectionDebugReportDao(); 71 } 72