Chromium Code Reviews| Index: content/browser/media/audio_stream_monitor.cc |
| diff --git a/chrome/browser/media/audio_stream_monitor.cc b/content/browser/media/audio_stream_monitor.cc |
| similarity index 42% |
| rename from chrome/browser/media/audio_stream_monitor.cc |
| rename to content/browser/media/audio_stream_monitor.cc |
| index 8633053d510684d1a69143590ec9d3047eb44194..7b13bc4adceb2b9462a9700841c51e193e9bbf7a 100644 |
| --- a/chrome/browser/media/audio_stream_monitor.cc |
| +++ b/content/browser/media/audio_stream_monitor.cc |
| @@ -2,14 +2,49 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -#include "chrome/browser/media/audio_stream_monitor.h" |
| +#include "content/browser/media/audio_stream_monitor.h" |
| #include "base/bind.h" |
| #include "base/bind_helpers.h" |
| +#include "content/public/browser/browser_thread.h" |
| #include "content/public/browser/invalidate_type.h" |
| +#include "content/public/browser/power_save_blocker.h" |
| +#include "content/public/browser/render_frame_host.h" |
| #include "content/public/browser/web_contents.h" |
| -DEFINE_WEB_CONTENTS_USER_DATA_KEY(AudioStreamMonitor); |
| +namespace content { |
| + |
| +namespace { |
| + |
| +AudioStreamMonitor* AudioStreamMonitorFromRenderFrame(int render_process_id, |
| + int render_frame_id) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + content::WebContents* const web_contents = |
|
jam
2014/09/02 19:50:33
nit: remove content:: from this file
DaleCurtis
2014/09/04 21:59:06
Done.
|
| + content::WebContents::FromRenderFrameHost( |
| + content::RenderFrameHost::FromID(render_process_id, render_frame_id)); |
| + if (!web_contents) |
| + return NULL; |
| + |
| + // Note: Calling CreateForWebContents() multiple times is valid (see usage |
| + // info for content::WebContentsUserData). |
| + AudioStreamMonitor::CreateForWebContents(web_contents); |
| + return AudioStreamMonitor::FromWebContents(web_contents); |
| +} |
| + |
| +} // namespace |
| + |
| +// TODO(dalecurtis): ???? DEFINE_WEB_CONTENTS_USER_DATA_KEY won't link, |
|
DaleCurtis
2014/09/03 18:48:55
No ideas on what's going on here? :)
miu
2014/09/03 22:41:53
If you were compiling on Windows, I would suggest:
DaleCurtis
2014/09/04 21:59:06
Correct. I tried NON_EXPORTED_BASE before creating
|
| +// kLocatorKey is created as a local symbol without the CONTENT_EXPORT |
| +// addition... without it, nm shows: |
| +// |
| +// $ nm content_unittests.audio_stream_monitor_unittest.o | grep kLocatorKey |
| +// U _ZN7content19...AudioStreamMonitorEE11kLocatorKeyE |
| +// $ nm libcontent.so | grep AudioStreamMonitor | grep kLocatorkey |
| +// 000000000139d8f0 b _ZN7content19...AudioStreamMonitorEE11kLocatorKeyE |
| +// |
| +template <> |
| +CONTENT_EXPORT int |
| + content::WebContentsUserData<AudioStreamMonitor>::kLocatorKey = 0; |
| AudioStreamMonitor::AudioStreamMonitor(content::WebContents* contents) |
| : web_contents_(contents), |
| @@ -25,7 +60,58 @@ bool AudioStreamMonitor::WasRecentlyAudible() const { |
| return was_recently_audible_; |
| } |
| +/* static */ |
| void AudioStreamMonitor::StartMonitoringStream( |
| + int render_process_id, |
| + int render_frame_id, |
| + int stream_id, |
| + const ReadPowerAndClipCallback& read_power_callback) { |
| + BrowserThread::PostTask(BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(&StartMonitoringHelper, |
| + render_process_id, |
| + render_frame_id, |
| + stream_id, |
| + read_power_callback)); |
| +} |
| + |
| +/* static */ |
| +void AudioStreamMonitor::StopMonitoringStream(int render_process_id, |
| + int render_frame_id, |
| + int stream_id) { |
| + BrowserThread::PostTask(BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(&StopMonitoringHelper, |
| + render_process_id, |
| + render_frame_id, |
| + stream_id)); |
| +} |
| + |
| +/* static */ |
| +void AudioStreamMonitor::StartMonitoringHelper( |
| + int render_process_id, |
| + int render_frame_id, |
| + int stream_id, |
| + const ReadPowerAndClipCallback& read_power_callback) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + AudioStreamMonitor* const monitor = |
| + AudioStreamMonitorFromRenderFrame(render_process_id, render_frame_id); |
| + if (monitor) |
| + monitor->StartMonitoringStreamOnUIThread(stream_id, read_power_callback); |
| +} |
| + |
| +/* static */ |
| +void AudioStreamMonitor::StopMonitoringHelper(int render_process_id, |
| + int render_frame_id, |
| + int stream_id) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + AudioStreamMonitor* const monitor = |
| + AudioStreamMonitorFromRenderFrame(render_process_id, render_frame_id); |
| + if (monitor) |
| + monitor->StopMonitoringStreamOnUIThread(stream_id); |
| +} |
| + |
| +void AudioStreamMonitor::StartMonitoringStreamOnUIThread( |
| int stream_id, |
| const ReadPowerAndClipCallback& read_power_callback) { |
| DCHECK(thread_checker_.CalledOnValidThread()); |
| @@ -39,7 +125,7 @@ void AudioStreamMonitor::StartMonitoringStream( |
| } |
| } |
| -void AudioStreamMonitor::StopMonitoringStream(int stream_id) { |
| +void AudioStreamMonitor::StopMonitoringStreamOnUIThread(int stream_id) { |
| DCHECK(thread_checker_.CalledOnValidThread()); |
| poll_callbacks_.erase(stream_id); |
| if (poll_callbacks_.empty()) |
| @@ -77,10 +163,18 @@ void AudioStreamMonitor::MaybeToggle() { |
| if (!should_indicator_be_on) { |
| off_timer_.Stop(); |
| + blocker_.reset(); |
| } else if (!off_timer_.IsRunning()) { |
| + if (!blocker_) { |
| + blocker_ = content::PowerSaveBlocker::Create( |
|
miu
2014/09/02 21:04:39
Can we move this into WebContentsImpl now? Meanin
DaleCurtis
2014/09/03 18:48:55
Let me play with it. I think that's a great idea i
DaleCurtis
2014/09/04 21:59:06
Done.
|
| + content::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, |
| + "Playing Audio"); |
| + } |
| off_timer_.Start( |
| FROM_HERE, |
| off_time - now, |
| base::Bind(&AudioStreamMonitor::MaybeToggle, base::Unretained(this))); |
| } |
| } |
| + |
| +} // namespace content |