summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/webui/media_router/query_result_manager.h
blob: f355b07d5acca7db49079ed984f77b093f9a62ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_UI_WEBUI_MEDIA_ROUTER_QUERY_RESULT_MANAGER_H_
#define CHROME_BROWSER_UI_WEBUI_MEDIA_ROUTER_QUERY_RESULT_MANAGER_H_

#include <map>
#include <set>
#include <vector>

#include "base/gtest_prod_util.h"
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/observer_list.h"
#include "base/threading/thread_checker.h"
#include "chrome/browser/media/router/media_routes_observer.h"
#include "chrome/browser/media/router/media_sink.h"
#include "chrome/browser/media/router/media_source.h"
#include "chrome/browser/ui/webui/media_router/media_cast_mode.h"
#include "chrome/browser/ui/webui/media_router/media_sink_with_cast_modes.h"

namespace media_router {

class MediaRouter;
class MediaSinksObserver;
struct RoutesQueryResult;
struct SinksQueryResult;

// The Media Router dialog allows the user to initiate casting using one of
// several actions (each represented by a cast mode).  Each cast mode is
// associated with a media source.  This class allows the dialog to receive
// lists of MediaSinks compatible with the cast modes available through the
// dialog.
//
// Typical use:
//
//   QueryResultManager::Observer* observer = ...;
//   QueryResultManager result_manager(router);
//   result_manager.AddObserver(observer);
//   result_manager.StartSinksQuery(MediaCastMode::DEFAULT,
//       MediaSourceForPresentationUrl("http://google.com"));
//   result_manager.StartSinksQuery(MediaCastMode::TAB_MIRROR,
//       MediaSourceForTab(123));
//   ...
//   [Updates will be received by observer via OnResultsUpdated()]
//   ...
//   [When info on MediaSource is needed, i.e. when requesting route for a mode]
//   CastModeSet cast_modes;
//   result_manager.GetSupportedCastModes(&cast_modes);
//   [Logic to select a MediaCastMode from the set]
//   MediaSource source = result_manager.GetSourceForCastMode(
//       MediaCastMode::TAB_MIRROR);
//   if (!source.Empty()) {
//     ...
//   }
//
// Not thread-safe.  Must be used on a single thread.
class QueryResultManager {
 public:
  class Observer {
   public:
    virtual ~Observer() {}

    // Updated results have been received.
    // |sinks|: List of sinks and the cast modes they are compatible with.
    virtual void OnResultsUpdated(
        const std::vector<MediaSinkWithCastModes>& sinks) = 0;
  };

  explicit QueryResultManager(MediaRouter* media_router);
  ~QueryResultManager();

  // Adds/removes an observer that is notified with query results.
  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

  // Requests a list of MediaSinks compatible with |source| for |cast_mode|.
  // Results are sent to all observers registered with AddObserver().
  //
  // May start a new query in the Media Router for the registered source if
  // there is no existing query for it.  If there is an existing query for
  // |cast_mode|, it is stopped.
  //
  // If |source| is empty, no new queries are begun.
  void StartSinksQuery(MediaCastMode cast_mode, const MediaSource& source);

  // Stops notifying observers for |cast_mode|.
  void StopSinksQuery(MediaCastMode cast_mode);

  // Gets the set of cast modes that are being actively queried. |cast_mode_set|
  // should be empty.
  void GetSupportedCastModes(CastModeSet* cast_modes) const;

  // Returns the MediaSource registered for |cast_mode|.  Returns an empty
  // MediaSource if there is none.
  MediaSource GetSourceForCastMode(MediaCastMode cast_mode) const;

 private:
  class CastModeMediaSinksObserver;

  FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, Observers);
  FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, StartRoutesDiscovery);
  FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, MultipleQueries);

  // Sets the media source for |cast_mode|.
  void SetSourceForCastMode(MediaCastMode cast_mode, const MediaSource& source);

  // Stops and destroys the MediaSinksObserver for |cast_mode|.
  void RemoveObserverForCastMode(MediaCastMode cast_mode);

  // Returns true if the |entry|'s sink is compatible with at least one cast
  // mode.
  bool IsValid(const MediaSinkWithCastModes& entry) const;

  // Modifies the current set of results with |result| associated with
  // |cast_mode|.
  void UpdateWithSinksQueryResult(MediaCastMode cast_mode,
                                  const std::vector<MediaSink>& result);

  // Notifies observers that results have been updated.
  void NotifyOnResultsUpdated();

  // MediaSinksObservers that listens for compatible MediaSink updates.
  // Each observer is associated with a MediaCastMode. Results received by
  // observers are propagated back to this class.
  std::map<MediaCastMode, scoped_ptr<MediaSinksObserver>> sinks_observers_;

  // Holds registrations of MediaSources for cast modes.
  std::map<MediaCastMode, MediaSource> cast_mode_sources_;

  // Holds all known sinks and their associated cast modes.
  std::map<MediaSink::Id, MediaSinkWithCastModes> all_sinks_;

  // Registered observers.
  base::ObserverList<Observer> observers_;

  // Not owned by this object.
  MediaRouter* router_;

  base::ThreadChecker thread_checker_;

  DISALLOW_COPY_AND_ASSIGN(QueryResultManager);
};

}  // namespace media_router

#endif  // CHROME_BROWSER_UI_WEBUI_MEDIA_ROUTER_QUERY_RESULT_MANAGER_H_