diff --git a/app/src/common/shared/org/mozilla/vrbrowser/browser/PromptDelegate.java b/app/src/common/shared/org/mozilla/vrbrowser/browser/PromptDelegate.java index 832984da1..7768e0bd8 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/browser/PromptDelegate.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/browser/PromptDelegate.java @@ -12,6 +12,7 @@ import org.mozilla.geckoview.AllowOrDeny; import org.mozilla.geckoview.GeckoResult; import org.mozilla.geckoview.GeckoSession; +import org.mozilla.geckoview.SlowScriptResponse; import org.mozilla.vrbrowser.AppExecutors; import org.mozilla.vrbrowser.R; import org.mozilla.vrbrowser.VRBrowserApplication; @@ -37,7 +38,8 @@ public class PromptDelegate implements GeckoSession.PromptDelegate, WindowWidget.WindowListener, - GeckoSession.NavigationDelegate { + GeckoSession.NavigationDelegate, + GeckoSession.ContentDelegate { public interface PopUpDelegate { void onPopUpAvailable(); @@ -46,6 +48,7 @@ public interface PopUpDelegate { private PromptWidget mPrompt; private PopUpBlockDialogWidget mPopUpPrompt; + private ConfirmPromptWidget mSlowScriptPrompt; private Context mContext; private WindowWidget mAttachedWindow; private List mAllowedPopUpSites; @@ -99,11 +102,13 @@ private Session getSession() { private void setUpSession(@NonNull Session aSession) { aSession.setPromptDelegate(this); aSession.addNavigationListener(this); + aSession.addContentListener(this); } private void cleanSession(@NonNull Session aSession) { aSession.setPromptDelegate(null); aSession.removeNavigationListener(this); + aSession.removeContentListener(this); mPopUpRequests.remove(aSession.hashCode()); } @@ -389,6 +394,43 @@ private void showPopUp(int sessionId, @NonNull Pair onSlowScript(@NonNull GeckoSession aSession, @NonNull String aScriptFileName) { + final GeckoResult result = new GeckoResult<>(); + if (mSlowScriptPrompt == null) { + mSlowScriptPrompt = new ConfirmPromptWidget(mContext); + mSlowScriptPrompt.getPlacement().parentHandle = mAttachedWindow.getHandle(); + mSlowScriptPrompt.getPlacement().parentAnchorY = 0.0f; + mSlowScriptPrompt.getPlacement().translationY = WidgetPlacement.unitFromMeters(mContext, R.dimen.base_app_dialog_y_distance); + mSlowScriptPrompt.setTitle(mContext.getResources().getString(R.string.slow_script_dialog_title)); + mSlowScriptPrompt.setMessage(mContext.getResources().getString(R.string.slow_script_dialog_description, aScriptFileName)); + mSlowScriptPrompt.setButtons(new String[]{ + mContext.getResources().getString(R.string.slow_script_dialog_action_wait), + mContext.getResources().getString(R.string.slow_script_dialog_action_stop) + }); + mSlowScriptPrompt.setPromptDelegate(new ConfirmPromptWidget.ConfirmPromptDelegate() { + @Override + public void confirm(int index) { + result.complete(index == 0 ? SlowScriptResponse.CONTINUE : SlowScriptResponse.STOP); + } + @Override + public void dismiss() { + result.complete(SlowScriptResponse.CONTINUE); + } + }); + mSlowScriptPrompt.show(UIWidget.REQUEST_FOCUS); + } + + return result.then(value -> { + if (mSlowScriptPrompt != null && !mSlowScriptPrompt.isReleased()) { + mSlowScriptPrompt.releaseWidget(); + } + mSlowScriptPrompt = null; + return GeckoResult.fromValue(value); + }); + } + // WindowWidget.WindowListener @Override 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 5bf88e528..725212c20 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 @@ -30,6 +30,7 @@ import org.mozilla.geckoview.GeckoSession; import org.mozilla.geckoview.GeckoSessionSettings; import org.mozilla.geckoview.MediaElement; +import org.mozilla.geckoview.SlowScriptResponse; import org.mozilla.geckoview.WebRequestError; import org.mozilla.vrbrowser.R; import org.mozilla.vrbrowser.browser.Media; @@ -1029,6 +1030,20 @@ public void onFirstContentfulPaint(@NonNull GeckoSession aSession) { } } + @Nullable + @Override + public GeckoResult onSlowScript(@NonNull GeckoSession aSession, @NonNull String aScriptFileName) { + if (mState.mSession == aSession) { + for (GeckoSession.ContentDelegate listener : mContentListeners) { + GeckoResult result = listener.onSlowScript(aSession, aScriptFileName); + if (result != null) { + return result; + } + } + } + return null; + } + // TextInput Delegate @Override diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e690a70ee..ac7526bc5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1304,4 +1304,20 @@ the Select` button. When clicked it closes all the previously selected tabs --> Start Browsing + + + + Slow script + + + A web page is slowing down your browser (%1$s). What would you like to do? + + + Stop it + + + Wait