1 // Copyright (c) 2011 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.
4
5 #include "chrome/browser/ui/views/tab_contents/tab_contents_container.h"
6
7 #include "chrome/browser/ui/view_ids.h"
8 #include "chrome/browser/ui/views/tab_contents/native_tab_contents_container.h"
9 #include "content/browser/renderer_host/render_view_host.h"
10 #include "content/browser/renderer_host/render_widget_host_view.h"
11 #include "content/browser/tab_contents/interstitial_page.h"
12 #include "content/browser/tab_contents/tab_contents.h"
13 #include "content/common/notification_details.h"
14 #include "content/common/notification_source.h"
15
16 // Some of this class is implemented in tab_contents_container.cc, where
17 // the implementation doesn't vary between a pure views approach and a
18 // native view host approach. See the header file for details.
19
20 ////////////////////////////////////////////////////////////////////////////////
21 // TabContentsContainer, public:
22
TabContentsContainer()23 TabContentsContainer::TabContentsContainer()
24 : native_container_(NULL),
25 tab_contents_(NULL) {
26 SetID(VIEW_ID_TAB_CONTAINER);
27 }
28
SetReservedContentsRect(const gfx::Rect & reserved_rect)29 void TabContentsContainer::SetReservedContentsRect(
30 const gfx::Rect& reserved_rect) {
31 cached_reserved_rect_ = reserved_rect;
32 if (tab_contents_ && tab_contents_->GetRenderWidgetHostView()) {
33 tab_contents_->GetRenderWidgetHostView()->set_reserved_contents_rect(
34 reserved_rect);
35 }
36 }
37
ChangeTabContents(TabContents * contents)38 void TabContentsContainer::ChangeTabContents(TabContents* contents) {
39 if (tab_contents_) {
40 native_container_->DetachContents(tab_contents_);
41 tab_contents_->WasHidden();
42 RemoveObservers();
43 }
44 tab_contents_ = contents;
45 // When detaching the last tab of the browser ChangeTabContents is invoked
46 // with NULL. Don't attempt to do anything in that case.
47 if (tab_contents_) {
48 RenderWidgetHostViewChanged(tab_contents_->GetRenderWidgetHostView());
49 native_container_->AttachContents(tab_contents_);
50 AddObservers();
51 }
52 }
53
TabContentsFocused(TabContents * tab_contents)54 void TabContentsContainer::TabContentsFocused(TabContents* tab_contents) {
55 native_container_->TabContentsFocused(tab_contents);
56 }
57
SetFastResize(bool fast_resize)58 void TabContentsContainer::SetFastResize(bool fast_resize) {
59 native_container_->SetFastResize(fast_resize);
60 }
61
62 ////////////////////////////////////////////////////////////////////////////////
63 // TabContentsContainer, View overrides:
64
Layout()65 void TabContentsContainer::Layout() {
66 if (native_container_) {
67 native_container_->GetView()->SetBounds(0, 0, width(), height());
68 native_container_->GetView()->Layout();
69 }
70 }
71
ViewHierarchyChanged(bool is_add,views::View * parent,views::View * child)72 void TabContentsContainer::ViewHierarchyChanged(bool is_add,
73 views::View* parent,
74 views::View* child) {
75 if (is_add && child == this) {
76 native_container_ = NativeTabContentsContainer::CreateNativeContainer(this);
77 AddChildView(native_container_->GetView());
78 }
79 }
80
81 ////////////////////////////////////////////////////////////////////////////////
82 // TabContentsContainer, private:
83
RenderViewHostChanged(RenderViewHost * old_host,RenderViewHost * new_host)84 void TabContentsContainer::RenderViewHostChanged(RenderViewHost* old_host,
85 RenderViewHost* new_host) {
86 if (new_host)
87 RenderWidgetHostViewChanged(new_host->view());
88 native_container_->RenderViewHostChanged(old_host, new_host);
89 }
90