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

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: Rebase + comments Created 6 years, 8 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
19 DEFINE_WEB_CONTENTS_USER_DATA_KEY(InfoBarService); 15 DEFINE_WEB_CONTENTS_USER_DATA_KEY(InfoBarService);
20 16
21 InfoBar* InfoBarService::AddInfoBar(scoped_ptr<InfoBar> infobar) { 17 // static
22 DCHECK(infobar); 18 InfoBarManager* InfoBarService::InfoBarManagerFromWebContents(
23 if (!infobars_enabled_) 19 content::WebContents* web_contents) {
20 InfoBarService* infobar_service = FromWebContents(web_contents);
21 // |infobar_service| may be NULL during shutdown.
22 if (!infobar_service)
24 return NULL; 23 return NULL;
25 24 return infobar_service->infobar_manager();
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 } 25 }
47 26
48 void InfoBarService::RemoveInfoBar(InfoBar* infobar) { 27 InfoBar* InfoBarService::AddInfoBar(scoped_ptr<InfoBar> infobar) {
49 RemoveInfoBarInternal(infobar, true); 28 return infobar_manager_.AddInfoBar(infobar.Pass());
50 } 29 }
51 30
52 InfoBar* InfoBarService::ReplaceInfoBar(InfoBar* old_infobar, 31 InfoBar* InfoBarService::ReplaceInfoBar(InfoBar* old_infobar,
53 scoped_ptr<InfoBar> new_infobar) { 32 scoped_ptr<InfoBar> new_infobar) {
54 DCHECK(old_infobar); 33 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 } 34 }
93 35
94 InfoBarService::InfoBarService(content::WebContents* web_contents) 36 InfoBarService::InfoBarService(content::WebContents* web_contents)
95 : content::WebContentsObserver(web_contents), 37 : content::WebContentsObserver(web_contents),
96 infobars_enabled_(true) { 38 infobar_manager_(web_contents) {
97 DCHECK(web_contents); 39 DCHECK(web_contents);
98 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableInfoBars)) 40 infobar_manager_.AddObserver(this);
99 infobars_enabled_ = false;
100 } 41 }
101 42
102 InfoBarService::~InfoBarService() { 43 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 44
109 void InfoBarService::RenderProcessGone(base::TerminationStatus status) { 45 void InfoBarService::RenderProcessGone(base::TerminationStatus status) {
110 RemoveAllInfoBars(true); 46 infobar_manager_.RemoveAllInfoBars(true);
111 } 47 }
112 48
113 void InfoBarService::NavigationEntryCommitted( 49 void InfoBarService::NavigationEntryCommitted(
114 const content::LoadCommittedDetails& load_details) { 50 const content::LoadCommittedDetails& load_details) {
115 // NOTE: It is not safe to change the following code to count upwards or 51 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 } 52 }
124 53
125 void InfoBarService::WebContentsDestroyed(content::WebContents* web_contents) { 54 void InfoBarService::WebContentsDestroyed(content::WebContents* web_contents) {
55 infobar_manager_.OnWebContentsDestroyed();
56
126 // The WebContents is going away; be aggressively paranoid and delete 57 // The WebContents is going away; be aggressively paranoid and delete
127 // ourselves lest other parts of the system attempt to add infobars or use 58 // ourselves lest other parts of the system attempt to add infobars or use
128 // us otherwise during the destruction. 59 // us otherwise during the destruction.
129 web_contents->RemoveUserData(UserDataKey()); 60 web_contents->RemoveUserData(UserDataKey());
130 // That was the equivalent of "delete this". This object is now destroyed; 61 // That was the equivalent of "delete this". This object is now destroyed;
131 // returning from this function is the only safe thing to do. 62 // returning from this function is the only safe thing to do.
132 } 63 }
133 64
134 bool InfoBarService::OnMessageReceived(const IPC::Message& message) { 65 bool InfoBarService::OnMessageReceived(const IPC::Message& message) {
135 bool handled = true; 66 bool handled = true;
136 IPC_BEGIN_MESSAGE_MAP(InfoBarService, message) 67 IPC_BEGIN_MESSAGE_MAP(InfoBarService, message)
137 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockDisplayingInsecureContent, 68 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockDisplayingInsecureContent,
138 OnDidBlockDisplayingInsecureContent) 69 OnDidBlockDisplayingInsecureContent)
139 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockRunningInsecureContent, 70 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockRunningInsecureContent,
140 OnDidBlockRunningInsecureContent) 71 OnDidBlockRunningInsecureContent)
141 IPC_MESSAGE_UNHANDLED(handled = false) 72 IPC_MESSAGE_UNHANDLED(handled = false)
142 IPC_END_MESSAGE_MAP() 73 IPC_END_MESSAGE_MAP()
143 return handled; 74 return handled;
144 } 75 }
145 76
146 void InfoBarService::RemoveInfoBarInternal(InfoBar* infobar, bool animate) { 77 void InfoBarService::OnInfoBarAdded(InfoBar* infobar) {
147 DCHECK(infobar); 78 // TODO(droger): Remove the notifications and have listeners change to be
148 if (!infobars_enabled_) { 79 // NavigationManager::Observers instead. See http://crbug.com/354380
149 DCHECK(infobars_.empty()); 80 content::NotificationService::current()->Notify(
150 return; 81 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED,
151 } 82 content::Source<InfoBarService>(this),
83 content::Details<InfoBar::AddedDetails>(infobar));
84 }
152 85
153 InfoBars::iterator i(std::find(infobars_.begin(), infobars_.end(), infobar)); 86 void InfoBarService::OnInfoBarReplaced(InfoBar* old_infobar,
154 DCHECK(i != infobars_.end()); 87 InfoBar* new_infobar) {
88 // TODO(droger): Remove the notifications and have listeners change to be
89 // NavigationManager::Observers instead. See http://crbug.com/354380
90 InfoBar::ReplacedDetails replaced_details(old_infobar, new_infobar);
91 content::NotificationService::current()->Notify(
92 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED,
93 content::Source<InfoBarService>(this),
94 content::Details<InfoBar::ReplacedDetails>(&replaced_details));
95 }
155 96
156 // Remove the infobar before notifying, so that if any observers call back to 97 void InfoBarService::OnInfoBarRemoved(InfoBar* infobar, bool animate) {
157 // AddInfoBar() or similar, we don't dupe-check against this infobar. 98 // TODO(droger): Remove the notifications and have listeners change to be
158 infobars_.erase(i); 99 // NavigationManager::Observers instead. See http://crbug.com/354380
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.
165 // See http://crbug.com/354380
166 InfoBar::RemovedDetails removed_details(infobar, animate); 100 InfoBar::RemovedDetails removed_details(infobar, animate);
167 content::NotificationService::current()->Notify( 101 content::NotificationService::current()->Notify(
168 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, 102 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
169 content::Source<InfoBarService>(this), 103 content::Source<InfoBarService>(this),
170 content::Details<InfoBar::RemovedDetails>(&removed_details)); 104 content::Details<InfoBar::RemovedDetails>(&removed_details));
171
172 infobar->CloseSoon();
173 } 105 }
174 106
175 void InfoBarService::RemoveAllInfoBars(bool animate) { 107 void InfoBarService::OnManagerShuttingDown(InfoBarManager* manager) {
176 while (!infobars_.empty()) 108 infobar_manager_.RemoveObserver(this);
177 RemoveInfoBarInternal(infobars_.back(), animate);
178 } 109 }
179 110
180 void InfoBarService::OnDidBlockDisplayingInsecureContent() { 111 void InfoBarService::OnDidBlockDisplayingInsecureContent() {
181 InsecureContentInfoBarDelegate::Create( 112 InsecureContentInfoBarDelegate::Create(
182 this, InsecureContentInfoBarDelegate::DISPLAY); 113 this, InsecureContentInfoBarDelegate::DISPLAY);
183 } 114 }
184 115
185 void InfoBarService::OnDidBlockRunningInsecureContent() { 116 void InfoBarService::OnDidBlockRunningInsecureContent() {
186 InsecureContentInfoBarDelegate::Create(this, 117 InsecureContentInfoBarDelegate::Create(this,
187 InsecureContentInfoBarDelegate::RUN); 118 InsecureContentInfoBarDelegate::RUN);
188 } 119 }
OLDNEW
« no previous file with comments | « chrome/browser/infobars/infobar_service.h ('k') | chrome/browser/infobars/infobars_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698