Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_manager.h" | 5 #include "chrome/browser/infobars/infobar_manager.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "chrome/browser/infobars/infobar.h" | 8 #include "chrome/browser/infobars/infobar.h" |
| 9 #include "chrome/common/chrome_switches.h" | 9 #include "chrome/common/chrome_switches.h" |
| 10 | 10 |
| 11 InfoBar* InfoBarManager::AddInfoBar(scoped_ptr<InfoBar> infobar) { | 11 InfoBar* InfoBarManager::AddInfoBar(scoped_ptr<InfoBar> infobar) { |
| 12 DCHECK(infobar); | 12 DCHECK(infobar); |
| 13 if (!infobars_enabled_) | 13 if (!infobars_enabled_) |
| 14 return NULL; | 14 return NULL; |
| 15 | 15 |
| 16 for (InfoBars::const_iterator i(infobars_.begin()); i != infobars_.end(); | 16 for (InfoBars::const_iterator i(infobars_.begin()); i != infobars_.end(); |
| 17 ++i) { | 17 ++i) { |
| 18 if ((*i)->delegate()->EqualsDelegate(infobar->delegate())) { | 18 if ((*i)->delegate()->EqualsDelegate(infobar->delegate())) { |
| 19 DCHECK_NE((*i)->delegate(), infobar->delegate()); | 19 DCHECK_NE((*i)->delegate(), infobar->delegate()); |
| 20 return NULL; | 20 return NULL; |
| 21 } | 21 } |
| 22 } | 22 } |
| 23 | 23 |
| 24 InfoBar* infobar_ptr = infobar.release(); | 24 InfoBar* infobar_ptr = infobar.release(); |
| 25 infobars_.push_back(infobar_ptr); | 25 infobars_.push_back(infobar_ptr); |
| 26 infobar_ptr->SetOwner(this); | 26 infobar_ptr->SetOwner(this); |
| 27 | 27 |
| 28 FOR_EACH_OBSERVER(Observer, observer_list_, OnInfoBarAdded(infobar_ptr)); | 28 NotifyInfoBarAdded(infobar_ptr); |
| 29 | 29 |
| 30 return infobar_ptr; | 30 return infobar_ptr; |
| 31 } | 31 } |
| 32 | 32 |
| 33 void InfoBarManager::RemoveInfoBar(InfoBar* infobar) { | 33 void InfoBarManager::RemoveInfoBar(InfoBar* infobar) { |
| 34 RemoveInfoBarInternal(infobar, true); | 34 RemoveInfoBarInternal(infobar, true); |
| 35 } | 35 } |
| 36 | 36 |
| 37 void InfoBarManager::RemoveAllInfoBars(bool animate) { | 37 void InfoBarManager::RemoveAllInfoBars(bool animate) { |
| 38 while (!infobars_.empty()) | 38 while (!infobars_.empty()) |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 51 DCHECK(i != infobars_.end()); | 51 DCHECK(i != infobars_.end()); |
| 52 | 52 |
| 53 InfoBar* new_infobar_ptr = new_infobar.release(); | 53 InfoBar* new_infobar_ptr = new_infobar.release(); |
| 54 i = infobars_.insert(i, new_infobar_ptr); | 54 i = infobars_.insert(i, new_infobar_ptr); |
| 55 new_infobar_ptr->SetOwner(this); | 55 new_infobar_ptr->SetOwner(this); |
| 56 | 56 |
| 57 // Remove the old infobar before notifying, so that if any observers call back | 57 // Remove the old infobar before notifying, so that if any observers call back |
| 58 // to AddInfoBar() or similar, we don't dupe-check against this infobar. | 58 // to AddInfoBar() or similar, we don't dupe-check against this infobar. |
| 59 infobars_.erase(++i); | 59 infobars_.erase(++i); |
| 60 | 60 |
| 61 FOR_EACH_OBSERVER(Observer, | 61 NotifyInfoBarReplaced(old_infobar, new_infobar_ptr); |
| 62 observer_list_, | |
| 63 OnInfoBarReplaced(old_infobar, new_infobar_ptr)); | |
| 64 | 62 |
| 65 old_infobar->CloseSoon(); | 63 old_infobar->CloseSoon(); |
| 66 return new_infobar_ptr; | 64 return new_infobar_ptr; |
| 67 } | 65 } |
| 68 | 66 |
| 69 void InfoBarManager::AddObserver(Observer* obs) { | 67 void InfoBarManager::AddObserver(Observer* obs) { |
| 70 observer_list_.AddObserver(obs); | 68 observer_list_.AddObserver(obs); |
| 71 } | 69 } |
| 72 | 70 |
| 73 void InfoBarManager::RemoveObserver(Observer* obs) { | 71 void InfoBarManager::RemoveObserver(Observer* obs) { |
| 74 observer_list_.RemoveObserver(obs); | 72 observer_list_.RemoveObserver(obs); |
| 75 } | 73 } |
| 76 | 74 |
| 77 InfoBarManager::InfoBarManager(content::WebContents* web_contents) | 75 InfoBarManager::InfoBarManager(content::WebContents* web_contents) |
| 78 : infobars_enabled_(true), | 76 : infobars_enabled_(true), |
| 77 did_shut_down_(false), | |
|
Peter Kasting
2014/04/08 20:39:56
Nit: Don't add members just for sanity-check purpo
| |
| 79 web_contents_(web_contents) { | 78 web_contents_(web_contents) { |
| 80 DCHECK(web_contents); | 79 DCHECK(web_contents); |
| 81 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableInfoBars)) | 80 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableInfoBars)) |
| 82 infobars_enabled_ = false; | 81 infobars_enabled_ = false; |
| 83 } | 82 } |
| 84 | 83 |
| 85 InfoBarManager::~InfoBarManager() { | 84 InfoBarManager::~InfoBarManager() { |
| 85 DCHECK(did_shut_down_); | |
| 86 } | |
| 87 | |
| 88 void InfoBarManager::ShutDown() { | |
| 86 // Destroy all remaining InfoBars. It's important to not animate here so that | 89 // Destroy all remaining InfoBars. It's important to not animate here so that |
| 87 // we guarantee that we'll delete all delegates before we do anything else. | 90 // we guarantee that we'll delete all delegates before we do anything else. |
| 88 RemoveAllInfoBars(false); | 91 RemoveAllInfoBars(false); |
| 89 FOR_EACH_OBSERVER(Observer, observer_list_, OnManagerShuttingDown(this)); | 92 NotifyManagerShuttingDown(); |
| 93 did_shut_down_ = true; | |
| 90 } | 94 } |
| 91 | 95 |
| 92 void InfoBarManager::OnNavigation( | 96 void InfoBarManager::OnNavigation( |
| 93 const InfoBarDelegate::NavigationDetails& details) { | 97 const InfoBarDelegate::NavigationDetails& details) { |
| 94 // NOTE: It is not safe to change the following code to count upwards or | 98 // NOTE: It is not safe to change the following code to count upwards or |
| 95 // use iterators, as the RemoveInfoBar() call synchronously modifies our | 99 // use iterators, as the RemoveInfoBar() call synchronously modifies our |
| 96 // delegate list. | 100 // delegate list. |
| 97 for (size_t i = infobars_.size(); i > 0; --i) { | 101 for (size_t i = infobars_.size(); i > 0; --i) { |
| 98 InfoBar* infobar = infobars_[i - 1]; | 102 InfoBar* infobar = infobars_[i - 1]; |
| 99 if (infobar->delegate()->ShouldExpire(details)) | 103 if (infobar->delegate()->ShouldExpire(details)) |
| 100 RemoveInfoBar(infobar); | 104 RemoveInfoBar(infobar); |
| 101 } | 105 } |
| 102 } | 106 } |
| 103 | 107 |
| 104 void InfoBarManager::OnWebContentsDestroyed() { web_contents_ = NULL; } | 108 void InfoBarManager::OnWebContentsDestroyed() { web_contents_ = NULL; } |
| 105 | 109 |
| 110 void InfoBarManager::NotifyInfoBarAdded(InfoBar* infobar) { | |
| 111 FOR_EACH_OBSERVER(Observer, observer_list_, OnInfoBarAdded(infobar)); | |
| 112 } | |
| 113 | |
| 114 void InfoBarManager::NotifyInfoBarRemoved(InfoBar* infobar, bool animate) { | |
| 115 FOR_EACH_OBSERVER(Observer, observer_list_, | |
| 116 OnInfoBarRemoved(infobar, animate)); | |
| 117 } | |
| 118 | |
| 119 void InfoBarManager::NotifyInfoBarReplaced(InfoBar* old_infobar, | |
| 120 InfoBar* new_infobar) { | |
| 121 FOR_EACH_OBSERVER(Observer, | |
| 122 observer_list_, | |
| 123 OnInfoBarReplaced(old_infobar, new_infobar)); | |
| 124 } | |
| 125 | |
| 126 void InfoBarManager::NotifyManagerShuttingDown() { | |
| 127 FOR_EACH_OBSERVER(Observer, observer_list_, OnManagerShuttingDown(this)); | |
| 128 } | |
| 129 | |
| 106 void InfoBarManager::RemoveInfoBarInternal(InfoBar* infobar, bool animate) { | 130 void InfoBarManager::RemoveInfoBarInternal(InfoBar* infobar, bool animate) { |
| 107 DCHECK(infobar); | 131 DCHECK(infobar); |
| 108 if (!infobars_enabled_) { | 132 if (!infobars_enabled_) { |
| 109 DCHECK(infobars_.empty()); | 133 DCHECK(infobars_.empty()); |
| 110 return; | 134 return; |
| 111 } | 135 } |
| 112 | 136 |
| 113 InfoBars::iterator i(std::find(infobars_.begin(), infobars_.end(), infobar)); | 137 InfoBars::iterator i(std::find(infobars_.begin(), infobars_.end(), infobar)); |
| 114 DCHECK(i != infobars_.end()); | 138 DCHECK(i != infobars_.end()); |
| 115 | 139 |
| 116 // Remove the infobar before notifying, so that if any observers call back to | 140 // Remove the infobar before notifying, so that if any observers call back to |
| 117 // AddInfoBar() or similar, we don't dupe-check against this infobar. | 141 // AddInfoBar() or similar, we don't dupe-check against this infobar. |
| 118 infobars_.erase(i); | 142 infobars_.erase(i); |
| 119 | 143 |
| 120 // This notification must happen before the call to CloseSoon() below, since | 144 // This notification must happen before the call to CloseSoon() below, since |
| 121 // observers may want to access |infobar| and that call can delete it. | 145 // observers may want to access |infobar| and that call can delete it. |
| 122 FOR_EACH_OBSERVER(Observer, observer_list_, | 146 NotifyInfoBarRemoved(infobar, animate); |
| 123 OnInfoBarRemoved(infobar, animate)); | |
| 124 | 147 |
| 125 infobar->CloseSoon(); | 148 infobar->CloseSoon(); |
| 126 } | 149 } |
| OLD | NEW |