Move DidCommitProvisionalLoad code from RenderView to RenderFrame.
[chromium-blink-merge.git] / content / public / browser / web_contents_observer.h
blobae71eec2af2f1f77a29ea4302c66ce2986062fbd
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/page_transition_types.h"
14 #include "ipc/ipc_listener.h"
15 #include "ipc/ipc_sender.h"
16 #include "ui/base/window_open_disposition.h"
18 namespace content {
20 class NavigationEntry;
21 class RenderFrameHost;
22 class RenderViewHost;
23 class WebContents;
24 class WebContentsImpl;
25 struct FaviconURL;
26 struct FrameNavigateParams;
27 struct LoadCommittedDetails;
28 struct LoadFromMemoryCacheDetails;
29 struct Referrer;
30 struct ResourceRedirectDetails;
31 struct ResourceRequestDetails;
33 // An observer API implemented by classes which are interested in various page
34 // load events from WebContents. They also get a chance to filter IPC messages.
36 // Since a WebContents can be a delegate to almost arbitrarily many
37 // RenderViewHosts, it is important to check in those WebContentsObserver
38 // methods which take a RenderViewHost that the event came from the
39 // RenderViewHost the observer cares about.
41 // Usually, observers should only care about the current RenderViewHost as
42 // returned by GetRenderViewHost().
44 // TODO(creis, jochen): Hide the fact that there are several RenderViewHosts
45 // from the WebContentsObserver API. http://crbug.com/173325
46 class CONTENT_EXPORT WebContentsObserver : public IPC::Listener,
47 public IPC::Sender {
48 public:
49 // Called when a RenderFrameHost associated with this WebContents is created.
50 virtual void RenderFrameCreated(RenderFrameHost* render_frame_host) {}
52 // Called whenever a RenderFrameHost associated with this WebContents is
53 // deleted.
54 virtual void RenderFrameDeleted(RenderFrameHost* render_frame_host) {}
56 // This is called when a RVH is created for a WebContents, but not if it's an
57 // interstitial.
58 virtual void RenderViewCreated(RenderViewHost* render_view_host) {}
60 // Called for every RenderFrameHost that's created for an interstitial.
61 virtual void RenderFrameForInterstitialPageCreated(
62 RenderFrameHost* render_frame_host) {}
64 // This method is invoked when the RenderView of the current RenderViewHost
65 // is ready, e.g. because we recreated it after a crash.
66 virtual void RenderViewReady() {}
68 // This method is invoked when a RenderViewHost of the WebContents is
69 // deleted. Note that this does not always happen when the WebContents starts
70 // to use a different RenderViewHost, as the old RenderViewHost might get
71 // just swapped out.
72 virtual void RenderViewDeleted(RenderViewHost* render_view_host) {}
74 // This method is invoked when the process for the current RenderView crashes.
75 // The WebContents continues to use the RenderViewHost, e.g. when the user
76 // reloads the current page. When the RenderViewHost itself is deleted, the
77 // RenderViewDeleted method will be invoked.
79 // Note that this is equivalent to
80 // RenderProcessHostObserver::RenderProcessExited().
81 virtual void RenderProcessGone(base::TerminationStatus status) {}
83 // This method is invoked when a WebContents swaps its render view host with
84 // another one, possibly changing processes. The RenderViewHost that has
85 // been replaced is in |old_render_view_host|, which is NULL if the old RVH
86 // was shut down.
87 virtual void RenderViewHostChanged(RenderViewHost* old_host,
88 RenderViewHost* new_host) {}
90 // This method is invoked after the WebContents decided which RenderViewHost
91 // to use for the next navigation, but before the navigation starts.
92 virtual void AboutToNavigateRenderView(
93 RenderViewHost* render_view_host) {}
95 // This method is invoked after the browser process starts a navigation to a
96 // pending NavigationEntry. It is not called for renderer-initiated
97 // navigations unless they are sent to the browser process via OpenURL. It may
98 // be called multiple times for a given navigation, such as a typed URL
99 // followed by a cross-process client or server redirect.
100 virtual void DidStartNavigationToPendingEntry(
101 const GURL& url,
102 NavigationController::ReloadType reload_type) {}
104 // |render_view_host| is the RenderViewHost for which the provisional load is
105 // happening. |frame_id| is a positive, non-zero integer identifying the
106 // navigating frame in the given |render_view_host|. |parent_frame_id| is the
107 // frame identifier of the frame containing the navigating frame, or -1 if the
108 // frame is not contained in another frame.
110 // Since the URL validation will strip error URLs, or srcdoc URLs, the boolean
111 // flags |is_error_page| and |is_iframe_srcdoc| will indicate that the not
112 // validated URL was either an error page or an iframe srcdoc.
114 // Note that during a cross-process navigation, several provisional loads
115 // can be on-going in parallel.
116 virtual void DidStartProvisionalLoadForFrame(
117 int64 frame_id,
118 int64 parent_frame_id,
119 bool is_main_frame,
120 const GURL& validated_url,
121 bool is_error_page,
122 bool is_iframe_srcdoc,
123 RenderViewHost* render_view_host) {}
125 // This method is invoked right after the DidStartProvisionalLoadForFrame if
126 // the provisional load affects the main frame, or if the provisional load
127 // was redirected. The latter use case is DEPRECATED. You should listen to
128 // WebContentsObserver::DidGetRedirectForResourceRequest instead.
129 virtual void ProvisionalChangeToMainFrameUrl(
130 const GURL& url,
131 RenderFrameHost* render_frame_host) {}
133 // This method is invoked when the provisional load was successfully
134 // committed. The |render_view_host| is now the current RenderViewHost of the
135 // WebContents.
137 // If the navigation only changed the reference fragment, or was triggered
138 // using the history API (e.g. window.history.replaceState), we will receive
139 // this signal without a prior DidStartProvisionalLoadForFrame signal.
140 virtual void DidCommitProvisionalLoadForFrame(
141 int64 frame_id,
142 const base::string16& frame_unique_name,
143 bool is_main_frame,
144 const GURL& url,
145 PageTransition transition_type,
146 RenderViewHost* render_view_host) {}
148 // This method is invoked when the provisional load failed.
149 virtual void DidFailProvisionalLoad(int64 frame_id,
150 const base::string16& frame_unique_name,
151 bool is_main_frame,
152 const GURL& validated_url,
153 int error_code,
154 const base::string16& error_description,
155 RenderViewHost* render_view_host) {}
157 // If the provisional load corresponded to the main frame, this method is
158 // invoked in addition to DidCommitProvisionalLoadForFrame.
159 virtual void DidNavigateMainFrame(
160 const LoadCommittedDetails& details,
161 const FrameNavigateParams& params) {}
163 // And regardless of what frame navigated, this method is invoked after
164 // DidCommitProvisionalLoadForFrame was invoked.
165 virtual void DidNavigateAnyFrame(
166 const LoadCommittedDetails& details,
167 const FrameNavigateParams& params) {}
169 // This method is invoked once the window.document object of the main frame
170 // was created.
171 virtual void DocumentAvailableInMainFrame() {}
173 // This method is invoked once the onload handler of the main frame has
174 // completed.
175 virtual void DocumentOnLoadCompletedInMainFrame(int32 page_id) {}
177 // This method is invoked when the document in the given frame finished
178 // loading. At this point, scripts marked as defer were executed, and
179 // content scripts marked "document_end" get injected into the frame.
180 virtual void DocumentLoadedInFrame(int64 frame_id,
181 RenderViewHost* render_view_host) {}
183 // This method is invoked when the navigation is done, i.e. the spinner of
184 // the tab will stop spinning, and the onload event was dispatched.
186 // If the WebContents is displaying replacement content, e.g. network error
187 // pages, DidFinishLoad is invoked for frames that were not sending
188 // navigational events before. It is safe to ignore these events.
189 virtual void DidFinishLoad(int64 frame_id,
190 const GURL& validated_url,
191 bool is_main_frame,
192 RenderViewHost* render_view_host) {}
194 // This method is like DidFinishLoad, but when the load failed or was
195 // cancelled, e.g. window.stop() is invoked.
196 virtual void DidFailLoad(int64 frame_id,
197 const GURL& validated_url,
198 bool is_main_frame,
199 int error_code,
200 const base::string16& error_description,
201 RenderViewHost* render_view_host) {}
203 // This method is invoked when content was loaded from an in-memory cache.
204 virtual void DidLoadResourceFromMemoryCache(
205 const LoadFromMemoryCacheDetails& details) {}
207 // This method is invoked when a response has been received for a resource
208 // request.
209 virtual void DidGetResourceResponseStart(
210 const ResourceRequestDetails& details) {}
212 // This method is invoked when a redirect was received while requesting a
213 // resource.
214 virtual void DidGetRedirectForResourceRequest(
215 RenderViewHost* render_view_host,
216 const ResourceRedirectDetails& details) {}
218 // This method is invoked when a new non-pending navigation entry is created.
219 // This corresponds to one NavigationController entry being created
220 // (in the case of new navigations) or renavigated to (for back/forward
221 // navigations).
222 virtual void NavigationEntryCommitted(
223 const LoadCommittedDetails& load_details) {}
225 // This method is invoked when a new WebContents was created in response to
226 // an action in the observed WebContents, e.g. a link with target=_blank was
227 // clicked. The |source_frame_id| indicates in which frame the action took
228 // place.
229 virtual void DidOpenRequestedURL(WebContents* new_contents,
230 const GURL& url,
231 const Referrer& referrer,
232 WindowOpenDisposition disposition,
233 PageTransition transition,
234 int64 source_frame_id) {}
236 virtual void FrameDetached(RenderViewHost* render_view_host,
237 int64 frame_id) {}
239 // This method is invoked when the renderer has completed its first paint
240 // after a non-empty layout.
241 virtual void DidFirstVisuallyNonEmptyPaint(int32 page_id) {}
243 // These two methods correspond to the points in time when the spinner of the
244 // tab starts and stops spinning.
245 virtual void DidStartLoading(RenderViewHost* render_view_host) {}
246 virtual void DidStopLoading(RenderViewHost* render_view_host) {}
248 // When WebContents::Stop() is called, the WebContents stops loading and then
249 // invokes this method. If there are ongoing navigations, their respective
250 // failure methods will also be invoked.
251 virtual void NavigationStopped() {}
253 // This indicates that the next navigation was triggered by a user gesture.
254 virtual void DidGetUserGesture() {}
256 // This method is invoked when a RenderViewHost of this WebContents was
257 // configured to ignore UI events, and an UI event took place.
258 virtual void DidGetIgnoredUIEvent() {}
260 // These methods are invoked every time the WebContents changes visibility.
261 virtual void WasShown() {}
262 virtual void WasHidden() {}
264 // This methods is invoked when the title of the WebContents is set. If the
265 // title was explicitly set, |explicit_set| is true, otherwise the title was
266 // synthesized and |explicit_set| is false.
267 virtual void TitleWasSet(NavigationEntry* entry, bool explicit_set) {}
269 virtual void AppCacheAccessed(const GURL& manifest_url,
270 bool blocked_by_policy) {}
272 // Notification that a plugin has crashed.
273 // |plugin_pid| is the process ID identifying the plugin process. Note that
274 // this ID is supplied by the renderer, so should not be trusted. Besides, the
275 // corresponding process has probably died at this point. The ID may even have
276 // been reused by a new process.
277 virtual void PluginCrashed(const base::FilePath& plugin_path,
278 base::ProcessId plugin_pid) {}
280 // Notification that the given plugin has hung or become unhung. This
281 // notification is only for Pepper plugins.
283 // The plugin_child_id is the unique child process ID from the plugin. Note
284 // that this ID is supplied by the renderer, so should be validated before
285 // it's used for anything in case there's an exploited renderer.
286 virtual void PluginHungStatusChanged(int plugin_child_id,
287 const base::FilePath& plugin_path,
288 bool is_hung) {}
290 // Invoked when WebContents::Clone() was used to clone a WebContents.
291 virtual void DidCloneToNewWebContents(WebContents* old_web_contents,
292 WebContents* new_web_contents) {}
294 // Invoked when the WebContents is being destroyed. Gives subclasses a chance
295 // to cleanup. At the time this is invoked |web_contents()| returns NULL.
296 // It is safe to delete 'this' from here.
297 virtual void WebContentsDestroyed(WebContents* web_contents) {}
299 // Called when the user agent override for a WebContents has been changed.
300 virtual void UserAgentOverrideSet(const std::string& user_agent) {}
302 // Invoked when new FaviconURL candidates are received from the renderer.
303 virtual void DidUpdateFaviconURL(int32 page_id,
304 const std::vector<FaviconURL>& candidates) {}
306 // Invoked when a pepper plugin creates and shows or destroys a fullscreen
307 // render widget.
308 virtual void DidShowFullscreenWidget(int routing_id) {}
309 virtual void DidDestroyFullscreenWidget(int routing_id) {}
311 // Invoked when visible SSL state (as defined by SSLStatus) changes.
312 virtual void DidChangeVisibleSSLState() {}
314 // Invoked when an interstitial page is attached or detached.
315 virtual void DidAttachInterstitialPage() {}
316 virtual void DidDetachInterstitialPage() {}
318 // Invoked before a form repost warning is shown.
319 virtual void BeforeFormRepostWarningShow() {}
321 // Invoked when the beforeunload handler fires. The time is from the renderer.
322 virtual void BeforeUnloadFired(const base::TimeTicks& proceed_time) {}
324 // Invoked when a user cancels a before unload dialog.
325 virtual void BeforeUnloadDialogCancelled() {}
327 // IPC::Listener implementation.
328 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
330 // IPC::Sender implementation.
331 virtual bool Send(IPC::Message* message) OVERRIDE;
332 int routing_id() const;
334 protected:
335 // Use this constructor when the object is tied to a single WebContents for
336 // its entire lifetime.
337 explicit WebContentsObserver(WebContents* web_contents);
339 // Use this constructor when the object wants to observe a WebContents for
340 // part of its lifetime. It can then call Observe() to start and stop
341 // observing.
342 WebContentsObserver();
344 virtual ~WebContentsObserver();
346 // Start observing a different WebContents; used with the default constructor.
347 void Observe(WebContents* web_contents);
349 WebContents* web_contents() const;
351 private:
352 friend class WebContentsImpl;
354 // Invoked from WebContentsImpl. Invokes WebContentsDestroyed and NULL out
355 // |web_contents_|.
356 void WebContentsImplDestroyed();
358 WebContentsImpl* web_contents_;
360 DISALLOW_COPY_AND_ASSIGN(WebContentsObserver);
363 } // namespace content
365 #endif // CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_OBSERVER_H_