OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_UI_UNLOAD_CONTROLLER_H_ | 5 #ifndef CHROME_BROWSER_UI_UNLOAD_CONTROLLER_H_ |
6 #define CHROME_BROWSER_UI_UNLOAD_CONTROLLER_H_ | 6 #define CHROME_BROWSER_UI_UNLOAD_CONTROLLER_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/memory/scoped_ptr.h" | |
10 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
12 #include "base/strings/string_piece.h" | |
11 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h" | 13 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h" |
12 #include "content/public/browser/notification_observer.h" | 14 #include "content/public/browser/notification_observer.h" |
13 #include "content/public/browser/notification_registrar.h" | 15 #include "content/public/browser/notification_registrar.h" |
14 | 16 |
15 class Browser; | 17 class Browser; |
16 class TabStripModel; | 18 class TabStripModel; |
17 | 19 |
18 namespace content { | 20 namespace content { |
19 class NotificationSource; | 21 class NotificationSource; |
20 class NotifictaionDetails; | 22 class NotifictaionDetails; |
21 class WebContents; | 23 class WebContents; |
22 } | 24 } |
23 | 25 |
24 namespace chrome { | 26 namespace chrome { |
25 | 27 // UnloadController 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, UnloadController returns | |
42 // true and calls ProcessPendingTabs() (private method). | |
43 // 2. For each tab with a beforeunload/unload handler, ProcessPendingTabs() | |
44 // calls |web_contents->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 // | |
26 class UnloadController : public content::NotificationObserver, | 54 class UnloadController : public content::NotificationObserver, |
27 public TabStripModelObserver { | 55 public TabStripModelObserver { |
28 public: | 56 public: |
29 explicit UnloadController(Browser* browser); | 57 explicit UnloadController(Browser* browser); |
30 virtual ~UnloadController(); | 58 virtual ~UnloadController(); |
31 | 59 |
32 // Returns true if |contents| can be cleanly closed. When |browser_| is being | 60 // Returns true if |contents| can be cleanly closed. When |browser_| is being |
33 // closed, this function will return false to indicate |contents| should not | 61 // closed, this function will return false to indicate |contents| should not |
34 // be cleanly closed, since the fast shutdown path will just kill its | 62 // be cleanly closed, since the fast shutdown path will just kill its |
35 // renderer. | 63 // renderer. |
36 bool CanCloseContents(content::WebContents* contents); | 64 bool CanCloseContents(content::WebContents* contents); |
37 | 65 |
38 // Called when a BeforeUnload handler is fired for |contents|. |proceed| | 66 // Called when a BeforeUnload handler is fired for |contents|. |proceed| |
39 // indicates the user's response to the Y/N BeforeUnload handler dialog. If | 67 // indicates the user's response to the Y/N BeforeUnload handler dialog. If |
40 // this parameter is false, any pending attempt to close the whole browser | 68 // this parameter is false, any pending attempt to close the whole browser |
41 // will be canceled. Returns true if Unload handlers should be fired. When the | 69 // will be canceled. Returns true if Unload handlers should be fired. When the |
42 // |browser_| is being closed, Unload handlers for any particular WebContents | 70 // |browser_| is being closed, Unload handlers for any particular WebContents |
43 // will not be run until every WebContents being closed has a chance to run | 71 // will not be run until every WebContents being closed has a chance to run |
44 // its BeforeUnloadHandler. | 72 // its BeforeUnloadHandler. |
45 bool BeforeUnloadFired(content::WebContents* contents, bool proceed); | 73 bool BeforeUnloadFired(content::WebContents* contents, bool proceed); |
46 | 74 |
47 bool is_attempting_to_close_browser() const { | 75 bool is_attempting_to_close_browser() const { |
48 return is_attempting_to_close_browser_; | 76 return is_attempting_to_close_browser_; |
49 } | 77 } |
50 | 78 |
51 // Called in response to a request to close |browser_|'s window. Returns true | 79 // Called in response to a request to close |browser_|'s window. Returns true |
52 // when there are no remaining unload handlers to be run. | 80 // when there are no remaining beforeunload handlers to be run. |
53 bool ShouldCloseWindow(); | 81 bool ShouldCloseWindow(); |
54 | 82 |
55 // Returns true if |browser_| has any tabs that have BeforeUnload handlers | 83 // Returns true if |browser_| has any tabs that have BeforeUnload handlers |
56 // that have not been fired. This method is non-const because it builds a list | 84 // that have not been fired. This method is non-const because it builds a list |
57 // of tabs that need their BeforeUnloadHandlers fired. | 85 // of tabs that need their BeforeUnloadHandlers fired. |
58 // TODO(beng): This seems like it could be private but it is used by | 86 // TODO(beng): This seems like it could be private but it is used by |
59 // AreAllBrowsersCloseable() in application_lifetime.cc. It seems | 87 // AreAllBrowsersCloseable() in application_lifetime.cc. It seems |
60 // very similar to ShouldCloseWindow() and some consolidation | 88 // very similar to ShouldCloseWindow() and some consolidation |
61 // could be pursued. | 89 // could be pursued. |
62 bool TabsNeedBeforeUnloadFired(); | 90 bool TabsNeedBeforeUnloadFired(); |
63 | 91 |
92 // Returns true if all tabs' beforeunload/unload events have fired. | |
93 bool HasCompletedUnloadProcessing() const; | |
94 | |
64 private: | 95 private: |
65 typedef std::set<content::WebContents*> UnloadListenerSet; | |
66 | |
67 // Overridden from content::NotificationObserver: | 96 // Overridden from content::NotificationObserver: |
68 virtual void Observe(int type, | 97 virtual void Observe(int type, |
69 const content::NotificationSource& source, | 98 const content::NotificationSource& source, |
70 const content::NotificationDetails& details) OVERRIDE; | 99 const content::NotificationDetails& details) OVERRIDE; |
71 | 100 |
72 // Overridden from TabStripModelObserver: | 101 // Overridden from TabStripModelObserver: |
73 virtual void TabInsertedAt(content::WebContents* contents, | 102 virtual void TabInsertedAt(content::WebContents* contents, |
74 int index, | 103 int index, |
75 bool foreground) OVERRIDE; | 104 bool foreground) OVERRIDE; |
76 virtual void TabDetachedAt(content::WebContents* contents, | 105 virtual void TabDetachedAt(content::WebContents* contents, |
77 int index) OVERRIDE; | 106 int index) OVERRIDE; |
78 virtual void TabReplacedAt(TabStripModel* tab_strip_model, | 107 virtual void TabReplacedAt(TabStripModel* tab_strip_model, |
79 content::WebContents* old_contents, | 108 content::WebContents* old_contents, |
80 content::WebContents* new_contents, | 109 content::WebContents* new_contents, |
81 int index) OVERRIDE; | 110 int index) OVERRIDE; |
82 virtual void TabStripEmpty() OVERRIDE; | 111 virtual void TabStripEmpty() OVERRIDE; |
83 | 112 |
84 void TabAttachedImpl(content::WebContents* contents); | 113 void TabAttachedImpl(content::WebContents* contents); |
85 void TabDetachedImpl(content::WebContents* contents); | 114 void TabDetachedImpl(content::WebContents* contents); |
86 | 115 |
116 // Detach |contents| and wait for it to finish closing. | |
117 // The close must be inititiated outside of this method. | |
Avi (use Gerrit)
2013/06/10 03:32:40
typo: inititiated
| |
118 // Returns true if it succeeds. | |
119 bool DetachWebContents(content::WebContents* contents); | |
120 | |
87 // Processes the next tab that needs it's beforeunload/unload event fired. | 121 // Processes the next tab that needs it's beforeunload/unload event fired. |
88 void ProcessPendingTabs(); | 122 void ProcessPendingTabs(); |
89 | 123 |
90 // Whether we've completed firing all the tabs' beforeunload/unload events. | |
91 bool HasCompletedUnloadProcessing() const; | |
92 | |
93 // Clears all the state associated with processing tabs' beforeunload/unload | 124 // Clears all the state associated with processing tabs' beforeunload/unload |
94 // events since the user cancelled closing the window. | 125 // events since the user cancelled closing the window. |
95 void CancelWindowClose(); | 126 void CancelWindowClose(); |
96 | 127 |
97 // Removes |web_contents| from the passed |set|. | 128 // Cleans up state appropriately when we are trying to close the |
98 // Returns whether the tab was in the set in the first place. | 129 // browser or close a tab in the background. We also use this in the |
99 bool RemoveFromSet(UnloadListenerSet* set, | 130 // cases where a tab crashes or hangs even if the |
100 content::WebContents* web_contents); | 131 // beforeunload/unload haven't successfully fired. |
132 void ClearUnloadState(content::WebContents* contents); | |
101 | 133 |
102 // Cleans up state appropriately when we are trying to close the browser and | 134 // Helper for |ClearUnloadState| to unwind stack before proceeding. |
103 // the tab has finished firing its unload handler. We also use this in the | 135 void PostTaskForProcessPendingTabs(); |
104 // cases where a tab crashes or hangs even if the beforeunload/unload haven't | 136 |
105 // successfully fired. If |process_now| is true |ProcessPendingTabs| is | 137 // Log a step of the unload processing. |
106 // invoked immediately, otherwise it is invoked after a delay (PostTask). | 138 void LogUnloadStep(const base::StringPiece& step_name, |
107 // | 139 content::WebContents* contents) const; |
108 // Typically you'll want to pass in true for |process_now|. Passing in true | |
109 // may result in deleting |tab|. If you know that shouldn't happen (because of | |
110 // the state of the stack), pass in false. | |
111 void ClearUnloadState(content::WebContents* web_contents, bool process_now); | |
112 | 140 |
113 Browser* browser_; | 141 Browser* browser_; |
114 | 142 |
115 content::NotificationRegistrar registrar_; | 143 content::NotificationRegistrar registrar_; |
116 | 144 |
117 // Tracks tabs that need there beforeunload event fired before we can | 145 typedef std::set<content::WebContents*> WebContentsSet; |
118 // close the browser. Only gets populated when we try to close the browser. | |
119 UnloadListenerSet tabs_needing_before_unload_fired_; | |
120 | 146 |
121 // Tracks tabs that need there unload event fired before we can | 147 // Tracks tabs that need their beforeunload event started. |
122 // close the browser. Only gets populated when we try to close the browser. | 148 // Only gets populated when we try to close the browser. |
123 UnloadListenerSet tabs_needing_unload_fired_; | 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_; | |
124 | 162 |
125 // Whether we are processing the beforeunload and unload events of each tab | 163 // Whether we are processing the beforeunload and unload events of each tab |
126 // in preparation for closing the browser. UnloadController owns this state | 164 // in preparation for closing the browser. UnloadController owns this state |
127 // rather than Browser because unload handlers are the only reason that a | 165 // rather than Browser because unload handlers are the only reason that a |
128 // Browser window isn't just immediately closed. | 166 // Browser window isn't just immediately closed. |
129 bool is_attempting_to_close_browser_; | 167 bool is_attempting_to_close_browser_; |
130 | 168 |
169 // Manage tabs with beforeunload/unload handlers that close detached. | |
170 class DetachedWebContentsDelegate; | |
171 scoped_ptr<DetachedWebContentsDelegate> detached_delegate_; | |
172 | |
131 base::WeakPtrFactory<UnloadController> weak_factory_; | 173 base::WeakPtrFactory<UnloadController> weak_factory_; |
132 | 174 |
133 DISALLOW_COPY_AND_ASSIGN(UnloadController); | 175 DISALLOW_COPY_AND_ASSIGN(UnloadController); |
134 }; | 176 }; |
135 | 177 |
136 } // namespace chrome | 178 } // namespace chrome |
137 | 179 |
138 #endif // CHROME_BROWSER_UI_UNLOAD_CONTROLLER_H_ | 180 #endif // CHROME_BROWSER_UI_UNLOAD_CONTROLLER_H_ |
OLD | NEW |