1 // Copyright (c) 2012 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.
5 #include "chrome/browser/ui/views/extensions/extension_view_views.h"
7 #include "chrome/browser/extensions/extension_view_host.h"
8 #include "chrome/browser/ui/browser.h"
9 #include "chrome/browser/ui/views/extensions/extension_popup.h"
10 #include "content/public/browser/content_browser_client.h"
11 #include "content/public/browser/render_view_host.h"
12 #include "content/public/browser/render_widget_host_view.h"
13 #include "content/public/browser/web_contents.h"
14 #include "extensions/browser/extension_host.h"
15 #include "extensions/common/view_type.h"
16 #include "ui/events/event.h"
17 #include "ui/views/widget/widget.h"
20 #include "ui/base/cursor/cursor.h"
23 ExtensionViewViews::ExtensionViewViews(extensions::ExtensionHost
* host
,
30 // This view needs to be focusable so it can act as the focused view for the
31 // focus manager. This is required to have SkipDefaultKeyEventProcessing
32 // called so the tab key events are forwarded to the renderer.
36 ExtensionViewViews::~ExtensionViewViews() {
38 parent()->RemoveChildView(this);
42 gfx::Size
ExtensionViewViews::GetMinimumSize() const {
43 // If the minimum size has never been set, returns the preferred size (same
44 // behavior as views::View).
45 return (minimum_size_
== gfx::Size()) ? GetPreferredSize() : minimum_size_
;
48 void ExtensionViewViews::SetVisible(bool is_visible
) {
49 if (is_visible
!= visible()) {
50 NativeViewHost::SetVisible(is_visible
);
52 // Also tell RenderWidgetHostView the new visibility. Despite its name, it
53 // is not part of the View hierarchy and does not know about the change
55 content::RenderWidgetHostView
* host_view
= render_view_host()->GetView();
65 gfx::NativeCursor
ExtensionViewViews::GetCursor(const ui::MouseEvent
& event
) {
66 return gfx::kNullCursor
;
69 void ExtensionViewViews::ViewHierarchyChanged(
70 const ViewHierarchyChangedDetails
& details
) {
71 NativeViewHost::ViewHierarchyChanged(details
);
72 if (details
.is_add
&& GetWidget() && !initialized_
)
73 CreateWidgetHostView();
76 void ExtensionViewViews::SetIsClipped(bool is_clipped
) {
77 if (is_clipped_
!= is_clipped
) {
78 is_clipped_
= is_clipped
;
80 ShowIfCompletelyLoaded();
84 Browser
* ExtensionViewViews::GetBrowser() {
88 gfx::NativeView
ExtensionViewViews::GetNativeView() {
92 void ExtensionViewViews::ResizeDueToAutoResize(const gfx::Size
& new_size
) {
93 // Don't actually do anything with this information until we have been shown.
94 // Size changes will not be honored by lower layers while we are hidden.
96 pending_preferred_size_
= new_size
;
100 if (new_size
!= GetPreferredSize())
101 SetPreferredSize(new_size
);
104 void ExtensionViewViews::RenderViewCreated() {
105 extensions::ViewType host_type
= host_
->extension_host_type();
106 if (host_type
== extensions::VIEW_TYPE_EXTENSION_POPUP
) {
107 render_view_host()->EnableAutoResize(
108 gfx::Size(ExtensionPopup::kMinWidth
, ExtensionPopup::kMinHeight
),
109 gfx::Size(ExtensionPopup::kMaxWidth
, ExtensionPopup::kMaxHeight
));
113 void ExtensionViewViews::HandleKeyboardEvent(
114 content::WebContents
* source
,
115 const content::NativeWebKeyboardEvent
& event
) {
117 // Handle lower priority browser shortcuts such as Ctrl-f.
118 browser_
->HandleKeyboardEvent(source
, event
);
122 unhandled_keyboard_event_handler_
.HandleKeyboardEvent(event
,
126 void ExtensionViewViews::DidStopLoading() {
127 ShowIfCompletelyLoaded();
130 bool ExtensionViewViews::SkipDefaultKeyEventProcessing(const ui::KeyEvent
& e
) {
131 // Let the tab key event be processed by the renderer (instead of moving the
132 // focus to the next focusable view). Also handle Backspace, since otherwise
133 // (on Windows at least), pressing Backspace, when focus is on a text field
134 // within the ExtensionViewViews, will navigate the page back instead of
135 // erasing a character.
136 return (e
.key_code() == ui::VKEY_TAB
|| e
.key_code() == ui::VKEY_BACK
);
139 void ExtensionViewViews::OnBoundsChanged(const gfx::Rect
& previous_bounds
) {
140 // Propagate the new size to RenderWidgetHostView.
141 // We can't send size zero because RenderWidget DCHECKs that.
142 if (render_view_host()->GetView() && !bounds().IsEmpty())
143 render_view_host()->GetView()->SetSize(size());
146 void ExtensionViewViews::PreferredSizeChanged() {
147 View::PreferredSizeChanged();
149 container_
->OnExtensionSizeChanged(this);
152 void ExtensionViewViews::OnFocus() {
153 host()->host_contents()->Focus();
156 void ExtensionViewViews::CreateWidgetHostView() {
157 DCHECK(!initialized_
);
159 Attach(host_
->host_contents()->GetNativeView());
160 host_
->CreateRenderViewSoon();
164 void ExtensionViewViews::ShowIfCompletelyLoaded() {
165 if (visible() || is_clipped_
)
168 // We wait to show the ExtensionViewViews until it has loaded, and the view
169 // has actually been created. These can happen in different orders.
170 if (host_
->has_loaded_once()) {
172 ResizeDueToAutoResize(pending_preferred_size_
);
176 void ExtensionViewViews::CleanUp() {
181 initialized_
= false;
184 namespace extensions
{
187 scoped_ptr
<ExtensionView
> ExtensionViewHost::CreateExtensionView(
188 ExtensionViewHost
* host
,
190 scoped_ptr
<ExtensionViewViews
> view(new ExtensionViewViews(host
, browser
));
191 // We own |view_|, so don't auto delete when it's removed from the view
193 view
->set_owned_by_client();
197 } // namespace extensions