Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / android / java / src / org / chromium / chrome / browser / prerender / ExternalPrerenderHandler.java
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.
4
5 package org.chromium.chrome.browser.prerender;
6
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;
11
12 /**
13  * A handler class for prerender requests coming from  other applications.
14  */
15 @JNINamespace("prerender")
16 public class ExternalPrerenderHandler {
17
18     private long mNativeExternalPrerenderHandler;
19
20     public ExternalPrerenderHandler() {
21         mNativeExternalPrerenderHandler = nativeInit();
22     }
23
24     /**
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.
32      */
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;
38         }
39         ContentViewUtil.destroyNativeWebContents(webContentsPtr);
40         return 0;
41     }
42
43     /**
44      * Cancel the current prerender action on this {@link ExternalPrerenderHandler}.
45      */
46     public void cancelCurrentPrerender() {
47         nativeCancelCurrentPrerender(mNativeExternalPrerenderHandler);
48     }
49
50     /**
51      * Check whether a given url has been prerendering for the given profile and session id for the
52      * given web contents.
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.
57      */
58     public static boolean hasPrerenderedUrl(Profile profile, String url, long webContentsPtr)  {
59         return nativeHasPrerenderedUrl(profile, url, webContentsPtr);
60     }
61
62     /**
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.
67      */
68     @VisibleForTesting
69     public static boolean checkCookieStoreLoadedForTesting(Profile profile)  {
70         return nativeHasCookieStoreLoaded(profile);
71     }
72
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(
80             Profile profile);
81     private static native void nativeCancelCurrentPrerender(
82             long nativeExternalPrerenderHandlerAndroid);
83 }