| 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::RunUnloadListenersOrDestroy(TabContentsWrapper* tab, |
| 94 int index) { |
| 95 if (!tab->tab_contents()->NeedToFireBeforeUnload()) { |
| 96 // Tab doesn't have any before unload listeners and can be safely deleted. |
| 97 delete tab; |
| 98 return; |
| 99 } |
| 100 |
| 101 // Tab has before unload listener. Install a delegate and fire the before |
| 102 // unload listener. |
| 103 TabContentsDelegateImpl* delegate = |
| 104 new TabContentsDelegateImpl(this, tab, index); |
| 105 delegates_.push_back(delegate); |
| 106 // TODO: decide if we really want false here. false is used for tab closes, |
| 107 // and is needed so that the tab correctly closes but it doesn't really match |
| 108 // what's logically happening. |
| 109 tab->tab_contents()->render_view_host()->FirePageBeforeUnload(false); |
| 110 } |
| 111 |
| 112 void InstantUnloadHandler::Activate(TabContentsDelegateImpl* delegate) { |
| 113 // Take ownership of the TabContents from the delegate. |
| 114 TabContentsWrapper* tab = delegate->ReleaseTab(); |
| 115 browser::NavigateParams params(browser_, tab); |
| 116 params.disposition = NEW_FOREGROUND_TAB; |
| 117 params.tabstrip_index = delegate->index(); |
| 118 |
| 119 // Remove (and delete) the delegate. |
| 120 ScopedVector<TabContentsDelegateImpl>::iterator i = |
| 121 std::find(delegates_.begin(), delegates_.end(), delegate); |
| 122 DCHECK(i != delegates_.end()); |
| 123 delegates_.erase(i); |
| 124 delegate = NULL; |
| 125 |
| 126 // Add the tab back in. |
| 127 browser::Navigate(¶ms); |
| 128 } |
| 129 |
| 130 void InstantUnloadHandler::Destroy(TabContentsDelegateImpl* delegate) { |
| 131 ScopedVector<TabContentsDelegateImpl>::iterator i = |
| 132 std::find(delegates_.begin(), delegates_.end(), delegate); |
| 133 DCHECK(i != delegates_.end()); |
| 134 delegates_.erase(i); |
| 135 } |
| OLD | NEW |