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_
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"
22 class NotificationSource
;
23 class NotificationDetails
;
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
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
{
58 explicit FastUnloadController(Browser
* browser
);
59 ~FastUnloadController() override
;
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
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
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
107 bool TabsNeedBeforeUnloadFired();
109 // Returns true if all tabs' beforeunload/unload events have fired.
110 bool HasCompletedUnloadProcessing() const;
112 // Clears all the state associated with processing tabs' beforeunload/unload
113 // events since the user cancelled closing the window.
114 void CancelWindowClose();
117 // Overridden from content::NotificationObserver:
118 void Observe(int type
,
119 const content::NotificationSource
& source
,
120 const content::NotificationDetails
& details
) override
;
122 // Overridden from TabStripModelObserver:
123 void TabInsertedAt(content::WebContents
* contents
,
125 bool foreground
) override
;
126 void TabDetachedAt(content::WebContents
* contents
, int index
) override
;
127 void TabReplacedAt(TabStripModel
* tab_strip_model
,
128 content::WebContents
* old_contents
,
129 content::WebContents
* new_contents
,
131 void TabStripEmpty() override
;
133 void TabAttachedImpl(content::WebContents
* contents
);
134 void TabDetachedImpl(content::WebContents
* contents
);
136 // Detach |contents| and wait for it to finish closing.
137 // The close must be inititiated outside of this method.
138 // Returns true if it succeeds.
139 bool DetachWebContents(content::WebContents
* contents
);
141 // Processes the next tab that needs it's beforeunload/unload event fired.
142 void ProcessPendingTabs();
144 // Cleans up state appropriately when we are trying to close the
145 // browser or close a tab in the background. We also use this in the
146 // cases where a tab crashes or hangs even if the
147 // beforeunload/unload haven't successfully fired.
148 void ClearUnloadState(content::WebContents
* contents
);
150 // Helper for |ClearUnloadState| to unwind stack before proceeding.
151 void PostTaskForProcessPendingTabs();
153 // Log a step of the unload processing.
154 void LogUnloadStep(const base::StringPiece
& step_name
,
155 content::WebContents
* contents
) const;
157 bool is_calling_before_unload_handlers() {
158 return !on_close_confirmed_
.is_null();
163 content::NotificationRegistrar registrar_
;
165 typedef std::set
<content::WebContents
*> WebContentsSet
;
167 // Tracks tabs that need their beforeunload event started.
168 // Only gets populated when we try to close the browser.
169 WebContentsSet tabs_needing_before_unload_
;
171 // Tracks the tab that needs its beforeunload event result.
172 // Only gets populated when we try to close the browser.
173 content::WebContents
* tab_needing_before_unload_ack_
;
175 // Tracks tabs that need their unload event started.
176 // Only gets populated when we try to close the browser.
177 WebContentsSet tabs_needing_unload_
;
179 // Tracks tabs that need to finish running their unload event.
180 // Populated both when closing individual tabs and when closing the browser.
181 WebContentsSet tabs_needing_unload_ack_
;
183 // Whether we are processing the beforeunload and unload events of each tab
184 // in preparation for closing the browser. FastUnloadController owns this
185 // state rather than Browser because unload handlers are the only reason that
186 // a Browser window isn't just immediately closed.
187 bool is_attempting_to_close_browser_
;
189 // A callback to call to report whether the user chose to close all tabs of
190 // |browser_| that have beforeunload event handlers. This is set only if we
191 // are currently confirming that the browser is closable.
192 base::Callback
<void(bool)> on_close_confirmed_
;
194 // Manage tabs with beforeunload/unload handlers that close detached.
195 class DetachedWebContentsDelegate
;
196 scoped_ptr
<DetachedWebContentsDelegate
> detached_delegate_
;
198 base::WeakPtrFactory
<FastUnloadController
> weak_factory_
;
200 DISALLOW_COPY_AND_ASSIGN(FastUnloadController
);
203 } // namespace chrome
205 #endif // CHROME_BROWSER_UI_FAST_UNLOAD_CONTROLLER_H_