Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1529)

Side by Side Diff: chrome/browser/media/router/presentation_service_delegate_impl.h

Issue 1132903002: [MediaRouter] Add implementation of PresentationServiceDelegate (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addresses Derek's comments Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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_MEDIA_ROUTER_PRESENTATION_SERVICE_DELEGATE_IMPL_H_
6 #define CHROME_BROWSER_MEDIA_ROUTER_PRESENTATION_SERVICE_DELEGATE_IMPL_H_
7
8 #include <map>
9 #include <string>
10 #include <utility>
11
12 #include "base/gtest_prod_util.h"
13 #include "base/macros.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/observer_list.h"
17 #include "chrome/browser/media/router/media_router.h"
18 #include "chrome/browser/media/router/media_source.h"
19 #include "content/public/browser/presentation_service_delegate.h"
20 #include "content/public/browser/web_contents_observer.h"
21 #include "content/public/browser/web_contents_user_data.h"
22
23 namespace content {
24 class RenderFrameHost;
25 class PresentationScreenAvailabilityListener;
26 class WebContents;
27 struct PresentationSessionInfo;
28 struct PresentationSessionMessage;
29 }
30
31 namespace media_router {
32
33 class MediaRoute;
34 class MediaSinksObserver;
35 class PresentationFrameMap;
36
37 using RenderFrameHostId = std::pair<int, int>;
38
39 // Implementation of PresentationServiceDelegate that that interfaces an
40 // instance of WebContents with the Chrome Media Router.
41 // In addition to fulfilling Presentation requests, it also
42 // provide tab-level information that is required for creating
43 // browser-initiated sessions.
44 class PresentationServiceDelegateImpl
45 : public content::WebContentsUserData<PresentationServiceDelegateImpl>,
46 public content::PresentationServiceDelegate {
47 public:
48 // Maximum number of sources that can be registered at a time.
49 static const size_t kMaxNumSources = 256;
imcheng (use chromium acct) 2015/05/19 20:03:29 This should be defined in PresentationFrameMap sin
haibinlu 2015/05/19 21:14:35 It is sort of public contract with the caller of t
50
51 ~PresentationServiceDelegateImpl() override;
52
53 // content::PresentationServiceDelegate implementation.
54 // PresentationScreenAvailabilityListener is one per
55 // (frame, presentation URL), while PresentationServiceDelegate::Observer
56 // is one per frame.
57 void AddObserver(
58 int render_process_id,
59 int render_frame_id,
60 content::PresentationServiceDelegate::Observer* observer) override;
61 void RemoveObserver(int render_process_id, int render_frame_id) override;
62 bool AddScreenAvailabilityListener(
63 int render_process_id,
64 int render_frame_id,
65 content::PresentationScreenAvailabilityListener* listener) override;
66 void RemoveScreenAvailabilityListener(
67 int render_process_id,
68 int render_frame_id,
69 content::PresentationScreenAvailabilityListener* listener) override;
70 void Reset(int render_process_id, int render_frame_id) override;
71 void SetDefaultPresentationUrl(
72 int render_process_id,
73 int render_frame_id,
74 const std::string& default_presentation_url,
75 const std::string& default_presentation_id) override;
76 void StartSession(int render_process_id,
77 int render_frame_id,
78 const std::string& presentation_url,
79 const std::string& presentation_id,
80 const PresentationSessionSuccessCallback& success_cb,
81 const PresentationSessionErrorCallback& error_cb) override;
82 void JoinSession(int render_process_id,
83 int render_frame_id,
84 const std::string& presentation_url,
85 const std::string& presentation_id,
86 const PresentationSessionSuccessCallback& success_cb,
87 const PresentationSessionErrorCallback& error_cb) override;
88 void ListenForSessionMessages(
89 int render_process_id,
90 int render_frame_id,
91 const PresentationSessionMessageCallback& message_cb) override;
92 void SendMessage(
93 int render_process_id,
94 int render_frame_id,
95 scoped_ptr<content::PresentationSessionMessage> message_request,
96 const SendMessageCallback& send_message_cb) override;
97
98 // Notifies this object that |route| has been created or joined outside of a
99 // Presentation API request. The route could be due to
100 // browser action (e.g., browser initiated media router dialog) or
101 // a media route provider (e.g., autojoin).
102 void OnRouteCreated(const MediaRoute& route);
103
104 // Returns the default MediaSource for this tab if there is one.
105 // Returns an empty MediaSource otherwise.
106 MediaSource default_source() const { return default_source_; }
107
108 // Observer interface for listening to default MediaSource changes for the
109 // WebContents.
110 class DefaultMediaSourceObserver {
111 public:
112 virtual ~DefaultMediaSourceObserver() {}
113
114 // Called when default media source for the corresponding WebContents has
115 // changed.
116 // |source|: New default MediaSource, or empty if default was removed.
117 // |source_host|: Host name of the frame that contains the default media
118 // source, or empty if there is no default media source.
119 virtual void OnDefaultMediaSourceChanged(
120 const MediaSource& source,
121 const std::string& source_host) = 0;
122 };
123
124 // Adds / removes an observer for listening to default MediaSource changes.
125 void AddDefaultMediaSourceObserver(DefaultMediaSourceObserver* observer);
126 void RemoveDefaultMediaSourceObserver(DefaultMediaSourceObserver* observer);
127
128 base::WeakPtr<PresentationServiceDelegateImpl> GetWeakPtr();
129
130 private:
131 explicit PresentationServiceDelegateImpl(content::WebContents* web_contents);
132 friend class content::WebContentsUserData<PresentationServiceDelegateImpl>;
133
134 friend class PresentationServiceDelegateImplTest;
135 friend class PresentationServiceDelegateImplBrowserTest;
136 FRIEND_TEST_ALL_PREFIXES(PresentationServiceDelegateImplTest,
137 AddScreenAvailabilityListener);
138 FRIEND_TEST_ALL_PREFIXES(PresentationServiceDelegateImplTest,
139 MaxNumObservers);
140 FRIEND_TEST_ALL_PREFIXES(PresentationServiceDelegateImplTest,
141 AddListenerSameUrlTwice);
142 FRIEND_TEST_ALL_PREFIXES(PresentationServiceDelegateImplTest, Reset);
143 FRIEND_TEST_ALL_PREFIXES(PresentationServiceDelegateImplTest,
144 DelegateObservers);
145 FRIEND_TEST_ALL_PREFIXES(PresentationServiceDelegateImplBrowserTest,
146 RemoveObserversOnNavigation);
147
148 // Returns |listener|'s presentation URL as a MediaSource. If |listener| does
149 // not have a persentation URL, returns the tab mirroring MediaSource.
150 MediaSource GetMediaSourceFromListener(
151 content::PresentationScreenAvailabilityListener* listener);
152
153 // Returns |true| if frame |rfh_id| is the main frame of |web_contents_|.
154 bool IsMainFrame(RenderFrameHostId rfh_id) const;
155
156 // Updates tab-level default MediaSource and source host name. If either
157 // changed, notify the observers.
158 void UpdateDefaultMediaSourceAndNotifyObservers(
159 const MediaSource& new_default_source,
160 const std::string& new_default_source_host);
161
162 void SetMediaRouterForTest(MediaRouter* router);
163
164 bool HasScreenAvailabilityListenerForTest(
165 RenderFrameHostId rfh_id,
166 const MediaSourceId& source_id) const;
167
168 // Manages frames that are using presentation API.
169 scoped_ptr<PresentationFrameMap> frame_map_;
170
171 MediaSource default_source_;
172 std::string default_source_host_;
173
174 content::WebContents* web_contents_;
175
176 // Observers listening for changes to default media source. Not owned by
177 // this class.
178 ObserverList<DefaultMediaSourceObserver> default_media_source_observers_;
179
180 // Does not own this object.
181 MediaRouter* router_;
182
183 // NOTE: All WeakPtrs must be invalidated before member variables.
184 base::WeakPtrFactory<PresentationServiceDelegateImpl> weak_factory_;
185
186 DISALLOW_COPY_AND_ASSIGN(PresentationServiceDelegateImpl);
187 };
188
189 } // namespace media_router
190
191 #endif // CHROME_BROWSER_MEDIA_ROUTER_PRESENTATION_SERVICE_DELEGATE_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698