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