| OLD | NEW | 
|---|
| (Empty) |  | 
|  | 1 // Copyright (c) 2010 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 #include "chrome/browser/instant/instant_unload_handler.h" | 
|  | 6 | 
|  | 7 #include "chrome/browser/renderer_host/render_view_host.h" | 
|  | 8 #include "chrome/browser/tab_contents/tab_contents.h" | 
|  | 9 #include "chrome/browser/tab_contents/tab_contents_delegate.h" | 
|  | 10 #include "chrome/browser/ui/browser.h" | 
|  | 11 #include "chrome/browser/ui/browser_navigator.h" | 
|  | 12 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 
|  | 13 | 
|  | 14 // TabContentsDelegate implementation. This owns the TabContents supplied to the | 
|  | 15 // constructor. | 
|  | 16 class InstantUnloadHandler::TabContentsDelegateImpl | 
|  | 17     : public TabContentsDelegate { | 
|  | 18  public: | 
|  | 19   TabContentsDelegateImpl(InstantUnloadHandler* handler, | 
|  | 20                           TabContentsWrapper* tab_contents, | 
|  | 21                           int index) | 
|  | 22       : handler_(handler), | 
|  | 23         tab_contents_(tab_contents), | 
|  | 24         index_(index) { | 
|  | 25     tab_contents->tab_contents()->set_delegate(this); | 
|  | 26   } | 
|  | 27 | 
|  | 28   ~TabContentsDelegateImpl() { | 
|  | 29   } | 
|  | 30 | 
|  | 31   // Releases ownership of the TabContentsWrapper to the caller. | 
|  | 32   TabContentsWrapper* ReleaseTab() { | 
|  | 33     TabContentsWrapper* tab = tab_contents_.release(); | 
|  | 34     tab->tab_contents()->set_delegate(NULL); | 
|  | 35     return tab; | 
|  | 36   } | 
|  | 37 | 
|  | 38   // See description above field. | 
|  | 39   int index() const { return index_; } | 
|  | 40 | 
|  | 41   // TabContentsDelegate overrides: | 
|  | 42   virtual void WillRunBeforeUnloadConfirm() { | 
|  | 43     handler_->Activate(this); | 
|  | 44   } | 
|  | 45 | 
|  | 46   virtual bool ShouldSuppressDialogs() { | 
|  | 47     return true;  // Return true so dialogs are suppressed. | 
|  | 48   } | 
|  | 49 | 
|  | 50   virtual void CloseContents(TabContents* source) { | 
|  | 51     handler_->Destroy(this); | 
|  | 52   } | 
|  | 53 | 
|  | 54   // All of the following are overriden to do nothing (they are pure | 
|  | 55   // virtual). When we're attemping to close the tab, none of this matters. | 
|  | 56   virtual void OpenURLFromTab(TabContents* source, | 
|  | 57                               const GURL& url, const GURL& referrer, | 
|  | 58                               WindowOpenDisposition disposition, | 
|  | 59                               PageTransition::Type transition) {} | 
|  | 60   virtual void NavigationStateChanged(const TabContents* source, | 
|  | 61                                       unsigned changed_flags) {} | 
|  | 62   virtual void AddNewContents(TabContents* source, | 
|  | 63                               TabContents* new_contents, | 
|  | 64                               WindowOpenDisposition disposition, | 
|  | 65                               const gfx::Rect& initial_pos, | 
|  | 66                               bool user_gesture) {} | 
|  | 67   virtual void ActivateContents(TabContents* contents) {} | 
|  | 68   virtual void DeactivateContents(TabContents* contents) {} | 
|  | 69   virtual void LoadingStateChanged(TabContents* source) {} | 
|  | 70   virtual void MoveContents(TabContents* source, const gfx::Rect& pos) {} | 
|  | 71   virtual void ToolbarSizeChanged(TabContents* source, bool is_animating) {} | 
|  | 72   virtual void URLStarredChanged(TabContents* source, bool starred) {} | 
|  | 73   virtual void UpdateTargetURL(TabContents* source, const GURL& url) {} | 
|  | 74 | 
|  | 75  private: | 
|  | 76   InstantUnloadHandler* handler_; | 
|  | 77   scoped_ptr<TabContentsWrapper> tab_contents_; | 
|  | 78 | 
|  | 79   // The index |tab_contents_| was originally at. If we add the tab back we add | 
|  | 80   // it at this index. | 
|  | 81   const int index_; | 
|  | 82 | 
|  | 83   DISALLOW_COPY_AND_ASSIGN(TabContentsDelegateImpl); | 
|  | 84 }; | 
|  | 85 | 
|  | 86 InstantUnloadHandler::InstantUnloadHandler(Browser* browser) | 
|  | 87     : browser_(browser) { | 
|  | 88 } | 
|  | 89 | 
|  | 90 InstantUnloadHandler::~InstantUnloadHandler() { | 
|  | 91 } | 
|  | 92 | 
|  | 93 void InstantUnloadHandler::Own(TabContentsWrapper* tab, int index) { | 
|  | 94   if (!tab->tab_contents()->NeedToFireBeforeUnload()) { | 
|  | 95     // Tab doesn't have any before unload listeners and can be safely deleted. | 
|  | 96     delete tab; | 
|  | 97     return; | 
|  | 98   } | 
|  | 99 | 
|  | 100   // Tab has before unload listener. Install a delegate and fire the before | 
|  | 101   // unload listener. | 
|  | 102   TabContentsDelegateImpl* delegate = | 
|  | 103       new TabContentsDelegateImpl(this, tab, index); | 
|  | 104   delegates_.push_back(delegate); | 
|  | 105   // TODO: decide if we really want false here. false is used for tab closes, | 
|  | 106   // and is needed so that the tab correctly closes but it doesn't really match | 
|  | 107   // what's logically happening. | 
|  | 108   tab->tab_contents()->render_view_host()->FirePageBeforeUnload(false); | 
|  | 109 } | 
|  | 110 | 
|  | 111 void InstantUnloadHandler::Activate(TabContentsDelegateImpl* delegate) { | 
|  | 112   // Take ownership of the TabContents from the delegate. | 
|  | 113   TabContentsWrapper* tab = delegate->ReleaseTab(); | 
|  | 114   browser::NavigateParams params(browser_, tab); | 
|  | 115   params.disposition = NEW_FOREGROUND_TAB; | 
|  | 116   params.tabstrip_index = delegate->index(); | 
|  | 117 | 
|  | 118   // Remove (and delete) the delegate. | 
|  | 119   ScopedVector<TabContentsDelegateImpl>::iterator i = | 
|  | 120       std::find(delegates_.begin(), delegates_.end(), delegate); | 
|  | 121   DCHECK(i != delegates_.end()); | 
|  | 122   delegates_.erase(i); | 
|  | 123   delegate = NULL; | 
|  | 124 | 
|  | 125   // Add the tab back in. | 
|  | 126   browser::Navigate(¶ms); | 
|  | 127 } | 
|  | 128 | 
|  | 129 void InstantUnloadHandler::Destroy(TabContentsDelegateImpl* delegate) { | 
|  | 130   ScopedVector<TabContentsDelegateImpl>::iterator i = | 
|  | 131       std::find(delegates_.begin(), delegates_.end(), delegate); | 
|  | 132   DCHECK(i != delegates_.end()); | 
|  | 133   delegates_.erase(i); | 
|  | 134 } | 
| OLD | NEW | 
|---|