Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Handle potential exception capturing snapshots (#2386)
Browse files Browse the repository at this point in the history
* Handle potential exception when GeckoSession.capture() is called

* Reset the  mCaptureOnPageStop member when switching sessions
  • Loading branch information
MortimerGoro authored and keianhzo committed Nov 27, 2019
1 parent d6f46c4 commit f567fff
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -474,15 +474,25 @@ public void captureBitmap() {
if (mState.mDisplay == null || !mFirstContentfulPaint) {
return;
}
mState.mDisplay.screenshot().aspectPreservingSize(500).capture().then(bitmap -> {
if (bitmap != null) {
BitmapCache.getInstance(mContext).addBitmap(getId(), bitmap);
for (BitmapChangedListener listener: mBitmapChangedListeners) {
listener.onBitmapChanged(Session.this, bitmap);
try {
mState.mDisplay.screenshot().aspectPreservingSize(500).capture().then(bitmap -> {
if (bitmap != null) {
BitmapCache.getInstance(mContext).addBitmap(getId(), bitmap);
for (BitmapChangedListener listener: mBitmapChangedListeners) {
listener.onBitmapChanged(Session.this, bitmap);
}
}
}
return null;
});
return null;
}).exceptionally(throwable -> {
Log.e(LOGTAG, "Error capturing session bitmap");
throwable.printStackTrace();
return null;
});
} catch (Exception ex) {
Log.e(LOGTAG, "Error capturing session bitmap");
ex.printStackTrace();
}

}

public CompletableFuture<Void> captureBackgroundBitmap(int displayWidth, int displayHeight) {
Expand All @@ -497,19 +507,38 @@ public CompletableFuture<Void> captureBackgroundBitmap(int displayWidth, int dis
CompletableFuture<Void> result = new CompletableFuture<>();
GeckoDisplay display = mState.mSession.acquireDisplay();
display.surfaceChanged(captureSurface, displayWidth, displayHeight);
display.screenshot().aspectPreservingSize(500).capture().then(bitmap -> {
if (bitmap != null) {
BitmapCache.getInstance(mContext).addBitmap(getId(), bitmap);
for (BitmapChangedListener listener: mBitmapChangedListeners) {
listener.onBitmapChanged(Session.this, bitmap);
}
}

Runnable cleanResources = () -> {
display.surfaceDestroyed();
mState.mSession.releaseDisplay(display);
BitmapCache.getInstance(mContext).releaseCaptureSurface();
};

try {
display.screenshot().aspectPreservingSize(500).capture().then(bitmap -> {
if (bitmap != null) {
BitmapCache.getInstance(mContext).addBitmap(getId(), bitmap);
for (BitmapChangedListener listener : mBitmapChangedListeners) {
listener.onBitmapChanged(Session.this, bitmap);
}
}
cleanResources.run();
result.complete(null);
return null;
}).exceptionally(throwable -> {
Log.e(LOGTAG, "Error capturing session background bitmap");
throwable.printStackTrace();
cleanResources.run();
result.complete(null);
return null;
});
}
catch (Exception ex) {
Log.e(LOGTAG, "Error capturing session background bitmap");
ex.printStackTrace();
cleanResources.run();
result.complete(null);
return null;
});
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,7 @@ public void setSession(@NonNull Session aSession, @OldSessionDisplayAction int a
listener.onSessionChanged(oldSession, aSession);
}
}
mCaptureOnPageStop = false;
hideLibraryPanels();
}

Expand Down

0 comments on commit f567fff

Please sign in to comment.