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" |
23 | 24 |
24 using content::BrowserContext; | 25 using content::BrowserContext; |
25 using content::WebContents; | 26 using content::WebContents; |
26 | 27 |
| 28 namespace { |
| 29 |
| 30 ExtensionViewViews* GetExtensionView(extensions::ExtensionViewHost* host) { |
| 31 return static_cast<ExtensionViewViews*>(host->view()); |
| 32 } |
| 33 |
| 34 } // namespace |
| 35 |
27 ExtensionDialog::ExtensionDialog(extensions::ExtensionViewHost* host, | 36 ExtensionDialog::ExtensionDialog(extensions::ExtensionViewHost* host, |
28 ExtensionDialogObserver* observer) | 37 ExtensionDialogObserver* observer) |
29 : host_(host), | 38 : host_(host), |
30 observer_(observer) { | 39 observer_(observer) { |
31 AddRef(); // Balanced in DeleteDelegate(); | 40 AddRef(); // Balanced in DeleteDelegate(); |
32 | 41 |
33 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, | 42 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, |
34 content::Source<BrowserContext>(host->browser_context())); | 43 content::Source<BrowserContext>(host->browser_context())); |
35 // Listen for the containing view calling window.close(); | 44 // Listen for the containing view calling window.close(); |
36 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE, | 45 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE, |
(...skipping 17 matching lines...) Expand all Loading... |
54 int min_width, | 63 int min_width, |
55 int min_height, | 64 int min_height, |
56 const base::string16& title, | 65 const base::string16& title, |
57 ExtensionDialogObserver* observer) { | 66 ExtensionDialogObserver* observer) { |
58 extensions::ExtensionViewHost* host = | 67 extensions::ExtensionViewHost* host = |
59 extensions::ExtensionViewHostFactory::CreateDialogHost(url, profile); | 68 extensions::ExtensionViewHostFactory::CreateDialogHost(url, profile); |
60 if (!host) | 69 if (!host) |
61 return NULL; | 70 return NULL; |
62 // Preferred size must be set before views::Widget::CreateWindowWithParent | 71 // Preferred size must be set before views::Widget::CreateWindowWithParent |
63 // is called because CreateWindowWithParent refers the result of CanResize(). | 72 // is called because CreateWindowWithParent refers the result of CanResize(). |
64 host->view()->SetPreferredSize(gfx::Size(width, height)); | 73 ExtensionViewViews* view = GetExtensionView(host); |
65 host->view()->set_minimum_size(gfx::Size(min_width, min_height)); | 74 view->SetPreferredSize(gfx::Size(width, height)); |
| 75 view->set_minimum_size(gfx::Size(min_width, min_height)); |
66 host->SetAssociatedWebContents(web_contents); | 76 host->SetAssociatedWebContents(web_contents); |
67 | 77 |
68 DCHECK(parent_window); | 78 DCHECK(parent_window); |
69 ExtensionDialog* dialog = new ExtensionDialog(host, observer); | 79 ExtensionDialog* dialog = new ExtensionDialog(host, observer); |
70 dialog->set_title(title); | 80 dialog->set_title(title); |
71 dialog->InitWindow(parent_window, width, height); | 81 dialog->InitWindow(parent_window, width, height); |
72 | 82 |
73 // Show a white background while the extension loads. This is prettier than | 83 // Show a white background while the extension loads. This is prettier than |
74 // flashing a black unfilled window frame. | 84 // flashing a black unfilled window frame. |
75 host->view()->set_background( | 85 view->set_background( |
76 views::Background::CreateSolidBackground(0xFF, 0xFF, 0xFF)); | 86 views::Background::CreateSolidBackground(0xFF, 0xFF, 0xFF)); |
77 host->view()->SetVisible(true); | 87 view->SetVisible(true); |
78 | 88 |
79 // Ensure the DOM JavaScript can respond immediately to keyboard shortcuts. | 89 // Ensure the DOM JavaScript can respond immediately to keyboard shortcuts. |
80 host->host_contents()->Focus(); | 90 host->host_contents()->Focus(); |
81 return dialog; | 91 return dialog; |
82 } | 92 } |
83 | 93 |
84 void ExtensionDialog::InitWindow(aura::Window* parent, | 94 void ExtensionDialog::InitWindow(aura::Window* parent, |
85 int width, | 95 int width, |
86 int height) { | 96 int height) { |
87 views::Widget* window = CreateBrowserModalDialogViews(this, parent); | 97 views::Widget* window = CreateBrowserModalDialogViews(this, parent); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 ///////////////////////////////////////////////////////////////////////////// | 135 ///////////////////////////////////////////////////////////////////////////// |
126 // views::DialogDelegate overrides. | 136 // views::DialogDelegate overrides. |
127 | 137 |
128 int ExtensionDialog::GetDialogButtons() const { | 138 int ExtensionDialog::GetDialogButtons() const { |
129 // The only user, SelectFileDialogExtension, provides its own buttons. | 139 // The only user, SelectFileDialogExtension, provides its own buttons. |
130 return ui::DIALOG_BUTTON_NONE; | 140 return ui::DIALOG_BUTTON_NONE; |
131 } | 141 } |
132 | 142 |
133 bool ExtensionDialog::CanResize() const { | 143 bool ExtensionDialog::CanResize() const { |
134 // Can resize only if minimum contents size set. | 144 // Can resize only if minimum contents size set. |
135 return host_->view()->GetPreferredSize() != gfx::Size(); | 145 return GetExtensionView(host_.get())->GetPreferredSize() != gfx::Size(); |
136 } | 146 } |
137 | 147 |
138 void ExtensionDialog::SetMinimumContentsSize(int width, int height) { | 148 void ExtensionDialog::SetMinimumContentsSize(int width, int height) { |
139 host_->view()->SetPreferredSize(gfx::Size(width, height)); | 149 GetExtensionView(host_.get())->SetPreferredSize(gfx::Size(width, height)); |
140 } | 150 } |
141 | 151 |
142 ui::ModalType ExtensionDialog::GetModalType() const { | 152 ui::ModalType ExtensionDialog::GetModalType() const { |
143 return ui::MODAL_TYPE_WINDOW; | 153 return ui::MODAL_TYPE_WINDOW; |
144 } | 154 } |
145 | 155 |
146 bool ExtensionDialog::ShouldShowWindowTitle() const { | 156 bool ExtensionDialog::ShouldShowWindowTitle() const { |
147 return !window_title_.empty(); | 157 return !window_title_.empty(); |
148 } | 158 } |
149 | 159 |
150 base::string16 ExtensionDialog::GetWindowTitle() const { | 160 base::string16 ExtensionDialog::GetWindowTitle() const { |
151 return window_title_; | 161 return window_title_; |
152 } | 162 } |
153 | 163 |
154 void ExtensionDialog::WindowClosing() { | 164 void ExtensionDialog::WindowClosing() { |
155 if (observer_) | 165 if (observer_) |
156 observer_->ExtensionDialogClosing(this); | 166 observer_->ExtensionDialogClosing(this); |
157 } | 167 } |
158 | 168 |
159 void ExtensionDialog::DeleteDelegate() { | 169 void ExtensionDialog::DeleteDelegate() { |
160 // The window has finished closing. Allow ourself to be deleted. | 170 // The window has finished closing. Allow ourself to be deleted. |
161 Release(); | 171 Release(); |
162 } | 172 } |
163 | 173 |
164 views::Widget* ExtensionDialog::GetWidget() { | 174 views::Widget* ExtensionDialog::GetWidget() { |
165 return host_->view()->GetWidget(); | 175 return GetExtensionView(host_.get())->GetWidget(); |
166 } | 176 } |
167 | 177 |
168 const views::Widget* ExtensionDialog::GetWidget() const { | 178 const views::Widget* ExtensionDialog::GetWidget() const { |
169 return host_->view()->GetWidget(); | 179 return GetExtensionView(host_.get())->GetWidget(); |
170 } | 180 } |
171 | 181 |
172 views::View* ExtensionDialog::GetContentsView() { | 182 views::View* ExtensionDialog::GetContentsView() { |
173 return host_->view(); | 183 return GetExtensionView(host_.get()); |
174 } | 184 } |
175 | 185 |
176 bool ExtensionDialog::UseNewStyleForThisDialog() const { | 186 bool ExtensionDialog::UseNewStyleForThisDialog() const { |
177 return false; | 187 return false; |
178 } | 188 } |
179 | 189 |
180 ///////////////////////////////////////////////////////////////////////////// | 190 ///////////////////////////////////////////////////////////////////////////// |
181 // content::NotificationObserver overrides. | 191 // content::NotificationObserver overrides. |
182 | 192 |
183 void ExtensionDialog::Observe(int type, | 193 void ExtensionDialog::Observe(int type, |
184 const content::NotificationSource& source, | 194 const content::NotificationSource& source, |
185 const content::NotificationDetails& details) { | 195 const content::NotificationDetails& details) { |
186 switch (type) { | 196 switch (type) { |
187 case chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING: | 197 case chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING: |
188 // Avoid potential overdraw by removing the temporary background after | 198 // Avoid potential overdraw by removing the temporary background after |
189 // the extension finishes loading. | 199 // the extension finishes loading. |
190 host_->view()->set_background(NULL); | 200 GetExtensionView(host_.get())->set_background(NULL); |
191 // The render view is created during the LoadURL(), so we should | 201 // The render view is created during the LoadURL(), so we should |
192 // set the focus to the view if nobody else takes the focus. | 202 // set the focus to the view if nobody else takes the focus. |
193 if (content::Details<extensions::ExtensionHost>(host()) == details) | 203 if (content::Details<extensions::ExtensionHost>(host()) == details) |
194 MaybeFocusRenderView(); | 204 MaybeFocusRenderView(); |
195 break; | 205 break; |
196 case chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE: | 206 case chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE: |
197 // If we aren't the host of the popup, then disregard the notification. | 207 // If we aren't the host of the popup, then disregard the notification. |
198 if (content::Details<extensions::ExtensionHost>(host()) != details) | 208 if (content::Details<extensions::ExtensionHost>(host()) != details) |
199 return; | 209 return; |
200 GetWidget()->Close(); | 210 GetWidget()->Close(); |
201 break; | 211 break; |
202 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: | 212 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: |
203 if (content::Details<extensions::ExtensionHost>(host()) != details) | 213 if (content::Details<extensions::ExtensionHost>(host()) != details) |
204 return; | 214 return; |
205 if (observer_) | 215 if (observer_) |
206 observer_->ExtensionTerminated(this); | 216 observer_->ExtensionTerminated(this); |
207 break; | 217 break; |
208 default: | 218 default: |
209 NOTREACHED() << L"Received unexpected notification"; | 219 NOTREACHED() << L"Received unexpected notification"; |
210 break; | 220 break; |
211 } | 221 } |
212 } | 222 } |
OLD | NEW |