• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 androidx.room.Database;
20 import androidx.room.RoomDatabase;
21 import androidx.room.TypeConverters;
22 
23 import com.android.adservices.data.common.FledgeRoomConverters;
24 import com.android.adservices.service.common.compat.FileCompatUtils;
25 import com.android.adservices.shared.common.ApplicationContextSingleton;
26 
27 /** Room based database for Ad Selection Debug Reporting. */
28 @Database(
29         entities = {DBAdSelectionDebugReport.class},
30         version = AdSelectionDebugReportingDatabase.DATABASE_VERSION)
31 @TypeConverters({FledgeRoomConverters.class})
32 public abstract class AdSelectionDebugReportingDatabase extends RoomDatabase {
33     private static final Object SINGLETON_LOCK = new Object();
34 
35     public static final int DATABASE_VERSION = 1;
36 
37     public static final String DATABASE_NAME =
38             FileCompatUtils.getAdservicesFilename("adselection_debug_reporting.db");
39 
40     private static volatile AdSelectionDebugReportingDatabase sSingleton = null;
41 
42     /** Returns an instance of the AdSelectionDatabase given a context. */
getInstance()43     public static AdSelectionDebugReportingDatabase getInstance() {
44         // Initialization pattern recommended on page 334 of "Effective Java" 3rd edition
45         AdSelectionDebugReportingDatabase singleReadResult = sSingleton;
46         if (singleReadResult != null) {
47             return singleReadResult;
48         }
49         synchronized (SINGLETON_LOCK) {
50             if (sSingleton == null) {
51                 sSingleton =
52                         FileCompatUtils.roomDatabaseBuilderHelper(
53                                         ApplicationContextSingleton.get(),
54                                         AdSelectionDebugReportingDatabase.class,
55                                         DATABASE_NAME)
56                                 .fallbackToDestructiveMigration()
57                                 .build();
58             }
59             return sSingleton;
60         }
61     }
62 
63     /**
64      * @return a Dao to access entities in AdSelection database.
65      */
getAdSelectionDebugReportDao()66     public abstract AdSelectionDebugReportDao getAdSelectionDebugReportDao();
67 }
68