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

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

Powered by Google App Engine
This is Rietveld 408576698