Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / views / extensions / extension_view_views.cc
blob15a077620d53353e0cdbaaa646959ba31fddffec
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"
19 #if defined(USE_AURA)
20 #include "ui/base/cursor/cursor.h"
21 #endif
23 ExtensionViewViews::ExtensionViewViews(extensions::ExtensionHost* host,
24 Browser* browser)
25 : host_(host),
26 browser_(browser),
27 initialized_(false),
28 container_(NULL),
29 is_clipped_(false) {
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.
33 SetFocusable(true);
36 ExtensionViewViews::~ExtensionViewViews() {
37 if (parent())
38 parent()->RemoveChildView(this);
39 CleanUp();
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
54 // unless we tell it.
55 content::RenderWidgetHostView* host_view = render_view_host()->GetView();
56 if (host_view) {
57 if (is_visible)
58 host_view->Show();
59 else
60 host_view->Hide();
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;
79 if (visible())
80 ShowIfCompletelyLoaded();
84 Browser* ExtensionViewViews::GetBrowser() {
85 return browser_;
88 gfx::NativeView ExtensionViewViews::GetNativeView() {
89 return native_view();
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.
95 if (!visible()) {
96 pending_preferred_size_ = new_size;
97 return;
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) {
116 if (browser_) {
117 // Handle lower priority browser shortcuts such as Ctrl-f.
118 browser_->HandleKeyboardEvent(source, event);
119 return;
122 unhandled_keyboard_event_handler_.HandleKeyboardEvent(event,
123 GetFocusManager());
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();
148 if (container_)
149 container_->OnExtensionSizeChanged(this);
152 void ExtensionViewViews::OnFocus() {
153 host()->host_contents()->Focus();
156 void ExtensionViewViews::CreateWidgetHostView() {
157 DCHECK(!initialized_);
158 initialized_ = true;
159 Attach(host_->host_contents()->GetNativeView());
160 host_->CreateRenderViewSoon();
161 SetVisible(false);
164 void ExtensionViewViews::ShowIfCompletelyLoaded() {
165 if (visible() || is_clipped_)
166 return;
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()) {
171 SetVisible(true);
172 ResizeDueToAutoResize(pending_preferred_size_);
176 void ExtensionViewViews::CleanUp() {
177 if (!initialized_)
178 return;
179 if (native_view())
180 Detach();
181 initialized_ = false;
184 namespace extensions {
186 // static
187 scoped_ptr<ExtensionView> ExtensionViewHost::CreateExtensionView(
188 ExtensionViewHost* host,
189 Browser* browser) {
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
192 // hierarchy.
193 view->set_owned_by_client();
194 return view.Pass();
197 } // namespace extensions