| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/blocked_content_container.h" | 5 #include "chrome/browser/ui/content_settings/blocked_content_container.h" |
| 6 | 6 |
| 7 #include "chrome/browser/ui/content_settings/content_settings_tab_helper.h" |
| 8 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 7 #include "content/browser/tab_contents/tab_contents.h" | 9 #include "content/browser/tab_contents/tab_contents.h" |
| 8 #include "ui/gfx/rect.h" | 10 #include "ui/gfx/rect.h" |
| 9 | 11 |
| 10 // static | 12 // static |
| 11 const size_t BlockedContentContainer::kImpossibleNumberOfPopups = 30; | 13 const size_t BlockedContentContainer::kImpossibleNumberOfPopups = 30; |
| 12 | 14 |
| 13 struct BlockedContentContainer::BlockedContent { | 15 struct BlockedContentContainer::BlockedContent { |
| 14 BlockedContent(TabContents* tab_contents, | 16 BlockedContent(TabContentsWrapper* tab_contents, |
| 15 WindowOpenDisposition disposition, | 17 WindowOpenDisposition disposition, |
| 16 const gfx::Rect& bounds, | 18 const gfx::Rect& bounds, |
| 17 bool user_gesture) | 19 bool user_gesture) |
| 18 : tab_contents(tab_contents), | 20 : tab_contents(tab_contents), |
| 19 disposition(disposition), | 21 disposition(disposition), |
| 20 bounds(bounds), | 22 bounds(bounds), |
| 21 user_gesture(user_gesture) { | 23 user_gesture(user_gesture) { |
| 22 } | 24 } |
| 23 | 25 |
| 24 TabContents* tab_contents; | 26 TabContentsWrapper* tab_contents; |
| 25 WindowOpenDisposition disposition; | 27 WindowOpenDisposition disposition; |
| 26 gfx::Rect bounds; | 28 gfx::Rect bounds; |
| 27 bool user_gesture; | 29 bool user_gesture; |
| 28 }; | 30 }; |
| 29 | 31 |
| 30 BlockedContentContainer::BlockedContentContainer(TabContents* owner) | 32 BlockedContentContainer::BlockedContentContainer(TabContentsWrapper* owner) |
| 31 : owner_(owner) { | 33 : owner_(owner) { |
| 32 } | 34 } |
| 33 | 35 |
| 34 BlockedContentContainer::~BlockedContentContainer() {} | 36 BlockedContentContainer::~BlockedContentContainer() { |
| 37 Clear(); |
| 38 } |
| 35 | 39 |
| 36 void BlockedContentContainer::AddTabContents(TabContents* tab_contents, | 40 void BlockedContentContainer::AddTabContents(TabContentsWrapper* tab_contents, |
| 37 WindowOpenDisposition disposition, | 41 WindowOpenDisposition disposition, |
| 38 const gfx::Rect& bounds, | 42 const gfx::Rect& bounds, |
| 39 bool user_gesture) { | 43 bool user_gesture) { |
| 40 if (blocked_contents_.size() == (kImpossibleNumberOfPopups - 1)) { | 44 if (blocked_contents_.size() == (kImpossibleNumberOfPopups - 1)) { |
| 41 delete tab_contents; | 45 delete tab_contents; |
| 42 VLOG(1) << "Warning: Renderer is sending more popups to us than should be " | 46 VLOG(1) << "Warning: Renderer is sending more popups to us than should be " |
| 43 "possible. Renderer compromised?"; | 47 "possible. Renderer compromised?"; |
| 44 return; | 48 return; |
| 45 } | 49 } |
| 46 | 50 |
| 47 blocked_contents_.push_back( | 51 blocked_contents_.push_back( |
| 48 BlockedContent(tab_contents, disposition, bounds, user_gesture)); | 52 BlockedContent(tab_contents, disposition, bounds, user_gesture)); |
| 49 tab_contents->set_delegate(this); | 53 // TODO(avi): remove once TabContentsDelegate::GetConstrainingContents goes |
| 50 // Since the new tab_contents will not be showed, call WasHidden to change | 54 // away. |
| 55 tab_contents->tab_contents()->set_delegate(this); |
| 56 tab_contents->content_settings_tab_helper()->set_delegate(this); |
| 57 // Since the new tab_contents will not be shown, call WasHidden to change |
| 51 // its status on both RenderViewHost and RenderView. | 58 // its status on both RenderViewHost and RenderView. |
| 52 tab_contents->WasHidden(); | 59 tab_contents->tab_contents()->WasHidden(); |
| 53 if (blocked_contents_.size() == 1) | |
| 54 owner_->PopupNotificationVisibilityChanged(true); | |
| 55 } | 60 } |
| 56 | 61 |
| 57 void BlockedContentContainer::LaunchForContents(TabContents* tab_contents) { | 62 void BlockedContentContainer::LaunchForContents( |
| 63 TabContentsWrapper* tab_contents) { |
| 58 // Open the popup. | 64 // Open the popup. |
| 59 for (BlockedContents::iterator i(blocked_contents_.begin()); | 65 for (BlockedContents::iterator i(blocked_contents_.begin()); |
| 60 i != blocked_contents_.end(); ++i) { | 66 i != blocked_contents_.end(); ++i) { |
| 61 if (i->tab_contents == tab_contents) { | 67 if (i->tab_contents == tab_contents) { |
| 62 // To support the owner blocking the content again we copy and erase | 68 // To support the owner blocking the content again we copy and erase |
| 63 // before attempting to add. | 69 // before attempting to add. |
| 64 BlockedContent content(*i); | 70 BlockedContent content(*i); |
| 65 blocked_contents_.erase(i); | 71 blocked_contents_.erase(i); |
| 66 i = blocked_contents_.end(); | 72 i = blocked_contents_.end(); |
| 67 tab_contents->set_delegate(NULL); | 73 // TODO(avi): remove once TabContentsDelegate::GetConstrainingContents |
| 74 // goes away. |
| 75 tab_contents->tab_contents()->set_delegate(NULL); |
| 76 tab_contents->content_settings_tab_helper()->set_delegate(NULL); |
| 68 // We needn't call WasRestored to change its status because the | 77 // We needn't call WasRestored to change its status because the |
| 69 // TabContents::AddNewContents will do it. | 78 // TabContents::AddNewContents will do it. |
| 70 owner_->AddNewContents(tab_contents, content.disposition, content.bounds, | 79 owner_->tab_contents()->AddNewContents(tab_contents->tab_contents(), |
| 71 content.user_gesture); | 80 content.disposition, |
| 81 content.bounds, |
| 82 content.user_gesture); |
| 72 break; | 83 break; |
| 73 } | 84 } |
| 74 } | 85 } |
| 75 | |
| 76 if (blocked_contents_.empty()) | |
| 77 Destroy(); | |
| 78 } | 86 } |
| 79 | 87 |
| 80 size_t BlockedContentContainer::GetBlockedContentsCount() const { | 88 size_t BlockedContentContainer::GetBlockedContentsCount() const { |
| 81 return blocked_contents_.size(); | 89 return blocked_contents_.size(); |
| 82 } | 90 } |
| 83 | 91 |
| 84 void BlockedContentContainer::GetBlockedContents( | 92 void BlockedContentContainer::GetBlockedContents( |
| 85 std::vector<TabContents*>* blocked_contents) const { | 93 std::vector<TabContentsWrapper*>* blocked_contents) const { |
| 86 DCHECK(blocked_contents); | 94 DCHECK(blocked_contents); |
| 87 for (BlockedContents::const_iterator i(blocked_contents_.begin()); | 95 for (BlockedContents::const_iterator i(blocked_contents_.begin()); |
| 88 i != blocked_contents_.end(); ++i) | 96 i != blocked_contents_.end(); ++i) |
| 89 blocked_contents->push_back(i->tab_contents); | 97 blocked_contents->push_back(i->tab_contents); |
| 90 } | 98 } |
| 91 | 99 |
| 92 void BlockedContentContainer::Destroy() { | 100 void BlockedContentContainer::Clear() { |
| 93 for (BlockedContents::iterator i(blocked_contents_.begin()); | 101 for (BlockedContents::iterator i(blocked_contents_.begin()); |
| 94 i != blocked_contents_.end(); ++i) { | 102 i != blocked_contents_.end(); ++i) { |
| 95 TabContents* tab_contents = i->tab_contents; | 103 TabContentsWrapper* tab_contents = i->tab_contents; |
| 96 tab_contents->set_delegate(NULL); | 104 // TODO(avi): remove once TabContentsDelegate::GetConstrainingContents goes |
| 105 // away. |
| 106 tab_contents->tab_contents()->set_delegate(NULL); |
| 107 tab_contents->content_settings_tab_helper()->set_delegate(NULL); |
| 97 delete tab_contents; | 108 delete tab_contents; |
| 98 } | 109 } |
| 99 blocked_contents_.clear(); | 110 blocked_contents_.clear(); |
| 100 owner_->WillCloseBlockedContentContainer(this); | |
| 101 delete this; | |
| 102 } | 111 } |
| 103 | 112 |
| 104 // Overridden from TabContentsDelegate: | 113 // Overridden from TabContentsDelegate: |
| 105 void BlockedContentContainer::OpenURLFromTab(TabContents* source, | 114 void BlockedContentContainer::OpenURLFromTab(TabContents* source, |
| 106 const GURL& url, | 115 const GURL& url, |
| 107 const GURL& referrer, | 116 const GURL& referrer, |
| 108 WindowOpenDisposition disposition, | 117 WindowOpenDisposition disposition, |
| 109 PageTransition::Type transition) { | 118 PageTransition::Type transition) { |
| 110 owner_->OpenURL(url, referrer, disposition, transition); | 119 owner_->tab_contents()->OpenURL(url, referrer, disposition, transition); |
| 111 } | 120 } |
| 112 | 121 |
| 113 void BlockedContentContainer::AddNewContents(TabContents* source, | 122 void BlockedContentContainer::AddNewContents(TabContents* source, |
| 114 TabContents* new_contents, | 123 TabContents* new_contents, |
| 115 WindowOpenDisposition disposition, | 124 WindowOpenDisposition disposition, |
| 116 const gfx::Rect& initial_position, | 125 const gfx::Rect& initial_position, |
| 117 bool user_gesture) { | 126 bool user_gesture) { |
| 118 owner_->AddNewContents(new_contents, disposition, initial_position, | 127 owner_->tab_contents()->AddNewContents( |
| 119 user_gesture); | 128 new_contents, disposition, initial_position, user_gesture); |
| 120 } | 129 } |
| 121 | 130 |
| 122 void BlockedContentContainer::CloseContents(TabContents* source) { | 131 void BlockedContentContainer::CloseContents(TabContents* source) { |
| 123 for (BlockedContents::iterator i(blocked_contents_.begin()); | 132 for (BlockedContents::iterator i(blocked_contents_.begin()); |
| 124 i != blocked_contents_.end(); ++i) { | 133 i != blocked_contents_.end(); ++i) { |
| 125 TabContents* tab_contents = i->tab_contents; | 134 TabContentsWrapper* tab_contents = i->tab_contents; |
| 126 if (tab_contents == source) { | 135 if (tab_contents->tab_contents() == source) { |
| 127 tab_contents->set_delegate(NULL); | 136 // TODO(avi): remove once TabContentsDelegate::GetConstrainingContents |
| 137 // goes away. |
| 138 tab_contents->tab_contents()->set_delegate(NULL); |
| 139 tab_contents->content_settings_tab_helper()->set_delegate(NULL); |
| 128 blocked_contents_.erase(i); | 140 blocked_contents_.erase(i); |
| 129 delete tab_contents; | 141 delete tab_contents; |
| 130 break; | 142 break; |
| 131 } | 143 } |
| 132 } | 144 } |
| 133 } | 145 } |
| 134 | 146 |
| 135 void BlockedContentContainer::MoveContents(TabContents* source, | 147 void BlockedContentContainer::MoveContents(TabContents* source, |
| 136 const gfx::Rect& new_bounds) { | 148 const gfx::Rect& new_bounds) { |
| 137 for (BlockedContents::iterator i(blocked_contents_.begin()); | 149 for (BlockedContents::iterator i(blocked_contents_.begin()); |
| 138 i != blocked_contents_.end(); ++i) { | 150 i != blocked_contents_.end(); ++i) { |
| 139 if (i->tab_contents == source) { | 151 if (i->tab_contents->tab_contents() == source) { |
| 140 i->bounds = new_bounds; | 152 i->bounds = new_bounds; |
| 141 break; | 153 break; |
| 142 } | 154 } |
| 143 } | 155 } |
| 144 } | 156 } |
| 145 | 157 |
| 146 bool BlockedContentContainer::IsPopup(const TabContents* source) const { | 158 bool BlockedContentContainer::IsPopup(const TabContents* source) const { |
| 147 // Assume everything added is a popup. This may turn out to be wrong, but | 159 // Assume everything added is a popup. This may turn out to be wrong, but |
| 148 // callers don't cache this information so it should be fine if the value ends | 160 // callers don't cache this information so it should be fine if the value ends |
| 149 // up changing. | 161 // up changing. |
| 150 return true; | 162 return true; |
| 151 } | 163 } |
| 152 | 164 |
| 153 TabContents* BlockedContentContainer::GetConstrainingContents( | 165 TabContents* BlockedContentContainer::GetConstrainingContents( |
| 154 TabContents* source) { | 166 TabContents* source) { |
| 167 return owner_->tab_contents(); |
| 168 } |
| 169 |
| 170 TabContentsWrapper* BlockedContentContainer::GetConstrainingContents( |
| 171 TabContentsWrapper* source) { |
| 155 return owner_; | 172 return owner_; |
| 156 } | 173 } |
| OLD | NEW |