| 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/exclusive_access_bubble_views.h" | 5 #include "chrome/browser/ui/views/exclusive_access_bubble_views.h" |
| 6 | 6 |
| 7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/app/chrome_command_ids.h" | 9 #include "chrome/app/chrome_command_ids.h" |
| 10 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
| 11 #include "chrome/browser/ui/exclusive_access/exclusive_access_manager.h" | 11 #include "chrome/browser/ui/exclusive_access/exclusive_access_manager.h" |
| 12 #include "chrome/browser/ui/exclusive_access/fullscreen_controller.h" | 12 #include "chrome/browser/ui/exclusive_access/fullscreen_controller.h" |
| 13 #include "chrome/browser/ui/views/frame/browser_view.h" | 13 #include "chrome/browser/ui/views/exclusive_access_bubble_views_context.h" |
| 14 #include "chrome/browser/ui/views/frame/immersive_mode_controller.h" | 14 #include "chrome/browser/ui/views/frame/immersive_mode_controller.h" |
| 15 #include "chrome/browser/ui/views/frame/top_container_view.h" | 15 #include "chrome/browser/ui/views/frame/top_container_view.h" |
| 16 #include "chrome/grit/generated_resources.h" | 16 #include "chrome/grit/generated_resources.h" |
| 17 #include "content/public/browser/notification_service.h" | 17 #include "content/public/browser/notification_service.h" |
| 18 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
| 19 #include "ui/base/resource/resource_bundle.h" | 19 #include "ui/base/resource/resource_bundle.h" |
| 20 #include "ui/events/keycodes/keyboard_codes.h" | 20 #include "ui/events/keycodes/keyboard_codes.h" |
| 21 #include "ui/gfx/animation/slide_animation.h" | 21 #include "ui/gfx/animation/slide_animation.h" |
| 22 #include "ui/gfx/canvas.h" | 22 #include "ui/gfx/canvas.h" |
| 23 #include "ui/gfx/screen.h" | 23 #include "ui/gfx/screen.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 50 | 50 |
| 51 // Returns an empty size when the view is not visible. | 51 // Returns an empty size when the view is not visible. |
| 52 gfx::Size GetPreferredSize() const override; | 52 gfx::Size GetPreferredSize() const override; |
| 53 | 53 |
| 54 views::LabelButton* accept_button() const { return accept_button_; } | 54 views::LabelButton* accept_button() const { return accept_button_; } |
| 55 views::LabelButton* deny_button() const { return deny_button_; } | 55 views::LabelButton* deny_button() const { return deny_button_; } |
| 56 | 56 |
| 57 private: | 57 private: |
| 58 views::LabelButton* accept_button_; | 58 views::LabelButton* accept_button_; |
| 59 views::LabelButton* deny_button_; | 59 views::LabelButton* deny_button_; |
| 60 | |
| 61 DISALLOW_COPY_AND_ASSIGN(ButtonView); | 60 DISALLOW_COPY_AND_ASSIGN(ButtonView); |
| 62 }; | 61 }; |
| 63 | 62 |
| 64 ButtonView::ButtonView(views::ButtonListener* listener, | 63 ButtonView::ButtonView(views::ButtonListener* listener, |
| 65 int between_button_spacing) | 64 int between_button_spacing) |
| 66 : accept_button_(NULL), deny_button_(NULL) { | 65 : accept_button_(NULL), deny_button_(NULL) { |
| 67 accept_button_ = new views::LabelButton(listener, base::string16()); | 66 accept_button_ = new views::LabelButton(listener, base::string16()); |
| 68 accept_button_->SetStyle(views::Button::STYLE_BUTTON); | 67 accept_button_->SetStyle(views::Button::STYLE_BUTTON); |
| 69 accept_button_->SetFocusable(false); | 68 accept_button_->SetFocusable(false); |
| 70 AddChildView(accept_button_); | 69 AddChildView(accept_button_); |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 #endif | 247 #endif |
| 249 link_->SetVisible(link_visible); | 248 link_->SetVisible(link_visible); |
| 250 mouse_lock_exit_instruction_->SetVisible(!link_visible); | 249 mouse_lock_exit_instruction_->SetVisible(!link_visible); |
| 251 button_view_->SetVisible(false); | 250 button_view_->SetVisible(false); |
| 252 } | 251 } |
| 253 } | 252 } |
| 254 | 253 |
| 255 // ExclusiveAccessBubbleViews -------------------------------------------------- | 254 // ExclusiveAccessBubbleViews -------------------------------------------------- |
| 256 | 255 |
| 257 ExclusiveAccessBubbleViews::ExclusiveAccessBubbleViews( | 256 ExclusiveAccessBubbleViews::ExclusiveAccessBubbleViews( |
| 258 BrowserView* browser_view, | 257 scoped_ptr<ExclusiveAccessBubbleViewsContext> context, |
| 259 const GURL& url, | 258 const GURL& url, |
| 260 ExclusiveAccessBubbleType bubble_type) | 259 ExclusiveAccessBubbleType bubble_type) |
| 261 : ExclusiveAccessBubble(browser_view->browser(), url, bubble_type), | 260 : ExclusiveAccessBubble(context->GetExclusiveAccessManager(), |
| 262 browser_view_(browser_view), | 261 url, |
| 262 bubble_type), |
| 263 bubble_view_context_(context.Pass()), |
| 263 popup_(NULL), | 264 popup_(NULL), |
| 264 animation_(new gfx::SlideAnimation(this)), | 265 animation_(new gfx::SlideAnimation(this)), |
| 265 animated_attribute_(ANIMATED_ATTRIBUTE_BOUNDS) { | 266 animated_attribute_(ANIMATED_ATTRIBUTE_BOUNDS) { |
| 266 animation_->Reset(1); | 267 animation_->Reset(1); |
| 267 | 268 |
| 268 // Create the contents view. | 269 // Create the contents view. |
| 269 ui::Accelerator accelerator(ui::VKEY_UNKNOWN, ui::EF_NONE); | 270 ui::Accelerator accelerator(ui::VKEY_UNKNOWN, ui::EF_NONE); |
| 270 bool got_accelerator = | 271 bool got_accelerator = bubble_view_context_->GetWidget()->GetAccelerator( |
| 271 browser_view_->GetWidget()->GetAccelerator(IDC_FULLSCREEN, &accelerator); | 272 IDC_FULLSCREEN, &accelerator); |
| 272 DCHECK(got_accelerator); | 273 DCHECK(got_accelerator); |
| 273 view_ = new ExclusiveAccessView(this, accelerator.GetShortcutText(), url, | 274 view_ = new ExclusiveAccessView(this, accelerator.GetShortcutText(), url, |
| 274 bubble_type_); | 275 bubble_type_); |
| 275 | 276 |
| 276 // TODO(yzshen): Change to use the new views bubble, BubbleDelegateView. | 277 // TODO(yzshen): Change to use the new views bubble, BubbleDelegateView. |
| 277 // TODO(pkotwicz): When this becomes a views bubble, make sure that this | 278 // TODO(pkotwicz): When this becomes a views bubble, make sure that this |
| 278 // bubble is ignored by ImmersiveModeControllerAsh::BubbleManager. | 279 // bubble is ignored by ImmersiveModeControllerAsh::BubbleManager. |
| 279 // Initialize the popup. | 280 // Initialize the popup. |
| 280 popup_ = new views::Widget; | 281 popup_ = new views::Widget; |
| 281 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP); | 282 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP); |
| 282 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; | 283 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; |
| 283 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 284 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 284 params.parent = browser_view_->GetWidget()->GetNativeView(); | 285 params.parent = bubble_view_context_->GetWidget()->GetNativeView(); |
| 285 params.bounds = GetPopupRect(false); | 286 params.bounds = GetPopupRect(false); |
| 286 popup_->Init(params); | 287 popup_->Init(params); |
| 287 gfx::Size size = GetPopupRect(true).size(); | 288 gfx::Size size = GetPopupRect(true).size(); |
| 288 popup_->SetContentsView(view_); | 289 popup_->SetContentsView(view_); |
| 289 // We set layout manager to NULL to prevent the widget from sizing its | 290 // We set layout manager to NULL to prevent the widget from sizing its |
| 290 // contents to the same size as itself. This prevents the widget contents from | 291 // contents to the same size as itself. This prevents the widget contents from |
| 291 // shrinking while we animate the height of the popup to give the impression | 292 // shrinking while we animate the height of the popup to give the impression |
| 292 // that it is sliding off the top of the screen. | 293 // that it is sliding off the top of the screen. |
| 293 popup_->GetRootView()->SetLayoutManager(NULL); | 294 popup_->GetRootView()->SetLayoutManager(NULL); |
| 294 view_->SetBounds(0, 0, size.width(), size.height()); | 295 view_->SetBounds(0, 0, size.width(), size.height()); |
| 295 popup_->ShowInactive(); // This does not activate the popup. | 296 popup_->ShowInactive(); // This does not activate the popup. |
| 296 | 297 |
| 297 popup_->AddObserver(this); | 298 popup_->AddObserver(this); |
| 298 | 299 |
| 299 registrar_.Add( | 300 registrar_.Add(this, chrome::NOTIFICATION_FULLSCREEN_CHANGED, |
| 300 this, chrome::NOTIFICATION_FULLSCREEN_CHANGED, | 301 content::Source<FullscreenController>( |
| 301 content::Source<FullscreenController>(browser_view_->browser() | 302 bubble_view_context_->GetExclusiveAccessManager() |
| 302 ->exclusive_access_manager() | 303 ->fullscreen_controller())); |
| 303 ->fullscreen_controller())); | |
| 304 | 304 |
| 305 UpdateForImmersiveState(); | 305 UpdateForImmersiveState(); |
| 306 } | 306 } |
| 307 | 307 |
| 308 ExclusiveAccessBubbleViews::~ExclusiveAccessBubbleViews() { | 308 ExclusiveAccessBubbleViews::~ExclusiveAccessBubbleViews() { |
| 309 popup_->RemoveObserver(this); | 309 popup_->RemoveObserver(this); |
| 310 | 310 |
| 311 // This is tricky. We may be in an ATL message handler stack, in which case | 311 // This is tricky. We may be in an ATL message handler stack, in which case |
| 312 // the popup cannot be deleted yet. We also can't set the popup's ownership | 312 // the popup cannot be deleted yet. We also can't set the popup's ownership |
| 313 // model to NATIVE_WIDGET_OWNS_WIDGET because if the user closed the last tab | 313 // model to NATIVE_WIDGET_OWNS_WIDGET because if the user closed the last tab |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 return; | 362 return; |
| 363 | 363 |
| 364 if (should_watch_mouse) | 364 if (should_watch_mouse) |
| 365 StartWatchingMouse(); | 365 StartWatchingMouse(); |
| 366 else | 366 else |
| 367 StopWatchingMouse(); | 367 StopWatchingMouse(); |
| 368 } | 368 } |
| 369 | 369 |
| 370 void ExclusiveAccessBubbleViews::UpdateForImmersiveState() { | 370 void ExclusiveAccessBubbleViews::UpdateForImmersiveState() { |
| 371 AnimatedAttribute expected_animated_attribute = | 371 AnimatedAttribute expected_animated_attribute = |
| 372 browser_view_->immersive_mode_controller()->IsEnabled() | 372 bubble_view_context_->IsImmersiveModeEnabled() |
| 373 ? ANIMATED_ATTRIBUTE_OPACITY | 373 ? ANIMATED_ATTRIBUTE_OPACITY |
| 374 : ANIMATED_ATTRIBUTE_BOUNDS; | 374 : ANIMATED_ATTRIBUTE_BOUNDS; |
| 375 if (animated_attribute_ != expected_animated_attribute) { | 375 if (animated_attribute_ != expected_animated_attribute) { |
| 376 // If an animation is currently in progress, skip to the end because | 376 // If an animation is currently in progress, skip to the end because |
| 377 // switching the animated attribute midway through the animation looks | 377 // switching the animated attribute midway through the animation looks |
| 378 // weird. | 378 // weird. |
| 379 animation_->End(); | 379 animation_->End(); |
| 380 | 380 |
| 381 animated_attribute_ = expected_animated_attribute; | 381 animated_attribute_ = expected_animated_attribute; |
| 382 | 382 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 394 | 394 |
| 395 void ExclusiveAccessBubbleViews::UpdateBounds() { | 395 void ExclusiveAccessBubbleViews::UpdateBounds() { |
| 396 gfx::Rect popup_rect(GetPopupRect(false)); | 396 gfx::Rect popup_rect(GetPopupRect(false)); |
| 397 if (!popup_rect.IsEmpty()) { | 397 if (!popup_rect.IsEmpty()) { |
| 398 popup_->SetBounds(popup_rect); | 398 popup_->SetBounds(popup_rect); |
| 399 view_->SetY(popup_rect.height() - view_->height()); | 399 view_->SetY(popup_rect.height() - view_->height()); |
| 400 } | 400 } |
| 401 } | 401 } |
| 402 | 402 |
| 403 views::View* ExclusiveAccessBubbleViews::GetBrowserRootView() const { | 403 views::View* ExclusiveAccessBubbleViews::GetBrowserRootView() const { |
| 404 return browser_view_->GetWidget()->GetRootView(); | 404 return bubble_view_context_->GetWidget()->GetRootView(); |
| 405 } | 405 } |
| 406 | 406 |
| 407 void ExclusiveAccessBubbleViews::AnimationProgressed( | 407 void ExclusiveAccessBubbleViews::AnimationProgressed( |
| 408 const gfx::Animation* animation) { | 408 const gfx::Animation* animation) { |
| 409 if (animated_attribute_ == ANIMATED_ATTRIBUTE_OPACITY) { | 409 if (animated_attribute_ == ANIMATED_ATTRIBUTE_OPACITY) { |
| 410 int opacity = animation_->CurrentValueBetween(0, 255); | 410 int opacity = animation_->CurrentValueBetween(0, 255); |
| 411 if (opacity == 0) { | 411 if (opacity == 0) { |
| 412 popup_->Hide(); | 412 popup_->Hide(); |
| 413 } else { | 413 } else { |
| 414 popup_->Show(); | 414 popup_->Show(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 427 void ExclusiveAccessBubbleViews::AnimationEnded( | 427 void ExclusiveAccessBubbleViews::AnimationEnded( |
| 428 const gfx::Animation* animation) { | 428 const gfx::Animation* animation) { |
| 429 AnimationProgressed(animation); | 429 AnimationProgressed(animation); |
| 430 } | 430 } |
| 431 | 431 |
| 432 gfx::Rect ExclusiveAccessBubbleViews::GetPopupRect( | 432 gfx::Rect ExclusiveAccessBubbleViews::GetPopupRect( |
| 433 bool ignore_animation_state) const { | 433 bool ignore_animation_state) const { |
| 434 gfx::Size size(view_->GetPreferredSize()); | 434 gfx::Size size(view_->GetPreferredSize()); |
| 435 // NOTE: don't use the bounds of the root_view_. On linux GTK changing window | 435 // NOTE: don't use the bounds of the root_view_. On linux GTK changing window |
| 436 // size is async. Instead we use the size of the screen. | 436 // size is async. Instead we use the size of the screen. |
| 437 gfx::Screen* screen = | 437 gfx::Screen* screen = gfx::Screen::GetScreenFor( |
| 438 gfx::Screen::GetScreenFor(browser_view_->GetWidget()->GetNativeView()); | 438 bubble_view_context_->GetWidget()->GetNativeView()); |
| 439 gfx::Rect screen_bounds = | 439 gfx::Rect screen_bounds = |
| 440 screen->GetDisplayNearestWindow( | 440 screen->GetDisplayNearestWindow( |
| 441 browser_view_->GetWidget()->GetNativeView()).bounds(); | 441 bubble_view_context_->GetWidget()->GetNativeView()).bounds(); |
| 442 int x = screen_bounds.x() + (screen_bounds.width() - size.width()) / 2; | 442 int x = screen_bounds.x() + (screen_bounds.width() - size.width()) / 2; |
| 443 | 443 |
| 444 int top_container_bottom = screen_bounds.y(); | 444 int top_container_bottom = screen_bounds.y(); |
| 445 if (browser_view_->immersive_mode_controller()->IsEnabled()) { | 445 if (bubble_view_context_->IsImmersiveModeEnabled()) { |
| 446 // Skip querying the top container height in non-immersive fullscreen | 446 // Skip querying the top container height in non-immersive fullscreen |
| 447 // because: | 447 // because: |
| 448 // - The top container height is always zero in non-immersive fullscreen. | 448 // - The top container height is always zero in non-immersive fullscreen. |
| 449 // - Querying the top container height may return the height before entering | 449 // - Querying the top container height may return the height before entering |
| 450 // fullscreen because layout is disabled while entering fullscreen. | 450 // fullscreen because layout is disabled while entering fullscreen. |
| 451 // A visual glitch due to the delayed layout is avoided in immersive | 451 // A visual glitch due to the delayed layout is avoided in immersive |
| 452 // fullscreen because entering fullscreen starts with the top container | 452 // fullscreen because entering fullscreen starts with the top container |
| 453 // revealed. When revealed, the top container has the same height as before | 453 // revealed. When revealed, the top container has the same height as before |
| 454 // entering fullscreen. | 454 // entering fullscreen. |
| 455 top_container_bottom = | 455 top_container_bottom = |
| 456 browser_view_->top_container()->GetBoundsInScreen().bottom(); | 456 bubble_view_context_->GetTopContainerBoundsInScreen().bottom(); |
| 457 } | 457 } |
| 458 int y = top_container_bottom + kPopupTopPx; | 458 int y = top_container_bottom + kPopupTopPx; |
| 459 | 459 |
| 460 if (!ignore_animation_state && | 460 if (!ignore_animation_state && |
| 461 animated_attribute_ == ANIMATED_ATTRIBUTE_BOUNDS) { | 461 animated_attribute_ == ANIMATED_ATTRIBUTE_BOUNDS) { |
| 462 int total_height = size.height() + kPopupTopPx; | 462 int total_height = size.height() + kPopupTopPx; |
| 463 int popup_bottom = animation_->CurrentValueBetween(total_height, 0); | 463 int popup_bottom = animation_->CurrentValueBetween(total_height, 0); |
| 464 int y_offset = std::min(popup_bottom, kPopupTopPx); | 464 int y_offset = std::min(popup_bottom, kPopupTopPx); |
| 465 size.set_height(size.height() - popup_bottom + y_offset); | 465 size.set_height(size.height() - popup_bottom + y_offset); |
| 466 y -= y_offset; | 466 y -= y_offset; |
| 467 } | 467 } |
| 468 return gfx::Rect(gfx::Point(x, y), size); | 468 return gfx::Rect(gfx::Point(x, y), size); |
| 469 } | 469 } |
| 470 | 470 |
| 471 gfx::Point ExclusiveAccessBubbleViews::GetCursorScreenPoint() { | 471 gfx::Point ExclusiveAccessBubbleViews::GetCursorScreenPoint() { |
| 472 gfx::Point cursor_pos = | 472 gfx::Point cursor_pos = |
| 473 gfx::Screen::GetScreenFor(browser_view_->GetWidget()->GetNativeView()) | 473 gfx::Screen::GetScreenFor( |
| 474 bubble_view_context_->GetWidget()->GetNativeView()) |
| 474 ->GetCursorScreenPoint(); | 475 ->GetCursorScreenPoint(); |
| 475 views::View::ConvertPointFromScreen(GetBrowserRootView(), &cursor_pos); | 476 views::View::ConvertPointFromScreen(GetBrowserRootView(), &cursor_pos); |
| 476 return cursor_pos; | 477 return cursor_pos; |
| 477 } | 478 } |
| 478 | 479 |
| 479 bool ExclusiveAccessBubbleViews::WindowContainsPoint(gfx::Point pos) { | 480 bool ExclusiveAccessBubbleViews::WindowContainsPoint(gfx::Point pos) { |
| 480 return GetBrowserRootView()->HitTestPoint(pos); | 481 return GetBrowserRootView()->HitTestPoint(pos); |
| 481 } | 482 } |
| 482 | 483 |
| 483 bool ExclusiveAccessBubbleViews::IsWindowActive() { | 484 bool ExclusiveAccessBubbleViews::IsWindowActive() { |
| 484 return browser_view_->GetWidget()->IsActive(); | 485 return bubble_view_context_->GetWidget()->IsActive(); |
| 485 } | 486 } |
| 486 | 487 |
| 487 void ExclusiveAccessBubbleViews::Hide() { | 488 void ExclusiveAccessBubbleViews::Hide() { |
| 488 animation_->SetSlideDuration(kSlideOutDurationMs); | 489 animation_->SetSlideDuration(kSlideOutDurationMs); |
| 489 animation_->Hide(); | 490 animation_->Hide(); |
| 490 } | 491 } |
| 491 | 492 |
| 492 void ExclusiveAccessBubbleViews::Show() { | 493 void ExclusiveAccessBubbleViews::Show() { |
| 493 animation_->SetSlideDuration(kSlideInDurationMs); | 494 animation_->SetSlideDuration(kSlideInDurationMs); |
| 494 animation_->Show(); | 495 animation_->Show(); |
| 495 } | 496 } |
| 496 | 497 |
| 497 bool ExclusiveAccessBubbleViews::IsAnimating() { | 498 bool ExclusiveAccessBubbleViews::IsAnimating() { |
| 498 return animation_->is_animating(); | 499 return animation_->is_animating(); |
| 499 } | 500 } |
| 500 | 501 |
| 501 bool ExclusiveAccessBubbleViews::CanMouseTriggerSlideIn() const { | 502 bool ExclusiveAccessBubbleViews::CanMouseTriggerSlideIn() const { |
| 502 return !browser_view_->immersive_mode_controller()->IsEnabled(); | 503 return !bubble_view_context_->IsImmersiveModeEnabled(); |
| 503 } | 504 } |
| 504 | 505 |
| 505 void ExclusiveAccessBubbleViews::Observe( | 506 void ExclusiveAccessBubbleViews::Observe( |
| 506 int type, | 507 int type, |
| 507 const content::NotificationSource& source, | 508 const content::NotificationSource& source, |
| 508 const content::NotificationDetails& details) { | 509 const content::NotificationDetails& details) { |
| 509 DCHECK_EQ(chrome::NOTIFICATION_FULLSCREEN_CHANGED, type); | 510 DCHECK_EQ(chrome::NOTIFICATION_FULLSCREEN_CHANGED, type); |
| 510 UpdateForImmersiveState(); | 511 UpdateForImmersiveState(); |
| 511 } | 512 } |
| 512 | 513 |
| 513 void ExclusiveAccessBubbleViews::OnWidgetVisibilityChanged( | 514 void ExclusiveAccessBubbleViews::OnWidgetVisibilityChanged( |
| 514 views::Widget* widget, | 515 views::Widget* widget, |
| 515 bool visible) { | 516 bool visible) { |
| 516 UpdateMouseWatcher(); | 517 UpdateMouseWatcher(); |
| 517 } | 518 } |
| OLD | NEW |