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() |
78 : infobars_enabled_(true), | 76 : infobars_enabled_(true) { |
79 web_contents_(web_contents) { | |
80 DCHECK(web_contents); | |
81 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableInfoBars)) | 77 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableInfoBars)) |
82 infobars_enabled_ = false; | 78 infobars_enabled_ = false; |
83 } | 79 } |
84 | 80 |
85 InfoBarManager::~InfoBarManager() { | 81 InfoBarManager::~InfoBarManager() {} |
| 82 |
| 83 void InfoBarManager::ShutDown() { |
86 // Destroy all remaining InfoBars. It's important to not animate here so that | 84 // 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. | 85 // we guarantee that we'll delete all delegates before we do anything else. |
88 RemoveAllInfoBars(false); | 86 RemoveAllInfoBars(false); |
89 FOR_EACH_OBSERVER(Observer, observer_list_, OnManagerShuttingDown(this)); | 87 NotifyManagerShuttingDown(); |
90 } | 88 } |
91 | 89 |
92 void InfoBarManager::OnNavigation( | 90 void InfoBarManager::OnNavigation( |
93 const InfoBarDelegate::NavigationDetails& details) { | 91 const InfoBarDelegate::NavigationDetails& details) { |
94 // NOTE: It is not safe to change the following code to count upwards or | 92 // NOTE: It is not safe to change the following code to count upwards or |
95 // use iterators, as the RemoveInfoBar() call synchronously modifies our | 93 // use iterators, as the RemoveInfoBar() call synchronously modifies our |
96 // delegate list. | 94 // delegate list. |
97 for (size_t i = infobars_.size(); i > 0; --i) { | 95 for (size_t i = infobars_.size(); i > 0; --i) { |
98 InfoBar* infobar = infobars_[i - 1]; | 96 InfoBar* infobar = infobars_[i - 1]; |
99 if (infobar->delegate()->ShouldExpire(details)) | 97 if (infobar->delegate()->ShouldExpire(details)) |
100 RemoveInfoBar(infobar); | 98 RemoveInfoBar(infobar); |
101 } | 99 } |
102 } | 100 } |
103 | 101 |
104 void InfoBarManager::OnWebContentsDestroyed() { web_contents_ = NULL; } | 102 void InfoBarManager::NotifyInfoBarAdded(InfoBar* infobar) { |
| 103 FOR_EACH_OBSERVER(Observer, observer_list_, OnInfoBarAdded(infobar)); |
| 104 } |
| 105 |
| 106 void InfoBarManager::NotifyInfoBarRemoved(InfoBar* infobar, bool animate) { |
| 107 FOR_EACH_OBSERVER(Observer, observer_list_, |
| 108 OnInfoBarRemoved(infobar, animate)); |
| 109 } |
| 110 |
| 111 void InfoBarManager::NotifyInfoBarReplaced(InfoBar* old_infobar, |
| 112 InfoBar* new_infobar) { |
| 113 FOR_EACH_OBSERVER(Observer, |
| 114 observer_list_, |
| 115 OnInfoBarReplaced(old_infobar, new_infobar)); |
| 116 } |
| 117 |
| 118 void InfoBarManager::NotifyManagerShuttingDown() { |
| 119 FOR_EACH_OBSERVER(Observer, observer_list_, OnManagerShuttingDown(this)); |
| 120 } |
105 | 121 |
106 void InfoBarManager::RemoveInfoBarInternal(InfoBar* infobar, bool animate) { | 122 void InfoBarManager::RemoveInfoBarInternal(InfoBar* infobar, bool animate) { |
107 DCHECK(infobar); | 123 DCHECK(infobar); |
108 if (!infobars_enabled_) { | 124 if (!infobars_enabled_) { |
109 DCHECK(infobars_.empty()); | 125 DCHECK(infobars_.empty()); |
110 return; | 126 return; |
111 } | 127 } |
112 | 128 |
113 InfoBars::iterator i(std::find(infobars_.begin(), infobars_.end(), infobar)); | 129 InfoBars::iterator i(std::find(infobars_.begin(), infobars_.end(), infobar)); |
114 DCHECK(i != infobars_.end()); | 130 DCHECK(i != infobars_.end()); |
115 | 131 |
116 // Remove the infobar before notifying, so that if any observers call back to | 132 // 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. | 133 // AddInfoBar() or similar, we don't dupe-check against this infobar. |
118 infobars_.erase(i); | 134 infobars_.erase(i); |
119 | 135 |
120 // This notification must happen before the call to CloseSoon() below, since | 136 // This notification must happen before the call to CloseSoon() below, since |
121 // observers may want to access |infobar| and that call can delete it. | 137 // observers may want to access |infobar| and that call can delete it. |
122 FOR_EACH_OBSERVER(Observer, observer_list_, | 138 NotifyInfoBarRemoved(infobar, animate); |
123 OnInfoBarRemoved(infobar, animate)); | |
124 | 139 |
125 infobar->CloseSoon(); | 140 infobar->CloseSoon(); |
126 } | 141 } |
OLD | NEW |