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 InfoBar* InfoBarManager::ReplaceInfoBar(InfoBar* old_infobar, |
53 scoped_ptr<InfoBar> new_infobar) { | 39 scoped_ptr<InfoBar> new_infobar) { |
54 DCHECK(old_infobar); | 40 DCHECK(old_infobar); |
55 if (!infobars_enabled_) | 41 if (!infobars_enabled_) |
56 return AddInfoBar(new_infobar.Pass()); // Deletes the infobar. | 42 return AddInfoBar(new_infobar.Pass()); // Deletes the infobar. |
57 DCHECK(new_infobar); | 43 DCHECK(new_infobar); |
58 | 44 |
59 InfoBars::iterator i(std::find(infobars_.begin(), infobars_.end(), | 45 InfoBars::iterator i(std::find(infobars_.begin(), infobars_.end(), |
60 old_infobar)); | 46 old_infobar)); |
61 DCHECK(i != infobars_.end()); | 47 DCHECK(i != infobars_.end()); |
62 | 48 |
63 InfoBar* new_infobar_ptr = new_infobar.release(); | 49 InfoBar* new_infobar_ptr = new_infobar.release(); |
64 i = infobars_.insert(i, new_infobar_ptr); | 50 i = infobars_.insert(i, new_infobar_ptr); |
65 new_infobar_ptr->SetOwner(this); | 51 new_infobar_ptr->SetOwner(this); |
66 InfoBar::ReplacedDetails replaced_details(old_infobar, new_infobar_ptr); | |
67 | 52 |
68 // Remove the old infobar before notifying, so that if any observers call back | 53 // 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. | 54 // to AddInfoBar() or similar, we don't dupe-check against this infobar. |
70 infobars_.erase(++i); | 55 infobars_.erase(++i); |
71 | 56 |
72 FOR_EACH_OBSERVER(Observer, | 57 FOR_EACH_OBSERVER(Observer, |
73 observer_list_, | 58 observer_list_, |
74 OnInfoBarReplaced(old_infobar, new_infobar_ptr)); | 59 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 | 60 |
82 old_infobar->CloseSoon(); | 61 old_infobar->CloseSoon(); |
83 return new_infobar_ptr; | 62 return new_infobar_ptr; |
84 } | 63 } |
85 | 64 |
86 void InfoBarService::AddObserver(Observer* obs) { | 65 void InfoBarManager::AddObserver(Observer* obs) { |
87 observer_list_.AddObserver(obs); | 66 observer_list_.AddObserver(obs); |
88 } | 67 } |
89 | 68 |
90 void InfoBarService::RemoveObserver(Observer* obs) { | 69 void InfoBarManager::RemoveObserver(Observer* obs) { |
91 observer_list_.RemoveObserver(obs); | 70 observer_list_.RemoveObserver(obs); |
92 } | 71 } |
93 | 72 |
94 InfoBarService::InfoBarService(content::WebContents* web_contents) | 73 InfoBarManager::InfoBarManager(content::WebContents* web_contents) |
95 : content::WebContentsObserver(web_contents), | 74 : infobars_enabled_(true), web_contents_(web_contents) { |
Peter Kasting
2014/03/26 23:51:30
Nit: One member per line
| |
96 infobars_enabled_(true) { | |
97 DCHECK(web_contents); | 75 DCHECK(web_contents); |
98 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableInfoBars)) | 76 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableInfoBars)) |
99 infobars_enabled_ = false; | 77 infobars_enabled_ = false; |
100 } | 78 } |
101 | 79 |
102 InfoBarService::~InfoBarService() { | 80 InfoBarManager::~InfoBarManager() { |
103 // Destroy all remaining InfoBars. It's important to not animate here so that | 81 // 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. | 82 // we guarantee that we'll delete all delegates before we do anything else. |
105 RemoveAllInfoBars(false); | 83 RemoveAllInfoBars(false); |
106 FOR_EACH_OBSERVER(Observer, observer_list_, OnServiceShuttingDown(this)); | 84 FOR_EACH_OBSERVER(Observer, observer_list_, OnManagerShuttingDown(this)); |
107 } | 85 } |
108 | 86 |
109 void InfoBarService::RenderProcessGone(base::TerminationStatus status) { | 87 void InfoBarManager::OnNavigation( |
110 RemoveAllInfoBars(true); | |
111 } | |
112 | |
113 void InfoBarService::NavigationEntryCommitted( | |
114 const content::LoadCommittedDetails& load_details) { | 88 const content::LoadCommittedDetails& load_details) { |
115 // NOTE: It is not safe to change the following code to count upwards or | 89 // NOTE: It is not safe to change the following code to count upwards or |
116 // use iterators, as the RemoveInfoBar() call synchronously modifies our | 90 // use iterators, as the RemoveInfoBar() call synchronously modifies our |
117 // delegate list. | 91 // delegate list. |
118 for (size_t i = infobars_.size(); i > 0; --i) { | 92 for (size_t i = infobars_.size(); i > 0; --i) { |
119 InfoBar* infobar = infobars_[i - 1]; | 93 InfoBar* infobar = infobars_[i - 1]; |
120 if (infobar->delegate()->ShouldExpire(load_details)) | 94 if (infobar->delegate()->ShouldExpire(load_details)) |
121 RemoveInfoBar(infobar); | 95 RemoveInfoBar(infobar); |
122 } | 96 } |
123 } | 97 } |
124 | 98 |
125 void InfoBarService::WebContentsDestroyed(content::WebContents* web_contents) { | 99 void InfoBarManager::CleanUp() { web_contents_ = NULL; } |
blundell
2014/03/27 09:40:30
The long-term impl of this method is to cycle thro
| |
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 | 100 |
134 bool InfoBarService::OnMessageReceived(const IPC::Message& message) { | 101 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); | 102 DCHECK(infobar); |
148 if (!infobars_enabled_) { | 103 if (!infobars_enabled_) { |
149 DCHECK(infobars_.empty()); | 104 DCHECK(infobars_.empty()); |
150 return; | 105 return; |
151 } | 106 } |
152 | 107 |
153 InfoBars::iterator i(std::find(infobars_.begin(), infobars_.end(), infobar)); | 108 InfoBars::iterator i(std::find(infobars_.begin(), infobars_.end(), infobar)); |
154 DCHECK(i != infobars_.end()); | 109 DCHECK(i != infobars_.end()); |
155 | 110 |
156 // Remove the infobar before notifying, so that if any observers call back to | 111 // 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. | 112 // AddInfoBar() or similar, we don't dupe-check against this infobar. |
158 infobars_.erase(i); | 113 infobars_.erase(i); |
159 | 114 |
160 // This notification must happen before the call to CloseSoon() below, since | 115 // This notification must happen before the call to CloseSoon() below, since |
161 // observers may want to access |infobar| and that call can delete it. | 116 // observers may want to access |infobar| and that call can delete it. |
162 FOR_EACH_OBSERVER(Observer, observer_list_, | 117 FOR_EACH_OBSERVER(Observer, observer_list_, |
163 OnInfoBarRemoved(infobar, animate)); | 118 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 | 119 |
172 infobar->CloseSoon(); | 120 infobar->CloseSoon(); |
173 } | 121 } |
174 | 122 |
175 void InfoBarService::RemoveAllInfoBars(bool animate) { | 123 void InfoBarManager::RemoveAllInfoBars(bool animate) { |
176 while (!infobars_.empty()) | 124 while (!infobars_.empty()) |
177 RemoveInfoBarInternal(infobars_.back(), animate); | 125 RemoveInfoBarInternal(infobars_.back(), animate); |
178 } | 126 } |
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 |