Chromium Code Reviews| Index: chromecast/browser/cast_media_blocker.cc |
| diff --git a/chromecast/browser/cast_media_blocker.cc b/chromecast/browser/cast_media_blocker.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8644c1a1ac1d3b2511f0366e136890b1f578dd86 |
| --- /dev/null |
| +++ b/chromecast/browser/cast_media_blocker.cc |
| @@ -0,0 +1,60 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chromecast/browser/cast_media_blocker.h" |
| + |
| +#include "base/threading/thread_checker.h" |
| +#include "content/public/browser/web_contents.h" |
| + |
| +namespace chromecast { |
| +namespace shell { |
| + |
| +CastMediaBlocker::CastMediaBlocker(content::WebContents* web_contents) |
| + : content::WebContentsObserver(web_contents), |
| + controllable_(false), |
| + suspended_(false), |
| + state_(UNBLOCKED) {} |
| + |
| +CastMediaBlocker::~CastMediaBlocker() {} |
| + |
| +void CastMediaBlocker::BlockMediaLoading(bool blocked) { |
| + if (blocked) { |
| + state_ = BLOCKED; |
| + } else { |
| + state_ = (state_ == BLOCKED ? UNBLOCKING : UNBLOCKED); |
|
halliwell
2016/09/13 23:59:47
Can state_ be UNBLOCKING when this is called? If
derekjchow1
2016/09/14 00:14:05
Good point. If we're unblocking, we should always
|
| + } |
| + |
| + UpdateMediaBlockedState(); |
| +} |
| + |
| +void CastMediaBlocker::UpdateMediaBlockedState() { |
| + if (!web_contents()) { |
| + return; |
| + } |
| + |
| + if (!controllable_) { |
| + return; |
| + } |
| + |
| + if (state_ == BLOCKED && !suspended_) { |
| + web_contents()->SuspendMediaSession(); |
| + } |
| + |
| + if (state_ == UNBLOCKING && suspended_) { |
| + web_contents()->ResumeMediaSession(); |
| + state_ = UNBLOCKED; |
| + } |
| +} |
| + |
| +void CastMediaBlocker::MediaSessionStateChanged( |
| + bool is_controllable, |
| + bool is_suspended, |
| + const base::Optional<content::MediaMetadata>& metadata) { |
| + controllable_ = is_controllable; |
| + suspended_ = is_suspended; |
| + UpdateMediaBlockedState(); |
| +} |
| + |
| +} // namespace shell |
| +} // namespace chromecast |