| 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/infobars/infobar_tab_helper.h" | 5 #include "chrome/browser/infobars/infobar_tab_helper.h" | 
| 6 | 6 | 
| 7 #include "chrome/browser/api/infobars/infobar_delegate.h" | 7 #include "chrome/browser/api/infobars/infobar_delegate.h" | 
| 8 #include "chrome/browser/infobars/infobar.h" | 8 #include "chrome/browser/infobars/infobar.h" | 
| 9 #include "chrome/browser/infobars/insecure_content_infobar_delegate.h" | 9 #include "chrome/browser/infobars/insecure_content_infobar_delegate.h" | 
| 10 #include "chrome/common/chrome_notification_types.h" | 10 #include "chrome/common/chrome_notification_types.h" | 
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 53   // TODO(pkasting): If there is no InfoBarContainer, this leaks all the | 53   // TODO(pkasting): If there is no InfoBarContainer, this leaks all the | 
| 54   // InfoBarDelegates.  This will be fixed once we call CloseSoon() directly on | 54   // InfoBarDelegates.  This will be fixed once we call CloseSoon() directly on | 
| 55   // Infobars. | 55   // Infobars. | 
| 56   RemoveAllInfoBars(false); | 56   RemoveAllInfoBars(false); | 
| 57 } | 57 } | 
| 58 | 58 | 
| 59 void InfoBarTabHelper::SetInfoBarsEnabled(bool enabled) { | 59 void InfoBarTabHelper::SetInfoBarsEnabled(bool enabled) { | 
| 60   infobars_enabled_ = enabled; | 60   infobars_enabled_ = enabled; | 
| 61 } | 61 } | 
| 62 | 62 | 
| 63 bool InfoBarTabHelper::AddInfoBar(InfoBarDelegate* delegate) { | 63 InfoBarDelegate* InfoBarTabHelper::AddInfoBar( | 
| 64   if (!infobars_enabled_) { | 64     scoped_ptr<InfoBarDelegate> delegate) { | 
| 65     delegate->InfoBarClosed(); | 65   if (!infobars_enabled_) | 
| 66     return false; | 66     return NULL; | 
| 67   } |  | 
| 68 | 67 | 
| 69   for (InfoBars::const_iterator i(infobars_.begin()); i != infobars_.end(); | 68   for (InfoBars::const_iterator i(infobars_.begin()); i != infobars_.end(); | 
| 70        ++i) { | 69        ++i) { | 
| 71     if ((*i)->EqualsDelegate(delegate)) { | 70     if ((*i)->EqualsDelegate(delegate.get())) { | 
| 72       DCHECK_NE(*i, delegate); | 71       DCHECK_NE(*i, delegate.get()); | 
| 73       delegate->InfoBarClosed(); | 72       return NULL; | 
| 74       return false; |  | 
| 75     } | 73     } | 
| 76   } | 74   } | 
| 77 | 75 | 
| 78   // TODO(pkasting): Consider removing InfoBarTabHelper arg from delegate | 76   // TODO(pkasting): Consider removing InfoBarTabHelper arg from delegate | 
| 79   // constructors and instead using a setter from here. | 77   // constructors and instead using a setter from here. | 
| 80   infobars_.push_back(delegate); | 78   InfoBarDelegate* delegate_ptr = delegate.release(); | 
|  | 79   infobars_.push_back(delegate_ptr); | 
| 81   // Add ourselves as an observer for navigations the first time a delegate is | 80   // Add ourselves as an observer for navigations the first time a delegate is | 
| 82   // added. We use this notification to expire InfoBars that need to expire on | 81   // added. We use this notification to expire InfoBars that need to expire on | 
| 83   // page transitions. | 82   // page transitions. | 
| 84   if (infobars_.size() == 1) { | 83   if (infobars_.size() == 1) { | 
| 85     registrar_.Add( | 84     registrar_.Add( | 
| 86         this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 85         this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 
| 87         content::Source<NavigationController>( | 86         content::Source<NavigationController>( | 
| 88             &web_contents()->GetController())); | 87             &web_contents()->GetController())); | 
| 89   } | 88   } | 
| 90 | 89 | 
| 91   content::NotificationService::current()->Notify( | 90   content::NotificationService::current()->Notify( | 
| 92       chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, | 91       chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, | 
| 93       content::Source<InfoBarService>(this), | 92       content::Source<InfoBarService>(this), | 
| 94       content::Details<InfoBarAddedDetails>(delegate)); | 93       content::Details<InfoBarAddedDetails>(delegate_ptr)); | 
| 95   return true; | 94   return delegate_ptr; | 
| 96 } | 95 } | 
| 97 | 96 | 
| 98 void InfoBarTabHelper::RemoveInfoBar(InfoBarDelegate* delegate) { | 97 void InfoBarTabHelper::RemoveInfoBar(InfoBarDelegate* delegate) { | 
| 99   RemoveInfoBarInternal(delegate, true); | 98   RemoveInfoBarInternal(delegate, true); | 
| 100 } | 99 } | 
| 101 | 100 | 
| 102 bool InfoBarTabHelper::ReplaceInfoBar(InfoBarDelegate* old_delegate, | 101 InfoBarDelegate* InfoBarTabHelper::ReplaceInfoBar( | 
| 103                                       InfoBarDelegate* new_delegate) { | 102     InfoBarDelegate* old_delegate, | 
|  | 103     scoped_ptr<InfoBarDelegate> new_delegate) { | 
| 104   if (!infobars_enabled_) | 104   if (!infobars_enabled_) | 
| 105     return AddInfoBar(new_delegate);  // Deletes the delegate. | 105     return AddInfoBar(new_delegate.Pass());  // Deletes the delegate. | 
| 106 | 106 | 
| 107   InfoBars::iterator i(std::find(infobars_.begin(), infobars_.end(), | 107   InfoBars::iterator i(std::find(infobars_.begin(), infobars_.end(), | 
| 108                                  old_delegate)); | 108                                  old_delegate)); | 
| 109   DCHECK(i != infobars_.end()); | 109   DCHECK(i != infobars_.end()); | 
| 110 | 110 | 
| 111   i = infobars_.insert(i, new_delegate) + 1; | 111   InfoBarDelegate* new_delegate_ptr = new_delegate.release(); | 
|  | 112   i = infobars_.insert(i, new_delegate_ptr); | 
|  | 113   InfoBarReplacedDetails replaced_details(old_delegate, new_delegate_ptr); | 
| 112   // Remove the old delegate before notifying, so that if any observers call | 114   // Remove the old delegate before notifying, so that if any observers call | 
| 113   // back to AddInfoBar() or similar, we don't dupe-check against this delegate. | 115   // back to AddInfoBar() or similar, we don't dupe-check against this delegate. | 
| 114   infobars_.erase(i); | 116   infobars_.erase(++i); | 
| 115 | 117 | 
| 116   old_delegate->clear_owner(); | 118   old_delegate->clear_owner(); | 
| 117   InfoBarReplacedDetails replaced_details(old_delegate, new_delegate); |  | 
| 118   content::NotificationService::current()->Notify( | 119   content::NotificationService::current()->Notify( | 
| 119       chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED, | 120       chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED, | 
| 120       content::Source<InfoBarService>(this), | 121       content::Source<InfoBarService>(this), | 
| 121       content::Details<InfoBarReplacedDetails>(&replaced_details)); | 122       content::Details<InfoBarReplacedDetails>(&replaced_details)); | 
| 122   return true; | 123   return new_delegate_ptr; | 
| 123 } | 124 } | 
| 124 | 125 | 
| 125 size_t InfoBarTabHelper::GetInfoBarCount() const { | 126 size_t InfoBarTabHelper::GetInfoBarCount() const { | 
| 126   return infobars_.size(); | 127   return infobars_.size(); | 
| 127 } | 128 } | 
| 128 | 129 | 
| 129 InfoBarDelegate* InfoBarTabHelper::GetInfoBarDelegateAt(size_t index) { | 130 InfoBarDelegate* InfoBarTabHelper::GetInfoBarDelegateAt(size_t index) { | 
| 130   return infobars_[index]; | 131   return infobars_[index]; | 
| 131 } | 132 } | 
| 132 | 133 | 
| (...skipping 29 matching lines...) Expand all  Loading... | 
| 162       content::Source<InfoBarService>(this), | 163       content::Source<InfoBarService>(this), | 
| 163       content::Details<InfoBarRemovedDetails>(&removed_details)); | 164       content::Details<InfoBarRemovedDetails>(&removed_details)); | 
| 164 } | 165 } | 
| 165 | 166 | 
| 166 void InfoBarTabHelper::RemoveAllInfoBars(bool animate) { | 167 void InfoBarTabHelper::RemoveAllInfoBars(bool animate) { | 
| 167   while (!infobars_.empty()) | 168   while (!infobars_.empty()) | 
| 168     RemoveInfoBarInternal(GetInfoBarDelegateAt(GetInfoBarCount() - 1), animate); | 169     RemoveInfoBarInternal(GetInfoBarDelegateAt(GetInfoBarCount() - 1), animate); | 
| 169 } | 170 } | 
| 170 | 171 | 
| 171 void InfoBarTabHelper::OnDidBlockDisplayingInsecureContent() { | 172 void InfoBarTabHelper::OnDidBlockDisplayingInsecureContent() { | 
| 172   // At most one infobar and do not supersede the stronger running content bar. | 173   InsecureContentInfoBarDelegate::Create( | 
| 173   for (size_t i = 0; i < infobars_.size(); ++i) { | 174       this, InsecureContentInfoBarDelegate::DISPLAY); | 
| 174     if (GetInfoBarDelegateAt(i)->AsInsecureContentInfoBarDelegate()) |  | 
| 175       return; |  | 
| 176   } |  | 
| 177   AddInfoBar(new InsecureContentInfoBarDelegate(this, |  | 
| 178       InsecureContentInfoBarDelegate::DISPLAY)); |  | 
| 179 } | 175 } | 
| 180 | 176 | 
| 181 void InfoBarTabHelper::OnDidBlockRunningInsecureContent() { | 177 void InfoBarTabHelper::OnDidBlockRunningInsecureContent() { | 
| 182   // At most one infobar superseding any weaker displaying content bar. | 178   InsecureContentInfoBarDelegate::Create(this, | 
| 183   for (size_t i = 0; i < infobars_.size(); ++i) { | 179                                          InsecureContentInfoBarDelegate::RUN); | 
| 184     InsecureContentInfoBarDelegate* delegate = |  | 
| 185         GetInfoBarDelegateAt(i)->AsInsecureContentInfoBarDelegate(); |  | 
| 186     if (delegate) { |  | 
| 187       if (delegate->type() != InsecureContentInfoBarDelegate::RUN) { |  | 
| 188         ReplaceInfoBar(delegate, new InsecureContentInfoBarDelegate( |  | 
| 189             this, InsecureContentInfoBarDelegate::RUN)); |  | 
| 190       } |  | 
| 191       return; |  | 
| 192     } |  | 
| 193   } |  | 
| 194   AddInfoBar(new InsecureContentInfoBarDelegate(this, |  | 
| 195       InsecureContentInfoBarDelegate::RUN)); |  | 
| 196 } | 180 } | 
| 197 | 181 | 
| 198 void InfoBarTabHelper::RenderViewGone(base::TerminationStatus status) { | 182 void InfoBarTabHelper::RenderViewGone(base::TerminationStatus status) { | 
| 199   RemoveAllInfoBars(true); | 183   RemoveAllInfoBars(true); | 
| 200 } | 184 } | 
| 201 | 185 | 
| 202 bool InfoBarTabHelper::OnMessageReceived(const IPC::Message& message) { | 186 bool InfoBarTabHelper::OnMessageReceived(const IPC::Message& message) { | 
| 203   bool handled = true; | 187   bool handled = true; | 
| 204   IPC_BEGIN_MESSAGE_MAP(InfoBarTabHelper, message) | 188   IPC_BEGIN_MESSAGE_MAP(InfoBarTabHelper, message) | 
| 205     IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockDisplayingInsecureContent, | 189     IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockDisplayingInsecureContent, | 
| (...skipping 30 matching lines...) Expand all  Loading... | 
| 236   DCHECK_EQ(type, content::NOTIFICATION_WEB_CONTENTS_DESTROYED); | 220   DCHECK_EQ(type, content::NOTIFICATION_WEB_CONTENTS_DESTROYED); | 
| 237   // The WebContents is going away; be aggressively paranoid and delete | 221   // The WebContents is going away; be aggressively paranoid and delete | 
| 238   // ourselves lest other parts of the system attempt to add infobars or use | 222   // ourselves lest other parts of the system attempt to add infobars or use | 
| 239   // us otherwise during the destruction. | 223   // us otherwise during the destruction. | 
| 240   DCHECK_EQ(web_contents(), content::Source<WebContents>(source).ptr()); | 224   DCHECK_EQ(web_contents(), content::Source<WebContents>(source).ptr()); | 
| 241   web_contents()->RemoveUserData(UserDataKey()); | 225   web_contents()->RemoveUserData(UserDataKey()); | 
| 242   // That was the equivalent of "delete this". This object is now destroyed; | 226   // That was the equivalent of "delete this". This object is now destroyed; | 
| 243   // returning from this function is the only safe thing to do. | 227   // returning from this function is the only safe thing to do. | 
| 244   return; | 228   return; | 
| 245 } | 229 } | 
| OLD | NEW | 
|---|