|
| 1 | +// Copyright 2021 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.firebase.heartbeatinfo; |
| 16 | + |
| 17 | +import android.content.Context; |
| 18 | +import android.util.Base64; |
| 19 | +import androidx.annotation.NonNull; |
| 20 | +import androidx.annotation.VisibleForTesting; |
| 21 | +import androidx.core.os.UserManagerCompat; |
| 22 | +import com.google.android.gms.tasks.Task; |
| 23 | +import com.google.android.gms.tasks.Tasks; |
| 24 | +import com.google.firebase.FirebaseApp; |
| 25 | +import com.google.firebase.components.Component; |
| 26 | +import com.google.firebase.components.Dependency; |
| 27 | +import com.google.firebase.inject.Provider; |
| 28 | +import com.google.firebase.platforminfo.UserAgentPublisher; |
| 29 | +import java.io.ByteArrayOutputStream; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Set; |
| 32 | +import java.util.concurrent.Executor; |
| 33 | +import java.util.concurrent.LinkedBlockingQueue; |
| 34 | +import java.util.concurrent.ThreadFactory; |
| 35 | +import java.util.concurrent.ThreadPoolExecutor; |
| 36 | +import java.util.concurrent.TimeUnit; |
| 37 | +import java.util.zip.GZIPOutputStream; |
| 38 | +import org.json.JSONArray; |
| 39 | +import org.json.JSONObject; |
| 40 | + |
| 41 | +/** Provides a function to store heartbeats and another function to retrieve stored heartbeats. */ |
| 42 | +public class DefaultHeartBeatController implements HeartBeatController, HeartBeatInfo { |
| 43 | + |
| 44 | + private final Provider<HeartBeatInfoStorage> storageProvider; |
| 45 | + |
| 46 | + private final Context applicationContext; |
| 47 | + |
| 48 | + private final Provider<UserAgentPublisher> userAgentProvider; |
| 49 | + |
| 50 | + private final Set<HeartBeatConsumer> consumers; |
| 51 | + |
| 52 | + private final Executor backgroundExecutor; |
| 53 | + |
| 54 | + private static final ThreadFactory THREAD_FACTORY = |
| 55 | + r -> new Thread(r, "heartbeat-information-executor"); |
| 56 | + |
| 57 | + public Task<Void> registerHeartBeat() { |
| 58 | + if (consumers.size() <= 0) { |
| 59 | + return Tasks.forResult(null); |
| 60 | + } |
| 61 | + boolean inDirectBoot = !UserManagerCompat.isUserUnlocked(applicationContext); |
| 62 | + if (inDirectBoot) { |
| 63 | + return Tasks.forResult(null); |
| 64 | + } |
| 65 | + |
| 66 | + return Tasks.call( |
| 67 | + backgroundExecutor, |
| 68 | + () -> { |
| 69 | + synchronized (DefaultHeartBeatController.this) { |
| 70 | + this.storageProvider |
| 71 | + .get() |
| 72 | + .storeHeartBeat( |
| 73 | + System.currentTimeMillis(), this.userAgentProvider.get().getUserAgent()); |
| 74 | + } |
| 75 | + |
| 76 | + return null; |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public Task<String> getHeartBeatsHeader() { |
| 82 | + boolean inDirectBoot = !UserManagerCompat.isUserUnlocked(applicationContext); |
| 83 | + if (inDirectBoot) { |
| 84 | + return Tasks.forResult(""); |
| 85 | + } |
| 86 | + return Tasks.call( |
| 87 | + backgroundExecutor, |
| 88 | + () -> { |
| 89 | + synchronized (DefaultHeartBeatController.this) { |
| 90 | + HeartBeatInfoStorage storage = this.storageProvider.get(); |
| 91 | + List<HeartBeatResult> allHeartBeats = storage.getAllHeartBeats(); |
| 92 | + storage.deleteAllHeartBeats(); |
| 93 | + JSONArray array = new JSONArray(); |
| 94 | + for (int i = 0; i < allHeartBeats.size(); i++) { |
| 95 | + HeartBeatResult result = allHeartBeats.get(i); |
| 96 | + JSONObject obj = new JSONObject(); |
| 97 | + obj.put("agent", result.getUserAgent()); |
| 98 | + obj.put("date", result.getUsedDates()); |
| 99 | + array.put(obj); |
| 100 | + } |
| 101 | + JSONObject output = new JSONObject(); |
| 102 | + output.put("heartbeats", array); |
| 103 | + output.put("version", "2"); |
| 104 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 105 | + GZIPOutputStream gzip = new GZIPOutputStream(out); |
| 106 | + gzip.write(output.toString().getBytes()); |
| 107 | + gzip.close(); |
| 108 | + return Base64.encodeToString(out.toString("UTF-8").getBytes(), Base64.URL_SAFE); |
| 109 | + } |
| 110 | + }); |
| 111 | + } |
| 112 | + |
| 113 | + private DefaultHeartBeatController( |
| 114 | + Context context, |
| 115 | + String persistenceKey, |
| 116 | + Set<HeartBeatConsumer> consumers, |
| 117 | + Provider<UserAgentPublisher> userAgentProvider) { |
| 118 | + this( |
| 119 | + () -> new HeartBeatInfoStorage(context, persistenceKey), |
| 120 | + consumers, |
| 121 | + new ThreadPoolExecutor( |
| 122 | + 0, 1, 30, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), THREAD_FACTORY), |
| 123 | + userAgentProvider, |
| 124 | + context); |
| 125 | + } |
| 126 | + |
| 127 | + @VisibleForTesting |
| 128 | + DefaultHeartBeatController( |
| 129 | + Provider<HeartBeatInfoStorage> testStorage, |
| 130 | + Set<HeartBeatConsumer> consumers, |
| 131 | + Executor executor, |
| 132 | + Provider<UserAgentPublisher> userAgentProvider, |
| 133 | + Context context) { |
| 134 | + storageProvider = testStorage; |
| 135 | + this.consumers = consumers; |
| 136 | + this.backgroundExecutor = executor; |
| 137 | + this.userAgentProvider = userAgentProvider; |
| 138 | + this.applicationContext = context; |
| 139 | + } |
| 140 | + |
| 141 | + public static @NonNull Component<DefaultHeartBeatController> component() { |
| 142 | + return Component.builder( |
| 143 | + DefaultHeartBeatController.class, HeartBeatController.class, HeartBeatInfo.class) |
| 144 | + .add(Dependency.required(Context.class)) |
| 145 | + .add(Dependency.required(FirebaseApp.class)) |
| 146 | + .add(Dependency.setOf(HeartBeatConsumer.class)) |
| 147 | + .add(Dependency.requiredProvider(UserAgentPublisher.class)) |
| 148 | + .factory( |
| 149 | + c -> |
| 150 | + new DefaultHeartBeatController( |
| 151 | + c.get(Context.class), |
| 152 | + c.get(FirebaseApp.class).getPersistenceKey(), |
| 153 | + c.setOf(HeartBeatConsumer.class), |
| 154 | + c.getProvider(UserAgentPublisher.class))) |
| 155 | + .build(); |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + @NonNull |
| 160 | + public synchronized HeartBeat getHeartBeatCode(@NonNull String heartBeatTag) { |
| 161 | + long presentTime = System.currentTimeMillis(); |
| 162 | + HeartBeatInfoStorage storage = storageProvider.get(); |
| 163 | + boolean shouldSendGlobalHB = storage.shouldSendGlobalHeartBeat(presentTime); |
| 164 | + if (shouldSendGlobalHB) { |
| 165 | + storage.postHeartBeatCleanUp(); |
| 166 | + return HeartBeat.GLOBAL; |
| 167 | + } else { |
| 168 | + return HeartBeat.NONE; |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments