| 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 #include "chrome/browser/instant/instant_unload_handler.h" | 5 #include "chrome/browser/instant/instant_unload_handler.h" |
| 6 | 6 |
| 7 #include "chrome/browser/ui/browser.h" | 7 #include "chrome/browser/ui/browser.h" |
| 8 #include "chrome/browser/ui/browser_navigator.h" | 8 #include "chrome/browser/ui/browser_navigator.h" |
| 9 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 9 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 10 #include "content/public/browser/render_view_host.h" | 10 #include "content/public/browser/render_view_host.h" |
| 11 #include "content/public/browser/web_contents.h" | 11 #include "content/public/browser/web_contents.h" |
| 12 #include "content/public/browser/web_contents_delegate.h" | 12 #include "content/public/browser/web_contents_delegate.h" |
| 13 | 13 |
| 14 #include <algorithm> | 14 #include <algorithm> |
| 15 | 15 |
| 16 using content::WebContents; | 16 using content::WebContents; |
| 17 | 17 |
| 18 // TabContentsDelegate implementation. This owns the TabContentsWrapper supplied | 18 // TabContentsDelegate implementation. This owns the TabContents supplied |
| 19 // to the constructor. | 19 // to the constructor. |
| 20 class InstantUnloadHandler::TabContentsDelegateImpl | 20 class InstantUnloadHandler::TabContentsDelegateImpl |
| 21 : public content::WebContentsDelegate { | 21 : public content::WebContentsDelegate { |
| 22 public: | 22 public: |
| 23 TabContentsDelegateImpl(InstantUnloadHandler* handler, | 23 TabContentsDelegateImpl(InstantUnloadHandler* handler, |
| 24 TabContentsWrapper* tab_contents, | 24 TabContents* tab_contents, |
| 25 int index) | 25 int index) |
| 26 : handler_(handler), | 26 : handler_(handler), |
| 27 tab_contents_(tab_contents), | 27 tab_contents_(tab_contents), |
| 28 index_(index) { | 28 index_(index) { |
| 29 tab_contents->web_contents()->SetDelegate(this); | 29 tab_contents->web_contents()->SetDelegate(this); |
| 30 } | 30 } |
| 31 | 31 |
| 32 ~TabContentsDelegateImpl() { | 32 ~TabContentsDelegateImpl() { |
| 33 } | 33 } |
| 34 | 34 |
| 35 // Releases ownership of the TabContentsWrapper to the caller. | 35 // Releases ownership of the TabContents to the caller. |
| 36 TabContentsWrapper* ReleaseTab() { | 36 TabContents* ReleaseTab() { |
| 37 TabContentsWrapper* tab = tab_contents_.release(); | 37 TabContents* tab = tab_contents_.release(); |
| 38 tab->web_contents()->SetDelegate(NULL); | 38 tab->web_contents()->SetDelegate(NULL); |
| 39 return tab; | 39 return tab; |
| 40 } | 40 } |
| 41 | 41 |
| 42 // See description above field. | 42 // See description above field. |
| 43 int index() const { return index_; } | 43 int index() const { return index_; } |
| 44 | 44 |
| 45 // content::WebContentsDelegate overrides: | 45 // content::WebContentsDelegate overrides: |
| 46 virtual void WillRunBeforeUnloadConfirm() { | 46 virtual void WillRunBeforeUnloadConfirm() { |
| 47 handler_->Activate(this); | 47 handler_->Activate(this); |
| 48 } | 48 } |
| 49 | 49 |
| 50 virtual bool ShouldSuppressDialogs() { | 50 virtual bool ShouldSuppressDialogs() { |
| 51 return true; // Return true so dialogs are suppressed. | 51 return true; // Return true so dialogs are suppressed. |
| 52 } | 52 } |
| 53 | 53 |
| 54 virtual void CloseContents(WebContents* source) OVERRIDE { | 54 virtual void CloseContents(WebContents* source) OVERRIDE { |
| 55 handler_->Destroy(this); | 55 handler_->Destroy(this); |
| 56 } | 56 } |
| 57 | 57 |
| 58 private: | 58 private: |
| 59 InstantUnloadHandler* handler_; | 59 InstantUnloadHandler* handler_; |
| 60 scoped_ptr<TabContentsWrapper> tab_contents_; | 60 scoped_ptr<TabContents> tab_contents_; |
| 61 | 61 |
| 62 // The index |tab_contents_| was originally at. If we add the tab back we add | 62 // The index |tab_contents_| was originally at. If we add the tab back we add |
| 63 // it at this index. | 63 // it at this index. |
| 64 const int index_; | 64 const int index_; |
| 65 | 65 |
| 66 DISALLOW_COPY_AND_ASSIGN(TabContentsDelegateImpl); | 66 DISALLOW_COPY_AND_ASSIGN(TabContentsDelegateImpl); |
| 67 }; | 67 }; |
| 68 | 68 |
| 69 InstantUnloadHandler::InstantUnloadHandler(Browser* browser) | 69 InstantUnloadHandler::InstantUnloadHandler(Browser* browser) |
| 70 : browser_(browser) { | 70 : browser_(browser) { |
| 71 } | 71 } |
| 72 | 72 |
| 73 InstantUnloadHandler::~InstantUnloadHandler() { | 73 InstantUnloadHandler::~InstantUnloadHandler() { |
| 74 } | 74 } |
| 75 | 75 |
| 76 void InstantUnloadHandler::RunUnloadListenersOrDestroy(TabContentsWrapper* tab, | 76 void InstantUnloadHandler::RunUnloadListenersOrDestroy(TabContents* tab, |
| 77 int index) { | 77 int index) { |
| 78 if (!tab->web_contents()->NeedToFireBeforeUnload()) { | 78 if (!tab->web_contents()->NeedToFireBeforeUnload()) { |
| 79 // Tab doesn't have any before unload listeners and can be safely deleted. | 79 // Tab doesn't have any before unload listeners and can be safely deleted. |
| 80 delete tab; | 80 delete tab; |
| 81 return; | 81 return; |
| 82 } | 82 } |
| 83 | 83 |
| 84 // Tab has before unload listener. Install a delegate and fire the before | 84 // Tab has before unload listener. Install a delegate and fire the before |
| 85 // unload listener. | 85 // unload listener. |
| 86 TabContentsDelegateImpl* delegate = | 86 TabContentsDelegateImpl* delegate = |
| 87 new TabContentsDelegateImpl(this, tab, index); | 87 new TabContentsDelegateImpl(this, tab, index); |
| 88 delegates_.push_back(delegate); | 88 delegates_.push_back(delegate); |
| 89 // TODO: decide if we really want false here. false is used for tab closes, | 89 // TODO: decide if we really want false here. false is used for tab closes, |
| 90 // and is needed so that the tab correctly closes but it doesn't really match | 90 // and is needed so that the tab correctly closes but it doesn't really match |
| 91 // what's logically happening. | 91 // what's logically happening. |
| 92 tab->web_contents()->GetRenderViewHost()->FirePageBeforeUnload(false); | 92 tab->web_contents()->GetRenderViewHost()->FirePageBeforeUnload(false); |
| 93 } | 93 } |
| 94 | 94 |
| 95 void InstantUnloadHandler::Activate(TabContentsDelegateImpl* delegate) { | 95 void InstantUnloadHandler::Activate(TabContentsDelegateImpl* delegate) { |
| 96 // Take ownership of the TabContentsWrapper from the delegate. | 96 // Take ownership of the TabContents from the delegate. |
| 97 TabContentsWrapper* tab = delegate->ReleaseTab(); | 97 TabContents* tab = delegate->ReleaseTab(); |
| 98 browser::NavigateParams params(browser_, tab); | 98 browser::NavigateParams params(browser_, tab); |
| 99 params.disposition = NEW_FOREGROUND_TAB; | 99 params.disposition = NEW_FOREGROUND_TAB; |
| 100 params.tabstrip_index = delegate->index(); | 100 params.tabstrip_index = delegate->index(); |
| 101 | 101 |
| 102 // Remove (and delete) the delegate. | 102 // Remove (and delete) the delegate. |
| 103 ScopedVector<TabContentsDelegateImpl>::iterator i = | 103 ScopedVector<TabContentsDelegateImpl>::iterator i = |
| 104 std::find(delegates_.begin(), delegates_.end(), delegate); | 104 std::find(delegates_.begin(), delegates_.end(), delegate); |
| 105 DCHECK(i != delegates_.end()); | 105 DCHECK(i != delegates_.end()); |
| 106 delegates_.erase(i); | 106 delegates_.erase(i); |
| 107 delegate = NULL; | 107 delegate = NULL; |
| 108 | 108 |
| 109 // Add the tab back in. | 109 // Add the tab back in. |
| 110 browser::Navigate(¶ms); | 110 browser::Navigate(¶ms); |
| 111 } | 111 } |
| 112 | 112 |
| 113 void InstantUnloadHandler::Destroy(TabContentsDelegateImpl* delegate) { | 113 void InstantUnloadHandler::Destroy(TabContentsDelegateImpl* delegate) { |
| 114 ScopedVector<TabContentsDelegateImpl>::iterator i = | 114 ScopedVector<TabContentsDelegateImpl>::iterator i = |
| 115 std::find(delegates_.begin(), delegates_.end(), delegate); | 115 std::find(delegates_.begin(), delegates_.end(), delegate); |
| 116 DCHECK(i != delegates_.end()); | 116 DCHECK(i != delegates_.end()); |
| 117 delegates_.erase(i); | 117 delegates_.erase(i); |
| 118 } | 118 } |
| OLD | NEW |