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.annotation.NonNull; 22 import androidx.room.AutoMigration; 23 import androidx.room.Database; 24 import androidx.room.RoomDatabase; 25 import androidx.room.TypeConverters; 26 27 import com.android.adservices.data.common.FledgeRoomConverters; 28 import com.android.adservices.service.common.compat.FileCompatUtils; 29 30 import java.util.Objects; 31 32 /** Room based database for cross-app data that is read from during ad selection */ 33 @Database( 34 entities = { 35 DBAppInstallPermissions.class, 36 DBHistogramEventData.class, 37 DBHistogramIdentifier.class, 38 }, 39 version = SharedStorageDatabase.DATABASE_VERSION, 40 autoMigrations = {@AutoMigration(from = 1, to = 2), @AutoMigration(from = 2, to = 3)}) 41 @TypeConverters({FledgeRoomConverters.class}) 42 public abstract class SharedStorageDatabase extends RoomDatabase { 43 private static final Object SINGLETON_LOCK = new Object(); 44 45 public static final int DATABASE_VERSION = 3; 46 public static final String DATABASE_NAME = 47 FileCompatUtils.getAdservicesFilename("sharedstorage.db"); 48 static final Long FOREIGN_KEY_AUTOGENERATE_SUBSTITUTE = null; 49 50 private static volatile SharedStorageDatabase sSingleton = null; 51 52 /** Returns or creates the instance of SharedStorageDatabase given a context. */ getInstance(@onNull Context context)53 public static SharedStorageDatabase getInstance(@NonNull Context context) { 54 Objects.requireNonNull(context, "Context must be provided."); 55 // Initialization pattern recommended on page 334 of "Effective Java" 3rd edition 56 SharedStorageDatabase singleReadResult = sSingleton; 57 if (singleReadResult != null) { 58 return singleReadResult; 59 } 60 synchronized (SINGLETON_LOCK) { 61 if (sSingleton == null) { 62 sSingleton = 63 FileCompatUtils.roomDatabaseBuilderHelper( 64 context, SharedStorageDatabase.class, DATABASE_NAME) 65 .fallbackToDestructiveMigration() 66 .build(); 67 } 68 return sSingleton; 69 } 70 } 71 72 /** @return a Dao to run queries for app install */ appInstallDao()73 public abstract AppInstallDao appInstallDao(); 74 75 /** @return a DAO for interfacing with ad event histograms and their overrides */ frequencyCapDao()76 public abstract FrequencyCapDao frequencyCapDao(); 77 } 78