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

Side by Side Diff: chrome/browser/media/router/receiver_presentation_service_delegate_impl.cc

Issue 1314413005: [Presentation API] 1-UA presentation support + presenter APIs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: addressed yuri's comments #16 Created 5 years, 2 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 #include "chrome/browser/media/router/receiver_presentation_service_delegate_imp l.h"
6
7 #include "chrome/browser/media/router/offscreen_presentation_manager.h"
8 #include "chrome/browser/media/router/offscreen_presentation_manager_factory.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "content/public/browser/presentation_session_state_listener.h"
11
12 DEFINE_WEB_CONTENTS_USER_DATA_KEY(
13 media_router::ReceiverPresentationServiceDelegateImpl);
14
15 using content::PresentationServiceDelegate;
16 using content::RenderFrameHost;
17
18 namespace media_router {
19
20 using OffscreenPresentationSession =
21 OffscreenPresentationManager::OffscreenPresentationSession;
22
23 // static
24 void ReceiverPresentationServiceDelegateImpl::CreateForWebContents(
25 content::WebContents* web_contents,
26 const std::string& presentation_id) {
27 DCHECK(web_contents);
mark a. foltz 2015/10/12 21:56:10 Who is responsible for checking that the WebConten
imcheng 2015/10/17 01:00:24 Right, offscreen tab API calls this with the offsc
28
29 if (FromWebContents(web_contents))
30 return;
31
32 web_contents->SetUserData(UserDataKey(),
33 new ReceiverPresentationServiceDelegateImpl(
34 web_contents, presentation_id));
35 }
36
37 ReceiverPresentationServiceDelegateImpl::
38 ~ReceiverPresentationServiceDelegateImpl() {
39 for (auto& observer_pair : observers_)
40 observer_pair.second->OnDelegateDestroyed();
41
42 receiver_sessions_.clear();
43 offscreen_presentation_manager_->UnregisterOffscreenPresentationReceiver(
44 presentation_id_);
45 }
46
47 void ReceiverPresentationServiceDelegateImpl::AddObserver(
48 int render_process_id,
49 int render_frame_id,
50 content::PresentationServiceDelegate::Observer* observer) {
51 DCHECK(observer);
52
53 RenderFrameHostId rfh_id(render_process_id, render_frame_id);
54 DCHECK(!ContainsKey(observers_, rfh_id));
55 observers_[rfh_id] = observer;
56 }
57
58 void ReceiverPresentationServiceDelegateImpl::RemoveObserver(
59 int render_process_id,
60 int render_frame_id) {
61 observers_.erase(RenderFrameHostId(render_process_id, render_frame_id));
62 }
63
64 bool ReceiverPresentationServiceDelegateImpl::AddScreenAvailabilityListener(
65 int render_process_id,
66 int render_frame_id,
67 content::PresentationScreenAvailabilityListener* listener) {
68 NOTIMPLEMENTED();
69 return false;
70 }
71
72 void ReceiverPresentationServiceDelegateImpl::RemoveScreenAvailabilityListener(
73 int render_process_id,
74 int render_frame_id,
75 content::PresentationScreenAvailabilityListener* listener) {
76 NOTIMPLEMENTED();
77 }
78
79 void ReceiverPresentationServiceDelegateImpl::Reset(int render_process_id,
80 int render_frame_id) {
81 DVLOG(2) << __FUNCTION__ << render_process_id << ", " << render_frame_id;
82
83 receiver_sessions_.clear();
84 receiver_available_callback_.Run(nullptr);
85 receiver_available_callback_.Reset();
86 }
87
88 void ReceiverPresentationServiceDelegateImpl::SetDefaultPresentationUrl(
89 int render_process_id,
90 int render_frame_id,
91 const std::string& default_presentation_url) {
92 NOTIMPLEMENTED();
93 }
94
95 void ReceiverPresentationServiceDelegateImpl::StartSession(
96 int render_process_id,
97 int render_frame_id,
98 const std::string& presentation_url,
99 const PresentationSessionSuccessCallback& success_cb,
100 const PresentationSessionErrorCallback& error_cb) {
101 NOTIMPLEMENTED();
102 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN,
103 "Not implemented"));
104 }
105
106 void ReceiverPresentationServiceDelegateImpl::JoinSession(
107 int render_process_id,
108 int render_frame_id,
109 const std::string& presentation_url,
110 const std::string& presentation_id,
111 const PresentationSessionSuccessCallback& success_cb,
112 const PresentationSessionErrorCallback& error_cb) {
113 NOTIMPLEMENTED();
114 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN,
115 "Not implemented"));
116 }
117
118 void ReceiverPresentationServiceDelegateImpl::CloseSession(
119 int render_process_id,
120 int render_frame_id,
121 const std::string& presentation_id) {
122 NOTIMPLEMENTED();
123 }
124
125 void ReceiverPresentationServiceDelegateImpl::ListenForSessionMessages(
126 int render_process_id,
127 int render_frame_id,
128 const content::PresentationSessionInfo& session_info,
129 const content::PresentationSessionMessageCallback& message_cb) {
130 DVLOG(2) << __FUNCTION__ << render_process_id << ", " << render_frame_id;
131
132 if (!IsMainFrame(render_process_id, render_frame_id)) {
133 DVLOG(2) << __FUNCTION__ << ": not main frame";
134 return;
135 }
136
137 OffscreenPresentationSession* offscreen_session = FindSession(session_info);
138 if (!offscreen_session)
139 return;
140
141 offscreen_session->ListenForMessages(message_cb);
142 }
143
144 void ReceiverPresentationServiceDelegateImpl::SendMessage(
145 int render_process_id,
146 int render_frame_id,
147 const content::PresentationSessionInfo& session_info,
148 scoped_ptr<content::PresentationSessionMessage> message,
149 const content::SendMessageCallback& send_message_cb) {
150 DVLOG(2) << __FUNCTION__ << render_process_id << ", " << render_frame_id;
151
152 if (!IsMainFrame(render_process_id, render_frame_id)) {
153 DVLOG(2) << __FUNCTION__ << ": not main frame";
154 send_message_cb.Run(false);
155 return;
156 }
157
158 OffscreenPresentationSession* offscreen_session = FindSession(session_info);
159 if (!offscreen_session) {
160 send_message_cb.Run(false);
161 return;
162 }
163
164 offscreen_session->SendMessage(message.Pass(), send_message_cb);
165 }
166
167 bool ReceiverPresentationServiceDelegateImpl::ListenForSessionStateChange(
168 int render_process_id,
169 int render_frame_id,
170 content::PresentationSessionStateListener* listener) {
171 DVLOG(2) << __FUNCTION__ << render_process_id << ", " << render_frame_id;
172
173 if (!IsMainFrame(render_process_id, render_frame_id)) {
174 DVLOG(2) << __FUNCTION__ << ": not main frame";
175 return false;
176 }
177
178 OffscreenPresentationSession* offscreen_session =
179 FindSession(listener->GetSessionInfo());
180 if (!offscreen_session)
181 return false;
182
183 offscreen_session->ListenForStateChanges(listener);
184 return true;
185 }
186
187 void ReceiverPresentationServiceDelegateImpl::
188 NotifyWhenReceiverSessionIsAvailable(
189 int render_process_id,
190 int render_frame_id,
191 const content::PresentationReceiverSessionAvailableCallback& callback) {
192 DVLOG(2) << __FUNCTION__ << render_process_id << ", " << render_frame_id;
193 DCHECK(!receiver_available_callback_.is_null());
194
195 if (!IsMainFrame(render_process_id, render_frame_id)) {
196 DVLOG(2) << __FUNCTION__ << ": not main frame";
197 callback.Run(nullptr);
198 return;
199 }
200
201 if (!receiver_sessions_.empty()) {
202 content::PresentationSessionInfo session_info(std::string(),
203 presentation_id_);
204 callback.Run(&session_info);
205 return;
206 }
207
208 receiver_available_callback_ = callback;
209 }
210
211 std::vector<content::PresentationSessionInfo>
212 ReceiverPresentationServiceDelegateImpl::GetPresentationReceiverSessions(
213 int render_process_id,
214 int render_frame_id) {
215 DVLOG(2) << __FUNCTION__ << render_process_id << ", " << render_frame_id;
216
217 if (!IsMainFrame(render_process_id, render_frame_id)) {
218 DVLOG(2) << __FUNCTION__ << ": not main frame";
219 return std::vector<content::PresentationSessionInfo>();
220 }
221
222 // TODO(imcheng): This is currently broken for the multiple controllers case.
223 // We need additional ID to distinguish between controllers. crbug.com/529911
224 return std::vector<content::PresentationSessionInfo>(
225 receiver_sessions_.size(),
226 content::PresentationSessionInfo(std::string(), presentation_id_));
227 }
228
229 ReceiverPresentationServiceDelegateImpl::
230 ReceiverPresentationServiceDelegateImpl(content::WebContents* web_contents,
231 const std::string& presentation_id)
232 : web_contents_(web_contents),
233 presentation_id_(presentation_id),
234 offscreen_presentation_manager_(
235 OffscreenPresentationManagerFactory::GetOrCreateForBrowserContext(
236 Profile::FromBrowserContext(web_contents_->GetBrowserContext())
237 ->GetOriginalProfile())) {
238 DCHECK(web_contents_);
239 DCHECK(!presentation_id.empty());
240 DCHECK(offscreen_presentation_manager_);
241
242 offscreen_presentation_manager_->RegisterOffscreenPresentationReceiver(
243 presentation_id_,
244 base::Bind(
245 &ReceiverPresentationServiceDelegateImpl::OnReceiverSessionAvailable,
246 base::Unretained(this)));
247 }
248
249 void ReceiverPresentationServiceDelegateImpl::OnReceiverSessionAvailable(
250 scoped_ptr<OffscreenPresentationSession> session) {
251 if (!receiver_available_callback_.is_null()) {
252 content::PresentationSessionInfo session_info(std::string(),
253 presentation_id_);
254 receiver_available_callback_.Run(&session_info);
255 receiver_available_callback_.Reset();
256 }
257
258 receiver_sessions_.push_back(session.Pass());
259 }
260
261 bool ReceiverPresentationServiceDelegateImpl::IsMainFrame(int render_process_id,
262 int render_frame_id) {
263 content::RenderFrameHost* render_frame_host = web_contents_->GetMainFrame();
264 DCHECK(render_frame_host);
265
266 return render_process_id == render_frame_host->GetProcess()->GetID() &&
267 render_frame_id == render_frame_host->GetRoutingID();
268 }
269
270 OffscreenPresentationSession*
271 ReceiverPresentationServiceDelegateImpl::FindSession(
272 const content::PresentationSessionInfo& session_info) {
273 // TODO(imcheng): Need additional information to locate correct session object
274 // in multiple controllers case. crbug.com/529911
275 if (receiver_sessions_.empty()) {
276 DVLOG(2) << "Session not found: " << session_info.presentation_id;
277 return nullptr;
278 } else {
279 return receiver_sessions_.front();
280 }
281 }
282
283 } // namespace media_router
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698