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