From f567ffffa945b8bb8b61a14d16af2c5ddf2d308f Mon Sep 17 00:00:00 2001 From: Imanol Fernandez Date: Wed, 27 Nov 2019 19:43:25 +0100 Subject: [PATCH] Handle potential exception capturing snapshots (#2386) * Handle potential exception when GeckoSession.capture() is called * Reset the mCaptureOnPageStop member when switching sessions --- .../vrbrowser/browser/engine/Session.java | 63 ++++++++++++++----- .../vrbrowser/ui/widgets/WindowWidget.java | 1 + 2 files changed, 47 insertions(+), 17 deletions(-) diff --git a/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/Session.java b/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/Session.java index bc9c81dad..56606b123 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/Session.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/Session.java @@ -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 captureBackgroundBitmap(int displayWidth, int displayHeight) { @@ -497,19 +507,38 @@ public CompletableFuture captureBackgroundBitmap(int displayWidth, int dis CompletableFuture 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; } diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/WindowWidget.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/WindowWidget.java index 176a4dafc..f853c9af4 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/WindowWidget.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/WindowWidget.java @@ -1075,6 +1075,7 @@ public void setSession(@NonNull Session aSession, @OldSessionDisplayAction int a listener.onSessionChanged(oldSession, aSession); } } + mCaptureOnPageStop = false; hideLibraryPanels(); }