• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_UNLOAD_CONTROLLER_H_
6 #define CHROME_BROWSER_UI_UNLOAD_CONTROLLER_H_
7 
8 #include <set>
9 
10 #include "base/callback.h"
11 #include "base/memory/weak_ptr.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
13 #include "content/public/browser/notification_observer.h"
14 #include "content/public/browser/notification_registrar.h"
15 
16 class Browser;
17 class TabStripModel;
18 
19 namespace content {
20 class NotificationSource;
21 class NotifictaionDetails;
22 class WebContents;
23 }
24 
25 namespace chrome {
26 
27 class UnloadController : public content::NotificationObserver,
28                          public TabStripModelObserver {
29  public:
30   explicit UnloadController(Browser* browser);
31   virtual ~UnloadController();
32 
33   // Returns true if |contents| can be cleanly closed. When |browser_| is being
34   // closed, this function will return false to indicate |contents| should not
35   // be cleanly closed, since the fast shutdown path will just kill its
36   // renderer.
37   bool CanCloseContents(content::WebContents* contents);
38 
39   // Returns true if we need to run unload events for the |contents|.
40   static bool ShouldRunUnloadEventsHelper(content::WebContents* contents);
41 
42   // Helper function to run beforeunload listeners on a WebContents.
43   // Returns true if |contents| beforeunload listeners were invoked.
44   static bool RunUnloadEventsHelper(content::WebContents* contents);
45 
46   // Called when a BeforeUnload handler is fired for |contents|. |proceed|
47   // indicates the user's response to the Y/N BeforeUnload handler dialog. If
48   // this parameter is false, any pending attempt to close the whole browser
49   // will be canceled. Returns true if Unload handlers should be fired. When the
50   // |browser_| is being closed, Unload handlers for any particular WebContents
51   // will not be run until every WebContents being closed has a chance to run
52   // its BeforeUnloadHandler.
53   bool BeforeUnloadFired(content::WebContents* contents, bool proceed);
54 
is_attempting_to_close_browser()55   bool is_attempting_to_close_browser() const {
56     return is_attempting_to_close_browser_;
57   }
58 
59   // Called in response to a request to close |browser_|'s window. Returns true
60   // when there are no remaining beforeunload handlers to be run.
61   bool ShouldCloseWindow();
62 
63   // Begins the process of confirming whether the associated browser can be
64   // closed.
65   bool CallBeforeUnloadHandlers(
66       const base::Callback<void(bool)>& on_close_confirmed);
67 
68   // Clears the results of any beforeunload confirmation dialogs triggered by a
69   // CallBeforeUnloadHandlers call.
70   void ResetBeforeUnloadHandlers();
71 
72   // Returns true if |browser_| has any tabs that have BeforeUnload handlers
73   // that have not been fired. This method is non-const because it builds a list
74   // of tabs that need their BeforeUnloadHandlers fired.
75   // TODO(beng): This seems like it could be private but it is used by
76   //             AreAllBrowsersCloseable() in application_lifetime.cc. It seems
77   //             very similar to ShouldCloseWindow() and some consolidation
78   //             could be pursued.
79   bool TabsNeedBeforeUnloadFired();
80 
81  private:
82   typedef std::set<content::WebContents*> UnloadListenerSet;
83 
84   // Overridden from content::NotificationObserver:
85   virtual void Observe(int type,
86                        const content::NotificationSource& source,
87                        const content::NotificationDetails& details) OVERRIDE;
88 
89   // Overridden from TabStripModelObserver:
90   virtual void TabInsertedAt(content::WebContents* contents,
91                              int index,
92                              bool foreground) OVERRIDE;
93   virtual void TabDetachedAt(content::WebContents* contents,
94                              int index) OVERRIDE;
95   virtual void TabReplacedAt(TabStripModel* tab_strip_model,
96                              content::WebContents* old_contents,
97                              content::WebContents* new_contents,
98                              int index) OVERRIDE;
99   virtual void TabStripEmpty() OVERRIDE;
100 
101   void TabAttachedImpl(content::WebContents* contents);
102   void TabDetachedImpl(content::WebContents* contents);
103 
104   // Processes the next tab that needs it's beforeunload/unload event fired.
105   void ProcessPendingTabs();
106 
107   // Whether we've completed firing all the tabs' beforeunload/unload events.
108   bool HasCompletedUnloadProcessing() const;
109 
110   // Clears all the state associated with processing tabs' beforeunload/unload
111   // events since the user cancelled closing the window.
112   void CancelWindowClose();
113 
114   // Removes |web_contents| from the passed |set|.
115   // Returns whether the tab was in the set in the first place.
116   bool RemoveFromSet(UnloadListenerSet* set,
117                      content::WebContents* web_contents);
118 
119   // Cleans up state appropriately when we are trying to close the browser and
120   // the tab has finished firing its unload handler. We also use this in the
121   // cases where a tab crashes or hangs even if the beforeunload/unload haven't
122   // successfully fired. If |process_now| is true |ProcessPendingTabs| is
123   // invoked immediately, otherwise it is invoked after a delay (PostTask).
124   //
125   // Typically you'll want to pass in true for |process_now|. Passing in true
126   // may result in deleting |tab|. If you know that shouldn't happen (because of
127   // the state of the stack), pass in false.
128   void ClearUnloadState(content::WebContents* web_contents, bool process_now);
129 
is_calling_before_unload_handlers()130   bool is_calling_before_unload_handlers() {
131     return !on_close_confirmed_.is_null();
132   }
133 
134   Browser* browser_;
135 
136   content::NotificationRegistrar registrar_;
137 
138   // Tracks tabs that need there beforeunload event fired before we can
139   // close the browser. Only gets populated when we try to close the browser.
140   UnloadListenerSet tabs_needing_before_unload_fired_;
141 
142   // Tracks tabs that need there unload event fired before we can
143   // close the browser. Only gets populated when we try to close the browser.
144   UnloadListenerSet tabs_needing_unload_fired_;
145 
146   // Whether we are processing the beforeunload and unload events of each tab
147   // in preparation for closing the browser. UnloadController owns this state
148   // rather than Browser because unload handlers are the only reason that a
149   // Browser window isn't just immediately closed.
150   bool is_attempting_to_close_browser_;
151 
152   // A callback to call to report whether the user chose to close all tabs of
153   // |browser_| that have beforeunload event handlers. This is set only if we
154   // are currently confirming that the browser is closable.
155   base::Callback<void(bool)> on_close_confirmed_;
156 
157   base::WeakPtrFactory<UnloadController> weak_factory_;
158 
159   DISALLOW_COPY_AND_ASSIGN(UnloadController);
160 };
161 
162 }  // namespace chrome
163 
164 #endif  // CHROME_BROWSER_UI_UNLOAD_CONTROLLER_H_
165