| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/devtools/global_confirm_info_bar.h" | 5 #include "chrome/browser/devtools/global_confirm_info_bar.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/stl_util.h" |
| 10 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
| 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 12 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 12 #include "components/infobars/core/infobar.h" | 13 #include "components/infobars/core/infobar.h" |
| 13 #include "ui/gfx/image/image.h" | 14 #include "ui/gfx/image/image.h" |
| 14 | 15 |
| 15 // InfoBarDelegateProxy ------------------------------------------------------- | 16 // InfoBarDelegateProxy ------------------------------------------------------- |
| 16 | 17 |
| 17 class GlobalConfirmInfoBar::DelegateProxy : public ConfirmInfoBarDelegate { | 18 class GlobalConfirmInfoBar::DelegateProxy : public ConfirmInfoBarDelegate { |
| 18 public: | 19 public: |
| 19 explicit DelegateProxy(base::WeakPtr<GlobalConfirmInfoBar> global_info_bar); | 20 explicit DelegateProxy(base::WeakPtr<GlobalConfirmInfoBar> global_info_bar); |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 GlobalConfirmInfoBar::~GlobalConfirmInfoBar() { | 154 GlobalConfirmInfoBar::~GlobalConfirmInfoBar() { |
| 154 while (!proxies_.empty()) { | 155 while (!proxies_.empty()) { |
| 155 auto it = proxies_.begin(); | 156 auto it = proxies_.begin(); |
| 156 it->second->Detach(); | 157 it->second->Detach(); |
| 157 it->first->RemoveObserver(this); | 158 it->first->RemoveObserver(this); |
| 158 it->first->RemoveInfoBar(it->second->info_bar_); | 159 it->first->RemoveInfoBar(it->second->info_bar_); |
| 159 proxies_.erase(it); | 160 proxies_.erase(it); |
| 160 } | 161 } |
| 161 } | 162 } |
| 162 | 163 |
| 163 void GlobalConfirmInfoBar::TabInsertedAt(content::WebContents* web_contents, | 164 void GlobalConfirmInfoBar::TabInsertedAt(TabStripModel* tab_strip_model, |
| 165 content::WebContents* web_contents, |
| 164 int index, | 166 int index, |
| 165 bool foreground) { | 167 bool foreground) { |
| 166 InfoBarService* infobar_service = | 168 MaybeAddInfoBar(web_contents); |
| 167 InfoBarService::FromWebContents(web_contents); | |
| 168 // WebContents from the tab strip must have the infobar service. | |
| 169 DCHECK(infobar_service); | |
| 170 if (proxies_.find(infobar_service) != proxies_.end()) | |
| 171 return; | |
| 172 | |
| 173 std::unique_ptr<GlobalConfirmInfoBar::DelegateProxy> proxy( | |
| 174 new GlobalConfirmInfoBar::DelegateProxy(weak_factory_.GetWeakPtr())); | |
| 175 GlobalConfirmInfoBar::DelegateProxy* proxy_ptr = proxy.get(); | |
| 176 infobars::InfoBar* added_bar = infobar_service->AddInfoBar( | |
| 177 infobar_service->CreateConfirmInfoBar(std::move(proxy))); | |
| 178 | |
| 179 proxy_ptr->info_bar_ = added_bar; | |
| 180 DCHECK(added_bar); | |
| 181 proxies_[infobar_service] = proxy_ptr; | |
| 182 infobar_service->AddObserver(this); | |
| 183 } | 169 } |
| 184 | 170 |
| 185 void GlobalConfirmInfoBar::TabChangedAt(content::WebContents* web_contents, | 171 void GlobalConfirmInfoBar::TabChangedAt(content::WebContents* web_contents, |
| 186 int index, | 172 int index, |
| 187 TabChangeType change_type) { | 173 TabChangeType change_type) { |
| 188 InfoBarService* infobar_service = | 174 MaybeAddInfoBar(web_contents); |
| 189 InfoBarService::FromWebContents(web_contents); | |
| 190 auto it = proxies_.find(infobar_service); | |
| 191 if (it == proxies_.end()) | |
| 192 TabInsertedAt(web_contents, index, false); | |
| 193 } | 175 } |
| 194 | 176 |
| 195 void GlobalConfirmInfoBar::OnInfoBarRemoved(infobars::InfoBar* info_bar, | 177 void GlobalConfirmInfoBar::OnInfoBarRemoved(infobars::InfoBar* info_bar, |
| 196 bool animate) { | 178 bool animate) { |
| 197 // Do not process alien infobars. | 179 // Do not process alien infobars. |
| 198 for (auto it : proxies_) { | 180 for (auto it : proxies_) { |
| 199 if (it.second->info_bar_ == info_bar) { | 181 if (it.second->info_bar_ == info_bar) { |
| 200 OnManagerShuttingDown(info_bar->owner()); | 182 OnManagerShuttingDown(info_bar->owner()); |
| 201 break; | 183 break; |
| 202 } | 184 } |
| 203 } | 185 } |
| 204 } | 186 } |
| 205 | 187 |
| 206 void GlobalConfirmInfoBar::OnManagerShuttingDown( | 188 void GlobalConfirmInfoBar::OnManagerShuttingDown( |
| 207 infobars::InfoBarManager* manager) { | 189 infobars::InfoBarManager* manager) { |
| 208 manager->RemoveObserver(this); | 190 manager->RemoveObserver(this); |
| 209 proxies_.erase(manager); | 191 proxies_.erase(manager); |
| 210 } | 192 } |
| 193 |
| 194 void GlobalConfirmInfoBar::MaybeAddInfoBar(content::WebContents* web_contents) { |
| 195 InfoBarService* infobar_service = |
| 196 InfoBarService::FromWebContents(web_contents); |
| 197 // WebContents from the tab strip must have the infobar service. |
| 198 DCHECK(infobar_service); |
| 199 if (ContainsKey(proxies_, infobar_service)) |
| 200 return; |
| 201 |
| 202 std::unique_ptr<GlobalConfirmInfoBar::DelegateProxy> proxy( |
| 203 new GlobalConfirmInfoBar::DelegateProxy(weak_factory_.GetWeakPtr())); |
| 204 GlobalConfirmInfoBar::DelegateProxy* proxy_ptr = proxy.get(); |
| 205 infobars::InfoBar* added_bar = infobar_service->AddInfoBar( |
| 206 infobar_service->CreateConfirmInfoBar(std::move(proxy))); |
| 207 |
| 208 proxy_ptr->info_bar_ = added_bar; |
| 209 DCHECK(added_bar); |
| 210 proxies_[infobar_service] = proxy_ptr; |
| 211 infobar_service->AddObserver(this); |
| 212 } |
| OLD | NEW |