146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// found in the LICENSE file.
446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "media/cdm/player_tracker_impl.h"
646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include <utility>
846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "base/stl_util.h"
1046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
1146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)namespace media {
1246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
1346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)PlayerTrackerImpl::PlayerCallbacks::PlayerCallbacks(
1446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    base::Closure new_key_cb,
1546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    base::Closure cdm_unset_cb)
1646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    : new_key_cb(new_key_cb), cdm_unset_cb(cdm_unset_cb) {
1746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
1846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
1946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)PlayerTrackerImpl::PlayerCallbacks::~PlayerCallbacks() {
2046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
2146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
2246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)PlayerTrackerImpl::PlayerTrackerImpl() : next_registration_id_(1) {}
2346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
2446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)PlayerTrackerImpl::~PlayerTrackerImpl() {}
2546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
2646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)int PlayerTrackerImpl::RegisterPlayer(const base::Closure& new_key_cb,
2746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                                      const base::Closure& cdm_unset_cb) {
2846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
2946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  int registration_id = next_registration_id_++;
3046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  DCHECK(!ContainsKey(player_callbacks_map_, registration_id));
3146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  player_callbacks_map_.insert(std::make_pair(
3246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      registration_id, PlayerCallbacks(new_key_cb, cdm_unset_cb)));
3346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  return registration_id;
3446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
3546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
3646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)void PlayerTrackerImpl::UnregisterPlayer(int registration_id) {
3746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
3846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  DCHECK(ContainsKey(player_callbacks_map_, registration_id))
3946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      << registration_id;
4046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  player_callbacks_map_.erase(registration_id);
4146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
4246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
4346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)void PlayerTrackerImpl::NotifyNewKey() {
4446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
4546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  std::map<int, PlayerCallbacks>::iterator iter = player_callbacks_map_.begin();
4646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  for (; iter != player_callbacks_map_.end(); ++iter)
4746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    iter->second.new_key_cb.Run();
4846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
4946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
5046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)void PlayerTrackerImpl::NotifyCdmUnset() {
5146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
5246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  std::map<int, PlayerCallbacks>::iterator iter = player_callbacks_map_.begin();
5346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  for (; iter != player_callbacks_map_.end(); ++iter)
5446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    iter->second.cdm_unset_cb.Run();
5546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
5646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
5746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}  // namespace media
58