OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/audio/audio_service.h" | 5 #include "chrome/browser/extensions/api/audio/audio_service.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
9 #include "base/observer_list.h" | 9 #include "base/observer_list.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/threading/thread_checker.h" |
11 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
12 | 13 |
13 using content::BrowserThread; | 14 using content::BrowserThread; |
14 | 15 |
15 namespace extensions { | 16 namespace extensions { |
16 | 17 |
17 using api::audio::OutputDeviceInfo; | 18 using api::audio::OutputDeviceInfo; |
18 using api::audio::InputDeviceInfo; | 19 using api::audio::InputDeviceInfo; |
19 | 20 |
20 class AudioServiceImpl : public AudioService { | 21 class AudioServiceImpl : public AudioService { |
21 public: | 22 public: |
22 AudioServiceImpl(); | 23 AudioServiceImpl(); |
23 virtual ~AudioServiceImpl(); | 24 virtual ~AudioServiceImpl(); |
24 | 25 |
| 26 private: |
25 // Called by listeners to this service to add/remove themselves as observers. | 27 // Called by listeners to this service to add/remove themselves as observers. |
26 virtual void AddObserver(AudioService::Observer* observer) OVERRIDE; | 28 virtual void AddObserver(AudioService::Observer* observer) OVERRIDE; |
27 virtual void RemoveObserver(AudioService::Observer* observer) OVERRIDE; | 29 virtual void RemoveObserver(AudioService::Observer* observer) OVERRIDE; |
28 | 30 |
29 // Start to query audio device information. | 31 // Start to query audio device information. |
30 virtual void StartGetInfo(const GetInfoCallback& callback) OVERRIDE; | 32 virtual void StartGetInfo(const GetInfoCallback& callback) OVERRIDE; |
31 virtual void SetActiveDevices(const DeviceIdList& device_list) OVERRIDE; | 33 virtual void SetActiveDevices(const DeviceIdList& device_list) OVERRIDE; |
32 virtual bool SetDeviceProperties(const std::string& device_id, | 34 virtual bool SetDeviceProperties(const std::string& device_id, |
33 bool muted, | 35 bool muted, |
34 int volume, | 36 int volume, |
35 int gain) OVERRIDE; | 37 int gain) OVERRIDE; |
36 | 38 |
37 // List of observers. | 39 // List of observers. |
38 ObserverList<AudioService::Observer> observer_list_; | 40 ObserverList<AudioService::Observer> observer_list_; |
39 | 41 |
| 42 base::ThreadChecker thread_checker_; |
| 43 |
40 // Note: This should remain the last member so it'll be destroyed and | 44 // Note: This should remain the last member so it'll be destroyed and |
41 // invalidate the weak pointers before any other members are destroyed. | 45 // invalidate the weak pointers before any other members are destroyed. |
42 base::WeakPtrFactory<AudioServiceImpl> weak_ptr_factory_; | 46 base::WeakPtrFactory<AudioServiceImpl> weak_ptr_factory_; |
43 | 47 |
44 DISALLOW_COPY_AND_ASSIGN(AudioServiceImpl); | 48 DISALLOW_COPY_AND_ASSIGN(AudioServiceImpl); |
45 }; | 49 }; |
46 | 50 |
47 AudioServiceImpl::AudioServiceImpl() : weak_ptr_factory_(this) { | 51 AudioServiceImpl::AudioServiceImpl() : weak_ptr_factory_(this) { |
48 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 52 DCHECK(thread_checker_.CalledOnValidThread()); |
49 } | 53 } |
50 | 54 |
51 AudioServiceImpl::~AudioServiceImpl() { | 55 AudioServiceImpl::~AudioServiceImpl() { |
52 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 56 DCHECK(thread_checker_.CalledOnValidThread()); |
53 } | 57 } |
54 | 58 |
55 void AudioServiceImpl::AddObserver(AudioService::Observer* observer) { | 59 void AudioServiceImpl::AddObserver(AudioService::Observer* observer) { |
56 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 60 DCHECK(thread_checker_.CalledOnValidThread()); |
57 observer_list_.AddObserver(observer); | 61 observer_list_.AddObserver(observer); |
58 } | 62 } |
59 | 63 |
60 void AudioServiceImpl::RemoveObserver(AudioService::Observer* observer) { | 64 void AudioServiceImpl::RemoveObserver(AudioService::Observer* observer) { |
61 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 65 DCHECK(thread_checker_.CalledOnValidThread()); |
62 observer_list_.RemoveObserver(observer); | 66 observer_list_.RemoveObserver(observer); |
63 } | 67 } |
64 | 68 |
65 void AudioServiceImpl::StartGetInfo(const GetInfoCallback& callback) { | 69 void AudioServiceImpl::StartGetInfo(const GetInfoCallback& callback) { |
66 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 70 DCHECK(thread_checker_.CalledOnValidThread()); |
67 if (!callback.is_null()) | 71 if (!callback.is_null()) |
68 callback.Run(OutputInfo(), InputInfo(), false); | 72 callback.Run(OutputInfo(), InputInfo(), false); |
69 } | 73 } |
70 | 74 |
71 void AudioServiceImpl::SetActiveDevices(const DeviceIdList& device_list) { | 75 void AudioServiceImpl::SetActiveDevices(const DeviceIdList& device_list) { |
72 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 76 DCHECK(thread_checker_.CalledOnValidThread()); |
73 } | 77 } |
74 | 78 |
75 bool AudioServiceImpl::SetDeviceProperties(const std::string& device_id, | 79 bool AudioServiceImpl::SetDeviceProperties(const std::string& device_id, |
76 bool muted, | 80 bool muted, |
77 int volume, | 81 int volume, |
78 int gain) { | 82 int gain) { |
79 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 83 DCHECK(thread_checker_.CalledOnValidThread()); |
80 return false; | 84 return false; |
81 } | 85 } |
82 | 86 |
83 AudioService* AudioService::CreateInstance() { | 87 AudioService* AudioService::CreateInstance() { |
84 return new AudioServiceImpl; | 88 return new AudioServiceImpl; |
85 } | 89 } |
86 | 90 |
87 } // namespace extensions | 91 } // namespace extensions |
OLD | NEW |