Content settings: remove some plugin-related code/resources when... there are no...
[chromium-blink-merge.git] / content / public / browser / web_contents_observer.h
blobd310cb229e485af2d66577fff629b5c71e791334
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 DidRedirect/Commit/FinishNavigation
125 // related to this navigation.
127 // Note that this is fired by navigations in any frame of the WebContents,
128 // not just the main frame.
130 // Note that more than one navigation can be ongoing in the same frame at the
131 // same time (including the main frame). Each will get its own
132 // NavigationHandle.
134 // Note that there is no guarantee that DidFinishNavigation will be called
135 // for any particular navigation before DidStartNavigation is called on the
136 // next.
137 virtual void DidStartNavigation(NavigationHandle* navigation_handle) {}
139 // Called when a navigation encountered a server redirect.
140 virtual void DidRedirectNavigation(NavigationHandle* navigation_handle) {}
142 // Called when a navigation was committed.
143 virtual void DidCommitNavigation(NavigationHandle* navigation_handle) {}
145 // Called when a navigation stopped in the WebContents. This happens when a
146 // navigation is either aborted, replaced by a new one, or the document load
147 // finishes. Note that |navigation_handle| will be destroyed at the end of
148 // this call, so do not keep a reference to it afterward.
149 virtual void DidFinishNavigation(NavigationHandle* navigation_handle) {}
151 // ---------------------------------------------------------------------------
153 // This method is invoked after the WebContents decides which RenderFrameHost
154 // to use for the next browser-initiated navigation, but before the navigation
155 // starts. It is not called for most renderer-initiated navigations, and it
156 // does not guarantee that the navigation will commit (e.g., 204s, downloads).
158 // DEPRECATED. This method is difficult to use correctly and should be
159 // removed. TODO(creis): Remove in http://crbug.com/424641.
160 virtual void AboutToNavigateRenderFrame(RenderFrameHost* old_host,
161 RenderFrameHost* new_host) {}
163 // This method is invoked after the browser process starts a navigation to a
164 // pending NavigationEntry. It is not called for renderer-initiated
165 // navigations unless they are sent to the browser process via OpenURL. It may
166 // be called multiple times for a given navigation, such as a typed URL
167 // followed by a cross-process client or server redirect.
168 virtual void DidStartNavigationToPendingEntry(
169 const GURL& url,
170 NavigationController::ReloadType reload_type) {}
172 // |render_frame_host| is the RenderFrameHost for which the provisional load
173 // is happening.
175 // Since the URL validation will strip error URLs, or srcdoc URLs, the boolean
176 // flags |is_error_page| and |is_iframe_srcdoc| will indicate that the not
177 // validated URL was either an error page or an iframe srcdoc.
179 // Note that during a cross-process navigation, several provisional loads
180 // can be on-going in parallel.
181 virtual void DidStartProvisionalLoadForFrame(
182 RenderFrameHost* render_frame_host,
183 const GURL& validated_url,
184 bool is_error_page,
185 bool is_iframe_srcdoc) {}
187 // This method is invoked when the provisional load was successfully
188 // committed.
190 // If the navigation only changed the reference fragment, or was triggered
191 // using the history API (e.g. window.history.replaceState), we will receive
192 // this signal without a prior DidStartProvisionalLoadForFrame signal.
193 virtual void DidCommitProvisionalLoadForFrame(
194 RenderFrameHost* render_frame_host,
195 const GURL& url,
196 ui::PageTransition transition_type) {}
198 // This method is invoked when the provisional load failed.
199 virtual void DidFailProvisionalLoad(
200 RenderFrameHost* render_frame_host,
201 const GURL& validated_url,
202 int error_code,
203 const base::string16& error_description,
204 bool was_ignored_by_handler) {}
206 // If the provisional load corresponded to the main frame, this method is
207 // invoked in addition to DidCommitProvisionalLoadForFrame.
208 virtual void DidNavigateMainFrame(
209 const LoadCommittedDetails& details,
210 const FrameNavigateParams& params) {}
212 // And regardless of what frame navigated, this method is invoked after
213 // DidCommitProvisionalLoadForFrame was invoked.
214 virtual void DidNavigateAnyFrame(
215 RenderFrameHost* render_frame_host,
216 const LoadCommittedDetails& details,
217 const FrameNavigateParams& params) {}
219 // This method is invoked when the SecurityStyle of the WebContents changes.
220 // |security_style| is the new SecurityStyle. |security_style_explanations|
221 // contains human-readable strings explaining why the SecurityStyle of the
222 // page has been downgraded.
223 virtual void SecurityStyleChanged(
224 SecurityStyle security_style,
225 const SecurityStyleExplanations& security_style_explanations) {}
227 // This method is invoked once the window.document object of the main frame
228 // was created.
229 virtual void DocumentAvailableInMainFrame() {}
231 // This method is invoked once the onload handler of the main frame has
232 // completed.
233 virtual void DocumentOnLoadCompletedInMainFrame() {}
235 // This method is invoked when the document in the given frame finished
236 // loading. At this point, scripts marked as defer were executed, and
237 // content scripts marked "document_end" get injected into the frame.
238 virtual void DocumentLoadedInFrame(RenderFrameHost* render_frame_host) {}
240 // This method is invoked when the navigation is done, i.e. the spinner of
241 // the tab will stop spinning, and the onload event was dispatched.
243 // If the WebContents is displaying replacement content, e.g. network error
244 // pages, DidFinishLoad is invoked for frames that were not sending
245 // navigational events before. It is safe to ignore these events.
246 virtual void DidFinishLoad(RenderFrameHost* render_frame_host,
247 const GURL& validated_url) {}
249 // This method is like DidFinishLoad, but when the load failed or was
250 // cancelled, e.g. window.stop() is invoked.
251 virtual void DidFailLoad(RenderFrameHost* render_frame_host,
252 const GURL& validated_url,
253 int error_code,
254 const base::string16& error_description,
255 bool was_ignored_by_handler) {}
257 // This method is invoked when content was loaded from an in-memory cache.
258 virtual void DidLoadResourceFromMemoryCache(
259 const LoadFromMemoryCacheDetails& details) {}
261 // This method is invoked when a response has been received for a resource
262 // request.
263 virtual void DidGetResourceResponseStart(
264 const ResourceRequestDetails& details) {}
266 // This method is invoked when a redirect was received while requesting a
267 // resource.
268 virtual void DidGetRedirectForResourceRequest(
269 RenderFrameHost* render_frame_host,
270 const ResourceRedirectDetails& details) {}
272 // This method is invoked when a new non-pending navigation entry is created.
273 // This corresponds to one NavigationController entry being created
274 // (in the case of new navigations) or renavigated to (for back/forward
275 // navigations).
276 virtual void NavigationEntryCommitted(
277 const LoadCommittedDetails& load_details) {}
279 // This method is invoked when a new WebContents was created in response to
280 // an action in the observed WebContents, e.g. a link with target=_blank was
281 // clicked. The |source_render_frame_host| is the frame in which the action
282 // took place.
283 virtual void DidOpenRequestedURL(WebContents* new_contents,
284 RenderFrameHost* source_render_frame_host,
285 const GURL& url,
286 const Referrer& referrer,
287 WindowOpenDisposition disposition,
288 ui::PageTransition transition) {}
290 // This method is invoked when the renderer process has completed its first
291 // paint after a non-empty layout.
292 virtual void DidFirstVisuallyNonEmptyPaint() {}
294 // These two methods correspond to the points in time when the spinner of the
295 // tab starts and stops spinning.
296 virtual void DidStartLoading() {}
297 virtual void DidStopLoading() {}
299 // When WebContents::Stop() is called, the WebContents stops loading and then
300 // invokes this method. If there are ongoing navigations, their respective
301 // failure methods will also be invoked.
302 virtual void NavigationStopped() {}
304 // This indicates that the next navigation was triggered by a user gesture.
305 virtual void DidGetUserGesture() {}
307 // This method is invoked when a RenderViewHost of this WebContents was
308 // configured to ignore UI events, and an UI event took place.
309 virtual void DidGetIgnoredUIEvent() {}
311 // These methods are invoked every time the WebContents changes visibility.
312 virtual void WasShown() {}
313 virtual void WasHidden() {}
315 // Invoked when the main frame changes size.
316 virtual void MainFrameWasResized(bool width_changed) {}
318 // Invoked when the given frame changes its window.name property.
319 virtual void FrameNameChanged(RenderFrameHost* render_frame_host,
320 const std::string& name) {}
322 // This methods is invoked when the title of the WebContents is set. If the
323 // title was explicitly set, |explicit_set| is true, otherwise the title was
324 // synthesized and |explicit_set| is false.
325 virtual void TitleWasSet(NavigationEntry* entry, bool explicit_set) {}
327 virtual void AppCacheAccessed(const GURL& manifest_url,
328 bool blocked_by_policy) {}
330 // These methods are invoked when a Pepper plugin instance is created/deleted
331 // in the DOM.
332 virtual void PepperInstanceCreated() {}
333 virtual void PepperInstanceDeleted() {}
335 // Notification that a plugin has crashed.
336 // |plugin_pid| is the process ID identifying the plugin process. Note that
337 // this ID is supplied by the renderer process, so should not be trusted.
338 // Besides, the corresponding process has probably died at this point. The ID
339 // may even have been reused by a new process.
340 virtual void PluginCrashed(const base::FilePath& plugin_path,
341 base::ProcessId plugin_pid) {}
343 // Notification that the given plugin has hung or become unhung. This
344 // notification is only for Pepper plugins.
346 // The plugin_child_id is the unique child process ID from the plugin. Note
347 // that this ID is supplied by the renderer process, so should be validated
348 // before it's used for anything in case there's an exploited renderer
349 // process.
350 virtual void PluginHungStatusChanged(int plugin_child_id,
351 const base::FilePath& plugin_path,
352 bool is_hung) {}
354 // Invoked when WebContents::Clone() was used to clone a WebContents.
355 virtual void DidCloneToNewWebContents(WebContents* old_web_contents,
356 WebContents* new_web_contents) {}
358 // Invoked when the WebContents is being destroyed. Gives subclasses a chance
359 // to cleanup. After the whole loop over all WebContentsObservers has been
360 // finished, web_contents() returns nullptr.
361 virtual void WebContentsDestroyed() {}
363 // Called when the user agent override for a WebContents has been changed.
364 virtual void UserAgentOverrideSet(const std::string& user_agent) {}
366 // Invoked when new FaviconURL candidates are received from the renderer
367 // process.
368 virtual void DidUpdateFaviconURL(const std::vector<FaviconURL>& candidates) {}
370 // Invoked when a pepper plugin creates and shows or destroys a fullscreen
371 // RenderWidget.
372 virtual void DidShowFullscreenWidget(int routing_id) {}
373 virtual void DidDestroyFullscreenWidget(int routing_id) {}
375 // Invoked when the renderer process has toggled the tab into/out of
376 // fullscreen mode.
377 virtual void DidToggleFullscreenModeForTab(bool entered_fullscreen) {}
379 // Invoked when an interstitial page is attached or detached.
380 virtual void DidAttachInterstitialPage() {}
381 virtual void DidDetachInterstitialPage() {}
383 // Invoked before a form repost warning is shown.
384 virtual void BeforeFormRepostWarningShow() {}
386 // Invoked when the beforeunload handler fires. The time is from the renderer
387 // process.
388 virtual void BeforeUnloadFired(const base::TimeTicks& proceed_time) {}
390 // Invoked when a user cancels a before unload dialog.
391 virtual void BeforeUnloadDialogCancelled() {}
393 // Invoked when an accessibility event is received from the renderer process.
394 virtual void AccessibilityEventReceived(
395 const std::vector<AXEventNotificationDetails>& details) {}
397 // Invoked when theme color is changed to |theme_color|.
398 virtual void DidChangeThemeColor(SkColor theme_color) {}
400 // Invoked when media is playing.
401 virtual void MediaStartedPlaying() {}
403 // Invoked when media is paused.
404 virtual void MediaPaused() {}
406 // Invoked when media session has changed its state.
407 virtual void MediaSessionStateChanged(bool is_controllable,
408 bool is_suspended) {}
410 // Invoked if an IPC message is coming from a specific RenderFrameHost.
411 virtual bool OnMessageReceived(const IPC::Message& message,
412 RenderFrameHost* render_frame_host);
414 // Notification that |contents| has gained focus.
415 virtual void OnWebContentsFocused() {}
417 // IPC::Listener implementation.
418 bool OnMessageReceived(const IPC::Message& message) override;
420 // IPC::Sender implementation.
421 bool Send(IPC::Message* message) override;
422 int routing_id() const;
424 WebContents* web_contents() const;
426 protected:
427 // Use this constructor when the object is tied to a single WebContents for
428 // its entire lifetime.
429 explicit WebContentsObserver(WebContents* web_contents);
431 // Use this constructor when the object wants to observe a WebContents for
432 // part of its lifetime. It can then call Observe() to start and stop
433 // observing.
434 WebContentsObserver();
436 ~WebContentsObserver() override;
438 // Start observing a different WebContents; used with the default constructor.
439 void Observe(WebContents* web_contents);
441 private:
442 friend class WebContentsImpl;
444 void ResetWebContents();
446 WebContentsImpl* web_contents_;
448 DISALLOW_COPY_AND_ASSIGN(WebContentsObserver);
451 } // namespace content
453 #endif // CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_OBSERVER_H_