media_router_dialog_controller.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2015 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 COMPONENTS_MEDIA_ROUTER_BROWSER_MEDIA_ROUTER_DIALOG_CONTROLLER_H_
  5. #define COMPONENTS_MEDIA_ROUTER_BROWSER_MEDIA_ROUTER_DIALOG_CONTROLLER_H_
  6. #include <memory>
  7. #include "base/callback.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "content/public/browser/presentation_request.h"
  10. #include "content/public/browser/presentation_service_delegate.h"
  11. #include "content/public/browser/web_contents_observer.h"
  12. namespace content {
  13. class WebContents;
  14. } // namespace content
  15. namespace media_router {
  16. class StartPresentationContext;
  17. enum class MediaRouterDialogActivationLocation;
  18. // An abstract base class for Media Router dialog controllers. Tied to a
  19. // WebContents known as the |initiator|, and is lazily created when a Media
  20. // Router dialog needs to be shown. The MediaRouterDialogController allows
  21. // showing and closing a Media Router dialog modal to the initiator WebContents.
  22. // This class is not thread safe and must be called on the UI thread.
  23. class MediaRouterDialogController {
  24. public:
  25. MediaRouterDialogController(const MediaRouterDialogController&) = delete;
  26. MediaRouterDialogController& operator=(const MediaRouterDialogController&) =
  27. delete;
  28. virtual ~MediaRouterDialogController();
  29. using GetOrCreate = base::RepeatingCallback<MediaRouterDialogController*(
  30. content::WebContents*)>;
  31. // Sets the factory/getter for MediaRouterDialogController, which is platform
  32. // and embedder dependent. The callback should create or return an existing
  33. // instance as needed.
  34. static void SetGetOrCreate(const GetOrCreate& get_or_create);
  35. // Gets a reference to the MediaRouterDialogController associated with
  36. // |web_contents|, creating one if it does not exist. The returned pointer is
  37. // guaranteed to be non-null.
  38. static MediaRouterDialogController* GetOrCreateForWebContents(
  39. content::WebContents* web_contents);
  40. // Shows the media router dialog modal to |initiator_|, with additional
  41. // context for a PresentationRequest coming from the page given by the input
  42. // parameters.
  43. // Returns true if the dialog is created as a result of this call.
  44. // If the dialog already exists, or dialog cannot be created, then false is
  45. // returned, and |error_cb| will be invoked.
  46. virtual bool ShowMediaRouterDialogForPresentation(
  47. std::unique_ptr<StartPresentationContext> context);
  48. // Shows the media router dialog modal to |initiator_|.
  49. // Creates the dialog if it did not exist prior to this call, returns true.
  50. // If the dialog already exists, brings it to the front, returns false.
  51. virtual bool ShowMediaRouterDialog(
  52. MediaRouterDialogActivationLocation activation_location);
  53. // Hides the media router dialog.
  54. // It is a no-op to call this function if there is currently no dialog.
  55. void HideMediaRouterDialog();
  56. // Indicates if the media router dialog already exists.
  57. virtual bool IsShowingMediaRouterDialog() const = 0;
  58. protected:
  59. // Use MediaRouterDialogController::GetOrCreateForWebContents() to create an
  60. // instance.
  61. explicit MediaRouterDialogController(content::WebContents* initiator);
  62. // Creates a media router dialog if necessary, then activates the WebContents
  63. // that initiated the dialog, e.g. focuses the tab.
  64. void FocusOnMediaRouterDialog(
  65. bool dialog_needs_creation,
  66. MediaRouterDialogActivationLocation activation_location);
  67. // Returns the WebContents that initiated showing the dialog.
  68. content::WebContents* initiator() const { return initiator_; }
  69. // Resets the state of the controller. Must be called from the overrides.
  70. virtual void Reset();
  71. // Creates a new media router dialog modal to |initiator_|.
  72. virtual void CreateMediaRouterDialog(
  73. MediaRouterDialogActivationLocation activation_location) = 0;
  74. // Closes the media router dialog if it exists.
  75. virtual void CloseMediaRouterDialog() = 0;
  76. // Data for dialogs created at the request of the Presentation API.
  77. // Created from arguments passed in via ShowMediaRouterDialogForPresentation.
  78. std::unique_ptr<StartPresentationContext> start_presentation_context_;
  79. private:
  80. class InitiatorWebContentsObserver;
  81. // An observer for the |initiator_| that closes the dialog when |initiator_|
  82. // is destroyed or navigated.
  83. std::unique_ptr<InitiatorWebContentsObserver> initiator_observer_;
  84. const raw_ptr<content::WebContents> initiator_;
  85. };
  86. } // namespace media_router
  87. #endif // COMPONENTS_MEDIA_ROUTER_BROWSER_MEDIA_ROUTER_DIALOG_CONTROLLER_H_