• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.room.AutoMigration;
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 import com.android.adservices.shared.common.ApplicationContextSingleton;
29 
30 import com.google.errorprone.annotations.concurrent.GuardedBy;
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     @GuardedBy("SINGLETON_LOCK")
51     private static volatile SharedStorageDatabase sSingleton;
52 
53     /** Returns or creates the instance of SharedStorageDatabase given a context. */
getInstance()54     public static SharedStorageDatabase getInstance() {
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                 Context context = ApplicationContextSingleton.get();
63                 sSingleton =
64                         FileCompatUtils.roomDatabaseBuilderHelper(
65                                         context, SharedStorageDatabase.class, DATABASE_NAME)
66                                 .fallbackToDestructiveMigration()
67                                 .build();
68             }
69             return sSingleton;
70         }
71     }
72 
73     /** @return a Dao to run queries for app install */
appInstallDao()74     public abstract AppInstallDao appInstallDao();
75 
76     /** @return a DAO for interfacing with ad event histograms and their overrides */
frequencyCapDao()77     public abstract FrequencyCapDao frequencyCapDao();
78 }
79