OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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" | 10 #include "chrome/browser/infobars/infobar_delegate.h" |
11 #include "chrome/browser/infobars/insecure_content_infobar_delegate.h" | 11 #include "chrome/browser/infobars/insecure_content_infobar_delegate.h" |
12 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
13 #include "chrome/common/render_messages.h" | 13 #include "chrome/common/render_messages.h" |
14 #include "content/public/browser/navigation_controller.h" | 14 #include "content/public/browser/navigation_controller.h" |
15 #include "content/public/browser/navigation_details.h" | |
16 #include "content/public/browser/navigation_entry.h" | |
15 #include "content/public/browser/notification_service.h" | 17 #include "content/public/browser/notification_service.h" |
16 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
17 | 19 |
18 | 20 |
19 DEFINE_WEB_CONTENTS_USER_DATA_KEY(InfoBarService); | 21 DEFINE_WEB_CONTENTS_USER_DATA_KEY(InfoBarService); |
20 | 22 |
21 InfoBar* InfoBarService::AddInfoBar(scoped_ptr<InfoBar> infobar) { | 23 InfoBarService::InfoBarService(content::WebContents* web_contents) |
22 DCHECK(infobar); | 24 : content::WebContentsObserver(web_contents) { |
23 if (!infobars_enabled_) | 25 DCHECK(web_contents); |
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 content::NotificationService::current()->Notify( | |
39 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, | |
40 content::Source<InfoBarService>(this), | |
41 content::Details<InfoBar::AddedDetails>(infobar_ptr)); | |
42 return infobar_ptr; | |
43 } | 26 } |
44 | 27 |
45 void InfoBarService::RemoveInfoBar(InfoBar* infobar) { | 28 InfoBarService::~InfoBarService() {} |
46 RemoveInfoBarInternal(infobar, true); | 29 |
30 InfoBar* InfoBarService::AddInfoBar(scoped_ptr<InfoBar> infobar) { | |
31 return infobar_manager_.AddInfoBar(infobar.Pass(), | |
32 GetActiveEntryID(web_contents())); | |
47 } | 33 } |
48 | 34 |
49 InfoBar* InfoBarService::ReplaceInfoBar(InfoBar* old_infobar, | 35 InfoBar* InfoBarService::ReplaceInfoBar(InfoBar* old_infobar, |
50 scoped_ptr<InfoBar> new_infobar) { | 36 scoped_ptr<InfoBar> new_infobar) { |
51 DCHECK(old_infobar); | 37 return infobar_manager_.ReplaceInfoBar( |
52 if (!infobars_enabled_) | 38 old_infobar, new_infobar.Pass(), GetActiveEntryID(web_contents())); |
53 return AddInfoBar(new_infobar.Pass()); // Deletes the infobar. | |
54 DCHECK(new_infobar); | |
55 | |
56 InfoBars::iterator i(std::find(infobars_.begin(), infobars_.end(), | |
57 old_infobar)); | |
58 DCHECK(i != infobars_.end()); | |
59 | |
60 InfoBar* new_infobar_ptr = new_infobar.release(); | |
61 i = infobars_.insert(i, new_infobar_ptr); | |
62 new_infobar_ptr->SetOwner(this); | |
63 InfoBar::ReplacedDetails replaced_details(old_infobar, new_infobar_ptr); | |
64 | |
65 // 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 infobars_.erase(++i); | |
68 | |
69 content::NotificationService::current()->Notify( | |
70 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED, | |
71 content::Source<InfoBarService>(this), | |
72 content::Details<InfoBar::ReplacedDetails>(&replaced_details)); | |
73 | |
74 old_infobar->CloseSoon(); | |
75 return new_infobar_ptr; | |
76 } | 39 } |
77 | 40 |
78 InfoBarService::InfoBarService(content::WebContents* web_contents) | 41 // static |
79 : content::WebContentsObserver(web_contents), | 42 InfoBarDelegate::NavigationDetails |
80 infobars_enabled_(true) { | 43 InfoBarService::NavigationDetailsFromLoadCommittedDetails( |
Peter Kasting
2014/03/18 18:29:17
Nit: Indent 4
| |
81 DCHECK(web_contents); | 44 const content::LoadCommittedDetails& details) { |
82 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableInfoBars)) | 45 InfoBarDelegate::NavigationDetails navigation_details; |
83 infobars_enabled_ = false; | 46 navigation_details.entry_id = details.entry->GetUniqueID(); |
47 navigation_details.is_reload = content::PageTransitionStripQualifier( | |
48 details.entry->GetTransitionType()) == | |
49 content::PAGE_TRANSITION_RELOAD; | |
50 navigation_details.is_redirect = | |
51 (details.entry->GetTransitionType() & | |
52 content::PAGE_TRANSITION_IS_REDIRECT_MASK) != 0; | |
53 navigation_details.did_replace_entry = details.did_replace_entry; | |
54 navigation_details.is_navigation_to_different_page = | |
55 details.is_navigation_to_different_page(); | |
56 navigation_details.is_main_frame = details.is_main_frame; | |
57 return navigation_details; | |
84 } | 58 } |
85 | 59 |
86 InfoBarService::~InfoBarService() { | 60 // static |
87 // Destroy all remaining InfoBars. It's important to not animate here so that | 61 int InfoBarService::GetActiveEntryID(content::WebContents* web_contents) { |
88 // we guarantee that we'll delete all delegates before we do anything else. | 62 content::NavigationEntry* active_entry = |
89 RemoveAllInfoBars(false); | 63 web_contents->GetController().GetActiveEntry(); |
64 return active_entry ? active_entry->GetUniqueID() : 0; | |
90 } | 65 } |
91 | 66 |
92 void InfoBarService::RenderProcessGone(base::TerminationStatus status) { | 67 void InfoBarService::RenderProcessGone(base::TerminationStatus status) { |
93 RemoveAllInfoBars(true); | 68 infobar_manager_.RemoveAllInfoBars(true); |
94 } | 69 } |
95 | 70 |
96 void InfoBarService::NavigationEntryCommitted( | 71 void InfoBarService::NavigationEntryCommitted( |
97 const content::LoadCommittedDetails& load_details) { | 72 const content::LoadCommittedDetails& load_details) { |
98 // NOTE: It is not safe to change the following code to count upwards or | 73 infobar_manager_.OnNavigation( |
99 // use iterators, as the RemoveInfoBar() call synchronously modifies our | 74 NavigationDetailsFromLoadCommittedDetails(load_details)); |
100 // delegate list. | |
101 for (size_t i = infobars_.size(); i > 0; --i) { | |
102 InfoBar* infobar = infobars_[i - 1]; | |
103 if (infobar->delegate()->ShouldExpire(load_details)) | |
104 RemoveInfoBar(infobar); | |
105 } | |
106 } | 75 } |
107 | 76 |
108 void InfoBarService::WebContentsDestroyed(content::WebContents* web_contents) { | 77 void InfoBarService::WebContentsDestroyed(content::WebContents* web_contents) { |
78 infobar_manager_.CleanUp(); | |
109 // The WebContents is going away; be aggressively paranoid and delete | 79 // The WebContents is going away; be aggressively paranoid and delete |
110 // ourselves lest other parts of the system attempt to add infobars or use | 80 // ourselves lest other parts of the system attempt to add infobars or use |
111 // us otherwise during the destruction. | 81 // us otherwise during the destruction. |
112 web_contents->RemoveUserData(UserDataKey()); | 82 web_contents->RemoveUserData(UserDataKey()); |
113 // That was the equivalent of "delete this". This object is now destroyed; | 83 // That was the equivalent of "delete this". This object is now destroyed; |
114 // returning from this function is the only safe thing to do. | 84 // returning from this function is the only safe thing to do. |
115 } | 85 } |
116 | 86 |
117 bool InfoBarService::OnMessageReceived(const IPC::Message& message) { | 87 bool InfoBarService::OnMessageReceived(const IPC::Message& message) { |
118 bool handled = true; | 88 bool handled = true; |
119 IPC_BEGIN_MESSAGE_MAP(InfoBarService, message) | 89 IPC_BEGIN_MESSAGE_MAP(InfoBarService, message) |
120 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockDisplayingInsecureContent, | 90 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockDisplayingInsecureContent, |
121 OnDidBlockDisplayingInsecureContent) | 91 OnDidBlockDisplayingInsecureContent) |
122 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockRunningInsecureContent, | 92 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockRunningInsecureContent, |
123 OnDidBlockRunningInsecureContent) | 93 OnDidBlockRunningInsecureContent) |
124 IPC_MESSAGE_UNHANDLED(handled = false) | 94 IPC_MESSAGE_UNHANDLED(handled = false) |
125 IPC_END_MESSAGE_MAP() | 95 IPC_END_MESSAGE_MAP() |
126 return handled; | 96 return handled; |
127 } | 97 } |
128 | 98 |
129 void InfoBarService::RemoveInfoBarInternal(InfoBar* infobar, bool animate) { | |
130 DCHECK(infobar); | |
131 if (!infobars_enabled_) { | |
132 DCHECK(infobars_.empty()); | |
133 return; | |
134 } | |
135 | |
136 InfoBars::iterator i(std::find(infobars_.begin(), infobars_.end(), infobar)); | |
137 DCHECK(i != infobars_.end()); | |
138 | |
139 // 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. | |
141 infobars_.erase(i); | |
142 | |
143 // This notification must happen before the call to CloseSoon() below, since | |
144 // observers may want to access |infobar| and that call can delete it. | |
145 InfoBar::RemovedDetails removed_details(infobar, animate); | |
146 content::NotificationService::current()->Notify( | |
147 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, | |
148 content::Source<InfoBarService>(this), | |
149 content::Details<InfoBar::RemovedDetails>(&removed_details)); | |
150 | |
151 infobar->CloseSoon(); | |
152 } | |
153 | |
154 void InfoBarService::RemoveAllInfoBars(bool animate) { | |
155 while (!infobars_.empty()) | |
156 RemoveInfoBarInternal(infobars_.back(), animate); | |
157 } | |
158 | |
159 void InfoBarService::OnDidBlockDisplayingInsecureContent() { | 99 void InfoBarService::OnDidBlockDisplayingInsecureContent() { |
160 InsecureContentInfoBarDelegate::Create( | 100 InsecureContentInfoBarDelegate::Create( |
161 this, InsecureContentInfoBarDelegate::DISPLAY); | 101 this, InsecureContentInfoBarDelegate::DISPLAY); |
162 } | 102 } |
163 | 103 |
164 void InfoBarService::OnDidBlockRunningInsecureContent() { | 104 void InfoBarService::OnDidBlockRunningInsecureContent() { |
165 InsecureContentInfoBarDelegate::Create(this, | 105 InsecureContentInfoBarDelegate::Create(this, |
166 InsecureContentInfoBarDelegate::RUN); | 106 InsecureContentInfoBarDelegate::RUN); |
167 } | 107 } |
108 | |
109 void InfoBarService::OnInfoBarAdded(InfoBar* infobar) { | |
droger
2014/03/18 15:59:53
These functions listen for InfoBarManager and emit
Peter Kasting
2014/03/18 18:29:17
Since we're trying to kill notifications in Chrome
blundell
2014/03/19 15:08:09
There are 20+ listeners for these notifications cu
Peter Kasting
2014/03/19 18:11:11
Splitting work into smaller pieces is always fine
| |
110 content::NotificationService::current()->Notify( | |
111 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, | |
112 content::Source<InfoBarService>(this), | |
113 content::Details<InfoBar::AddedDetails>(infobar)); | |
114 } | |
115 | |
116 void InfoBarService::OnInfoBarReplaced(InfoBar* old_infobar, | |
117 InfoBar* new_infobar) { | |
118 InfoBar::ReplacedDetails replaced_details(old_infobar, new_infobar); | |
119 content::NotificationService::current()->Notify( | |
120 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED, | |
121 content::Source<InfoBarService>(this), | |
122 content::Details<InfoBar::ReplacedDetails>(&replaced_details)); | |
123 } | |
124 | |
125 void InfoBarService::OnInfoBarRemoved(InfoBar* infobar, bool animate) { | |
126 InfoBar::RemovedDetails removed_details(infobar, animate); | |
127 content::NotificationService::current()->Notify( | |
128 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, | |
129 content::Source<InfoBarService>(this), | |
130 content::Details<InfoBar::RemovedDetails>(&removed_details)); | |
131 } | |
OLD | NEW |