license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | f3ec774 | 2009-01-15 00:59:16 | [diff] [blame] | 5 | #include "chrome/browser/tab_contents/tab_contents.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
| 7 | #include "chrome/browser/cert_store.h" |
[email protected] | bcef0dc0 | 2009-02-28 00:35:02 | [diff] [blame] | 8 | #include "chrome/browser/download/download_item_model.h" |
| 9 | #include "chrome/browser/download/download_shelf.h" |
[email protected] | f3ec774 | 2009-01-15 00:59:16 | [diff] [blame] | 10 | #include "chrome/browser/tab_contents/navigation_entry.h" |
| 11 | #include "chrome/browser/tab_contents/tab_contents_delegate.h" |
| 12 | #include "chrome/browser/tab_contents/web_contents.h" |
[email protected] | a4feef8 | 2008-10-02 15:11:22 | [diff] [blame] | 13 | #include "chrome/common/l10n_util.h" |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 14 | #include "chrome/common/notification_service.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 15 | #include "chrome/common/pref_names.h" |
[email protected] | 1eb89e8 | 2008-08-15 12:27:03 | [diff] [blame] | 16 | #include "chrome/common/pref_service.h" |
[email protected] | 34ac8f3 | 2009-02-22 23:03:27 | [diff] [blame] | 17 | #include "grit/generated_resources.h" |
[email protected] | 3e45ba9 | 2009-02-20 21:09:00 | [diff] [blame] | 18 | |
| 19 | #if defined(OS_WIN) |
| 20 | // TODO(port): some of these headers should be ported. |
| 21 | #include "chrome/browser/tab_contents/infobar_delegate.h" |
[email protected] | 3e45ba9 | 2009-02-20 21:09:00 | [diff] [blame] | 22 | #include "chrome/browser/views/download_started_animation.h" |
| 23 | #include "chrome/browser/views/blocked_popup_container.h" |
[email protected] | 6f32909 | 2009-03-17 04:56:55 | [diff] [blame] | 24 | #include "chrome/views/controls/scrollbar/native_scroll_bar.h" |
[email protected] | 3e45ba9 | 2009-02-20 21:09:00 | [diff] [blame] | 25 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 26 | |
[email protected] | 3e45ba9 | 2009-02-20 21:09:00 | [diff] [blame] | 27 | #if defined(OS_WIN) |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 28 | namespace { |
| 29 | |
| 30 | BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) { |
[email protected] | 4d0bd10 | 2008-10-16 00:26:30 | [diff] [blame] | 31 | // Note: erase is required to properly paint some widgets borders. This can |
| 32 | // be seen with textfields. |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 33 | InvalidateRect(hwnd, NULL, TRUE); |
| 34 | return TRUE; |
| 35 | } |
| 36 | |
| 37 | } // namespace |
[email protected] | 332af773 | 2009-03-11 13:21:35 | [diff] [blame] | 38 | #endif |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 39 | |
[email protected] | b680ad2 | 2009-04-15 23:19:42 | [diff] [blame^] | 40 | TabContents::TabContents() |
| 41 | : delegate_(NULL), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 42 | controller_(NULL), |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 43 | is_loading_(false), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 44 | is_crashed_(false), |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 45 | waiting_for_response_(false), |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 46 | shelf_visible_(false), |
| 47 | max_page_id_(-1), |
[email protected] | 2d843e6 | 2008-11-26 21:16:35 | [diff] [blame] | 48 | capturing_contents_(false), |
[email protected] | 332af773 | 2009-03-11 13:21:35 | [diff] [blame] | 49 | blocked_popups_(NULL), |
[email protected] | 2d843e6 | 2008-11-26 21:16:35 | [diff] [blame] | 50 | is_being_destroyed_(false) { |
[email protected] | 332af773 | 2009-03-11 13:21:35 | [diff] [blame] | 51 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 52 | |
| 53 | TabContents::~TabContents() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 54 | } |
| 55 | |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 56 | // static |
| 57 | void TabContents::RegisterUserPrefs(PrefService* prefs) { |
| 58 | prefs->RegisterBooleanPref(prefs::kBlockPopups, false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 59 | } |
| 60 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 61 | void TabContents::CloseContents() { |
| 62 | // Destroy our NavigationController, which will Destroy all tabs it owns. |
| 63 | controller_->Destroy(); |
| 64 | // Note that the controller may have deleted us at this point, |
| 65 | // so don't touch any member variables here. |
| 66 | } |
| 67 | |
| 68 | void TabContents::Destroy() { |
[email protected] | 9501428d | 2008-11-27 01:44:44 | [diff] [blame] | 69 | DCHECK(!is_being_destroyed_); |
[email protected] | 2d843e6 | 2008-11-26 21:16:35 | [diff] [blame] | 70 | is_being_destroyed_ = true; |
| 71 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 72 | // First cleanly close all child windows. |
| 73 | // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked |
| 74 | // some of these to close. CloseWindows is async, so it might get called |
| 75 | // twice before it runs. |
| 76 | int size = static_cast<int>(child_windows_.size()); |
| 77 | for (int i = size - 1; i >= 0; --i) { |
| 78 | ConstrainedWindow* window = child_windows_[i]; |
| 79 | if (window) |
| 80 | window->CloseConstrainedWindow(); |
| 81 | } |
| 82 | |
[email protected] | b618713 | 2009-01-21 23:20:48 | [diff] [blame] | 83 | // Notify any lasting InfobarDelegates that have not yet been removed that |
| 84 | // whatever infobar they were handling in this TabContents has closed, |
| 85 | // because the TabContents is going away entirely. |
| 86 | for (int i = 0; i < infobar_delegate_count(); ++i) { |
| 87 | InfoBarDelegate* delegate = GetInfoBarDelegateAt(i); |
| 88 | delegate->InfoBarClosed(); |
| 89 | } |
| 90 | infobar_delegates_.clear(); |
| 91 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 92 | // Notify any observer that have a reference on this tab contents. |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 93 | NotificationService::current()->Notify( |
| 94 | NotificationType::TAB_CONTENTS_DESTROYED, |
| 95 | Source<TabContents>(this), |
| 96 | NotificationService::NoDetails()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 97 | |
[email protected] | 76f68dc | 2009-03-10 20:31:32 | [diff] [blame] | 98 | #if defined(OS_WIN) |
[email protected] | 92edc47 | 2009-02-10 20:32:06 | [diff] [blame] | 99 | // If we still have a window handle, destroy it. GetNativeView can return |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 100 | // NULL if this contents was part of a window that closed. |
[email protected] | 92edc47 | 2009-02-10 20:32:06 | [diff] [blame] | 101 | if (GetNativeView()) |
| 102 | ::DestroyWindow(GetNativeView()); |
[email protected] | 76f68dc | 2009-03-10 20:31:32 | [diff] [blame] | 103 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 104 | |
| 105 | // Notify our NavigationController. Make sure we are deleted first, so |
| 106 | // that the controller is the last to die. |
| 107 | NavigationController* controller = controller_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 108 | |
| 109 | delete this; |
| 110 | |
[email protected] | 7f0005a | 2009-04-15 03:25:11 | [diff] [blame] | 111 | controller->TabContentsWasDestroyed(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 112 | } |
| 113 | |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 114 | void TabContents::SetupController(Profile* profile) { |
| 115 | DCHECK(!controller_); |
| 116 | controller_ = new NavigationController(this, profile); |
| 117 | } |
| 118 | |
| 119 | bool TabContents::SupportsURL(GURL* url) { |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 120 | return true; |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | const GURL& TabContents::GetURL() const { |
| 124 | // We may not have a navigation entry yet |
| 125 | NavigationEntry* entry = controller_->GetActiveEntry(); |
| 126 | return entry ? entry->display_url() : GURL::EmptyGURL(); |
| 127 | } |
| 128 | |
[email protected] | 4c4d8d2 | 2009-03-04 05:29:27 | [diff] [blame] | 129 | const string16& TabContents::GetTitle() const { |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 130 | // We use the title for the last committed entry rather than a pending |
| 131 | // navigation entry. For example, when the user types in a URL, we want to |
| 132 | // keep the old page's title until the new load has committed and we get a new |
| 133 | // title. |
| 134 | // The exception is with transient pages, for which we really want to use |
| 135 | // their title, as they are not committed. |
| 136 | NavigationEntry* entry = controller_->GetTransientEntry(); |
[email protected] | 3d627bbc | 2008-10-23 20:49:07 | [diff] [blame] | 137 | if (entry) |
[email protected] | 4c4d8d2 | 2009-03-04 05:29:27 | [diff] [blame] | 138 | return entry->GetTitleForDisplay(controller_); |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 139 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 140 | entry = controller_->GetLastCommittedEntry(); |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 141 | if (entry) |
[email protected] | 4c4d8d2 | 2009-03-04 05:29:27 | [diff] [blame] | 142 | return entry->GetTitleForDisplay(controller_); |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 143 | else if (controller_->LoadingURLLazily()) |
| 144 | return controller_->GetLazyTitle(); |
[email protected] | 4c4d8d2 | 2009-03-04 05:29:27 | [diff] [blame] | 145 | return EmptyString16(); |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | int32 TabContents::GetMaxPageID() { |
| 149 | if (GetSiteInstance()) |
| 150 | return GetSiteInstance()->max_page_id(); |
| 151 | else |
| 152 | return max_page_id_; |
| 153 | } |
| 154 | |
| 155 | void TabContents::UpdateMaxPageID(int32 page_id) { |
| 156 | // Ensure both the SiteInstance and RenderProcessHost update their max page |
| 157 | // IDs in sync. Only WebContents will also have site instances, except during |
| 158 | // testing. |
| 159 | if (GetSiteInstance()) |
| 160 | GetSiteInstance()->UpdateMaxPageID(page_id); |
| 161 | |
| 162 | if (AsWebContents()) |
| 163 | AsWebContents()->process()->UpdateMaxPageID(page_id); |
| 164 | else |
| 165 | max_page_id_ = std::max(max_page_id_, page_id); |
| 166 | } |
| 167 | |
| 168 | const std::wstring TabContents::GetDefaultTitle() const { |
| 169 | return l10n_util::GetString(IDS_DEFAULT_TAB_TITLE); |
| 170 | } |
| 171 | |
| 172 | SkBitmap TabContents::GetFavIcon() const { |
| 173 | // Like GetTitle(), we also want to use the favicon for the last committed |
| 174 | // entry rather than a pending navigation entry. |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 175 | NavigationEntry* entry = controller_->GetTransientEntry(); |
| 176 | if (entry) |
| 177 | return entry->favicon().bitmap(); |
| 178 | |
| 179 | entry = controller_->GetLastCommittedEntry(); |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 180 | if (entry) |
| 181 | return entry->favicon().bitmap(); |
| 182 | else if (controller_->LoadingURLLazily()) |
| 183 | return controller_->GetLazyFavIcon(); |
| 184 | return SkBitmap(); |
| 185 | } |
| 186 | |
[email protected] | 3e45ba9 | 2009-02-20 21:09:00 | [diff] [blame] | 187 | #if defined(OS_WIN) |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 188 | SecurityStyle TabContents::GetSecurityStyle() const { |
| 189 | // We may not have a navigation entry yet. |
| 190 | NavigationEntry* entry = controller_->GetActiveEntry(); |
| 191 | return entry ? entry->ssl().security_style() : SECURITY_STYLE_UNKNOWN; |
| 192 | } |
| 193 | |
| 194 | bool TabContents::GetSSLEVText(std::wstring* ev_text, |
| 195 | std::wstring* ev_tooltip_text) const { |
| 196 | DCHECK(ev_text && ev_tooltip_text); |
| 197 | ev_text->clear(); |
| 198 | ev_tooltip_text->clear(); |
| 199 | |
| 200 | NavigationEntry* entry = controller_->GetActiveEntry(); |
| 201 | if (!entry || |
| 202 | net::IsCertStatusError(entry->ssl().cert_status()) || |
| 203 | ((entry->ssl().cert_status() & net::CERT_STATUS_IS_EV) == 0)) |
| 204 | return false; |
| 205 | |
| 206 | scoped_refptr<net::X509Certificate> cert; |
| 207 | CertStore::GetSharedInstance()->RetrieveCert(entry->ssl().cert_id(), &cert); |
| 208 | if (!cert.get()) { |
| 209 | NOTREACHED(); |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | return SSLManager::GetEVCertNames(*cert, ev_text, ev_tooltip_text); |
| 214 | } |
[email protected] | 76f68dc | 2009-03-10 20:31:32 | [diff] [blame] | 215 | #endif |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 216 | |
| 217 | void TabContents::SetIsCrashed(bool state) { |
| 218 | if (state == is_crashed_) |
| 219 | return; |
| 220 | |
| 221 | is_crashed_ = state; |
| 222 | if (delegate_) |
| 223 | delegate_->ContentsStateChanged(this); |
| 224 | } |
| 225 | |
| 226 | void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) { |
| 227 | if (delegate_) |
| 228 | delegate_->NavigationStateChanged(this, changed_flags); |
| 229 | } |
| 230 | |
| 231 | void TabContents::DidBecomeSelected() { |
| 232 | if (controller_) |
| 233 | controller_->SetActive(true); |
| 234 | |
[email protected] | 76f68dc | 2009-03-10 20:31:32 | [diff] [blame] | 235 | #if defined(OS_WIN) |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 236 | // Invalidate all descendants. (take care to exclude invalidating ourselves!) |
[email protected] | 92edc47 | 2009-02-10 20:32:06 | [diff] [blame] | 237 | EnumChildWindows(GetNativeView(), InvalidateWindow, 0); |
[email protected] | 76f68dc | 2009-03-10 20:31:32 | [diff] [blame] | 238 | #endif |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | void TabContents::WasHidden() { |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 242 | NotificationService::current()->Notify( |
| 243 | NotificationType::TAB_CONTENTS_HIDDEN, |
| 244 | Source<TabContents>(this), |
| 245 | NotificationService::NoDetails()); |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | void TabContents::Activate() { |
| 249 | if (delegate_) |
| 250 | delegate_->ActivateContents(this); |
| 251 | } |
| 252 | |
[email protected] | c058805 | 2008-10-27 23:01:50 | [diff] [blame] | 253 | void TabContents::OpenURL(const GURL& url, const GURL& referrer, |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 254 | WindowOpenDisposition disposition, |
| 255 | PageTransition::Type transition) { |
| 256 | if (delegate_) |
[email protected] | c058805 | 2008-10-27 23:01:50 | [diff] [blame] | 257 | delegate_->OpenURLFromTab(this, url, referrer, disposition, transition); |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | bool TabContents::NavigateToPendingEntry(bool reload) { |
| 261 | // Our benavior is just to report that the entry was committed. |
[email protected] | 76f68dc | 2009-03-10 20:31:32 | [diff] [blame] | 262 | string16 default_title = WideToUTF16Hack(GetDefaultTitle()); |
[email protected] | 7f0005a | 2009-04-15 03:25:11 | [diff] [blame] | 263 | controller()->pending_entry()->set_title(default_title); |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 264 | controller()->CommitPendingEntry(); |
| 265 | return true; |
| 266 | } |
| 267 | |
[email protected] | 76f68dc | 2009-03-10 20:31:32 | [diff] [blame] | 268 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 269 | ConstrainedWindow* TabContents::CreateConstrainedDialog( |
[email protected] | c2dacc9 | 2008-10-16 23:51:38 | [diff] [blame] | 270 | views::WindowDelegate* window_delegate, |
| 271 | views::View* contents_view) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 272 | ConstrainedWindow* window = |
| 273 | ConstrainedWindow::CreateConstrainedDialog( |
| 274 | this, gfx::Rect(), contents_view, window_delegate); |
| 275 | child_windows_.push_back(window); |
| 276 | return window; |
| 277 | } |
[email protected] | f3332e1 | 2009-03-23 15:28:57 | [diff] [blame] | 278 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 279 | |
| 280 | void TabContents::AddNewContents(TabContents* new_contents, |
| 281 | WindowOpenDisposition disposition, |
| 282 | const gfx::Rect& initial_pos, |
| 283 | bool user_gesture) { |
| 284 | if (!delegate_) |
| 285 | return; |
| 286 | |
[email protected] | f3332e1 | 2009-03-23 15:28:57 | [diff] [blame] | 287 | #if defined(OS_WIN) |
[email protected] | 3b0a45e8 | 2008-10-13 21:01:03 | [diff] [blame] | 288 | if ((disposition == NEW_POPUP) && !user_gesture) { |
| 289 | // Unrequested popups from normal pages are constrained. |
| 290 | TabContents* popup_owner = this; |
| 291 | TabContents* our_owner = delegate_->GetConstrainingContents(this); |
| 292 | if (our_owner) |
| 293 | popup_owner = our_owner; |
| 294 | popup_owner->AddConstrainedPopup(new_contents, initial_pos); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 295 | } else { |
[email protected] | 0aa5531 | 2008-10-17 21:53:08 | [diff] [blame] | 296 | new_contents->DisassociateFromPopupCount(); |
| 297 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 298 | delegate_->AddNewContents(this, new_contents, disposition, initial_pos, |
| 299 | user_gesture); |
[email protected] | 634a6f9 | 2008-12-01 21:39:31 | [diff] [blame] | 300 | |
| 301 | PopupNotificationVisibilityChanged(ShowingBlockedPopupNotification()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 302 | } |
[email protected] | f3332e1 | 2009-03-23 15:28:57 | [diff] [blame] | 303 | #else |
| 304 | // TODO(port): implement the popup blocker stuff |
| 305 | delegate_->AddNewContents(this, new_contents, disposition, initial_pos, |
| 306 | user_gesture); |
| 307 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 308 | } |
| 309 | |
[email protected] | f3332e1 | 2009-03-23 15:28:57 | [diff] [blame] | 310 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 311 | void TabContents::AddConstrainedPopup(TabContents* new_contents, |
| 312 | const gfx::Rect& initial_pos) { |
[email protected] | d6598c05 | 2008-11-05 19:03:25 | [diff] [blame] | 313 | if (!blocked_popups_) { |
| 314 | CRect client_rect; |
[email protected] | 92edc47 | 2009-02-10 20:32:06 | [diff] [blame] | 315 | GetClientRect(GetNativeView(), &client_rect); |
[email protected] | d6598c05 | 2008-11-05 19:03:25 | [diff] [blame] | 316 | gfx::Point anchor_position( |
| 317 | client_rect.Width() - |
| 318 | views::NativeScrollBar::GetVerticalScrollBarWidth(), |
| 319 | client_rect.Height()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 320 | |
[email protected] | d6598c05 | 2008-11-05 19:03:25 | [diff] [blame] | 321 | blocked_popups_ = BlockedPopupContainer::Create( |
| 322 | this, profile(), anchor_position); |
| 323 | child_windows_.push_back(blocked_popups_); |
| 324 | } |
| 325 | |
| 326 | blocked_popups_->AddTabContents(new_contents, initial_pos); |
[email protected] | 634a6f9 | 2008-12-01 21:39:31 | [diff] [blame] | 327 | PopupNotificationVisibilityChanged(ShowingBlockedPopupNotification()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 328 | } |
| 329 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 330 | void TabContents::CloseAllSuppressedPopups() { |
[email protected] | d6598c05 | 2008-11-05 19:03:25 | [diff] [blame] | 331 | if (blocked_popups_) |
| 332 | blocked_popups_->CloseAllPopups(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 333 | } |
[email protected] | 332af773 | 2009-03-11 13:21:35 | [diff] [blame] | 334 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 335 | |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 336 | void TabContents::AddInfoBar(InfoBarDelegate* delegate) { |
| 337 | // Look through the existing InfoBarDelegates we have for a match. If we've |
| 338 | // already got one that matches, then we don't add the new one. |
[email protected] | f86a0702 | 2008-11-25 01:06:05 | [diff] [blame] | 339 | for (int i = 0; i < infobar_delegate_count(); ++i) { |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 340 | if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate)) |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | infobar_delegates_.push_back(delegate); |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 345 | NotificationService::current()->Notify( |
| 346 | NotificationType::TAB_CONTENTS_INFOBAR_ADDED, |
| 347 | Source<TabContents>(this), |
| 348 | Details<InfoBarDelegate>(delegate)); |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 349 | |
| 350 | // Add ourselves as an observer for navigations the first time a delegate is |
| 351 | // added. We use this notification to expire InfoBars that need to expire on |
| 352 | // page transitions. |
| 353 | if (infobar_delegates_.size() == 1) { |
[email protected] | 6a02963e | 2009-01-06 16:58:03 | [diff] [blame] | 354 | DCHECK(controller()); |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 355 | registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, |
[email protected] | 6a02963e | 2009-01-06 16:58:03 | [diff] [blame] | 356 | Source<NavigationController>(controller())); |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
| 360 | void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) { |
| 361 | std::vector<InfoBarDelegate*>::iterator it = |
| 362 | find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate); |
| 363 | if (it != infobar_delegates_.end()) { |
| 364 | InfoBarDelegate* delegate = *it; |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 365 | NotificationService::current()->Notify( |
| 366 | NotificationType::TAB_CONTENTS_INFOBAR_REMOVED, |
| 367 | Source<TabContents>(this), |
| 368 | Details<InfoBarDelegate>(delegate)); |
[email protected] | f86a0702 | 2008-11-25 01:06:05 | [diff] [blame] | 369 | infobar_delegates_.erase(it); |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 370 | |
[email protected] | 6a02963e | 2009-01-06 16:58:03 | [diff] [blame] | 371 | // Remove ourselves as an observer if we are tracking no more InfoBars. |
| 372 | if (infobar_delegates_.empty()) { |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 373 | registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED, |
[email protected] | 6a02963e | 2009-01-06 16:58:03 | [diff] [blame] | 374 | Source<NavigationController>(controller())); |
| 375 | } |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 376 | } |
| 377 | } |
[email protected] | 3edd952 | 2009-03-04 22:19:41 | [diff] [blame] | 378 | |
| 379 | void TabContents::ToolbarSizeChanged(bool is_animating) { |
| 380 | TabContentsDelegate* d = delegate(); |
| 381 | if (d) |
| 382 | d->ToolbarSizeChanged(this, is_animating); |
| 383 | } |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 384 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 385 | void TabContents::SetDownloadShelfVisible(bool visible) { |
| 386 | if (shelf_visible_ != visible) { |
| 387 | if (visible) { |
[email protected] | bcef0dc0 | 2009-02-28 00:35:02 | [diff] [blame] | 388 | // Invoke GetDownloadShelf to force the shelf to be created. |
| 389 | GetDownloadShelf(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 390 | } |
| 391 | shelf_visible_ = visible; |
| 392 | |
[email protected] | 019d8350 | 2008-07-30 22:44:50 | [diff] [blame] | 393 | if (delegate_) |
| 394 | delegate_->ContentsStateChanged(this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 395 | } |
[email protected] | ce2390b68 | 2008-08-08 22:24:51 | [diff] [blame] | 396 | |
| 397 | // SetShelfVisible can force-close the shelf, so make sure we lay out |
| 398 | // everything correctly, as if the animation had finished. This doesn't |
| 399 | // matter for showing the shelf, as the show animation will do it. |
| 400 | ToolbarSizeChanged(false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 401 | } |
| 402 | |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 403 | void TabContents::OnStartDownload(DownloadItem* download) { |
| 404 | DCHECK(download); |
| 405 | TabContents* tab_contents = this; |
| 406 | |
[email protected] | 3edd952 | 2009-03-04 22:19:41 | [diff] [blame] | 407 | // TODO(port): port contraining contents. |
| 408 | #if defined(OS_WIN) |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 409 | // Download in a constrained popup is shown in the tab that opened it. |
| 410 | TabContents* constraining_tab = delegate()->GetConstrainingContents(this); |
| 411 | if (constraining_tab) |
| 412 | tab_contents = constraining_tab; |
[email protected] | 3edd952 | 2009-03-04 22:19:41 | [diff] [blame] | 413 | #endif |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 414 | |
[email protected] | bcef0dc0 | 2009-02-28 00:35:02 | [diff] [blame] | 415 | // GetDownloadShelf creates the download shelf if it was not yet created. |
| 416 | tab_contents->GetDownloadShelf()->AddDownload( |
| 417 | new DownloadItemModel(download)); |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 418 | tab_contents->SetDownloadShelfVisible(true); |
| 419 | |
[email protected] | 3edd952 | 2009-03-04 22:19:41 | [diff] [blame] | 420 | // TODO(port): port animatinos. |
| 421 | #if defined(OS_WIN) |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 422 | // This animation will delete itself when it finishes, or if we become hidden |
| 423 | // or destroyed. |
[email protected] | 92edc47 | 2009-02-10 20:32:06 | [diff] [blame] | 424 | if (IsWindowVisible(GetNativeView())) { // For minimized windows, unit |
| 425 | // tests, etc. |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 426 | new DownloadStartedAnimation(tab_contents); |
| 427 | } |
[email protected] | 3edd952 | 2009-03-04 22:19:41 | [diff] [blame] | 428 | #endif |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 429 | } |
| 430 | |
[email protected] | bcef0dc0 | 2009-02-28 00:35:02 | [diff] [blame] | 431 | DownloadShelf* TabContents::GetDownloadShelf() { |
| 432 | if (!download_shelf_.get()) |
| 433 | download_shelf_.reset(DownloadShelf::Create(this)); |
| 434 | return download_shelf_.get(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 435 | } |
| 436 | |
[email protected] | bcef0dc0 | 2009-02-28 00:35:02 | [diff] [blame] | 437 | void TabContents::MigrateShelfFrom(TabContents* tab_contents) { |
| 438 | download_shelf_.reset(tab_contents->GetDownloadShelf()); |
| 439 | download_shelf_->ChangeTabContents(tab_contents, this); |
| 440 | tab_contents->ReleaseDownloadShelf(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 441 | } |
| 442 | |
[email protected] | 3edd952 | 2009-03-04 22:19:41 | [diff] [blame] | 443 | void TabContents::ReleaseDownloadShelf() { |
| 444 | download_shelf_.release(); |
| 445 | } |
| 446 | |
| 447 | // static |
| 448 | void TabContents::MigrateShelf(TabContents* from, TabContents* to) { |
| 449 | bool was_shelf_visible = from->IsDownloadShelfVisible(); |
| 450 | if (was_shelf_visible) |
| 451 | to->MigrateShelfFrom(from); |
| 452 | to->SetDownloadShelfVisible(was_shelf_visible); |
| 453 | } |
[email protected] | 3edd952 | 2009-03-04 22:19:41 | [diff] [blame] | 454 | |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 455 | void TabContents::WillClose(ConstrainedWindow* window) { |
| 456 | ConstrainedWindowList::iterator it = |
| 457 | find(child_windows_.begin(), child_windows_.end(), window); |
| 458 | if (it != child_windows_.end()) |
| 459 | child_windows_.erase(it); |
| 460 | |
[email protected] | 332af773 | 2009-03-11 13:21:35 | [diff] [blame] | 461 | #if defined(OS_WIN) |
[email protected] | d6598c05 | 2008-11-05 19:03:25 | [diff] [blame] | 462 | if (window == blocked_popups_) |
| 463 | blocked_popups_ = NULL; |
| 464 | |
[email protected] | 92edc47 | 2009-02-10 20:32:06 | [diff] [blame] | 465 | if (::IsWindow(GetNativeView())) { |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 466 | CRect client_rect; |
[email protected] | 92edc47 | 2009-02-10 20:32:06 | [diff] [blame] | 467 | GetClientRect(GetNativeView(), &client_rect); |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 468 | RepositionSupressedPopupsToFit( |
| 469 | gfx::Size(client_rect.Width(), client_rect.Height())); |
| 470 | } |
[email protected] | 332af773 | 2009-03-11 13:21:35 | [diff] [blame] | 471 | #endif |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 472 | } |
| 473 | |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 474 | void TabContents::DidMoveOrResize(ConstrainedWindow* window) { |
[email protected] | 332af773 | 2009-03-11 13:21:35 | [diff] [blame] | 475 | #if defined(OS_WIN) |
[email protected] | 92edc47 | 2009-02-10 20:32:06 | [diff] [blame] | 476 | UpdateWindow(GetNativeView()); |
[email protected] | 332af773 | 2009-03-11 13:21:35 | [diff] [blame] | 477 | #endif |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 478 | } |
| 479 | |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 480 | void TabContents::Observe(NotificationType type, |
| 481 | const NotificationSource& source, |
| 482 | const NotificationDetails& details) { |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 483 | DCHECK(type == NotificationType::NAV_ENTRY_COMMITTED); |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 484 | DCHECK(controller() == Source<NavigationController>(source).ptr()); |
| 485 | |
| 486 | NavigationController::LoadCommittedDetails& committed_details = |
| 487 | *(Details<NavigationController::LoadCommittedDetails>(details).ptr()); |
| 488 | ExpireInfoBars(committed_details); |
| 489 | } |
| 490 | |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 491 | void TabContents::SetIsLoading(bool is_loading, |
| 492 | LoadNotificationDetails* details) { |
| 493 | if (is_loading == is_loading_) |
| 494 | return; |
| 495 | |
| 496 | is_loading_ = is_loading; |
| 497 | waiting_for_response_ = is_loading; |
| 498 | |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 499 | if (delegate_) |
| 500 | delegate_->LoadingStateChanged(this); |
| 501 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 502 | NotificationType type = is_loading ? NotificationType::LOAD_START : |
| 503 | NotificationType::LOAD_STOP; |
[email protected] | 76f68dc | 2009-03-10 20:31:32 | [diff] [blame] | 504 | NotificationDetails det = NotificationService::NoDetails();; |
| 505 | if (details) |
| 506 | det = Details<LoadNotificationDetails>(details); |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 507 | NotificationService::current()->Notify(type, |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 508 | Source<NavigationController>(this->controller()), |
| 509 | det); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 510 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 511 | |
[email protected] | 76f68dc | 2009-03-10 20:31:32 | [diff] [blame] | 512 | #if defined(OS_WIN) |
[email protected] | 9e0534b | 2008-10-21 15:03:01 | [diff] [blame] | 513 | // TODO(brettw) This should be on the WebContentsView. |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 514 | void TabContents::RepositionSupressedPopupsToFit(const gfx::Size& new_size) { |
| 515 | // TODO(erg): There's no way to detect whether scroll bars are |
| 516 | // visible, so for beta, we're just going to assume that the |
| 517 | // vertical scroll bar is visible, and not care about covering up |
| 518 | // the horizontal scroll bar. Fixing this is half of |
| 519 | // http://b/1118139. |
| 520 | gfx::Point anchor_position( |
| 521 | new_size.width() - |
[email protected] | c2dacc9 | 2008-10-16 23:51:38 | [diff] [blame] | 522 | views::NativeScrollBar::GetVerticalScrollBarWidth(), |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 523 | new_size.height()); |
[email protected] | d6598c05 | 2008-11-05 19:03:25 | [diff] [blame] | 524 | |
| 525 | if (blocked_popups_) |
| 526 | blocked_popups_->RepositionConstrainedWindowTo(anchor_position); |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 527 | } |
| 528 | |
[email protected] | b968131 | 2008-11-07 00:08:26 | [diff] [blame] | 529 | bool TabContents::ShowingBlockedPopupNotification() const { |
| 530 | return blocked_popups_ != NULL && |
| 531 | blocked_popups_->GetTabContentsCount() != 0; |
| 532 | } |
[email protected] | 332af773 | 2009-03-11 13:21:35 | [diff] [blame] | 533 | #endif // defined(OS_WIN) |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 534 | |
| 535 | namespace { |
| 536 | bool TransitionIsReload(PageTransition::Type transition) { |
| 537 | return PageTransition::StripQualifier(transition) == PageTransition::RELOAD; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | void TabContents::ExpireInfoBars( |
| 542 | const NavigationController::LoadCommittedDetails& details) { |
| 543 | // Only hide InfoBars when the user has done something that makes the main |
| 544 | // frame load. We don't want various automatic or subframe navigations making |
| 545 | // it disappear. |
| 546 | if (!details.is_user_initiated_main_frame_load()) |
| 547 | return; |
| 548 | |
[email protected] | f86a0702 | 2008-11-25 01:06:05 | [diff] [blame] | 549 | for (int i = infobar_delegate_count() - 1; i >= 0; --i) { |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 550 | InfoBarDelegate* delegate = GetInfoBarDelegateAt(i); |
[email protected] | f86a0702 | 2008-11-25 01:06:05 | [diff] [blame] | 551 | if (delegate->ShouldExpire(details)) |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 552 | RemoveInfoBar(delegate); |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 553 | } |
| 554 | } |