Commit dab3a709 authored by Cathy Lu's avatar Cathy Lu
Browse files

Bug 1764998 - Add geckosession API to set priority r=geckoview-reviewers,agi,owlish,jonalmeida

Corresponding Design Doc: https://docs.google.com/document/d/1xGXQhVLnWBU84WmQ0ebkJ-MqI6o67mXosjxT1p3M26w/edit?usp=sharing

This API will allow apps to set a geckosession/tab to either high priority or default with the priorityHint.

Differential Revision: https://phabricator.services.mozilla.com/D145789
parent b67097f2
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -924,6 +924,7 @@ package org.mozilla.geckoview {
    method @AnyThread public void setMediaSessionDelegate(@Nullable MediaSession.Delegate);
    method @UiThread public void setNavigationDelegate(@Nullable GeckoSession.NavigationDelegate);
    method @UiThread public void setPermissionDelegate(@Nullable GeckoSession.PermissionDelegate);
    method @AnyThread public void setPriorityHint(int);
    method @UiThread public void setProgressDelegate(@Nullable GeckoSession.ProgressDelegate);
    method @AnyThread public void setPromptDelegate(@Nullable GeckoSession.PromptDelegate);
    method @UiThread public void setScrollDelegate(@Nullable GeckoSession.ScrollDelegate);
@@ -947,6 +948,8 @@ package org.mozilla.geckoview {
    field public static final int LOAD_FLAGS_FORCE_ALLOW_DATA_URI = 32;
    field public static final int LOAD_FLAGS_NONE = 0;
    field public static final int LOAD_FLAGS_REPLACE_HISTORY = 64;
    field public static final int PRIORITY_DEFAULT = 0;
    field public static final int PRIORITY_HIGH = 1;
    field @Nullable protected GeckoSession.Window mWindow;
  }

@@ -1167,6 +1170,9 @@ package org.mozilla.geckoview {
  @Retention(value=RetentionPolicy.SOURCE) public static interface GeckoSession.PermissionDelegate.MediaSource.Type {
  }

  @Retention(value=RetentionPolicy.SOURCE) public static interface GeckoSession.Priority {
  }

  public static interface GeckoSession.ProgressDelegate {
    method @UiThread default public void onPageStart(@NonNull GeckoSession, @NonNull String);
    method @UiThread default public void onPageStop(@NonNull GeckoSession, boolean);
+31 −8
Original line number Diff line number Diff line
@@ -132,7 +132,7 @@ class GeckoViewTest : BaseSessionTest() {

            // A tab with priority hint does not get de-prioritized even when
            // the surface is destroyed
            sessionRule.runtime.webExtensionController.setTabActive(mainSession, true)
            otherSession.setPriorityHint(GeckoSession.PRIORITY_HIGH)

            // This will destroy mainSession's surface and create a surface for otherSession
            it.view.setSession(otherSession)
@@ -146,7 +146,7 @@ class GeckoViewTest : BaseSessionTest() {
            waitUntilContentProcessPriority(high = listOf(mainSession), low = listOf())

            // Cleanup
            sessionRule.runtime.webExtensionController.setTabActive(mainSession, false)
            otherSession.setPriorityHint(GeckoSession.PRIORITY_DEFAULT)
        }
    }

@@ -158,15 +158,15 @@ class GeckoViewTest : BaseSessionTest() {

        val otherSession = setupPriorityTest()

        // Setting tab active raises priority
        sessionRule.runtime.webExtensionController.setTabActive(otherSession, true)
        // Setting priorityHint to PRIORITY_HIGH raises priority
        otherSession.setPriorityHint(GeckoSession.PRIORITY_HIGH)

        waitUntilContentProcessPriority(
            high = listOf(mainSession, otherSession), low = listOf()
        )

        // Unsetting the tab as active should lower priority
        sessionRule.runtime.webExtensionController.setTabActive(otherSession, false)
        // Setting the priorityHint to default should lower priority
        otherSession.setPriorityHint(GeckoSession.PRIORITY_DEFAULT)

        waitUntilContentProcessPriority(
            high = listOf(mainSession), low = listOf(otherSession)
@@ -199,10 +199,33 @@ class GeckoViewTest : BaseSessionTest() {
            waitUntilContentProcessPriority(
                high = listOf(mainSession), low = listOf(otherSession))

            // Setting the session to active should also raise priority
            otherSession.setActive(true)
            // Setting priorityHint to PRIORITY_HIGH raises priority
            otherSession.setPriorityHint(GeckoSession.PRIORITY_HIGH)
            waitUntilContentProcessPriority(
                high = listOf(mainSession, otherSession), low = listOf())
        }
    }

    @Test
    @NullDelegate(Autofill.Delegate::class)
    fun setPriorityHint() {
        // Bug 1767346
        assumeThat(false, equalTo(true))

        val otherSession = setupPriorityTest()

        // Setting priorityHint to PRIORITY_HIGH raises priority
        otherSession.setPriorityHint(GeckoSession.PRIORITY_HIGH)

        waitUntilContentProcessPriority(
            high = listOf(mainSession, otherSession), low = listOf()
        )

        // Setting priorityHint to PRIORITY_DEFAULT should lower priority
        otherSession.setPriorityHint(GeckoSession.PRIORITY_DEFAULT)

        waitUntilContentProcessPriority(
            high = listOf(mainSession), low = listOf(otherSession)
        )
    }
}
+27 −0
Original line number Diff line number Diff line
@@ -1580,6 +1580,17 @@ public class GeckoSession {
    return mMagnifier;
  }

  // The priority of the GeckoSession, either default or high.
  @Retention(RetentionPolicy.SOURCE)
  @IntDef({PRIORITY_DEFAULT, PRIORITY_HIGH})
  public @interface Priority {}

  /** Value for Priority when it is default. */
  public static final int PRIORITY_DEFAULT = 0;

  /** Value for Priority when it is high. */
  public static final int PRIORITY_HIGH = 1;

  @Retention(RetentionPolicy.SOURCE)
  @IntDef(
      flag = true,
@@ -2247,6 +2258,22 @@ public class GeckoSession {
    mEventDispatcher.dispatch("GeckoView:SetFocused", msg);
  }

  /**
   * Notify GeckoView of the priority for this GeckoSession.
   *
   * <p>Set this GeckoSession to high priority (PRIORITY_HIGH) whenever the app wants to signal to
   * GeckoView that this GeckoSession is important to the app. GeckoView will keep the session state
   * as long as possible. Set this to default priority (PRIORITY_DEFAULT) in any other case.
   *
   * @param priorityHint Priority of the geckosession, either high priority or default.
   */
  @AnyThread
  public void setPriorityHint(final @Priority int priorityHint) {
    final GeckoBundle msg = new GeckoBundle(1);
    msg.putInt("priorityHint", priorityHint);
    mEventDispatcher.dispatch("GeckoView:SetPriorityHint", msg);
  }

  /** Class representing a saved session state. */
  @AnyThread
  public static class SessionState extends AbstractSequentialList<HistoryDelegate.HistoryItem>
+3 −1
Original line number Diff line number Diff line
@@ -17,11 +17,13 @@ exclude: true
- Added [`DateTimePrompt.stepValue`][102.1] to export [`step`][102.2] attribute of input element.
  ([bug 1499635]({{bugzilla}}1499635))
- Deprecated [`onLocationChange(2)`][102.3], please use [`onLocationChange(3)`][102.4].
- Added [`GeckoSession.setPriorityHint`][102.5] function to set the session to either high priority or default.

[102.1]: {{javadoc_uri}}/GeckoSession.PromptDelegate.DateTimePrompt.html#stepValue
[102.2]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date#step
[102.3]: {{javadoc_uri}}/GeckoSession.NavigationDelegate.html#onLocationChange(org.mozilla.geckoview.GeckoSession,java.lang.String)
[102.4]: {{javadoc_uri}}/GeckoSession.NavigationDelegate.html#onLocationChange(org.mozilla.geckoview.GeckoSession,java.lang.String,java.util.List)
[102.5]: {{javadoc_uri}}/GeckoSession.html#setPriorityHint(int)

## v101
- Added [`GeckoDisplay.surfaceChanged`][101.1] function taking new type [`GeckoDisplay.SurfaceInfo`][101.2].
@@ -1176,4 +1178,4 @@ to allow adding gecko profiler markers.
[65.24]: {{javadoc_uri}}/CrashReporter.html#sendCrashReport(android.content.Context,android.os.Bundle,java.lang.String)
[65.25]: {{javadoc_uri}}/GeckoResult.html

[api-version]: 036203b3b292abd94071762009cf965908e40f04
[api-version]: 2f401dd976431a7a150b8743f3949a50adbeeb4b
+9 −0
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ class GeckoViewContent extends GeckoViewModule {
      "GeckoView:ScrollTo",
      "GeckoView:SetActive",
      "GeckoView:SetFocused",
      "GeckoView:SetPriorityHint",
      "GeckoView:UpdateInitData",
      "GeckoView:ZoomToInput",
    ]);
@@ -162,6 +163,14 @@ class GeckoViewContent extends GeckoViewModule {
          this.browser.blur();
        }
        break;
      case "GeckoView:SetPriorityHint":
        if (this.browser.isRemoteBrowser) {
          const remoteTab = this.browser.frameLoader?.remoteTab;
          if (remoteTab) {
            remoteTab.priorityHint = aData.priorityHint;
          }
        }
        break;
      case "GeckoView:RestoreState":
        this.actor.restoreState(aData);
        break;
Loading