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

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

Issue 2545523008: [Media Router] Crasher fix for PresentationServiceDelegateImpl. (Closed)
Patch Set: Created 4 years 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "chrome/browser/media/router/presentation_service_delegate_impl.h" 5 #include "chrome/browser/media/router/presentation_service_delegate_impl.h"
6 6
7 #include <string> 7 #include <string>
8 #include <unordered_map> 8 #include <unordered_map>
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/containers/small_map.h" 12 #include "base/containers/small_map.h"
13 #include "base/guid.h" 13 #include "base/guid.h"
14 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
15 #include "base/stl_util.h"
15 #include "base/strings/string_util.h" 16 #include "base/strings/string_util.h"
16 #include "chrome/browser/media/router/create_presentation_connection_request.h" 17 #include "chrome/browser/media/router/create_presentation_connection_request.h"
17 #include "chrome/browser/media/router/media_route.h" 18 #include "chrome/browser/media/router/media_route.h"
18 #include "chrome/browser/media/router/media_router.h" 19 #include "chrome/browser/media/router/media_router.h"
19 #include "chrome/browser/media/router/media_router_dialog_controller.h" 20 #include "chrome/browser/media/router/media_router_dialog_controller.h"
20 #include "chrome/browser/media/router/media_router_factory.h" 21 #include "chrome/browser/media/router/media_router_factory.h"
21 #include "chrome/browser/media/router/media_sink.h" 22 #include "chrome/browser/media/router/media_sink.h"
22 #include "chrome/browser/media/router/media_source_helper.h" 23 #include "chrome/browser/media/router/media_source_helper.h"
23 #include "chrome/browser/media/router/presentation_media_sinks_observer.h" 24 #include "chrome/browser/media/router/presentation_media_sinks_observer.h"
24 #include "chrome/browser/media/router/route_message.h" 25 #include "chrome/browser/media/router/route_message.h"
(...skipping 19 matching lines...) Expand all
44 45
45 // Returns the unique identifier for the supplied RenderFrameHost. 46 // Returns the unique identifier for the supplied RenderFrameHost.
46 RenderFrameHostId GetRenderFrameHostId(RenderFrameHost* render_frame_host) { 47 RenderFrameHostId GetRenderFrameHostId(RenderFrameHost* render_frame_host) {
47 int render_process_id = render_frame_host->GetProcess()->GetID(); 48 int render_process_id = render_frame_host->GetProcess()->GetID();
48 int render_frame_id = render_frame_host->GetRoutingID(); 49 int render_frame_id = render_frame_host->GetRoutingID();
49 return RenderFrameHostId(render_process_id, render_frame_id); 50 return RenderFrameHostId(render_process_id, render_frame_id);
50 } 51 }
51 52
52 // Gets the last committed URL for the render frame specified by 53 // Gets the last committed URL for the render frame specified by
53 // |render_frame_host_id|. 54 // |render_frame_host_id|.
54 GURL GetLastCommittedURLForFrame(RenderFrameHostId render_frame_host_id) { 55 GURL GetLastCommittedURLForFrame(RenderFrameHostId render_frame_host_id,
56 content::WebContents* web_contents) {
55 RenderFrameHost* render_frame_host = RenderFrameHost::FromID( 57 RenderFrameHost* render_frame_host = RenderFrameHost::FromID(
56 render_frame_host_id.first, render_frame_host_id.second); 58 render_frame_host_id.first, render_frame_host_id.second);
59 // crbug.com/663740: The RFH may not be associated with the frame tree; in
60 // that case, GetLastCommittedOrigin() may crash.
61 if (!render_frame_host ||
62 !base::ContainsValue(web_contents->GetAllFrames(), render_frame_host))
63 return GURL();
64
57 // TODO(crbug.com/632623): Use url::Origin in place of GURL for origins 65 // TODO(crbug.com/632623): Use url::Origin in place of GURL for origins
58 return render_frame_host 66 return render_frame_host->GetLastCommittedOrigin().GetURL();
59 ? render_frame_host->GetLastCommittedOrigin().GetURL()
60 : GURL();
61 } 67 }
62 68
63 // Observes messages originating from the MediaSink connected to a MediaRoute 69 // Observes messages originating from the MediaSink connected to a MediaRoute
64 // that represents a presentation. Converts the messages into 70 // that represents a presentation. Converts the messages into
65 // content::PresentationSessionMessages and dispatches them via the provided 71 // content::PresentationSessionMessages and dispatches them via the provided
66 // PresentationSessionMessageCallback. 72 // PresentationSessionMessageCallback.
67 class PresentationSessionMessagesObserver : public RouteMessageObserver { 73 class PresentationSessionMessagesObserver : public RouteMessageObserver {
68 public: 74 public:
69 // |message_cb|: The callback to invoke whenever messages are received. 75 // |message_cb|: The callback to invoke whenever messages are received.
70 // |route_id|: ID of MediaRoute to listen for messages. 76 // |route_id|: ID of MediaRoute to listen for messages.
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 std::unique_ptr<PresentationConnectionStateSubscription>> 169 std::unique_ptr<PresentationConnectionStateSubscription>>
164 connection_state_subscriptions_; 170 connection_state_subscriptions_;
165 std::unordered_map< 171 std::unordered_map<
166 MediaRoute::Id, 172 MediaRoute::Id,
167 std::unique_ptr<PresentationSessionMessagesObserver>> 173 std::unique_ptr<PresentationSessionMessagesObserver>>
168 session_messages_observers_; 174 session_messages_observers_;
169 175
170 RenderFrameHostId render_frame_host_id_; 176 RenderFrameHostId render_frame_host_id_;
171 177
172 // References to the owning WebContents, and the corresponding MediaRouter. 178 // References to the owning WebContents, and the corresponding MediaRouter.
173 const content::WebContents* web_contents_; 179 content::WebContents* web_contents_;
174 MediaRouter* router_; 180 MediaRouter* router_;
175 181
176 DelegateObserver* delegate_observer_; 182 DelegateObserver* delegate_observer_;
177 }; 183 };
178 184
179 PresentationFrame::PresentationFrame( 185 PresentationFrame::PresentationFrame(
180 const RenderFrameHostId& render_frame_host_id, 186 const RenderFrameHostId& render_frame_host_id,
181 content::WebContents* web_contents, 187 content::WebContents* web_contents,
182 MediaRouter* router) 188 MediaRouter* router)
183 : render_frame_host_id_(render_frame_host_id), 189 : render_frame_host_id_(render_frame_host_id),
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 224
219 bool PresentationFrame::SetScreenAvailabilityListener( 225 bool PresentationFrame::SetScreenAvailabilityListener(
220 content::PresentationScreenAvailabilityListener* listener) { 226 content::PresentationScreenAvailabilityListener* listener) {
221 MediaSource source(GetMediaSourceFromListener(listener)); 227 MediaSource source(GetMediaSourceFromListener(listener));
222 auto& sinks_observer = url_to_sinks_observer_[source.id()]; 228 auto& sinks_observer = url_to_sinks_observer_[source.id()];
223 if (sinks_observer && sinks_observer->listener() == listener) 229 if (sinks_observer && sinks_observer->listener() == listener)
224 return false; 230 return false;
225 231
226 sinks_observer.reset(new PresentationMediaSinksObserver( 232 sinks_observer.reset(new PresentationMediaSinksObserver(
227 router_, listener, source, 233 router_, listener, source,
228 GetLastCommittedURLForFrame(render_frame_host_id_).GetOrigin())); 234 GetLastCommittedURLForFrame(render_frame_host_id_, web_contents_)
235 .GetOrigin()));
229 236
230 if (!sinks_observer->Init()) { 237 if (!sinks_observer->Init()) {
231 url_to_sinks_observer_.erase(source.id()); 238 url_to_sinks_observer_.erase(source.id());
232 listener->OnScreenAvailabilityNotSupported(); 239 listener->OnScreenAvailabilityNotSupported();
233 return false; 240 return false;
234 } 241 }
235 242
236 return true; 243 return true;
237 } 244 }
238 245
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after
542 const RenderFrameHostId& render_frame_host_id, 549 const RenderFrameHostId& render_frame_host_id,
543 const GURL& default_presentation_url, 550 const GURL& default_presentation_url,
544 const content::PresentationSessionStartedCallback& callback) { 551 const content::PresentationSessionStartedCallback& callback) {
545 if (!IsMainFrame(render_frame_host_id)) 552 if (!IsMainFrame(render_frame_host_id))
546 return; 553 return;
547 554
548 if (default_presentation_url.is_empty()) { 555 if (default_presentation_url.is_empty()) {
549 ClearDefaultPresentationRequest(); 556 ClearDefaultPresentationRequest();
550 } else { 557 } else {
551 DCHECK(!callback.is_null()); 558 DCHECK(!callback.is_null());
552 GURL frame_url(GetLastCommittedURLForFrame(render_frame_host_id)); 559 GURL frame_url(
560 GetLastCommittedURLForFrame(render_frame_host_id, web_contents_));
553 PresentationRequest request(render_frame_host_id, 561 PresentationRequest request(render_frame_host_id,
554 std::vector<GURL>({default_presentation_url}), 562 std::vector<GURL>({default_presentation_url}),
555 frame_url); 563 frame_url);
556 default_presentation_started_callback_ = callback; 564 default_presentation_started_callback_ = callback;
557 SetDefaultPresentationRequest(request); 565 SetDefaultPresentationRequest(request);
558 } 566 }
559 } 567 }
560 568
561 void PresentationFrameManager::AddDelegateObserver( 569 void PresentationFrameManager::AddDelegateObserver(
562 const RenderFrameHostId& render_frame_host_id, 570 const RenderFrameHostId& render_frame_host_id,
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
788 !IsValidPresentationUrl(presentation_url)) { 796 !IsValidPresentationUrl(presentation_url)) {
789 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, 797 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN,
790 "Invalid presentation arguments.")); 798 "Invalid presentation arguments."));
791 return; 799 return;
792 } 800 }
793 801
794 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); 802 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id);
795 std::unique_ptr<CreatePresentationConnectionRequest> request( 803 std::unique_ptr<CreatePresentationConnectionRequest> request(
796 new CreatePresentationConnectionRequest( 804 new CreatePresentationConnectionRequest(
797 render_frame_host_id, presentation_url, 805 render_frame_host_id, presentation_url,
798 GetLastCommittedURLForFrame(render_frame_host_id), 806 GetLastCommittedURLForFrame(render_frame_host_id, web_contents_),
799 base::Bind(&PresentationServiceDelegateImpl::OnStartSessionSucceeded, 807 base::Bind(&PresentationServiceDelegateImpl::OnStartSessionSucceeded,
800 weak_factory_.GetWeakPtr(), render_process_id, 808 weak_factory_.GetWeakPtr(), render_process_id,
801 render_frame_id, success_cb), 809 render_frame_id, success_cb),
802 error_cb)); 810 error_cb));
803 MediaRouterDialogController* controller = 811 MediaRouterDialogController* controller =
804 MediaRouterDialogController::GetOrCreateForWebContents(web_contents_); 812 MediaRouterDialogController::GetOrCreateForWebContents(web_contents_);
805 if (!controller->ShowMediaRouterDialogForPresentation(std::move(request))) { 813 if (!controller->ShowMediaRouterDialogForPresentation(std::move(request))) {
806 LOG(ERROR) << "Media router dialog already exists. Ignoring StartSession."; 814 LOG(ERROR) << "Media router dialog already exists. Ignoring StartSession.";
807 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, 815 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN,
808 "Unable to create dialog.")); 816 "Unable to create dialog."));
(...skipping 18 matching lines...) Expand all
827 const GURL& presentation_url = presentation_urls[0]; 835 const GURL& presentation_url = presentation_urls[0];
828 bool incognito = web_contents_->GetBrowserContext()->IsOffTheRecord(); 836 bool incognito = web_contents_->GetBrowserContext()->IsOffTheRecord();
829 std::vector<MediaRouteResponseCallback> route_response_callbacks; 837 std::vector<MediaRouteResponseCallback> route_response_callbacks;
830 route_response_callbacks.push_back( 838 route_response_callbacks.push_back(
831 base::Bind(&PresentationServiceDelegateImpl::OnJoinRouteResponse, 839 base::Bind(&PresentationServiceDelegateImpl::OnJoinRouteResponse,
832 weak_factory_.GetWeakPtr(), render_process_id, render_frame_id, 840 weak_factory_.GetWeakPtr(), render_process_id, render_frame_id,
833 presentation_url, presentation_id, success_cb, error_cb)); 841 presentation_url, presentation_id, success_cb, error_cb));
834 router_->JoinRoute( 842 router_->JoinRoute(
835 MediaSourceForPresentationUrl(presentation_url).id(), presentation_id, 843 MediaSourceForPresentationUrl(presentation_url).id(), presentation_id,
836 GetLastCommittedURLForFrame( 844 GetLastCommittedURLForFrame(
837 RenderFrameHostId(render_process_id, render_frame_id)) 845 RenderFrameHostId(render_process_id, render_frame_id), web_contents_)
838 .GetOrigin(), 846 .GetOrigin(),
839 web_contents_, route_response_callbacks, base::TimeDelta(), incognito); 847 web_contents_, route_response_callbacks, base::TimeDelta(), incognito);
840 } 848 }
841 849
842 void PresentationServiceDelegateImpl::CloseConnection( 850 void PresentationServiceDelegateImpl::CloseConnection(
843 int render_process_id, 851 int render_process_id,
844 int render_frame_id, 852 int render_frame_id,
845 const std::string& presentation_id) { 853 const std::string& presentation_id) {
846 const RenderFrameHostId rfh_id(render_process_id, render_frame_id); 854 const RenderFrameHostId rfh_id(render_process_id, render_frame_id);
847 const MediaRoute::Id& route_id = 855 const MediaRoute::Id& route_id =
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
963 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest( 971 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest(
964 int render_process_id, 972 int render_process_id,
965 int render_frame_id, 973 int render_frame_id,
966 const MediaSource::Id& source_id) const { 974 const MediaSource::Id& source_id) const {
967 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); 975 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id);
968 return frame_manager_->HasScreenAvailabilityListenerForTest( 976 return frame_manager_->HasScreenAvailabilityListenerForTest(
969 render_frame_host_id, source_id); 977 render_frame_host_id, source_id);
970 } 978 }
971 979
972 } // namespace media_router 980 } // namespace media_router
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698