summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/views/tab_contents/tab_contents_container.cc
blob: 9ab6078d3e03aacb4225e15ca6190ce1068c73e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/views/tab_contents/tab_contents_container.h"

#include "chrome/browser/renderer_host/render_view_host.h"
#include "chrome/browser/renderer_host/render_widget_host_view.h"
#include "chrome/browser/tab_contents/interstitial_page.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/browser/view_ids.h"
#include "chrome/browser/views/tab_contents/native_tab_contents_container.h"
#include "chrome/common/notification_service.h"

#if defined(TOUCH_UI)
#include "chrome/browser/ui/views/tab_contents/native_tab_contents_container_gtk.h"
#include "chrome/browser/ui/views/tab_contents/tab_contents_view_views.h"
#include "views/border.h"
#include "views/fill_layout.h"
#endif

////////////////////////////////////////////////////////////////////////////////
// TabContentsContainer, public:

TabContentsContainer::TabContentsContainer()
    : native_container_(NULL),
      tab_contents_(NULL),
      reserved_area_delegate_(NULL) {
  SetID(VIEW_ID_TAB_CONTAINER);
}

TabContentsContainer::~TabContentsContainer() {
  if (tab_contents_)
    RemoveObservers();
}

void TabContentsContainer::ChangeTabContents(TabContents* contents) {
  if (tab_contents_) {
#if !defined(TOUCH_UI)
    native_container_->DetachContents(tab_contents_);
#endif
    tab_contents_->WasHidden();
    RemoveObservers();
  }
#if !defined(TOUCH_UI)
  TabContents* old_contents = tab_contents_;
#endif
  tab_contents_ = contents;
  // When detaching the last tab of the browser ChangeTabContents is invoked
  // with NULL. Don't attempt to do anything in that case.
  if (tab_contents_) {
#if defined(TOUCH_UI)
    views::View *v = static_cast<TabContentsViewViews*>(contents->view());
    // Guard against re-adding ourselves, which happens because the NULL
    // value is ignored by the pre-existing if() above.
    if (v->GetParent() != this) {
      AddChildView(v);
      SetLayoutManager(new views::FillLayout());
      Layout();
    }
#else
    RenderWidgetHostViewChanged(
        old_contents ? old_contents->GetRenderWidgetHostView() : NULL,
        tab_contents_->GetRenderWidgetHostView());
    native_container_->AttachContents(tab_contents_);
#endif
    AddObservers();
  }
}

void TabContentsContainer::TabContentsFocused(TabContents* tab_contents) {
  if (native_container_)
    native_container_->TabContentsFocused(tab_contents);
}

void TabContentsContainer::SetFastResize(bool fast_resize) {
  if (native_container_)
    native_container_->SetFastResize(fast_resize);
}

////////////////////////////////////////////////////////////////////////////////
// TabContentsContainer, NotificationObserver implementation:

void TabContentsContainer::Observe(NotificationType type,
                                   const NotificationSource& source,
                                   const NotificationDetails& details) {
  if (type == NotificationType::RENDER_VIEW_HOST_CHANGED) {
    RenderViewHostSwitchedDetails* switched_details =
        Details<RenderViewHostSwitchedDetails>(details).ptr();
    RenderViewHostChanged(switched_details->old_host,
                          switched_details->new_host);
  } else if (type == NotificationType::TAB_CONTENTS_DESTROYED) {
    TabContentsDestroyed(Source<TabContents>(source).ptr());
  } else {
    NOTREACHED();
  }
}

////////////////////////////////////////////////////////////////////////////////
// TabContentsContainer, View overrides:

void TabContentsContainer::Layout() {
#if defined(TOUCH_UI)
  views::View::Layout();
#else
  if (native_container_) {
    if (reserved_area_delegate_)
      reserved_area_delegate_->UpdateReservedContentsRect(this);
    native_container_->GetView()->SetBounds(0, 0, width(), height());
    native_container_->GetView()->Layout();
  }
#endif
}

AccessibilityTypes::Role TabContentsContainer::GetAccessibleRole() {
  return AccessibilityTypes::ROLE_WINDOW;
}

void TabContentsContainer::ViewHierarchyChanged(bool is_add,
                                                views::View* parent,
                                                views::View* child) {
#if defined(TOUCH_UI)
  views::View::ViewHierarchyChanged(is_add, parent, child);
#else
  if (is_add && child == this) {
    native_container_ = NativeTabContentsContainer::CreateNativeContainer(this);
    AddChildView(native_container_->GetView());
  }
#endif
}

////////////////////////////////////////////////////////////////////////////////
// TabContentsContainer, private:

void TabContentsContainer::AddObservers() {
  // TabContents can change their RenderViewHost and hence the HWND that is
  // shown and getting focused.  We need to keep track of that so we install
  // the focus subclass on the shown HWND so we intercept focus change events.
  registrar_.Add(this,
                 NotificationType::RENDER_VIEW_HOST_CHANGED,
                 Source<NavigationController>(&tab_contents_->controller()));

  registrar_.Add(this,
                 NotificationType::TAB_CONTENTS_DESTROYED,
                 Source<TabContents>(tab_contents_));
}

void TabContentsContainer::RemoveObservers() {
  registrar_.RemoveAll();
}

void TabContentsContainer::RenderViewHostChanged(RenderViewHost* old_host,
                                                 RenderViewHost* new_host) {
#if defined(TOUCH_UI)
  NOTIMPLEMENTED(); // TODO(anicolao)
#else
  if (new_host) {
    RenderWidgetHostViewChanged(
        old_host ? old_host->view() : NULL, new_host->view());
  }
  native_container_->RenderViewHostChanged(old_host, new_host);
#endif
}

void TabContentsContainer::TabContentsDestroyed(TabContents* contents) {
  // Sometimes, a TabContents is destroyed before we know about it. This allows
  // us to clean up our state in case this happens.
  DCHECK(contents == tab_contents_);
  ChangeTabContents(NULL);
}

void TabContentsContainer::RenderWidgetHostViewChanged(
    RenderWidgetHostView* old_view, RenderWidgetHostView* new_view) {
  // Carry over the reserved rect, if possible.
  if (old_view && new_view) {
    new_view->set_reserved_contents_rect(old_view->reserved_contents_rect());
  } else {
    if (reserved_area_delegate_)
      reserved_area_delegate_->UpdateReservedContentsRect(this);
  }
}