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

Side by Side Diff: chromecast/browser/cast_media_blocker.cc

Issue 2456783003: Let CastMediaBlocker observe MediaSession instead of WebContents [FOR TRY] (Closed)
Patch Set: fixed Linux build Created 4 years, 1 month 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 "chromecast/browser/cast_media_blocker.h" 5 #include "chromecast/browser/cast_media_blocker.h"
6 6
7 #include "base/callback.h"
8 #include "base/threading/thread_checker.h" 7 #include "base/threading/thread_checker.h"
8 #include "content/public/browser/media_session.h"
9 #include "content/public/browser/web_contents.h" 9 #include "content/public/browser/web_contents.h"
10 10
11 namespace chromecast { 11 namespace chromecast {
12 namespace shell { 12 namespace shell {
13 13
14 CastMediaBlocker::CastMediaBlocker(content::WebContents* web_contents) 14 CastMediaBlocker::CastMediaBlocker(content::MediaSession* media_session)
15 : CastMediaBlocker::CastMediaBlocker( 15 : content::MediaSessionObserver(media_session),
16 web_contents,
17 base::Bind(&CastMediaBlocker::Suspend, base::Unretained(this)),
18 base::Bind(&CastMediaBlocker::Resume, base::Unretained(this))) {}
19
20 CastMediaBlocker::CastMediaBlocker(content::WebContents* web_contents,
21 const base::Closure& suspend_cb,
22 const base::Closure& resume_cb)
23 : content::WebContentsObserver(web_contents),
24 suspend_cb_(suspend_cb),
25 resume_cb_(resume_cb),
26 blocked_(false), 16 blocked_(false),
27 paused_by_user_(true), 17 paused_by_user_(true),
28 suspended_(true), 18 suspended_(true),
29 controllable_(false) {} 19 controllable_(false) {}
30 20
31 CastMediaBlocker::~CastMediaBlocker() {} 21 CastMediaBlocker::~CastMediaBlocker() {}
32 22
33 void CastMediaBlocker::BlockMediaLoading(bool blocked) { 23 void CastMediaBlocker::BlockMediaLoading(bool blocked) {
34 if (blocked_ == blocked) 24 if (blocked_ == blocked)
35 return; 25 return;
36 26
37 blocked_ = blocked; 27 blocked_ = blocked;
38 UpdateMediaBlockedState(); 28 UpdateMediaBlockedState();
39 29
40 LOG(INFO) << __FUNCTION__ << " blocked=" << blocked_ 30 LOG(INFO) << __FUNCTION__ << " blocked=" << blocked_
41 << " suspended=" << suspended_ << " controllable=" << controllable_ 31 << " suspended=" << suspended_ << " controllable=" << controllable_
42 << " paused_by_user=" << paused_by_user_; 32 << " paused_by_user=" << paused_by_user_;
43 33
44 // If blocking media, suspend if possible. 34 // If blocking media, suspend if possible.
45 if (blocked_) { 35 if (blocked_) {
46 if (!suspended_ && controllable_) { 36 if (!suspended_ && controllable_) {
47 suspend_cb_.Run(); 37 Suspend();
48 } 38 }
49 return; 39 return;
50 } 40 }
51 41
52 // If unblocking media, resume if media was not paused by user. 42 // If unblocking media, resume if media was not paused by user.
53 if (!paused_by_user_ && suspended_ && controllable_) { 43 if (!paused_by_user_ && suspended_ && controllable_) {
54 paused_by_user_ = true; 44 paused_by_user_ = true;
55 resume_cb_.Run(); 45 Resume();
56 } 46 }
57 } 47 }
58 48
59 void CastMediaBlocker::MediaSessionStateChanged(bool is_controllable, 49 void CastMediaBlocker::MediaSessionStateChanged(bool is_controllable,
60 bool is_suspended) { 50 bool is_suspended) {
61 LOG(INFO) << __FUNCTION__ << " blocked=" << blocked_ 51 LOG(INFO) << __FUNCTION__ << " blocked=" << blocked_
62 << " is_suspended=" << is_suspended 52 << " is_suspended=" << is_suspended
63 << " is_controllable=" << is_controllable 53 << " is_controllable=" << is_controllable
64 << " paused_by_user=" << paused_by_user_; 54 << " paused_by_user=" << paused_by_user_;
65 55
66 // Process controllability first. 56 // Process controllability first.
67 if (controllable_ != is_controllable) { 57 if (controllable_ != is_controllable) {
68 controllable_ = is_controllable; 58 controllable_ = is_controllable;
69 59
70 // If not blocked, and we regain control and the media wasn't paused when 60 // If not blocked, and we regain control and the media wasn't paused when
71 // blocked, resume media if suspended. 61 // blocked, resume media if suspended.
72 if (!blocked_ && !paused_by_user_ && is_suspended && controllable_) { 62 if (!blocked_ && !paused_by_user_ && is_suspended && controllable_) {
73 paused_by_user_ = true; 63 paused_by_user_ = true;
74 resume_cb_.Run(); 64 Resume();
75 } 65 }
76 66
77 // Suspend if blocked and the session becomes controllable. 67 // Suspend if blocked and the session becomes controllable.
78 if (blocked_ && !is_suspended && controllable_) { 68 if (blocked_ && !is_suspended && controllable_) {
79 // Only suspend if suspended_ doesn't change. Otherwise, this will be 69 // Only suspend if suspended_ doesn't change. Otherwise, this will be
80 // handled in the suspended changed block. 70 // handled in the suspended changed block.
81 if (suspended_ == is_suspended) 71 if (suspended_ == is_suspended)
82 suspend_cb_.Run(); 72 Suspend();
83 } 73 }
84 } 74 }
85 75
86 // Process suspended state next. 76 // Process suspended state next.
87 if (suspended_ != is_suspended) { 77 if (suspended_ != is_suspended) {
88 suspended_ = is_suspended; 78 suspended_ = is_suspended;
89 // If blocking, suspend media whenever possible. 79 // If blocking, suspend media whenever possible.
90 if (blocked_ && !suspended_) { 80 if (blocked_ && !suspended_) {
91 // If media was resumed when blocked, the user tried to play music. 81 // If media was resumed when blocked, the user tried to play music.
92 paused_by_user_ = false; 82 paused_by_user_ = false;
93 if (controllable_) 83 if (controllable_)
94 suspend_cb_.Run(); 84 Suspend();
95 } 85 }
96 86
97 // If not blocking, cache the user's play intent. 87 // If not blocking, cache the user's play intent.
98 if (!blocked_) 88 if (!blocked_)
99 paused_by_user_ = suspended_; 89 paused_by_user_ = suspended_;
100 } 90 }
101 } 91 }
102 92
103 void CastMediaBlocker::Suspend() { 93 void CastMediaBlocker::Suspend() {
104 if (!web_contents()) 94 if (!media_session())
105 return; 95 return;
106 96
107 LOG(INFO) << "Suspending media session."; 97 LOG(INFO) << "Suspending media session.";
108 web_contents()->SuspendMediaSession(); 98 media_session()->Suspend(content::MediaSession::SuspendType::SYSTEM);
109 } 99 }
110 100
111 void CastMediaBlocker::Resume() { 101 void CastMediaBlocker::Resume() {
112 if (!web_contents()) 102 if (!media_session())
113 return; 103 return;
114 104
115 LOG(INFO) << "Resuming media session."; 105 LOG(INFO) << "Resuming media session.";
116 web_contents()->ResumeMediaSession(); 106 media_session()->Resume(content::MediaSession::SuspendType::SYSTEM);
117 } 107 }
118 108
119 } // namespace shell 109 } // namespace shell
120 } // namespace chromecast 110 } // namespace chromecast
OLDNEW
« no previous file with comments | « chromecast/browser/cast_media_blocker.h ('k') | chromecast/browser/test/cast_media_blocker_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698