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( |
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 = details.entry->GetTransitionType() & |
| 51 content::PAGE_TRANSITION_IS_REDIRECT_MASK; |
| 52 navigation_details.did_replace_entry = details.did_replace_entry; |
| 53 navigation_details.is_navigation_to_different_page = |
| 54 details.is_navigation_to_different_page(); |
| 55 navigation_details.is_main_frame = details.is_main_frame; |
| 56 return navigation_details; |
84 } | 57 } |
85 | 58 |
86 InfoBarService::~InfoBarService() { | 59 // static |
87 // Destroy all remaining InfoBars. It's important to not animate here so that | 60 int InfoBarService::GetActiveEntryID(content::WebContents* web_contents) { |
88 // we guarantee that we'll delete all delegates before we do anything else. | 61 content::NavigationEntry* active_entry = |
89 RemoveAllInfoBars(false); | 62 web_contents->GetController().GetActiveEntry(); |
| 63 return active_entry ? active_entry->GetUniqueID() : 0; |
90 } | 64 } |
91 | 65 |
92 void InfoBarService::RenderProcessGone(base::TerminationStatus status) { | 66 void InfoBarService::RenderProcessGone(base::TerminationStatus status) { |
93 RemoveAllInfoBars(true); | 67 infobar_manager_.RemoveAllInfoBars(true); |
94 } | 68 } |
95 | 69 |
96 void InfoBarService::NavigationEntryCommitted( | 70 void InfoBarService::NavigationEntryCommitted( |
97 const content::LoadCommittedDetails& load_details) { | 71 const content::LoadCommittedDetails& load_details) { |
98 // NOTE: It is not safe to change the following code to count upwards or | 72 infobar_manager_.OnNavigation( |
99 // use iterators, as the RemoveInfoBar() call synchronously modifies our | 73 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 } | 74 } |
107 | 75 |
108 void InfoBarService::WebContentsDestroyed(content::WebContents* web_contents) { | 76 void InfoBarService::WebContentsDestroyed(content::WebContents* web_contents) { |
| 77 infobar_manager_.CleanUp(); |
109 // The WebContents is going away; be aggressively paranoid and delete | 78 // The WebContents is going away; be aggressively paranoid and delete |
110 // ourselves lest other parts of the system attempt to add infobars or use | 79 // ourselves lest other parts of the system attempt to add infobars or use |
111 // us otherwise during the destruction. | 80 // us otherwise during the destruction. |
112 web_contents->RemoveUserData(UserDataKey()); | 81 web_contents->RemoveUserData(UserDataKey()); |
113 // That was the equivalent of "delete this". This object is now destroyed; | 82 // That was the equivalent of "delete this". This object is now destroyed; |
114 // returning from this function is the only safe thing to do. | 83 // returning from this function is the only safe thing to do. |
115 } | 84 } |
116 | 85 |
117 bool InfoBarService::OnMessageReceived(const IPC::Message& message) { | 86 bool InfoBarService::OnMessageReceived(const IPC::Message& message) { |
118 bool handled = true; | 87 bool handled = true; |
119 IPC_BEGIN_MESSAGE_MAP(InfoBarService, message) | 88 IPC_BEGIN_MESSAGE_MAP(InfoBarService, message) |
120 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockDisplayingInsecureContent, | 89 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockDisplayingInsecureContent, |
121 OnDidBlockDisplayingInsecureContent) | 90 OnDidBlockDisplayingInsecureContent) |
122 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockRunningInsecureContent, | 91 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockRunningInsecureContent, |
123 OnDidBlockRunningInsecureContent) | 92 OnDidBlockRunningInsecureContent) |
124 IPC_MESSAGE_UNHANDLED(handled = false) | 93 IPC_MESSAGE_UNHANDLED(handled = false) |
125 IPC_END_MESSAGE_MAP() | 94 IPC_END_MESSAGE_MAP() |
126 return handled; | 95 return handled; |
127 } | 96 } |
128 | 97 |
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() { | 98 void InfoBarService::OnDidBlockDisplayingInsecureContent() { |
160 InsecureContentInfoBarDelegate::Create( | 99 InsecureContentInfoBarDelegate::Create( |
161 this, InsecureContentInfoBarDelegate::DISPLAY); | 100 this, InsecureContentInfoBarDelegate::DISPLAY); |
162 } | 101 } |
163 | 102 |
164 void InfoBarService::OnDidBlockRunningInsecureContent() { | 103 void InfoBarService::OnDidBlockRunningInsecureContent() { |
165 InsecureContentInfoBarDelegate::Create(this, | 104 InsecureContentInfoBarDelegate::Create(this, |
166 InsecureContentInfoBarDelegate::RUN); | 105 InsecureContentInfoBarDelegate::RUN); |
167 } | 106 } |
| 107 |
| 108 void InfoBarService::OnInfoBarAdded(InfoBar* infobar) { |
| 109 content::NotificationService::current()->Notify( |
| 110 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, |
| 111 content::Source<InfoBarService>(this), |
| 112 content::Details<InfoBar::AddedDetails>(infobar)); |
| 113 } |
| 114 |
| 115 void InfoBarService::OnInfoBarReplaced(InfoBar* old_infobar, |
| 116 InfoBar* new_infobar) { |
| 117 InfoBar::ReplacedDetails replaced_details(old_infobar, new_infobar); |
| 118 content::NotificationService::current()->Notify( |
| 119 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED, |
| 120 content::Source<InfoBarService>(this), |
| 121 content::Details<InfoBar::ReplacedDetails>(&replaced_details)); |
| 122 } |
| 123 |
| 124 void InfoBarService::OnInfoBarRemoved(InfoBar* infobar, bool animate) { |
| 125 InfoBar::RemovedDetails removed_details(infobar, animate); |
| 126 content::NotificationService::current()->Notify( |
| 127 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, |
| 128 content::Source<InfoBarService>(this), |
| 129 content::Details<InfoBar::RemovedDetails>(&removed_details)); |
| 130 } |
OLD | NEW |