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