| 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/extensions/extension_host.h" | 7 #include "chrome/browser/extensions/extension_host.h" |
| 8 #include "chrome/browser/extensions/extension_process_manager.h" | 8 #include "chrome/browser/extensions/extension_process_manager.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/ui/base_window.h" | 10 #include "chrome/browser/ui/base_window.h" |
| 11 #include "chrome/browser/ui/views/extensions/extension_dialog_observer.h" | 11 #include "chrome/browser/ui/views/extensions/extension_dialog_observer.h" |
| 12 #include "chrome/browser/ui/views/extensions/extension_view_views.h" |
| 12 #include "chrome/common/chrome_notification_types.h" | 13 #include "chrome/common/chrome_notification_types.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 "googleurl/src/gurl.h" | 19 #include "googleurl/src/gurl.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" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 35 AddRef(); // Balanced in DeleteDelegate(); | 36 AddRef(); // Balanced in DeleteDelegate(); |
| 36 | 37 |
| 37 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, | 38 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, |
| 38 content::Source<Profile>(host->profile())); | 39 content::Source<Profile>(host->profile())); |
| 39 // Listen for the containing view calling window.close(); | 40 // Listen for the containing view calling window.close(); |
| 40 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE, | 41 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE, |
| 41 content::Source<Profile>(host->profile())); | 42 content::Source<Profile>(host->profile())); |
| 42 // Listen for a crash or other termination of the extension process. | 43 // Listen for a crash or other termination of the extension process. |
| 43 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED, | 44 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED, |
| 44 content::Source<Profile>(host->profile())); | 45 content::Source<Profile>(host->profile())); |
| 46 |
| 47 view_ = static_cast<ExtensionViewViews*>(host->GetExtensionView()); |
| 45 } | 48 } |
| 46 | 49 |
| 47 ExtensionDialog::~ExtensionDialog() { | 50 ExtensionDialog::~ExtensionDialog() { |
| 48 } | 51 } |
| 49 | 52 |
| 50 // static | 53 // static |
| 51 ExtensionDialog* ExtensionDialog::Show( | 54 ExtensionDialog* ExtensionDialog::Show( |
| 52 const GURL& url, | 55 const GURL& url, |
| 53 BaseWindow* base_window, | 56 BaseWindow* base_window, |
| 54 Profile* profile, | 57 Profile* profile, |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 ExtensionDialogObserver* observer) { | 97 ExtensionDialogObserver* observer) { |
| 95 CHECK(fullscreen || base_window); | 98 CHECK(fullscreen || base_window); |
| 96 ExtensionDialog* dialog = new ExtensionDialog(host, observer); | 99 ExtensionDialog* dialog = new ExtensionDialog(host, observer); |
| 97 dialog->set_title(title); | 100 dialog->set_title(title); |
| 98 | 101 |
| 99 if (fullscreen) | 102 if (fullscreen) |
| 100 dialog->InitWindowFullscreen(); | 103 dialog->InitWindowFullscreen(); |
| 101 else | 104 else |
| 102 dialog->InitWindow(base_window, width, height); | 105 dialog->InitWindow(base_window, width, height); |
| 103 | 106 |
| 104 // Show a white background while the extension loads. This is prettier than | |
| 105 // flashing a black unfilled window frame. | |
| 106 host->view()->set_background( | |
| 107 views::Background::CreateSolidBackground(0xFF, 0xFF, 0xFF)); | |
| 108 host->view()->SetVisible(true); | |
| 109 | |
| 110 // Ensure the DOM JavaScript can respond immediately to keyboard shortcuts. | 107 // Ensure the DOM JavaScript can respond immediately to keyboard shortcuts. |
| 111 host->host_contents()->Focus(); | 108 host->host_contents()->Focus(); |
| 112 return dialog; | 109 return dialog; |
| 113 } | 110 } |
| 114 | 111 |
| 115 // static | 112 // static |
| 116 extensions::ExtensionHost* ExtensionDialog::CreateExtensionHost( | 113 extensions::ExtensionHost* ExtensionDialog::CreateExtensionHost( |
| 117 const GURL& url, | 114 const GURL& url, |
| 118 Profile* profile) { | 115 Profile* profile) { |
| 119 DCHECK(profile); | 116 DCHECK(profile); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 137 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); | 134 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
| 138 params.delegate = this; | 135 params.delegate = this; |
| 139 params.parent = root_window; | 136 params.parent = root_window; |
| 140 window_->Init(params); | 137 window_->Init(params); |
| 141 window_->StackAtTop(); | 138 window_->StackAtTop(); |
| 142 window_->SetBounds(screen_rect); | 139 window_->SetBounds(screen_rect); |
| 143 window_->Show(); | 140 window_->Show(); |
| 144 | 141 |
| 145 // TODO(jamescook): Remove redundant call to Activate()? | 142 // TODO(jamescook): Remove redundant call to Activate()? |
| 146 window_->Activate(); | 143 window_->Activate(); |
| 144 |
| 145 // Show a white background while the extension loads. This is prettier than |
| 146 // flashing a black unfilled window frame. |
| 147 view_->set_background( |
| 148 views::Background::CreateSolidBackground(0xFF, 0xFF, 0xFF)); |
| 149 view_->SetVisible(true); |
| 147 } | 150 } |
| 148 #else | 151 #else |
| 149 void ExtensionDialog::InitWindowFullscreen() { | 152 void ExtensionDialog::InitWindowFullscreen() { |
| 150 NOTIMPLEMENTED(); | 153 NOTIMPLEMENTED(); |
| 151 } | 154 } |
| 152 #endif | 155 #endif |
| 153 | 156 |
| 154 | 157 |
| 155 void ExtensionDialog::InitWindow(BaseWindow* base_window, | 158 void ExtensionDialog::InitWindow(BaseWindow* base_window, |
| 156 int width, | 159 int width, |
| 157 int height) { | 160 int height) { |
| 158 gfx::NativeWindow parent = base_window->GetNativeWindow(); | 161 gfx::NativeWindow parent = base_window->GetNativeWindow(); |
| 159 window_ = views::Widget::CreateWindowWithParent(this, parent); | 162 window_ = views::Widget::CreateWindowWithParent(this, parent); |
| 160 | 163 |
| 161 // Center the window over the browser. | 164 // Center the window over the browser. |
| 162 gfx::Point center = base_window->GetBounds().CenterPoint(); | 165 gfx::Point center = base_window->GetBounds().CenterPoint(); |
| 163 int x = center.x() - width / 2; | 166 int x = center.x() - width / 2; |
| 164 int y = center.y() - height / 2; | 167 int y = center.y() - height / 2; |
| 165 // Ensure the top left and top right of the window are on screen, with | 168 // Ensure the top left and top right of the window are on screen, with |
| 166 // priority given to the top left. | 169 // priority given to the top left. |
| 167 gfx::Rect screen_rect = gfx::Screen::GetDisplayNearestPoint(center).bounds(); | 170 gfx::Rect screen_rect = gfx::Screen::GetDisplayNearestPoint(center).bounds(); |
| 168 gfx::Rect bounds_rect = gfx::Rect(x, y, width, height); | 171 gfx::Rect bounds_rect = gfx::Rect(x, y, width, height); |
| 169 bounds_rect = bounds_rect.AdjustToFit(screen_rect); | 172 bounds_rect = bounds_rect.AdjustToFit(screen_rect); |
| 170 window_->SetBounds(bounds_rect); | 173 window_->SetBounds(bounds_rect); |
| 171 | 174 |
| 172 window_->Show(); | 175 window_->Show(); |
| 173 // TODO(jamescook): Remove redundant call to Activate()? | 176 // TODO(jamescook): Remove redundant call to Activate()? |
| 174 window_->Activate(); | 177 window_->Activate(); |
| 178 |
| 179 // Show a white background while the extension loads. This is prettier than |
| 180 // flashing a black unfilled window frame. |
| 181 view_->set_background( |
| 182 views::Background::CreateSolidBackground(0xFF, 0xFF, 0xFF)); |
| 183 view_->SetVisible(true); |
| 175 } | 184 } |
| 176 | 185 |
| 177 void ExtensionDialog::ObserverDestroyed() { | 186 void ExtensionDialog::ObserverDestroyed() { |
| 178 observer_ = NULL; | 187 observer_ = NULL; |
| 179 } | 188 } |
| 180 | 189 |
| 181 void ExtensionDialog::Close() { | 190 void ExtensionDialog::Close() { |
| 182 if (!window_) | 191 if (!window_) |
| 183 return; | 192 return; |
| 184 | 193 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 204 // See bug.com/127222. | 213 // See bug.com/127222. |
| 205 focus_manager->SetFocusedView(GetContentsView()); | 214 focus_manager->SetFocusedView(GetContentsView()); |
| 206 view->Focus(); | 215 view->Focus(); |
| 207 } | 216 } |
| 208 | 217 |
| 209 ///////////////////////////////////////////////////////////////////////////// | 218 ///////////////////////////////////////////////////////////////////////////// |
| 210 // views::WidgetDelegate overrides. | 219 // views::WidgetDelegate overrides. |
| 211 | 220 |
| 212 bool ExtensionDialog::CanResize() const { | 221 bool ExtensionDialog::CanResize() const { |
| 213 // Can resize only if minimum contents size set. | 222 // Can resize only if minimum contents size set. |
| 214 return extension_host_->view()->GetPreferredSize() != gfx::Size(); | 223 return view_->GetPreferredSize() != gfx::Size(); |
| 215 } | 224 } |
| 216 | 225 |
| 217 void ExtensionDialog::SetMinimumContentsSize(int width, int height) { | 226 void ExtensionDialog::SetMinimumContentsSize(int width, int height) { |
| 218 extension_host_->view()->SetPreferredSize(gfx::Size(width, height)); | 227 view_->SetPreferredSize(gfx::Size(width, height)); |
| 219 } | 228 } |
| 220 | 229 |
| 221 ui::ModalType ExtensionDialog::GetModalType() const { | 230 ui::ModalType ExtensionDialog::GetModalType() const { |
| 222 return ui::MODAL_TYPE_WINDOW; | 231 return ui::MODAL_TYPE_WINDOW; |
| 223 } | 232 } |
| 224 | 233 |
| 225 bool ExtensionDialog::ShouldShowWindowTitle() const { | 234 bool ExtensionDialog::ShouldShowWindowTitle() const { |
| 226 return !window_title_.empty(); | 235 return !window_title_.empty(); |
| 227 } | 236 } |
| 228 | 237 |
| 229 string16 ExtensionDialog::GetWindowTitle() const { | 238 string16 ExtensionDialog::GetWindowTitle() const { |
| 230 return window_title_; | 239 return window_title_; |
| 231 } | 240 } |
| 232 | 241 |
| 233 void ExtensionDialog::WindowClosing() { | 242 void ExtensionDialog::WindowClosing() { |
| 234 if (observer_) | 243 if (observer_) |
| 235 observer_->ExtensionDialogClosing(this); | 244 observer_->ExtensionDialogClosing(this); |
| 236 } | 245 } |
| 237 | 246 |
| 238 void ExtensionDialog::DeleteDelegate() { | 247 void ExtensionDialog::DeleteDelegate() { |
| 239 // The window has finished closing. Allow ourself to be deleted. | 248 // The window has finished closing. Allow ourself to be deleted. |
| 240 Release(); | 249 Release(); |
| 241 } | 250 } |
| 242 | 251 |
| 243 views::Widget* ExtensionDialog::GetWidget() { | 252 views::Widget* ExtensionDialog::GetWidget() { |
| 244 return extension_host_->view()->GetWidget(); | 253 return view_->GetWidget(); |
| 245 } | 254 } |
| 246 | 255 |
| 247 const views::Widget* ExtensionDialog::GetWidget() const { | 256 const views::Widget* ExtensionDialog::GetWidget() const { |
| 248 return extension_host_->view()->GetWidget(); | 257 return view_->GetWidget(); |
| 249 } | 258 } |
| 250 | 259 |
| 251 views::View* ExtensionDialog::GetContentsView() { | 260 views::View* ExtensionDialog::GetContentsView() { |
| 252 return extension_host_->view(); | 261 return view_; |
| 253 } | 262 } |
| 254 | 263 |
| 255 ///////////////////////////////////////////////////////////////////////////// | 264 ///////////////////////////////////////////////////////////////////////////// |
| 256 // content::NotificationObserver overrides. | 265 // content::NotificationObserver overrides. |
| 257 | 266 |
| 258 void ExtensionDialog::Observe(int type, | 267 void ExtensionDialog::Observe(int type, |
| 259 const content::NotificationSource& source, | 268 const content::NotificationSource& source, |
| 260 const content::NotificationDetails& details) { | 269 const content::NotificationDetails& details) { |
| 261 switch (type) { | 270 switch (type) { |
| 262 case chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING: | 271 case chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING: |
| 263 // Avoid potential overdraw by removing the temporary background after | 272 // Avoid potential overdraw by removing the temporary background after |
| 264 // the extension finishes loading. | 273 // the extension finishes loading. |
| 265 extension_host_->view()->set_background(NULL); | 274 view_->set_background(NULL); |
| 266 // The render view is created during the LoadURL(), so we should | 275 // The render view is created during the LoadURL(), so we should |
| 267 // set the focus to the view if nobody else takes the focus. | 276 // set the focus to the view if nobody else takes the focus. |
| 268 if (content::Details<extensions::ExtensionHost>(host()) == details) | 277 if (content::Details<extensions::ExtensionHost>(host()) == details) |
| 269 MaybeFocusRenderView(); | 278 MaybeFocusRenderView(); |
| 270 break; | 279 break; |
| 271 case chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE: | 280 case chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE: |
| 272 // If we aren't the host of the popup, then disregard the notification. | 281 // If we aren't the host of the popup, then disregard the notification. |
| 273 if (content::Details<extensions::ExtensionHost>(host()) != details) | 282 if (content::Details<extensions::ExtensionHost>(host()) != details) |
| 274 return; | 283 return; |
| 275 Close(); | 284 Close(); |
| 276 break; | 285 break; |
| 277 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: | 286 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: |
| 278 if (content::Details<extensions::ExtensionHost>(host()) != details) | 287 if (content::Details<extensions::ExtensionHost>(host()) != details) |
| 279 return; | 288 return; |
| 280 if (observer_) | 289 if (observer_) |
| 281 observer_->ExtensionTerminated(this); | 290 observer_->ExtensionTerminated(this); |
| 282 break; | 291 break; |
| 283 default: | 292 default: |
| 284 NOTREACHED() << L"Received unexpected notification"; | 293 NOTREACHED() << L"Received unexpected notification"; |
| 285 break; | 294 break; |
| 286 } | 295 } |
| 287 } | 296 } |
| OLD | NEW |