262 lines
13 KiB
Java
262 lines
13 KiB
Java
|
package com.google.firebase.remoteconfig.internal;
|
||
|
|
||
|
import android.text.format.DateUtils;
|
||
|
import com.google.android.gms.common.util.Clock;
|
||
|
import com.google.android.gms.tasks.Task;
|
||
|
import com.google.android.gms.tasks.Tasks;
|
||
|
import com.google.firebase.analytics.connector.AnalyticsConnector;
|
||
|
import com.google.firebase.installations.FirebaseInstallationsApi;
|
||
|
import com.google.firebase.installations.InstallationTokenResult;
|
||
|
import com.google.firebase.remoteconfig.FirebaseRemoteConfigClientException;
|
||
|
import com.google.firebase.remoteconfig.FirebaseRemoteConfigException;
|
||
|
import com.google.firebase.remoteconfig.FirebaseRemoteConfigFetchThrottledException;
|
||
|
import com.google.firebase.remoteconfig.FirebaseRemoteConfigServerException;
|
||
|
import com.google.firebase.remoteconfig.internal.ConfigMetadataClient;
|
||
|
import java.util.Date;
|
||
|
import java.util.HashMap;
|
||
|
import java.util.Map;
|
||
|
import java.util.Random;
|
||
|
import java.util.concurrent.Executor;
|
||
|
import java.util.concurrent.TimeUnit;
|
||
|
|
||
|
/* loaded from: classes.dex */
|
||
|
public class ConfigFetchHandler {
|
||
|
private final AnalyticsConnector analyticsConnector;
|
||
|
private final Clock clock;
|
||
|
private final Map<String, String> customHttpHeaders;
|
||
|
private final Executor executor;
|
||
|
private final ConfigCacheClient fetchedConfigsCache;
|
||
|
private final FirebaseInstallationsApi firebaseInstallations;
|
||
|
private final ConfigFetchHttpClient frcBackendApiClient;
|
||
|
private final ConfigMetadataClient frcMetadata;
|
||
|
private final Random randomGenerator;
|
||
|
public static final long DEFAULT_MINIMUM_FETCH_INTERVAL_IN_SECONDS = TimeUnit.HOURS.toSeconds(12);
|
||
|
static final int[] BACKOFF_TIME_DURATIONS_IN_MINUTES = {2, 4, 8, 16, 32, 64, 128, 256};
|
||
|
|
||
|
private boolean isThrottleableServerError(int i) {
|
||
|
return i == 429 || i == 502 || i == 503 || i == 504;
|
||
|
}
|
||
|
|
||
|
public ConfigFetchHandler(FirebaseInstallationsApi firebaseInstallationsApi, AnalyticsConnector analyticsConnector, Executor executor, Clock clock, Random random, ConfigCacheClient configCacheClient, ConfigFetchHttpClient configFetchHttpClient, ConfigMetadataClient configMetadataClient, Map<String, String> map) {
|
||
|
this.firebaseInstallations = firebaseInstallationsApi;
|
||
|
this.analyticsConnector = analyticsConnector;
|
||
|
this.executor = executor;
|
||
|
this.clock = clock;
|
||
|
this.randomGenerator = random;
|
||
|
this.fetchedConfigsCache = configCacheClient;
|
||
|
this.frcBackendApiClient = configFetchHttpClient;
|
||
|
this.frcMetadata = configMetadataClient;
|
||
|
this.customHttpHeaders = map;
|
||
|
}
|
||
|
|
||
|
public Task<FetchResponse> fetch() {
|
||
|
return fetch(this.frcMetadata.getMinimumFetchIntervalInSeconds());
|
||
|
}
|
||
|
|
||
|
public Task<FetchResponse> fetch(long j) {
|
||
|
return this.fetchedConfigsCache.get().continueWithTask(this.executor, ConfigFetchHandler$$Lambda$1.lambdaFactory$(this, j));
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: private */
|
||
|
public Task<FetchResponse> fetchIfCacheExpiredAndNotThrottled(Task<ConfigContainer> task, long j) {
|
||
|
Task continueWithTask;
|
||
|
Date date = new Date(this.clock.currentTimeMillis());
|
||
|
if (task.isSuccessful() && areCachedFetchConfigsValid(j, date)) {
|
||
|
return Tasks.forResult(FetchResponse.forLocalStorageUsed(date));
|
||
|
}
|
||
|
Date backoffEndTimeInMillis = getBackoffEndTimeInMillis(date);
|
||
|
if (backoffEndTimeInMillis != null) {
|
||
|
continueWithTask = Tasks.forException(new FirebaseRemoteConfigFetchThrottledException(createThrottledMessage(backoffEndTimeInMillis.getTime() - date.getTime()), backoffEndTimeInMillis.getTime()));
|
||
|
} else {
|
||
|
Task<String> id = this.firebaseInstallations.getId();
|
||
|
Task<InstallationTokenResult> token = this.firebaseInstallations.getToken(false);
|
||
|
continueWithTask = Tasks.whenAllComplete((Task<?>[]) new Task[]{id, token}).continueWithTask(this.executor, ConfigFetchHandler$$Lambda$2.lambdaFactory$(this, id, token, date));
|
||
|
}
|
||
|
return continueWithTask.continueWithTask(this.executor, ConfigFetchHandler$$Lambda$3.lambdaFactory$(this, date));
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
public static /* synthetic */ Task lambda$fetchIfCacheExpiredAndNotThrottled$1(ConfigFetchHandler configFetchHandler, Task task, Task task2, Date date, Task task3) throws Exception {
|
||
|
if (!task.isSuccessful()) {
|
||
|
return Tasks.forException(new FirebaseRemoteConfigClientException("Firebase Installations failed to get installation ID for fetch.", task.getException()));
|
||
|
}
|
||
|
if (!task2.isSuccessful()) {
|
||
|
return Tasks.forException(new FirebaseRemoteConfigClientException("Firebase Installations failed to get installation auth token for fetch.", task2.getException()));
|
||
|
}
|
||
|
return configFetchHandler.fetchFromBackendAndCacheResponse((String) task.getResult(), ((InstallationTokenResult) task2.getResult()).getToken(), date);
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
public static /* synthetic */ Task lambda$fetchIfCacheExpiredAndNotThrottled$2(ConfigFetchHandler configFetchHandler, Date date, Task task) throws Exception {
|
||
|
configFetchHandler.updateLastFetchStatusAndTime(task, date);
|
||
|
return task;
|
||
|
}
|
||
|
|
||
|
private boolean areCachedFetchConfigsValid(long j, Date date) {
|
||
|
Date lastSuccessfulFetchTime = this.frcMetadata.getLastSuccessfulFetchTime();
|
||
|
if (lastSuccessfulFetchTime.equals(ConfigMetadataClient.LAST_FETCH_TIME_NO_FETCH_YET)) {
|
||
|
return false;
|
||
|
}
|
||
|
return date.before(new Date(lastSuccessfulFetchTime.getTime() + TimeUnit.SECONDS.toMillis(j)));
|
||
|
}
|
||
|
|
||
|
private Date getBackoffEndTimeInMillis(Date date) {
|
||
|
Date backoffEndTime = this.frcMetadata.getBackoffMetadata().getBackoffEndTime();
|
||
|
if (date.before(backoffEndTime)) {
|
||
|
return backoffEndTime;
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
private String createThrottledMessage(long j) {
|
||
|
return String.format("Fetch is throttled. Please wait before calling fetch again: %s", DateUtils.formatElapsedTime(TimeUnit.MILLISECONDS.toSeconds(j)));
|
||
|
}
|
||
|
|
||
|
private Task<FetchResponse> fetchFromBackendAndCacheResponse(String str, String str2, Date date) {
|
||
|
try {
|
||
|
FetchResponse fetchFromBackend = fetchFromBackend(str, str2, date);
|
||
|
if (fetchFromBackend.getStatus() != 0) {
|
||
|
return Tasks.forResult(fetchFromBackend);
|
||
|
}
|
||
|
return this.fetchedConfigsCache.put(fetchFromBackend.getFetchedConfigs()).onSuccessTask(this.executor, ConfigFetchHandler$$Lambda$4.lambdaFactory$(fetchFromBackend));
|
||
|
} catch (FirebaseRemoteConfigException e) {
|
||
|
return Tasks.forException(e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private FetchResponse fetchFromBackend(String str, String str2, Date date) throws FirebaseRemoteConfigException {
|
||
|
try {
|
||
|
FetchResponse fetch = this.frcBackendApiClient.fetch(this.frcBackendApiClient.createHttpURLConnection(), str, str2, getUserProperties(), this.frcMetadata.getLastFetchETag(), this.customHttpHeaders, date);
|
||
|
if (fetch.getLastFetchETag() != null) {
|
||
|
this.frcMetadata.setLastFetchETag(fetch.getLastFetchETag());
|
||
|
}
|
||
|
this.frcMetadata.resetBackoff();
|
||
|
return fetch;
|
||
|
} catch (FirebaseRemoteConfigServerException e) {
|
||
|
ConfigMetadataClient.BackoffMetadata updateAndReturnBackoffMetadata = updateAndReturnBackoffMetadata(e.getHttpStatusCode(), date);
|
||
|
if (shouldThrottle(updateAndReturnBackoffMetadata, e.getHttpStatusCode())) {
|
||
|
throw new FirebaseRemoteConfigFetchThrottledException(updateAndReturnBackoffMetadata.getBackoffEndTime().getTime());
|
||
|
}
|
||
|
throw createExceptionWithGenericMessage(e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private FirebaseRemoteConfigServerException createExceptionWithGenericMessage(FirebaseRemoteConfigServerException firebaseRemoteConfigServerException) throws FirebaseRemoteConfigClientException {
|
||
|
String str;
|
||
|
int httpStatusCode = firebaseRemoteConfigServerException.getHttpStatusCode();
|
||
|
if (httpStatusCode == 401) {
|
||
|
str = "The request did not have the required credentials. Please make sure your google-services.json is valid.";
|
||
|
} else if (httpStatusCode == 403) {
|
||
|
str = "The user is not authorized to access the project. Please make sure you are using the API key that corresponds to your Firebase project.";
|
||
|
} else {
|
||
|
if (httpStatusCode == 429) {
|
||
|
throw new FirebaseRemoteConfigClientException("The throttled response from the server was not handled correctly by the FRC SDK.");
|
||
|
}
|
||
|
if (httpStatusCode != 500) {
|
||
|
switch (httpStatusCode) {
|
||
|
case 502:
|
||
|
case 503:
|
||
|
case 504:
|
||
|
str = "The server is unavailable. Please try again later.";
|
||
|
break;
|
||
|
default:
|
||
|
str = "The server returned an unexpected error.";
|
||
|
break;
|
||
|
}
|
||
|
} else {
|
||
|
str = "There was an internal server error.";
|
||
|
}
|
||
|
}
|
||
|
return new FirebaseRemoteConfigServerException(firebaseRemoteConfigServerException.getHttpStatusCode(), "Fetch failed: ".concat(str), firebaseRemoteConfigServerException);
|
||
|
}
|
||
|
|
||
|
private ConfigMetadataClient.BackoffMetadata updateAndReturnBackoffMetadata(int i, Date date) {
|
||
|
if (isThrottleableServerError(i)) {
|
||
|
updateBackoffMetadataWithLastFailedFetchTime(date);
|
||
|
}
|
||
|
return this.frcMetadata.getBackoffMetadata();
|
||
|
}
|
||
|
|
||
|
private void updateBackoffMetadataWithLastFailedFetchTime(Date date) {
|
||
|
int numFailedFetches = this.frcMetadata.getBackoffMetadata().getNumFailedFetches() + 1;
|
||
|
this.frcMetadata.setBackoffMetadata(numFailedFetches, new Date(date.getTime() + getRandomizedBackoffDurationInMillis(numFailedFetches)));
|
||
|
}
|
||
|
|
||
|
private long getRandomizedBackoffDurationInMillis(int i) {
|
||
|
TimeUnit timeUnit = TimeUnit.MINUTES;
|
||
|
int[] iArr = BACKOFF_TIME_DURATIONS_IN_MINUTES;
|
||
|
return (timeUnit.toMillis(iArr[Math.min(i, iArr.length) - 1]) / 2) + this.randomGenerator.nextInt((int) r0);
|
||
|
}
|
||
|
|
||
|
private boolean shouldThrottle(ConfigMetadataClient.BackoffMetadata backoffMetadata, int i) {
|
||
|
return backoffMetadata.getNumFailedFetches() > 1 || i == 429;
|
||
|
}
|
||
|
|
||
|
private void updateLastFetchStatusAndTime(Task<FetchResponse> task, Date date) {
|
||
|
if (task.isSuccessful()) {
|
||
|
this.frcMetadata.updateLastFetchAsSuccessfulAt(date);
|
||
|
return;
|
||
|
}
|
||
|
Exception exception = task.getException();
|
||
|
if (exception == null) {
|
||
|
return;
|
||
|
}
|
||
|
if (exception instanceof FirebaseRemoteConfigFetchThrottledException) {
|
||
|
this.frcMetadata.updateLastFetchAsThrottled();
|
||
|
} else {
|
||
|
this.frcMetadata.updateLastFetchAsFailed();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private Map<String, String> getUserProperties() {
|
||
|
HashMap hashMap = new HashMap();
|
||
|
AnalyticsConnector analyticsConnector = this.analyticsConnector;
|
||
|
if (analyticsConnector == null) {
|
||
|
return hashMap;
|
||
|
}
|
||
|
for (Map.Entry<String, Object> entry : analyticsConnector.getUserProperties(false).entrySet()) {
|
||
|
hashMap.put(entry.getKey(), entry.getValue().toString());
|
||
|
}
|
||
|
return hashMap;
|
||
|
}
|
||
|
|
||
|
/* loaded from: classes2.dex */
|
||
|
public static class FetchResponse {
|
||
|
private final Date fetchTime;
|
||
|
private final ConfigContainer fetchedConfigs;
|
||
|
private final String lastFetchETag;
|
||
|
private final int status;
|
||
|
|
||
|
private FetchResponse(Date date, int i, ConfigContainer configContainer, String str) {
|
||
|
this.fetchTime = date;
|
||
|
this.status = i;
|
||
|
this.fetchedConfigs = configContainer;
|
||
|
this.lastFetchETag = str;
|
||
|
}
|
||
|
|
||
|
public static FetchResponse forBackendUpdatesFetched(ConfigContainer configContainer, String str) {
|
||
|
return new FetchResponse(configContainer.getFetchTime(), 0, configContainer, str);
|
||
|
}
|
||
|
|
||
|
public static FetchResponse forBackendHasNoUpdates(Date date) {
|
||
|
return new FetchResponse(date, 1, null, null);
|
||
|
}
|
||
|
|
||
|
public static FetchResponse forLocalStorageUsed(Date date) {
|
||
|
return new FetchResponse(date, 2, null, null);
|
||
|
}
|
||
|
|
||
|
int getStatus() {
|
||
|
return this.status;
|
||
|
}
|
||
|
|
||
|
String getLastFetchETag() {
|
||
|
return this.lastFetchETag;
|
||
|
}
|
||
|
|
||
|
public ConfigContainer getFetchedConfigs() {
|
||
|
return this.fetchedConfigs;
|
||
|
}
|
||
|
}
|
||
|
}
|