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