• 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.annotation.NonNull;
22 import androidx.room.AutoMigration;
23 import androidx.room.Database;
24 import androidx.room.Room;
25 import androidx.room.RoomDatabase;
26 import androidx.room.TypeConverters;
27 
28 import com.android.adservices.data.common.FledgeRoomConverters;
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)})
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 = 2;
46     public static final String DATABASE_NAME = "sharedstorage.db";
47     static final Long FOREIGN_KEY_AUTOGENERATE_SUBSTITUTE = null;
48 
49     private static volatile SharedStorageDatabase sSingleton = null;
50 
51     /** Returns or creates the instance of SharedStorageDatabase given a context. */
getInstance(@onNull Context context)52     public static SharedStorageDatabase getInstance(@NonNull Context context) {
53         Objects.requireNonNull(context, "Context must be provided.");
54         // Initialization pattern recommended on page 334 of "Effective Java" 3rd edition
55         SharedStorageDatabase singleReadResult = sSingleton;
56         if (singleReadResult != null) {
57             return singleReadResult;
58         }
59         synchronized (SINGLETON_LOCK) {
60             if (sSingleton == null) {
61                 sSingleton =
62                         Room.databaseBuilder(context, SharedStorageDatabase.class, DATABASE_NAME)
63                                 .fallbackToDestructiveMigration()
64                                 .build();
65             }
66             return sSingleton;
67         }
68     }
69 
70     /** @return a Dao to run queries for app install */
appInstallDao()71     public abstract AppInstallDao appInstallDao();
72 
73     /** @return a DAO for interfacing with ad event histograms and their overrides */
frequencyCapDao()74     public abstract FrequencyCapDao frequencyCapDao();
75 }
76