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

Side by Side Diff: content/browser/presentation/presentation_service_impl.cc

Issue 979413002: [Presentation API] Additional plumbing for PresentationServiceImpl. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@REAL-NEW-MASTER
Patch Set: Rebase Created 5 years, 9 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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/presentation/presentation_service_impl.h" 5 #include "content/browser/presentation/presentation_service_impl.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "content/browser/presentation/presentation_type_converters.h"
8 #include "content/public/browser/content_browser_client.h" 9 #include "content/public/browser/content_browser_client.h"
9 #include "content/public/browser/navigation_details.h" 10 #include "content/public/browser/navigation_details.h"
10 #include "content/public/browser/render_frame_host.h" 11 #include "content/public/browser/render_frame_host.h"
11 #include "content/public/browser/render_process_host.h" 12 #include "content/public/browser/render_process_host.h"
12 #include "content/public/browser/web_contents.h" 13 #include "content/public/browser/web_contents.h"
13 #include "content/public/common/content_client.h" 14 #include "content/public/common/content_client.h"
14 #include "content/public/common/frame_navigate_params.h" 15 #include "content/public/common/frame_navigate_params.h"
15 16
16 namespace content { 17 namespace content {
17 18
18 PresentationServiceImpl::PresentationServiceImpl( 19 PresentationServiceImpl::PresentationServiceImpl(
19 RenderFrameHost* render_frame_host, 20 RenderFrameHost* render_frame_host,
20 WebContents* web_contents, 21 WebContents* web_contents,
21 PresentationServiceDelegate* delegate) 22 PresentationServiceDelegate* delegate)
22 : WebContentsObserver(web_contents), 23 : WebContentsObserver(web_contents),
23 render_frame_host_(render_frame_host), 24 render_frame_host_(render_frame_host),
24 delegate_(delegate) { 25 delegate_(delegate),
26 weak_factory_(this) {
25 DCHECK(render_frame_host_); 27 DCHECK(render_frame_host_);
26 DCHECK(web_contents); 28 DCHECK(web_contents);
27 VLOG(2) << "PresentationServiceImpl: " 29 VLOG(2) << "PresentationServiceImpl: "
mark a. foltz 2015/03/11 01:08:18 Is VLOG() necessary for these messages, or would D
imcheng 2015/03/12 20:03:41 Converted to DVLOG().
28 << render_frame_host_->GetProcess()->GetID() << ", " 30 << render_frame_host_->GetProcess()->GetID() << ", "
29 << render_frame_host_->GetRoutingID(); 31 << render_frame_host_->GetRoutingID();
30 if (delegate_) 32 if (delegate_)
31 delegate_->AddObserver(this); 33 delegate_->AddObserver(this);
32 } 34 }
33 35
34 PresentationServiceImpl::~PresentationServiceImpl() { 36 PresentationServiceImpl::~PresentationServiceImpl() {
35 if (delegate_) 37 if (delegate_)
36 delegate_->RemoveObserver(this); 38 delegate_->RemoveObserver(this);
37 } 39 }
38 40
39 // static 41 // static
40 void PresentationServiceImpl::CreateMojoService( 42 void PresentationServiceImpl::CreateMojoService(
41 RenderFrameHost* render_frame_host, 43 RenderFrameHost* render_frame_host,
42 mojo::InterfaceRequest<presentation::PresentationService> request) { 44 mojo::InterfaceRequest<presentation::PresentationService> request) {
43 VLOG(2) << "PresentationServiceImpl::CreateService"; 45 VLOG(2) << "CreateMojoService";
44 WebContents* web_contents = 46 WebContents* web_contents =
45 WebContents::FromRenderFrameHost(render_frame_host); 47 WebContents::FromRenderFrameHost(render_frame_host);
46 DCHECK(web_contents); 48 DCHECK(web_contents);
47 49
48 mojo::BindToRequest( 50 mojo::BindToRequest(
49 new PresentationServiceImpl( 51 new PresentationServiceImpl(
50 render_frame_host, 52 render_frame_host,
51 web_contents, 53 web_contents,
52 GetContentClient()->browser()->GetPresentationServiceDelegate( 54 GetContentClient()->browser()->GetPresentationServiceDelegate(
53 web_contents)), 55 web_contents)),
54 &request); 56 &request);
55 } 57 }
56 58
57 void PresentationServiceImpl::OnConnectionError() { 59 void PresentationServiceImpl::OnConnectionError() {
58 VLOG(1) << "PresentationServiceImpl::OnConnectionError: " 60 VLOG(1) << "OnConnectionError: "
59 << render_frame_host_->GetProcess()->GetID() << ", " 61 << render_frame_host_->GetProcess()->GetID() << ", "
60 << render_frame_host_->GetRoutingID(); 62 << render_frame_host_->GetRoutingID();
61 } 63 }
62 64
63 void PresentationServiceImpl::GetScreenAvailability( 65 void PresentationServiceImpl::GetScreenAvailability(
64 const mojo::String& presentation_url, 66 const mojo::String& presentation_url,
65 const ScreenAvailabilityMojoCallback& callback) { 67 const ScreenAvailabilityMojoCallback& callback) {
66 VLOG(2) << "PresentationServiceImpl::GetScreenAvailability"; 68 VLOG(2) << "GetScreenAvailability";
67 if (!delegate_) 69 if (!delegate_)
68 return; 70 return;
69 71
70 const std::string& presentation_url_str = !presentation_url.is_null() ? 72 const std::string& presentation_url_str = !presentation_url.is_null() ?
71 presentation_url.get() : default_presentation_url_; 73 presentation_url.get() : default_presentation_url_;
72 74
73 // GetScreenAvailability() is called with no URL and there is no default 75 // GetScreenAvailability() is called with no URL and there is no default
74 // Presentation URL. 76 // Presentation URL.
mark a. foltz 2015/03/11 01:08:18 In this case we will be falling back on "1-UA" mod
imcheng 2015/03/12 20:03:41 Ok. In that case, we can pass an empty string to P
75 if (presentation_url_str.empty()) 77 if (presentation_url_str.empty()) {
78 VLOG(1) << "No valid presentation URL.";
76 return; 79 return;
80 }
77 81
78 auto it = availability_contexts_.find(presentation_url_str); 82 auto it = availability_contexts_.find(presentation_url_str);
79 if (it == availability_contexts_.end()) { 83 if (it == availability_contexts_.end()) {
80 linked_ptr<ScreenAvailabilityContext> context( 84 linked_ptr<ScreenAvailabilityContext> context(
81 new ScreenAvailabilityContext(presentation_url_str)); 85 new ScreenAvailabilityContext(presentation_url_str));
82 86
83 if (!delegate_->AddScreenAvailabilityListener( 87 if (!delegate_->AddScreenAvailabilityListener(
84 render_frame_host_->GetProcess()->GetID(), 88 render_frame_host_->GetProcess()->GetID(),
85 render_frame_host_->GetRoutingID(), 89 render_frame_host_->GetRoutingID(),
86 context.get())) { 90 context.get())) {
87 VLOG(1) << "AddScreenAvailabilityListener failed. Ignoring request."; 91 VLOG(1) << "AddScreenAvailabilityListener failed. Ignoring request.";
88 return; 92 return;
89 } 93 }
90 94
91 it = availability_contexts_.insert( 95 it = availability_contexts_.insert(
92 std::make_pair(presentation_url_str, context)).first; 96 std::make_pair(context->GetPresentationUrl(), context)).first;
93 } 97 }
94 98
95 it->second->CallbackReceived(callback); 99 it->second->CallbackReceived(callback);
96 } 100 }
97 101
98 void PresentationServiceImpl::OnScreenAvailabilityListenerRemoved() { 102 void PresentationServiceImpl::OnScreenAvailabilityListenerRemoved() {
99 NOTIMPLEMENTED(); 103 VLOG(2) << "OnScreenAvailabilityListenerRemoved";
104 if (!delegate_)
mark a. foltz 2015/03/11 23:55:57 When could this be null? Can we enforce a non-nul
imcheng 2015/03/12 20:03:41 We should have these null checks because we don't
105 return;
106
107 auto it = availability_contexts_.find(default_presentation_url_);
108 if (it == availability_contexts_.end())
109 return;
110
111 delegate_->RemoveScreenAvailabilityListener(
112 render_frame_host_->GetProcess()->GetID(),
113 render_frame_host_->GetRoutingID(),
114 it->second.get());
115 availability_contexts_.erase(it);
100 } 116 }
101 117
102 void PresentationServiceImpl::StartSession( 118 void PresentationServiceImpl::StartSession(
103 const mojo::String& presentation_url, 119 const mojo::String& presentation_url,
104 const mojo::String& presentation_id, 120 const mojo::String& presentation_id,
105 const NewSessionMojoCallback& callback) { 121 const NewSessionMojoCallback& callback) {
106 NOTIMPLEMENTED(); 122 VLOG(2) << "StartSession";
123 if (start_or_join_session_cb_.get())
124 return;
125
126 delegate_->StartSession(
127 render_frame_host_->GetProcess()->GetID(),
128 render_frame_host_->GetRoutingID(),
129 presentation_url,
130 presentation_id,
131 base::Bind(&PresentationServiceImpl::OnStartOrJoinSessionSucceeded,
132 weak_factory_.GetWeakPtr()),
133 base::Bind(&PresentationServiceImpl::OnStartOrJoinSessionError,
134 weak_factory_.GetWeakPtr()));
135 start_or_join_session_cb_.reset(new NewSessionMojoCallback(callback));
107 } 136 }
108 137
109 void PresentationServiceImpl::JoinSession( 138 void PresentationServiceImpl::JoinSession(
110 const mojo::String& presentation_url, 139 const mojo::String& presentation_url,
111 const mojo::String& presentation_id, 140 const mojo::String& presentation_id,
112 const NewSessionMojoCallback& callback) { 141 const NewSessionMojoCallback& callback) {
113 NOTIMPLEMENTED(); 142 VLOG(2) << "JoinSession";
143 if (start_or_join_session_cb_.get())
144 return;
145
146 delegate_->JoinSession(
147 render_frame_host_->GetProcess()->GetID(),
148 render_frame_host_->GetRoutingID(),
149 presentation_url,
150 presentation_id,
151 base::Bind(&PresentationServiceImpl::OnStartOrJoinSessionSucceeded,
152 weak_factory_.GetWeakPtr()),
153 base::Bind(&PresentationServiceImpl::OnStartOrJoinSessionError,
154 weak_factory_.GetWeakPtr()));
155 start_or_join_session_cb_.reset(new NewSessionMojoCallback(callback));
mark a. foltz 2015/03/11 23:55:57 All the two bound callbacks above do is invoke the
imcheng 2015/03/12 20:03:41 The problem is that embedders do not have access t
156 }
157
158 void PresentationServiceImpl::OnStartOrJoinSessionSucceeded(
159 const PresentationSessionInfo& session_info) {
160 CHECK(start_or_join_session_cb_.get());
161 start_or_join_session_cb_->Run(
162 presentation::PresentationSessionInfo::From(session_info),
163 presentation::PresentationErrorPtr());
164 start_or_join_session_cb_.reset();
165 }
166
167 void PresentationServiceImpl::OnStartOrJoinSessionError(
168 const PresentationError& error) {
169 CHECK(start_or_join_session_cb_.get());
170 start_or_join_session_cb_->Run(
171 presentation::PresentationSessionInfoPtr(),
172 presentation::PresentationError::From(error));
173 start_or_join_session_cb_.reset();
174 }
175
mark a. foltz 2015/03/11 23:55:57 extra newline
imcheng 2015/03/12 20:03:41 Done.
176
177 void PresentationServiceImpl::DoSetDefaultPresentationUrl(
178 const std::string& presentation_url) {
179 delegate_->SetDefaultPresentationUrl(
180 render_frame_host_->GetProcess()->GetID(),
181 render_frame_host_->GetRoutingID(),
182 presentation_url);
183 default_presentation_url_ = presentation_url;
mark a. foltz 2015/03/11 23:55:57 Does this class also need to know the default_pres
imcheng 2015/03/12 20:03:41 Yes, this class needs to know the DPU. Both this c
184 }
185
186 void PresentationServiceImpl::SetDefaultPresentationUrl(
187 const mojo::String& presentation_url) {
188 VLOG(2) << "SetDefaultPresentationUrl";
189 if (!delegate_)
190 return;
191
192 std::string old_default_url = default_presentation_url_;
193 std::string new_default_url = presentation_url;
194
195 if (old_default_url == new_default_url)
196 return;
197
198 if (old_default_url.empty()) {
199 DoSetDefaultPresentationUrl(new_default_url);
200 return;
201 }
202
203 auto old_it = availability_contexts_.find(old_default_url);
204 // Haven't started listening yet.
205 if (old_it == availability_contexts_.end()) {
206 DoSetDefaultPresentationUrl(new_default_url);
207 return;
208 }
209
210 // Have already started listening. Create a listener for the new URL and
211 // transfer the callback from the old listener, if any.
212 // This is done so that a listener added before default URL is changed
213 // will continue to work.
mark a. foltz 2015/03/11 23:55:57 Are we sure that the callback that registered for
imcheng 2015/03/12 20:03:41 That would depend on the blink implementation. I b
214 ScreenAvailabilityMojoCallback* old_callback = old_it->second->callback();
215 if (!new_default_url.empty()) {
216 auto new_it = availability_contexts_.find(new_default_url);
217 if (new_it == availability_contexts_.end()) {
218 linked_ptr<ScreenAvailabilityContext> context(
219 new ScreenAvailabilityContext(new_default_url));
220 if (!delegate_->AddScreenAvailabilityListener(
221 render_frame_host_->GetProcess()->GetID(),
222 render_frame_host_->GetRoutingID(),
223 context.get())) {
224 VLOG(1) << "AddScreenAvailabilityListener failed. Ignoring request.";
225 return;
226 }
227 new_it = availability_contexts_.insert(
228 std::make_pair(context->GetPresentationUrl(), context)).first;
229 }
230 if (old_callback)
231 new_it->second->CallbackReceived(*old_callback);
232 }
233
234 // Remove listener for old default presentation URL.
235 delegate_->RemoveScreenAvailabilityListener(
236 render_frame_host_->GetProcess()->GetID(),
237 render_frame_host_->GetRoutingID(),
238 old_it->second.get());
239 availability_contexts_.erase(old_it);
240 DoSetDefaultPresentationUrl(new_default_url);
114 } 241 }
115 242
116 void PresentationServiceImpl::DidNavigateAnyFrame( 243 void PresentationServiceImpl::DidNavigateAnyFrame(
117 content::RenderFrameHost* render_frame_host, 244 content::RenderFrameHost* render_frame_host,
118 const content::LoadCommittedDetails& details, 245 const content::LoadCommittedDetails& details,
119 const content::FrameNavigateParams& params) { 246 const content::FrameNavigateParams& params) {
120 VLOG(2) << "PresentationServiceImpl::DidNavigateAnyFrame"; 247 VLOG(2) << "PresentationServiceImpl::DidNavigateAnyFrame";
121 if (render_frame_host_ != render_frame_host) 248 if (render_frame_host_ != render_frame_host)
122 return; 249 return;
123 250
124 std::string prev_url_host = details.previous_url.host(); 251 std::string prev_url_host = details.previous_url.host();
125 std::string curr_url_host = params.url.host(); 252 std::string curr_url_host = params.url.host();
126 253
127 // If a frame navigation is in-page (e.g. navigating to a fragment in 254 // If a frame navigation is in-page (e.g. navigating to a fragment in
128 // same page) then we do not unregister listeners. 255 // same page) then we do not unregister listeners.
129 bool in_page_navigation = details.is_in_page || 256 bool in_page_navigation = details.is_in_page ||
130 details.type == content::NAVIGATION_TYPE_IN_PAGE; 257 details.type == content::NAVIGATION_TYPE_IN_PAGE;
131 258
132 VLOG(2) << "DidNavigateAnyFrame: " 259 VLOG(2) << "DidNavigateAnyFrame: "
133 << "prev host: " << prev_url_host << ", curr host: " << curr_url_host 260 << "prev host: " << prev_url_host << ", curr host: " << curr_url_host
134 << ", in_page_navigation: " << in_page_navigation; 261 << ", in_page_navigation: " << in_page_navigation;
135 262
136 if (in_page_navigation) 263 if (in_page_navigation)
137 return; 264 return;
138 265
139 // Unregister all sources if the frame actually navigated. 266 // Reset if the frame actually navigated.
140 RemoveAllListeners(); 267 Reset();
141 } 268 }
142 269
143 void PresentationServiceImpl::RenderFrameDeleted( 270 void PresentationServiceImpl::RenderFrameDeleted(
144 content::RenderFrameHost* render_frame_host) { 271 content::RenderFrameHost* render_frame_host) {
145 VLOG(2) << "PresentationServiceImpl::RenderFrameDeleted"; 272 VLOG(2) << "PresentationServiceImpl::RenderFrameDeleted";
146 if (render_frame_host_ != render_frame_host) 273 if (render_frame_host_ != render_frame_host)
147 return; 274 return;
148 275
149 // RenderFrameDeleted means this object is getting deleted soon. 276 // RenderFrameDeleted means this object is getting deleted soon.
150 RemoveAllListeners(); 277 Reset();
151 } 278 }
152 279
153 void PresentationServiceImpl::RemoveAllListeners() { 280 void PresentationServiceImpl::Reset() {
154 VLOG(2) << "PresentationServiceImpl::RemoveAllListeners"; 281 VLOG(2) << "PresentationServiceImpl::Reset";
155 if (!delegate_) 282 if (!delegate_)
156 return; 283 return;
157 284
285 // Unregister all sources.
158 delegate_->RemoveAllScreenAvailabilityListeners( 286 delegate_->RemoveAllScreenAvailabilityListeners(
159 render_frame_host_->GetProcess()->GetID(), 287 render_frame_host_->GetProcess()->GetID(),
160 render_frame_host_->GetRoutingID()); 288 render_frame_host_->GetRoutingID());
289 availability_contexts_.clear();
161 290
162 availability_contexts_.clear(); 291 // Clear default presentation URL.
292 DoSetDefaultPresentationUrl(std::string());
293
294 start_or_join_session_cb_.reset();
163 } 295 }
164 296
165 void PresentationServiceImpl::OnDelegateDestroyed() { 297 void PresentationServiceImpl::OnDelegateDestroyed() {
166 VLOG(2) << "PresentationServiceImpl::OnDelegateDestroyed"; 298 VLOG(2) << "PresentationServiceImpl::OnDelegateDestroyed";
167 delegate_ = nullptr; 299 delegate_ = nullptr;
300 start_or_join_session_cb_.reset();
168 } 301 }
169 302
170 PresentationServiceImpl::ScreenAvailabilityContext::ScreenAvailabilityContext( 303 PresentationServiceImpl::ScreenAvailabilityContext::ScreenAvailabilityContext(
171 const std::string& presentation_url) 304 const std::string& presentation_url)
172 : presentation_url_(presentation_url) { 305 : presentation_url_(presentation_url) {
173 } 306 }
174 307
175 PresentationServiceImpl::ScreenAvailabilityContext:: 308 PresentationServiceImpl::ScreenAvailabilityContext::
176 ~ScreenAvailabilityContext() { 309 ~ScreenAvailabilityContext() {
177 } 310 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 } else { 342 } else {
210 // Invoke callback and erase it. 343 // Invoke callback and erase it.
211 // There shouldn't be any result stored in this scenario. 344 // There shouldn't be any result stored in this scenario.
212 DCHECK(!available_ptr_); 345 DCHECK(!available_ptr_);
213 callback_ptr_->Run(available); 346 callback_ptr_->Run(available);
214 Reset(); 347 Reset();
215 } 348 }
216 } 349 }
217 350
218 } // namespace content 351 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698