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