Switch TestFrameNavigationObserver to DidCommitProvisionalLoadForFrame.
[chromium-blink-merge.git] / media / audio / sounds / sounds_manager.cc
blob72a7a3b15c1b91eba20e426873a5bee5fb5c37ba
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/audio/sounds/sounds_manager.h"
7 #include "base/compiler_specific.h"
8 #include "base/logging.h"
9 #include "base/memory/linked_ptr.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/single_thread_task_runner.h"
12 #include "media/audio/audio_manager.h"
13 #include "media/audio/sounds/audio_stream_handler.h"
15 namespace media {
17 namespace {
19 SoundsManager* g_instance = NULL;
20 bool g_initialized_for_testing = false;
22 // SoundsManagerImpl ---------------------------------------------------
24 class SoundsManagerImpl : public SoundsManager {
25 public:
26 SoundsManagerImpl();
27 ~SoundsManagerImpl() override;
29 // SoundsManager implementation:
30 bool Initialize(SoundKey key, const base::StringPiece& data) override;
31 bool Play(SoundKey key) override;
32 base::TimeDelta GetDuration(SoundKey key) override;
34 private:
35 base::hash_map<SoundKey, linked_ptr<AudioStreamHandler> > handlers_;
36 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
38 DISALLOW_COPY_AND_ASSIGN(SoundsManagerImpl);
41 SoundsManagerImpl::SoundsManagerImpl()
42 : task_runner_(AudioManager::Get()->GetTaskRunner()) {
45 SoundsManagerImpl::~SoundsManagerImpl() { DCHECK(CalledOnValidThread()); }
47 bool SoundsManagerImpl::Initialize(SoundKey key,
48 const base::StringPiece& data) {
49 if (handlers_.find(key) != handlers_.end() && handlers_[key]->IsInitialized())
50 return true;
51 linked_ptr<AudioStreamHandler> handler(new AudioStreamHandler(data));
52 if (!handler->IsInitialized()) {
53 LOG(WARNING) << "Can't initialize AudioStreamHandler for key=" << key;
54 return false;
56 handlers_[key] = handler;
57 return true;
60 bool SoundsManagerImpl::Play(SoundKey key) {
61 DCHECK(CalledOnValidThread());
62 if (handlers_.find(key) == handlers_.end() ||
63 !handlers_[key]->IsInitialized()) {
64 return false;
66 return handlers_[key]->Play();
69 base::TimeDelta SoundsManagerImpl::GetDuration(SoundKey key) {
70 DCHECK(CalledOnValidThread());
71 if (handlers_.find(key) == handlers_.end() ||
72 !handlers_[key]->IsInitialized()) {
73 return base::TimeDelta();
75 const WavAudioHandler& wav_audio = handlers_[key]->wav_audio_handler();
76 return wav_audio.GetDuration();
79 } // namespace
81 SoundsManager::SoundsManager() {}
83 SoundsManager::~SoundsManager() { DCHECK(CalledOnValidThread()); }
85 // static
86 void SoundsManager::Create() {
87 CHECK(!g_instance || g_initialized_for_testing)
88 << "SoundsManager::Create() is called twice";
89 if (g_initialized_for_testing)
90 return;
91 g_instance = new SoundsManagerImpl();
94 // static
95 void SoundsManager::Shutdown() {
96 CHECK(g_instance) << "SoundsManager::Shutdown() is called "
97 << "without previous call to Create()";
98 delete g_instance;
99 g_instance = NULL;
102 // static
103 SoundsManager* SoundsManager::Get() {
104 CHECK(g_instance) << "SoundsManager::Get() is called before Create()";
105 return g_instance;
108 // static
109 void SoundsManager::InitializeForTesting(SoundsManager* manager) {
110 CHECK(!g_instance) << "SoundsManager is already initialized.";
111 CHECK(manager);
112 g_instance = manager;
113 g_initialized_for_testing = true;
116 } // namespace media