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

Side by Side Diff: content/browser/media/session/media_session_service_impl.cc

Issue 2715723002: Fix a crash in MediaSessionServiceImpl due to life time issue (Closed)
Patch Set: addressed avayvod@'s comments Created 3 years, 10 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/media/session/media_session_service_impl.h" 5 #include "content/browser/media/session/media_session_service_impl.h"
6 6
7 #include "content/browser/media/session/media_metadata_sanitizer.h" 7 #include "content/browser/media/session/media_metadata_sanitizer.h"
8 #include "content/browser/media/session/media_session_impl.h" 8 #include "content/browser/media/session/media_session_impl.h"
9 #include "content/browser/web_contents/web_contents_impl.h" 9 #include "content/browser/web_contents/web_contents_impl.h"
10 #include "content/public/browser/browser_thread.h" 10 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/render_frame_host.h"
11 #include "content/public/browser/web_contents.h" 12 #include "content/public/browser/web_contents.h"
12 13
13 namespace content { 14 namespace content {
14 15
15 MediaSessionServiceImpl::MediaSessionServiceImpl( 16 MediaSessionServiceImpl::MediaSessionServiceImpl(
16 RenderFrameHost* render_frame_host) 17 RenderFrameHost* render_frame_host)
17 : render_frame_host_(render_frame_host), 18 : render_frame_process_id_(render_frame_host->GetProcess()->GetID()),
19 render_frame_routing_id_(render_frame_host->GetRoutingID()),
18 playback_state_(blink::mojom::MediaSessionPlaybackState::NONE) { 20 playback_state_(blink::mojom::MediaSessionPlaybackState::NONE) {
19 MediaSessionImpl* session = GetMediaSession(); 21 MediaSessionImpl* session = GetMediaSession();
20 if (session) 22 if (session)
21 session->OnServiceCreated(this); 23 session->OnServiceCreated(this);
22 } 24 }
23 25
24 MediaSessionServiceImpl::~MediaSessionServiceImpl() { 26 MediaSessionServiceImpl::~MediaSessionServiceImpl() {
25 MediaSessionImpl* session = GetMediaSession(); 27 MediaSessionImpl* session = GetMediaSession();
26 if (session) 28 if (session)
27 session->OnServiceDestroyed(this); 29 session->OnServiceDestroyed(this);
28 } 30 }
29 31
30 // static 32 // static
31 void MediaSessionServiceImpl::Create( 33 void MediaSessionServiceImpl::Create(
32 RenderFrameHost* render_frame_host, 34 RenderFrameHost* render_frame_host,
33 blink::mojom::MediaSessionServiceRequest request) { 35 blink::mojom::MediaSessionServiceRequest request) {
34 MediaSessionServiceImpl* impl = 36 MediaSessionServiceImpl* impl =
35 new MediaSessionServiceImpl(render_frame_host); 37 new MediaSessionServiceImpl(render_frame_host);
36 impl->Bind(std::move(request)); 38 impl->Bind(std::move(request));
37 } 39 }
38 40
41 RenderFrameHost* MediaSessionServiceImpl::GetRenderFrameHost() {
42 return content::RenderFrameHost::FromID(render_frame_process_id_,
Charlie Reis 2017/02/23 20:36:41 nit: Drop "content::", since we're already in the
Zhiqiang Zhang (Slow) 2017/02/23 21:04:05 Done.
43 render_frame_routing_id_);
44 }
45
39 void MediaSessionServiceImpl::SetClient( 46 void MediaSessionServiceImpl::SetClient(
40 blink::mojom::MediaSessionClientPtr client) { 47 blink::mojom::MediaSessionClientPtr client) {
41 client_ = std::move(client); 48 client_ = std::move(client);
42 } 49 }
43 50
44 void MediaSessionServiceImpl::SetPlaybackState( 51 void MediaSessionServiceImpl::SetPlaybackState(
45 blink::mojom::MediaSessionPlaybackState state) { 52 blink::mojom::MediaSessionPlaybackState state) {
46 playback_state_ = state; 53 playback_state_ = state;
47 MediaSessionImpl* session = GetMediaSession(); 54 MediaSessionImpl* session = GetMediaSession();
48 if (session) 55 if (session)
49 session->OnMediaSessionPlaybackStateChanged(this); 56 session->OnMediaSessionPlaybackStateChanged(this);
50 } 57 }
51 58
52 void MediaSessionServiceImpl::SetMetadata( 59 void MediaSessionServiceImpl::SetMetadata(
53 const base::Optional<content::MediaMetadata>& metadata) { 60 const base::Optional<content::MediaMetadata>& metadata) {
54 // When receiving a MediaMetadata, the browser process can't trust that it is 61 // When receiving a MediaMetadata, the browser process can't trust that it is
55 // coming from a known and secure source. It must be processed accordingly. 62 // coming from a known and secure source. It must be processed accordingly.
56 if (metadata.has_value() && 63 if (metadata.has_value() &&
57 !MediaMetadataSanitizer::CheckSanity(metadata.value())) { 64 !MediaMetadataSanitizer::CheckSanity(metadata.value())) {
58 render_frame_host_->GetProcess()->ShutdownForBadMessage( 65 RenderFrameHost* rfh = GetRenderFrameHost();
59 RenderProcessHost::CrashReportMode::GENERATE_CRASH_DUMP); 66 if (rfh) {
67 rfh->GetProcess()->ShutdownForBadMessage(
68 RenderProcessHost::CrashReportMode::GENERATE_CRASH_DUMP);
69 }
60 return; 70 return;
61 } 71 }
62 metadata_ = metadata; 72 metadata_ = metadata;
63 73
64 MediaSessionImpl* session = GetMediaSession(); 74 MediaSessionImpl* session = GetMediaSession();
65 if (session) 75 if (session)
66 session->OnMediaSessionMetadataChanged(this); 76 session->OnMediaSessionMetadataChanged(this);
67 } 77 }
68 78
69 void MediaSessionServiceImpl::EnableAction( 79 void MediaSessionServiceImpl::EnableAction(
70 blink::mojom::MediaSessionAction action) { 80 blink::mojom::MediaSessionAction action) {
71 actions_.insert(action); 81 actions_.insert(action);
72 MediaSessionImpl* session = GetMediaSession(); 82 MediaSessionImpl* session = GetMediaSession();
73 if (session) 83 if (session)
74 session->OnMediaSessionActionsChanged(this); 84 session->OnMediaSessionActionsChanged(this);
75 } 85 }
76 86
77 void MediaSessionServiceImpl::DisableAction( 87 void MediaSessionServiceImpl::DisableAction(
78 blink::mojom::MediaSessionAction action) { 88 blink::mojom::MediaSessionAction action) {
79 actions_.erase(action); 89 actions_.erase(action);
80 MediaSessionImpl* session = GetMediaSession(); 90 MediaSessionImpl* session = GetMediaSession();
81 if (session) 91 if (session)
82 session->OnMediaSessionActionsChanged(this); 92 session->OnMediaSessionActionsChanged(this);
83 } 93 }
84 94
85 MediaSessionImpl* MediaSessionServiceImpl::GetMediaSession() { 95 MediaSessionImpl* MediaSessionServiceImpl::GetMediaSession() {
86 WebContentsImpl* contents = static_cast<WebContentsImpl*>( 96 RenderFrameHost* rfh = GetRenderFrameHost();
87 WebContentsImpl::FromRenderFrameHost(render_frame_host_)); 97 if (!rfh)
98 return nullptr;
99
100 WebContentsImpl* contents =
101 static_cast<WebContentsImpl*>(WebContentsImpl::FromRenderFrameHost(rfh));
88 if (!contents) 102 if (!contents)
89 return nullptr; 103 return nullptr;
104
90 return MediaSessionImpl::Get(contents); 105 return MediaSessionImpl::Get(contents);
91 } 106 }
92 107
93 void MediaSessionServiceImpl::Bind( 108 void MediaSessionServiceImpl::Bind(
94 blink::mojom::MediaSessionServiceRequest request) { 109 blink::mojom::MediaSessionServiceRequest request) {
95 binding_.reset(new mojo::Binding<blink::mojom::MediaSessionService>( 110 binding_.reset(new mojo::Binding<blink::mojom::MediaSessionService>(
96 this, std::move(request))); 111 this, std::move(request)));
97 } 112 }
98 113
99 } // namespace content 114 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698