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 | |
| 5 | #include "chrome/browser/tab_contents.h" |
| 6 | |
| 7 | #include "chrome/browser/cert_store.h" |
| 8 | #include "chrome/browser/navigation_entry.h" |
| 9 | #include "chrome/browser/views/download_shelf_view.h" |
| 10 | #include "chrome/browser/views/download_started_animation.h" |
| 11 | #include "chrome/browser/web_contents.h" |
| 12 | #include "chrome/browser/tab_contents_delegate.h" |
[email protected] | a4feef8 | 2008-10-02 15:11:22 | [diff] [blame] | 13 | #include "chrome/common/l10n_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | #include "chrome/common/pref_names.h" |
[email protected] | 1eb89e8 | 2008-08-15 12:27:03 | [diff] [blame] | 15 | #include "chrome/common/pref_service.h" |
[email protected] | c80c563 | 2008-10-10 20:34:35 | [diff] [blame] | 16 | #include "chrome/views/hwnd_view_container.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 17 | #include "chrome/views/native_scroll_bar.h" |
[email protected] | 1eb89e8 | 2008-08-15 12:27:03 | [diff] [blame] | 18 | #include "chrome/views/root_view.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 19 | #include "chrome/views/view.h" |
| 20 | #include "chrome/views/view_storage.h" |
| 21 | |
| 22 | #include "generated_resources.h" |
| 23 | |
[email protected] | 92db537 | 2008-09-26 23:40:45 | [diff] [blame] | 24 | static size_t kMaxNumberOfConstrainedPopups = 20; |
| 25 | |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 26 | namespace { |
| 27 | |
| 28 | BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) { |
| 29 | // Note: erase is required to properly paint some widgets borders. This can be |
| 30 | // seen with textfields. |
| 31 | InvalidateRect(hwnd, NULL, TRUE); |
| 32 | return TRUE; |
| 33 | } |
| 34 | |
| 35 | } // namespace |
| 36 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 37 | TabContents::TabContents(TabContentsType type) |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 38 | : type_(type), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 39 | delegate_(NULL), |
| 40 | controller_(NULL), |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 41 | is_loading_(false), |
| 42 | is_active_(true), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 43 | is_crashed_(false), |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 44 | waiting_for_response_(false), |
| 45 | saved_location_bar_state_(NULL), |
| 46 | shelf_visible_(false), |
| 47 | max_page_id_(-1), |
| 48 | capturing_contents_(false) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 49 | last_focused_view_storage_id_ = |
| 50 | ChromeViews::ViewStorage::GetSharedInstance()->CreateStorageID(); |
| 51 | } |
| 52 | |
| 53 | TabContents::~TabContents() { |
| 54 | // Makes sure to remove any stored view we may still have in the ViewStorage. |
| 55 | // |
| 56 | // It is possible the view went away before us, so we only do this if the |
| 57 | // view is registered. |
| 58 | ChromeViews::ViewStorage* view_storage = |
| 59 | ChromeViews::ViewStorage::GetSharedInstance(); |
| 60 | if (view_storage->RetrieveView(last_focused_view_storage_id_) != NULL) |
| 61 | view_storage->RemoveView(last_focused_view_storage_id_); |
| 62 | } |
| 63 | |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 64 | // static |
| 65 | void TabContents::RegisterUserPrefs(PrefService* prefs) { |
| 66 | prefs->RegisterBooleanPref(prefs::kBlockPopups, false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 67 | } |
| 68 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 69 | |
| 70 | void TabContents::CloseContents() { |
| 71 | // Destroy our NavigationController, which will Destroy all tabs it owns. |
| 72 | controller_->Destroy(); |
| 73 | // Note that the controller may have deleted us at this point, |
| 74 | // so don't touch any member variables here. |
| 75 | } |
| 76 | |
| 77 | void TabContents::Destroy() { |
| 78 | // First cleanly close all child windows. |
| 79 | // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked |
| 80 | // some of these to close. CloseWindows is async, so it might get called |
| 81 | // twice before it runs. |
| 82 | int size = static_cast<int>(child_windows_.size()); |
| 83 | for (int i = size - 1; i >= 0; --i) { |
| 84 | ConstrainedWindow* window = child_windows_[i]; |
| 85 | if (window) |
| 86 | window->CloseConstrainedWindow(); |
| 87 | } |
| 88 | |
| 89 | // Notify any observer that have a reference on this tab contents. |
| 90 | NotificationService::current()->Notify(NOTIFY_TAB_CONTENTS_DESTROYED, |
| 91 | Source<TabContents>(this), |
| 92 | NotificationService::NoDetails()); |
| 93 | |
| 94 | // If we still have a window handle, destroy it. GetContainerHWND can return |
| 95 | // NULL if this contents was part of a window that closed. |
| 96 | if (GetContainerHWND()) |
| 97 | ::DestroyWindow(GetContainerHWND()); |
| 98 | |
| 99 | // Notify our NavigationController. Make sure we are deleted first, so |
| 100 | // that the controller is the last to die. |
| 101 | NavigationController* controller = controller_; |
| 102 | TabContentsType type = this->type(); |
| 103 | |
| 104 | delete this; |
| 105 | |
| 106 | controller->TabContentsWasDestroyed(type); |
| 107 | } |
| 108 | |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 109 | void TabContents::SetupController(Profile* profile) { |
| 110 | DCHECK(!controller_); |
| 111 | controller_ = new NavigationController(this, profile); |
| 112 | } |
| 113 | |
| 114 | bool TabContents::SupportsURL(GURL* url) { |
| 115 | GURL u(*url); |
| 116 | if (TabContents::TypeForURL(&u) == type()) { |
| 117 | *url = u; |
| 118 | return true; |
| 119 | } |
| 120 | return false; |
| 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 | |
| 129 | const std::wstring& 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(); |
| 137 | if (entry && !entry->title().empty()) |
| 138 | return entry->title(); |
| 139 | |
| 140 | entry = controller_->GetLastCommittedEntry(); |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 141 | if (entry) |
| 142 | return entry->title(); |
| 143 | else if (controller_->LoadingURLLazily()) |
| 144 | return controller_->GetLazyTitle(); |
| 145 | return EmptyWString(); |
| 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 | |
| 187 | SecurityStyle TabContents::GetSecurityStyle() const { |
| 188 | // We may not have a navigation entry yet. |
| 189 | NavigationEntry* entry = controller_->GetActiveEntry(); |
| 190 | return entry ? entry->ssl().security_style() : SECURITY_STYLE_UNKNOWN; |
| 191 | } |
| 192 | |
| 193 | bool TabContents::GetSSLEVText(std::wstring* ev_text, |
| 194 | std::wstring* ev_tooltip_text) const { |
| 195 | DCHECK(ev_text && ev_tooltip_text); |
| 196 | ev_text->clear(); |
| 197 | ev_tooltip_text->clear(); |
| 198 | |
| 199 | NavigationEntry* entry = controller_->GetActiveEntry(); |
| 200 | if (!entry || |
| 201 | net::IsCertStatusError(entry->ssl().cert_status()) || |
| 202 | ((entry->ssl().cert_status() & net::CERT_STATUS_IS_EV) == 0)) |
| 203 | return false; |
| 204 | |
| 205 | scoped_refptr<net::X509Certificate> cert; |
| 206 | CertStore::GetSharedInstance()->RetrieveCert(entry->ssl().cert_id(), &cert); |
| 207 | if (!cert.get()) { |
| 208 | NOTREACHED(); |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | return SSLManager::GetEVCertNames(*cert, ev_text, ev_tooltip_text); |
| 213 | } |
| 214 | |
| 215 | void TabContents::SetIsCrashed(bool state) { |
| 216 | if (state == is_crashed_) |
| 217 | return; |
| 218 | |
| 219 | is_crashed_ = state; |
| 220 | if (delegate_) |
| 221 | delegate_->ContentsStateChanged(this); |
| 222 | } |
| 223 | |
| 224 | void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) { |
| 225 | if (delegate_) |
| 226 | delegate_->NavigationStateChanged(this, changed_flags); |
| 227 | } |
| 228 | |
| 229 | void TabContents::DidBecomeSelected() { |
| 230 | if (controller_) |
| 231 | controller_->SetActive(true); |
| 232 | |
| 233 | // Invalidate all descendants. (take care to exclude invalidating ourselves!) |
| 234 | EnumChildWindows(GetContainerHWND(), InvalidateWindow, 0); |
| 235 | } |
| 236 | |
| 237 | void TabContents::WasHidden() { |
| 238 | NotificationService::current()->Notify(NOTIFY_TAB_CONTENTS_HIDDEN, |
| 239 | Source<TabContents>(this), |
| 240 | NotificationService::NoDetails()); |
| 241 | } |
| 242 | |
| 243 | void TabContents::Activate() { |
| 244 | if (delegate_) |
| 245 | delegate_->ActivateContents(this); |
| 246 | } |
| 247 | |
| 248 | void TabContents::OpenURL(const GURL& url, |
| 249 | WindowOpenDisposition disposition, |
| 250 | PageTransition::Type transition) { |
| 251 | if (delegate_) |
| 252 | delegate_->OpenURLFromTab(this, url, disposition, transition); |
| 253 | } |
| 254 | |
| 255 | bool TabContents::NavigateToPendingEntry(bool reload) { |
| 256 | // Our benavior is just to report that the entry was committed. |
| 257 | controller()->GetPendingEntry()->set_title(GetDefaultTitle()); |
| 258 | controller()->CommitPendingEntry(); |
| 259 | return true; |
| 260 | } |
| 261 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 262 | ConstrainedWindow* TabContents::CreateConstrainedDialog( |
| 263 | ChromeViews::WindowDelegate* window_delegate, |
| 264 | ChromeViews::View* contents_view) { |
| 265 | ConstrainedWindow* window = |
| 266 | ConstrainedWindow::CreateConstrainedDialog( |
| 267 | this, gfx::Rect(), contents_view, window_delegate); |
| 268 | child_windows_.push_back(window); |
| 269 | return window; |
| 270 | } |
| 271 | |
| 272 | void TabContents::AddNewContents(TabContents* new_contents, |
| 273 | WindowOpenDisposition disposition, |
| 274 | const gfx::Rect& initial_pos, |
| 275 | bool user_gesture) { |
| 276 | if (!delegate_) |
| 277 | return; |
| 278 | |
[email protected] | 3b0a45e8 | 2008-10-13 21:01:03 | [diff] [blame] | 279 | if ((disposition == NEW_POPUP) && !user_gesture) { |
| 280 | // Unrequested popups from normal pages are constrained. |
| 281 | TabContents* popup_owner = this; |
| 282 | TabContents* our_owner = delegate_->GetConstrainingContents(this); |
| 283 | if (our_owner) |
| 284 | popup_owner = our_owner; |
| 285 | popup_owner->AddConstrainedPopup(new_contents, initial_pos); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 286 | } else { |
| 287 | delegate_->AddNewContents(this, new_contents, disposition, initial_pos, |
| 288 | user_gesture); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | void TabContents::AddConstrainedPopup(TabContents* new_contents, |
| 293 | const gfx::Rect& initial_pos) { |
[email protected] | 92db537 | 2008-09-26 23:40:45 | [diff] [blame] | 294 | if (child_windows_.size() > kMaxNumberOfConstrainedPopups) { |
| 295 | new_contents->CloseContents(); |
| 296 | return; |
| 297 | } |
| 298 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 299 | ConstrainedWindow* window = |
| 300 | ConstrainedWindow::CreateConstrainedPopup( |
| 301 | this, initial_pos, new_contents); |
| 302 | child_windows_.push_back(window); |
| 303 | |
| 304 | CRect client_rect; |
| 305 | GetClientRect(GetContainerHWND(), &client_rect); |
| 306 | gfx::Size new_size(client_rect.Width(), client_rect.Height()); |
| 307 | RepositionSupressedPopupsToFit(new_size); |
| 308 | } |
| 309 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 310 | void TabContents::CloseAllSuppressedPopups() { |
| 311 | // Close all auto positioned child windows to "clean up" the workspace. |
| 312 | int count = static_cast<int>(child_windows_.size()); |
| 313 | for (int i = count - 1; i >= 0; --i) { |
| 314 | ConstrainedWindow* window = child_windows_.at(i); |
| 315 | if (window->IsSuppressedConstrainedWindow()) |
| 316 | window->CloseConstrainedWindow(); |
| 317 | } |
| 318 | } |
| 319 | |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 320 | void TabContents::HideContents() { |
| 321 | // Hide the contents before adjusting its parent to avoid a full desktop |
| 322 | // flicker. |
| 323 | ShowWindow(GetContainerHWND(), SW_HIDE); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 324 | |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 325 | // Reset the parent to NULL to ensure hidden tabs don't receive messages. |
| 326 | SetParent(GetContainerHWND(), NULL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 327 | |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 328 | // Remove any focus manager related information. |
| 329 | ChromeViews::FocusManager::UninstallFocusSubclass(GetContainerHWND()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 330 | |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 331 | WasHidden(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 332 | } |
| 333 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 334 | void TabContents::Focus() { |
| 335 | ChromeViews::FocusManager* focus_manager = |
| 336 | ChromeViews::FocusManager::GetFocusManager(GetContainerHWND()); |
| 337 | DCHECK(focus_manager); |
| 338 | ChromeViews::View* v = |
| 339 | focus_manager->GetViewForWindow(GetContainerHWND(), true); |
| 340 | DCHECK(v); |
| 341 | if (v) |
| 342 | v->RequestFocus(); |
| 343 | } |
| 344 | |
| 345 | void TabContents::StoreFocus() { |
| 346 | ChromeViews::ViewStorage* view_storage = |
| 347 | ChromeViews::ViewStorage::GetSharedInstance(); |
| 348 | |
| 349 | if (view_storage->RetrieveView(last_focused_view_storage_id_) != NULL) |
| 350 | view_storage->RemoveView(last_focused_view_storage_id_); |
| 351 | |
| 352 | ChromeViews::FocusManager* focus_manager = |
| 353 | ChromeViews::FocusManager::GetFocusManager(GetContainerHWND()); |
| 354 | if (focus_manager) { |
| 355 | // |focus_manager| can be NULL if the tab has been detached but still |
| 356 | // exists. |
| 357 | ChromeViews::View* focused_view = focus_manager->GetFocusedView(); |
| 358 | if (focused_view) |
| 359 | view_storage->StoreView(last_focused_view_storage_id_, focused_view); |
| 360 | |
| 361 | // If the focus was on the page, explicitly clear the focus so that we |
| 362 | // don't end up with the focused HWND not part of the window hierarchy. |
[email protected] | c80c563 | 2008-10-10 20:34:35 | [diff] [blame] | 363 | // TODO(brettw) this should move to the view somehow. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 364 | HWND container_hwnd = GetContainerHWND(); |
| 365 | if (container_hwnd) { |
| 366 | ChromeViews::View* focused_view = focus_manager->GetFocusedView(); |
| 367 | if (focused_view) { |
| 368 | HWND hwnd = focused_view->GetRootView()->GetViewContainer()->GetHWND(); |
| 369 | if (container_hwnd == hwnd || ::IsChild(container_hwnd, hwnd)) |
| 370 | focus_manager->ClearFocus(); |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | void TabContents::RestoreFocus() { |
| 377 | ChromeViews::ViewStorage* view_storage = |
| 378 | ChromeViews::ViewStorage::GetSharedInstance(); |
| 379 | ChromeViews::View* last_focused_view = |
| 380 | view_storage->RetrieveView(last_focused_view_storage_id_); |
| 381 | |
| 382 | if (!last_focused_view) { |
| 383 | SetInitialFocus(); |
| 384 | } else { |
| 385 | ChromeViews::FocusManager* focus_manager = |
| 386 | ChromeViews::FocusManager::GetFocusManager(GetContainerHWND()); |
[email protected] | 6e2f2cec | 2008-09-23 22:59:06 | [diff] [blame] | 387 | |
| 388 | // If you hit this DCHECK, please report it to Jay (jcampan). |
| 389 | DCHECK(focus_manager != NULL) << "No focus manager when restoring focus."; |
| 390 | |
| 391 | if (focus_manager && focus_manager->ContainsView(last_focused_view)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 392 | last_focused_view->RequestFocus(); |
| 393 | } else { |
| 394 | // The focused view may not belong to the same window hierarchy (for |
| 395 | // example if the location bar was focused and the tab is dragged out). |
| 396 | // In that case we default to the default focus. |
| 397 | SetInitialFocus(); |
| 398 | } |
| 399 | view_storage->RemoveView(last_focused_view_storage_id_); |
| 400 | } |
| 401 | } |
| 402 | |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 403 | void TabContents::SetInitialFocus() { |
| 404 | ::SetFocus(GetContainerHWND()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | void TabContents::SetDownloadShelfVisible(bool visible) { |
| 408 | if (shelf_visible_ != visible) { |
| 409 | if (visible) { |
| 410 | // Invoke GetDownloadShelfView to force the shelf to be created. |
| 411 | GetDownloadShelfView(); |
| 412 | } |
| 413 | shelf_visible_ = visible; |
| 414 | |
[email protected] | 019d8350 | 2008-07-30 22:44:50 | [diff] [blame] | 415 | if (delegate_) |
| 416 | delegate_->ContentsStateChanged(this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 417 | } |
[email protected] | ce2390b68 | 2008-08-08 22:24:51 | [diff] [blame] | 418 | |
| 419 | // SetShelfVisible can force-close the shelf, so make sure we lay out |
| 420 | // everything correctly, as if the animation had finished. This doesn't |
| 421 | // matter for showing the shelf, as the show animation will do it. |
| 422 | ToolbarSizeChanged(false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 423 | } |
| 424 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 425 | void TabContents::ToolbarSizeChanged(bool is_animating) { |
| 426 | TabContentsDelegate* d = delegate(); |
| 427 | if (d) |
| 428 | d->ToolbarSizeChanged(this, is_animating); |
| 429 | } |
| 430 | |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 431 | void TabContents::OnStartDownload(DownloadItem* download) { |
| 432 | DCHECK(download); |
| 433 | TabContents* tab_contents = this; |
| 434 | |
| 435 | // Download in a constrained popup is shown in the tab that opened it. |
| 436 | TabContents* constraining_tab = delegate()->GetConstrainingContents(this); |
| 437 | if (constraining_tab) |
| 438 | tab_contents = constraining_tab; |
| 439 | |
| 440 | // GetDownloadShelfView creates the download shelf if it was not yet created. |
| 441 | tab_contents->GetDownloadShelfView()->AddDownload(download); |
| 442 | tab_contents->SetDownloadShelfVisible(true); |
| 443 | |
| 444 | // This animation will delete itself when it finishes, or if we become hidden |
| 445 | // or destroyed. |
| 446 | if (IsWindowVisible(GetContainerHWND())) { // For minimized windows, unit |
| 447 | // tests, etc. |
| 448 | new DownloadStartedAnimation(tab_contents); |
| 449 | } |
| 450 | } |
| 451 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 452 | DownloadShelfView* TabContents::GetDownloadShelfView() { |
| 453 | if (!download_shelf_view_.get()) { |
| 454 | download_shelf_view_.reset(new DownloadShelfView(this)); |
| 455 | // The TabContents owns the download-shelf. |
| 456 | download_shelf_view_->SetParentOwned(false); |
| 457 | } |
| 458 | return download_shelf_view_.get(); |
| 459 | } |
| 460 | |
| 461 | void TabContents::MigrateShelfViewFrom(TabContents* tab_contents) { |
| 462 | download_shelf_view_.reset(tab_contents->GetDownloadShelfView()); |
| 463 | download_shelf_view_->ChangeTabContents(tab_contents, this); |
| 464 | tab_contents->ReleaseDownloadShelfView(); |
| 465 | } |
| 466 | |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 467 | void TabContents::AddNewContents(ConstrainedWindow* window, |
| 468 | TabContents* new_contents, |
| 469 | WindowOpenDisposition disposition, |
| 470 | const gfx::Rect& initial_pos, |
| 471 | bool user_gesture) { |
| 472 | AddNewContents(new_contents, disposition, initial_pos, user_gesture); |
| 473 | } |
| 474 | |
| 475 | void TabContents::OpenURL(ConstrainedWindow* window, |
| 476 | const GURL& url, |
| 477 | WindowOpenDisposition disposition, |
| 478 | PageTransition::Type transition) { |
| 479 | OpenURL(url, disposition, transition); |
| 480 | } |
| 481 | |
| 482 | void TabContents::WillClose(ConstrainedWindow* window) { |
| 483 | ConstrainedWindowList::iterator it = |
| 484 | find(child_windows_.begin(), child_windows_.end(), window); |
| 485 | if (it != child_windows_.end()) |
| 486 | child_windows_.erase(it); |
| 487 | |
| 488 | if (::IsWindow(GetContainerHWND())) { |
| 489 | CRect client_rect; |
| 490 | GetClientRect(GetContainerHWND(), &client_rect); |
| 491 | RepositionSupressedPopupsToFit( |
| 492 | gfx::Size(client_rect.Width(), client_rect.Height())); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | void TabContents::DetachContents(ConstrainedWindow* window, |
| 497 | TabContents* contents, |
| 498 | const gfx::Rect& contents_bounds, |
| 499 | const gfx::Point& mouse_pt, |
| 500 | int frame_component) { |
| 501 | WillClose(window); |
| 502 | if (delegate_) { |
| 503 | delegate_->StartDraggingDetachedContents( |
| 504 | this, contents, contents_bounds, mouse_pt, frame_component); |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | void TabContents::DidMoveOrResize(ConstrainedWindow* window) { |
| 509 | UpdateWindow(GetContainerHWND()); |
| 510 | } |
| 511 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 512 | // static |
| 513 | void TabContents::MigrateShelfView(TabContents* from, TabContents* to) { |
| 514 | bool was_shelf_visible = from->IsDownloadShelfVisible(); |
| 515 | if (was_shelf_visible) |
| 516 | to->MigrateShelfViewFrom(from); |
| 517 | to->SetDownloadShelfVisible(was_shelf_visible); |
| 518 | } |
| 519 | |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 520 | void TabContents::SetIsLoading(bool is_loading, |
| 521 | LoadNotificationDetails* details) { |
| 522 | if (is_loading == is_loading_) |
| 523 | return; |
| 524 | |
| 525 | is_loading_ = is_loading; |
| 526 | waiting_for_response_ = is_loading; |
| 527 | |
| 528 | // Suppress notifications for this TabContents if we are not active. |
| 529 | if (!is_active_) |
| 530 | return; |
| 531 | |
| 532 | if (delegate_) |
| 533 | delegate_->LoadingStateChanged(this); |
| 534 | |
| 535 | NotificationService::current()-> |
| 536 | Notify((is_loading ? NOTIFY_LOAD_START : NOTIFY_LOAD_STOP), |
| 537 | Source<NavigationController>(this->controller()), |
| 538 | details ? Details<LoadNotificationDetails>(details) : |
| 539 | NotificationService::NoDetails()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 540 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 541 | |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 542 | void TabContents::RepositionSupressedPopupsToFit(const gfx::Size& new_size) { |
| 543 | // TODO(erg): There's no way to detect whether scroll bars are |
| 544 | // visible, so for beta, we're just going to assume that the |
| 545 | // vertical scroll bar is visible, and not care about covering up |
| 546 | // the horizontal scroll bar. Fixing this is half of |
| 547 | // http://b/1118139. |
| 548 | gfx::Point anchor_position( |
| 549 | new_size.width() - |
| 550 | ChromeViews::NativeScrollBar::GetVerticalScrollBarWidth(), |
| 551 | new_size.height()); |
| 552 | int window_count = static_cast<int>(child_windows_.size()); |
| 553 | for (int i = window_count - 1; i >= 0; --i) { |
| 554 | ConstrainedWindow* window = child_windows_.at(i); |
| 555 | if (window->IsSuppressedConstrainedWindow()) |
| 556 | window->RepositionConstrainedWindowTo(anchor_position); |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | void TabContents::ReleaseDownloadShelfView() { |
| 561 | download_shelf_view_.release(); |
| 562 | } |