Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / unload_controller.h
blob53fa365b9362f5db595a6b39a7d7e5bf3e822415
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 NotificationDetails;
22 class WebContents;
25 namespace chrome {
27 class UnloadController : public content::NotificationObserver,
28 public TabStripModelObserver {
29 public:
30 explicit UnloadController(Browser* browser);
31 ~UnloadController() override;
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 // Clears all the state associated with processing tabs' beforeunload/unload
82 // events since the user cancelled closing the window.
83 void CancelWindowClose();
85 private:
86 typedef std::set<content::WebContents*> UnloadListenerSet;
88 // Overridden from content::NotificationObserver:
89 void Observe(int type,
90 const content::NotificationSource& source,
91 const content::NotificationDetails& details) override;
93 // Overridden from TabStripModelObserver:
94 void TabInsertedAt(content::WebContents* contents,
95 int index,
96 bool foreground) override;
97 void TabDetachedAt(content::WebContents* contents, int index) override;
98 void TabReplacedAt(TabStripModel* tab_strip_model,
99 content::WebContents* old_contents,
100 content::WebContents* new_contents,
101 int index) override;
102 void TabStripEmpty() override;
104 void TabAttachedImpl(content::WebContents* contents);
105 void TabDetachedImpl(content::WebContents* contents);
107 // Processes the next tab that needs it's beforeunload/unload event fired.
108 void ProcessPendingTabs();
110 // Whether we've completed firing all the tabs' beforeunload/unload events.
111 bool HasCompletedUnloadProcessing() const;
113 // Removes |web_contents| from the passed |set|.
114 // Returns whether the tab was in the set in the first place.
115 bool RemoveFromSet(UnloadListenerSet* set,
116 content::WebContents* web_contents);
118 // Cleans up state appropriately when we are trying to close the browser and
119 // the tab has finished firing its unload handler. We also use this in the
120 // cases where a tab crashes or hangs even if the beforeunload/unload haven't
121 // successfully fired. If |process_now| is true |ProcessPendingTabs| is
122 // invoked immediately, otherwise it is invoked after a delay (PostTask).
124 // Typically you'll want to pass in true for |process_now|. Passing in true
125 // may result in deleting |tab|. If you know that shouldn't happen (because of
126 // the state of the stack), pass in false.
127 void ClearUnloadState(content::WebContents* web_contents, bool process_now);
129 bool is_calling_before_unload_handlers() {
130 return !on_close_confirmed_.is_null();
133 Browser* browser_;
135 content::NotificationRegistrar registrar_;
137 // Tracks tabs that need there beforeunload event fired before we can
138 // close the browser. Only gets populated when we try to close the browser.
139 UnloadListenerSet tabs_needing_before_unload_fired_;
141 // Tracks tabs that need there unload event fired before we can
142 // close the browser. Only gets populated when we try to close the browser.
143 UnloadListenerSet tabs_needing_unload_fired_;
145 // Whether we are processing the beforeunload and unload events of each tab
146 // in preparation for closing the browser. UnloadController owns this state
147 // rather than Browser because unload handlers are the only reason that a
148 // Browser window isn't just immediately closed.
149 bool is_attempting_to_close_browser_;
151 // A callback to call to report whether the user chose to close all tabs of
152 // |browser_| that have beforeunload event handlers. This is set only if we
153 // are currently confirming that the browser is closable.
154 base::Callback<void(bool)> on_close_confirmed_;
156 base::WeakPtrFactory<UnloadController> weak_factory_;
158 DISALLOW_COPY_AND_ASSIGN(UnloadController);
161 } // namespace chrome
163 #endif // CHROME_BROWSER_UI_UNLOAD_CONTROLLER_H_