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 void ExtensionViewViews::Init() {
85 // Initialization continues in ViewHierarchyChanged().
88 Browser* ExtensionViewViews::GetBrowser() {
92 gfx::NativeView ExtensionViewViews::GetNativeView() {
96 void ExtensionViewViews::ResizeDueToAutoResize(const gfx::Size& new_size) {
97 // Don't actually do anything with this information until we have been shown.
98 // Size changes will not be honored by lower layers while we are hidden.
100 pending_preferred_size_ = new_size;
104 if (new_size != GetPreferredSize())
105 SetPreferredSize(new_size);
108 void ExtensionViewViews::RenderViewCreated() {
109 extensions::ViewType host_type = host_->extension_host_type();
110 if (host_type == extensions::VIEW_TYPE_EXTENSION_POPUP) {
111 render_view_host()->EnableAutoResize(
112 gfx::Size(ExtensionPopup::kMinWidth, ExtensionPopup::kMinHeight),
113 gfx::Size(ExtensionPopup::kMaxWidth, ExtensionPopup::kMaxHeight));
117 void ExtensionViewViews::HandleKeyboardEvent(
118 content::WebContents* source,
119 const content::NativeWebKeyboardEvent& event) {
121 // Handle lower priority browser shortcuts such as Ctrl-f.
122 browser_->HandleKeyboardEvent(source, event);
126 unhandled_keyboard_event_handler_.HandleKeyboardEvent(event,
130 void ExtensionViewViews::DidStopLoading() {
131 ShowIfCompletelyLoaded();
134 bool ExtensionViewViews::SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) {
135 // Let the tab key event be processed by the renderer (instead of moving the
136 // focus to the next focusable view). Also handle Backspace, since otherwise
137 // (on Windows at least), pressing Backspace, when focus is on a text field
138 // within the ExtensionViewViews, will navigate the page back instead of
139 // erasing a character.
140 return (e.key_code() == ui::VKEY_TAB || e.key_code() == ui::VKEY_BACK);
143 void ExtensionViewViews::OnBoundsChanged(const gfx::Rect& previous_bounds) {
144 // Propagate the new size to RenderWidgetHostView.
145 // We can't send size zero because RenderWidget DCHECKs that.
146 if (render_view_host()->GetView() && !bounds().IsEmpty())
147 render_view_host()->GetView()->SetSize(size());
150 void ExtensionViewViews::PreferredSizeChanged() {
151 View::PreferredSizeChanged();
153 container_->OnExtensionSizeChanged(this);
156 void ExtensionViewViews::OnFocus() {
157 host()->host_contents()->Focus();
160 void ExtensionViewViews::CreateWidgetHostView() {
161 DCHECK(!initialized_);
163 Attach(host_->host_contents()->GetNativeView());
164 host_->CreateRenderViewSoon();
168 void ExtensionViewViews::ShowIfCompletelyLoaded() {
169 if (visible() || is_clipped_)
172 // We wait to show the ExtensionViewViews until it has loaded, and the view
173 // has actually been created. These can happen in different orders.
174 if (host_->did_stop_loading()) {
176 ResizeDueToAutoResize(pending_preferred_size_);
180 void ExtensionViewViews::CleanUp() {
185 initialized_ = false;
188 namespace extensions {
191 scoped_ptr<ExtensionView> ExtensionViewHost::CreateExtensionView(
192 ExtensionViewHost* host,
194 scoped_ptr<ExtensionViewViews> view(new ExtensionViewViews(host, browser));
195 // We own |view_|, so don't auto delete when it's removed from the view
197 view->set_owned_by_client();
198 return view.PassAs<ExtensionView>();
201 } // namespace extensions