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

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

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

Powered by Google App Engine
This is Rietveld 408576698