in_session_auth_dialog.cc 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/in_session_auth/in_session_auth_dialog.h"
  5. #include "base/command_line.h"
  6. #include "ui/aura/window.h"
  7. #include "ui/display/display.h"
  8. #include "ui/display/screen.h"
  9. #include "ui/views/widget/widget.h"
  10. #include "ui/views/widget/widget_delegate.h"
  11. namespace ash {
  12. namespace {
  13. // The top inset value is set such that the dialog overlaps with the browser
  14. // address bar, for anti-spoofing.
  15. constexpr int kTopInsetDp = 36;
  16. std::unique_ptr<views::Widget> CreateAuthDialogWidget(
  17. std::unique_ptr<views::View> contents_view,
  18. aura::Window* parent) {
  19. views::Widget::InitParams params(
  20. views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
  21. params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
  22. params.opacity = views::Widget::InitParams::WindowOpacity::kTranslucent;
  23. params.delegate = new views::WidgetDelegate();
  24. params.show_state = ui::SHOW_STATE_NORMAL;
  25. params.parent = parent;
  26. params.name = "AuthDialogWidget";
  27. params.delegate->SetInitiallyFocusedView(contents_view.get());
  28. params.delegate->SetModalType(ui::MODAL_TYPE_NONE);
  29. params.delegate->SetOwnedByWidget(true);
  30. std::unique_ptr<views::Widget> widget = std::make_unique<views::Widget>();
  31. widget->Init(std::move(params));
  32. widget->SetVisibilityAnimationTransition(views::Widget::ANIMATE_NONE);
  33. widget->SetContentsView(std::move(contents_view));
  34. return widget;
  35. }
  36. } // namespace
  37. InSessionAuthDialog::InSessionAuthDialog(
  38. uint32_t auth_methods,
  39. aura::Window* parent_window,
  40. const std::string& origin_name,
  41. const AuthDialogContentsView::AuthMethodsMetadata& auth_metadata,
  42. const UserAvatar& avatar)
  43. : auth_methods_(auth_methods) {
  44. widget_ = CreateAuthDialogWidget(
  45. std::make_unique<AuthDialogContentsView>(auth_methods, origin_name,
  46. auth_metadata, avatar),
  47. parent_window);
  48. gfx::Rect bounds = parent_window->GetBoundsInScreen();
  49. gfx::Size preferred_size = widget_->GetContentsView()->GetPreferredSize();
  50. int horizontal_inset_dp = (bounds.width() - preferred_size.width()) / 2;
  51. int bottom_inset_dp = bounds.height() - kTopInsetDp - preferred_size.height();
  52. bounds.Inset(gfx::Insets::TLBR(kTopInsetDp, horizontal_inset_dp,
  53. bottom_inset_dp, horizontal_inset_dp));
  54. widget_->SetBounds(bounds);
  55. widget_->Show();
  56. }
  57. InSessionAuthDialog::~InSessionAuthDialog() = default;
  58. uint32_t InSessionAuthDialog::GetAuthMethods() const {
  59. return auth_methods_;
  60. }
  61. } // namespace ash