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_public.browser;
6 
7 import androidx.annotation.IntDef;
8 import androidx.annotation.Nullable;
9 
10 import org.chromium.blink.mojom.ViewportFit;
11 import org.chromium.ui.base.WindowAndroid;
12 
13 import java.lang.annotation.Retention;
14 import java.lang.annotation.RetentionPolicy;
15 import java.lang.ref.WeakReference;
16 
17 /**
18  * This class receives callbacks that act as hooks for various a native web contents events related
19  * to loading a url. A single web contents can have multiple WebContentObservers.
20  */
21 public abstract class WebContentsObserver {
22     // TODO(jdduke): Remove the destroy method and hold observer embedders
23     // responsible for explicit observer detachment.
24     // Using a weak reference avoids cycles that might prevent GC of WebView's WebContents.
25     protected WeakReference<WebContents> mWebContents;
26 
WebContentsObserver(WebContents webContents)27     public WebContentsObserver(WebContents webContents) {
28         mWebContents = new WeakReference<WebContents>(webContents);
29         webContents.addObserver(this);
30     }
31 
32     /**
33      * Called when a RenderFrame for renderFrameHost is created in the
34      * renderer process.
35      * To avoid creating a RenderFrameHost object without necessity, only process id and frame id
36      * are passed. Call WebContents#getRenderFrameHostFromId() to get the RenderFrameHostImpl object
37      * if needed.
38      */
renderFrameCreated(int renderProcessId, int renderFrameId)39     public void renderFrameCreated(int renderProcessId, int renderFrameId) {}
40 
41     /**
42      * Called when a RenderFrame for renderFrameHost is deleted in the
43      * renderer process.
44      * To avoid creating a RenderFrameHost object without necessity, only process id and frame id
45      * are passed. Call WebContents#getRenderFrameHostFromId() to get the RenderFrameHostImpl object
46      * if needed.
47      */
renderFrameDeleted(int renderProcessId, int renderFrameId)48     public void renderFrameDeleted(int renderProcessId, int renderFrameId) {}
49 
50     /**
51      * Called when the RenderView of the current RenderViewHost is ready, e.g. because we recreated
52      * it after a crash.
53      */
renderViewReady()54     public void renderViewReady() {}
55 
renderProcessGone(boolean wasOomProtected)56     public void renderProcessGone(boolean wasOomProtected) {}
57 
58     /**
59      * Called when the browser process starts a navigation.
60      * @param navigationHandle
61      *        NavigationHandle are provided to several WebContentsObserver methods to allow
62      *        observers to track specific navigations. Observers should clear any references to a
63      *        NavigationHandle at didFinishNavigation();
64      */
didStartNavigation(NavigationHandle navigationHandle)65     public void didStartNavigation(NavigationHandle navigationHandle) {}
66 
67     /**
68      * Called when the browser process redirect a navigation.
69      * @param navigationHandle
70      *        NavigationHandle are provided to several WebContentsObserver methods to allow
71      *        observers to track specific navigations. Observers should clear any references to a
72      *        NavigationHandle at didFinishNavigation();
73      */
didRedirectNavigation(NavigationHandle navigationHandle)74     public void didRedirectNavigation(NavigationHandle navigationHandle) {}
75 
76     /**
77      * Called when the current navigation is finished. This happens when a navigation is committed,
78      * aborted or replaced by a new one.
79      * @param navigationHandle
80      *        NavigationHandle are provided to several WebContentsObserver methods to allow
81      *        observers to track specific navigations. Observers should clear any references to a
82      *        NavigationHandle at the end of this function.
83      */
didFinishNavigation(NavigationHandle navigationHandle)84     public void didFinishNavigation(NavigationHandle navigationHandle) {}
85 
86     /**
87      * Called when the a page starts loading.
88      * @param url The validated url for the loading page.
89      */
didStartLoading(String url)90     public void didStartLoading(String url) {}
91 
92     /**
93      * Called when the a page finishes loading.
94      * @param url The validated url for the page.
95      */
didStopLoading(String url)96     public void didStopLoading(String url) {}
97 
98     /**
99      * Called when a page's load progress has changed.
100      * @param progress The load progress in the range of [0,1].
101      */
loadProgressChanged(float progress)102     public void loadProgressChanged(float progress) {}
103 
104     /**
105      * Called when a page's visible security state has changed.
106      */
didChangeVisibleSecurityState()107     public void didChangeVisibleSecurityState() {}
108 
109     /**
110      * Called when an error occurs while loading a page and/or the page fails to load.
111      * @param isMainFrame Whether the navigation occurred in main frame.
112      * @param errorCode Error code for the occurring error.
113      * @param failingUrl The url that was loading when the error occurred.
114      */
didFailLoad(boolean isMainFrame, int errorCode, String failingUrl)115     public void didFailLoad(boolean isMainFrame, int errorCode, String failingUrl) {}
116 
117     /**
118      * Called when the page had painted something non-empty.
119      */
didFirstVisuallyNonEmptyPaint()120     public void didFirstVisuallyNonEmptyPaint() {}
121 
122     /**
123      * The web contents was shown.
124      */
wasShown()125     public void wasShown() {}
126 
127     /**
128      * The web contents was hidden.
129      */
wasHidden()130     public void wasHidden() {}
131 
132     /**
133      * Title was set.
134      * @param title The updated title.
135      */
titleWasSet(String title)136     public void titleWasSet(String title) {}
137 
138     /**
139      * Called once the window.document object of the main frame was created.
140      */
documentAvailableInMainFrame()141     public void documentAvailableInMainFrame() {}
142 
143     /**
144      * Notifies that a load has finished for a given frame.
145      * @param frameId A positive, non-zero integer identifying the navigating frame.
146      * @param validatedUrl The validated URL that is being navigated to.
147      * @param isMainFrame Whether the load is happening for the main frame.
148      */
didFinishLoad(long frameId, String validatedUrl, boolean isMainFrame)149     public void didFinishLoad(long frameId, String validatedUrl, boolean isMainFrame) {}
150 
151     /**
152      * Notifies that the document has finished loading for the given frame.
153      * @param frameId A positive, non-zero integer identifying the navigating frame.
154      */
documentLoadedInFrame(long frameId, boolean isMainFrame)155     public void documentLoadedInFrame(long frameId, boolean isMainFrame) {}
156 
157     /**
158      * Notifies that a navigation entry has been committed.
159      */
navigationEntryCommitted()160     public void navigationEntryCommitted() {}
161 
162     /**
163      * Called when navigation entries were removed.
164      */
navigationEntriesDeleted()165     public void navigationEntriesDeleted() {}
166 
167     /**
168      * Called when navigation entries were changed.
169      */
navigationEntriesChanged()170     public void navigationEntriesChanged() {}
171 
172     /**
173      * Called when the theme color was changed.
174      */
didChangeThemeColor()175     public void didChangeThemeColor() {}
176 
177     /**
178      * Called when the Web Contents leaves or enters fullscreen mode.
179      * @param isFullscreen whether fullscreen is being entered or left.
180      */
hasEffectivelyFullscreenVideoChange(boolean isFullscreen)181     public void hasEffectivelyFullscreenVideoChange(boolean isFullscreen) {}
182 
183     /**
184      * The Viewport Fit Type passed to viewportFitChanged. This is mirrored
185      * in an enum in display_cutout.mojom.
186      */
187     @Retention(RetentionPolicy.SOURCE)
188     @IntDef({ViewportFit.AUTO, ViewportFit.CONTAIN, ViewportFit.COVER})
189     public @interface ViewportFitType {}
190 
191     /**
192      * Called when the viewport fit of the Web Contents changes.
193      * @param value the new viewport fit value.
194      */
viewportFitChanged(@iewportFitType int value)195     public void viewportFitChanged(@ViewportFitType int value) {}
196 
197     /**
198      * This method is invoked when a RenderWidgetHost for a WebContents gains focus.
199      */
onWebContentsFocused()200     public void onWebContentsFocused() {}
201 
202     /**
203      * This method is invoked when a RenderWidgetHost for a WebContents loses focus. This may
204      * be immediately followed by onWebContentsFocused if focus was moving between two
205      * RenderWidgetHosts within the same WebContents.
206      */
onWebContentsLostFocus()207     public void onWebContentsLostFocus() {}
208 
209     /** Called when the top level WindowAndroid changes. */
onTopLevelNativeWindowChanged(@ullable WindowAndroid windowAndroid)210     public void onTopLevelNativeWindowChanged(@Nullable WindowAndroid windowAndroid) {}
211 
212     /**
213      * Stop observing the web contents and clean up associated references.
214      */
destroy()215     public void destroy() {
216         if (mWebContents == null) return;
217         final WebContents webContents = mWebContents.get();
218         mWebContents = null;
219         if (webContents == null) return;
220         webContents.removeObserver(this);
221     }
222 
WebContentsObserver()223     protected WebContentsObserver() {}
224 }
225