forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CastMediaBlocker and BrowserTest
CastMediaBlocker is a class which can prevent media from being played on a web page. BUG=NONE TEST=ChromecastShellMediaBlockingBrowserTest Review-Url: https://codereview.chromium.org/2330243002 Cr-Commit-Position: refs/heads/master@{#418897}
- Loading branch information
derekjchow
authored and
Commit bot
committed
Sep 15, 2016
1 parent
ca7c2f9
commit 502bcc9
Showing
6 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// 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 if (state_ == BLOCKED) { | ||
state_ = UNBLOCKING; | ||
} | ||
|
||
UpdateMediaBlockedState(); | ||
} | ||
|
||
void CastMediaBlocker::UpdateMediaBlockedState() { | ||
if (!web_contents()) { | ||
return; | ||
} | ||
|
||
if (!controllable_) { | ||
return; | ||
} | ||
|
||
if (state_ == BLOCKED && !suspended_) { | ||
web_contents()->SuspendMediaSession(); | ||
} else 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// 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. | ||
|
||
#ifndef CHROMECAST_BROWSER_CAST_MEDIA_BLOCKER_H_ | ||
#define CHROMECAST_BROWSER_CAST_MEDIA_BLOCKER_H_ | ||
|
||
#include "base/macros.h" | ||
#include "content/public/browser/web_contents_observer.h" | ||
|
||
namespace content { | ||
class WebContents; | ||
} // namespace content | ||
|
||
namespace chromecast { | ||
namespace shell { | ||
|
||
// This class implements a blocking mode for web applications and is used in | ||
// Chromecast internal code. | ||
class CastMediaBlocker : public content::WebContentsObserver { | ||
public: | ||
explicit CastMediaBlocker(content::WebContents* web_contents); | ||
~CastMediaBlocker() override; | ||
|
||
// Sets if the web contents is allowed to play media or not. If media is | ||
// unblocked, previously suspended elements should begin playing again. | ||
void BlockMediaLoading(bool blocked); | ||
|
||
protected: | ||
bool media_loading_blocked() const { return state_ == BLOCKED; } | ||
|
||
// Blocks or unblocks the render process from loading new media | ||
// according to |media_loading_blocked_|. | ||
virtual void UpdateMediaBlockedState(); | ||
|
||
private: | ||
enum BlockState { | ||
BLOCKED, // All media playback should be blocked. | ||
UNBLOCKING, // Media playback should be resumed at next oppurtunity. | ||
UNBLOCKED, // Media playback should not be blocked. | ||
}; | ||
|
||
// content::WebContentsObserver implementation: | ||
void MediaSessionStateChanged( | ||
bool is_controllable, | ||
bool is_suspended, | ||
const base::Optional<content::MediaMetadata>& metadata) override; | ||
|
||
// Whether or not media in the app can be controlled and if media is currently | ||
// suspended. These variables cache arguments from MediaSessionStateChanged(). | ||
bool controllable_; | ||
bool suspended_; | ||
|
||
BlockState state_; | ||
|
||
DISALLOW_COPY_AND_ASSIGN(CastMediaBlocker); | ||
}; | ||
|
||
} // namespace shell | ||
} // namespace chromecast | ||
|
||
#endif // CHROMECAST_BROWSER_CAST_MEDIA_BLOCKER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
chromecast/browser/test/chromecast_shell_media_blocking_browser_test.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// 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 "base/macros.h" | ||
#include "base/run_loop.h" | ||
#include "base/threading/platform_thread.h" | ||
#include "base/threading/thread_task_runner_handle.h" | ||
#include "chromecast/browser/test/chromecast_browser_test.h" | ||
#include "chromecast/browser/test/chromecast_browser_test_helper.h" | ||
#include "chromecast/chromecast_features.h" | ||
#include "content/public/browser/web_contents.h" | ||
#include "content/public/test/browser_test_utils.h" | ||
#include "media/base/test_data_util.h" | ||
#include "url/gurl.h" | ||
#include "url/url_constants.h" | ||
|
||
namespace chromecast { | ||
namespace shell { | ||
|
||
class ChromecastShellMediaBlockingBrowserTest : public ChromecastBrowserTest { | ||
public: | ||
ChromecastShellMediaBlockingBrowserTest() {} | ||
|
||
protected: | ||
void PlayMedia(const std::string& tag, const std::string& media_file) { | ||
base::StringPairs query_params; | ||
query_params.push_back(std::make_pair(tag, media_file)); | ||
|
||
std::string query = media::GetURLQueryString(query_params); | ||
GURL gurl = content::GetFileUrlWithQuery( | ||
media::GetTestDataFilePath("player.html"), query); | ||
|
||
web_contents_ = helper_->NavigateToURL(gurl); | ||
WaitForLoadStop(web_contents_); | ||
} | ||
|
||
void BlockAndTestPlayerState(const std::string& media_type, bool blocked) { | ||
helper_->BlockMediaLoading(blocked); | ||
|
||
// Changing states is not instant, but should be timely (< 0.5s). | ||
for (size_t i = 0; i < 5; i++) { | ||
base::RunLoop run_loop; | ||
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | ||
FROM_HERE, run_loop.QuitClosure(), | ||
base::TimeDelta::FromMilliseconds(50)); | ||
base::MessageLoop::ScopedNestableTaskAllower allow_nested( | ||
base::MessageLoop::current()); | ||
run_loop.Run(); | ||
|
||
const std::string command = | ||
"document.getElementsByTagName(\"" + media_type + "\")[0].paused"; | ||
const std::string js = | ||
"window.domAutomationController.send(" + command + ");"; | ||
|
||
bool paused; | ||
ASSERT_TRUE(ExecuteScriptAndExtractBool(web_contents_, js, &paused)); | ||
|
||
if (paused == blocked) { | ||
SUCCEED() << "Media element has been successfullly " | ||
<< (blocked ? "blocked" : "unblocked"); | ||
return; | ||
} | ||
} | ||
|
||
FAIL() << "Could not successfullly " << (blocked ? "block" : "unblock") | ||
<< " media element"; | ||
} | ||
|
||
content::WebContents* web_contents_; | ||
|
||
private: | ||
DISALLOW_COPY_AND_ASSIGN(ChromecastShellMediaBlockingBrowserTest); | ||
}; | ||
|
||
IN_PROC_BROWSER_TEST_F(ChromecastShellMediaBlockingBrowserTest, | ||
Audio_BlockUnblock) { | ||
PlayMedia("audio", "bear-audio-10s-CBR-has-TOC.mp3"); | ||
|
||
BlockAndTestPlayerState("audio", true); | ||
BlockAndTestPlayerState("audio", false); | ||
} | ||
|
||
#if !BUILDFLAG(IS_CAST_AUDIO_ONLY) | ||
IN_PROC_BROWSER_TEST_F(ChromecastShellMediaBlockingBrowserTest, | ||
Video_BlockUnblock) { | ||
PlayMedia("video", "tulip2.webm"); | ||
|
||
BlockAndTestPlayerState("video", true); | ||
BlockAndTestPlayerState("video", false); | ||
} | ||
#endif | ||
|
||
} // namespace shell | ||
} // namespace chromecast |