1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.chrome.browser.prerender;
7 import org.chromium.base.JNINamespace;
8 import org.chromium.base.VisibleForTesting;
9 import org.chromium.chrome.browser.ContentViewUtil;
10 import org.chromium.chrome.browser.profiles.Profile;
13 * A handler class for prerender requests coming from other applications.
15 @JNINamespace("prerender")
16 public class ExternalPrerenderHandler {
18 private long mNativeExternalPrerenderHandler;
20 public ExternalPrerenderHandler() {
21 mNativeExternalPrerenderHandler = nativeInit();
25 * Add a prerender for the given url and given content view dimensions.
26 * @param profile The profile to use for the prerender.
27 * @param url The url to prerender.
28 * @param referrer The referrer for the prerender request.
29 * @param width The width for the content view (render widget host view) for the prerender.
30 * @param height The height for the content view (render widget host view) for the prerender.
31 * @return The native web contents pointer that is linked to this prerender. 0 if unsuccessful.
33 public long addPrerender(Profile profile, String url, String referrer, int width, int height) {
34 long webContentsPtr = ContentViewUtil.createNativeWebContents(false);
35 if (nativeAddPrerender(mNativeExternalPrerenderHandler, profile, webContentsPtr,
36 url, referrer, width, height)) {
37 return webContentsPtr;
39 ContentViewUtil.destroyNativeWebContents(webContentsPtr);
44 * Cancel the current prerender action on this {@link ExternalPrerenderHandler}.
46 public void cancelCurrentPrerender() {
47 nativeCancelCurrentPrerender(mNativeExternalPrerenderHandler);
51 * Check whether a given url has been prerendering for the given profile and session id for the
53 * @param profile The profile to check for prerendering.
54 * @param url The url to check for prerender.
55 * @param webContentsPtr The native pointer for which to compare the session info.
56 * @return Whether the given url was prerendered.
58 public static boolean hasPrerenderedUrl(Profile profile, String url, long webContentsPtr) {
59 return nativeHasPrerenderedUrl(profile, url, webContentsPtr);
63 * Check whether the cookie store has been loaded. This is needed for prerender manager to allow
64 * to add prerenders and this call is to be used to wait until that happens in tests.
65 * @param profile The profile to be used.
66 * @return Whether the cookie store has been loaded.
69 public static boolean checkCookieStoreLoadedForTesting(Profile profile) {
70 return nativeHasCookieStoreLoaded(profile);
73 private static native long nativeInit();
74 private static native boolean nativeAddPrerender(
75 long nativeExternalPrerenderHandlerAndroid, Profile profile,
76 long webContentsPtr, String url, String referrer, int width, int height);
77 private static native boolean nativeHasPrerenderedUrl(
78 Profile profile, String url, long webContentsPtr);
79 private static native boolean nativeHasCookieStoreLoaded(
81 private static native void nativeCancelCurrentPrerender(
82 long nativeExternalPrerenderHandlerAndroid);