media_sinks_observer.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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_SINKS_OBSERVER_H_
  5. #define COMPONENTS_MEDIA_ROUTER_BROWSER_MEDIA_SINKS_OBSERVER_H_
  6. #include <vector>
  7. #include "base/memory/raw_ptr.h"
  8. #include "components/media_router/common/media_sink.h"
  9. #include "components/media_router/common/media_source.h"
  10. #include "third_party/abseil-cpp/absl/types/optional.h"
  11. #include "url/origin.h"
  12. namespace media_router {
  13. class MediaRouter;
  14. // Base class for observing when the collection of sinks compatible with
  15. // a MediaSource has been updated.
  16. // A MediaSinksObserver implementation can be registered to MediaRouter to
  17. // receive results. It can then interpret / process the results accordingly.
  18. // More documentation can be found at
  19. // docs.google.com/document/d/1RDXdzi2y7lRuL08HAe-qlSJG2DMz2iH3gBzMs0IRR78
  20. class MediaSinksObserver {
  21. public:
  22. // Constructs an observer from |origin| that will observe for sinks compatible
  23. // with |source|.
  24. MediaSinksObserver(MediaRouter* router,
  25. const MediaSource& source,
  26. const url::Origin& origin);
  27. // Constructs an observer for all sinks known to |router|.
  28. // NOTE: Not all Media Route Providers support sink queries with an empty
  29. // source, so the returned sink list may be incomplete.
  30. // TODO(crbug.com/929937): Fix this.
  31. explicit MediaSinksObserver(MediaRouter* router);
  32. MediaSinksObserver(const MediaSinksObserver&) = delete;
  33. MediaSinksObserver& operator=(const MediaSinksObserver&) = delete;
  34. virtual ~MediaSinksObserver();
  35. // Registers with MediaRouter to start observing. Must be called before the
  36. // observer will start receiving updates. Returns |true| if the observer is
  37. // initialized. This method is no-op if the observer is already initialized.
  38. bool Init();
  39. // This function is invoked when the list of sinks compatible with |source_|
  40. // has been updated, unless |source_| is nullopt, in which case it is invoked
  41. // with all sinks. The result also contains the list of valid origins.
  42. // If |origins| is empty or contains |origin_|, then |OnSinksReceived(sinks)|
  43. // will be invoked with |sinks|. Otherwise, it will be invoked with an empty
  44. // list.
  45. // Marked virtual for tests.
  46. virtual void OnSinksUpdated(const std::vector<MediaSink>& sinks,
  47. const std::vector<url::Origin>& origins);
  48. const absl::optional<const MediaSource>& source() const { return source_; }
  49. protected:
  50. // This function is invoked from |OnSinksUpdated(sinks, origins)|.
  51. // Implementations may not perform operations that modify the Media Router's
  52. // observer list. In particular, invoking this observer's destructor within
  53. // OnSinksReceived will result in undefined behavior.
  54. virtual void OnSinksReceived(const std::vector<MediaSink>& sinks) = 0;
  55. private:
  56. const absl::optional<const MediaSource> source_;
  57. const url::Origin origin_;
  58. const raw_ptr<MediaRouter> router_;
  59. bool initialized_;
  60. #if DCHECK_IS_ON()
  61. bool in_on_sinks_updated_ = false;
  62. #endif
  63. };
  64. } // namespace media_router
  65. #endif // COMPONENTS_MEDIA_ROUTER_BROWSER_MEDIA_SINKS_OBSERVER_H_