Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / public / browser / web_contents_observer.h
blob737b16a2384a571aa20bf0ef7cf4fa2c19021b3b
1 // Copyright (c) 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.
5 #ifndef CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_OBSERVER_H_
6 #define CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_OBSERVER_H_
8 #include "base/process/kill.h"
9 #include "base/process/process_handle.h"
10 #include "content/common/content_export.h"
11 #include "content/public/browser/navigation_controller.h"
12 #include "content/public/common/frame_navigate_params.h"
13 #include "ipc/ipc_listener.h"
14 #include "ipc/ipc_sender.h"
15 #include "third_party/skia/include/core/SkColor.h"
16 #include "ui/base/page_transition_types.h"
17 #include "ui/base/window_open_disposition.h"
19 namespace content {
21 class NavigationEntry;
22 class RenderFrameHost;
23 class RenderViewHost;
24 class WebContents;
25 class WebContentsImpl;
26 struct AXEventNotificationDetails;
27 struct FaviconURL;
28 struct FrameNavigateParams;
29 struct LoadCommittedDetails;
30 struct LoadFromMemoryCacheDetails;
31 struct Referrer;
32 struct ResourceRedirectDetails;
33 struct ResourceRequestDetails;
35 // An observer API implemented by classes which are interested in various page
36 // load events from WebContents. They also get a chance to filter IPC messages.
38 // Since a WebContents can be a delegate to almost arbitrarily many
39 // RenderViewHosts, it is important to check in those WebContentsObserver
40 // methods which take a RenderViewHost that the event came from the
41 // RenderViewHost the observer cares about.
43 // Usually, observers should only care about the current RenderViewHost as
44 // returned by GetRenderViewHost().
46 // TODO(creis, jochen): Hide the fact that there are several RenderViewHosts
47 // from the WebContentsObserver API. http://crbug.com/173325
48 class CONTENT_EXPORT WebContentsObserver : public IPC::Listener,
49 public IPC::Sender {
50 public:
51 // Called when a RenderFrame for |render_frame_host| is created in the
52 // renderer process. Use |RenderFrameDeleted| to listen for when this
53 // RenderFrame goes away.
54 virtual void RenderFrameCreated(RenderFrameHost* render_frame_host) {}
56 // Called when a RenderFrame for |render_frame_host| is deleted or the
57 // renderer process in which it runs it has died. Use |RenderFrameCreated| to
58 // listen for when RenderFrame objects are created.
59 virtual void RenderFrameDeleted(RenderFrameHost* render_frame_host) {}
61 // This method is invoked whenever one of the current frames of a WebContents
62 // swaps its RenderFrameHost with another one; for example because that frame
63 // navigated and the new content is in a different process. The
64 // RenderFrameHost that has been replaced is in |old_host|, which can be
65 // nullptr if the old RenderFrameHost was shut down or a new frame has been
66 // created and no old RenderFrameHost exists.
68 // This method, in combination with |FrameDeleted|, is appropriate for
69 // observers wishing to track the set of current RenderFrameHosts -- i.e.,
70 // those hosts that would be visited by calling WebContents::ForEachFrame.
71 virtual void RenderFrameHostChanged(RenderFrameHost* old_host,
72 RenderFrameHost* new_host) {}
74 // This method is invoked when a subframe associated with a WebContents is
75 // deleted or the WebContents is destroyed and the top-level frame is deleted.
76 // Use |RenderFrameHostChanged| to listen for when a RenderFrameHost object is
77 // made the current host for a frame.
78 virtual void FrameDeleted(RenderFrameHost* render_frame_host) {}
80 // This is called when a RVH is created for a WebContents, but not if it's an
81 // interstitial.
82 virtual void RenderViewCreated(RenderViewHost* render_view_host) {}
84 // Called for every RenderFrameHost that's created for an interstitial.
85 virtual void RenderFrameForInterstitialPageCreated(
86 RenderFrameHost* render_frame_host) {}
88 // This method is invoked when the RenderView of the current RenderViewHost
89 // is ready, e.g. because we recreated it after a crash.
90 virtual void RenderViewReady() {}
92 // This method is invoked when a RenderViewHost of the WebContents is
93 // deleted. Note that this does not always happen when the WebContents starts
94 // to use a different RenderViewHost, as the old RenderViewHost might get
95 // just swapped out.
96 virtual void RenderViewDeleted(RenderViewHost* render_view_host) {}
98 // This method is invoked when the process for the current RenderView crashes.
99 // The WebContents continues to use the RenderViewHost, e.g. when the user
100 // reloads the current page. When the RenderViewHost itself is deleted, the
101 // RenderViewDeleted method will be invoked.
103 // Note that this is equivalent to
104 // RenderProcessHostObserver::RenderProcessExited().
105 virtual void RenderProcessGone(base::TerminationStatus status) {}
107 // This method is invoked when a WebContents swaps its visible RenderViewHost
108 // with another one, possibly changing processes. The RenderViewHost that has
109 // been replaced is in |old_host|, which is nullptr if the old RVH was shut
110 // down.
111 virtual void RenderViewHostChanged(RenderViewHost* old_host,
112 RenderViewHost* new_host) {}
114 // This method is invoked after the WebContents decides which RenderFrameHost
115 // to use for the next browser-initiated navigation, but before the navigation
116 // starts. It is not called for most renderer-initiated navigations, and it
117 // does not guarantee that the navigation will commit (e.g., 204s, downloads).
119 // DEPRECATED. This method is difficult to use correctly and should be
120 // removed. TODO(creis): Remove in http://crbug.com/424641.
121 virtual void AboutToNavigateRenderFrame(RenderFrameHost* old_host,
122 RenderFrameHost* new_host) {}
124 // This method is invoked after the browser process starts a navigation to a
125 // pending NavigationEntry. It is not called for renderer-initiated
126 // navigations unless they are sent to the browser process via OpenURL. It may
127 // be called multiple times for a given navigation, such as a typed URL
128 // followed by a cross-process client or server redirect.
129 virtual void DidStartNavigationToPendingEntry(
130 const GURL& url,
131 NavigationController::ReloadType reload_type) {}
133 // |render_frame_host| is the RenderFrameHost for which the provisional load
134 // is happening.
136 // Since the URL validation will strip error URLs, or srcdoc URLs, the boolean
137 // flags |is_error_page| and |is_iframe_srcdoc| will indicate that the not
138 // validated URL was either an error page or an iframe srcdoc.
140 // Note that during a cross-process navigation, several provisional loads
141 // can be on-going in parallel.
142 virtual void DidStartProvisionalLoadForFrame(
143 RenderFrameHost* render_frame_host,
144 const GURL& validated_url,
145 bool is_error_page,
146 bool is_iframe_srcdoc) {}
148 // This method is invoked when the provisional load was successfully
149 // committed.
151 // If the navigation only changed the reference fragment, or was triggered
152 // using the history API (e.g. window.history.replaceState), we will receive
153 // this signal without a prior DidStartProvisionalLoadForFrame signal.
154 virtual void DidCommitProvisionalLoadForFrame(
155 RenderFrameHost* render_frame_host,
156 const GURL& url,
157 ui::PageTransition transition_type) {}
159 // This method is invoked when the provisional load failed.
160 virtual void DidFailProvisionalLoad(
161 RenderFrameHost* render_frame_host,
162 const GURL& validated_url,
163 int error_code,
164 const base::string16& error_description) {}
166 // If the provisional load corresponded to the main frame, this method is
167 // invoked in addition to DidCommitProvisionalLoadForFrame.
168 virtual void DidNavigateMainFrame(
169 const LoadCommittedDetails& details,
170 const FrameNavigateParams& params) {}
172 // And regardless of what frame navigated, this method is invoked after
173 // DidCommitProvisionalLoadForFrame was invoked.
174 virtual void DidNavigateAnyFrame(
175 RenderFrameHost* render_frame_host,
176 const LoadCommittedDetails& details,
177 const FrameNavigateParams& params) {}
179 // This method is invoked once the window.document object of the main frame
180 // was created.
181 virtual void DocumentAvailableInMainFrame() {}
183 // This method is invoked once the onload handler of the main frame has
184 // completed.
185 virtual void DocumentOnLoadCompletedInMainFrame() {}
187 // This method is invoked when the document in the given frame finished
188 // loading. At this point, scripts marked as defer were executed, and
189 // content scripts marked "document_end" get injected into the frame.
190 virtual void DocumentLoadedInFrame(RenderFrameHost* render_frame_host) {}
192 // This method is invoked when the navigation is done, i.e. the spinner of
193 // the tab will stop spinning, and the onload event was dispatched.
195 // If the WebContents is displaying replacement content, e.g. network error
196 // pages, DidFinishLoad is invoked for frames that were not sending
197 // navigational events before. It is safe to ignore these events.
198 virtual void DidFinishLoad(RenderFrameHost* render_frame_host,
199 const GURL& validated_url) {}
201 // This method is like DidFinishLoad, but when the load failed or was
202 // cancelled, e.g. window.stop() is invoked.
203 virtual void DidFailLoad(RenderFrameHost* render_frame_host,
204 const GURL& validated_url,
205 int error_code,
206 const base::string16& error_description) {}
208 // This method is invoked when content was loaded from an in-memory cache.
209 virtual void DidLoadResourceFromMemoryCache(
210 const LoadFromMemoryCacheDetails& details) {}
212 // This method is invoked when a response has been received for a resource
213 // request.
214 virtual void DidGetResourceResponseStart(
215 const ResourceRequestDetails& details) {}
217 // This method is invoked when a redirect was received while requesting a
218 // resource.
219 virtual void DidGetRedirectForResourceRequest(
220 RenderFrameHost* render_frame_host,
221 const ResourceRedirectDetails& details) {}
223 // This method is invoked when a new non-pending navigation entry is created.
224 // This corresponds to one NavigationController entry being created
225 // (in the case of new navigations) or renavigated to (for back/forward
226 // navigations).
227 virtual void NavigationEntryCommitted(
228 const LoadCommittedDetails& load_details) {}
230 // This method is invoked when a new WebContents was created in response to
231 // an action in the observed WebContents, e.g. a link with target=_blank was
232 // clicked. The |source_render_frame_host| is the frame in which the action
233 // took place.
234 virtual void DidOpenRequestedURL(WebContents* new_contents,
235 RenderFrameHost* source_render_frame_host,
236 const GURL& url,
237 const Referrer& referrer,
238 WindowOpenDisposition disposition,
239 ui::PageTransition transition) {}
241 // This method is invoked when the renderer process has completed its first
242 // paint after a non-empty layout.
243 virtual void DidFirstVisuallyNonEmptyPaint() {}
245 // These two methods correspond to the points in time when the spinner of the
246 // tab starts and stops spinning.
247 virtual void DidStartLoading() {}
248 virtual void DidStopLoading() {}
250 // When WebContents::Stop() is called, the WebContents stops loading and then
251 // invokes this method. If there are ongoing navigations, their respective
252 // failure methods will also be invoked.
253 virtual void NavigationStopped() {}
255 // This indicates that the next navigation was triggered by a user gesture.
256 virtual void DidGetUserGesture() {}
258 // This method is invoked when a RenderViewHost of this WebContents was
259 // configured to ignore UI events, and an UI event took place.
260 virtual void DidGetIgnoredUIEvent() {}
262 // These methods are invoked every time the WebContents changes visibility.
263 virtual void WasShown() {}
264 virtual void WasHidden() {}
266 // Invoked when the main frame changes size.
267 virtual void MainFrameWasResized(bool width_changed) {}
269 // Invoked when the given frame changes its window.name property.
270 virtual void FrameNameChanged(RenderFrameHost* render_frame_host,
271 const std::string& name) {}
273 // This methods is invoked when the title of the WebContents is set. If the
274 // title was explicitly set, |explicit_set| is true, otherwise the title was
275 // synthesized and |explicit_set| is false.
276 virtual void TitleWasSet(NavigationEntry* entry, bool explicit_set) {}
278 virtual void AppCacheAccessed(const GURL& manifest_url,
279 bool blocked_by_policy) {}
281 // Notification that a plugin has crashed.
282 // |plugin_pid| is the process ID identifying the plugin process. Note that
283 // this ID is supplied by the renderer process, so should not be trusted.
284 // Besides, the corresponding process has probably died at this point. The ID
285 // may even have been reused by a new process.
286 virtual void PluginCrashed(const base::FilePath& plugin_path,
287 base::ProcessId plugin_pid) {}
289 // Notification that the given plugin has hung or become unhung. This
290 // notification is only for Pepper plugins.
292 // The plugin_child_id is the unique child process ID from the plugin. Note
293 // that this ID is supplied by the renderer process, so should be validated
294 // before it's used for anything in case there's an exploited renderer
295 // process.
296 virtual void PluginHungStatusChanged(int plugin_child_id,
297 const base::FilePath& plugin_path,
298 bool is_hung) {}
300 // Invoked when WebContents::Clone() was used to clone a WebContents.
301 virtual void DidCloneToNewWebContents(WebContents* old_web_contents,
302 WebContents* new_web_contents) {}
304 // Invoked when the WebContents is being destroyed. Gives subclasses a chance
305 // to cleanup. After the whole loop over all WebContentsObservers has been
306 // finished, web_contents() returns nullptr.
307 virtual void WebContentsDestroyed() {}
309 // Called when the user agent override for a WebContents has been changed.
310 virtual void UserAgentOverrideSet(const std::string& user_agent) {}
312 // Invoked when new FaviconURL candidates are received from the renderer
313 // process.
314 virtual void DidUpdateFaviconURL(const std::vector<FaviconURL>& candidates) {}
316 // Invoked when a pepper plugin creates and shows or destroys a fullscreen
317 // RenderWidget.
318 virtual void DidShowFullscreenWidget(int routing_id) {}
319 virtual void DidDestroyFullscreenWidget(int routing_id) {}
321 // Invoked when the renderer process has toggled the tab into/out of
322 // fullscreen mode.
323 virtual void DidToggleFullscreenModeForTab(bool entered_fullscreen) {}
325 // Invoked when an interstitial page is attached or detached.
326 virtual void DidAttachInterstitialPage() {}
327 virtual void DidDetachInterstitialPage() {}
329 // Invoked before a form repost warning is shown.
330 virtual void BeforeFormRepostWarningShow() {}
332 // Invoked when the beforeunload handler fires. The time is from the renderer
333 // process.
334 virtual void BeforeUnloadFired(const base::TimeTicks& proceed_time) {}
336 // Invoked when a user cancels a before unload dialog.
337 virtual void BeforeUnloadDialogCancelled() {}
339 // Invoked when an accessibility event is received from the renderer process.
340 virtual void AccessibilityEventReceived(
341 const std::vector<AXEventNotificationDetails>& details) {}
343 // Invoked when theme color is changed to |theme_color|.
344 virtual void DidChangeThemeColor(SkColor theme_color) {}
346 // Invoked when media is playing.
347 virtual void MediaStartedPlaying() {}
349 // Invoked when media is paused.
350 virtual void MediaPaused() {}
352 // Invoked if an IPC message is coming from a specific RenderFrameHost.
353 virtual bool OnMessageReceived(const IPC::Message& message,
354 RenderFrameHost* render_frame_host);
356 // IPC::Listener implementation.
357 bool OnMessageReceived(const IPC::Message& message) override;
359 // IPC::Sender implementation.
360 bool Send(IPC::Message* message) override;
361 int routing_id() const;
363 WebContents* web_contents() const;
365 protected:
366 // Use this constructor when the object is tied to a single WebContents for
367 // its entire lifetime.
368 explicit WebContentsObserver(WebContents* web_contents);
370 // Use this constructor when the object wants to observe a WebContents for
371 // part of its lifetime. It can then call Observe() to start and stop
372 // observing.
373 WebContentsObserver();
375 ~WebContentsObserver() override;
377 // Start observing a different WebContents; used with the default constructor.
378 void Observe(WebContents* web_contents);
380 private:
381 friend class WebContentsImpl;
383 void ResetWebContents();
385 WebContentsImpl* web_contents_;
387 DISALLOW_COPY_AND_ASSIGN(WebContentsObserver);
390 } // namespace content
392 #endif // CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_OBSERVER_H_