| 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 is_clipped_(false) { |
| 25 host_->set_view(this); | 25 host_->SetExtensionView(this); |
| 26 | 26 |
| 27 // 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 |
| 28 // focus manager. This is required to have SkipDefaultKeyEventProcessing | 28 // focus manager. This is required to have SkipDefaultKeyEventProcessing |
| 29 // called so the tab key events are forwarded to the renderer. | 29 // called so the tab key events are forwarded to the renderer. |
| 30 set_focusable(true); | 30 set_focusable(true); |
| 31 } | 31 } |
| 32 | 32 |
| 33 ExtensionViewViews::~ExtensionViewViews() { | 33 ExtensionViewViews::~ExtensionViewViews() { |
| 34 if (parent()) | 34 if (parent()) |
| 35 parent()->RemoveChildView(this); | 35 parent()->RemoveChildView(this); |
| 36 CleanUp(); | 36 CleanUp(); |
| 37 } | 37 } |
| 38 | 38 |
| 39 const extensions::Extension* ExtensionViewViews::extension() const { | 39 const extensions::Extension* ExtensionViewViews::extension() const { |
| 40 return host_->extension(); | 40 return host_->extension(); |
| 41 } | 41 } |
| 42 | 42 |
| 43 content::RenderViewHost* ExtensionViewViews::render_view_host() const { | 43 content::RenderViewHost* ExtensionViewViews::render_view_host() const { |
| 44 return host_->render_view_host(); | 44 return host_->render_view_host(); |
| 45 } | 45 } |
| 46 | 46 |
| 47 void ExtensionViewViews::DidStopLoading() { | |
| 48 ShowIfCompletelyLoaded(); | |
| 49 } | |
| 50 | |
| 51 void ExtensionViewViews::SetIsClipped(bool is_clipped) { | 47 void ExtensionViewViews::SetIsClipped(bool is_clipped) { |
| 52 if (is_clipped_ != is_clipped) { | 48 if (is_clipped_ != is_clipped) { |
| 53 is_clipped_ = is_clipped; | 49 is_clipped_ = is_clipped; |
| 54 if (visible()) | 50 if (visible()) |
| 55 ShowIfCompletelyLoaded(); | 51 ShowIfCompletelyLoaded(); |
| 56 } | 52 } |
| 57 } | 53 } |
| 58 | 54 |
| 59 gfx::NativeCursor ExtensionViewViews::GetCursor(const ui::MouseEvent& event) { | 55 gfx::NativeCursor ExtensionViewViews::GetCursor(const ui::MouseEvent& event) { |
| 60 return gfx::kNullCursor; | 56 return gfx::kNullCursor; |
| 61 } | 57 } |
| 62 | 58 |
| 63 void ExtensionViewViews::SetVisible(bool is_visible) { | 59 void ExtensionViewViews::SetVisible(bool is_visible) { |
| 64 if (is_visible != visible()) { | 60 if (is_visible != visible()) { |
| 65 NativeViewHost::SetVisible(is_visible); | 61 NativeViewHost::SetVisible(is_visible); |
| 66 | 62 |
| 67 // Also tell RenderWidgetHostView the new visibility. Despite its name, it | 63 // 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 | 64 // is not part of the View hierarchy and does not know about the change |
| 69 // unless we tell it. | 65 // unless we tell it. |
| 70 if (render_view_host()->GetView()) { | 66 if (render_view_host()->GetView()) { |
| 71 if (is_visible) | 67 if (is_visible) |
| 72 render_view_host()->GetView()->Show(); | 68 render_view_host()->GetView()->Show(); |
| 73 else | 69 else |
| 74 render_view_host()->GetView()->Hide(); | 70 render_view_host()->GetView()->Hide(); |
| 75 } | 71 } |
| 76 } | 72 } |
| 77 } | 73 } |
| 78 | 74 |
| 75 Browser* ExtensionViewViews::GetBrowser() { |
| 76 return browser_; |
| 77 } |
| 78 |
| 79 const Browser* ExtensionViewViews::GetBrowser() const { |
| 80 return browser_; |
| 81 } |
| 82 |
| 83 void ExtensionViewViews::ResizeDueToAutoResize(const gfx::Size& new_size) { |
| 84 // Don't actually do anything with this information until we have been shown. |
| 85 // Size changes will not be honored by lower layers while we are hidden. |
| 86 if (!visible()) { |
| 87 pending_preferred_size_ = new_size; |
| 88 return; |
| 89 } |
| 90 |
| 91 gfx::Size preferred_size = GetPreferredSize(); |
| 92 if (new_size != preferred_size) |
| 93 SetPreferredSize(new_size); |
| 94 } |
| 95 |
| 96 void ExtensionViewViews::RenderViewCreated() { |
| 97 if (!pending_background_.empty() && render_view_host()->GetView()) { |
| 98 render_view_host()->GetView()->SetBackground(pending_background_); |
| 99 pending_background_.reset(); |
| 100 } |
| 101 |
| 102 chrome::ViewType host_type = host_->extension_host_type(); |
| 103 if (host_type == chrome::VIEW_TYPE_EXTENSION_POPUP) { |
| 104 gfx::Size min_size(ExtensionPopup::kMinWidth, |
| 105 ExtensionPopup::kMinHeight); |
| 106 gfx::Size max_size(ExtensionPopup::kMaxWidth, |
| 107 ExtensionPopup::kMaxHeight); |
| 108 render_view_host()->EnableAutoResize(min_size, max_size); |
| 109 } |
| 110 |
| 111 if (container_) |
| 112 container_->OnViewWasResized(); |
| 113 } |
| 114 |
| 115 void ExtensionViewViews::DidStopLoading() { |
| 116 ShowIfCompletelyLoaded(); |
| 117 } |
| 118 |
| 79 void ExtensionViewViews::CreateWidgetHostView() { | 119 void ExtensionViewViews::CreateWidgetHostView() { |
| 80 DCHECK(!initialized_); | 120 DCHECK(!initialized_); |
| 81 initialized_ = true; | 121 initialized_ = true; |
| 82 Attach(host_->host_contents()->GetView()->GetNativeView()); | 122 Attach(host_->host_contents()->GetView()->GetNativeView()); |
| 83 host_->CreateRenderViewSoon(); | 123 host_->CreateRenderViewSoon(); |
| 84 SetVisible(false); | 124 SetVisible(false); |
| 85 } | 125 } |
| 86 | 126 |
| 87 void ExtensionViewViews::ShowIfCompletelyLoaded() { | 127 void ExtensionViewViews::ShowIfCompletelyLoaded() { |
| 88 if (visible() || is_clipped_) | 128 if (visible() || is_clipped_) |
| (...skipping 17 matching lines...) Expand all Loading... |
| 106 | 146 |
| 107 void ExtensionViewViews::SetBackground(const SkBitmap& background) { | 147 void ExtensionViewViews::SetBackground(const SkBitmap& background) { |
| 108 if (render_view_host()->IsRenderViewLive() && render_view_host()->GetView()) { | 148 if (render_view_host()->IsRenderViewLive() && render_view_host()->GetView()) { |
| 109 render_view_host()->GetView()->SetBackground(background); | 149 render_view_host()->GetView()->SetBackground(background); |
| 110 } else { | 150 } else { |
| 111 pending_background_ = background; | 151 pending_background_ = background; |
| 112 } | 152 } |
| 113 ShowIfCompletelyLoaded(); | 153 ShowIfCompletelyLoaded(); |
| 114 } | 154 } |
| 115 | 155 |
| 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, | 156 void ExtensionViewViews::ViewHierarchyChanged(bool is_add, |
| 130 views::View* parent, | 157 views::View* parent, |
| 131 views::View* child) { | 158 views::View* child) { |
| 132 NativeViewHost::ViewHierarchyChanged(is_add, parent, child); | 159 NativeViewHost::ViewHierarchyChanged(is_add, parent, child); |
| 133 if (is_add && GetWidget() && !initialized_) | 160 if (is_add && GetWidget() && !initialized_) |
| 134 CreateWidgetHostView(); | 161 CreateWidgetHostView(); |
| 135 } | 162 } |
| 136 | 163 |
| 137 void ExtensionViewViews::PreferredSizeChanged() { | 164 void ExtensionViewViews::PreferredSizeChanged() { |
| 138 View::PreferredSizeChanged(); | 165 View::PreferredSizeChanged(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 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 (render_view_host()->GetView() && !bounds().IsEmpty()) { |
| 156 render_view_host()->GetView()->SetSize(size()); | 183 render_view_host()->GetView()->SetSize(size()); |
| 157 | 184 |
| 158 if (container_) | 185 if (container_) |
| 159 container_->OnViewWasResized(); | 186 container_->OnViewWasResized(); |
| 160 } | 187 } |
| 161 } | 188 } |
| 162 | 189 |
| 163 void ExtensionViewViews::RenderViewCreated() { | 190 // static |
| 164 if (!pending_background_.empty() && render_view_host()->GetView()) { | 191 ExtensionView* ExtensionView::Create(extensions::ExtensionHost* host, |
| 165 render_view_host()->GetView()->SetBackground(pending_background_); | 192 Browser* browser) { |
| 166 pending_background_.reset(); | 193 return new ExtensionViewViews(host, browser); |
| 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 } | 194 } |
| OLD | NEW |