Skip to content

Commit 2f3dab0

Browse files
mrwillis21jakeouellette
authored andcommitted
WIP
1 parent 7b0ce9c commit 2f3dab0

File tree

5 files changed

+232
-88
lines changed

5 files changed

+232
-88
lines changed

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

Lines changed: 4 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,96 +1104,12 @@ public boolean accept(File dir, String filename) {
11041104
// endregion
11051105

11061106
private void finalizePreviousNativeSession(String previousSessionId) throws IOException {
1107-
Logger.getLogger().d("Finalizing native report for session " + previousSessionId);
1108-
NativeSessionFileProvider nativeSessionFileProvider =
1109-
nativeComponent.getSessionFileProvider(previousSessionId);
1110-
1111-
final File minidump = nativeSessionFileProvider.getMinidumpFile();
1112-
final File binaryImages = nativeSessionFileProvider.getBinaryImagesFile();
1113-
final File metadata = nativeSessionFileProvider.getMetadataFile();
1114-
final File sessionFile = nativeSessionFileProvider.getSessionFile();
1115-
final File sessionApp = nativeSessionFileProvider.getAppFile();
1116-
final File sessionDevice = nativeSessionFileProvider.getDeviceFile();
1117-
final File sessionOs = nativeSessionFileProvider.getOsFile();
1118-
1119-
if (minidump == null || !minidump.exists()) {
1120-
Logger.getLogger().w("No minidump data found for session " + previousSessionId);
1121-
return;
1122-
}
1123-
1124-
final File filesDir = getFilesDir();
1125-
final MetaDataStore metaDataStore = new MetaDataStore(filesDir);
1126-
final File sessionUser = metaDataStore.getUserDataFileForSession(previousSessionId);
1127-
final File sessionKeys = metaDataStore.getKeysFileForSession(previousSessionId);
1128-
1129-
final LogFileManager previousSessionLogManager =
1130-
new LogFileManager(getContext(), logFileDirectoryProvider, previousSessionId);
1131-
final byte[] logs = previousSessionLogManager.getBytesForLog();
1132-
1133-
final File nativeSessionDirectory = new File(getNativeSessionFilesDir(), previousSessionId);
1134-
1135-
if (!nativeSessionDirectory.mkdirs()) {
1136-
Logger.getLogger().d("Couldn't create native sessions directory");
1137-
return;
1138-
}
1139-
1140-
gzipFile(minidump, new File(nativeSessionDirectory, "minidump"));
1141-
gzipIfNotEmpty(
1142-
NativeFileUtils.binaryImagesJsonFromMapsFile(binaryImages, context),
1143-
new File(nativeSessionDirectory, "binaryImages"));
1144-
gzipFile(metadata, new File(nativeSessionDirectory, "metadata"));
1145-
gzipFile(sessionFile, new File(nativeSessionDirectory, "session"));
1146-
gzipFile(sessionApp, new File(nativeSessionDirectory, "app"));
1147-
gzipFile(sessionDevice, new File(nativeSessionDirectory, "device"));
1148-
gzipFile(sessionOs, new File(nativeSessionDirectory, "os"));
1149-
gzipFile(sessionUser, new File(nativeSessionDirectory, "user"));
1150-
gzipFile(sessionKeys, new File(nativeSessionDirectory, "keys"));
1151-
gzipIfNotEmpty(logs, new File(nativeSessionDirectory, "logs"));
1152-
1153-
previousSessionLogManager.clearLog();
1154-
}
1155-
1156-
// TODO: Maybe make this a separate collaborator/serializer
1157-
private static void gzipFile(@NonNull File input, @NonNull File output) throws IOException {
1158-
if (!input.exists() || !input.isFile()) {
1159-
return;
1160-
}
1161-
byte[] buffer = new byte[1024];
1162-
FileInputStream fis = null;
1163-
GZIPOutputStream gos = null;
1164-
try {
1165-
fis = new FileInputStream(input);
1166-
gos = new GZIPOutputStream(new FileOutputStream(output));
1107+
// TODO: Provide a set of inputstreams?
1108+
// reportingCoordinator.persistNativeEvent(nativeSessionDirectory);
11671109

1168-
int read;
11691110

1170-
while ((read = fis.read(buffer)) > 0) {
1171-
gos.write(buffer, 0, read);
1172-
}
1173-
1174-
gos.finish();
1175-
} finally {
1176-
CommonUtils.closeQuietly(fis);
1177-
CommonUtils.closeQuietly(gos);
1178-
}
1179-
}
1180-
1181-
private static void gzipIfNotEmpty(@Nullable byte[] content, @NonNull File path)
1182-
throws IOException {
1183-
if (content != null && content.length > 0) {
1184-
gzip(content, path);
1185-
}
1186-
}
1187-
1188-
private static void gzip(@NonNull byte[] bytes, @NonNull File path) throws IOException {
1189-
GZIPOutputStream gos = null;
1190-
try {
1191-
gos = new GZIPOutputStream(new FileOutputStream(path));
1192-
gos.write(bytes, 0, bytes.length);
1193-
gos.finish();
1194-
} finally {
1195-
CommonUtils.closeQuietly(gos);
1196-
}
1111+
// TODO: Do this in the gzipfilenativething
1112+
// previousSessionLogManager.clearLog();
11971113
}
11981114

11991115
/** Removes dashes in the Crashlytics session identifier to conform to Firebase constraints. */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2020 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.crashlytics.internal.common;
16+
17+
import com.google.firebase.crashlytics.core.MetaDataStore;
18+
import com.google.firebase.crashlytics.internal.CrashlyticsNativeComponent;
19+
import com.google.firebase.crashlytics.internal.Logger;
20+
import com.google.firebase.crashlytics.internal.NativeSessionFileProvider;
21+
import com.google.firebase.crashlytics.internal.log.LogFileManager;
22+
import com.google.firebase.crashlytics.internal.model.CrashlyticsReport;
23+
import java.io.File;
24+
import java.io.IOException;
25+
26+
class CrashlyticsNativeReportGenerator implements NativeComponentSessionHandler<CrashlyticsReport> {
27+
28+
private CrashlyticsNativeComponent nativeComponent;
29+
30+
@Override
31+
public CrashlyticsReport handlePreviousNativeSession(String sessionId) throws IOException {
32+
Logger.getLogger().d("Finalizing native report for session " + sessionId);
33+
NativeSessionFileProvider nativeSessionFileProvider =
34+
nativeComponent.getSessionFileProvider(sessionId);
35+
36+
final File minidump = nativeSessionFileProvider.getMinidumpFile();
37+
final File binaryImages = nativeSessionFileProvider.getBinaryImagesFile();
38+
final File metadata = nativeSessionFileProvider.getMetadataFile();
39+
final File sessionFile = nativeSessionFileProvider.getSessionFile();
40+
final File sessionApp = nativeSessionFileProvider.getAppFile();
41+
final File sessionDevice = nativeSessionFileProvider.getDeviceFile();
42+
final File sessionOs = nativeSessionFileProvider.getOsFile();
43+
44+
if (minidump == null || !minidump.exists()) {
45+
Logger.getLogger().w("No minidump data found for session " + sessionId);
46+
return null;
47+
}
48+
49+
final File filesDir = fileStore.getFilesDir();
50+
final MetaDataStore metaDataStore = new MetaDataStore(filesDir);
51+
final File sessionUser = metaDataStore.getUserDataFileForSession(sessionId);
52+
final File sessionKeys = metaDataStore.getKeysFileForSession(sessionId);
53+
54+
final LogFileManager previousSessionLogManager =
55+
new LogFileManager(getContext(), logFileDirectoryProvider, sessionId);
56+
57+
return null;
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// Copyright 2020 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.crashlytics.internal.common;
16+
17+
import androidx.annotation.NonNull;
18+
import androidx.annotation.Nullable;
19+
import com.google.firebase.crashlytics.core.MetaDataStore;
20+
import com.google.firebase.crashlytics.internal.CrashlyticsNativeComponent;
21+
import com.google.firebase.crashlytics.internal.Logger;
22+
import com.google.firebase.crashlytics.internal.NativeSessionFileProvider;
23+
import com.google.firebase.crashlytics.internal.log.LogFileManager;
24+
import com.google.firebase.crashlytics.internal.ndk.NativeFileUtils;
25+
import com.google.firebase.crashlytics.internal.persistence.FileStore;
26+
import java.io.File;
27+
import java.io.FileInputStream;
28+
import java.io.FileOutputStream;
29+
import java.io.IOException;
30+
import java.util.zip.GZIPOutputStream;
31+
32+
class GzipFileNativeSessionHandler implements NativeComponentSessionHandler<Void> {
33+
34+
private CrashlyticsNativeComponent nativeComponent;
35+
private FileStore fileStore;
36+
37+
@Override
38+
public Void handlePreviousNativeSession(String sessionId) throws IOException {
39+
Logger.getLogger().d("Finalizing native report for session " + sessionId);
40+
NativeSessionFileProvider nativeSessionFileProvider =
41+
nativeComponent.getSessionFileProvider(sessionId);
42+
43+
final File minidump = nativeSessionFileProvider.getMinidumpFile();
44+
final File binaryImages = nativeSessionFileProvider.getBinaryImagesFile();
45+
final File metadata = nativeSessionFileProvider.getMetadataFile();
46+
final File sessionFile = nativeSessionFileProvider.getSessionFile();
47+
final File sessionApp = nativeSessionFileProvider.getAppFile();
48+
final File sessionDevice = nativeSessionFileProvider.getDeviceFile();
49+
final File sessionOs = nativeSessionFileProvider.getOsFile();
50+
51+
if (minidump == null || !minidump.exists()) {
52+
Logger.getLogger().w("No minidump data found for session " + sessionId);
53+
return null;
54+
}
55+
56+
final File filesDir = fileStore.getFilesDir();
57+
final MetaDataStore metaDataStore = new MetaDataStore(filesDir);
58+
final File sessionUser = metaDataStore.getUserDataFileForSession(sessionId);
59+
final File sessionKeys = metaDataStore.getKeysFileForSession(sessionId);
60+
61+
final LogFileManager previousSessionLogManager =
62+
new LogFileManager(getContext(), logFileDirectoryProvider, sessionId);
63+
final byte[] logs = previousSessionLogManager.getBytesForLog();
64+
65+
final File nativeSessionDirectory = new File(getNativeSessionFilesDir(), sessionId);
66+
67+
if (!nativeSessionDirectory.mkdirs()) {
68+
Logger.getLogger().d("Couldn't create native sessions directory");
69+
return;
70+
}
71+
72+
gzipFile(minidump, new File(nativeSessionDirectory, "minidump"));
73+
gzipIfNotEmpty(
74+
NativeFileUtils.binaryImagesJsonFromMapsFile(binaryImages, context),
75+
new File(nativeSessionDirectory, "binaryImages"));
76+
gzipFile(metadata, new File(nativeSessionDirectory, "metadata"));
77+
gzipFile(sessionFile, new File(nativeSessionDirectory, "session"));
78+
gzipFile(sessionApp, new File(nativeSessionDirectory, "app"));
79+
gzipFile(sessionDevice, new File(nativeSessionDirectory, "device"));
80+
gzipFile(sessionOs, new File(nativeSessionDirectory, "os"));
81+
gzipFile(sessionUser, new File(nativeSessionDirectory, "user"));
82+
gzipFile(sessionKeys, new File(nativeSessionDirectory, "keys"));
83+
gzipIfNotEmpty(logs, new File(nativeSessionDirectory, "logs"));
84+
85+
return null;
86+
}
87+
88+
private static void gzipFile(@NonNull File input, @NonNull File output) throws IOException {
89+
if (!input.exists() || !input.isFile()) {
90+
return;
91+
}
92+
byte[] buffer = new byte[1024];
93+
FileInputStream fis = null;
94+
GZIPOutputStream gos = null;
95+
try {
96+
fis = new FileInputStream(input);
97+
gos = new GZIPOutputStream(new FileOutputStream(output));
98+
99+
int read;
100+
101+
while ((read = fis.read(buffer)) > 0) {
102+
gos.write(buffer, 0, read);
103+
}
104+
105+
gos.finish();
106+
} finally {
107+
CommonUtils.closeQuietly(fis);
108+
CommonUtils.closeQuietly(gos);
109+
}
110+
}
111+
112+
private static void gzipIfNotEmpty(@Nullable byte[] content, @NonNull File path)
113+
throws IOException {
114+
if (content != null && content.length > 0) {
115+
gzip(content, path);
116+
}
117+
}
118+
119+
private static void gzip(@NonNull byte[] bytes, @NonNull File path) throws IOException {
120+
GZIPOutputStream gos = null;
121+
try {
122+
gos = new GZIPOutputStream(new FileOutputStream(path));
123+
gos.write(bytes, 0, bytes.length);
124+
gos.finish();
125+
} finally {
126+
CommonUtils.closeQuietly(gos);
127+
}
128+
}
129+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2020 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.crashlytics.internal.common;
16+
17+
import java.io.IOException;
18+
19+
interface NativeComponentSessionHandler<T> {
20+
T handlePreviousNativeSession(String sessionId) throws IOException;
21+
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
package com.google.firebase.crashlytics.internal.common;
1616

1717
import android.content.Context;
18+
import androidx.annotation.NonNull;
1819
import com.google.android.gms.tasks.Task;
1920
import com.google.firebase.crashlytics.internal.Logger;
2021
import com.google.firebase.crashlytics.internal.log.LogFileManager;
2122
import com.google.firebase.crashlytics.internal.model.CrashlyticsReport;
2223
import com.google.firebase.crashlytics.internal.model.CrashlyticsReport.CustomAttribute;
24+
import com.google.firebase.crashlytics.internal.model.CrashlyticsReport.FilesPayload;
2325
import com.google.firebase.crashlytics.internal.model.ImmutableList;
2426
import com.google.firebase.crashlytics.internal.persistence.CrashlyticsReportPersistence;
2527
import com.google.firebase.crashlytics.internal.persistence.FileStore;
@@ -130,6 +132,23 @@ public void persistNonFatalEvent(Throwable event, Thread thread, long timestamp)
130132
persistEvent(event, thread, EVENT_TYPE_LOGGED, timestamp, false);
131133
}
132134

135+
public void persistNativeEvent(@NonNull File nativeEventFilesDir) {
136+
// TODO: Consider passing some sort of transformer instead of the raw files dir.
137+
/*gzipFile(minidump, new File(nativeSessionDirectory, "minidump"));
138+
gzipIfNotEmpty(
139+
NativeFileUtils.binaryImagesJsonFromMapsFile(binaryImages, context),
140+
new File(nativeSessionDirectory, "binaryImages"));
141+
gzipFile(metadata, new File(nativeSessionDirectory, "metadata"));
142+
gzipFile(sessionFile, new File(nativeSessionDirectory, "session"));
143+
gzipFile(sessionApp, new File(nativeSessionDirectory, "app"));
144+
gzipFile(sessionDevice, new File(nativeSessionDirectory, "device"));
145+
gzipFile(sessionOs, new File(nativeSessionDirectory, "os"));
146+
gzipFile(sessionUser, new File(nativeSessionDirectory, "user"));
147+
gzipFile(sessionKeys, new File(nativeSessionDirectory, "keys"));
148+
gzipIfNotEmpty(logs, new File(nativeSessionDirectory, "logs"));*/
149+
final FilesPayload.File minidump = FilesPayload.File.builder().setFilename("minidump_file").setContents(new byte[0]).build();
150+
}
151+
133152
public void persistUserId() {
134153
reportPersistence.persistUserIdForSession(reportMetadata.getUserId(), currentSessionId);
135154
}

0 commit comments

Comments
 (0)