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

Add support for GeckoSession.ContentDelegate.onSlowScript #2176

Merged
merged 1 commit into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -31,6 +31,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 @@ -1031,6 +1032,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 @@ -1295,4 +1295,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>