| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/views/extensions/extension_view_views.h" | 5 #include "chrome/browser/ui/views/extensions/extension_view_views.h" |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/extension_host.h" | 7 #include "chrome/browser/extensions/extension_host.h" |
| 8 #include "chrome/browser/ui/views/extensions/extension_popup.h" | 8 #include "chrome/browser/ui/views/extensions/extension_popup.h" |
| 9 #include "chrome/common/view_type.h" | 9 #include "chrome/common/view_type.h" |
| 10 #include "content/public/browser/content_browser_client.h" | 10 #include "content/public/browser/content_browser_client.h" |
| 11 #include "content/public/browser/render_view_host.h" | 11 #include "content/public/browser/render_view_host.h" |
| 12 #include "content/public/browser/render_widget_host_view.h" | 12 #include "content/public/browser/render_widget_host_view.h" |
| 13 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
| 14 #include "content/public/browser/web_contents_view.h" | 14 #include "content/public/browser/web_contents_view.h" |
| 15 #include "ui/base/events/event.h" | 15 #include "ui/base/events/event.h" |
| 16 #include "ui/views/widget/widget.h" | 16 #include "ui/views/widget/widget.h" |
| 17 | 17 |
| 18 ExtensionViewViews::ExtensionViewViews(extensions::ExtensionHost* host, | 18 ExtensionViewViews::ExtensionViewViews(extensions::ExtensionHost* host, |
| 19 Browser* browser) | 19 Browser* browser) |
| 20 : host_(host), | 20 : host_(host), |
| 21 browser_(browser), | 21 browser_(browser), |
| 22 initialized_(false), | 22 initialized_(false), |
| 23 container_(NULL), | 23 container_(NULL) { |
| 24 is_clipped_(false) { | 24 // We are owned by ExtensionHost, so views hierarchy shouldn't delete us! |
| 25 host_->set_view(this); | 25 set_owned_by_client(); |
| 26 |
| 27 host_->SetExtensionView(this); |
| 26 | 28 |
| 27 // This view needs to be focusable so it can act as the focused view for the | 29 // This view needs to be focusable so it can act as the focused view for the |
| 28 // focus manager. This is required to have SkipDefaultKeyEventProcessing | 30 // focus manager. This is required to have SkipDefaultKeyEventProcessing |
| 29 // called so the tab key events are forwarded to the renderer. | 31 // called so the tab key events are forwarded to the renderer. |
| 30 set_focusable(true); | 32 set_focusable(true); |
| 31 } | 33 } |
| 32 | 34 |
| 33 ExtensionViewViews::~ExtensionViewViews() { | 35 ExtensionViewViews::~ExtensionViewViews() { |
| 34 if (parent()) | 36 if (parent()) |
| 35 parent()->RemoveChildView(this); | 37 parent()->RemoveChildView(this); |
| 36 CleanUp(); | 38 CleanUp(); |
| 37 } | 39 } |
| 38 | 40 |
| 39 const extensions::Extension* ExtensionViewViews::extension() const { | 41 void ExtensionViewViews::SetBackground(const SkBitmap& background) { |
| 40 return host_->extension(); | 42 if (GetRenderViewHost()->IsRenderViewLive() && |
| 41 } | 43 GetRenderViewHost()->GetView()) { |
| 42 | 44 GetRenderViewHost()->GetView()->SetBackground(background); |
| 43 content::RenderViewHost* ExtensionViewViews::render_view_host() const { | 45 } else { |
| 44 return host_->render_view_host(); | 46 pending_background_ = background; |
| 45 } | 47 } |
| 46 | |
| 47 void ExtensionViewViews::DidStopLoading() { | |
| 48 ShowIfCompletelyLoaded(); | 48 ShowIfCompletelyLoaded(); |
| 49 } | 49 } |
| 50 | 50 |
| 51 void ExtensionViewViews::SetIsClipped(bool is_clipped) { | |
| 52 if (is_clipped_ != is_clipped) { | |
| 53 is_clipped_ = is_clipped; | |
| 54 if (visible()) | |
| 55 ShowIfCompletelyLoaded(); | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 gfx::NativeCursor ExtensionViewViews::GetCursor(const ui::MouseEvent& event) { | |
| 60 return gfx::kNullCursor; | |
| 61 } | |
| 62 | |
| 63 void ExtensionViewViews::SetVisible(bool is_visible) { | 51 void ExtensionViewViews::SetVisible(bool is_visible) { |
| 64 if (is_visible != visible()) { | 52 if (is_visible != visible()) { |
| 65 NativeViewHost::SetVisible(is_visible); | 53 NativeViewHost::SetVisible(is_visible); |
| 66 | 54 |
| 67 // Also tell RenderWidgetHostView the new visibility. Despite its name, it | 55 // Also tell RenderWidgetHostView the new visibility. Despite its name, it |
| 68 // is not part of the View hierarchy and does not know about the change | 56 // is not part of the View hierarchy and does not know about the change |
| 69 // unless we tell it. | 57 // unless we tell it. |
| 70 if (render_view_host()->GetView()) { | 58 if (GetRenderViewHost()->GetView()) { |
| 71 if (is_visible) | 59 if (is_visible) |
| 72 render_view_host()->GetView()->Show(); | 60 GetRenderViewHost()->GetView()->Show(); |
| 73 else | 61 else |
| 74 render_view_host()->GetView()->Hide(); | 62 GetRenderViewHost()->GetView()->Hide(); |
| 75 } | 63 } |
| 76 } | 64 } |
| 77 } | 65 } |
| 78 | 66 |
| 67 gfx::NativeCursor ExtensionViewViews::GetCursor(const ui::MouseEvent& event) { |
| 68 return gfx::kNullCursor; |
| 69 } |
| 70 |
| 71 void ExtensionViewViews::ViewHierarchyChanged(bool is_add, |
| 72 views::View* parent, |
| 73 views::View* child) { |
| 74 NativeViewHost::ViewHierarchyChanged(is_add, parent, child); |
| 75 if (is_add && GetWidget() && !initialized_) |
| 76 CreateWidgetHostView(); |
| 77 } |
| 78 |
| 79 Browser* ExtensionViewViews::GetBrowser() { |
| 80 return browser_; |
| 81 } |
| 82 |
| 83 const Browser* ExtensionViewViews::GetBrowser() const { |
| 84 return browser_; |
| 85 } |
| 86 |
| 87 gfx::NativeView ExtensionViewViews::GetNativeView() { |
| 88 return host_->host_contents()->GetView()->GetNativeView(); |
| 89 } |
| 90 |
| 91 content::RenderViewHost* ExtensionViewViews::GetRenderViewHost() const { |
| 92 return host_->render_view_host(); |
| 93 } |
| 94 |
| 95 void ExtensionViewViews::SetContainer(ExtensionViewContainer* container) { |
| 96 container_ = container; |
| 97 } |
| 98 |
| 99 void ExtensionViewViews::ResizeDueToAutoResize(const gfx::Size& new_size) { |
| 100 // Don't actually do anything with this information until we have been shown. |
| 101 // Size changes will not be honored by lower layers while we are hidden. |
| 102 if (!visible()) { |
| 103 pending_preferred_size_ = new_size; |
| 104 return; |
| 105 } |
| 106 |
| 107 gfx::Size preferred_size = GetPreferredSize(); |
| 108 if (new_size != preferred_size) |
| 109 SetPreferredSize(new_size); |
| 110 } |
| 111 |
| 112 void ExtensionViewViews::RenderViewCreated() { |
| 113 if (!pending_background_.empty() && GetRenderViewHost()->GetView()) { |
| 114 GetRenderViewHost()->GetView()->SetBackground(pending_background_); |
| 115 pending_background_.reset(); |
| 116 } |
| 117 |
| 118 chrome::ViewType host_type = host_->extension_host_type(); |
| 119 if (host_type == chrome::VIEW_TYPE_EXTENSION_POPUP) { |
| 120 gfx::Size min_size(ExtensionPopup::kMinWidth, |
| 121 ExtensionPopup::kMinHeight); |
| 122 gfx::Size max_size(ExtensionPopup::kMaxWidth, |
| 123 ExtensionPopup::kMaxHeight); |
| 124 GetRenderViewHost()->EnableAutoResize(min_size, max_size); |
| 125 } |
| 126 } |
| 127 |
| 128 void ExtensionViewViews::DidStopLoading() { |
| 129 ShowIfCompletelyLoaded(); |
| 130 } |
| 131 |
| 132 void ExtensionViewViews::WindowFrameChanged() { |
| 133 NOTIMPLEMENTED(); |
| 134 } |
| 135 |
| 79 void ExtensionViewViews::CreateWidgetHostView() { | 136 void ExtensionViewViews::CreateWidgetHostView() { |
| 80 DCHECK(!initialized_); | 137 DCHECK(!initialized_); |
| 81 initialized_ = true; | 138 initialized_ = true; |
| 82 Attach(host_->host_contents()->GetView()->GetNativeView()); | 139 Attach(host_->host_contents()->GetView()->GetNativeView()); |
| 83 host_->CreateRenderViewSoon(); | 140 host_->CreateRenderViewSoon(); |
| 84 SetVisible(false); | 141 SetVisible(false); |
| 85 } | 142 } |
| 86 | 143 |
| 87 void ExtensionViewViews::ShowIfCompletelyLoaded() { | 144 void ExtensionViewViews::ShowIfCompletelyLoaded() { |
| 88 if (visible() || is_clipped_) | 145 if (visible()) |
| 89 return; | 146 return; |
| 90 | 147 |
| 91 // We wait to show the ExtensionViewViews until it has loaded, and the view | 148 // We wait to show the ExtensionViewViews until it has loaded, and the view |
| 92 // has actually been created. These can happen in different orders. | 149 // has actually been created. These can happen in different orders. |
| 93 if (host_->did_stop_loading()) { | 150 if (host_->did_stop_loading()) { |
| 94 SetVisible(true); | 151 SetVisible(true); |
| 95 ResizeDueToAutoResize(pending_preferred_size_); | 152 ResizeDueToAutoResize(pending_preferred_size_); |
| 96 } | 153 } |
| 97 } | 154 } |
| 98 | 155 |
| 99 void ExtensionViewViews::CleanUp() { | 156 void ExtensionViewViews::CleanUp() { |
| 100 if (!initialized_) | 157 if (!initialized_) |
| 101 return; | 158 return; |
| 102 if (native_view()) | 159 if (native_view()) |
| 103 Detach(); | 160 Detach(); |
| 104 initialized_ = false; | 161 initialized_ = false; |
| 105 } | 162 } |
| 106 | 163 |
| 107 void ExtensionViewViews::SetBackground(const SkBitmap& background) { | |
| 108 if (render_view_host()->IsRenderViewLive() && render_view_host()->GetView()) { | |
| 109 render_view_host()->GetView()->SetBackground(background); | |
| 110 } else { | |
| 111 pending_background_ = background; | |
| 112 } | |
| 113 ShowIfCompletelyLoaded(); | |
| 114 } | |
| 115 | |
| 116 void ExtensionViewViews::ResizeDueToAutoResize(const gfx::Size& new_size) { | |
| 117 // Don't actually do anything with this information until we have been shown. | |
| 118 // Size changes will not be honored by lower layers while we are hidden. | |
| 119 if (!visible()) { | |
| 120 pending_preferred_size_ = new_size; | |
| 121 return; | |
| 122 } | |
| 123 | |
| 124 gfx::Size preferred_size = GetPreferredSize(); | |
| 125 if (new_size != preferred_size) | |
| 126 SetPreferredSize(new_size); | |
| 127 } | |
| 128 | |
| 129 void ExtensionViewViews::ViewHierarchyChanged(bool is_add, | |
| 130 views::View* parent, | |
| 131 views::View* child) { | |
| 132 NativeViewHost::ViewHierarchyChanged(is_add, parent, child); | |
| 133 if (is_add && GetWidget() && !initialized_) | |
| 134 CreateWidgetHostView(); | |
| 135 } | |
| 136 | |
| 137 void ExtensionViewViews::PreferredSizeChanged() { | 164 void ExtensionViewViews::PreferredSizeChanged() { |
| 138 View::PreferredSizeChanged(); | 165 View::PreferredSizeChanged(); |
| 139 if (container_) | 166 if (container_) |
| 140 container_->OnExtensionSizeChanged(this); | 167 container_->OnExtensionSizeChanged(this, gfx::Size()); |
| 141 } | 168 } |
| 142 | 169 |
| 143 bool ExtensionViewViews::SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) { | 170 bool ExtensionViewViews::SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) { |
| 144 // Let the tab key event be processed by the renderer (instead of moving the | 171 // Let the tab key event be processed by the renderer (instead of moving the |
| 145 // focus to the next focusable view). Also handle Backspace, since otherwise | 172 // focus to the next focusable view). Also handle Backspace, since otherwise |
| 146 // (on Windows at least), pressing Backspace, when focus is on a text field | 173 // (on Windows at least), pressing Backspace, when focus is on a text field |
| 147 // within the ExtensionViewViews, will navigate the page back instead of | 174 // within the ExtensionViewViews, will navigate the page back instead of |
| 148 // erasing a character. | 175 // erasing a character. |
| 149 return (e.key_code() == ui::VKEY_TAB || e.key_code() == ui::VKEY_BACK); | 176 return (e.key_code() == ui::VKEY_TAB || e.key_code() == ui::VKEY_BACK); |
| 150 } | 177 } |
| 151 | 178 |
| 152 void ExtensionViewViews::OnBoundsChanged(const gfx::Rect& previous_bounds) { | 179 void ExtensionViewViews::OnBoundsChanged(const gfx::Rect& previous_bounds) { |
| 153 // Propagate the new size to RenderWidgetHostView. | 180 // Propagate the new size to RenderWidgetHostView. |
| 154 // We can't send size zero because RenderWidget DCHECKs that. | 181 // We can't send size zero because RenderWidget DCHECKs that. |
| 155 if (render_view_host()->GetView() && !bounds().IsEmpty()) { | 182 if (GetRenderViewHost()->GetView() && !bounds().IsEmpty()) |
| 156 render_view_host()->GetView()->SetSize(size()); | 183 GetRenderViewHost()->GetView()->SetSize(size()); |
| 157 | |
| 158 if (container_) | |
| 159 container_->OnViewWasResized(); | |
| 160 } | |
| 161 } | |
| 162 | |
| 163 void ExtensionViewViews::RenderViewCreated() { | |
| 164 if (!pending_background_.empty() && render_view_host()->GetView()) { | |
| 165 render_view_host()->GetView()->SetBackground(pending_background_); | |
| 166 pending_background_.reset(); | |
| 167 } | |
| 168 | |
| 169 chrome::ViewType host_type = host_->extension_host_type(); | |
| 170 if (host_type == chrome::VIEW_TYPE_EXTENSION_POPUP) { | |
| 171 gfx::Size min_size(ExtensionPopup::kMinWidth, | |
| 172 ExtensionPopup::kMinHeight); | |
| 173 gfx::Size max_size(ExtensionPopup::kMaxWidth, | |
| 174 ExtensionPopup::kMaxHeight); | |
| 175 render_view_host()->EnableAutoResize(min_size, max_size); | |
| 176 } | |
| 177 | |
| 178 if (container_) | |
| 179 container_->OnViewWasResized(); | |
| 180 } | 184 } |
| 181 | 185 |
| 182 void ExtensionViewViews::HandleKeyboardEvent( | 186 void ExtensionViewViews::HandleKeyboardEvent( |
| 183 const content::NativeWebKeyboardEvent& event) { | 187 const content::NativeWebKeyboardEvent& event) { |
| 184 unhandled_keyboard_event_handler_.HandleKeyboardEvent(event, | 188 unhandled_keyboard_event_handler_.HandleKeyboardEvent(event, |
| 185 GetFocusManager()); | 189 GetFocusManager()); |
| 186 } | 190 } |
| 191 |
| 192 // static |
| 193 ExtensionView* ExtensionView::Create(extensions::ExtensionHost* host, |
| 194 Browser* browser) { |
| 195 return new ExtensionViewViews(host, browser); |
| 196 } |
| OLD | NEW |