blob: 2deb196b8552c430e429061f5b2c919fc7024ee5 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// 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.commit09911bf2008-07-26 23:55:294
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]a4feef82008-10-02 15:11:2213#include "chrome/common/l10n_util.h"
initial.commit09911bf2008-07-26 23:55:2914#include "chrome/common/pref_names.h"
[email protected]1eb89e82008-08-15 12:27:0315#include "chrome/common/pref_service.h"
[email protected]c80c5632008-10-10 20:34:3516#include "chrome/views/hwnd_view_container.h"
initial.commit09911bf2008-07-26 23:55:2917#include "chrome/views/native_scroll_bar.h"
[email protected]1eb89e82008-08-15 12:27:0318#include "chrome/views/root_view.h"
initial.commit09911bf2008-07-26 23:55:2919#include "chrome/views/view.h"
20#include "chrome/views/view_storage.h"
21
22#include "generated_resources.h"
23
[email protected]92db5372008-09-26 23:40:4524static size_t kMaxNumberOfConstrainedPopups = 20;
25
[email protected]d5f942ba2008-09-26 19:30:3426namespace {
27
28BOOL 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.commit09911bf2008-07-26 23:55:2937TabContents::TabContents(TabContentsType type)
[email protected]d5f942ba2008-09-26 19:30:3438 : type_(type),
initial.commit09911bf2008-07-26 23:55:2939 delegate_(NULL),
40 controller_(NULL),
[email protected]d5f942ba2008-09-26 19:30:3441 is_loading_(false),
42 is_active_(true),
initial.commit09911bf2008-07-26 23:55:2943 is_crashed_(false),
[email protected]d5f942ba2008-09-26 19:30:3444 waiting_for_response_(false),
45 saved_location_bar_state_(NULL),
46 shelf_visible_(false),
47 max_page_id_(-1),
48 capturing_contents_(false) {
initial.commit09911bf2008-07-26 23:55:2949 last_focused_view_storage_id_ =
50 ChromeViews::ViewStorage::GetSharedInstance()->CreateStorageID();
51}
52
53TabContents::~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]d5f942ba2008-09-26 19:30:3464// static
65void TabContents::RegisterUserPrefs(PrefService* prefs) {
66 prefs->RegisterBooleanPref(prefs::kBlockPopups, false);
initial.commit09911bf2008-07-26 23:55:2967}
68
initial.commit09911bf2008-07-26 23:55:2969
70void 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
77void 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]d5f942ba2008-09-26 19:30:34109void TabContents::SetupController(Profile* profile) {
110 DCHECK(!controller_);
111 controller_ = new NavigationController(this, profile);
112}
113
114bool 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
123const 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
129const std::wstring& TabContents::GetTitle() const {
[email protected]cbab76d2008-10-13 22:42:47130 // 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]d5f942ba2008-09-26 19:30:34141 if (entry)
142 return entry->title();
143 else if (controller_->LoadingURLLazily())
144 return controller_->GetLazyTitle();
145 return EmptyWString();
146}
147
148int32 TabContents::GetMaxPageID() {
149 if (GetSiteInstance())
150 return GetSiteInstance()->max_page_id();
151 else
152 return max_page_id_;
153}
154
155void 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
168const std::wstring TabContents::GetDefaultTitle() const {
169 return l10n_util::GetString(IDS_DEFAULT_TAB_TITLE);
170}
171
172SkBitmap 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]cbab76d2008-10-13 22:42:47175 NavigationEntry* entry = controller_->GetTransientEntry();
176 if (entry)
177 return entry->favicon().bitmap();
178
179 entry = controller_->GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34180 if (entry)
181 return entry->favicon().bitmap();
182 else if (controller_->LoadingURLLazily())
183 return controller_->GetLazyFavIcon();
184 return SkBitmap();
185}
186
187SecurityStyle 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
193bool 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
215void 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
224void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
225 if (delegate_)
226 delegate_->NavigationStateChanged(this, changed_flags);
227}
228
229void 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
237void TabContents::WasHidden() {
238 NotificationService::current()->Notify(NOTIFY_TAB_CONTENTS_HIDDEN,
239 Source<TabContents>(this),
240 NotificationService::NoDetails());
241}
242
243void TabContents::Activate() {
244 if (delegate_)
245 delegate_->ActivateContents(this);
246}
247
248void TabContents::OpenURL(const GURL& url,
249 WindowOpenDisposition disposition,
250 PageTransition::Type transition) {
251 if (delegate_)
252 delegate_->OpenURLFromTab(this, url, disposition, transition);
253}
254
255bool 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.commit09911bf2008-07-26 23:55:29262ConstrainedWindow* 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
272void 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]3b0a45e82008-10-13 21:01:03279 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.commit09911bf2008-07-26 23:55:29286 } else {
287 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
288 user_gesture);
289 }
290}
291
292void TabContents::AddConstrainedPopup(TabContents* new_contents,
293 const gfx::Rect& initial_pos) {
[email protected]92db5372008-09-26 23:40:45294 if (child_windows_.size() > kMaxNumberOfConstrainedPopups) {
295 new_contents->CloseContents();
296 return;
297 }
298
initial.commit09911bf2008-07-26 23:55:29299 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.commit09911bf2008-07-26 23:55:29310void 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]d5f942ba2008-09-26 19:30:34320void TabContents::HideContents() {
321 // Hide the contents before adjusting its parent to avoid a full desktop
322 // flicker.
323 ShowWindow(GetContainerHWND(), SW_HIDE);
initial.commit09911bf2008-07-26 23:55:29324
[email protected]d5f942ba2008-09-26 19:30:34325 // Reset the parent to NULL to ensure hidden tabs don't receive messages.
326 SetParent(GetContainerHWND(), NULL);
initial.commit09911bf2008-07-26 23:55:29327
[email protected]d5f942ba2008-09-26 19:30:34328 // Remove any focus manager related information.
329 ChromeViews::FocusManager::UninstallFocusSubclass(GetContainerHWND());
initial.commit09911bf2008-07-26 23:55:29330
[email protected]d5f942ba2008-09-26 19:30:34331 WasHidden();
initial.commit09911bf2008-07-26 23:55:29332}
333
initial.commit09911bf2008-07-26 23:55:29334void 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
345void 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]c80c5632008-10-10 20:34:35363 // TODO(brettw) this should move to the view somehow.
initial.commit09911bf2008-07-26 23:55:29364 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
376void 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]6e2f2cec2008-09-23 22:59:06387
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.commit09911bf2008-07-26 23:55:29392 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]d5f942ba2008-09-26 19:30:34403void TabContents::SetInitialFocus() {
404 ::SetFocus(GetContainerHWND());
initial.commit09911bf2008-07-26 23:55:29405}
406
407void 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]019d83502008-07-30 22:44:50415 if (delegate_)
416 delegate_->ContentsStateChanged(this);
initial.commit09911bf2008-07-26 23:55:29417 }
[email protected]ce2390b682008-08-08 22:24:51418
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.commit09911bf2008-07-26 23:55:29423}
424
initial.commit09911bf2008-07-26 23:55:29425void TabContents::ToolbarSizeChanged(bool is_animating) {
426 TabContentsDelegate* d = delegate();
427 if (d)
428 d->ToolbarSizeChanged(this, is_animating);
429}
430
[email protected]d5f942ba2008-09-26 19:30:34431void 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.commit09911bf2008-07-26 23:55:29452DownloadShelfView* 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
461void 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]d5f942ba2008-09-26 19:30:34467void 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
475void TabContents::OpenURL(ConstrainedWindow* window,
476 const GURL& url,
477 WindowOpenDisposition disposition,
478 PageTransition::Type transition) {
479 OpenURL(url, disposition, transition);
480}
481
482void 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
496void 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
508void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
509 UpdateWindow(GetContainerHWND());
510}
511
initial.commit09911bf2008-07-26 23:55:29512// static
513void 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]d5f942ba2008-09-26 19:30:34520void 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.commit09911bf2008-07-26 23:55:29540}
license.botbf09a502008-08-24 00:55:55541
[email protected]d5f942ba2008-09-26 19:30:34542void 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
560void TabContents::ReleaseDownloadShelfView() {
561 download_shelf_view_.release();
562}