Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(198)

Side by Side Diff: chrome/browser/tab_contents/tab_contents.cc

Issue 4767001: Make TabContents own its infobar delegates.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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/tab_contents/tab_contents.h" 5 #include "chrome/browser/tab_contents/tab_contents.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 8
9 #include "app/l10n_util.h" 9 #include "app/l10n_util.h"
10 #include "app/resource_bundle.h" 10 #include "app/resource_bundle.h"
(...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after
470 Source<TabContents>(this), 470 Source<TabContents>(this),
471 NotificationService::NoDetails()); 471 NotificationService::NoDetails());
472 472
473 // Notify any lasting InfobarDelegates that have not yet been removed that 473 // Notify any lasting InfobarDelegates that have not yet been removed that
474 // whatever infobar they were handling in this TabContents has closed, 474 // whatever infobar they were handling in this TabContents has closed,
475 // because the TabContents is going away entirely. 475 // because the TabContents is going away entirely.
476 // This must happen after the TAB_CONTENTS_DESTROYED notification as the 476 // This must happen after the TAB_CONTENTS_DESTROYED notification as the
477 // notification may trigger infobars calls that access their delegate. (and 477 // notification may trigger infobars calls that access their delegate. (and
478 // some implementations of InfoBarDelegate do delete themselves on 478 // some implementations of InfoBarDelegate do delete themselves on
479 // InfoBarClosed()). 479 // InfoBarClosed()).
480 for (int i = 0; i < infobar_delegate_count(); ++i) { 480 for (InfoBarDelegates::reverse_iterator i(infobar_delegates_.rbegin());
481 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i); 481 i != infobar_delegates_.rend(); ++i)
482 delegate->InfoBarClosed(); 482 (*i)->InfoBarClosed();
483 }
484 infobar_delegates_.clear(); 483 infobar_delegates_.clear();
485 484
486 // TODO(brettw) this should be moved to the view. 485 // TODO(brettw) this should be moved to the view.
487 #if defined(OS_WIN) 486 #if defined(OS_WIN)
488 // If we still have a window handle, destroy it. GetNativeView can return 487 // If we still have a window handle, destroy it. GetNativeView can return
489 // NULL if this contents was part of a window that closed. 488 // NULL if this contents was part of a window that closed.
490 if (GetNativeView()) 489 if (GetNativeView())
491 ::DestroyWindow(GetNativeView()); 490 ::DestroyWindow(GetNativeView());
492 #endif 491 #endif
493 492
(...skipping 565 matching lines...) Expand 10 before | Expand all | Expand 10 after
1059 return true; 1058 return true;
1060 return false; 1059 return false;
1061 } 1060 }
1062 1061
1063 void TabContents::SetFocusToLocationBar(bool select_all) { 1062 void TabContents::SetFocusToLocationBar(bool select_all) {
1064 if (delegate()) 1063 if (delegate())
1065 delegate()->SetFocusToLocationBar(select_all); 1064 delegate()->SetFocusToLocationBar(select_all);
1066 } 1065 }
1067 1066
1068 void TabContents::AddInfoBar(InfoBarDelegate* delegate) { 1067 void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
1068 DCHECK(delegate != NULL);
1069 if (delegate_ && !delegate_->infobars_enabled()) { 1069 if (delegate_ && !delegate_->infobars_enabled()) {
1070 delegate->InfoBarClosed(); 1070 delegate->InfoBarClosed();
1071 return; 1071 return;
1072 } 1072 }
1073 1073
1074 // Look through the existing InfoBarDelegates we have for a match. If we've 1074 // Look through the existing InfoBarDelegates we have for a match. If we've
1075 // already got one that matches, then we don't add the new one. 1075 // already got one that matches, then we don't add the new one.
1076 for (int i = 0; i < infobar_delegate_count(); ++i) { 1076 InfoBarDelegates::iterator i(std::find_if(infobar_delegates_.begin(),
1077 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate)) { 1077 infobar_delegates_.end(),
1078 // Tell the new infobar to close so that it can clean itself up. 1078 std::bind2nd(std::mem_fun(&InfoBarDelegate::EqualsDelegate), delegate)));
1079 delegate->InfoBarClosed(); 1079 if (i != infobar_delegates_.end()) {
1080 return; 1080 // Tell the new infobar to close so that it can clean itself up.
1081 } 1081 delegate->InfoBarClosed();
1082 return;
1082 } 1083 }
1083 1084
1084 infobar_delegates_.push_back(delegate); 1085 infobar_delegates_.push_back(delegate);
1085 NotificationService::current()->Notify( 1086 NotificationService::current()->Notify(
1086 NotificationType::TAB_CONTENTS_INFOBAR_ADDED, 1087 NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
1087 Source<TabContents>(this), 1088 Source<TabContents>(this), Details<InfoBarDelegate>(delegate));
1088 Details<InfoBarDelegate>(delegate));
1089 1089
1090 // Add ourselves as an observer for navigations the first time a delegate is 1090 // Add ourselves as an observer for navigations the first time a delegate is
1091 // added. We use this notification to expire InfoBars that need to expire on 1091 // added. We use this notification to expire InfoBars that need to expire on
1092 // page transitions. 1092 // page transitions.
1093 if (infobar_delegates_.size() == 1) { 1093 if (infobar_delegates_.size() == 1) {
1094 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, 1094 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
1095 Source<NavigationController>(&controller_)); 1095 Source<NavigationController>(&controller_));
1096 } 1096 }
1097 } 1097 }
1098 1098
1099 void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) { 1099 void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
1100 DCHECK(delegate != NULL);
1100 if (delegate_ && !delegate_->infobars_enabled()) { 1101 if (delegate_ && !delegate_->infobars_enabled()) {
1102 delegate->InfoBarClosed();
1101 return; 1103 return;
1102 } 1104 }
1103 1105
1104 std::vector<InfoBarDelegate*>::iterator it = 1106 InfoBarDelegates::iterator i(std::find(infobar_delegates_.begin(),
1105 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate); 1107 infobar_delegates_.end(), delegate));
1106 if (it != infobar_delegates_.end()) { 1108 DCHECK(i != infobar_delegates_.end());
1107 InfoBarDelegate* delegate = *it; 1109 NotificationService::current()->Notify(
1108 NotificationService::current()->Notify( 1110 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
1109 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED, 1111 Source<TabContents>(this), Details<InfoBarDelegate>(delegate));
1110 Source<TabContents>(this),
1111 Details<InfoBarDelegate>(delegate));
1112 1112
1113 // Just to be safe, make sure the delegate was not removed by an observer. 1113 // Just to be safe, make sure the delegate was not removed by an observer. If
1114 it = find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate); 1114 // you hit this DCHECK, please comment in http://crbug.com/50428 how you got
1115 if (it != infobar_delegates_.end()) { 1115 // there.
1116 infobar_delegates_.erase(it); 1116 i = std::find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
1117 // Remove ourselves as an observer if we are tracking no more InfoBars. 1117 DCHECK(i != infobar_delegates_.end());
1118 if (infobar_delegates_.empty()) { 1118 infobar_delegates_.erase(i);
1119 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED, 1119 delegate->InfoBarClosed();
1120 Source<NavigationController>(&controller_)); 1120
1121 } 1121 // Remove ourselves as an observer if we are tracking no more InfoBars.
1122 } else { 1122 if (infobar_delegates_.empty()) {
1123 // If you hit this NOTREACHED, please comment in bug 1123 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
1124 // http://crbug.com/50428 how you got there. 1124 Source<NavigationController>(&controller_));
1125 NOTREACHED();
1126 }
1127 } 1125 }
1128 } 1126 }
1129 1127
1130 void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate, 1128 void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
1131 InfoBarDelegate* new_delegate) { 1129 InfoBarDelegate* new_delegate) {
1130 DCHECK(old_delegate != NULL);
1131 DCHECK(new_delegate != NULL);
1132 if (delegate_ && !delegate_->infobars_enabled()) { 1132 if (delegate_ && !delegate_->infobars_enabled()) {
1133 new_delegate->InfoBarClosed(); 1133 new_delegate->InfoBarClosed();
1134 return; 1134 return;
1135 } 1135 }
1136 1136
1137 std::vector<InfoBarDelegate*>::iterator it = 1137 InfoBarDelegates::iterator i(std::find(infobar_delegates_.begin(),
1138 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate); 1138 infobar_delegates_.end(), old_delegate));
1139 DCHECK(it != infobar_delegates_.end()); 1139 DCHECK(i != infobar_delegates_.end());
1140 1140
1141 // Notify the container about the change of plans. 1141 // Notify the container about the change of plans.
1142 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details( 1142 typedef std::pair<InfoBarDelegate*, InfoBarDelegate*> Delegates;
1143 new std::pair<InfoBarDelegate*, InfoBarDelegate*>( 1143 scoped_ptr<Delegates> details(new Delegates(old_delegate, new_delegate));
1144 old_delegate, new_delegate));
1145 NotificationService::current()->Notify( 1144 NotificationService::current()->Notify(
1146 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED, 1145 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
1147 Source<TabContents>(this), 1146 Source<TabContents>(this), Details<Delegates>(details.get()));
1148 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
1149 1147
1150 // Just to be safe, make sure the delegate was not removed by an observer. 1148 // Just to be safe, make sure the delegate was not removed by an observer. If
1151 it = find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate); 1149 // you hit this DCHECK, please comment in http://crbug.com/50428 how you got
1152 if (it != infobar_delegates_.end()) { 1150 // there.
1153 // Remove the old one. 1151 i = std::find(infobar_delegates_.begin(), infobar_delegates_.end(),
1154 infobar_delegates_.erase(it); 1152 old_delegate);
1155 } else { 1153 DCHECK(i != infobar_delegates_.end());
1156 // If you hit this NOTREACHED, please comment in bug 1154 // Remove the old one.
1157 // http://crbug.com/50428 how you got there. 1155 infobar_delegates_.erase(i);
1158 NOTREACHED(); 1156 old_delegate->InfoBarClosed();
1159 }
1160 1157
1161 // Add the new one. 1158 // Add the new one.
1162 DCHECK(find(infobar_delegates_.begin(), 1159 DCHECK(std::find(infobar_delegates_.begin(), infobar_delegates_.end(),
1163 infobar_delegates_.end(), new_delegate) == 1160 new_delegate) == infobar_delegates_.end());
1164 infobar_delegates_.end());
1165 infobar_delegates_.push_back(new_delegate); 1161 infobar_delegates_.push_back(new_delegate);
1166 } 1162 }
1167 1163
1168 bool TabContents::ShouldShowBookmarkBar() { 1164 bool TabContents::ShouldShowBookmarkBar() {
1169 if (showing_interstitial_page()) 1165 if (showing_interstitial_page())
1170 return false; 1166 return false;
1171 1167
1172 // Do not show bookmarks bar if bookmarks aren't enabled. 1168 // Do not show bookmarks bar if bookmarks aren't enabled.
1173 if (!browser_defaults::bookmarks_enabled) 1169 if (!browser_defaults::bookmarks_enabled)
1174 return false; 1170 return false;
1175 1171
1176 // See GetDOMUIForCurrentState() comment for more info. This case is very 1172 // See GetDOMUIForCurrentState() comment for more info. This case is very
1177 // similar, but for non-first loads, we want to use the committed entry. This 1173 // similar, but for non-first loads, we want to use the committed entry. This
1178 // is so the bookmarks bar disappears at the same time the page does. 1174 // is so the bookmarks bar disappears at the same time the page does.
1179 if (controller_.GetLastCommittedEntry()) { 1175 if (controller_.GetLastCommittedEntry()) {
1180 // Not the first load, always use the committed DOM UI. 1176 // Not the first load, always use the committed DOM UI.
1181 if (render_manager_.dom_ui()) 1177 return (render_manager_.dom_ui() == NULL) ?
tfarina 2010/11/10 13:11:57 This change to ternary operator just make it *less
1182 return render_manager_.dom_ui()->force_bookmark_bar_visible(); 1178 false : render_manager_.dom_ui()->force_bookmark_bar_visible();
1183 return false; // Default.
1184 } 1179 }
1185 1180
1186 // When it's the first load, we know either the pending one or the committed 1181 // When it's the first load, we know either the pending one or the committed
1187 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one 1182 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1188 // of them will be valid, so we can just check both. 1183 // of them will be valid, so we can just check both.
1189 if (render_manager_.pending_dom_ui()) 1184 if (render_manager_.pending_dom_ui())
1190 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible(); 1185 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
1191 if (render_manager_.dom_ui()) 1186 return (render_manager_.dom_ui() == NULL) ?
1192 return render_manager_.dom_ui()->force_bookmark_bar_visible(); 1187 false : render_manager_.dom_ui()->force_bookmark_bar_visible();
1193 return false; // Default.
1194 } 1188 }
1195 1189
1196 void TabContents::ToolbarSizeChanged(bool is_animating) { 1190 void TabContents::ToolbarSizeChanged(bool is_animating) {
1197 TabContentsDelegate* d = delegate(); 1191 TabContentsDelegate* d = delegate();
1198 if (d) 1192 if (d)
1199 d->ToolbarSizeChanged(this, is_animating); 1193 d->ToolbarSizeChanged(this, is_animating);
1200 } 1194 }
1201 1195
1202 bool TabContents::CanDownload(int request_id) { 1196 bool TabContents::CanDownload(int request_id) {
1203 TabContentsDelegate* d = delegate(); 1197 TabContentsDelegate* d = delegate();
1204 if (d) 1198 if (d)
1205 return d->CanDownload(request_id); 1199 return d->CanDownload(request_id);
1206 return true; 1200 return true;
1207 } 1201 }
1208 1202
1209 void TabContents::OnStartDownload(DownloadItem* download) { 1203 void TabContents::OnStartDownload(DownloadItem* download) {
1210 DCHECK(download); 1204 DCHECK(download);
1211 1205
1212 // Download in a constrained popup is shown in the tab that opened it. 1206 // Download in a constrained popup is shown in the tab that opened it.
1213 TabContents* tab_contents = delegate()->GetConstrainingContents(this); 1207 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
1214 1208
1215 if (tab_contents && tab_contents->delegate()) 1209 if (tab_contents && tab_contents->delegate())
1216 tab_contents->delegate()->OnStartDownload(download, this); 1210 tab_contents->delegate()->OnStartDownload(download, this);
1217 } 1211 }
1218 1212
1219 void TabContents::WillClose(ConstrainedWindow* window) { 1213 void TabContents::WillClose(ConstrainedWindow* window) {
1220 ConstrainedWindowList::iterator it = 1214 ConstrainedWindowList::iterator i(
1221 find(child_windows_.begin(), child_windows_.end(), window); 1215 std::find(child_windows_.begin(), child_windows_.end(), window));
1222 bool removed_topmost_window = it == child_windows_.begin(); 1216 bool removed_topmost_window = (i == child_windows_.begin());
1223 if (it != child_windows_.end()) 1217 if (i != child_windows_.end())
1224 child_windows_.erase(it); 1218 child_windows_.erase(i);
1225 if (child_windows_.size() > 0) { 1219 if (child_windows_.empty()) {
1226 if (removed_topmost_window) { 1220 BlockTabContent(false);
1221 } else {
1222 if (removed_topmost_window)
1227 child_windows_[0]->ShowConstrainedWindow(); 1223 child_windows_[0]->ShowConstrainedWindow();
1228 }
1229 BlockTabContent(true); 1224 BlockTabContent(true);
1230 } else {
1231 BlockTabContent(false);
1232 } 1225 }
1233 } 1226 }
1234 1227
1235 void TabContents::WillCloseBlockedContentContainer( 1228 void TabContents::WillCloseBlockedContentContainer(
1236 BlockedContentContainer* container) { 1229 BlockedContentContainer* container) {
1237 DCHECK(blocked_contents_ == container); 1230 DCHECK(blocked_contents_ == container);
1238 blocked_contents_ = NULL; 1231 blocked_contents_ = NULL;
1239 PopupNotificationVisibilityChanged(false); 1232 PopupNotificationVisibilityChanged(false);
1240 } 1233 }
1241 1234
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after
1552 } 1545 }
1553 1546
1554 void TabContents::ExpireInfoBars( 1547 void TabContents::ExpireInfoBars(
1555 const NavigationController::LoadCommittedDetails& details) { 1548 const NavigationController::LoadCommittedDetails& details) {
1556 // Only hide InfoBars when the user has done something that makes the main 1549 // Only hide InfoBars when the user has done something that makes the main
1557 // frame load. We don't want various automatic or subframe navigations making 1550 // frame load. We don't want various automatic or subframe navigations making
1558 // it disappear. 1551 // it disappear.
1559 if (!details.is_user_initiated_main_frame_load()) 1552 if (!details.is_user_initiated_main_frame_load())
1560 return; 1553 return;
1561 1554
1562 for (int i = infobar_delegate_count() - 1; i >= 0; --i) { 1555 for (InfoBarDelegates::reverse_iterator i(infobar_delegates_.rbegin());
1563 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i); 1556 i != infobar_delegates_.rend(); ++i) {
1564 if (!delegate) { 1557 if ((*i)->ShouldExpire(details))
1565 // If you hit this NOTREACHED, please comment in bug 1558 RemoveInfoBar(*i);
1566 // http://crbug.com/50428 how you got there.
1567 NOTREACHED();
1568 continue;
1569 }
1570
1571 if (delegate->ShouldExpire(details))
1572 RemoveInfoBar(delegate);
1573 } 1559 }
1574 } 1560 }
1575 1561
1576 DOMUI* TabContents::GetDOMUIForCurrentState() { 1562 DOMUI* TabContents::GetDOMUIForCurrentState() {
1577 // When there is a pending navigation entry, we want to use the pending DOMUI 1563 // When there is a pending navigation entry, we want to use the pending DOMUI
1578 // that goes along with it to control the basic flags. For example, we want to 1564 // that goes along with it to control the basic flags. For example, we want to
1579 // show the pending URL in the URL bar, so we want the display_url flag to 1565 // show the pending URL in the URL bar, so we want the display_url flag to
1580 // be from the pending entry. 1566 // be from the pending entry.
1581 // 1567 //
1582 // The confusion comes because there are multiple possibilities for the 1568 // The confusion comes because there are multiple possibilities for the
(...skipping 830 matching lines...) Expand 10 before | Expand all | Expand 10 after
2413 if (rvh != render_view_host()) { 2399 if (rvh != render_view_host()) {
2414 // The pending page's RenderViewHost is gone. 2400 // The pending page's RenderViewHost is gone.
2415 return; 2401 return;
2416 } 2402 }
2417 2403
2418 SetIsLoading(false, NULL); 2404 SetIsLoading(false, NULL);
2419 NotifyDisconnected(); 2405 NotifyDisconnected();
2420 SetIsCrashed(true); 2406 SetIsCrashed(true);
2421 2407
2422 // Remove all infobars. 2408 // Remove all infobars.
2423 for (int i = infobar_delegate_count() - 1; i >=0 ; --i) 2409 while (!infobar_delegates_.empty())
tfarina 2010/11/10 13:11:57 What is wrong with the for loop? I guess this is s
2424 RemoveInfoBar(GetInfoBarDelegateAt(i)); 2410 RemoveInfoBar(infobar_delegates_.front());
2425 2411
2426 // Tell the view that we've crashed so it can prepare the sad tab page. 2412 // Tell the view that we've crashed so it can prepare the sad tab page.
2427 // Only do this if we're not in browser shutdown, so that TabContents 2413 // Only do this if we're not in browser shutdown, so that TabContents
2428 // objects that are not in a browser (e.g., HTML dialogs) and thus are 2414 // objects that are not in a browser (e.g., HTML dialogs) and thus are
2429 // visible do not flash a sad tab page. 2415 // visible do not flash a sad tab page.
2430 if (browser_shutdown::GetShutdownType() == browser_shutdown::NOT_VALID) 2416 if (browser_shutdown::GetShutdownType() == browser_shutdown::NOT_VALID)
2431 view_->OnTabCrashed(); 2417 view_->OnTabCrashed();
2432 2418
2433 // Hide any visible hung renderer warning for this web contents' process. 2419 // Hide any visible hung renderer warning for this web contents' process.
2434 hung_renderer_dialog::HideForTabContents(this); 2420 hung_renderer_dialog::HideForTabContents(this);
(...skipping 898 matching lines...) Expand 10 before | Expand all | Expand 10 after
3333 AddInfoBar(new SavePasswordInfoBarDelegate(this, form_to_save)); 3319 AddInfoBar(new SavePasswordInfoBarDelegate(this, form_to_save));
3334 } 3320 }
3335 3321
3336 Profile* TabContents::GetProfileForPasswordManager() { 3322 Profile* TabContents::GetProfileForPasswordManager() {
3337 return profile(); 3323 return profile();
3338 } 3324 }
3339 3325
3340 bool TabContents::DidLastPageLoadEncounterSSLErrors() { 3326 bool TabContents::DidLastPageLoadEncounterSSLErrors() {
3341 return controller().ssl_manager()->ProcessedSSLErrorFromRequest(); 3327 return controller().ssl_manager()->ProcessedSSLErrorFromRequest();
3342 } 3328 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698