| 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 // We are owned by ExtensionHost, so views hierarchy shouldn't delete us! | 24 is_clipped_(false) { |
| 25 set_owned_by_client(); | 25 host_->set_view(this); |
| 26 | |
| 27 host_->SetExtensionView(this); | |
| 28 | 26 |
| 29 // This view needs to be focusable so it can act as the focused view for the | 27 // This view needs to be focusable so it can act as the focused view for the |
| 30 // focus manager. This is required to have SkipDefaultKeyEventProcessing | 28 // focus manager. This is required to have SkipDefaultKeyEventProcessing |
| 31 // called so the tab key events are forwarded to the renderer. | 29 // called so the tab key events are forwarded to the renderer. |
| 32 set_focusable(true); | 30 set_focusable(true); |
| 33 } | 31 } |
| 34 | 32 |
| 35 ExtensionViewViews::~ExtensionViewViews() { | 33 ExtensionViewViews::~ExtensionViewViews() { |
| 36 if (parent()) | 34 if (parent()) |
| 37 parent()->RemoveChildView(this); | 35 parent()->RemoveChildView(this); |
| 38 CleanUp(); | 36 CleanUp(); |
| 39 } | 37 } |
| 40 | 38 |
| 41 void ExtensionViewViews::SetBackground(const SkBitmap& background) { | 39 const extensions::Extension* ExtensionViewViews::extension() const { |
| 42 if (GetRenderViewHost()->IsRenderViewLive() && | 40 return host_->extension(); |
| 43 GetRenderViewHost()->GetView()) { | 41 } |
| 44 GetRenderViewHost()->GetView()->SetBackground(background); | 42 |
| 45 } else { | 43 content::RenderViewHost* ExtensionViewViews::render_view_host() const { |
| 46 pending_background_ = background; | 44 return host_->render_view_host(); |
| 45 } |
| 46 |
| 47 void ExtensionViewViews::DidStopLoading() { |
| 48 ShowIfCompletelyLoaded(); |
| 49 } |
| 50 |
| 51 void ExtensionViewViews::SetIsClipped(bool is_clipped) { |
| 52 if (is_clipped_ != is_clipped) { |
| 53 is_clipped_ = is_clipped; |
| 54 if (visible()) |
| 55 ShowIfCompletelyLoaded(); |
| 47 } | 56 } |
| 48 ShowIfCompletelyLoaded(); | 57 } |
| 58 |
| 59 gfx::NativeCursor ExtensionViewViews::GetCursor(const ui::MouseEvent& event) { |
| 60 return gfx::kNullCursor; |
| 49 } | 61 } |
| 50 | 62 |
| 51 void ExtensionViewViews::SetVisible(bool is_visible) { | 63 void ExtensionViewViews::SetVisible(bool is_visible) { |
| 52 if (is_visible != visible()) { | 64 if (is_visible != visible()) { |
| 53 NativeViewHost::SetVisible(is_visible); | 65 NativeViewHost::SetVisible(is_visible); |
| 54 | 66 |
| 55 // Also tell RenderWidgetHostView the new visibility. Despite its name, it | 67 // Also tell RenderWidgetHostView the new visibility. Despite its name, it |
| 56 // is not part of the View hierarchy and does not know about the change | 68 // is not part of the View hierarchy and does not know about the change |
| 57 // unless we tell it. | 69 // unless we tell it. |
| 58 if (GetRenderViewHost()->GetView()) { | 70 if (render_view_host()->GetView()) { |
| 59 if (is_visible) | 71 if (is_visible) |
| 60 GetRenderViewHost()->GetView()->Show(); | 72 render_view_host()->GetView()->Show(); |
| 61 else | 73 else |
| 62 GetRenderViewHost()->GetView()->Hide(); | 74 render_view_host()->GetView()->Hide(); |
| 63 } | 75 } |
| 64 } | 76 } |
| 65 } | 77 } |
| 66 | 78 |
| 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 | |
| 136 void ExtensionViewViews::CreateWidgetHostView() { | 79 void ExtensionViewViews::CreateWidgetHostView() { |
| 137 DCHECK(!initialized_); | 80 DCHECK(!initialized_); |
| 138 initialized_ = true; | 81 initialized_ = true; |
| 139 Attach(host_->host_contents()->GetView()->GetNativeView()); | 82 Attach(host_->host_contents()->GetView()->GetNativeView()); |
| 140 host_->CreateRenderViewSoon(); | 83 host_->CreateRenderViewSoon(); |
| 141 SetVisible(false); | 84 SetVisible(false); |
| 142 } | 85 } |
| 143 | 86 |
| 144 void ExtensionViewViews::ShowIfCompletelyLoaded() { | 87 void ExtensionViewViews::ShowIfCompletelyLoaded() { |
| 145 if (visible()) | 88 if (visible() || is_clipped_) |
| 146 return; | 89 return; |
| 147 | 90 |
| 148 // We wait to show the ExtensionViewViews until it has loaded, and the view | 91 // We wait to show the ExtensionViewViews until it has loaded, and the view |
| 149 // has actually been created. These can happen in different orders. | 92 // has actually been created. These can happen in different orders. |
| 150 if (host_->did_stop_loading()) { | 93 if (host_->did_stop_loading()) { |
| 151 SetVisible(true); | 94 SetVisible(true); |
| 152 ResizeDueToAutoResize(pending_preferred_size_); | 95 ResizeDueToAutoResize(pending_preferred_size_); |
| 153 } | 96 } |
| 154 } | 97 } |
| 155 | 98 |
| 156 void ExtensionViewViews::CleanUp() { | 99 void ExtensionViewViews::CleanUp() { |
| 157 if (!initialized_) | 100 if (!initialized_) |
| 158 return; | 101 return; |
| 159 if (native_view()) | 102 if (native_view()) |
| 160 Detach(); | 103 Detach(); |
| 161 initialized_ = false; | 104 initialized_ = false; |
| 162 } | 105 } |
| 163 | 106 |
| 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 |
| 164 void ExtensionViewViews::PreferredSizeChanged() { | 137 void ExtensionViewViews::PreferredSizeChanged() { |
| 165 View::PreferredSizeChanged(); | 138 View::PreferredSizeChanged(); |
| 166 if (container_) | 139 if (container_) |
| 167 container_->OnExtensionSizeChanged(this, gfx::Size()); | 140 container_->OnExtensionSizeChanged(this); |
| 168 } | 141 } |
| 169 | 142 |
| 170 bool ExtensionViewViews::SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) { | 143 bool ExtensionViewViews::SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) { |
| 171 // Let the tab key event be processed by the renderer (instead of moving the | 144 // Let the tab key event be processed by the renderer (instead of moving the |
| 172 // focus to the next focusable view). Also handle Backspace, since otherwise | 145 // focus to the next focusable view). Also handle Backspace, since otherwise |
| 173 // (on Windows at least), pressing Backspace, when focus is on a text field | 146 // (on Windows at least), pressing Backspace, when focus is on a text field |
| 174 // within the ExtensionViewViews, will navigate the page back instead of | 147 // within the ExtensionViewViews, will navigate the page back instead of |
| 175 // erasing a character. | 148 // erasing a character. |
| 176 return (e.key_code() == ui::VKEY_TAB || e.key_code() == ui::VKEY_BACK); | 149 return (e.key_code() == ui::VKEY_TAB || e.key_code() == ui::VKEY_BACK); |
| 177 } | 150 } |
| 178 | 151 |
| 179 void ExtensionViewViews::OnBoundsChanged(const gfx::Rect& previous_bounds) { | 152 void ExtensionViewViews::OnBoundsChanged(const gfx::Rect& previous_bounds) { |
| 180 // Propagate the new size to RenderWidgetHostView. | 153 // Propagate the new size to RenderWidgetHostView. |
| 181 // We can't send size zero because RenderWidget DCHECKs that. | 154 // We can't send size zero because RenderWidget DCHECKs that. |
| 182 if (GetRenderViewHost()->GetView() && !bounds().IsEmpty()) | 155 if (render_view_host()->GetView() && !bounds().IsEmpty()) { |
| 183 GetRenderViewHost()->GetView()->SetSize(size()); | 156 render_view_host()->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(); |
| 184 } | 180 } |
| 185 | 181 |
| 186 void ExtensionViewViews::HandleKeyboardEvent( | 182 void ExtensionViewViews::HandleKeyboardEvent( |
| 187 const content::NativeWebKeyboardEvent& event) { | 183 const content::NativeWebKeyboardEvent& event) { |
| 188 unhandled_keyboard_event_handler_.HandleKeyboardEvent(event, | 184 unhandled_keyboard_event_handler_.HandleKeyboardEvent(event, |
| 189 GetFocusManager()); | 185 GetFocusManager()); |
| 190 } | 186 } |
| 191 | |
| 192 // static | |
| 193 ExtensionView* ExtensionView::Create(extensions::ExtensionHost* host, | |
| 194 Browser* browser) { | |
| 195 return new ExtensionViewViews(host, browser); | |
| 196 } | |
| OLD | NEW |