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.service.stats;
18 
19 import android.annotation.NonNull;
20 
21 import com.android.adservices.shared.util.Clock;
22 import com.android.internal.annotations.VisibleForTesting;
23 
24 import java.util.Objects;
25 
26 /** Factory class to create loggers for logging Custom Audience process signals. */
27 public class CustomAudienceLoggerFactory {
28     private static CustomAudienceLoggerFactory sSingleton;
29     private Clock mClock;
30     private AdServicesLogger mAdServicesLogger;
31 
32     @VisibleForTesting
CustomAudienceLoggerFactory(@onNull Clock clock, @NonNull AdServicesLogger adServicesLogger)33     CustomAudienceLoggerFactory(@NonNull Clock clock, @NonNull AdServicesLogger adServicesLogger) {
34         Objects.requireNonNull(clock);
35         Objects.requireNonNull(adServicesLogger);
36         mClock = clock;
37         mAdServicesLogger = adServicesLogger;
38     }
39 
40     /** Returns the singleton instance of the {@link CustomAudienceLoggerFactory}. */
41     @NonNull
getInstance()42     public static CustomAudienceLoggerFactory getInstance() {
43         synchronized (CustomAudienceLoggerFactory.class) {
44             if (sSingleton == null) {
45                 sSingleton =
46                         new CustomAudienceLoggerFactory(
47                                 Clock.getInstance(), AdServicesLoggerImpl.getInstance());
48             }
49         }
50 
51         return sSingleton;
52     }
53 
54     /** Create and returns a new instance of {@link CustomAudienceLoggerFactory} that is no-op. */
55     @NonNull
getNoOpInstance()56     public static CustomAudienceLoggerFactory getNoOpInstance() {
57         return new CustomAudienceLoggerFactory(Clock.getInstance(), new NoOpLoggerImpl());
58     }
59 
60     /**
61      * @return a new {@link BackgroundFetchExecutionLogger}.
62      */
getBackgroundFetchExecutionLogger()63     public BackgroundFetchExecutionLogger getBackgroundFetchExecutionLogger() {
64         return new BackgroundFetchExecutionLogger(mClock, mAdServicesLogger);
65     }
66 
67     /**
68      * @return a new {@link UpdateCustomAudienceExecutionLogger}.
69      */
getUpdateCustomAudienceExecutionLogger()70     public UpdateCustomAudienceExecutionLogger getUpdateCustomAudienceExecutionLogger() {
71         return new UpdateCustomAudienceExecutionLogger(mClock, mAdServicesLogger);
72     }
73 }
74