diff options
author | avi@chromium.org <avi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-19 18:21:07 +0000 |
---|---|---|
committer | avi@chromium.org <avi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-19 18:21:07 +0000 |
commit | baff4519ccf57ef96204cc1eb6bde888ef979fc8 (patch) | |
tree | 822617b1fd0a69b41f6d2ea357072da2c627592b | |
parent | 1e981ef18167e46b14c8634d7bd5a0f810e1db88 (diff) | |
download | chromium_src-baff4519ccf57ef96204cc1eb6bde888ef979fc8.zip chromium_src-baff4519ccf57ef96204cc1eb6bde888ef979fc8.tar.gz chromium_src-baff4519ccf57ef96204cc1eb6bde888ef979fc8.tar.bz2 |
Prevent power saving while playing media.
BUG=100054
TEST=play movie while on battery; screen won't dim
Review URL: http://codereview.chromium.org/8344018
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@106328 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | content/browser/renderer_host/render_view_host.cc | 34 | ||||
-rw-r--r-- | content/browser/renderer_host/render_view_host.h | 24 | ||||
-rw-r--r-- | content/common/view_messages.h | 7 | ||||
-rw-r--r-- | content/renderer/render_view_impl.cc | 21 | ||||
-rw-r--r-- | content/renderer/render_view_impl.h | 8 | ||||
-rw-r--r-- | webkit/glue/webkit_glue.gypi | 1 | ||||
-rw-r--r-- | webkit/glue/webmediaplayer_delegate.h | 31 | ||||
-rw-r--r-- | webkit/glue/webmediaplayer_impl.cc | 12 | ||||
-rw-r--r-- | webkit/glue/webmediaplayer_impl.h | 4 | ||||
-rw-r--r-- | webkit/support/webkit_support.cc | 1 | ||||
-rw-r--r-- | webkit/tools/test_shell/test_webview_delegate.cc | 1 |
11 files changed, 138 insertions, 6 deletions
diff --git a/content/browser/renderer_host/render_view_host.cc b/content/browser/renderer_host/render_view_host.cc index 9fda132..5fa3bb6 100644 --- a/content/browser/renderer_host/render_view_host.cc +++ b/content/browser/renderer_host/render_view_host.cc @@ -11,6 +11,7 @@ #include "base/command_line.h" #include "base/i18n/rtl.h" #include "base/json/json_reader.h" +#include "base/stl_util.h" #include "base/string_util.h" #include "base/time.h" #include "base/utf_string_conversions.h" @@ -21,6 +22,7 @@ #include "content/browser/cross_site_request_manager.h" #include "content/browser/host_zoom_map.h" #include "content/browser/in_process_webkit/session_storage_namespace.h" +#include "content/browser/power_save_blocker.h" #include "content/browser/renderer_host/render_process_host.h" #include "content/browser/renderer_host/render_view_host_delegate.h" #include "content/browser/renderer_host/render_view_host_observer.h" @@ -144,6 +146,8 @@ RenderViewHost::~RenderViewHost() { content::Source<RenderViewHost>(this), NotificationService::NoDetails()); + ClearPowerSaveBlockers(); + delegate()->RenderViewDeleted(this); // Be sure to clean up any leftover state from cross-site requests. @@ -677,6 +681,7 @@ bool RenderViewHost::OnMessageReceived(const IPC::Message& msg) { OnAccessibilityNotifications) IPC_MESSAGE_HANDLER(ViewHostMsg_ScriptEvalResponse, OnScriptEvalResponse) IPC_MESSAGE_HANDLER(ViewHostMsg_DidZoomURL, OnDidZoomURL) + IPC_MESSAGE_HANDLER(ViewHostMsg_MediaNotification, OnMediaNotification) IPC_MESSAGE_HANDLER(DesktopNotificationHostMsg_RequestPermission, OnRequestDesktopNotificationPermission) IPC_MESSAGE_HANDLER(DesktopNotificationHostMsg_Show, @@ -790,6 +795,9 @@ void RenderViewHost::OnMsgRenderViewGone(int status, int exit_code) { render_view_termination_status_ = static_cast<base::TerminationStatus>(status); + // Reset state. + ClearPowerSaveBlockers(); + // Our base class RenderWidgetHost needs to reset some stuff. RendererExited(render_view_termination_status_, exit_code); @@ -1382,6 +1390,28 @@ void RenderViewHost::OnDidZoomURL(double zoom_level, } } +void RenderViewHost::OnMediaNotification(int64 player_cookie, + bool has_video, + bool has_audio, + bool is_playing) { + if (is_playing) { + PowerSaveBlocker* blocker = NULL; + if (has_video) { + blocker = new PowerSaveBlocker( + PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep); + } else if (has_audio) { + blocker = new PowerSaveBlocker( + PowerSaveBlocker::kPowerSaveBlockPreventSystemSleep); + } + + if (blocker) + power_save_blockers_[player_cookie] = blocker; + } else { + delete power_save_blockers_[player_cookie]; + power_save_blockers_.erase(player_cookie); + } +} + void RenderViewHost::OnRequestDesktopNotificationPermission( const GURL& source_origin, int callback_context) { content::GetContentClient()->browser()->RequestDesktopNotificationPermission( @@ -1433,3 +1463,7 @@ void RenderViewHost::OnWebUISend(const GURL& source_url, const base::ListValue& args) { delegate_->WebUISend(this, source_url, name, args); } + +void RenderViewHost::ClearPowerSaveBlockers() { + STLDeleteValues(&power_save_blockers_); +} diff --git a/content/browser/renderer_host/render_view_host.h b/content/browser/renderer_host/render_view_host.h index d731754..516be59 100644 --- a/content/browser/renderer_host/render_view_host.h +++ b/content/browser/renderer_host/render_view_host.h @@ -26,25 +26,26 @@ #include "webkit/glue/window_open_disposition.h" class ChildProcessSecurityPolicy; +struct ContextMenuParams; struct DesktopNotificationHostMsg_Show_Params; class FilePath; class GURL; +struct MediaPlayerAction; +class PowerSaveBlocker; class RenderViewHostDelegate; class RenderViewHostObserver; class SessionStorageNamespace; class SiteInstance; class SkBitmap; -class ViewMsg_Navigate; -struct ContextMenuParams; -struct MediaPlayerAction; +struct UserMetricsAction; struct ViewHostMsg_AccessibilityNotification_Params; struct ViewHostMsg_CreateWindow_Params; +struct ViewHostMsg_RunFileChooser_Params; struct ViewHostMsg_ShowPopup_Params; +class ViewMsg_Navigate; struct ViewMsg_Navigate_Params; -struct WebDropData; -struct UserMetricsAction; -struct ViewHostMsg_RunFileChooser_Params; struct ViewMsg_StopFinding_Params; +struct WebDropData; namespace base { class ListValue; @@ -538,6 +539,10 @@ class CONTENT_EXPORT RenderViewHost : public RenderWidgetHost { const std::vector<ViewHostMsg_AccessibilityNotification_Params>& params); void OnScriptEvalResponse(int id, const base::ListValue& result); void OnDidZoomURL(double zoom_level, bool remember, const GURL& url); + void OnMediaNotification(int64 player_cookie, + bool has_video, + bool has_audio, + bool is_playing); void OnRequestDesktopNotificationPermission(const GURL& origin, int callback_id); void OnShowDesktopNotification( @@ -555,6 +560,8 @@ class CONTENT_EXPORT RenderViewHost : public RenderWidgetHost { private: friend class TestRenderViewHost; + void ClearPowerSaveBlockers(); + // The SiteInstance associated with this RenderViewHost. All pages drawn // in this RenderViewHost are part of this SiteInstance. Should not change // over time. @@ -632,6 +639,11 @@ class CONTENT_EXPORT RenderViewHost : public RenderWidgetHost { // The termination status of the last render view that terminated. base::TerminationStatus render_view_termination_status_; + // Holds PowerSaveBlockers for the media players in use. Key is the + // player_cookie passed to OnMediaNotification, value is the PowerSaveBlocker. + typedef std::map<int64, PowerSaveBlocker*> PowerSaveBlockerMap; + PowerSaveBlockerMap power_save_blockers_; + // A list of observers that filter messages. Weak references. ObserverList<RenderViewHostObserver> observers_; diff --git a/content/common/view_messages.h b/content/common/view_messages.h index b948df7..8629fca 100644 --- a/content/common/view_messages.h +++ b/content/common/view_messages.h @@ -1302,6 +1302,13 @@ IPC_MESSAGE_ROUTED1(ViewHostMsg_SwapOut_ACK, // message. IPC_MESSAGE_ROUTED0(ViewHostMsg_ClosePage_ACK) +// Notifies the browser that media has started/stopped playing. +IPC_MESSAGE_ROUTED4(ViewHostMsg_MediaNotification, + int64 /* player_cookie, distinguishes instances */, + bool /* has_video */, + bool /* has_audio */, + bool /* is_playing */) + // Notifies the browser that we have session history information. // page_id: unique ID that allows us to distinguish between history entries. IPC_MESSAGE_ROUTED2(ViewHostMsg_UpdateState, diff --git a/content/renderer/render_view_impl.cc b/content/renderer/render_view_impl.cc index d3dfc1f..9d1b766 100644 --- a/content/renderer/render_view_impl.cc +++ b/content/renderer/render_view_impl.cc @@ -1947,6 +1947,7 @@ WebMediaPlayer* RenderViewImpl::createMediaPlayer( scoped_ptr<webkit_glue::WebMediaPlayerImpl> result( new webkit_glue::WebMediaPlayerImpl(client, + this, collection.release(), message_loop_factory.release(), media_stream_impl_.get(), @@ -3188,6 +3189,26 @@ WebCookieJar* RenderViewImpl::GetCookieJar() { return &cookie_jar_; } +void RenderViewImpl::DidPlay(webkit_glue::WebMediaPlayerImpl* player) { + Send(new ViewHostMsg_MediaNotification(routing_id_, + reinterpret_cast<int64>(player), + player->hasVideo(), + player->hasAudio(), + true)); +} + +void RenderViewImpl::DidPause(webkit_glue::WebMediaPlayerImpl* player) { + Send(new ViewHostMsg_MediaNotification(routing_id_, + reinterpret_cast<int64>(player), + player->hasVideo(), + player->hasAudio(), + false)); +} + +void RenderViewImpl::PlayerGone(webkit_glue::WebMediaPlayerImpl* player) { + DidPause(player); +} + void RenderViewImpl::SyncNavigationState() { if (!webview()) return; diff --git a/content/renderer/render_view_impl.h b/content/renderer/render_view_impl.h index 2ae0e9d..da6cd9b 100644 --- a/content/renderer/render_view_impl.h +++ b/content/renderer/render_view_impl.h @@ -43,6 +43,7 @@ #include "third_party/WebKit/Source/WebKit/chromium/public/WebViewClient.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebNavigationType.h" #include "ui/gfx/surface/transport_dib.h" +#include "webkit/glue/webmediaplayer_delegate.h" #include "webkit/glue/webpreferences.h" #include "webkit/plugins/npapi/webplugin_page_delegate.h" @@ -166,6 +167,7 @@ class RenderViewImpl : public RenderWidget, public WebKit::WebPageSerializerClient, public content::RenderView, public webkit::npapi::WebPluginPageDelegate, + public webkit_glue::WebMediaPlayerDelegate, public base::SupportsWeakPtr<RenderViewImpl> { public: // Creates a new RenderView. The parent_hwnd specifies a HWND to use as the @@ -603,6 +605,12 @@ class RenderViewImpl : public RenderWidget, virtual void DidStopLoadingForPlugin(); virtual WebKit::WebCookieJar* GetCookieJar(); + // webkit_glue::WebMediaPlayerDelegate implementation ------------------------ + + virtual void DidPlay(webkit_glue::WebMediaPlayerImpl* player) OVERRIDE; + virtual void DidPause(webkit_glue::WebMediaPlayerImpl* player) OVERRIDE; + virtual void PlayerGone(webkit_glue::WebMediaPlayerImpl* player) OVERRIDE; + // Please do not add your stuff randomly to the end here. If there is an // appropriate section, add it there. If not, there are some random functions // nearer to the top you can add it to. diff --git a/webkit/glue/webkit_glue.gypi b/webkit/glue/webkit_glue.gypi index 6ccde32..a5f5973 100644 --- a/webkit/glue/webkit_glue.gypi +++ b/webkit/glue/webkit_glue.gypi @@ -399,6 +399,7 @@ 'webkit_glue.h', 'webkitplatformsupport_impl.cc', 'webkitplatformsupport_impl.h', + 'webmediaplayer_delegate.h', 'webmediaplayer_impl.cc', 'webmediaplayer_impl.h', 'webmediaplayer_proxy.cc', diff --git a/webkit/glue/webmediaplayer_delegate.h b/webkit/glue/webmediaplayer_delegate.h new file mode 100644 index 0000000..bec39b0 --- /dev/null +++ b/webkit/glue/webmediaplayer_delegate.h @@ -0,0 +1,31 @@ +// Copyright (c) 2011 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. + +#ifndef WEBKIT_GLUE_WEBMEDIAPLAYER_DELEGATE_H_ +#define WEBKIT_GLUE_WEBMEDIAPLAYER_DELEGATE_H_ + +namespace webkit_glue { + +class WebMediaPlayerImpl; + +// An interface to allow a WebMediaPlayerImpl to communicate changes of state +// to objects that need to know. +class WebMediaPlayerDelegate { + public: + WebMediaPlayerDelegate() {} + virtual ~WebMediaPlayerDelegate() {} + + // The specified player started playing media. + virtual void DidPlay(WebMediaPlayerImpl* player) {} + + // The specified player stopped playing media. + virtual void DidPause(WebMediaPlayerImpl* player) {} + + // The specified player was destroyed. Do not call any methods on it. + virtual void PlayerGone(WebMediaPlayerImpl* player) {} +}; + +} // namespace webkit_glue + +#endif // WEBKIT_GLUE_WEBMEDIAPLAYER_DELEGATE_H_ diff --git a/webkit/glue/webmediaplayer_impl.cc b/webkit/glue/webmediaplayer_impl.cc index c504235..fd8d497 100644 --- a/webkit/glue/webmediaplayer_impl.cc +++ b/webkit/glue/webmediaplayer_impl.cc @@ -33,6 +33,7 @@ #include "webkit/glue/media/media_stream_client.h" #include "webkit/glue/media/video_renderer_impl.h" #include "webkit/glue/media/web_video_renderer.h" +#include "webkit/glue/webmediaplayer_delegate.h" #include "webkit/glue/webmediaplayer_proxy.h" #include "webkit/glue/webvideoframe_impl.h" @@ -97,6 +98,7 @@ namespace webkit_glue { WebMediaPlayerImpl::WebMediaPlayerImpl( WebKit::WebMediaPlayerClient* client, + WebMediaPlayerDelegate* delegate, media::FilterCollection* collection, media::MessageLoopFactory* message_loop_factory, MediaStreamClient* media_stream_client, @@ -113,6 +115,7 @@ WebMediaPlayerImpl::WebMediaPlayerImpl( pending_seek_(false), client_(client), proxy_(NULL), + delegate_(delegate), media_stream_client_(media_stream_client), media_log_(media_log), incremented_externally_allocated_memory_(false) { @@ -208,6 +211,9 @@ WebMediaPlayerImpl::~WebMediaPlayerImpl() { media_log_->AddEvent( media_log_->CreateEvent(media::MediaLogEvent::WEBMEDIAPLAYER_DESTROYED)); + if (delegate_) + delegate_->PlayerGone(this); + // Finally tell the |main_loop_| we don't want to be notified of destruction // event. if (main_loop_) { @@ -266,6 +272,9 @@ void WebMediaPlayerImpl::play() { pipeline_->SetPlaybackRate(playback_rate_); media_log_->AddEvent(media_log_->CreateEvent(media::MediaLogEvent::PLAY)); + + if (delegate_) + delegate_->DidPlay(this); } void WebMediaPlayerImpl::pause() { @@ -276,6 +285,9 @@ void WebMediaPlayerImpl::pause() { paused_time_ = pipeline_->GetCurrentTime(); media_log_->AddEvent(media_log_->CreateEvent(media::MediaLogEvent::PAUSE)); + + if (delegate_) + delegate_->DidPause(this); } bool WebMediaPlayerImpl::supportsFullscreen() const { diff --git a/webkit/glue/webmediaplayer_impl.h b/webkit/glue/webmediaplayer_impl.h index 00eef17..7f1eb13 100644 --- a/webkit/glue/webmediaplayer_impl.h +++ b/webkit/glue/webmediaplayer_impl.h @@ -73,6 +73,7 @@ namespace webkit_glue { class MediaResourceLoaderBridgeFactory; class MediaStreamClient; +class WebMediaPlayerDelegate; class WebMediaPlayerProxy; class WebVideoRenderer; @@ -104,6 +105,7 @@ class WebMediaPlayerImpl // // Callers must call |Initialize()| before they can use the object. WebMediaPlayerImpl(WebKit::WebMediaPlayerClient* client, + WebMediaPlayerDelegate* delegate, media::FilterCollection* collection, media::MessageLoopFactory* message_loop_factory, MediaStreamClient* media_stream_client, @@ -257,6 +259,8 @@ class WebMediaPlayerImpl scoped_refptr<WebMediaPlayerProxy> proxy_; + WebMediaPlayerDelegate* delegate_; + MediaStreamClient* media_stream_client_; #if WEBKIT_USING_CG diff --git a/webkit/support/webkit_support.cc b/webkit/support/webkit_support.cc index 3379981..6412419 100644 --- a/webkit/support/webkit_support.cc +++ b/webkit/support/webkit_support.cc @@ -324,6 +324,7 @@ WebKit::WebMediaPlayer* CreateMediaPlayer(WebFrame* frame, scoped_ptr<webkit_glue::WebMediaPlayerImpl> result( new webkit_glue::WebMediaPlayerImpl(client, + NULL, collection.release(), message_loop_factory.release(), NULL, diff --git a/webkit/tools/test_shell/test_webview_delegate.cc b/webkit/tools/test_shell/test_webview_delegate.cc index 8d63abd..e86b275 100644 --- a/webkit/tools/test_shell/test_webview_delegate.cc +++ b/webkit/tools/test_shell/test_webview_delegate.cc @@ -659,6 +659,7 @@ WebMediaPlayer* TestWebViewDelegate::createMediaPlayer( scoped_ptr<webkit_glue::WebMediaPlayerImpl> result( new webkit_glue::WebMediaPlayerImpl(client, + NULL, collection.release(), message_loop_factory.release(), NULL, |