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

Commit

Permalink
Add support for GeckoSession.ContentDelegate.onSlowScript (#2176)
Browse files Browse the repository at this point in the history
  • Loading branch information
MortimerGoro authored and bluemarvin committed Nov 14, 2019
1 parent 762b19b commit dd93fed
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,7 +38,8 @@
public class PromptDelegate implements
GeckoSession.PromptDelegate,
WindowWidget.WindowListener,
GeckoSession.NavigationDelegate {
GeckoSession.NavigationDelegate,
GeckoSession.ContentDelegate {

public interface PopUpDelegate {
void onPopUpAvailable();
Expand All @@ -46,6 +48,7 @@ public interface PopUpDelegate {

private PromptWidget mPrompt;
private PopUpBlockDialogWidget mPopUpPrompt;
private ConfirmPromptWidget mSlowScriptPrompt;
private Context mContext;
private WindowWidget mAttachedWindow;
private List<PopUpSite> mAllowedPopUpSites;
Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -389,6 +394,43 @@ private void showPopUp(int sessionId, @NonNull Pair<String, LinkedList<PopUpRequ
}
}

@Nullable
@Override
public GeckoResult<SlowScriptResponse> onSlowScript(@NonNull GeckoSession aSession, @NonNull String aScriptFileName) {
final GeckoResult<SlowScriptResponse> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1029,6 +1030,20 @@ public void onFirstContentfulPaint(@NonNull GeckoSession aSession) {
}
}

@Nullable
@Override
public GeckoResult<SlowScriptResponse> onSlowScript(@NonNull GeckoSession aSession, @NonNull String aScriptFileName) {
if (mState.mSession == aSession) {
for (GeckoSession.ContentDelegate listener : mContentListeners) {
GeckoResult<SlowScriptResponse> result = listener.onSlowScript(aSession, aScriptFileName);
if (result != null) {
return result;
}
}
}
return null;
}

// TextInput Delegate

@Override
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1304,4 +1304,20 @@ the Select` button. When clicked it closes all the previously selected tabs -->
<!-- This string is displayed a button of the what's new dialog that is displayed if the user has updated
or updated the app and hasn't signed it yet. If clicked the dialog is dismissed. -->
<string name="whats_new_button_start_browsing">Start Browsing</string>


<!-- This string is displayed in the title of the slow script dialog. -->
<string name="slow_script_dialog_title">Slow script</string>

<!-- This string is displayed as a description below the title of slow script dialog.
'%1$s' will be replaced the script filename that is causing the slow down. -->
<string name="slow_script_dialog_description">A web page is slowing down your browser (%1$s). What would you like to do?</string>

<!-- This string is displayed as a button below the slow script dialog description message. When clicked
it stops the slow script that is causing the web page slow down. -->
<string name="slow_script_dialog_action_stop">Stop it</string>

<!-- This string is displayed as a button below the slow script dialog description message. When clicked
it continues to wait for execution of the web page script that caused the slow down. -->
<string name="slow_script_dialog_action_wait">Wait</string>
</resources>

0 comments on commit dd93fed

Please sign in to comment.