Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(107)

Side by Side Diff: chrome/browser/ui/views/extensions/extension_view_views.cc

Issue 10913243: extensions: Add ExtensionView interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: last mac fix Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 host_->SetExtensionView(this);
25 host_->set_view(this);
26 25
27 // This view needs to be focusable so it can act as the focused view for the 26 // 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 27 // focus manager. This is required to have SkipDefaultKeyEventProcessing
29 // called so the tab key events are forwarded to the renderer. 28 // called so the tab key events are forwarded to the renderer.
30 set_focusable(true); 29 set_focusable(true);
31 } 30 }
32 31
33 ExtensionViewViews::~ExtensionViewViews() { 32 ExtensionViewViews::~ExtensionViewViews() {
34 if (parent()) 33 if (parent())
35 parent()->RemoveChildView(this); 34 parent()->RemoveChildView(this);
36 CleanUp(); 35 CleanUp();
37 } 36 }
38 37
39 const extensions::Extension* ExtensionViewViews::extension() const { 38 void ExtensionViewViews::SetBackground(const SkBitmap& background) {
40 return host_->extension(); 39 if (GetRenderViewHost()->IsRenderViewLive() &&
41 } 40 GetRenderViewHost()->GetView()) {
42 41 GetRenderViewHost()->GetView()->SetBackground(background);
43 content::RenderViewHost* ExtensionViewViews::render_view_host() const { 42 } else {
44 return host_->render_view_host(); 43 pending_background_ = background;
45 } 44 }
46
47 void ExtensionViewViews::DidStopLoading() {
48 ShowIfCompletelyLoaded(); 45 ShowIfCompletelyLoaded();
49 } 46 }
50 47
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) { 48 void ExtensionViewViews::SetVisible(bool is_visible) {
64 if (is_visible != visible()) { 49 if (is_visible != visible()) {
65 NativeViewHost::SetVisible(is_visible); 50 NativeViewHost::SetVisible(is_visible);
66 51
67 // Also tell RenderWidgetHostView the new visibility. Despite its name, it 52 // 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 53 // is not part of the View hierarchy and does not know about the change
69 // unless we tell it. 54 // unless we tell it.
70 if (render_view_host()->GetView()) { 55 if (GetRenderViewHost()->GetView()) {
71 if (is_visible) 56 if (is_visible)
72 render_view_host()->GetView()->Show(); 57 GetRenderViewHost()->GetView()->Show();
73 else 58 else
74 render_view_host()->GetView()->Hide(); 59 GetRenderViewHost()->GetView()->Hide();
75 } 60 }
76 } 61 }
77 } 62 }
78 63
64 gfx::NativeCursor ExtensionViewViews::GetCursor(const ui::MouseEvent& event) {
65 return gfx::kNullCursor;
66 }
67
68 void ExtensionViewViews::ViewHierarchyChanged(bool is_add,
69 views::View* parent,
70 views::View* child) {
71 NativeViewHost::ViewHierarchyChanged(is_add, parent, child);
72 if (is_add && GetWidget() && !initialized_)
73 CreateWidgetHostView();
74 }
75
76 Browser* ExtensionViewViews::GetBrowser() {
77 return browser_;
78 }
79
80 const Browser* ExtensionViewViews::GetBrowser() const {
81 return browser_;
82 }
83
84 gfx::NativeView ExtensionViewViews::GetNativeView() {
85 return host_->host_contents()->GetView()->GetNativeView();
86 }
87
88 content::RenderViewHost* ExtensionViewViews::GetRenderViewHost() const {
89 return host_->render_view_host();
90 }
91
92 void ExtensionViewViews::SetContainer(ExtensionViewContainer* container) {
93 container_ = container;
94 }
95
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.
99 if (!visible()) {
100 pending_preferred_size_ = new_size;
101 return;
102 }
103
104 gfx::Size preferred_size = GetPreferredSize();
105 if (new_size != preferred_size)
106 SetPreferredSize(new_size);
107 }
108
109 void ExtensionViewViews::RenderViewCreated() {
110 if (!pending_background_.empty() && GetRenderViewHost()->GetView()) {
111 GetRenderViewHost()->GetView()->SetBackground(pending_background_);
112 pending_background_.reset();
113 }
114
115 chrome::ViewType host_type = host_->extension_host_type();
116 if (host_type == chrome::VIEW_TYPE_EXTENSION_POPUP) {
117 gfx::Size min_size(ExtensionPopup::kMinWidth,
118 ExtensionPopup::kMinHeight);
119 gfx::Size max_size(ExtensionPopup::kMaxWidth,
120 ExtensionPopup::kMaxHeight);
121 GetRenderViewHost()->EnableAutoResize(min_size, max_size);
122 }
123 }
124
125 void ExtensionViewViews::DidStopLoading() {
126 ShowIfCompletelyLoaded();
127 }
128
129 void ExtensionViewViews::WindowFrameChanged() {
130 NOTIMPLEMENTED();
131 }
132
79 void ExtensionViewViews::CreateWidgetHostView() { 133 void ExtensionViewViews::CreateWidgetHostView() {
80 DCHECK(!initialized_); 134 DCHECK(!initialized_);
81 initialized_ = true; 135 initialized_ = true;
82 Attach(host_->host_contents()->GetView()->GetNativeView()); 136 Attach(host_->host_contents()->GetView()->GetNativeView());
83 host_->CreateRenderViewSoon(); 137 host_->CreateRenderViewSoon();
84 SetVisible(false); 138 SetVisible(false);
85 } 139 }
86 140
87 void ExtensionViewViews::ShowIfCompletelyLoaded() { 141 void ExtensionViewViews::ShowIfCompletelyLoaded() {
88 if (visible() || is_clipped_) 142 if (visible())
89 return; 143 return;
90 144
91 // We wait to show the ExtensionViewViews until it has loaded, and the view 145 // We wait to show the ExtensionViewViews until it has loaded, and the view
92 // has actually been created. These can happen in different orders. 146 // has actually been created. These can happen in different orders.
93 if (host_->did_stop_loading()) { 147 if (host_->did_stop_loading()) {
94 SetVisible(true); 148 SetVisible(true);
95 ResizeDueToAutoResize(pending_preferred_size_); 149 ResizeDueToAutoResize(pending_preferred_size_);
96 } 150 }
97 } 151 }
98 152
99 void ExtensionViewViews::CleanUp() { 153 void ExtensionViewViews::CleanUp() {
100 if (!initialized_) 154 if (!initialized_)
101 return; 155 return;
102 if (native_view()) 156 if (native_view())
103 Detach(); 157 Detach();
104 initialized_ = false; 158 initialized_ = false;
105 } 159 }
106 160
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() { 161 void ExtensionViewViews::PreferredSizeChanged() {
138 View::PreferredSizeChanged(); 162 View::PreferredSizeChanged();
139 if (container_) 163 if (container_)
140 container_->OnExtensionSizeChanged(this); 164 container_->OnExtensionSizeChanged(this, gfx::Size());
141 } 165 }
142 166
143 bool ExtensionViewViews::SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) { 167 bool ExtensionViewViews::SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) {
144 // Let the tab key event be processed by the renderer (instead of moving the 168 // 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 169 // 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 170 // (on Windows at least), pressing Backspace, when focus is on a text field
147 // within the ExtensionViewViews, will navigate the page back instead of 171 // within the ExtensionViewViews, will navigate the page back instead of
148 // erasing a character. 172 // erasing a character.
149 return (e.key_code() == ui::VKEY_TAB || e.key_code() == ui::VKEY_BACK); 173 return (e.key_code() == ui::VKEY_TAB || e.key_code() == ui::VKEY_BACK);
150 } 174 }
151 175
152 void ExtensionViewViews::OnBoundsChanged(const gfx::Rect& previous_bounds) { 176 void ExtensionViewViews::OnBoundsChanged(const gfx::Rect& previous_bounds) {
153 // Propagate the new size to RenderWidgetHostView. 177 // Propagate the new size to RenderWidgetHostView.
154 // We can't send size zero because RenderWidget DCHECKs that. 178 // We can't send size zero because RenderWidget DCHECKs that.
155 if (render_view_host()->GetView() && !bounds().IsEmpty()) { 179 if (GetRenderViewHost()->GetView() && !bounds().IsEmpty())
156 render_view_host()->GetView()->SetSize(size()); 180 GetRenderViewHost()->GetView()->SetSize(size());
157
158 if (container_)
159 container_->OnViewWasResized();
160 }
161 } 181 }
162 182
163 void ExtensionViewViews::RenderViewCreated() { 183 // static
164 if (!pending_background_.empty() && render_view_host()->GetView()) { 184 ExtensionView* ExtensionView::Create(extensions::ExtensionHost* host,
165 render_view_host()->GetView()->SetBackground(pending_background_); 185 Browser* browser) {
166 pending_background_.reset(); 186 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 } 187 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698