OLD | NEW |
1 // Copyright (c) 2013 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_service.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/chrome_notification_types.h" | |
9 #include "chrome/browser/infobars/infobar.h" | 8 #include "chrome/browser/infobars/infobar.h" |
10 #include "chrome/browser/infobars/infobar_delegate.h" | 9 #include "chrome/browser/infobars/infobar_delegate.h" |
11 #include "chrome/browser/infobars/insecure_content_infobar_delegate.h" | |
12 #include "chrome/common/chrome_switches.h" | 10 #include "chrome/common/chrome_switches.h" |
13 #include "chrome/common/render_messages.h" | |
14 #include "content/public/browser/navigation_controller.h" | |
15 #include "content/public/browser/notification_service.h" | |
16 #include "content/public/browser/web_contents.h" | |
17 | 11 |
18 | 12 InfoBar* InfoBarManager::AddInfoBar(scoped_ptr<InfoBar> infobar) { |
19 DEFINE_WEB_CONTENTS_USER_DATA_KEY(InfoBarService); | |
20 | |
21 InfoBar* InfoBarService::AddInfoBar(scoped_ptr<InfoBar> infobar) { | |
22 DCHECK(infobar); | 13 DCHECK(infobar); |
23 if (!infobars_enabled_) | 14 if (!infobars_enabled_) |
24 return NULL; | 15 return NULL; |
25 | 16 |
26 for (InfoBars::const_iterator i(infobars_.begin()); i != infobars_.end(); | 17 for (InfoBars::const_iterator i(infobars_.begin()); i != infobars_.end(); |
27 ++i) { | 18 ++i) { |
28 if ((*i)->delegate()->EqualsDelegate(infobar->delegate())) { | 19 if ((*i)->delegate()->EqualsDelegate(infobar->delegate())) { |
29 DCHECK_NE((*i)->delegate(), infobar->delegate()); | 20 DCHECK_NE((*i)->delegate(), infobar->delegate()); |
30 return NULL; | 21 return NULL; |
31 } | 22 } |
32 } | 23 } |
33 | 24 |
34 InfoBar* infobar_ptr = infobar.release(); | 25 InfoBar* infobar_ptr = infobar.release(); |
35 infobars_.push_back(infobar_ptr); | 26 infobars_.push_back(infobar_ptr); |
36 infobar_ptr->SetOwner(this); | 27 infobar_ptr->SetOwner(this); |
37 | 28 |
38 FOR_EACH_OBSERVER(Observer, observer_list_, OnInfoBarAdded(infobar_ptr)); | 29 FOR_EACH_OBSERVER(Observer, observer_list_, OnInfoBarAdded(infobar_ptr)); |
39 // TODO(droger): Remove the notifications and use observers instead. | 30 |
40 // See http://crbug.com/354380 | |
41 content::NotificationService::current()->Notify( | |
42 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, | |
43 content::Source<InfoBarService>(this), | |
44 content::Details<InfoBar::AddedDetails>(infobar_ptr)); | |
45 return infobar_ptr; | 31 return infobar_ptr; |
46 } | 32 } |
47 | 33 |
48 void InfoBarService::RemoveInfoBar(InfoBar* infobar) { | 34 void InfoBarManager::RemoveInfoBar(InfoBar* infobar) { |
49 RemoveInfoBarInternal(infobar, true); | 35 RemoveInfoBarInternal(infobar, true); |
50 } | 36 } |
51 | 37 |
52 InfoBar* InfoBarService::ReplaceInfoBar(InfoBar* old_infobar, | 38 void InfoBarManager::RemoveAllInfoBars(bool animate) { |
| 39 while (!infobars_.empty()) |
| 40 RemoveInfoBarInternal(infobars_.back(), animate); |
| 41 } |
| 42 |
| 43 InfoBar* InfoBarManager::ReplaceInfoBar(InfoBar* old_infobar, |
53 scoped_ptr<InfoBar> new_infobar) { | 44 scoped_ptr<InfoBar> new_infobar) { |
54 DCHECK(old_infobar); | 45 DCHECK(old_infobar); |
55 if (!infobars_enabled_) | 46 if (!infobars_enabled_) |
56 return AddInfoBar(new_infobar.Pass()); // Deletes the infobar. | 47 return AddInfoBar(new_infobar.Pass()); // Deletes the infobar. |
57 DCHECK(new_infobar); | 48 DCHECK(new_infobar); |
58 | 49 |
59 InfoBars::iterator i(std::find(infobars_.begin(), infobars_.end(), | 50 InfoBars::iterator i(std::find(infobars_.begin(), infobars_.end(), |
60 old_infobar)); | 51 old_infobar)); |
61 DCHECK(i != infobars_.end()); | 52 DCHECK(i != infobars_.end()); |
62 | 53 |
63 InfoBar* new_infobar_ptr = new_infobar.release(); | 54 InfoBar* new_infobar_ptr = new_infobar.release(); |
64 i = infobars_.insert(i, new_infobar_ptr); | 55 i = infobars_.insert(i, new_infobar_ptr); |
65 new_infobar_ptr->SetOwner(this); | 56 new_infobar_ptr->SetOwner(this); |
66 InfoBar::ReplacedDetails replaced_details(old_infobar, new_infobar_ptr); | |
67 | 57 |
68 // Remove the old infobar before notifying, so that if any observers call back | 58 // Remove the old infobar before notifying, so that if any observers call back |
69 // to AddInfoBar() or similar, we don't dupe-check against this infobar. | 59 // to AddInfoBar() or similar, we don't dupe-check against this infobar. |
70 infobars_.erase(++i); | 60 infobars_.erase(++i); |
71 | 61 |
72 FOR_EACH_OBSERVER(Observer, | 62 FOR_EACH_OBSERVER(Observer, |
73 observer_list_, | 63 observer_list_, |
74 OnInfoBarReplaced(old_infobar, new_infobar_ptr)); | 64 OnInfoBarReplaced(old_infobar, new_infobar_ptr)); |
75 // TODO(droger): Remove the notifications and use observers instead. | |
76 // See http://crbug.com/354380 | |
77 content::NotificationService::current()->Notify( | |
78 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED, | |
79 content::Source<InfoBarService>(this), | |
80 content::Details<InfoBar::ReplacedDetails>(&replaced_details)); | |
81 | 65 |
82 old_infobar->CloseSoon(); | 66 old_infobar->CloseSoon(); |
83 return new_infobar_ptr; | 67 return new_infobar_ptr; |
84 } | 68 } |
85 | 69 |
86 void InfoBarService::AddObserver(Observer* obs) { | 70 void InfoBarManager::AddObserver(Observer* obs) { |
87 observer_list_.AddObserver(obs); | 71 observer_list_.AddObserver(obs); |
88 } | 72 } |
89 | 73 |
90 void InfoBarService::RemoveObserver(Observer* obs) { | 74 void InfoBarManager::RemoveObserver(Observer* obs) { |
91 observer_list_.RemoveObserver(obs); | 75 observer_list_.RemoveObserver(obs); |
92 } | 76 } |
93 | 77 |
94 InfoBarService::InfoBarService(content::WebContents* web_contents) | 78 InfoBarManager::InfoBarManager(content::WebContents* web_contents) |
95 : content::WebContentsObserver(web_contents), | 79 : infobars_enabled_(true), |
96 infobars_enabled_(true) { | 80 web_contents_(web_contents) { |
97 DCHECK(web_contents); | 81 DCHECK(web_contents); |
98 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableInfoBars)) | 82 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableInfoBars)) |
99 infobars_enabled_ = false; | 83 infobars_enabled_ = false; |
100 } | 84 } |
101 | 85 |
102 InfoBarService::~InfoBarService() { | 86 InfoBarManager::~InfoBarManager() { |
103 // Destroy all remaining InfoBars. It's important to not animate here so that | 87 // Destroy all remaining InfoBars. It's important to not animate here so that |
104 // we guarantee that we'll delete all delegates before we do anything else. | 88 // we guarantee that we'll delete all delegates before we do anything else. |
105 RemoveAllInfoBars(false); | 89 RemoveAllInfoBars(false); |
106 FOR_EACH_OBSERVER(Observer, observer_list_, OnServiceShuttingDown(this)); | 90 FOR_EACH_OBSERVER(Observer, observer_list_, OnManagerShuttingDown(this)); |
107 } | 91 } |
108 | 92 |
109 void InfoBarService::RenderProcessGone(base::TerminationStatus status) { | 93 void InfoBarManager::OnNavigation( |
110 RemoveAllInfoBars(true); | |
111 } | |
112 | |
113 void InfoBarService::NavigationEntryCommitted( | |
114 const content::LoadCommittedDetails& load_details) { | 94 const content::LoadCommittedDetails& load_details) { |
115 // NOTE: It is not safe to change the following code to count upwards or | 95 // NOTE: It is not safe to change the following code to count upwards or |
116 // use iterators, as the RemoveInfoBar() call synchronously modifies our | 96 // use iterators, as the RemoveInfoBar() call synchronously modifies our |
117 // delegate list. | 97 // delegate list. |
118 for (size_t i = infobars_.size(); i > 0; --i) { | 98 for (size_t i = infobars_.size(); i > 0; --i) { |
119 InfoBar* infobar = infobars_[i - 1]; | 99 InfoBar* infobar = infobars_[i - 1]; |
120 if (infobar->delegate()->ShouldExpire(load_details)) | 100 if (infobar->delegate()->ShouldExpire(load_details)) |
121 RemoveInfoBar(infobar); | 101 RemoveInfoBar(infobar); |
122 } | 102 } |
123 } | 103 } |
124 | 104 |
125 void InfoBarService::WebContentsDestroyed(content::WebContents* web_contents) { | 105 void InfoBarManager::OnWebContentsDestroyed() { web_contents_ = NULL; } |
126 // The WebContents is going away; be aggressively paranoid and delete | |
127 // ourselves lest other parts of the system attempt to add infobars or use | |
128 // us otherwise during the destruction. | |
129 web_contents->RemoveUserData(UserDataKey()); | |
130 // That was the equivalent of "delete this". This object is now destroyed; | |
131 // returning from this function is the only safe thing to do. | |
132 } | |
133 | 106 |
134 bool InfoBarService::OnMessageReceived(const IPC::Message& message) { | 107 void InfoBarManager::RemoveInfoBarInternal(InfoBar* infobar, bool animate) { |
135 bool handled = true; | |
136 IPC_BEGIN_MESSAGE_MAP(InfoBarService, message) | |
137 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockDisplayingInsecureContent, | |
138 OnDidBlockDisplayingInsecureContent) | |
139 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockRunningInsecureContent, | |
140 OnDidBlockRunningInsecureContent) | |
141 IPC_MESSAGE_UNHANDLED(handled = false) | |
142 IPC_END_MESSAGE_MAP() | |
143 return handled; | |
144 } | |
145 | |
146 void InfoBarService::RemoveInfoBarInternal(InfoBar* infobar, bool animate) { | |
147 DCHECK(infobar); | 108 DCHECK(infobar); |
148 if (!infobars_enabled_) { | 109 if (!infobars_enabled_) { |
149 DCHECK(infobars_.empty()); | 110 DCHECK(infobars_.empty()); |
150 return; | 111 return; |
151 } | 112 } |
152 | 113 |
153 InfoBars::iterator i(std::find(infobars_.begin(), infobars_.end(), infobar)); | 114 InfoBars::iterator i(std::find(infobars_.begin(), infobars_.end(), infobar)); |
154 DCHECK(i != infobars_.end()); | 115 DCHECK(i != infobars_.end()); |
155 | 116 |
156 // Remove the infobar before notifying, so that if any observers call back to | 117 // Remove the infobar before notifying, so that if any observers call back to |
157 // AddInfoBar() or similar, we don't dupe-check against this infobar. | 118 // AddInfoBar() or similar, we don't dupe-check against this infobar. |
158 infobars_.erase(i); | 119 infobars_.erase(i); |
159 | 120 |
160 // This notification must happen before the call to CloseSoon() below, since | 121 // This notification must happen before the call to CloseSoon() below, since |
161 // observers may want to access |infobar| and that call can delete it. | 122 // observers may want to access |infobar| and that call can delete it. |
162 FOR_EACH_OBSERVER(Observer, observer_list_, | 123 FOR_EACH_OBSERVER(Observer, observer_list_, |
163 OnInfoBarRemoved(infobar, animate)); | 124 OnInfoBarRemoved(infobar, animate)); |
164 // TODO(droger): Remove the notifications and use observers instead. | |
165 // See http://crbug.com/354380 | |
166 InfoBar::RemovedDetails removed_details(infobar, animate); | |
167 content::NotificationService::current()->Notify( | |
168 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, | |
169 content::Source<InfoBarService>(this), | |
170 content::Details<InfoBar::RemovedDetails>(&removed_details)); | |
171 | 125 |
172 infobar->CloseSoon(); | 126 infobar->CloseSoon(); |
173 } | 127 } |
174 | |
175 void InfoBarService::RemoveAllInfoBars(bool animate) { | |
176 while (!infobars_.empty()) | |
177 RemoveInfoBarInternal(infobars_.back(), animate); | |
178 } | |
179 | |
180 void InfoBarService::OnDidBlockDisplayingInsecureContent() { | |
181 InsecureContentInfoBarDelegate::Create( | |
182 this, InsecureContentInfoBarDelegate::DISPLAY); | |
183 } | |
184 | |
185 void InfoBarService::OnDidBlockRunningInsecureContent() { | |
186 InsecureContentInfoBarDelegate::Create(this, | |
187 InsecureContentInfoBarDelegate::RUN); | |
188 } | |
OLD | NEW |