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 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         })
58 @TypeConverters({FledgeRoomConverters.class})
59 public abstract class AdSelectionDatabase extends RoomDatabase {
60     private static final Object SINGLETON_LOCK = new Object();
61 
62     public static final int DATABASE_VERSION = 9;
63     // TODO(b/230653780): Should we separate the DB.
64     public static final String DATABASE_NAME =
65             FileCompatUtils.getAdservicesFilename("adselection.db");
66 
67     private static volatile AdSelectionDatabase sSingleton = null;
68 
69     /** Returns an instance of the AdSelectionDatabase given a context. */
getInstance(@onNull Context context)70     public static AdSelectionDatabase getInstance(@NonNull Context context) {
71         Objects.requireNonNull(context, "Context must be provided.");
72         // Initialization pattern recommended on page 334 of "Effective Java" 3rd edition
73         AdSelectionDatabase singleReadResult = sSingleton;
74         if (singleReadResult != null) {
75             return singleReadResult;
76         }
77         synchronized (SINGLETON_LOCK) {
78             if (sSingleton == null) {
79                 sSingleton =
80                         FileCompatUtils.roomDatabaseBuilderHelper(
81                                         context, AdSelectionDatabase.class, DATABASE_NAME)
82                                 .fallbackToDestructiveMigration()
83                                 .build();
84             }
85             return sSingleton;
86         }
87     }
88 
89     /**
90      * @return a Dao to access entities in AdSelection database.
91      */
adSelectionEntryDao()92     public abstract AdSelectionEntryDao adSelectionEntryDao();
93 
94     /**
95      * @return a Dao to access ConsentedDebugConfiguration entities in AdSelection database.
96      */
consentedDebugConfigurationDao()97     public abstract ConsentedDebugConfigurationDao consentedDebugConfigurationDao();
98 }
99