Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / unload_controller.h
bloba96ec5d5c2ecc80c5d48a941803b8ca827c83538
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.
5 #ifndef CHROME_BROWSER_UI_UNLOAD_CONTROLLER_H_
6 #define CHROME_BROWSER_UI_UNLOAD_CONTROLLER_H_
8 #include <set>
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"
16 class Browser;
17 class TabStripModel;
19 namespace content {
20 class NotificationSource;
21 class NotifictaionDetails;
22 class WebContents;
25 namespace chrome {
27 class UnloadController : public content::NotificationObserver,
28 public TabStripModelObserver {
29 public:
30 explicit UnloadController(Browser* browser);
31 virtual ~UnloadController();
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);
39 // Returns true if we need to run unload events for the |contents|.
40 static bool ShouldRunUnloadEventsHelper(content::WebContents* contents);
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);
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);
55 bool is_attempting_to_close_browser() const {
56 return is_attempting_to_close_browser_;
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();
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);
68 // Clears the results of any beforeunload confirmation dialogs triggered by a
69 // CallBeforeUnloadHandlers call.
70 void ResetBeforeUnloadHandlers();
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();
81 private:
82 typedef std::set<content::WebContents*> UnloadListenerSet;
84 // Overridden from content::NotificationObserver:
85 virtual void Observe(int type,
86 const content::NotificationSource& source,
87 const content::NotificationDetails& details) OVERRIDE;
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;
101 void TabAttachedImpl(content::WebContents* contents);
102 void TabDetachedImpl(content::WebContents* contents);
104 // Processes the next tab that needs it's beforeunload/unload event fired.
105 void ProcessPendingTabs();
107 // Whether we've completed firing all the tabs' beforeunload/unload events.
108 bool HasCompletedUnloadProcessing() const;
110 // Clears all the state associated with processing tabs' beforeunload/unload
111 // events since the user cancelled closing the window.
112 void CancelWindowClose();
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);
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).
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);
130 bool is_calling_before_unload_handlers() {
131 return !on_close_confirmed_.is_null();
134 Browser* browser_;
136 content::NotificationRegistrar registrar_;
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_;
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_;
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_;
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_;
157 base::WeakPtrFactory<UnloadController> weak_factory_;
159 DISALLOW_COPY_AND_ASSIGN(UnloadController);
162 } // namespace chrome
164 #endif // CHROME_BROWSER_UI_UNLOAD_CONTROLLER_H_