Skip to content

Commit 23bf8e2

Browse files
committed
More stuff
1 parent 2a40a3d commit 23bf8e2

File tree

7 files changed

+93
-74
lines changed

7 files changed

+93
-74
lines changed

firebase-crashlytics/src/androidTest/java/com/google/firebase/crashlytics/internal/common/MetaDataStoreTest.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -224,19 +224,19 @@ public void testWriteKeys_readDifferentSession() {
224224

225225
public void testWriteKeys_readSeparateFromUser() {
226226
final Map<String, String> keys =
227-
new HashMap<String, String>() {
228-
{
229-
put(KEY_1, VALUE_1);
230-
}
231-
};
227+
new HashMap<String, String>() {
228+
{
229+
put(KEY_1, VALUE_1);
230+
}
231+
};
232232

233233
final Map<String, String> internalKeys =
234-
new HashMap<String, String>() {
235-
{
236-
put(KEY_2, VALUE_2);
237-
put(KEY_3, VALUE_3);
238-
}
239-
};
234+
new HashMap<String, String>() {
235+
{
236+
put(KEY_2, VALUE_2);
237+
put(KEY_3, VALUE_3);
238+
}
239+
};
240240

241241
storeUnderTest.writeKeyData(SESSION_ID_1, keys);
242242
storeUnderTest.writeKeyData(SESSION_ID_1, internalKeys, /*isInternal=*/ true);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ public boolean onPreExecute(AppData appData, SettingsDataProvider settingsProvid
147147
appData,
148148
logFileManager,
149149
userMetadata,
150+
internalKeys,
150151
stackTraceTrimmingStrategy,
151152
settingsProvider);
152153

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

Lines changed: 57 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package com.google.firebase.crashlytics.internal.common;
1616

1717
import androidx.annotation.NonNull;
18-
1918
import com.google.firebase.crashlytics.internal.Logger;
2019
import java.util.ArrayList;
2120
import java.util.Collections;
@@ -25,76 +24,75 @@
2524

2625
/** Handles custom attributes internal to the SDK, eg. for custom Unity metadata. */
2726
public class InternalKeys {
28-
static final int MAX_INTERNAL_KEYS = 64;
29-
static final int MAX_INTERNAL_KEY_SIZE = 8192;
27+
static final int MAX_INTERNAL_KEYS = 64;
28+
static final int MAX_INTERNAL_KEY_SIZE = 8192;
3029

31-
private final Map<String, String> internal_keys = new HashMap<>();
30+
private final Map<String, String> internal_keys = new HashMap<>();
3231

33-
public InternalKeys() {}
32+
public InternalKeys() {}
3433

35-
@NonNull
36-
public Map<String, String> getInternalKeys() {
37-
return Collections.unmodifiableMap(internal_keys);
38-
}
34+
@NonNull
35+
public Map<String, String> getInternalKeys() {
36+
return Collections.unmodifiableMap(internal_keys);
37+
}
3938

40-
public void setInternalKey(String key, String value) {
41-
setSyncInternalKeys(
42-
new HashMap<String, String>() {
43-
{
44-
put(sanitizeKey(key), sanitizeAttribute(value));
45-
}
46-
});
47-
}
39+
public void setInternalKey(String key, String value) {
40+
setSyncInternalKeys(
41+
new HashMap<String, String>() {
42+
{
43+
put(sanitizeKey(key), sanitizeAttribute(value));
44+
}
45+
});
46+
}
4847

49-
/** Gatekeeper function for access to attributes */
50-
private synchronized void setSyncInternalKeys(Map<String, String> keysAndValues) {
51-
// We want all access to the attributes hashmap to be locked so that there is no way to create
52-
// a race condition and add more than MAX_ATTRIBUTES keys.
48+
/** Gatekeeper function for access to attributes */
49+
private synchronized void setSyncInternalKeys(Map<String, String> keysAndValues) {
50+
// We want all access to the attributes hashmap to be locked so that there is no way to create
51+
// a race condition and add more than MAX_ATTRIBUTES keys.
5352

54-
// Update any existing keys first, then add any additional keys
55-
Map<String, String> currentKeys = new HashMap<String, String>();
56-
Map<String, String> newKeys = new HashMap<String, String>();
53+
// Update any existing keys first, then add any additional keys
54+
Map<String, String> currentKeys = new HashMap<String, String>();
55+
Map<String, String> newKeys = new HashMap<String, String>();
5756

58-
// Split into current and new keys
59-
for (Map.Entry<String, String> entry : keysAndValues.entrySet()) {
60-
String key = sanitizeKey(entry.getKey());
61-
String value = (entry.getValue() == null) ? "" : sanitizeAttribute(entry.getValue());
62-
if (internal_keys.containsKey(key)) {
63-
currentKeys.put(key, value);
64-
} else {
65-
newKeys.put(key, value);
66-
}
67-
}
57+
// Split into current and new keys
58+
for (Map.Entry<String, String> entry : keysAndValues.entrySet()) {
59+
String key = sanitizeKey(entry.getKey());
60+
String value = (entry.getValue() == null) ? "" : sanitizeAttribute(entry.getValue());
61+
if (internal_keys.containsKey(key)) {
62+
currentKeys.put(key, value);
63+
} else {
64+
newKeys.put(key, value);
65+
}
66+
}
6867

69-
internal_keys.putAll(currentKeys);
68+
internal_keys.putAll(currentKeys);
7069

71-
// Add new keys if there is space
72-
if (internal_keys.size() + newKeys.size() > MAX_INTERNAL_KEYS) {
73-
int keySlotsLeft = MAX_INTERNAL_KEYS - internal_keys.size();
74-
Logger.getLogger()
75-
.v("Exceeded maximum number of internal keys (" + MAX_INTERNAL_KEYS + ").");
76-
List<String> newKeyList = new ArrayList<>(newKeys.keySet());
77-
newKeys.keySet().retainAll(newKeyList.subList(0, keySlotsLeft));
78-
}
79-
internal_keys.putAll(newKeys);
70+
// Add new keys if there is space
71+
if (internal_keys.size() + newKeys.size() > MAX_INTERNAL_KEYS) {
72+
int keySlotsLeft = MAX_INTERNAL_KEYS - internal_keys.size();
73+
Logger.getLogger().v("Exceeded maximum number of internal keys (" + MAX_INTERNAL_KEYS + ").");
74+
List<String> newKeyList = new ArrayList<>(newKeys.keySet());
75+
newKeys.keySet().retainAll(newKeyList.subList(0, keySlotsLeft));
8076
}
77+
internal_keys.putAll(newKeys);
78+
}
8179

82-
/** Checks that the key is not null then sanitizes it. */
83-
private static String sanitizeKey(String key) {
84-
if (key == null) {
85-
throw new IllegalArgumentException("Custom attribute key must not be null.");
86-
}
87-
return sanitizeAttribute(key);
80+
/** Checks that the key is not null then sanitizes it. */
81+
private static String sanitizeKey(String key) {
82+
if (key == null) {
83+
throw new IllegalArgumentException("Custom attribute key must not be null.");
8884
}
85+
return sanitizeAttribute(key);
86+
}
8987

90-
/** Trims the string and truncates it to MAX_ATTRIBUTE_SIZE. */
91-
private static String sanitizeAttribute(String input) {
92-
if (input != null) {
93-
input = input.trim();
94-
if (input.length() > MAX_INTERNAL_KEY_SIZE) {
95-
input = input.substring(0, MAX_INTERNAL_KEY_SIZE);
96-
}
97-
}
98-
return input;
88+
/** Trims the string and truncates it to MAX_ATTRIBUTE_SIZE. */
89+
private static String sanitizeAttribute(String input) {
90+
if (input != null) {
91+
input = input.trim();
92+
if (input.length() > MAX_INTERNAL_KEY_SIZE) {
93+
input = input.substring(0, MAX_INTERNAL_KEY_SIZE);
94+
}
9995
}
96+
return input;
97+
}
10098
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,14 @@ public UserMetadata readUserData(String sessionId) {
8484
}
8585
return new UserMetadata();
8686
}
87+
8788
public void writeKeyData(String sessionId, Map<String, String> keyData) {
8889
writeKeyData(sessionId, keyData, false);
8990
}
9091

9192
void writeKeyData(String sessionId, Map<String, String> keyData, boolean isInternal) {
92-
final File f = isInternal ? getInternalKeysFileForSession(sessionId) : getKeysFileForSession(sessionId);
93+
final File f =
94+
isInternal ? getInternalKeysFileForSession(sessionId) : getKeysFileForSession(sessionId);
9395
Writer writer = null;
9496
try {
9597
final String keyDataString = keysDataToJson(keyData);
@@ -108,7 +110,8 @@ public Map<String, String> readKeyData(String sessionId) {
108110
}
109111

110112
Map<String, String> readKeyData(String sessionId, boolean isInternal) {
111-
final File f = isInternal ? getInternalKeysFileForSession(sessionId) : getKeysFileForSession(sessionId);
113+
final File f =
114+
isInternal ? getInternalKeysFileForSession(sessionId) : getKeysFileForSession(sessionId);
112115
if (!f.exists()) {
113116
return Collections.emptyMap();
114117
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public static SessionReportingCoordinator create(
5555
AppData appData,
5656
LogFileManager logFileManager,
5757
UserMetadata userMetadata,
58+
InternalKeys internalKeys,
5859
StackTraceTrimmingStrategy stackTraceTrimmingStrategy,
5960
SettingsDataProvider settingsProvider) {
6061
final File rootFilesDirectory = new File(fileStore.getFilesDirPath());
@@ -65,26 +66,29 @@ public static SessionReportingCoordinator create(
6566
final DataTransportCrashlyticsReportSender reportSender =
6667
DataTransportCrashlyticsReportSender.create(context);
6768
return new SessionReportingCoordinator(
68-
dataCapture, reportPersistence, reportSender, logFileManager, userMetadata);
69+
dataCapture, reportPersistence, reportSender, logFileManager, userMetadata, internalKeys);
6970
}
7071

7172
private final CrashlyticsReportDataCapture dataCapture;
7273
private final CrashlyticsReportPersistence reportPersistence;
7374
private final DataTransportCrashlyticsReportSender reportsSender;
7475
private final LogFileManager logFileManager;
7576
private final UserMetadata reportMetadata;
77+
private final InternalKeys reportInternalKeys;
7678

7779
SessionReportingCoordinator(
7880
CrashlyticsReportDataCapture dataCapture,
7981
CrashlyticsReportPersistence reportPersistence,
8082
DataTransportCrashlyticsReportSender reportsSender,
8183
LogFileManager logFileManager,
82-
UserMetadata reportMetadata) {
84+
UserMetadata reportMetadata,
85+
InternalKeys reportInternalKeys) {
8386
this.dataCapture = dataCapture;
8487
this.reportPersistence = reportPersistence;
8588
this.reportsSender = reportsSender;
8689
this.logFileManager = logFileManager;
8790
this.reportMetadata = reportMetadata;
91+
this.reportInternalKeys = reportInternalKeys;
8892
}
8993

9094
@Override
@@ -220,13 +224,16 @@ private void persistEvent(
220224

221225
final List<CustomAttribute> sortedCustomAttributes =
222226
getSortedCustomAttributes(reportMetadata.getCustomKeys());
227+
final List<CustomAttribute> sortedInternalKeys =
228+
getSortedCustomAttributes(reportInternalKeys.getInternalKeys());
223229

224230
if (!sortedCustomAttributes.isEmpty()) {
225231
eventBuilder.setApp(
226232
capturedEvent
227233
.getApp()
228234
.toBuilder()
229235
.setCustomAttributes(ImmutableList.from(sortedCustomAttributes))
236+
.setInternalKeys(ImmutableList.from(sortedInternalKeys))
230237
.build());
231238
}
232239

firebase-crashlytics/src/main/java/com/google/firebase/crashlytics/internal/model/CrashlyticsReport.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,9 @@ public static Builder builder() {
637637
@Nullable
638638
public abstract ImmutableList<CustomAttribute> getCustomAttributes();
639639

640+
@Nullable
641+
public abstract ImmutableList<CustomAttribute> getInternalKeys();
642+
640643
@Nullable
641644
public abstract Boolean getBackground();
642645

@@ -914,6 +917,9 @@ public abstract static class Builder {
914917
public abstract Builder setCustomAttributes(
915918
@NonNull ImmutableList<CustomAttribute> value);
916919

920+
@NonNull
921+
public abstract Builder setInternalKeys(@NonNull ImmutableList<CustomAttribute> value);
922+
917923
@NonNull
918924
public abstract Builder setBackground(@Nullable Boolean value);
919925

firebase-crashlytics/src/main/java/com/google/firebase/crashlytics/internal/model/serialization/CrashlyticsReportJsonTransform.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,10 @@ private static Event.Application parseEventApp(@NonNull JsonReader jsonReader)
403403
builder.setCustomAttributes(
404404
parseArray(jsonReader, CrashlyticsReportJsonTransform::parseCustomAttribute));
405405
break;
406+
case "internalKeys":
407+
builder.setInternalKeys(
408+
parseArray(jsonReader, CrashlyticsReportJsonTransform::parseCustomAttribute));
409+
break;
406410
default:
407411
jsonReader.skipValue();
408412
break;

0 commit comments

Comments
 (0)