Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(340)

Side by Side Diff: chrome/browser/infobars/infobar_service.cc

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

Powered by Google App Engine
This is Rietveld 408576698