Skip to content

Commit 3dee255

Browse files
authored
Set TrafficStats tag in FirebaseInstallationServiceClient.createFirebaseInstallation (#2270)
* Set TrafficStats tag in FirebaseInstallationServiceClient.createFirebaseInstallation Fixes #2227 Tested: I verified that com.google.googlequicksearchbox doesn't have the violation anymore in the e2e test. Signed-off-by: Régis Décamps <[email protected]> * Add TrafficStats tags for Create isntallation, Delete installation and Generate Token. * Rename tags to be prefixed with TRAFFIC_STATS * Update copyright header for 2021.
1 parent 6fd2864 commit 3dee255

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

firebase-installations/src/main/java/com/google/firebase/installations/remote/FirebaseInstallationServiceClient.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019 Google LLC
1+
// Copyright 2019-2021 Google LLC
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020

2121
import android.content.Context;
2222
import android.content.pm.PackageManager;
23+
import android.net.TrafficStats;
2324
import android.text.TextUtils;
2425
import android.util.JsonReader;
2526
import android.util.Log;
@@ -56,6 +57,16 @@
5657
* @hide
5758
*/
5859
public class FirebaseInstallationServiceClient {
60+
61+
// TrafficStats tags must be kept in sync with java/com/google/android/gms/libs/punchclock
62+
private static final int TRAFFIC_STATS_FIREBASE_INSTALLATIONS_TAG = 0x00008000;
63+
private static final int TRAFFIC_STATS_CREATE_INSTALLATION_TAG =
64+
TRAFFIC_STATS_FIREBASE_INSTALLATIONS_TAG | 0x1;
65+
private static final int TRAFFIC_STATS_DELETE_INSTALLATION_TAG =
66+
TRAFFIC_STATS_FIREBASE_INSTALLATIONS_TAG | 0x2;
67+
private static final int TRAFFIC_STATS_GENERATE_AUTH_TOKEN_TAG =
68+
TRAFFIC_STATS_FIREBASE_INSTALLATIONS_TAG | 0x3;
69+
5970
private static final String FIREBASE_INSTALLATIONS_API_DOMAIN =
6071
"firebaseinstallations.googleapis.com";
6172
private static final String CREATE_REQUEST_RESOURCE_NAME_FORMAT = "projects/%s/installations";
@@ -153,6 +164,7 @@ public InstallationResponse createFirebaseInstallation(
153164
URL url = getFullyQualifiedRequestUri(resourceName);
154165
for (int retryCount = 0; retryCount <= MAX_RETRIES; retryCount++) {
155166

167+
TrafficStats.setThreadStatsTag(TRAFFIC_STATS_CREATE_INSTALLATION_TAG);
156168
HttpURLConnection httpURLConnection = openHttpURLConnection(url, apiKey);
157169

158170
try {
@@ -194,6 +206,7 @@ public InstallationResponse createFirebaseInstallation(
194206
continue;
195207
} finally {
196208
httpURLConnection.disconnect();
209+
TrafficStats.clearThreadStatsTag();
197210
}
198211
}
199212

@@ -309,6 +322,7 @@ public void deleteFirebaseInstallation(
309322

310323
int retryCount = 0;
311324
while (retryCount <= MAX_RETRIES) {
325+
TrafficStats.setThreadStatsTag(TRAFFIC_STATS_DELETE_INSTALLATION_TAG);
312326
HttpURLConnection httpURLConnection = openHttpURLConnection(url, apiKey);
313327
try {
314328
httpURLConnection.setRequestMethod("DELETE");
@@ -335,6 +349,7 @@ public void deleteFirebaseInstallation(
335349
retryCount++;
336350
} finally {
337351
httpURLConnection.disconnect();
352+
TrafficStats.clearThreadStatsTag();
338353
}
339354
}
340355

@@ -390,6 +405,7 @@ public TokenResult generateAuthToken(
390405
URL url = getFullyQualifiedRequestUri(resourceName);
391406
for (int retryCount = 0; retryCount <= MAX_RETRIES; retryCount++) {
392407

408+
TrafficStats.setThreadStatsTag(TRAFFIC_STATS_GENERATE_AUTH_TOKEN_TAG);
393409
HttpURLConnection httpURLConnection = openHttpURLConnection(url, apiKey);
394410
try {
395411
httpURLConnection.setRequestMethod("POST");
@@ -430,6 +446,7 @@ public TokenResult generateAuthToken(
430446
continue;
431447
} finally {
432448
httpURLConnection.disconnect();
449+
TrafficStats.clearThreadStatsTag();
433450
}
434451
}
435452
throw new FirebaseInstallationsException(
@@ -619,7 +636,8 @@ private static String readErrorResponse(HttpURLConnection conn) {
619636
response.append(input).append('\n');
620637
}
621638
return String.format(
622-
"Error when communicating with the Firebase Installations server API. HTTP response: [%d %s: %s]",
639+
"Error when communicating with the Firebase Installations server API. HTTP response: [%d"
640+
+ " %s: %s]",
623641
conn.getResponseCode(), conn.getResponseMessage(), response);
624642
} catch (IOException ignored) {
625643
return null;

0 commit comments

Comments
 (0)