OLD | NEW |
(Empty) | |
| 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 |
| 5 #ifndef CHROME_BROWSER_UI_WEBUI_MEDIA_ROUTER_QUERY_RESULT_MANAGER_H_ |
| 6 #define CHROME_BROWSER_UI_WEBUI_MEDIA_ROUTER_QUERY_RESULT_MANAGER_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <set> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/gtest_prod_util.h" |
| 13 #include "base/memory/linked_ptr.h" |
| 14 #include "base/observer_list.h" |
| 15 #include "base/threading/thread_checker.h" |
| 16 #include "chrome/browser/media/router/media_routes_observer.h" |
| 17 #include "chrome/browser/media/router/media_sink.h" |
| 18 #include "chrome/browser/media/router/media_source.h" |
| 19 #include "chrome/browser/ui/webui/media_router/media_cast_mode.h" |
| 20 #include "chrome/browser/ui/webui/media_router/media_sink_with_cast_modes.h" |
| 21 |
| 22 namespace media_router { |
| 23 |
| 24 class MediaRouter; |
| 25 class MediaSinksObserver; |
| 26 struct RoutesQueryResult; |
| 27 struct SinksQueryResult; |
| 28 |
| 29 // The Media Router dialog allows the user to initiate casting using one of |
| 30 // several actions (each represented by a cast mode). Each cast mode is |
| 31 // associated with a media source. This class allows the dialog to receive |
| 32 // lists of MediaSinks compatible with the cast modes available through the |
| 33 // dialog. |
| 34 // |
| 35 // Typical use: |
| 36 // |
| 37 // QueryResultManager::Observer* observer = ...; |
| 38 // QueryResultManager result_manager(router); |
| 39 // result_manager.AddObserver(observer); |
| 40 // result_manager.StartSinksQuery(MediaCastMode::DEFAULT, |
| 41 // ForPresentationUrl("http://google.com")); |
| 42 // result_manager.StartSinksQuery(MediaCastMode::TAB_MIRROR, |
| 43 // ForTabMediaSource(123)); |
| 44 // ... |
| 45 // [Updates will be received by observer via OnResultsUpdated()] |
| 46 // ... |
| 47 // [When info on MediaSource is needed, i.e. when requesting route for a mode] |
| 48 // CastModeSet cast_modes; |
| 49 // result_manager.GetSupportedCastModes(&cast_modes); |
| 50 // [Logic to select a MediaCastMode from the set] |
| 51 // MediaSource source = result_manager.GetSourceForCastMode( |
| 52 // MediaCastMode::TAB_MIRROR); |
| 53 // if (!source.Empty()) { |
| 54 // ... |
| 55 // } |
| 56 // |
| 57 // Not thread-safe. Must be used on a single thread. |
| 58 class QueryResultManager { |
| 59 public: |
| 60 class Observer { |
| 61 public: |
| 62 virtual ~Observer() {} |
| 63 |
| 64 // Updated results have been received. |
| 65 // |sinks|: List of sinks and the cast modes they are compatible with. |
| 66 virtual void OnResultsUpdated( |
| 67 const std::vector<MediaSinkWithCastModes>& sinks) = 0; |
| 68 }; |
| 69 |
| 70 explicit QueryResultManager(MediaRouter* media_router); |
| 71 ~QueryResultManager(); |
| 72 |
| 73 // Adds/removes an observer that is notified with query results. |
| 74 void AddObserver(Observer* observer); |
| 75 void RemoveObserver(Observer* observer); |
| 76 |
| 77 // Requests a list of MediaSinks compatible with |source| for |cast_mode|. |
| 78 // Results are sent to all observers registered with AddObserver(). |
| 79 // |
| 80 // May start a new query in the Media Router for the registered source if |
| 81 // there is no existing query for it. If there is an existing query for |
| 82 // |cast_mode|, it is stopped. |
| 83 // |
| 84 // If |source| is empty, no new queries are begun. |
| 85 void StartSinksQuery(MediaCastMode cast_mode, const MediaSource& source); |
| 86 |
| 87 // Stops notifying observers for |cast_mode|. |
| 88 void StopSinksQuery(MediaCastMode cast_mode); |
| 89 |
| 90 // Gets the set of cast modes that are being actively queried. |cast_mode_set| |
| 91 // should be empty. |
| 92 void GetSupportedCastModes(CastModeSet* cast_modes) const; |
| 93 |
| 94 // Returns the MediaSource registered for |cast_mode|. Returns an empty |
| 95 // MediaSource if there is none. |
| 96 MediaSource GetSourceForCastMode(MediaCastMode cast_mode) const; |
| 97 |
| 98 private: |
| 99 class CastModeMediaSinksObserver; |
| 100 |
| 101 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, Observers); |
| 102 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, StartRoutesDiscovery); |
| 103 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, MultipleQueries); |
| 104 |
| 105 // Sets the media source for |cast_mode|. |
| 106 void SetSourceForCastMode(MediaCastMode cast_mode, const MediaSource& source); |
| 107 |
| 108 // Stops and destroys the MediaSinksObserver for |cast_mode|. |
| 109 void RemoveObserverForCastMode(MediaCastMode cast_mode); |
| 110 |
| 111 // Returns true if the |entry|'s sink is compatible with at least one cast |
| 112 // mode. |
| 113 bool IsValid(const MediaSinkWithCastModes& entry) const; |
| 114 |
| 115 // Modifies the current set of results with |result| associated with |
| 116 // |cast_mode|. |
| 117 void UpdateWithSinksQueryResult(MediaCastMode cast_mode, |
| 118 const std::vector<MediaSink>& result); |
| 119 |
| 120 // Notifies observers that results have been updated. |
| 121 void NotifyOnResultsUpdated(); |
| 122 |
| 123 // MediaSinksObservers that listens for compatible MediaSink updates. |
| 124 // Each observer is associated with a MediaCastMode. Results received by |
| 125 // observers are propagated back to this class. |
| 126 // TODO(mfoltz): Remove linked_ptr when there is a ScopedPtrMap available. |
| 127 std::map<MediaCastMode, linked_ptr<MediaSinksObserver>> sinks_observers_; |
| 128 |
| 129 // Holds registrations of MediaSources for cast modes. |
| 130 std::map<MediaCastMode, MediaSource> cast_mode_sources_; |
| 131 |
| 132 // Holds all known sinks and their associated cast modes. |
| 133 std::map<MediaSinkId, MediaSinkWithCastModes> all_sinks_; |
| 134 |
| 135 // Registered observers. |
| 136 ObserverList<Observer> observers_; |
| 137 |
| 138 // Not owned by this object. |
| 139 MediaRouter* router_; |
| 140 |
| 141 base::ThreadChecker thread_checker_; |
| 142 |
| 143 DISALLOW_COPY_AND_ASSIGN(QueryResultManager); |
| 144 }; |
| 145 |
| 146 } // namespace media_router |
| 147 |
| 148 #endif // CHROME_BROWSER_UI_WEBUI_MEDIA_ROUTER_QUERY_RESULT_MANAGER_H_ |
OLD | NEW |