Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / fast_unload_controller.h
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.
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/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"
17
18 class Browser;
19 class TabStripModel;
20
21 namespace content {
22 class NotificationSource;
23 class NotificationDetails;
24 class WebContents;
25 }
26
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).
31 //
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.
39 //
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.
54 //
55 class FastUnloadController : public content::NotificationObserver,
56                              public TabStripModelObserver {
57  public:
58   explicit FastUnloadController(Browser* browser);
59   virtual ~FastUnloadController();
60
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);
66
67   // Returns true if we need to run unload events for the |contents|.
68   static bool ShouldRunUnloadEventsHelper(content::WebContents* contents);
69
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);
73
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);
82
83   bool is_attempting_to_close_browser() const {
84     return is_attempting_to_close_browser_;
85   }
86
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();
90
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);
95
96   // Clears the results of any beforeunload confirmation dialogs triggered by a
97   // CallBeforeUnloadHandlers call.
98   void ResetBeforeUnloadHandlers();
99
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();
108
109   // Returns true if all tabs' beforeunload/unload events have fired.
110   bool HasCompletedUnloadProcessing() const;
111
112   // Clears all the state associated with processing tabs' beforeunload/unload
113   // events since the user cancelled closing the window.
114   void CancelWindowClose();
115
116  private:
117   // Overridden from content::NotificationObserver:
118   virtual void Observe(int type,
119                        const content::NotificationSource& source,
120                        const content::NotificationDetails& details) OVERRIDE;
121
122   // Overridden from TabStripModelObserver:
123   virtual void TabInsertedAt(content::WebContents* contents,
124                              int index,
125                              bool foreground) OVERRIDE;
126   virtual void TabDetachedAt(content::WebContents* contents,
127                              int index) OVERRIDE;
128   virtual void TabReplacedAt(TabStripModel* tab_strip_model,
129                              content::WebContents* old_contents,
130                              content::WebContents* new_contents,
131                              int index) OVERRIDE;
132   virtual void TabStripEmpty() OVERRIDE;
133
134   void TabAttachedImpl(content::WebContents* contents);
135   void TabDetachedImpl(content::WebContents* contents);
136
137   // Detach |contents| and wait for it to finish closing.
138   // The close must be inititiated outside of this method.
139   // Returns true if it succeeds.
140   bool DetachWebContents(content::WebContents* contents);
141
142   // Processes the next tab that needs it's beforeunload/unload event fired.
143   void ProcessPendingTabs();
144
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);
150
151   // Helper for |ClearUnloadState| to unwind stack before proceeding.
152   void PostTaskForProcessPendingTabs();
153
154   // Log a step of the unload processing.
155   void LogUnloadStep(const base::StringPiece& step_name,
156                      content::WebContents* contents) const;
157
158   bool is_calling_before_unload_handlers() {
159     return !on_close_confirmed_.is_null();
160   }
161
162   Browser* browser_;
163
164   content::NotificationRegistrar registrar_;
165
166   typedef std::set<content::WebContents*> WebContentsSet;
167
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_;
171
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_;
175
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_;
179
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_;
183
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_;
189
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_;
194
195   // Manage tabs with beforeunload/unload handlers that close detached.
196   class DetachedWebContentsDelegate;
197   scoped_ptr<DetachedWebContentsDelegate> detached_delegate_;
198
199   base::WeakPtrFactory<FastUnloadController> weak_factory_;
200
201   DISALLOW_COPY_AND_ASSIGN(FastUnloadController);
202 };
203
204 }  // namespace chrome
205
206 #endif  // CHROME_BROWSER_UI_FAST_UNLOAD_CONTROLLER_H_