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

Unified Diff: content/browser/media/session/media_session_controllers_manager.cc

Issue 1698933004: Make MediaSession a runtime-enabled feature on Desktop. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 9 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 side-by-side diff with in-line comments
Download patch
Index: content/browser/media/session/media_session_controllers_manager.cc
diff --git a/content/browser/media/session/media_session_controllers_manager.cc b/content/browser/media/session/media_session_controllers_manager.cc
new file mode 100644
index 0000000000000000000000000000000000000000..0b93dc89ac069538987ba63c78d0bb4637084c2b
--- /dev/null
+++ b/content/browser/media/session/media_session_controllers_manager.cc
@@ -0,0 +1,94 @@
+// 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 "content/browser/media/session/media_session_controllers_manager.h"
+
+#include "base/command_line.h"
+#include "content/browser/media/session/media_session.h"
+#include "content/browser/media/session/media_session_controller.h"
+#include "content/browser/media/session/media_session_observer.h"
+#include "media/base/media_switches.h"
+
+namespace content {
+
+namespace {
+
+bool IsDefaultMediaSessionEnabled() {
+#if defined(OS_ANDROID)
+ return true;
+#else
+ return base::CommandLine::ForCurrentProcess()->HasSwitch(
+ switches::kEnableDefaultMediaSession);
+#endif
+}
+
+} // anonymous namespace
+
+MediaSessionControllersManager::MediaSessionControllersManager(
+ MediaWebContentsObserver* media_web_contents_observer)
+ : media_web_contents_observer_(media_web_contents_observer) {
+}
+
+MediaSessionControllersManager::~MediaSessionControllersManager() = default;
+
+void MediaSessionControllersManager::RenderFrameDeleted(
+ RenderFrameHost* render_frame_host) {
+ if (!IsDefaultMediaSessionEnabled())
+ return;
+
+ for (auto it = controllers_map_.begin(); it != controllers_map_.end();) {
+ if (it->first.first == render_frame_host)
+ it = controllers_map_.erase(it);
+ else
+ ++it;
+ }
+}
+
+bool MediaSessionControllersManager::RequestPlay(const MediaPlayerId& id,
+ bool has_audio, bool is_remote, base::TimeDelta duration) {
+ if (!IsDefaultMediaSessionEnabled())
+ return true;
+
+ // Since we don't remove session instances on pause, there may be an existing
+ // instance for this playback attempt.
+ //
+ // In this case, try to reinitialize it with the new settings. If they are
+ // the same, this is a no-op. If the reinitialize fails, destroy the
+ // controller. A later playback attempt will create a new controller.
+ auto it = controllers_map_.find(id);
+ if (it != controllers_map_.end()) {
+ if (it->second->Initialize(has_audio, is_remote, duration))
+ return true;
+ controllers_map_.erase(it);
+ return false;
+ }
+
+ scoped_ptr<MediaSessionController> controller(
+ new MediaSessionController(id, media_web_contents_observer_));
+
+ if (!controller->Initialize(has_audio, is_remote, duration))
+ return false;
+
+ controllers_map_[id] = std::move(controller);
+ return true;
+}
+
+void MediaSessionControllersManager::OnPause(const MediaPlayerId& id) {
+ if (!IsDefaultMediaSessionEnabled())
+ return;
+
+ auto it = controllers_map_.find(id);
+ if (it == controllers_map_.end())
+ return;
+
+ it->second->OnPlaybackPaused();
+}
+
+void MediaSessionControllersManager::OnEnd(const MediaPlayerId& id) {
+ if (!IsDefaultMediaSessionEnabled())
+ return;
+ controllers_map_.erase(id);
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698