• 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.cobalt;
18 
19 import android.content.Context;
20 import android.content.res.AssetManager;
21 
22 import com.android.adservices.LoggerFactory;
23 import com.android.adservices.service.Flags;
24 import com.android.cobalt.domain.Project;
25 import com.android.cobalt.registry.RegistryMerger;
26 import com.android.internal.annotations.VisibleForTesting;
27 
28 import com.google.cobalt.CobaltRegistry;
29 import com.google.common.io.ByteStreams;
30 
31 import java.io.InputStream;
32 import java.util.Optional;
33 
34 /** Loads the Cobalt registry from a APK asset. */
35 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
36 public final class CobaltRegistryLoader {
37     private static final String REGISTRY_ASSET_FILE = "cobalt/cobalt_registry.binarypb";
38     private static final LoggerFactory.Logger sLogger = LoggerFactory.getLogger();
39 
40     /**
41      * Get the Cobalt registry from the APK asset directory.
42      *
43      * @return the CobaltRegistry
44      */
45     @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
46     // TODO(b/311183933): Remove passed in Context from static method.
47     @SuppressWarnings("AvoidStaticContext")
getRegistry(Context context, Flags flags)48     public static Project getRegistry(Context context, Flags flags)
49             throws CobaltInitializationException {
50         if (!CobaltRegistryValidated.IS_REGISTRY_VALIDATED) {
51             throw new AssertionError(
52                     "Cobalt registry was not validated at build time, something is very wrong");
53         }
54 
55         AssetManager assetManager = context.getAssets();
56         try (InputStream inputStream = assetManager.open(REGISTRY_ASSET_FILE)) {
57             CobaltRegistry baseRegistry =
58                     CobaltRegistry.parseFrom(ByteStreams.toByteArray(inputStream));
59 
60             if (flags.getCobaltFallBackToDefaultBaseRegistry()) {
61                 sLogger.d(
62                         "Use base Cobalt registry because fall back to default base registry flag"
63                                 + " is enabled.");
64                 return Project.create(baseRegistry);
65             }
66 
67             if (flags.getCobaltRegistryOutOfBandUpdateEnabled()) {
68                 Optional<CobaltRegistry> mddRegistry =
69                         CobaltDownloadRegistryManager.getInstance().getMddRegistry();
70                 if (mddRegistry.isPresent()) {
71                     sLogger.d("Use merged Cobalt registry.");
72                     return Project.create(
73                             RegistryMerger.mergeRegistries(baseRegistry, mddRegistry.get()));
74                 }
75             }
76             sLogger.d("Use base Cobalt registry.");
77             return Project.create(baseRegistry);
78         } catch (Exception e) {
79             throw new CobaltInitializationException("Exception while reading registry", e);
80         }
81     }
82 }
83