Commit 377ae030 authored by John Abd-El-Malek's avatar John Abd-El-Malek Committed by Chromium LUCI CQ

Add support for bfcache in WebLayer.

Missing: publisher URL support currently not working, see https://crbug.com/1158037

Bug: 1144912
Change-Id: I8a3021416c107ff47ea652f60be65c6ddb57b411
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2600244
Commit-Queue: John Abd-El-Malek <jam@chromium.org>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Cr-Commit-Position: refs/heads/master@{#840216}
parent b8cd5513
......@@ -30,6 +30,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.Criteria;
import org.chromium.base.test.util.CriteriaHelper;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
......@@ -104,6 +105,7 @@ public class NavigationTest {
private @NavigationState int mNavigationState;
private boolean mIsKnownProtocol;
private boolean mIsPageInitiatedNavigation;
private boolean mIsServedFromBackForwardCache;
public void notifyCalled(Navigation navigation) {
mUri = navigation.getUri();
......@@ -113,6 +115,7 @@ public class NavigationTest {
mLoadError = navigation.getLoadError();
mNavigationState = navigation.getState();
mIsKnownProtocol = navigation.isKnownProtocol();
mIsServedFromBackForwardCache = navigation.isServedFromBackForwardCache();
if (sShouldTrackPageInitiated) {
mIsPageInitiatedNavigation = navigation.isPageInitiated();
}
......@@ -157,6 +160,10 @@ public class NavigationTest {
return mIsKnownProtocol;
}
public boolean isServedFromBackForwardCache() {
return mIsServedFromBackForwardCache;
}
public boolean isPageInitiated() {
assert sShouldTrackPageInitiated;
return mIsPageInitiatedNavigation;
......@@ -1321,4 +1328,27 @@ public class NavigationTest {
mCallback.onLargestContentfulPaintCallback.getLargestContentfulPaintMs();
Assert.assertTrue(largestContentfulPaint <= (current - navigationStart));
}
@MinWebLayerVersion(89)
@Test
@SmallTest
@CommandLineFlags.Add("enable-features=BackForwardCache")
public void testServedFromBackForwardCache() throws Exception {
TestWebServer testServer = TestWebServer.start();
InstrumentationActivity activity = mActivityTestRule.launchShellWithUrl(null);
setNavigationCallback(activity);
String url = mActivityTestRule.getTestServer().getURL("/echo");
navigateAndWaitForCompletion(url,
() -> { activity.getTab().getNavigationController().navigate(Uri.parse(url)); });
Assert.assertFalse(mCallback.onStartedCallback.isServedFromBackForwardCache());
String url2 = testServer.setResponse("/ok.html", "<html>ok</html>", null);
mActivityTestRule.navigateAndWait(url2);
Assert.assertFalse(mCallback.onStartedCallback.isServedFromBackForwardCache());
navigateAndWaitForCompletion(
url, () -> { activity.getTab().getNavigationController().goBack(); });
Assert.assertTrue(mCallback.onStartedCallback.isServedFromBackForwardCache());
}
}
......@@ -186,6 +186,13 @@ public final class NavigationImpl extends INavigation.Stub {
return NavigationImplJni.get().isReload(mNativeNavigationImpl);
}
@Override
public boolean isServedFromBackForwardCache() {
StrictModeWorkaround.apply();
throwIfNativeDestroyed();
return NavigationImplJni.get().isServedFromBackForwardCache(mNativeNavigationImpl);
}
public void setIntentLaunched() {
mIntentLaunched = true;
}
......@@ -247,5 +254,6 @@ public final class NavigationImpl extends INavigation.Stub {
boolean setUserAgentString(long nativeNavigationImpl, String value);
boolean isPageInitiated(long nativeNavigationImpl);
boolean isReload(long nativeNavigationImpl);
boolean isServedFromBackForwardCache(long nativeNavigationImpl);
}
}
......@@ -38,4 +38,5 @@ interface INavigation {
boolean wasIntentLaunched() = 13;
boolean isUserDecidingIntentLaunch() = 14;
boolean isKnownProtocol() = 15;
boolean isServedFromBackForwardCache() = 16;
}
......@@ -114,6 +114,10 @@ bool NavigationImpl::IsReload() {
return navigation_handle_->GetReloadType() != content::ReloadType::NONE;
}
bool NavigationImpl::IsServedFromBackForwardCache() {
return navigation_handle_->IsServedFromBackForwardCache();
}
GURL NavigationImpl::GetURL() {
return navigation_handle_->GetURL();
}
......
......@@ -80,6 +80,9 @@ class NavigationImpl : public Navigation {
const base::android::JavaParamRef<jstring>& value);
jboolean IsPageInitiated(JNIEnv* env) { return IsPageInitiated(); }
jboolean IsReload(JNIEnv* env) { return IsReload(); }
jboolean IsServedFromBackForwardCache(JNIEnv* env) {
return IsServedFromBackForwardCache();
}
void SetResponse(
std::unique_ptr<embedder_support::WebResourceResponse> response);
......@@ -106,6 +109,7 @@ class NavigationImpl : public Navigation {
void SetUserAgentString(const std::string& value) override;
bool IsPageInitiated() override;
bool IsReload() override;
bool IsServedFromBackForwardCache() override;
private:
content::NavigationHandle* navigation_handle_;
......
......@@ -333,4 +333,25 @@ public class Navigation extends IClientNavigation.Stub {
throw new APICallException(e);
}
}
/**
* Whether the navigation is restoring a page from back-forward cache (see
* https://web.dev/bfcache/). Since a previously loaded page is being reused, there are some
* things embedders have to keep in mind such as:
* * there will be no NavigationObserver::onFirstContentfulPaint callbacks
* * if an embedder injects code using Tab::ExecuteScript there is no need to reinject scripts
*
* @since 89
*/
public boolean isServedFromBackForwardCache() {
ThreadCheck.ensureOnUiThread();
if (WebLayer.getSupportedMajorVersionInternal() < 89) {
throw new UnsupportedOperationException();
}
try {
return mNavigationImpl.isServedFromBackForwardCache();
} catch (RemoteException e) {
throw new APICallException(e);
}
}
}
......@@ -111,7 +111,9 @@ public abstract class NavigationCallback {
/**
* This is fired after each navigation has completed to indicate that the first paint after a
* non-empty layout has finished. This is *not* called for same-document navigations.
* non-empty layout has finished. This is *not* called for same-document navigations or when the
* page is loaded from the back-forward cache; see {@link
* Navigation#isServedFromBackForwardCache}.
*/
public void onFirstContentfulPaint() {}
......
......@@ -135,6 +135,14 @@ class Navigation {
// * page-initiated reloads, e.g. location.reload()
// * reloads when the network interface is reconnected
virtual bool IsReload() = 0;
// Whether the navigation is restoring a page from back-forward cache (see
// https://web.dev/bfcache/). Since a previously loaded page is being reused,
// there are some things embedders have to keep in mind such as:
// * there will be no NavigationObserver::onFirstContentfulPaint callbacks
// * if an embedder injects code using Tab::ExecuteScript there is no need
// to reinject scripts
virtual bool IsServedFromBackForwardCache() = 0;
};
} // namespace weblayer
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment