WebContentsObserverAndroid.java revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
1eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Copyright 2012 The Chromium Authors. All rights reserved.
2eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Use of this source code is governed by a BSD-style license that can be
3eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// found in the LICENSE file.
4eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
5eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochpackage org.chromium.content.browser;
6eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
7eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochimport org.chromium.base.CalledByNative;
8eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochimport org.chromium.base.JNINamespace;
9eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochimport org.chromium.base.ThreadUtils;
10eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochimport org.chromium.content_public.browser.WebContents;
11eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
12eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch/**
13eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch * This class receives callbacks that act as hooks for various a native web contents events related
14eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch * to loading a url. A single web contents can have multiple WebContentObserverAndroids.
15eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch */
16eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch@JNINamespace("content")
17eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochpublic abstract class WebContentsObserverAndroid {
18eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    private long mNativeWebContentsObserverAndroid;
19eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
20eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    public WebContentsObserverAndroid(WebContents webContents) {
21eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        ThreadUtils.assertOnUiThread();
22eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        mNativeWebContentsObserverAndroid = nativeInit(webContents);
23eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
24eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
25eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    @CalledByNative
26eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    public void renderProcessGone(boolean wasOomProtected) {
27eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
28eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
29eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    /**
30eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch     * Called when the a page starts loading.
31eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch     * @param url The validated url for the loading page.
32eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch     */
33eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    @CalledByNative
34eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    public void didStartLoading(String url) {
35eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
36eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
37eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    /**
38eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch     * Called when the a page finishes loading.
39eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch     * @param url The validated url for the page.
40eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch     */
41eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    @CalledByNative
42eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    public void didStopLoading(String url) {
43eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
44eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
45eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    /**
46eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch     * Called when an error occurs while loading a page and/or the page fails to load.
47eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch     * @param errorCode Error code for the occurring error.
48eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch     * @param description The description for the error.
49eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch     * @param failingUrl The url that was loading when the error occurred.
50eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch     */
51eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    @CalledByNative
52eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    public void didFailLoad(boolean isProvisionalLoad,
53eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch            boolean isMainFrame, int errorCode, String description, String failingUrl) {
54eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
55eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
56eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    /**
57eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch     * Called when the main frame of the page has committed.
58eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch     * @param url The validated url for the page.
59eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch     * @param baseUrl The validated base url for the page.
60eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch     * @param isNavigationToDifferentPage Whether the main frame navigated to a different page.
61eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch     * @param isFragmentNavigation Whether the main frame navigation did not cause changes to the
62eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch     *                             document (for example scrolling to a named anchor or PopState).
63eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch     */
64eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    @CalledByNative
65eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    public void didNavigateMainFrame(String url, String baseUrl,
66eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch            boolean isNavigationToDifferentPage, boolean isFragmentNavigation) {
67eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
68eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
69eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    /**
70eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch     * Called when the page had painted something non-empty.
71eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch     */
72eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    @CalledByNative
73eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    public void didFirstVisuallyNonEmptyPaint() {
74eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
75eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
76eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    /**
77eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch     * Similar to didNavigateMainFrame but also called on subframe navigations.
78     * @param url The validated url for the page.
79     * @param baseUrl The validated base url for the page.
80     * @param isReload True if this navigation is a reload.
81     */
82    @CalledByNative
83    public void didNavigateAnyFrame(String url, String baseUrl, boolean isReload) {
84    }
85
86    /**
87     * Notifies that a load is started for a given frame.
88     * @param frameId A positive, non-zero integer identifying the navigating frame.
89     * @param parentFrameId The frame identifier of the frame containing the navigating frame,
90     *                      or -1 if the frame is not contained in another frame.
91     * @param isMainFrame Whether the load is happening for the main frame.
92     * @param validatedUrl The validated URL that is being navigated to.
93     * @param isErrorPage Whether this is navigating to an error page.
94     * @param isIframeSrcdoc Whether this is navigating to about:srcdoc.
95     */
96    @CalledByNative
97    public void didStartProvisionalLoadForFrame(
98            long frameId,
99            long parentFrameId,
100            boolean isMainFrame,
101            String validatedUrl,
102            boolean isErrorPage,
103            boolean isIframeSrcdoc) {
104    }
105
106    /**
107     * Notifies that the provisional load was successfully committed. The RenderViewHost is now
108     * the current RenderViewHost of the WebContents.
109     * @param frameId A positive, non-zero integer identifying the navigating frame.
110     * @param isMainFrame Whether the load is happening for the main frame.
111     * @param url The committed URL being navigated to.
112     * @param transitionType The transition type as defined in
113     *                      {@link org.chromium.ui.base.PageTransitionTypes} for the load.
114     */
115    @CalledByNative
116    public void didCommitProvisionalLoadForFrame(
117            long frameId, boolean isMainFrame, String url, int transitionType) {
118
119    }
120
121    /**
122     * Notifies that a load has finished for a given frame.
123     * @param frameId A positive, non-zero integer identifying the navigating frame.
124     * @param validatedUrl The validated URL that is being navigated to.
125     * @param isMainFrame Whether the load is happening for the main frame.
126     */
127    @CalledByNative
128    public void didFinishLoad(long frameId, String validatedUrl, boolean isMainFrame) {
129    }
130
131    /**
132     * Notifies that the document has finished loading for the given frame.
133     * @param frameId A positive, non-zero integer identifying the navigating frame.
134     */
135    @CalledByNative
136    public void documentLoadedInFrame(long frameId) {
137    }
138
139    /**
140     * Notifies that a navigation entry has been committed.
141     */
142    @CalledByNative
143    public void navigationEntryCommitted() {
144    }
145
146    /**
147     * Called when an interstitial page gets attached to the tab content.
148     */
149    @CalledByNative
150    public void didAttachInterstitialPage() {
151    }
152
153    /**
154     * Called when an interstitial page gets detached from the tab content.
155     */
156    @CalledByNative
157    public void didDetachInterstitialPage() {
158    }
159
160    /**
161     * Called when the theme color was changed.
162     * @param color the new color in ARGB format
163     */
164    @CalledByNative
165    public void didChangeThemeColor(int color) {
166    }
167
168    /**
169     * Destroy the corresponding native object.
170     */
171    @CalledByNative
172    public void detachFromWebContents() {
173        if (mNativeWebContentsObserverAndroid != 0) {
174            nativeDestroy(mNativeWebContentsObserverAndroid);
175            mNativeWebContentsObserverAndroid = 0;
176        }
177    }
178
179    private native long nativeInit(WebContents webContents);
180    private native void nativeDestroy(long nativeWebContentsObserverAndroid);
181}
182