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
|
// Copyright (c) 2006-2009 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/extensions/extension_view.h"
#include "chrome/browser/extensions/extension_host.h"
#include "chrome/browser/renderer_host/render_view_host.h"
#include "chrome/browser/renderer_host/render_widget_host_view.h"
#if defined(OS_WIN)
#include "chrome/browser/renderer_host/render_widget_host_view_win.h"
#endif
#include "views/widget/widget.h"
ExtensionView::ExtensionView(ExtensionHost* host, Browser* browser)
: host_(host), browser_(browser),
initialized_(false), pending_preferred_width_(0), container_(NULL),
is_clipped_(false) {
host_->set_view(this);
}
ExtensionView::~ExtensionView() {
View* parent = GetParent();
if (parent)
parent->RemoveChildView(this);
CleanUp();
}
Extension* ExtensionView::extension() const {
return host_->extension();
}
RenderViewHost* ExtensionView::render_view_host() const {
return host_->render_view_host();
}
void ExtensionView::DidStopLoading() {
ShowIfCompletelyLoaded();
}
void ExtensionView::SetIsClipped(bool is_clipped) {
if (is_clipped_ != is_clipped) {
is_clipped_ = is_clipped;
if (IsVisible())
ShowIfCompletelyLoaded();
}
}
void ExtensionView::SetVisible(bool is_visible) {
if (is_visible != IsVisible()) {
NativeViewHost::SetVisible(is_visible);
// Also tell RenderWidgetHostView the new visibility. Despite its name, it
// is not part of the View heirarchy and does not know about the change
// unless we tell it.
if (render_view_host()->view()) {
if (is_visible)
render_view_host()->view()->Show();
else
render_view_host()->view()->Hide();
}
}
}
void ExtensionView::DidChangeBounds(const gfx::Rect& previous,
const gfx::Rect& current) {
View::DidChangeBounds(previous, current);
// Propagate the new size to RenderWidgetHostView.
// We can't send size zero because RenderWidget DCHECKs that.
if (render_view_host()->view() && !current.IsEmpty())
render_view_host()->view()->SetSize(gfx::Size(width(), height()));
}
void ExtensionView::CreateWidgetHostView() {
DCHECK(!initialized_);
initialized_ = true;
RenderWidgetHostView* view =
RenderWidgetHostView::CreateViewForWidget(render_view_host());
// TODO(mpcomplete): RWHV needs a cross-platform Init function.
#if defined(OS_WIN)
// Create the HWND. Note:
// RenderWidgetHostHWND supports windowed plugins, but if we ever also
// wanted to support constrained windows with this, we would need an
// additional HWND to parent off of because windowed plugin HWNDs cannot
// exist in the same z-order as constrained windows.
RenderWidgetHostViewWin* view_win =
static_cast<RenderWidgetHostViewWin*>(view);
HWND hwnd = view_win->Create(GetWidget()->GetNativeView());
view_win->ShowWindow(SW_SHOW);
Attach(hwnd);
#else
NOTIMPLEMENTED();
#endif
host_->CreateRenderViewSoon(view);
SetVisible(false);
}
void ExtensionView::ShowIfCompletelyLoaded() {
if (IsVisible() || is_clipped_)
return;
// We wait to show the ExtensionView until it has loaded, and the view has
// actually been created. These can happen in different orders.
if (host_->did_stop_loading()) {
// For toolstrips, also wait until our parent has given us a background.
if (host_->GetRenderViewType() == ViewType::EXTENSION_TOOLSTRIP &&
render_view_host()->view()->background().empty()) {
return;
}
SetVisible(true);
UpdatePreferredWidth(pending_preferred_width_);
}
}
void ExtensionView::CleanUp() {
if (!initialized_)
return;
if (native_view())
Detach();
initialized_ = false;
}
void ExtensionView::SetBackground(const SkBitmap& background) {
if (render_view_host()->IsRenderViewLive() && render_view_host()->view()) {
render_view_host()->view()->SetBackground(background);
} else {
pending_background_ = background;
}
ShowIfCompletelyLoaded();
}
void ExtensionView::UpdatePreferredWidth(int pref_width) {
// Don't actually do anything with this information until we have been shown.
// Size changes will not be honored by lower layers while we are hidden.
gfx::Size preferred_size = GetPreferredSize();
if (!IsVisible()) {
pending_preferred_width_ = pref_width;
} else if (pref_width > 0 && pref_width != preferred_size.width()) {
if (preferred_size.height() == 0)
preferred_size.set_height(height());
SetPreferredSize(gfx::Size(pref_width, preferred_size.height()));
}
}
void ExtensionView::ViewHierarchyChanged(bool is_add,
views::View *parent,
views::View *child) {
NativeViewHost::ViewHierarchyChanged(is_add, parent, child);
if (is_add && GetWidget() && !initialized_)
CreateWidgetHostView();
}
void ExtensionView::HandleMouseEvent() {
if (container_)
container_->OnExtensionMouseEvent(this);
}
void ExtensionView::HandleMouseLeave() {
if (container_)
container_->OnExtensionMouseLeave(this);
}
void ExtensionView::RenderViewCreated() {
if (!pending_background_.empty() && render_view_host()->view()) {
render_view_host()->view()->SetBackground(pending_background_);
pending_background_.reset();
}
}
|