Skip to content

Commit e94165b

Browse files
committed
Remove InternalKeys class
1 parent b03a676 commit e94165b

File tree

5 files changed

+39
-132
lines changed

5 files changed

+39
-132
lines changed

firebase-crashlytics/src/main/java/com/google/firebase/crashlytics/internal/common/CrashlyticsController.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ class CrashlyticsController {
6767
private final DataCollectionArbiter dataCollectionArbiter;
6868
private final CrashlyticsFileMarker crashMarker;
6969
private final UserMetadata userMetadata;
70-
private final InternalKeys internalKeys;
7170

7271
private final CrashlyticsBackgroundWorker backgroundWorker;
7372

@@ -110,7 +109,6 @@ class CrashlyticsController {
110109
CrashlyticsFileMarker crashMarker,
111110
AppData appData,
112111
UserMetadata userMetadata,
113-
InternalKeys internalKeys,
114112
LogFileManager logFileManager,
115113
LogFileManager.DirectoryProvider logFileDirectoryProvider,
116114
SessionReportingCoordinator sessionReportingCoordinator,
@@ -124,7 +122,6 @@ class CrashlyticsController {
124122
this.crashMarker = crashMarker;
125123
this.appData = appData;
126124
this.userMetadata = userMetadata;
127-
this.internalKeys = internalKeys;
128125
this.logFileManager = logFileManager;
129126
this.logFileDirectoryProvider = logFileDirectoryProvider;
130127
this.nativeComponent = nativeComponent;
@@ -449,7 +446,7 @@ void setCustomKeys(Map<String, String> keysAndValues) {
449446

450447
void setInternalKey(String key, String value) {
451448
try {
452-
internalKeys.setInternalKey(key, value);
449+
userMetadata.setInternalKey(key, value);
453450
} catch (IllegalArgumentException ex) {
454451
if (context != null && CommonUtils.isAppDebuggable(context)) {
455452
throw ex;
@@ -458,7 +455,7 @@ void setInternalKey(String key, String value) {
458455
return;
459456
}
460457
}
461-
cacheKeyData(internalKeys.getInternalKeys(), true);
458+
cacheKeyData(userMetadata.getInternalKeys(), true);
462459
}
463460

464461
/**

firebase-crashlytics/src/main/java/com/google/firebase/crashlytics/internal/common/CrashlyticsCore.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ public boolean onPreExecute(AppData appData, SettingsDataProvider settingsProvid
131131
initializationMarker = new CrashlyticsFileMarker(INITIALIZATION_MARKER_FILE_NAME, fileStore);
132132

133133
final UserMetadata userMetadata = new UserMetadata();
134-
final InternalKeys internalKeys = new InternalKeys();
135134
final LogFileDirectoryProvider logFileDirectoryProvider =
136135
new LogFileDirectoryProvider(fileStore);
137136
final LogFileManager logFileManager = new LogFileManager(context, logFileDirectoryProvider);
@@ -147,7 +146,6 @@ public boolean onPreExecute(AppData appData, SettingsDataProvider settingsProvid
147146
appData,
148147
logFileManager,
149148
userMetadata,
150-
internalKeys,
151149
stackTraceTrimmingStrategy,
152150
settingsProvider);
153151

@@ -161,7 +159,6 @@ public boolean onPreExecute(AppData appData, SettingsDataProvider settingsProvid
161159
crashMarker,
162160
appData,
163161
userMetadata,
164-
internalKeys,
165162
logFileManager,
166163
logFileDirectoryProvider,
167164
sessionReportingCoordinator,

firebase-crashlytics/src/main/java/com/google/firebase/crashlytics/internal/common/InternalKeys.java

Lines changed: 0 additions & 98 deletions
This file was deleted.

firebase-crashlytics/src/main/java/com/google/firebase/crashlytics/internal/common/SessionReportingCoordinator.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public static SessionReportingCoordinator create(
5555
AppData appData,
5656
LogFileManager logFileManager,
5757
UserMetadata userMetadata,
58-
InternalKeys internalKeys,
5958
StackTraceTrimmingStrategy stackTraceTrimmingStrategy,
6059
SettingsDataProvider settingsProvider) {
6160
final File rootFilesDirectory = new File(fileStore.getFilesDirPath());
@@ -66,29 +65,26 @@ public static SessionReportingCoordinator create(
6665
final DataTransportCrashlyticsReportSender reportSender =
6766
DataTransportCrashlyticsReportSender.create(context);
6867
return new SessionReportingCoordinator(
69-
dataCapture, reportPersistence, reportSender, logFileManager, userMetadata, internalKeys);
68+
dataCapture, reportPersistence, reportSender, logFileManager, userMetadata);
7069
}
7170

7271
private final CrashlyticsReportDataCapture dataCapture;
7372
private final CrashlyticsReportPersistence reportPersistence;
7473
private final DataTransportCrashlyticsReportSender reportsSender;
7574
private final LogFileManager logFileManager;
7675
private final UserMetadata reportMetadata;
77-
private final InternalKeys reportInternalKeys;
7876

7977
SessionReportingCoordinator(
8078
CrashlyticsReportDataCapture dataCapture,
8179
CrashlyticsReportPersistence reportPersistence,
8280
DataTransportCrashlyticsReportSender reportsSender,
8381
LogFileManager logFileManager,
84-
UserMetadata reportMetadata,
85-
InternalKeys reportInternalKeys) {
82+
UserMetadata reportMetadata) {
8683
this.dataCapture = dataCapture;
8784
this.reportPersistence = reportPersistence;
8885
this.reportsSender = reportsSender;
8986
this.logFileManager = logFileManager;
9087
this.reportMetadata = reportMetadata;
91-
this.reportInternalKeys = reportInternalKeys;
9288
}
9389

9490
@Override
@@ -225,7 +221,7 @@ private void persistEvent(
225221
final List<CustomAttribute> sortedCustomAttributes =
226222
getSortedCustomAttributes(reportMetadata.getCustomKeys());
227223
final List<CustomAttribute> sortedInternalKeys =
228-
getSortedCustomAttributes(reportInternalKeys.getInternalKeys());
224+
getSortedCustomAttributes(reportMetadata.getInternalKeys());
229225

230226
if (!sortedCustomAttributes.isEmpty()) {
231227
eventBuilder.setApp(

firebase-crashlytics/src/main/java/com/google/firebase/crashlytics/internal/common/UserMetadata.java

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@
2727
public class UserMetadata {
2828
static final int MAX_ATTRIBUTES = 64;
2929
static final int MAX_ATTRIBUTE_SIZE = 1024;
30+
static final int MAX_INTERNAL_KEY_SIZE = 8192;
3031

3132
private String userId = null;
3233
private final Map<String, String> attributes = new HashMap<>();
34+
private final Map<String, String> internalKeys = new HashMap<>();
3335

3436
public UserMetadata() {}
3537

@@ -39,7 +41,7 @@ public String getUserId() {
3941
}
4042

4143
public void setUserId(String identifier) {
42-
userId = sanitizeAttribute(identifier);
44+
userId = sanitizeAttribute(identifier, MAX_ATTRIBUTE_SIZE);
4345
}
4446

4547
@NonNull
@@ -51,18 +53,31 @@ public void setCustomKey(String key, String value) {
5153
setSyncCustomKeys(
5254
new HashMap<String, String>() {
5355
{
54-
put(sanitizeKey(key), sanitizeAttribute(value));
56+
put(key, value);
5557
}
56-
});
58+
}, attributes, MAX_ATTRIBUTE_SIZE);
5759
}
5860

5961
public void setCustomKeys(Map<String, String> keysAndValues) {
60-
setSyncCustomKeys(keysAndValues);
62+
setSyncCustomKeys(keysAndValues, attributes, MAX_ATTRIBUTE_SIZE);
6163
}
6264

63-
/** Gatekeeper function for access to attributes */
64-
private synchronized void setSyncCustomKeys(Map<String, String> keysAndValues) {
65-
// We want all access to the attributes hashmap to be locked so that there is no way to create
65+
public Map<String, String> getInternalKeys() {
66+
return Collections.unmodifiableMap(internalKeys);
67+
}
68+
69+
public void setInternalKey(String key, String value) {
70+
setSyncCustomKeys(
71+
new HashMap<String, String>() {
72+
{
73+
put(key, value);
74+
}
75+
}, internalKeys, MAX_INTERNAL_KEY_SIZE);
76+
}
77+
78+
/** Gatekeeper function for access to attributes or internalKeys */
79+
private synchronized void setSyncCustomKeys(Map<String, String> keysAndValues, Map<String, String> keys_map, int maxAttributeSize) {
80+
// We want all access to the keys_map hashmap to be locked so that there is no way to create
6681
// a race condition and add more than MAX_ATTRIBUTES keys.
6782

6883
// Update any existing keys first, then add any additional keys
@@ -71,42 +86,42 @@ private synchronized void setSyncCustomKeys(Map<String, String> keysAndValues) {
7186

7287
// Split into current and new keys
7388
for (Map.Entry<String, String> entry : keysAndValues.entrySet()) {
74-
String key = sanitizeKey(entry.getKey());
75-
String value = (entry.getValue() == null) ? "" : sanitizeAttribute(entry.getValue());
76-
if (attributes.containsKey(key)) {
89+
String key = sanitizeKey(entry.getKey(), maxAttributeSize);
90+
String value = (entry.getValue() == null) ? "" : sanitizeAttribute(entry.getValue(), maxAttributeSize);
91+
if (keys_map.containsKey(key)) {
7792
currentKeys.put(key, value);
7893
} else {
7994
newKeys.put(key, value);
8095
}
8196
}
8297

83-
attributes.putAll(currentKeys);
98+
keys_map.putAll(currentKeys);
8499

85100
// Add new keys if there is space
86-
if (attributes.size() + newKeys.size() > MAX_ATTRIBUTES) {
87-
int keySlotsLeft = MAX_ATTRIBUTES - attributes.size();
101+
if (keys_map.size() + newKeys.size() > MAX_ATTRIBUTES) {
102+
int keySlotsLeft = MAX_ATTRIBUTES - keys_map.size();
88103
Logger.getLogger()
89104
.v("Exceeded maximum number of custom attributes (" + MAX_ATTRIBUTES + ").");
90105
List<String> newKeyList = new ArrayList<>(newKeys.keySet());
91106
newKeys.keySet().retainAll(newKeyList.subList(0, keySlotsLeft));
92107
}
93-
attributes.putAll(newKeys);
108+
keys_map.putAll(newKeys);
94109
}
95110

96111
/** Checks that the key is not null then sanitizes it. */
97-
private static String sanitizeKey(String key) {
112+
private static String sanitizeKey(String key, int maxAttributeSize) {
98113
if (key == null) {
99114
throw new IllegalArgumentException("Custom attribute key must not be null.");
100115
}
101-
return sanitizeAttribute(key);
116+
return sanitizeAttribute(key, maxAttributeSize);
102117
}
103118

104119
/** Trims the string and truncates it to MAX_ATTRIBUTE_SIZE. */
105-
private static String sanitizeAttribute(String input) {
120+
private static String sanitizeAttribute(String input, int maxAttributeSize) {
106121
if (input != null) {
107122
input = input.trim();
108-
if (input.length() > MAX_ATTRIBUTE_SIZE) {
109-
input = input.substring(0, MAX_ATTRIBUTE_SIZE);
123+
if (input.length() > maxAttributeSize) {
124+
input = input.substring(0, maxAttributeSize);
110125
}
111126
}
112127
return input;

0 commit comments

Comments
 (0)