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_dialog.h" | 5 #include "chrome/browser/ui/views/extensions/extension_dialog.h" |
6 | 6 |
7 #include "chrome/browser/chrome_notification_types.h" | 7 #include "chrome/browser/chrome_notification_types.h" |
8 #include "chrome/browser/extensions/extension_view_host.h" | 8 #include "chrome/browser/extensions/extension_view_host.h" |
9 #include "chrome/browser/extensions/extension_view_host_factory.h" | 9 #include "chrome/browser/extensions/extension_view_host_factory.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/browser/ui/views/constrained_window_views.h" | 11 #include "chrome/browser/ui/views/constrained_window_views.h" |
12 #include "chrome/browser/ui/views/extensions/extension_dialog_observer.h" | 12 #include "chrome/browser/ui/views/extensions/extension_dialog_observer.h" |
| 13 #include "chrome/browser/ui/views/extensions/extension_view_views.h" |
13 #include "content/public/browser/notification_details.h" | 14 #include "content/public/browser/notification_details.h" |
14 #include "content/public/browser/notification_source.h" | 15 #include "content/public/browser/notification_source.h" |
15 #include "content/public/browser/render_view_host.h" | 16 #include "content/public/browser/render_view_host.h" |
16 #include "content/public/browser/render_widget_host_view.h" | 17 #include "content/public/browser/render_widget_host_view.h" |
17 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
18 #include "ui/base/base_window.h" | 19 #include "ui/base/base_window.h" |
19 #include "ui/gfx/screen.h" | 20 #include "ui/gfx/screen.h" |
20 #include "ui/views/background.h" | 21 #include "ui/views/background.h" |
21 #include "ui/views/widget/widget.h" | 22 #include "ui/views/widget/widget.h" |
22 #include "url/gurl.h" | 23 #include "url/gurl.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 int min_width, | 55 int min_width, |
55 int min_height, | 56 int min_height, |
56 const base::string16& title, | 57 const base::string16& title, |
57 ExtensionDialogObserver* observer) { | 58 ExtensionDialogObserver* observer) { |
58 extensions::ExtensionViewHost* host = | 59 extensions::ExtensionViewHost* host = |
59 extensions::ExtensionViewHostFactory::CreateDialogHost(url, profile); | 60 extensions::ExtensionViewHostFactory::CreateDialogHost(url, profile); |
60 if (!host) | 61 if (!host) |
61 return NULL; | 62 return NULL; |
62 // Preferred size must be set before views::Widget::CreateWindowWithParent | 63 // Preferred size must be set before views::Widget::CreateWindowWithParent |
63 // is called because CreateWindowWithParent refers the result of CanResize(). | 64 // is called because CreateWindowWithParent refers the result of CanResize(). |
64 host->view()->SetPreferredSize(gfx::Size(width, height)); | 65 ExtensionViewViews* view = static_cast<ExtensionViewViews*>(host->view()); |
65 host->view()->set_minimum_size(gfx::Size(min_width, min_height)); | 66 view->SetPreferredSize(gfx::Size(width, height)); |
| 67 view->set_minimum_size(gfx::Size(min_width, min_height)); |
66 host->SetAssociatedWebContents(web_contents); | 68 host->SetAssociatedWebContents(web_contents); |
67 | 69 |
68 DCHECK(parent_window); | 70 DCHECK(parent_window); |
69 ExtensionDialog* dialog = new ExtensionDialog(host, observer); | 71 ExtensionDialog* dialog = new ExtensionDialog(host, observer); |
70 dialog->set_title(title); | 72 dialog->set_title(title); |
71 dialog->InitWindow(parent_window, width, height); | 73 dialog->InitWindow(parent_window, width, height); |
72 | 74 |
73 // Show a white background while the extension loads. This is prettier than | 75 // Show a white background while the extension loads. This is prettier than |
74 // flashing a black unfilled window frame. | 76 // flashing a black unfilled window frame. |
75 host->view()->set_background( | 77 view->set_background( |
76 views::Background::CreateSolidBackground(0xFF, 0xFF, 0xFF)); | 78 views::Background::CreateSolidBackground(0xFF, 0xFF, 0xFF)); |
77 host->view()->SetVisible(true); | 79 view->SetVisible(true); |
78 | 80 |
79 // Ensure the DOM JavaScript can respond immediately to keyboard shortcuts. | 81 // Ensure the DOM JavaScript can respond immediately to keyboard shortcuts. |
80 host->host_contents()->Focus(); | 82 host->host_contents()->Focus(); |
81 return dialog; | 83 return dialog; |
82 } | 84 } |
83 | 85 |
84 void ExtensionDialog::InitWindow(aura::Window* parent, | 86 void ExtensionDialog::InitWindow(aura::Window* parent, |
85 int width, | 87 int width, |
86 int height) { | 88 int height) { |
87 views::Widget* window = CreateBrowserModalDialogViews(this, parent); | 89 views::Widget* window = CreateBrowserModalDialogViews(this, parent); |
88 | 90 |
89 // Center the window over the browser. | 91 // Center the window over the browser. |
90 gfx::Point center = parent->GetBoundsInScreen().CenterPoint(); | 92 gfx::Point center = parent->GetBoundsInScreen().CenterPoint(); |
91 int x = center.x() - width / 2; | 93 int x = center.x() - width / 2; |
92 int y = center.y() - height / 2; | 94 int y = center.y() - height / 2; |
93 // Ensure the top left and top right of the window are on screen, with | 95 // Ensure the top left and top right of the window are on screen, with |
94 // priority given to the top left. | 96 // priority given to the top left. |
95 gfx::Rect screen_rect = gfx::Screen::GetScreenFor(parent)-> | 97 gfx::Rect screen_rect = gfx::Screen::GetScreenFor(parent)-> |
96 GetDisplayNearestPoint(center).bounds(); | 98 GetDisplayNearestPoint(center).bounds(); |
97 gfx::Rect bounds_rect = gfx::Rect(x, y, width, height); | 99 gfx::Rect bounds_rect = gfx::Rect(x, y, width, height); |
98 bounds_rect.AdjustToFit(screen_rect); | 100 bounds_rect.AdjustToFit(screen_rect); |
99 window->SetBounds(bounds_rect); | 101 window->SetBounds(bounds_rect); |
100 | 102 |
101 window->Show(); | 103 window->Show(); |
102 // TODO(jamescook): Remove redundant call to Activate()? | 104 // TODO(jamescook): Remove redundant call to Activate()? |
103 window->Activate(); | 105 window->Activate(); |
104 } | 106 } |
105 | 107 |
| 108 ExtensionViewViews* ExtensionDialog::GetExtensionView() const { |
| 109 return static_cast<ExtensionViewViews*>(host_->view()); |
| 110 } |
| 111 |
106 void ExtensionDialog::ObserverDestroyed() { | 112 void ExtensionDialog::ObserverDestroyed() { |
107 observer_ = NULL; | 113 observer_ = NULL; |
108 } | 114 } |
109 | 115 |
110 void ExtensionDialog::MaybeFocusRenderView() { | 116 void ExtensionDialog::MaybeFocusRenderView() { |
111 views::FocusManager* focus_manager = GetWidget()->GetFocusManager(); | 117 views::FocusManager* focus_manager = GetWidget()->GetFocusManager(); |
112 DCHECK(focus_manager != NULL); | 118 DCHECK(focus_manager != NULL); |
113 | 119 |
114 // Already there's a focused view, so no need to switch the focus. | 120 // Already there's a focused view, so no need to switch the focus. |
115 if (focus_manager->GetFocusedView()) | 121 if (focus_manager->GetFocusedView()) |
116 return; | 122 return; |
117 | 123 |
118 content::RenderWidgetHostView* view = host()->render_view_host()->GetView(); | 124 content::RenderWidgetHostView* view = host()->render_view_host()->GetView(); |
119 if (!view) | 125 if (!view) |
120 return; | 126 return; |
121 | 127 |
122 view->Focus(); | 128 view->Focus(); |
123 } | 129 } |
124 | 130 |
125 ///////////////////////////////////////////////////////////////////////////// | 131 ///////////////////////////////////////////////////////////////////////////// |
126 // views::DialogDelegate overrides. | 132 // views::DialogDelegate overrides. |
127 | 133 |
128 int ExtensionDialog::GetDialogButtons() const { | 134 int ExtensionDialog::GetDialogButtons() const { |
129 // The only user, SelectFileDialogExtension, provides its own buttons. | 135 // The only user, SelectFileDialogExtension, provides its own buttons. |
130 return ui::DIALOG_BUTTON_NONE; | 136 return ui::DIALOG_BUTTON_NONE; |
131 } | 137 } |
132 | 138 |
133 bool ExtensionDialog::CanResize() const { | 139 bool ExtensionDialog::CanResize() const { |
134 // Can resize only if minimum contents size set. | 140 // Can resize only if minimum contents size set. |
135 return host_->view()->GetPreferredSize() != gfx::Size(); | 141 return GetExtensionView()->GetPreferredSize() != gfx::Size(); |
136 } | 142 } |
137 | 143 |
138 void ExtensionDialog::SetMinimumContentsSize(int width, int height) { | 144 void ExtensionDialog::SetMinimumContentsSize(int width, int height) { |
139 host_->view()->SetPreferredSize(gfx::Size(width, height)); | 145 GetExtensionView()->SetPreferredSize(gfx::Size(width, height)); |
140 } | 146 } |
141 | 147 |
142 ui::ModalType ExtensionDialog::GetModalType() const { | 148 ui::ModalType ExtensionDialog::GetModalType() const { |
143 return ui::MODAL_TYPE_WINDOW; | 149 return ui::MODAL_TYPE_WINDOW; |
144 } | 150 } |
145 | 151 |
146 bool ExtensionDialog::ShouldShowWindowTitle() const { | 152 bool ExtensionDialog::ShouldShowWindowTitle() const { |
147 return !window_title_.empty(); | 153 return !window_title_.empty(); |
148 } | 154 } |
149 | 155 |
150 base::string16 ExtensionDialog::GetWindowTitle() const { | 156 base::string16 ExtensionDialog::GetWindowTitle() const { |
151 return window_title_; | 157 return window_title_; |
152 } | 158 } |
153 | 159 |
154 void ExtensionDialog::WindowClosing() { | 160 void ExtensionDialog::WindowClosing() { |
155 if (observer_) | 161 if (observer_) |
156 observer_->ExtensionDialogClosing(this); | 162 observer_->ExtensionDialogClosing(this); |
157 } | 163 } |
158 | 164 |
159 void ExtensionDialog::DeleteDelegate() { | 165 void ExtensionDialog::DeleteDelegate() { |
160 // The window has finished closing. Allow ourself to be deleted. | 166 // The window has finished closing. Allow ourself to be deleted. |
161 Release(); | 167 Release(); |
162 } | 168 } |
163 | 169 |
164 views::Widget* ExtensionDialog::GetWidget() { | 170 views::Widget* ExtensionDialog::GetWidget() { |
165 return host_->view()->GetWidget(); | 171 return GetExtensionView()->GetWidget(); |
166 } | 172 } |
167 | 173 |
168 const views::Widget* ExtensionDialog::GetWidget() const { | 174 const views::Widget* ExtensionDialog::GetWidget() const { |
169 return host_->view()->GetWidget(); | 175 return GetExtensionView()->GetWidget(); |
170 } | 176 } |
171 | 177 |
172 views::View* ExtensionDialog::GetContentsView() { | 178 views::View* ExtensionDialog::GetContentsView() { |
173 return host_->view(); | 179 return GetExtensionView(); |
174 } | 180 } |
175 | 181 |
176 bool ExtensionDialog::UseNewStyleForThisDialog() const { | 182 bool ExtensionDialog::UseNewStyleForThisDialog() const { |
177 return false; | 183 return false; |
178 } | 184 } |
179 | 185 |
180 ///////////////////////////////////////////////////////////////////////////// | 186 ///////////////////////////////////////////////////////////////////////////// |
181 // content::NotificationObserver overrides. | 187 // content::NotificationObserver overrides. |
182 | 188 |
183 void ExtensionDialog::Observe(int type, | 189 void ExtensionDialog::Observe(int type, |
184 const content::NotificationSource& source, | 190 const content::NotificationSource& source, |
185 const content::NotificationDetails& details) { | 191 const content::NotificationDetails& details) { |
186 switch (type) { | 192 switch (type) { |
187 case chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING: | 193 case chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING: |
188 // Avoid potential overdraw by removing the temporary background after | 194 // Avoid potential overdraw by removing the temporary background after |
189 // the extension finishes loading. | 195 // the extension finishes loading. |
190 host_->view()->set_background(NULL); | 196 GetExtensionView()->set_background(NULL); |
191 // The render view is created during the LoadURL(), so we should | 197 // The render view is created during the LoadURL(), so we should |
192 // set the focus to the view if nobody else takes the focus. | 198 // set the focus to the view if nobody else takes the focus. |
193 if (content::Details<extensions::ExtensionHost>(host()) == details) | 199 if (content::Details<extensions::ExtensionHost>(host()) == details) |
194 MaybeFocusRenderView(); | 200 MaybeFocusRenderView(); |
195 break; | 201 break; |
196 case chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE: | 202 case chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE: |
197 // If we aren't the host of the popup, then disregard the notification. | 203 // If we aren't the host of the popup, then disregard the notification. |
198 if (content::Details<extensions::ExtensionHost>(host()) != details) | 204 if (content::Details<extensions::ExtensionHost>(host()) != details) |
199 return; | 205 return; |
200 GetWidget()->Close(); | 206 GetWidget()->Close(); |
201 break; | 207 break; |
202 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: | 208 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: |
203 if (content::Details<extensions::ExtensionHost>(host()) != details) | 209 if (content::Details<extensions::ExtensionHost>(host()) != details) |
204 return; | 210 return; |
205 if (observer_) | 211 if (observer_) |
206 observer_->ExtensionTerminated(this); | 212 observer_->ExtensionTerminated(this); |
207 break; | 213 break; |
208 default: | 214 default: |
209 NOTREACHED() << L"Received unexpected notification"; | 215 NOTREACHED() << L"Received unexpected notification"; |
210 break; | 216 break; |
211 } | 217 } |
212 } | 218 } |
OLD | NEW |