Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / fast_unload_controller.h
blobdbcee928ab2d8f89cdccee92b86fbf0066a9047f
1 // Copyright 2013 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 CHROME_BROWSER_UI_FAST_UNLOAD_CONTROLLER_H_
6 #define CHROME_BROWSER_UI_FAST_UNLOAD_CONTROLLER_H_
8 #include <set>
10 #include "base/callback.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/strings/string_piece.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
15 #include "content/public/browser/notification_observer.h"
16 #include "content/public/browser/notification_registrar.h"
18 class Browser;
19 class TabStripModel;
21 namespace content {
22 class NotificationSource;
23 class NotifictaionDetails;
24 class WebContents;
27 namespace chrome {
28 // FastUnloadController manages closing tabs and windows -- especially in
29 // regards to beforeunload handlers (have proceed/cancel dialogs) and
30 // unload handlers (have no user interaction).
32 // Typical flow of closing a tab:
33 // 1. Browser calls CanCloseContents().
34 // If true, browser calls contents::CloseWebContents().
35 // 2. WebContents notifies us via its delegate and BeforeUnloadFired()
36 // that the beforeunload handler was run. If the user allowed the
37 // close to continue, we detached the tab and hold onto it while the
38 // close finishes.
40 // Typical flow of closing a window:
41 // 1. BrowserView::CanClose() calls TabsNeedBeforeUnloadFired().
42 // If beforeunload/unload handlers need to run, FastUnloadController returns
43 // true and calls ProcessPendingTabs() (private method).
44 // 2. For each tab with a beforeunload/unload handler, ProcessPendingTabs()
45 // calls |CoreTabHelper::OnCloseStarted()|
46 // and |web_contents->GetRenderViewHost()->FirePageBeforeUnload()|.
47 // 3. If the user allowed the close to continue, we detach all the tabs with
48 // unload handlers, remove them from the tab strip, and finish closing
49 // the tabs in the background.
50 // 4. The browser gets notified that the tab strip is empty and calls
51 // CloseFrame where the empty tab strip causes the window to hide.
52 // Once the detached tabs finish, the browser calls CloseFrame again and
53 // the window is finally closed.
55 class FastUnloadController : public content::NotificationObserver,
56 public TabStripModelObserver {
57 public:
58 explicit FastUnloadController(Browser* browser);
59 virtual ~FastUnloadController();
61 // Returns true if |contents| can be cleanly closed. When |browser_| is being
62 // closed, this function will return false to indicate |contents| should not
63 // be cleanly closed, since the fast shutdown path will just kill its
64 // renderer.
65 bool CanCloseContents(content::WebContents* contents);
67 // Returns true if we need to run unload events for the |contents|.
68 static bool ShouldRunUnloadEventsHelper(content::WebContents* contents);
70 // Helper function to run beforeunload listeners on a WebContents.
71 // Returns true if |contents| beforeunload listeners were invoked.
72 static bool RunUnloadEventsHelper(content::WebContents* contents);
74 // Called when a BeforeUnload handler is fired for |contents|. |proceed|
75 // indicates the user's response to the Y/N BeforeUnload handler dialog. If
76 // this parameter is false, any pending attempt to close the whole browser
77 // will be canceled. Returns true if Unload handlers should be fired. When the
78 // |browser_| is being closed, Unload handlers for any particular WebContents
79 // will not be run until every WebContents being closed has a chance to run
80 // its BeforeUnloadHandler.
81 bool BeforeUnloadFired(content::WebContents* contents, bool proceed);
83 bool is_attempting_to_close_browser() const {
84 return is_attempting_to_close_browser_;
87 // Called in response to a request to close |browser_|'s window. Returns true
88 // when there are no remaining beforeunload handlers to be run.
89 bool ShouldCloseWindow();
91 // Begins the process of confirming whether the associated browser can be
92 // closed.
93 bool CallBeforeUnloadHandlers(
94 const base::Callback<void(bool)>& on_close_confirmed);
96 // Clears the results of any beforeunload confirmation dialogs triggered by a
97 // CallBeforeUnloadHandlers call.
98 void ResetBeforeUnloadHandlers();
100 // Returns true if |browser_| has any tabs that have BeforeUnload handlers
101 // that have not been fired. This method is non-const because it builds a list
102 // of tabs that need their BeforeUnloadHandlers fired.
103 // TODO(beng): This seems like it could be private but it is used by
104 // AreAllBrowsersCloseable() in application_lifetime.cc. It seems
105 // very similar to ShouldCloseWindow() and some consolidation
106 // could be pursued.
107 bool TabsNeedBeforeUnloadFired();
109 // Returns true if all tabs' beforeunload/unload events have fired.
110 bool HasCompletedUnloadProcessing() const;
112 private:
113 // Overridden from content::NotificationObserver:
114 virtual void Observe(int type,
115 const content::NotificationSource& source,
116 const content::NotificationDetails& details) OVERRIDE;
118 // Overridden from TabStripModelObserver:
119 virtual void TabInsertedAt(content::WebContents* contents,
120 int index,
121 bool foreground) OVERRIDE;
122 virtual void TabDetachedAt(content::WebContents* contents,
123 int index) OVERRIDE;
124 virtual void TabReplacedAt(TabStripModel* tab_strip_model,
125 content::WebContents* old_contents,
126 content::WebContents* new_contents,
127 int index) OVERRIDE;
128 virtual void TabStripEmpty() OVERRIDE;
130 void TabAttachedImpl(content::WebContents* contents);
131 void TabDetachedImpl(content::WebContents* contents);
133 // Detach |contents| and wait for it to finish closing.
134 // The close must be inititiated outside of this method.
135 // Returns true if it succeeds.
136 bool DetachWebContents(content::WebContents* contents);
138 // Processes the next tab that needs it's beforeunload/unload event fired.
139 void ProcessPendingTabs();
141 // Clears all the state associated with processing tabs' beforeunload/unload
142 // events since the user cancelled closing the window.
143 void CancelWindowClose();
145 // Cleans up state appropriately when we are trying to close the
146 // browser or close a tab in the background. We also use this in the
147 // cases where a tab crashes or hangs even if the
148 // beforeunload/unload haven't successfully fired.
149 void ClearUnloadState(content::WebContents* contents);
151 // Helper for |ClearUnloadState| to unwind stack before proceeding.
152 void PostTaskForProcessPendingTabs();
154 // Log a step of the unload processing.
155 void LogUnloadStep(const base::StringPiece& step_name,
156 content::WebContents* contents) const;
158 bool is_calling_before_unload_handlers() {
159 return !on_close_confirmed_.is_null();
162 Browser* browser_;
164 content::NotificationRegistrar registrar_;
166 typedef std::set<content::WebContents*> WebContentsSet;
168 // Tracks tabs that need their beforeunload event started.
169 // Only gets populated when we try to close the browser.
170 WebContentsSet tabs_needing_before_unload_;
172 // Tracks the tab that needs its beforeunload event result.
173 // Only gets populated when we try to close the browser.
174 content::WebContents* tab_needing_before_unload_ack_;
176 // Tracks tabs that need their unload event started.
177 // Only gets populated when we try to close the browser.
178 WebContentsSet tabs_needing_unload_;
180 // Tracks tabs that need to finish running their unload event.
181 // Populated both when closing individual tabs and when closing the browser.
182 WebContentsSet tabs_needing_unload_ack_;
184 // Whether we are processing the beforeunload and unload events of each tab
185 // in preparation for closing the browser. FastUnloadController owns this
186 // state rather than Browser because unload handlers are the only reason that
187 // a Browser window isn't just immediately closed.
188 bool is_attempting_to_close_browser_;
190 // A callback to call to report whether the user chose to close all tabs of
191 // |browser_| that have beforeunload event handlers. This is set only if we
192 // are currently confirming that the browser is closable.
193 base::Callback<void(bool)> on_close_confirmed_;
195 // Manage tabs with beforeunload/unload handlers that close detached.
196 class DetachedWebContentsDelegate;
197 scoped_ptr<DetachedWebContentsDelegate> detached_delegate_;
199 base::WeakPtrFactory<FastUnloadController> weak_factory_;
201 DISALLOW_COPY_AND_ASSIGN(FastUnloadController);
204 } // namespace chrome
206 #endif // CHROME_BROWSER_UI_FAST_UNLOAD_CONTROLLER_H_