Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(452)

Side by Side Diff: chrome/browser/ui/fast_unload_controller.h

Issue 17571018: Reland fast tab closure behind a flag (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased, 2nd attempt to land. Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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 CHROME_BROWSER_UI_FAST_UNLOAD_CONTROLLER_H_
6 #define CHROME_BROWSER_UI_FAST_UNLOAD_CONTROLLER_H_
7
8 #include <set>
9
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/strings/string_piece.h"
13 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
14 #include "content/public/browser/notification_observer.h"
15 #include "content/public/browser/notification_registrar.h"
16
17 class Browser;
18 class TabStripModel;
19
20 namespace content {
21 class NotificationSource;
22 class NotifictaionDetails;
23 class WebContents;
24 }
25
26 namespace chrome {
27 // FastUnloadController manages closing tabs and windows -- especially in
28 // regards to beforeunload handlers (have proceed/cancel dialogs) and
29 // unload handlers (have no user interaction).
30 //
31 // Typical flow of closing a tab:
32 // 1. Browser calls CanCloseContents().
33 // If true, browser calls contents::CloseWebContents().
34 // 2. WebContents notifies us via its delegate and BeforeUnloadFired()
35 // that the beforeunload handler was run. If the user allowed the
36 // close to continue, we detached the tab and hold onto it while the
37 // close finishes.
38 //
39 // Typical flow of closing a window:
40 // 1. BrowserView::CanClose() calls TabsNeedBeforeUnloadFired().
41 // If beforeunload/unload handlers need to run, FastUnloadController returns
42 // true and calls ProcessPendingTabs() (private method).
43 // 2. For each tab with a beforeunload/unload handler, ProcessPendingTabs()
44 // calls |CoreTabHelper::OnCloseStarted()|
45 // and |web_contents->GetRenderViewHost()->FirePageBeforeUnload()|.
46 // 3. If the user allowed the close to continue, we detach all the tabs with
47 // unload handlers, remove them from the tab strip, and finish closing
48 // the tabs in the background.
49 // 4. The browser gets notified that the tab strip is empty and calls
50 // CloseFrame where the empty tab strip causes the window to hide.
51 // Once the detached tabs finish, the browser calls CloseFrame again and
52 // the window is finally closed.
53 //
54 class FastUnloadController : public content::NotificationObserver,
55 public TabStripModelObserver {
56 public:
57 explicit FastUnloadController(Browser* browser);
58 virtual ~FastUnloadController();
59
60 // Returns true if |contents| can be cleanly closed. When |browser_| is being
61 // closed, this function will return false to indicate |contents| should not
62 // be cleanly closed, since the fast shutdown path will just kill its
63 // renderer.
64 bool CanCloseContents(content::WebContents* contents);
65
66 // Called when a BeforeUnload handler is fired for |contents|. |proceed|
67 // indicates the user's response to the Y/N BeforeUnload handler dialog. If
68 // this parameter is false, any pending attempt to close the whole browser
69 // will be canceled. Returns true if Unload handlers should be fired. When the
70 // |browser_| is being closed, Unload handlers for any particular WebContents
71 // will not be run until every WebContents being closed has a chance to run
72 // its BeforeUnloadHandler.
73 bool BeforeUnloadFired(content::WebContents* contents, bool proceed);
74
75 bool is_attempting_to_close_browser() const {
76 return is_attempting_to_close_browser_;
77 }
78
79 // Called in response to a request to close |browser_|'s window. Returns true
80 // when there are no remaining beforeunload handlers to be run.
81 bool ShouldCloseWindow();
82
83 // Returns true if |browser_| has any tabs that have BeforeUnload handlers
84 // that have not been fired. This method is non-const because it builds a list
85 // of tabs that need their BeforeUnloadHandlers fired.
86 // TODO(beng): This seems like it could be private but it is used by
87 // AreAllBrowsersCloseable() in application_lifetime.cc. It seems
88 // very similar to ShouldCloseWindow() and some consolidation
89 // could be pursued.
90 bool TabsNeedBeforeUnloadFired();
91
92 // Returns true if all tabs' beforeunload/unload events have fired.
93 bool HasCompletedUnloadProcessing() const;
94
95 private:
96 // Overridden from content::NotificationObserver:
97 virtual void Observe(int type,
98 const content::NotificationSource& source,
99 const content::NotificationDetails& details) OVERRIDE;
100
101 // Overridden from TabStripModelObserver:
102 virtual void TabInsertedAt(content::WebContents* contents,
103 int index,
104 bool foreground) OVERRIDE;
105 virtual void TabDetachedAt(content::WebContents* contents,
106 int index) OVERRIDE;
107 virtual void TabReplacedAt(TabStripModel* tab_strip_model,
108 content::WebContents* old_contents,
109 content::WebContents* new_contents,
110 int index) OVERRIDE;
111 virtual void TabStripEmpty() OVERRIDE;
112
113 void TabAttachedImpl(content::WebContents* contents);
114 void TabDetachedImpl(content::WebContents* contents);
115
116 // Detach |contents| and wait for it to finish closing.
117 // The close must be inititiated outside of this method.
118 // Returns true if it succeeds.
119 bool DetachWebContents(content::WebContents* contents);
120
121 // Processes the next tab that needs it's beforeunload/unload event fired.
122 void ProcessPendingTabs();
123
124 // Clears all the state associated with processing tabs' beforeunload/unload
125 // events since the user cancelled closing the window.
126 void CancelWindowClose();
127
128 // Cleans up state appropriately when we are trying to close the
129 // browser or close a tab in the background. We also use this in the
130 // cases where a tab crashes or hangs even if the
131 // beforeunload/unload haven't successfully fired.
132 void ClearUnloadState(content::WebContents* contents);
133
134 // Helper for |ClearUnloadState| to unwind stack before proceeding.
135 void PostTaskForProcessPendingTabs();
136
137 // Log a step of the unload processing.
138 void LogUnloadStep(const base::StringPiece& step_name,
139 content::WebContents* contents) const;
140
141 Browser* browser_;
142
143 content::NotificationRegistrar registrar_;
144
145 typedef std::set<content::WebContents*> WebContentsSet;
146
147 // Tracks tabs that need their beforeunload event started.
148 // Only gets populated when we try to close the browser.
149 WebContentsSet tabs_needing_before_unload_;
150
151 // Tracks the tab that needs its beforeunload event result.
152 // Only gets populated when we try to close the browser.
153 content::WebContents* tab_needing_before_unload_ack_;
154
155 // Tracks tabs that need their unload event started.
156 // Only gets populated when we try to close the browser.
157 WebContentsSet tabs_needing_unload_;
158
159 // Tracks tabs that need to finish running their unload event.
160 // Populated both when closing individual tabs and when closing the browser.
161 WebContentsSet tabs_needing_unload_ack_;
162
163 // Whether we are processing the beforeunload and unload events of each tab
164 // in preparation for closing the browser. FastUnloadController owns this
165 // state rather than Browser because unload handlers are the only reason that
166 // a Browser window isn't just immediately closed.
167 bool is_attempting_to_close_browser_;
168
169 // Manage tabs with beforeunload/unload handlers that close detached.
170 class DetachedWebContentsDelegate;
171 scoped_ptr<DetachedWebContentsDelegate> detached_delegate_;
172
173 base::WeakPtrFactory<FastUnloadController> weak_factory_;
174
175 DISALLOW_COPY_AND_ASSIGN(FastUnloadController);
176 };
177
178 } // namespace chrome
179
180 #endif // CHROME_BROWSER_UI_FAST_UNLOAD_CONTROLLER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698