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