Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / content / public / browser / web_contents_observer.h
blob6a2b1acdd7976fa13a5855cae2faa574cabb1530
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 "content/public/common/security_style.h"
14 #include "ipc/ipc_listener.h"
15 #include "ipc/ipc_sender.h"
16 #include "third_party/skia/include/core/SkColor.h"
17 #include "ui/base/page_transition_types.h"
18 #include "ui/base/window_open_disposition.h"
20 namespace content {
22 class NavigationEntry;
23 class NavigationHandle;
24 class RenderFrameHost;
25 class RenderViewHost;
26 class WebContents;
27 class WebContentsImpl;
28 struct AXEventNotificationDetails;
29 struct FaviconURL;
30 struct FrameNavigateParams;
31 struct LoadCommittedDetails;
32 struct LoadFromMemoryCacheDetails;
33 struct Referrer;
34 struct ResourceRedirectDetails;
35 struct ResourceRequestDetails;
36 struct SecurityStyleExplanations;
38 // An observer API implemented by classes which are interested in various page
39 // load events from WebContents. They also get a chance to filter IPC messages.
41 // Since a WebContents can be a delegate to almost arbitrarily many
42 // RenderViewHosts, it is important to check in those WebContentsObserver
43 // methods which take a RenderViewHost that the event came from the
44 // RenderViewHost the observer cares about.
46 // Usually, observers should only care about the current RenderViewHost as
47 // returned by GetRenderViewHost().
49 // TODO(creis, jochen): Hide the fact that there are several RenderViewHosts
50 // from the WebContentsObserver API. http://crbug.com/173325
51 class CONTENT_EXPORT WebContentsObserver : public IPC::Listener,
52 public IPC::Sender {
53 public:
54 // Called when a RenderFrame for |render_frame_host| is created in the
55 // renderer process. Use |RenderFrameDeleted| to listen for when this
56 // RenderFrame goes away.
57 virtual void RenderFrameCreated(RenderFrameHost* render_frame_host) {}
59 // Called when a RenderFrame for |render_frame_host| is deleted or the
60 // renderer process in which it runs it has died. Use |RenderFrameCreated| to
61 // listen for when RenderFrame objects are created.
62 virtual void RenderFrameDeleted(RenderFrameHost* render_frame_host) {}
64 // This method is invoked whenever one of the current frames of a WebContents
65 // swaps its RenderFrameHost with another one; for example because that frame
66 // navigated and the new content is in a different process. The
67 // RenderFrameHost that has been replaced is in |old_host|, which can be
68 // nullptr if the old RenderFrameHost was shut down or a new frame has been
69 // created and no old RenderFrameHost exists.
71 // This method, in combination with |FrameDeleted|, is appropriate for
72 // observers wishing to track the set of current RenderFrameHosts -- i.e.,
73 // those hosts that would be visited by calling WebContents::ForEachFrame.
74 virtual void RenderFrameHostChanged(RenderFrameHost* old_host,
75 RenderFrameHost* new_host) {}
77 // This method is invoked when a subframe associated with a WebContents is
78 // deleted or the WebContents is destroyed and the top-level frame is deleted.
79 // Use |RenderFrameHostChanged| to listen for when a RenderFrameHost object is
80 // made the current host for a frame.
81 virtual void FrameDeleted(RenderFrameHost* render_frame_host) {}
83 // This is called when a RVH is created for a WebContents, but not if it's an
84 // interstitial.
85 virtual void RenderViewCreated(RenderViewHost* render_view_host) {}
87 // Called for every RenderFrameHost that's created for an interstitial.
88 virtual void RenderFrameForInterstitialPageCreated(
89 RenderFrameHost* render_frame_host) {}
91 // This method is invoked when the RenderView of the current RenderViewHost
92 // is ready, e.g. because we recreated it after a crash.
93 virtual void RenderViewReady() {}
95 // This method is invoked when a RenderViewHost of the WebContents is
96 // deleted. Note that this does not always happen when the WebContents starts
97 // to use a different RenderViewHost, as the old RenderViewHost might get
98 // just swapped out.
99 virtual void RenderViewDeleted(RenderViewHost* render_view_host) {}
101 // This method is invoked when the process for the current main
102 // RenderFrameHost exits (usually by crashing, though possibly by other
103 // means). The WebContents continues to use the RenderFrameHost, e.g. when the
104 // user reloads the current page. When the RenderFrameHost itself is deleted,
105 // the RenderFrameDeleted method will be invoked.
107 // Note that this is triggered upstream through
108 // RenderProcessHostObserver::RenderProcessExited(); for code that doesn't
109 // otherwise need to be a WebContentsObserver, that API is probably a better
110 // choice.
111 virtual void RenderProcessGone(base::TerminationStatus status) {}
113 // This method is invoked when a WebContents swaps its visible RenderViewHost
114 // with another one, possibly changing processes. The RenderViewHost that has
115 // been replaced is in |old_host|, which is nullptr if the old RVH was shut
116 // down.
117 virtual void RenderViewHostChanged(RenderViewHost* old_host,
118 RenderViewHost* new_host) {}
120 // Navigation related events ------------------------------------------------
122 // Called when a navigation started in the WebContents. |navigation_handle|
123 // is unique to a specific navigation. The same |navigation_handle| will be
124 // provided on subsequent calls to
125 // DidRedirect/Commit/FinishNavigation/ReadyToCommitNavigation related to
126 // this navigation.
128 // Note that this is fired by navigations in any frame of the WebContents,
129 // not just the main frame.
131 // Note that more than one navigation can be ongoing in the same frame at the
132 // same time (including the main frame). Each will get its own
133 // NavigationHandle.
135 // Note that there is no guarantee that DidFinishNavigation will be called
136 // for any particular navigation before DidStartNavigation is called on the
137 // next.
138 virtual void DidStartNavigation(NavigationHandle* navigation_handle) {}
140 // Called when a navigation encountered a server redirect.
141 virtual void DidRedirectNavigation(NavigationHandle* navigation_handle) {}
143 // PlzNavigate
144 // Called when the navigation is ready to be committed in a renderer. This is
145 // the first point in time where a RenderFrameHost is associated with the
146 // navigation. Observers that want to initialize any renderer side
147 // structures/state before the RenderFrame is navigated, should use this
148 // method as opposed to DidCommitNavigation, which is after the fact.
149 virtual void ReadyToCommitNavigation(NavigationHandle* navigation_handle) {}
151 // Called when a navigation was committed.
152 virtual void DidCommitNavigation(NavigationHandle* navigation_handle) {}
154 // Called when a navigation stopped in the WebContents. This happens when a
155 // navigation is either aborted, replaced by a new one, or the document load
156 // finishes. Note that |navigation_handle| will be destroyed at the end of
157 // this call, so do not keep a reference to it afterward.
158 virtual void DidFinishNavigation(NavigationHandle* navigation_handle) {}
160 // ---------------------------------------------------------------------------
162 // This method is invoked after the WebContents decides which RenderFrameHost
163 // to use for the next browser-initiated navigation, but before the navigation
164 // starts. It is not called for most renderer-initiated navigations, and it
165 // does not guarantee that the navigation will commit (e.g., 204s, downloads).
167 // DEPRECATED. This method is difficult to use correctly and should be
168 // removed. TODO(creis): Remove in http://crbug.com/424641.
169 virtual void AboutToNavigateRenderFrame(RenderFrameHost* old_host,
170 RenderFrameHost* new_host) {}
172 // This method is invoked after the browser process starts a navigation to a
173 // pending NavigationEntry. It is not called for renderer-initiated
174 // navigations unless they are sent to the browser process via OpenURL. It may
175 // be called multiple times for a given navigation, such as a typed URL
176 // followed by a cross-process client or server redirect.
177 virtual void DidStartNavigationToPendingEntry(
178 const GURL& url,
179 NavigationController::ReloadType reload_type) {}
181 // |render_frame_host| is the RenderFrameHost for which the provisional load
182 // is happening.
184 // Since the URL validation will strip error URLs, or srcdoc URLs, the boolean
185 // flags |is_error_page| and |is_iframe_srcdoc| will indicate that the not
186 // validated URL was either an error page or an iframe srcdoc.
188 // Note that during a cross-process navigation, several provisional loads
189 // can be on-going in parallel.
190 virtual void DidStartProvisionalLoadForFrame(
191 RenderFrameHost* render_frame_host,
192 const GURL& validated_url,
193 bool is_error_page,
194 bool is_iframe_srcdoc) {}
196 // This method is invoked when the provisional load was successfully
197 // committed.
199 // If the navigation only changed the reference fragment, or was triggered
200 // using the history API (e.g. window.history.replaceState), we will receive
201 // this signal without a prior DidStartProvisionalLoadForFrame signal.
202 virtual void DidCommitProvisionalLoadForFrame(
203 RenderFrameHost* render_frame_host,
204 const GURL& url,
205 ui::PageTransition transition_type) {}
207 // This method is invoked when the provisional load failed.
208 virtual void DidFailProvisionalLoad(
209 RenderFrameHost* render_frame_host,
210 const GURL& validated_url,
211 int error_code,
212 const base::string16& error_description,
213 bool was_ignored_by_handler) {}
215 // If the provisional load corresponded to the main frame, this method is
216 // invoked in addition to DidCommitProvisionalLoadForFrame.
217 virtual void DidNavigateMainFrame(
218 const LoadCommittedDetails& details,
219 const FrameNavigateParams& params) {}
221 // And regardless of what frame navigated, this method is invoked after
222 // DidCommitProvisionalLoadForFrame was invoked.
223 virtual void DidNavigateAnyFrame(
224 RenderFrameHost* render_frame_host,
225 const LoadCommittedDetails& details,
226 const FrameNavigateParams& params) {}
228 // This method is invoked when the SecurityStyle of the WebContents changes.
229 // |security_style| is the new SecurityStyle. |security_style_explanations|
230 // contains human-readable strings explaining why the SecurityStyle of the
231 // page has been downgraded.
232 virtual void SecurityStyleChanged(
233 SecurityStyle security_style,
234 const SecurityStyleExplanations& security_style_explanations) {}
236 // This method is invoked once the window.document object of the main frame
237 // was created.
238 virtual void DocumentAvailableInMainFrame() {}
240 // This method is invoked once the onload handler of the main frame has
241 // completed.
242 virtual void DocumentOnLoadCompletedInMainFrame() {}
244 // This method is invoked when the document in the given frame finished
245 // loading. At this point, scripts marked as defer were executed, and
246 // content scripts marked "document_end" get injected into the frame.
247 virtual void DocumentLoadedInFrame(RenderFrameHost* render_frame_host) {}
249 // This method is invoked when the navigation is done, i.e. the spinner of
250 // the tab will stop spinning, and the onload event was dispatched.
252 // If the WebContents is displaying replacement content, e.g. network error
253 // pages, DidFinishLoad is invoked for frames that were not sending
254 // navigational events before. It is safe to ignore these events.
255 virtual void DidFinishLoad(RenderFrameHost* render_frame_host,
256 const GURL& validated_url) {}
258 // This method is like DidFinishLoad, but when the load failed or was
259 // cancelled, e.g. window.stop() is invoked.
260 virtual void DidFailLoad(RenderFrameHost* render_frame_host,
261 const GURL& validated_url,
262 int error_code,
263 const base::string16& error_description,
264 bool was_ignored_by_handler) {}
266 // This method is invoked when content was loaded from an in-memory cache.
267 virtual void DidLoadResourceFromMemoryCache(
268 const LoadFromMemoryCacheDetails& details) {}
270 // This method is invoked when a response has been received for a resource
271 // request.
272 virtual void DidGetResourceResponseStart(
273 const ResourceRequestDetails& details) {}
275 // This method is invoked when a redirect was received while requesting a
276 // resource.
277 virtual void DidGetRedirectForResourceRequest(
278 RenderFrameHost* render_frame_host,
279 const ResourceRedirectDetails& details) {}
281 // This method is invoked when a new non-pending navigation entry is created.
282 // This corresponds to one NavigationController entry being created
283 // (in the case of new navigations) or renavigated to (for back/forward
284 // navigations).
285 virtual void NavigationEntryCommitted(
286 const LoadCommittedDetails& load_details) {}
288 // This method is invoked when a new WebContents was created in response to
289 // an action in the observed WebContents, e.g. a link with target=_blank was
290 // clicked. The |source_render_frame_host| is the frame in which the action
291 // took place.
292 virtual void DidOpenRequestedURL(WebContents* new_contents,
293 RenderFrameHost* source_render_frame_host,
294 const GURL& url,
295 const Referrer& referrer,
296 WindowOpenDisposition disposition,
297 ui::PageTransition transition) {}
299 // This method is invoked when the renderer process has completed its first
300 // paint after a non-empty layout.
301 virtual void DidFirstVisuallyNonEmptyPaint() {}
303 // These two methods correspond to the points in time when the spinner of the
304 // tab starts and stops spinning.
305 virtual void DidStartLoading() {}
306 virtual void DidStopLoading() {}
308 // When WebContents::Stop() is called, the WebContents stops loading and then
309 // invokes this method. If there are ongoing navigations, their respective
310 // failure methods will also be invoked.
311 virtual void NavigationStopped() {}
313 // This indicates that the next navigation was triggered by a user gesture.
314 virtual void DidGetUserGesture() {}
316 // This method is invoked when a RenderViewHost of this WebContents was
317 // configured to ignore UI events, and an UI event took place.
318 virtual void DidGetIgnoredUIEvent() {}
320 // These methods are invoked every time the WebContents changes visibility.
321 virtual void WasShown() {}
322 virtual void WasHidden() {}
324 // Invoked when the main frame changes size.
325 virtual void MainFrameWasResized(bool width_changed) {}
327 // Invoked when the given frame changes its window.name property.
328 virtual void FrameNameChanged(RenderFrameHost* render_frame_host,
329 const std::string& name) {}
331 // This methods is invoked when the title of the WebContents is set. If the
332 // title was explicitly set, |explicit_set| is true, otherwise the title was
333 // synthesized and |explicit_set| is false.
334 virtual void TitleWasSet(NavigationEntry* entry, bool explicit_set) {}
336 virtual void AppCacheAccessed(const GURL& manifest_url,
337 bool blocked_by_policy) {}
339 // These methods are invoked when a Pepper plugin instance is created/deleted
340 // in the DOM.
341 virtual void PepperInstanceCreated() {}
342 virtual void PepperInstanceDeleted() {}
344 // Notification that a plugin has crashed.
345 // |plugin_pid| is the process ID identifying the plugin process. Note that
346 // this ID is supplied by the renderer process, so should not be trusted.
347 // Besides, the corresponding process has probably died at this point. The ID
348 // may even have been reused by a new process.
349 virtual void PluginCrashed(const base::FilePath& plugin_path,
350 base::ProcessId plugin_pid) {}
352 // Notification that the given plugin has hung or become unhung. This
353 // notification is only for Pepper plugins.
355 // The plugin_child_id is the unique child process ID from the plugin. Note
356 // that this ID is supplied by the renderer process, so should be validated
357 // before it's used for anything in case there's an exploited renderer
358 // process.
359 virtual void PluginHungStatusChanged(int plugin_child_id,
360 const base::FilePath& plugin_path,
361 bool is_hung) {}
363 // Invoked when WebContents::Clone() was used to clone a WebContents.
364 virtual void DidCloneToNewWebContents(WebContents* old_web_contents,
365 WebContents* new_web_contents) {}
367 // Invoked when the WebContents is being destroyed. Gives subclasses a chance
368 // to cleanup. After the whole loop over all WebContentsObservers has been
369 // finished, web_contents() returns nullptr.
370 virtual void WebContentsDestroyed() {}
372 // Called when the user agent override for a WebContents has been changed.
373 virtual void UserAgentOverrideSet(const std::string& user_agent) {}
375 // Invoked when new FaviconURL candidates are received from the renderer
376 // process.
377 virtual void DidUpdateFaviconURL(const std::vector<FaviconURL>& candidates) {}
379 // Invoked when a pepper plugin creates and shows or destroys a fullscreen
380 // RenderWidget.
381 virtual void DidShowFullscreenWidget(int routing_id) {}
382 virtual void DidDestroyFullscreenWidget(int routing_id) {}
384 // Invoked when the renderer process has toggled the tab into/out of
385 // fullscreen mode.
386 virtual void DidToggleFullscreenModeForTab(bool entered_fullscreen) {}
388 // Invoked when an interstitial page is attached or detached.
389 virtual void DidAttachInterstitialPage() {}
390 virtual void DidDetachInterstitialPage() {}
392 // Invoked before a form repost warning is shown.
393 virtual void BeforeFormRepostWarningShow() {}
395 // Invoked when the beforeunload handler fires. The time is from the renderer
396 // process.
397 virtual void BeforeUnloadFired(const base::TimeTicks& proceed_time) {}
399 // Invoked when a user cancels a before unload dialog.
400 virtual void BeforeUnloadDialogCancelled() {}
402 // Invoked when an accessibility event is received from the renderer process.
403 virtual void AccessibilityEventReceived(
404 const std::vector<AXEventNotificationDetails>& details) {}
406 // Invoked when theme color is changed to |theme_color|.
407 virtual void DidChangeThemeColor(SkColor theme_color) {}
409 // Invoked when media is playing.
410 virtual void MediaStartedPlaying() {}
412 // Invoked when media is paused.
413 virtual void MediaPaused() {}
415 // Invoked when media session has changed its state.
416 virtual void MediaSessionStateChanged(bool is_controllable,
417 bool is_suspended) {}
419 // Invoked if an IPC message is coming from a specific RenderFrameHost.
420 virtual bool OnMessageReceived(const IPC::Message& message,
421 RenderFrameHost* render_frame_host);
423 // Notification that |contents| has gained focus.
424 virtual void OnWebContentsFocused() {}
426 // IPC::Listener implementation.
427 bool OnMessageReceived(const IPC::Message& message) override;
429 // IPC::Sender implementation.
430 bool Send(IPC::Message* message) override;
431 int routing_id() const;
433 WebContents* web_contents() const;
435 protected:
436 // Use this constructor when the object is tied to a single WebContents for
437 // its entire lifetime.
438 explicit WebContentsObserver(WebContents* web_contents);
440 // Use this constructor when the object wants to observe a WebContents for
441 // part of its lifetime. It can then call Observe() to start and stop
442 // observing.
443 WebContentsObserver();
445 ~WebContentsObserver() override;
447 // Start observing a different WebContents; used with the default constructor.
448 void Observe(WebContents* web_contents);
450 private:
451 friend class WebContentsImpl;
453 void ResetWebContents();
455 WebContentsImpl* web_contents_;
457 DISALLOW_COPY_AND_ASSIGN(WebContentsObserver);
460 } // namespace content
462 #endif // CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_OBSERVER_H_