Index: chrome/browser/ui/webui/mediaplayer_ui.cc |
diff --git a/chrome/browser/ui/webui/mediaplayer_ui.cc b/chrome/browser/ui/webui/mediaplayer_ui.cc |
index cb900b002541001893b2a23edc3a4f1d69d6850b..22bfa8a239215e7d5dc4c71ad59692e7ee836a28 100644 |
--- a/chrome/browser/ui/webui/mediaplayer_ui.cc |
+++ b/chrome/browser/ui/webui/mediaplayer_ui.cc |
@@ -16,6 +16,7 @@ |
#include "base/time.h" |
#include "base/values.h" |
#include "chrome/browser/bookmarks/bookmark_model.h" |
+#include "chrome/browser/chromeos/extensions/media_player_event_router.h" |
#include "chrome/browser/download/download_manager.h" |
#include "chrome/browser/download/download_util.h" |
#include "chrome/browser/extensions/file_manager_util.h" |
@@ -48,296 +49,18 @@ |
#include "chrome/browser/chromeos/frame/panel_browser_view.h" |
#endif |
-static const char kPropertyPath[] = "path"; |
-static const char kPropertyForce[] = "force"; |
-static const char kPropertyOffset[] = "currentOffset"; |
-static const char kPropertyError[] = "error"; |
- |
-static const char* kMediaplayerURL = "chrome://mediaplayer"; |
-static const char* kMediaplayerPlaylistURL = "chrome://mediaplayer#playlist"; |
static const char* kMediaPlayerAppName = "mediaplayer"; |
static const int kPopupLeft = 0; |
static const int kPopupTop = 0; |
static const int kPopupWidth = 350; |
static const int kPopupHeight = 300; |
-class MediaplayerUIHTMLSource : public ChromeURLDataManager::DataSource { |
- public: |
- explicit MediaplayerUIHTMLSource(bool is_playlist); |
- |
- // Called when the network layer has requested a resource underneath |
- // the path we registered. |
- virtual void StartDataRequest(const std::string& path, |
- bool is_incognito, |
- int request_id); |
- virtual std::string GetMimeType(const std::string&) const { |
- return "text/html"; |
- } |
- |
- private: |
- ~MediaplayerUIHTMLSource() {} |
- bool is_playlist_; |
- |
- DISALLOW_COPY_AND_ASSIGN(MediaplayerUIHTMLSource); |
-}; |
- |
-// The handler for Javascript messages related to the "mediaplayer" view. |
-class MediaplayerHandler : public WebUIMessageHandler, |
- public base::SupportsWeakPtr<MediaplayerHandler> { |
- public: |
- |
- struct MediaUrl { |
- MediaUrl() {} |
- explicit MediaUrl(const GURL& newurl) |
- : url(newurl), |
- haderror(false) {} |
- GURL url; |
- bool haderror; |
- }; |
- typedef std::vector<MediaUrl> UrlVector; |
- |
- explicit MediaplayerHandler(bool is_playlist); |
- |
- virtual ~MediaplayerHandler(); |
- |
- // Init work after Attach. |
- void Init(bool is_playlist, TabContents* contents); |
- |
- // WebUIMessageHandler implementation. |
- virtual WebUIMessageHandler* Attach(WebUI* web_ui); |
- virtual void RegisterMessages(); |
- |
- // Callback for the "currentOffsetChanged" message. |
- void HandleCurrentOffsetChanged(const ListValue* args); |
- |
- void FirePlaylistChanged(const std::string& path, |
- bool force, |
- int offset); |
- |
- void PlaybackMediaFile(const GURL& url); |
- |
- void EnqueueMediaFileUrl(const GURL& url); |
- |
- void GetPlaylistValue(ListValue& args); |
- |
- // Callback for the "playbackError" message. |
- void HandlePlaybackError(const ListValue* args); |
- |
- // Callback for the "getCurrentPlaylist" message. |
- void HandleGetCurrentPlaylist(const ListValue* args); |
- |
- void HandleTogglePlaylist(const ListValue* args); |
- void HandleShowPlaylist(const ListValue* args); |
- void HandleSetCurrentPlaylistOffset(const ListValue* args); |
- void HandleToggleFullscreen(const ListValue* args); |
- |
- const UrlVector& GetCurrentPlaylist(); |
- |
- int GetCurrentPlaylistOffset(); |
- void SetCurrentPlaylistOffset(int offset); |
- // Sets the playlist for playlist views, since the playlist is |
- // maintained by the mediaplayer itself. Offset is the item in the |
- // playlist which is either now playing, or should be played. |
- void SetCurrentPlaylist(const UrlVector& playlist, int offset); |
- |
- private: |
- // The current playlist of urls. |
- UrlVector current_playlist_; |
- // The offset into the current_playlist_ of the currently playing item. |
- int current_offset_; |
- // Indicator of if this handler is a playlist or a mediaplayer. |
- bool is_playlist_; |
- DISALLOW_COPY_AND_ASSIGN(MediaplayerHandler); |
-}; |
- |
-//////////////////////////////////////////////////////////////////////////////// |
-// |
-// MediaplayerHTMLSource |
-// |
-//////////////////////////////////////////////////////////////////////////////// |
- |
-MediaplayerUIHTMLSource::MediaplayerUIHTMLSource(bool is_playlist) |
- : DataSource(chrome::kChromeUIMediaplayerHost, MessageLoop::current()) { |
- is_playlist_ = is_playlist; |
-} |
- |
-void MediaplayerUIHTMLSource::StartDataRequest(const std::string& path, |
- bool is_incognito, |
- int request_id) { |
- DictionaryValue localized_strings; |
- // TODO(dhg): Fix the strings that are currently hardcoded so they |
- // use the localized versions. |
- localized_strings.SetString("errorstring", "Error Playing Back"); |
- |
- SetFontAndTextDirection(&localized_strings); |
- |
- std::string full_html; |
- |
- static const base::StringPiece mediaplayer_html( |
- ResourceBundle::GetSharedInstance().GetRawDataResource( |
- IDR_MEDIAPLAYER_HTML)); |
- |
- static const base::StringPiece playlist_html( |
- ResourceBundle::GetSharedInstance().GetRawDataResource( |
- IDR_MEDIAPLAYERPLAYLIST_HTML)); |
- |
- if (is_playlist_) { |
- full_html = jstemplate_builder::GetI18nTemplateHtml( |
- playlist_html, &localized_strings); |
- } else { |
- full_html = jstemplate_builder::GetI18nTemplateHtml( |
- mediaplayer_html, &localized_strings); |
- } |
- |
- scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); |
- html_bytes->data.resize(full_html.size()); |
- std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin()); |
- |
- SendResponse(request_id, html_bytes); |
-} |
- |
-//////////////////////////////////////////////////////////////////////////////// |
-// |
-// MediaplayerHandler |
-// |
-//////////////////////////////////////////////////////////////////////////////// |
-MediaplayerHandler::MediaplayerHandler(bool is_playlist) |
- : current_offset_(0), |
- is_playlist_(is_playlist) { |
-} |
- |
-MediaplayerHandler::~MediaplayerHandler() { |
-} |
- |
-WebUIMessageHandler* MediaplayerHandler::Attach(WebUI* web_ui) { |
- // Create our favicon data source. |
- Profile* profile = web_ui->GetProfile(); |
- profile->GetChromeURLDataManager()->AddDataSource( |
- new FaviconSource(profile, FaviconSource::FAVICON)); |
- |
- return WebUIMessageHandler::Attach(web_ui); |
-} |
- |
-void MediaplayerHandler::Init(bool is_playlist, TabContents* contents) { |
- MediaPlayer* player = MediaPlayer::GetInstance(); |
- if (!is_playlist) { |
- player->SetNewHandler(this, contents); |
- } else { |
- player->RegisterNewPlaylistHandler(this, contents); |
- } |
-} |
- |
-void MediaplayerHandler::RegisterMessages() { |
- web_ui_->RegisterMessageCallback("currentOffsetChanged", |
- NewCallback(this, &MediaplayerHandler::HandleCurrentOffsetChanged)); |
- web_ui_->RegisterMessageCallback("playbackError", |
- NewCallback(this, &MediaplayerHandler::HandlePlaybackError)); |
- web_ui_->RegisterMessageCallback("getCurrentPlaylist", |
- NewCallback(this, &MediaplayerHandler::HandleGetCurrentPlaylist)); |
- web_ui_->RegisterMessageCallback("togglePlaylist", |
- NewCallback(this, &MediaplayerHandler::HandleTogglePlaylist)); |
- web_ui_->RegisterMessageCallback("setCurrentPlaylistOffset", |
- NewCallback(this, &MediaplayerHandler::HandleSetCurrentPlaylistOffset)); |
- web_ui_->RegisterMessageCallback("toggleFullscreen", |
- NewCallback(this, &MediaplayerHandler::HandleToggleFullscreen)); |
- web_ui_->RegisterMessageCallback("showPlaylist", |
- NewCallback(this, &MediaplayerHandler::HandleShowPlaylist)); |
-} |
- |
-void MediaplayerHandler::GetPlaylistValue(ListValue& urls) { |
- for (size_t x = 0; x < current_playlist_.size(); x++) { |
- DictionaryValue* url_value = new DictionaryValue(); |
- url_value->SetString(kPropertyPath, current_playlist_[x].url.spec()); |
- url_value->SetBoolean(kPropertyError, current_playlist_[x].haderror); |
- urls.Append(url_value); |
- } |
-} |
- |
-void MediaplayerHandler::PlaybackMediaFile(const GURL& url) { |
- current_playlist_.push_back(MediaplayerHandler::MediaUrl(url)); |
- FirePlaylistChanged(url.spec(), true, current_playlist_.size() - 1); |
- MediaPlayer::GetInstance()->NotifyPlaylistChanged(); |
-} |
- |
-const MediaplayerHandler::UrlVector& MediaplayerHandler::GetCurrentPlaylist() { |
+const MediaPlayer::UrlVector& MediaPlayer::GetPlaylist() const { |
return current_playlist_; |
} |
-int MediaplayerHandler::GetCurrentPlaylistOffset() { |
- return current_offset_; |
-} |
- |
-void MediaplayerHandler::HandleToggleFullscreen(const ListValue* args) { |
- MediaPlayer::GetInstance()->ToggleFullscreen(); |
-} |
- |
-void MediaplayerHandler::HandleSetCurrentPlaylistOffset(const ListValue* args) { |
- int id; |
- CHECK(ExtractIntegerValue(args, &id)); |
- MediaPlayer::GetInstance()->SetPlaylistOffset(id); |
-} |
- |
-void MediaplayerHandler::FirePlaylistChanged(const std::string& path, |
- bool force, |
- int offset) { |
- DictionaryValue info_value; |
- ListValue urls; |
- GetPlaylistValue(urls); |
- info_value.SetString(kPropertyPath, path); |
- info_value.SetBoolean(kPropertyForce, force); |
- info_value.SetInteger(kPropertyOffset, offset); |
- web_ui_->CallJavascriptFunction("playlistChanged", info_value, urls); |
-} |
- |
-void MediaplayerHandler::SetCurrentPlaylistOffset(int offset) { |
- current_offset_ = offset; |
- FirePlaylistChanged(std::string(), true, current_offset_); |
-} |
- |
-void MediaplayerHandler::SetCurrentPlaylist( |
- const MediaplayerHandler::UrlVector& playlist, int offset) { |
- current_playlist_ = playlist; |
- current_offset_ = offset; |
- FirePlaylistChanged(std::string(), false, current_offset_); |
-} |
- |
-void MediaplayerHandler::EnqueueMediaFileUrl(const GURL& url) { |
- current_playlist_.push_back(MediaplayerHandler::MediaUrl(url)); |
- FirePlaylistChanged(url.spec(), false, current_offset_); |
- MediaPlayer::GetInstance()->NotifyPlaylistChanged(); |
-} |
- |
-void MediaplayerHandler::HandleCurrentOffsetChanged(const ListValue* args) { |
- CHECK(ExtractIntegerValue(args, ¤t_offset_)); |
- MediaPlayer::GetInstance()->NotifyPlaylistChanged(); |
-} |
- |
-void MediaplayerHandler::HandlePlaybackError(const ListValue* args) { |
- std::string error; |
- std::string url; |
- // Get path string. |
- if (args->GetString(0, &error)) |
- LOG(ERROR) << "Playback error" << error; |
- if (args->GetString(1, &url)) { |
- for (size_t x = 0; x < current_playlist_.size(); x++) { |
- if (current_playlist_[x].url == GURL(url)) { |
- current_playlist_[x].haderror = true; |
- } |
- } |
- FirePlaylistChanged(std::string(), false, current_offset_); |
- } |
-} |
- |
-void MediaplayerHandler::HandleGetCurrentPlaylist(const ListValue* args) { |
- FirePlaylistChanged(std::string(), false, current_offset_); |
-} |
- |
-void MediaplayerHandler::HandleTogglePlaylist(const ListValue* args) { |
- MediaPlayer::GetInstance()->TogglePlaylistWindowVisible(); |
-} |
- |
-void MediaplayerHandler::HandleShowPlaylist(const ListValue* args) { |
- MediaPlayer::GetInstance()->ShowPlaylistWindow(); |
+int MediaPlayer::GetPlaylistPosition() const { |
+ return current_position_; |
} |
//////////////////////////////////////////////////////////////////////////////// |
@@ -360,41 +83,44 @@ MediaPlayer* MediaPlayer::GetInstance() { |
void MediaPlayer::EnqueueMediaFile(Profile* profile, const FilePath& file_path, |
Browser* creator) { |
- static GURL origin_url(kMediaplayerURL); |
GURL url; |
if (!FileManagerUtil::ConvertFileToFileSystemUrl(profile, file_path, |
- origin_url, &url)) { |
+ GetOriginUrl(), &url)) { |
} |
EnqueueMediaFileUrl(url, creator); |
} |
void MediaPlayer::EnqueueMediaFileUrl(const GURL& url, Browser* creator) { |
- if (handler_ == NULL) { |
- unhandled_urls_.push_back(url); |
+ if (mediaplayer_browser_ == NULL) { |
PopupMediaPlayer(creator); |
- } else { |
- handler_->EnqueueMediaFileUrl(url); |
} |
+ EnqueueMediaFileUrl(url); |
+} |
+ |
+void MediaPlayer::EnqueueMediaFileUrl(const GURL& url) { |
+ current_playlist_.push_back(MediaUrl(url)); |
+ NotifyPlaylistChanged(); |
} |
void MediaPlayer::ForcePlayMediaFile(Profile* profile, |
const FilePath& file_path, |
Browser* creator) { |
- static GURL origin_url(kMediaplayerURL); |
GURL url; |
if (!FileManagerUtil::ConvertFileToFileSystemUrl(profile, file_path, |
- origin_url, &url)) { |
+ GetOriginUrl(), &url)) { |
+ return; |
} |
ForcePlayMediaURL(url, creator); |
} |
void MediaPlayer::ForcePlayMediaURL(const GURL& url, Browser* creator) { |
- if (handler_ == NULL) { |
- unhandled_urls_.push_back(url); |
+ if (mediaplayer_browser_ == NULL) { |
PopupMediaPlayer(creator); |
- } else { |
- handler_->PlaybackMediaFile(url); |
} |
+ current_playlist_.push_back(MediaUrl(url)); |
+ current_position_ = current_playlist_.size() - 1; |
+ pending_playback_request_ = true; |
+ NotifyPlaylistChanged(); |
} |
void MediaPlayer::TogglePlaylistWindowVisible() { |
@@ -417,98 +143,69 @@ void MediaPlayer::ClosePlaylistWindow() { |
} |
} |
-void MediaPlayer::SetPlaylistOffset(int offset) { |
- if (handler_) { |
- handler_->SetCurrentPlaylistOffset(offset); |
- } |
- if (playlist_) { |
- playlist_->SetCurrentPlaylistOffset(offset); |
+void MediaPlayer::SetPlaylistPosition(int position) { |
+ const int playlist_size = current_playlist_.size(); |
+ if (current_position_ < 0 || current_position_ > playlist_size) |
+ position = current_playlist_.size(); |
+ if (current_position_ != position) { |
+ current_position_ = position; |
+ NotifyPlaylistChanged(); |
} |
} |
-void MediaPlayer::SetNewHandler(MediaplayerHandler* handler, |
- TabContents* contents) { |
- handler_ = handler; |
- mediaplayer_tab_ = contents; |
- RegisterListeners(); |
- for (size_t x = 0; x < unhandled_urls_.size(); x++) { |
- handler_->EnqueueMediaFileUrl(unhandled_urls_[x]); |
+void MediaPlayer::SetPlaybackError(GURL const& url) { |
+ for (size_t x = 0; x < current_playlist_.size(); x++) { |
+ if (current_playlist_[x].url == url) { |
+ current_playlist_[x].haderror = true; |
+ } |
} |
- unhandled_urls_.clear(); |
+ NotifyPlaylistChanged(); |
} |
-void MediaPlayer::RegisterListeners() { |
- registrar_.RemoveAll(); |
- if (playlist_tab_) { |
- registrar_.Add(this, |
- NotificationType::TAB_CONTENTS_DESTROYED, |
- Source<TabContents>(playlist_tab_)); |
- } |
- if (mediaplayer_tab_) { |
- registrar_.Add(this, |
- NotificationType::TAB_CONTENTS_DESTROYED, |
- Source<TabContents>(mediaplayer_tab_)); |
- } |
-}; |
- |
void MediaPlayer::Observe(NotificationType type, |
const NotificationSource& source, |
const NotificationDetails& details) { |
- DCHECK(type == NotificationType::TAB_CONTENTS_DESTROYED); |
- if (Source<TabContents>(source).ptr() == mediaplayer_tab_) { |
- RemoveHandler(handler_); |
- RegisterListeners(); |
- ClosePlaylistWindow(); |
- } else if (Source<TabContents>(source).ptr() == playlist_tab_) { |
- RemovePlaylistHandler(playlist_); |
- RegisterListeners(); |
+ DCHECK(type == NotificationType::BROWSER_CLOSING); |
+ registrar_.Remove(this, |
+ NotificationType::BROWSER_CLOSING, |
+ source); |
+ if (Source<Browser>(source).ptr() == mediaplayer_browser_) { |
+ mediaplayer_browser_ = NULL; |
+ } else if (Source<Browser>(source).ptr() == playlist_browser_) { |
+ playlist_browser_ = NULL; |
} |
} |
-void MediaPlayer::RegisterNewPlaylistHandler(MediaplayerHandler* handler, |
- TabContents* contents) { |
- playlist_ = handler; |
- playlist_tab_ = contents; |
- RegisterListeners(); |
- NotifyPlaylistChanged(); |
+void MediaPlayer::NotifyPlaylistChanged() { |
+ ExtensionMediaPlayerEventRouter::GetInstance()->NotifyPlaylistChanged(); |
} |
-void MediaPlayer::RemovePlaylistHandler(MediaplayerHandler* handler) { |
- if (handler == playlist_) { |
- playlist_ = NULL; |
- playlist_browser_ = NULL; |
- playlist_tab_ = NULL; |
- } |
+bool MediaPlayer::GetPendingPlayRequestAndReset() { |
+ bool result = pending_playback_request_; |
+ pending_playback_request_ = false; |
+ return result; |
} |
-void MediaPlayer::NotifyPlaylistChanged() { |
- if (handler_ && playlist_) { |
- playlist_->SetCurrentPlaylist(handler_->GetCurrentPlaylist(), |
- handler_->GetCurrentPlaylistOffset()); |
- } |
+void MediaPlayer::SetPlaybackRequest() { |
+ pending_playback_request_ = true; |
} |
void MediaPlayer::ToggleFullscreen() { |
- if (handler_ && mediaplayer_browser_) { |
+ if (mediaplayer_browser_) { |
mediaplayer_browser_->ToggleFullscreenMode(); |
} |
} |
-void MediaPlayer::RemoveHandler(MediaplayerHandler* handler) { |
- if (handler == handler_) { |
- handler_ = NULL; |
- mediaplayer_browser_ = NULL; |
- mediaplayer_tab_ = NULL; |
- } |
-} |
- |
void MediaPlayer::PopupPlaylist(Browser* creator) { |
Profile* profile = BrowserList::GetLastActive()->profile(); |
playlist_browser_ = Browser::CreateForApp(Browser::TYPE_PANEL, |
kMediaPlayerAppName, |
gfx::Size(), |
profile); |
- playlist_browser_->AddSelectedTabWithURL(GURL(kMediaplayerPlaylistURL), |
+ registrar_.Add(this, |
+ NotificationType::BROWSER_CLOSING, |
+ Source<Browser>(playlist_browser_)); |
+ playlist_browser_->AddSelectedTabWithURL(GetMediaplayerPlaylistUrl(), |
PageTransition::LINK); |
playlist_browser_->window()->SetBounds(gfx::Rect(kPopupLeft, |
kPopupTop, |
@@ -530,6 +227,10 @@ void MediaPlayer::PopupMediaPlayer(Browser* creator) { |
kMediaPlayerAppName, |
gfx::Size(), |
profile); |
+ registrar_.Add(this, |
+ NotificationType::BROWSER_CLOSING, |
+ Source<Browser>(mediaplayer_browser_)); |
+ |
#if defined(OS_CHROMEOS) |
// Since we are on chromeos, popups should be a PanelBrowserView, |
// so we can just cast it. |
@@ -542,7 +243,7 @@ void MediaPlayer::PopupMediaPlayer(Browser* creator) { |
view->SetCreatorView(creatorview); |
} |
#endif |
- mediaplayer_browser_->AddSelectedTabWithURL(GURL(kMediaplayerURL), |
+ mediaplayer_browser_->AddSelectedTabWithURL(GetMediaPlayerUrl(), |
PageTransition::LINK); |
mediaplayer_browser_->window()->SetBounds(gfx::Rect(kPopupLeft, |
kPopupTop, |
@@ -587,38 +288,24 @@ net::URLRequestJob* MediaPlayer::MaybeInterceptResponse( |
return NULL; |
} |
+GURL MediaPlayer::GetOriginUrl() const { |
+ return FileManagerUtil::GetMediaPlayerUrl().GetOrigin(); |
+} |
+ |
+GURL MediaPlayer::GetMediaplayerPlaylistUrl() const { |
+ return FileManagerUtil::GetMediaPlayerPlaylistUrl(); |
+} |
+ |
+GURL MediaPlayer::GetMediaPlayerUrl() const { |
+ return FileManagerUtil::GetMediaPlayerUrl(); |
+} |
+ |
MediaPlayer::MediaPlayer() |
- : handler_(NULL), |
- playlist_(NULL), |
+ : current_position_(0), |
+ pending_playback_request_(false), |
playlist_browser_(NULL), |
- mediaplayer_browser_(NULL), |
- mediaplayer_tab_(NULL), |
- playlist_tab_(NULL) { |
+ mediaplayer_browser_(NULL) { |
for (size_t i = 0; i < arraysize(supported_mime_type_list); ++i) { |
supported_mime_types_.insert(supported_mime_type_list[i]); |
} |
}; |
- |
-//////////////////////////////////////////////////////////////////////////////// |
-// |
-// MediaplayerUIContents |
-// |
-//////////////////////////////////////////////////////////////////////////////// |
- |
-MediaplayerUI::MediaplayerUI(TabContents* contents) : WebUI(contents) { |
- const GURL& url = contents->GetURL(); |
- bool is_playlist = (url.ref() == "playlist"); |
- MediaplayerHandler* handler = new MediaplayerHandler(is_playlist); |
- AddMessageHandler(handler->Attach(this)); |
- if (is_playlist) { |
- handler->Init(true, contents); |
- } else { |
- handler->Init(false, contents); |
- } |
- |
- MediaplayerUIHTMLSource* html_source = |
- new MediaplayerUIHTMLSource(is_playlist); |
- |
- // Set up the chrome://mediaplayer/ source. |
- contents->profile()->GetChromeURLDataManager()->AddDataSource(html_source); |
-} |