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