| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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 <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "chrome/browser/ui/browser_navigator.h" | 10 #include "chrome/browser/ui/browser_navigator.h" |
| 11 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 11 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 12 #include "content/public/browser/render_view_host.h" | 12 #include "content/public/browser/render_view_host.h" |
| 13 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
| 14 #include "content/public/browser/web_contents_delegate.h" | 14 #include "content/public/browser/web_contents_delegate.h" |
| 15 | 15 |
| 16 // WebContentsDelegate implementation. This owns the TabContents supplied to the | 16 // WebContentsDelegate implementation. This owns the WebContents supplied to the |
| 17 // constructor. | 17 // constructor. |
| 18 class InstantUnloadHandler::WebContentsDelegateImpl | 18 class InstantUnloadHandler::WebContentsDelegateImpl |
| 19 : public content::WebContentsDelegate { | 19 : public content::WebContentsDelegate { |
| 20 public: | 20 public: |
| 21 WebContentsDelegateImpl(InstantUnloadHandler* handler, | 21 WebContentsDelegateImpl(InstantUnloadHandler* handler, |
| 22 TabContents* tab_contents, | 22 content::WebContents* contents, |
| 23 int index) | 23 int index) |
| 24 : handler_(handler), | 24 : handler_(handler), |
| 25 tab_contents_(tab_contents), | 25 contents_(contents), |
| 26 index_(index) { | 26 index_(index) { |
| 27 tab_contents->web_contents()->SetDelegate(this); | 27 contents->SetDelegate(this); |
| 28 } | 28 } |
| 29 | 29 |
| 30 // content::WebContentsDelegate overrides: | 30 // content::WebContentsDelegate overrides: |
| 31 virtual void WillRunBeforeUnloadConfirm() OVERRIDE { | 31 virtual void WillRunBeforeUnloadConfirm() OVERRIDE { |
| 32 TabContents* tab = tab_contents_.release(); | 32 content::WebContents* contents = contents_.release(); |
| 33 tab->web_contents()->SetDelegate(NULL); | 33 contents->SetDelegate(NULL); |
| 34 handler_->Activate(this, tab, index_); | 34 handler_->Activate(this, contents, index_); |
| 35 } | 35 } |
| 36 | 36 |
| 37 virtual bool ShouldSuppressDialogs() OVERRIDE { | 37 virtual bool ShouldSuppressDialogs() OVERRIDE { |
| 38 return true; // Return true so dialogs are suppressed. | 38 return true; // Return true so dialogs are suppressed. |
| 39 } | 39 } |
| 40 | 40 |
| 41 virtual void CloseContents(content::WebContents* source) OVERRIDE { | 41 virtual void CloseContents(content::WebContents* source) OVERRIDE { |
| 42 tab_contents_->web_contents()->SetDelegate(NULL); | 42 contents_->SetDelegate(NULL); |
| 43 handler_->Destroy(this); | 43 handler_->Destroy(this); |
| 44 } | 44 } |
| 45 | 45 |
| 46 private: | 46 private: |
| 47 InstantUnloadHandler* const handler_; | 47 InstantUnloadHandler* const handler_; |
| 48 scoped_ptr<TabContents> tab_contents_; | 48 scoped_ptr<content::WebContents> contents_; |
| 49 | 49 |
| 50 // The index |tab_contents_| was originally at. If we add the tab back we add | 50 // The index |contents_| was originally at. If we add the tab back we add it |
| 51 // it at this index. | 51 // at this index. |
| 52 const int index_; | 52 const int index_; |
| 53 | 53 |
| 54 DISALLOW_COPY_AND_ASSIGN(WebContentsDelegateImpl); | 54 DISALLOW_COPY_AND_ASSIGN(WebContentsDelegateImpl); |
| 55 }; | 55 }; |
| 56 | 56 |
| 57 InstantUnloadHandler::InstantUnloadHandler(Browser* browser) | 57 InstantUnloadHandler::InstantUnloadHandler(Browser* browser) |
| 58 : browser_(browser) { | 58 : browser_(browser) { |
| 59 } | 59 } |
| 60 | 60 |
| 61 InstantUnloadHandler::~InstantUnloadHandler() { | 61 InstantUnloadHandler::~InstantUnloadHandler() { |
| 62 } | 62 } |
| 63 | 63 |
| 64 void InstantUnloadHandler::RunUnloadListenersOrDestroy(TabContents* tab, | 64 void InstantUnloadHandler::RunUnloadListenersOrDestroy( |
| 65 int index) { | 65 content::WebContents* contents, |
| 66 if (!tab->web_contents()->NeedToFireBeforeUnload()) { | 66 int index) { |
| 67 if (!contents->NeedToFireBeforeUnload()) { |
| 67 // Tab doesn't have any beforeunload listeners and can be safely deleted. | 68 // Tab doesn't have any beforeunload listeners and can be safely deleted. |
| 68 delete tab; | 69 delete contents; |
| 69 return; | 70 return; |
| 70 } | 71 } |
| 71 | 72 |
| 72 // Tab has before unload listener. Install a delegate and fire the before | 73 // Tab has before unload listener. Install a delegate and fire the before |
| 73 // unload listener. | 74 // unload listener. |
| 74 delegates_.push_back(new WebContentsDelegateImpl(this, tab, index)); | 75 delegates_.push_back(new WebContentsDelegateImpl(this, contents, index)); |
| 75 tab->web_contents()->GetRenderViewHost()->FirePageBeforeUnload(false); | 76 contents->GetRenderViewHost()->FirePageBeforeUnload(false); |
| 76 } | 77 } |
| 77 | 78 |
| 78 void InstantUnloadHandler::Activate(WebContentsDelegateImpl* delegate, | 79 void InstantUnloadHandler::Activate(WebContentsDelegateImpl* delegate, |
| 79 TabContents* tab, | 80 content::WebContents* contents, |
| 80 int index) { | 81 int index) { |
| 81 chrome::NavigateParams params(browser_, tab); | 82 chrome::NavigateParams params(browser_, |
| 83 TabContents::FromWebContents(contents)); |
| 82 params.disposition = NEW_FOREGROUND_TAB; | 84 params.disposition = NEW_FOREGROUND_TAB; |
| 83 params.tabstrip_index = index; | 85 params.tabstrip_index = index; |
| 84 | 86 |
| 85 // Remove (and delete) the delegate. | 87 // Remove (and delete) the delegate. |
| 86 Destroy(delegate); | 88 Destroy(delegate); |
| 87 | 89 |
| 88 // Add the tab back in. | 90 // Add the tab back in. |
| 89 chrome::Navigate(¶ms); | 91 chrome::Navigate(¶ms); |
| 90 } | 92 } |
| 91 | 93 |
| 92 void InstantUnloadHandler::Destroy(WebContentsDelegateImpl* delegate) { | 94 void InstantUnloadHandler::Destroy(WebContentsDelegateImpl* delegate) { |
| 93 ScopedVector<WebContentsDelegateImpl>::iterator i = | 95 ScopedVector<WebContentsDelegateImpl>::iterator i = |
| 94 std::find(delegates_.begin(), delegates_.end(), delegate); | 96 std::find(delegates_.begin(), delegates_.end(), delegate); |
| 95 DCHECK(i != delegates_.end()); | 97 DCHECK(i != delegates_.end()); |
| 96 | 98 |
| 97 // The delegate's method is a caller on the stack, so schedule the deletion | 99 // The delegate's method is a caller on the stack, so schedule the deletion |
| 98 // for later. | 100 // for later. |
| 99 delegates_.weak_erase(i); | 101 delegates_.weak_erase(i); |
| 100 MessageLoop::current()->DeleteSoon(FROM_HERE, delegate); | 102 MessageLoop::current()->DeleteSoon(FROM_HERE, delegate); |
| 101 } | 103 } |
| OLD | NEW |