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.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 import com.android.internal.annotations.GuardedBy;
30 
31 import java.util.Objects;
32 
33 /** Room based database for ad selection on servers. */
34 @SuppressWarnings("deprecation")
35 @Database(
36         entities = {
37             DBReportingUris.class,
38             DBEncryptionKey.class,
39             DBEncryptionContext.class,
40             DBAuctionServerAdSelection.class,
41             DBProtectedServersEncryptionConfig.class
42         },
43         version = AdSelectionServerDatabase.DATABASE_VERSION,
44         autoMigrations = {
45             @AutoMigration(from = 1, to = 2),
46             @AutoMigration(from = 2, to = 3),
47             @AutoMigration(from = 3, to = 4),
48             @AutoMigration(from = 4, to = 5),
49         })
50 @TypeConverters({FledgeRoomConverters.class})
51 public abstract class AdSelectionServerDatabase extends RoomDatabase {
52     public static final int DATABASE_VERSION = 5;
53     public static final String DATABASE_NAME =
54             FileCompatUtils.getAdservicesFilename("adselectionserver.db");
55 
56     private static final Object SINGLETON_LOCK = new Object();
57 
58     @GuardedBy("SINGLETON_LOCK")
59     private static AdSelectionServerDatabase sSingleton = null;
60 
61     /** Returns an instance of the AdSelectionEncryptionDatabase given a context. */
getInstance(@onNull Context context)62     public static AdSelectionServerDatabase getInstance(@NonNull Context context) {
63         Objects.requireNonNull(context, "Context must be present.");
64         synchronized (SINGLETON_LOCK) {
65             if (Objects.isNull(sSingleton)) {
66                 sSingleton =
67                         FileCompatUtils.roomDatabaseBuilderHelper(
68                                         context, AdSelectionServerDatabase.class, DATABASE_NAME)
69                                 .fallbackToDestructiveMigration()
70                                 .build();
71             }
72             return sSingleton;
73         }
74     }
75 
76     /**
77      * @return a Dao to access entities in {@link DBEncryptionKey} database.
78      */
encryptionKeyDao()79     public abstract EncryptionKeyDao encryptionKeyDao();
80 
81     /**
82      * @return a Dao to access entities in {@link DBProtectedServersEncryptionConfig} table.
83      */
protectedServersEncryptionConfigDao()84     public abstract ProtectedServersEncryptionConfigDao protectedServersEncryptionConfigDao();
85 
86     /**
87      * @return a Dao to access entities in {@link DBEncryptionContext} database.
88      */
encryptionContextDao()89     public abstract EncryptionContextDao encryptionContextDao();
90 
91     /**
92      * @return a Dao to access entities in {@link DBAuctionServerAdSelection} database.
93      */
auctionServerAdSelectionDao()94     public abstract AuctionServerAdSelectionDao auctionServerAdSelectionDao();
95 }
96