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