forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose an API to perform a prerender (implemented using NoStatePrefetch). A browsertest and a javatest have been added to verify the expected behavior. Bug: 1136091 Change-Id: I158b5b954fbbf2e3cb9821180d6afd6aa042abd2 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2470634 Commit-Queue: Mugdha Lakhani <nator@chromium.org> Reviewed-by: Scott Violet <sky@chromium.org> Reviewed-by: Ryan Sturm <ryansturm@chromium.org> Reviewed-by: Colin Blundell <blundell@chromium.org> Cr-Commit-Position: refs/heads/master@{#819762}
- Loading branch information
Mugdha Lakhani
authored and
Commit Bot
committed
Oct 22, 2020
1 parent
c93a3aa
commit 1a0ff23
Showing
19 changed files
with
425 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...yer/browser/android/javatests/src/org/chromium/weblayer/test/PrerenderControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2020 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package org.chromium.weblayer.test; | ||
|
||
import android.net.Uri; | ||
|
||
import androidx.test.filters.SmallTest; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import org.chromium.base.test.util.CallbackHelper; | ||
import org.chromium.base.test.util.Feature; | ||
import org.chromium.content_public.browser.test.util.TestThreadUtils; | ||
import org.chromium.net.test.util.TestWebServer; | ||
import org.chromium.net.test.util.WebServer; | ||
import org.chromium.weblayer.PrerenderController; | ||
import org.chromium.weblayer.shell.InstrumentationActivity; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
|
||
/** Tests verifying PrerenderController behavior. */ | ||
@RunWith(WebLayerJUnit4ClassRunner.class) | ||
public class PrerenderControllerTest { | ||
private static final String DEFAULT_BODY = "<html><title>TestPage</title>Hello World!</html>"; | ||
private CallbackHelper mPrerenderedPageFetched; | ||
|
||
private WebServer.RequestHandler mRequestHandler = new WebServer.RequestHandler() { | ||
@Override | ||
public void handleRequest(WebServer.HTTPRequest request, OutputStream stream) { | ||
try { | ||
if (request.getURI().contains("prerendered_page.html")) { | ||
TestThreadUtils.runOnUiThreadBlocking( | ||
() -> mPrerenderedPageFetched.notifyCalled()); | ||
} | ||
WebServer.writeResponse(stream, WebServer.STATUS_OK, DEFAULT_BODY.getBytes()); | ||
} catch (IOException exception) { | ||
Assert.fail(exception.getMessage() | ||
+ " \n while handling request: " + request.toString()); | ||
} | ||
} | ||
}; | ||
|
||
@Rule | ||
public InstrumentationActivityTestRule mActivityTestRule = | ||
new InstrumentationActivityTestRule(); | ||
|
||
@Test | ||
@SmallTest | ||
@Feature({"WebLayer"}) | ||
public void testAddingPrerender() throws Exception { | ||
TestWebServer testServer = TestWebServer.start(); | ||
testServer.setRequestHandler(mRequestHandler); | ||
mPrerenderedPageFetched = new CallbackHelper(); | ||
InstrumentationActivity activity = mActivityTestRule.launchShellWithUrl(null); | ||
TestThreadUtils.runOnUiThreadBlocking(() -> { | ||
PrerenderController prerenderController = | ||
activity.getBrowser().getProfile().getPrerenderController(); | ||
prerenderController.schedulePrerender( | ||
Uri.parse(testServer.getResponseUrl("/prerendered_page.html"))); | ||
}); | ||
mPrerenderedPageFetched.waitForFirst(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
weblayer/browser/java/org/chromium/weblayer_private/PrerenderControllerImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2020 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package org.chromium.weblayer_private; | ||
|
||
import org.chromium.base.LifetimeAssert; | ||
import org.chromium.base.annotations.JNINamespace; | ||
import org.chromium.base.annotations.NativeMethods; | ||
import org.chromium.weblayer_private.interfaces.IPrerenderController; | ||
|
||
/** | ||
* Implementation of {@link IPrerenderController}. | ||
*/ | ||
@JNINamespace("weblayer") | ||
public class PrerenderControllerImpl extends IPrerenderController.Stub { | ||
private long mNativePrerenderController; | ||
private final LifetimeAssert mLifetimeAssert = LifetimeAssert.create(this); | ||
|
||
void destroy() { | ||
mNativePrerenderController = 0; | ||
|
||
// If mLifetimeAssert is GC'ed before this is called, it will throw an exception | ||
// with a stack trace showing the stack during LifetimeAssert.create(). | ||
LifetimeAssert.setSafeToGc(mLifetimeAssert, true); | ||
} | ||
|
||
public PrerenderControllerImpl(long nativePrerenderController) { | ||
mNativePrerenderController = nativePrerenderController; | ||
} | ||
|
||
@Override | ||
public void prerender(String url) { | ||
PrerenderControllerImplJni.get().prerender(mNativePrerenderController, url); | ||
} | ||
|
||
@NativeMethods() | ||
interface Natives { | ||
void prerender(long nativePrerenderControllerImpl, String url); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
weblayer/browser/java/org/chromium/weblayer_private/interfaces/IPrerenderController.aidl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Copyright 2020 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package org.chromium.weblayer_private.interfaces; | ||
|
||
interface IPrerenderController { | ||
// Since 88 | ||
void prerender(in String url) = 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
weblayer/browser/no_state_prefetch/prerender_controller_impl.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2020 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "weblayer/browser/no_state_prefetch/prerender_controller_impl.h" | ||
|
||
#include "build/build_config.h" | ||
#include "components/prerender/browser/prerender_handle.h" | ||
#include "components/prerender/browser/prerender_manager.h" | ||
#include "content/public/browser/browser_context.h" | ||
#include "ui/gfx/geometry/rect.h" | ||
#include "url/gurl.h" | ||
#include "weblayer/browser/no_state_prefetch/prerender_manager_factory.h" | ||
|
||
#if defined(OS_ANDROID) | ||
#include "base/android/jni_string.h" | ||
#include "weblayer/browser/java/jni/PrerenderControllerImpl_jni.h" | ||
#endif | ||
|
||
namespace weblayer { | ||
|
||
PrerenderControllerImpl::PrerenderControllerImpl( | ||
content::BrowserContext* browser_context) | ||
: browser_context_(browser_context) { | ||
DCHECK(browser_context_); | ||
} | ||
|
||
PrerenderControllerImpl::~PrerenderControllerImpl() = default; | ||
|
||
#if defined(OS_ANDROID) | ||
void PrerenderControllerImpl::Prerender( | ||
JNIEnv* env, | ||
const base::android::JavaParamRef<jstring>& url) { | ||
Prerender(GURL(ConvertJavaStringToUTF8(url))); | ||
} | ||
#endif | ||
|
||
void PrerenderControllerImpl::Prerender(const GURL& url) { | ||
auto* prerender_manager = | ||
PrerenderManagerFactory::GetForBrowserContext(browser_context_); | ||
DCHECK(prerender_manager); | ||
|
||
// The referrer parameter results in a header being set that lets the server | ||
// serving the URL being prerendered see where the request originated. It's | ||
// an optional header, it's okay to skip setting it here. | ||
// SessionStorageNamespace isn't necessary for NoStatePrefetch, so it's okay | ||
// to pass in a nullptr. | ||
// PrerenderManager uses default bounds if the one provided is empty. | ||
prerender_manager->AddPrerenderFromExternalRequest( | ||
url, content::Referrer(), /* session_storage_namespace= */ nullptr, | ||
/* bounds= */ gfx::Rect()); | ||
} | ||
|
||
} // namespace weblayer |
Oops, something went wrong.