resolution_notification_controller.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2013 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. #ifndef ASH_DISPLAY_RESOLUTION_NOTIFICATION_CONTROLLER_H_
  5. #define ASH_DISPLAY_RESOLUTION_NOTIFICATION_CONTROLLER_H_
  6. #include <stdint.h>
  7. #include "ash/ash_export.h"
  8. #include "ash/display/display_change_dialog.h"
  9. #include "ash/display/window_tree_host_manager.h"
  10. #include "base/callback.h"
  11. #include "base/gtest_prod_util.h"
  12. #include "base/memory/weak_ptr.h"
  13. #include "chromeos/crosapi/mojom/cros_display_config.mojom.h"
  14. #include "ui/display/display_observer.h"
  15. #include "ui/gfx/geometry/size.h"
  16. namespace ash {
  17. FORWARD_DECLARE_TEST(DisplayPrefsTest, PreventStore);
  18. // A class which manages the dialog displayed to notify the user that
  19. // the display configuration has been changed.
  20. class ASH_EXPORT ResolutionNotificationController
  21. : public display::DisplayObserver,
  22. public WindowTreeHostManager::Observer {
  23. public:
  24. ResolutionNotificationController();
  25. ResolutionNotificationController(const ResolutionNotificationController&) =
  26. delete;
  27. ResolutionNotificationController& operator=(
  28. const ResolutionNotificationController&) = delete;
  29. ~ResolutionNotificationController() override;
  30. // If |display_id| is not the internal display and |source| is |kSourceUser|
  31. // (which means user initiated the change), Prepare a resolution change
  32. // notification for |display_id| from |old_resolution| to |new_resolution|,
  33. // which offers a button to revert the change in case something goes wrong.
  34. // The dialog is not dismissed by the user, the change is reverted.
  35. //
  36. // Then call DisplayManager::SetDisplayMode() to apply the resolution change,
  37. // and return the result; true if success, false otherwise.
  38. // In case SetDisplayMode() fails, the prepared notification will be
  39. // discarded.
  40. //
  41. // If |display_id| is the internal display or |source| is |kSourcePolicy|, the
  42. // resolution change is applied directly without preparing the confirm/revert
  43. // notification (this kind of notification is only useful for external
  44. // displays).
  45. //
  46. // This method does not create a notification itself. The notification will be
  47. // created the next OnDisplayConfigurationChanged(), which will be called
  48. // asynchronously after the resolution change is requested by this method.
  49. //
  50. // |accept_callback| will be called when the user accepts the resoltion change
  51. // by closing the notification bubble or clicking on the accept button (if
  52. // any).
  53. [[nodiscard]] bool PrepareNotificationAndSetDisplayMode(
  54. int64_t display_id,
  55. const display::ManagedDisplayMode& old_resolution,
  56. const display::ManagedDisplayMode& new_resolution,
  57. crosapi::mojom::DisplayConfigSource source,
  58. base::OnceClosure accept_callback);
  59. DisplayChangeDialog* dialog_for_testing() const {
  60. return confirmation_dialog_.get();
  61. }
  62. bool ShouldShowDisplayChangeDialog() const;
  63. private:
  64. friend class ResolutionNotificationControllerTest;
  65. FRIEND_TEST_ALL_PREFIXES(ResolutionNotificationControllerTest, Timeout);
  66. FRIEND_TEST_ALL_PREFIXES(DisplayPrefsTest, PreventStore);
  67. // A struct to bundle the data for a single resolution change.
  68. struct ResolutionChangeInfo;
  69. // Create a new modal dialog, or replace the dialog if it already exists.
  70. void CreateOrReplaceModalDialog();
  71. // Called when the user accepts the display resolution change. Set
  72. // |close_notification| to true when the notification should be removed.
  73. void AcceptResolutionChange();
  74. // Called when the user wants to revert the display resolution change.
  75. void RevertResolutionChange(bool display_was_removed);
  76. // display::DisplayObserver overrides:
  77. void OnDisplayRemoved(const display::Display& old_display) override;
  78. // WindowTreeHostManager::Observer overrides:
  79. void OnDisplayConfigurationChanged() override;
  80. std::unique_ptr<ResolutionChangeInfo> change_info_;
  81. display::ScopedDisplayObserver display_observer_{this};
  82. base::WeakPtr<DisplayChangeDialog> confirmation_dialog_;
  83. base::WeakPtrFactory<ResolutionNotificationController> weak_factory_{this};
  84. };
  85. } // namespace ash
  86. #endif // ASH_DISPLAY_RESOLUTION_NOTIFICATION_CONTROLLER_H_