Chromium Code Reviews| 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 OpenURLFromTab(TabContents* source, | |
|
Charlie Reis
2010/12/08 01:26:12
What is the implication of having all of these do
sky
2010/12/08 03:04:22
Yes, it's intentional. I added a comment.
| |
| 51 const GURL& url, const GURL& referrer, | |
| 52 WindowOpenDisposition disposition, | |
| 53 PageTransition::Type transition) {} | |
| 54 virtual void NavigationStateChanged(const TabContents* source, | |
| 55 unsigned changed_flags) {} | |
| 56 virtual void AddNewContents(TabContents* source, | |
| 57 TabContents* new_contents, | |
| 58 WindowOpenDisposition disposition, | |
| 59 const gfx::Rect& initial_pos, | |
| 60 bool user_gesture) {} | |
| 61 virtual void ActivateContents(TabContents* contents) {} | |
| 62 virtual void DeactivateContents(TabContents* contents) {} | |
| 63 virtual void LoadingStateChanged(TabContents* source) {} | |
| 64 virtual void CloseContents(TabContents* source) { | |
| 65 handler_->Destroy(this); | |
| 66 } | |
| 67 virtual void MoveContents(TabContents* source, const gfx::Rect& pos) {} | |
| 68 virtual void ToolbarSizeChanged(TabContents* source, bool is_animating) {} | |
| 69 virtual void URLStarredChanged(TabContents* source, bool starred) {} | |
| 70 virtual void UpdateTargetURL(TabContents* source, const GURL& url) {} | |
| 71 | |
| 72 private: | |
| 73 InstantUnloadHandler* handler_; | |
| 74 scoped_ptr<TabContentsWrapper> tab_contents_; | |
| 75 | |
| 76 // The index the newly created tab was inserted at. If we add the tab back we | |
| 77 // add it at this index. | |
|
Charlie Reis
2010/12/08 01:26:12
I don't understand this comment. The first senten
sky
2010/12/08 03:04:22
Done.
| |
| 78 const int index_; | |
| 79 | |
| 80 DISALLOW_COPY_AND_ASSIGN(TabContentsDelegateImpl); | |
| 81 }; | |
| 82 | |
| 83 InstantUnloadHandler::InstantUnloadHandler(Browser* browser) | |
| 84 : browser_(browser) { | |
| 85 } | |
| 86 | |
| 87 InstantUnloadHandler::~InstantUnloadHandler() { | |
| 88 } | |
| 89 | |
| 90 void InstantUnloadHandler::Own(TabContentsWrapper* tab, int index) { | |
| 91 if (!tab->tab_contents()->NeedToFireBeforeUnload()) { | |
| 92 // Tab doesn't have any before unload listeners and can be safely deleted. | |
|
Charlie Reis
2010/12/08 01:26:12
Again, we need a TODO for the unload handler (unle
sky
2010/12/08 03:04:22
NeedToFireBeforeUnload returns true for both cases
| |
| 93 delete tab; | |
| 94 return; | |
| 95 } | |
| 96 | |
| 97 // Tab has before unload listeners, Install a delegate and fire the unload | |
| 98 // before listener. | |
|
Charlie Reis
2010/12/08 01:26:12
Typo: "unload before listener"
| |
| 99 TabContentsDelegateImpl* delegate = | |
| 100 new TabContentsDelegateImpl(this, tab, index); | |
| 101 delegates_.push_back(delegate); | |
| 102 // TODO: decide if we really want false here. false is used for tab closes, | |
| 103 // and is needed so that the tab correctly closes but it doesn't really match | |
| 104 // what's logically happening. | |
|
Charlie Reis
2010/12/08 01:26:12
This sounds right at first glance, but I haven't l
sky
2010/12/08 03:04:22
Done.
| |
| 105 tab->tab_contents()->render_view_host()->FirePageBeforeUnload(false); | |
| 106 } | |
| 107 | |
| 108 void InstantUnloadHandler::Activate(TabContentsDelegateImpl* delegate) { | |
| 109 // Take ownership of the TabContents from the delegate. | |
| 110 TabContentsWrapper* tab = delegate->ReleaseTab(); | |
| 111 browser::NavigateParams params(browser_, tab); | |
| 112 params.disposition = NEW_FOREGROUND_TAB; | |
| 113 params.tabstrip_index = delegate->index(); | |
| 114 | |
| 115 // Remove (and delete) the delegate. | |
|
Charlie Reis
2010/12/08 01:26:12
Technically, it's not deleting the delegate, is it
sky
2010/12/08 03:04:22
Yes, scopedvector's erase deletes here.
| |
| 116 ScopedVector<TabContentsDelegateImpl>::iterator i = | |
| 117 std::find(delegates_.begin(), delegates_.end(), delegate); | |
| 118 DCHECK(i != delegates_.end()); | |
| 119 delegates_.erase(i); | |
| 120 delegate = NULL; | |
| 121 | |
| 122 // Add the tab back in. | |
| 123 browser::Navigate(¶ms); | |
| 124 } | |
| 125 | |
| 126 void InstantUnloadHandler::Destroy(TabContentsDelegateImpl* delegate) { | |
| 127 ScopedVector<TabContentsDelegateImpl>::iterator i = | |
| 128 std::find(delegates_.begin(), delegates_.end(), delegate); | |
| 129 DCHECK(i != delegates_.end()); | |
| 130 delegates_.erase(i); | |
|
Charlie Reis
2010/12/08 01:26:12
This doesn't seem to be doing what the comment say
sky
2010/12/08 03:04:22
ScopedVector's erase deletes.
Charlie Reis
2010/12/08 05:50:11
Ah, I missed the fact that the delegate's tab will
| |
| 131 } | |
| OLD | NEW |