OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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. |
| 4 |
| 5 #include "chrome/browser/chromeos/media/media_player.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/memory/singleton.h" |
| 10 #include "base/memory/weak_ptr.h" |
| 11 #include "base/message_loop.h" |
| 12 #include "base/path_service.h" |
| 13 #include "base/string_piece.h" |
| 14 #include "base/string_util.h" |
| 15 #include "base/threading/thread.h" |
| 16 #include "base/time.h" |
| 17 #include "base/values.h" |
| 18 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 19 #include "chrome/browser/chromeos/extensions/media_player_event_router.h" |
| 20 #include "chrome/browser/download/download_manager.h" |
| 21 #include "chrome/browser/download/download_util.h" |
| 22 #include "chrome/browser/extensions/file_manager_util.h" |
| 23 #include "chrome/browser/history/history_types.h" |
| 24 #include "chrome/browser/profiles/profile.h" |
| 25 #include "chrome/browser/tabs/tab_strip_model.h" |
| 26 #include "chrome/browser/ui/browser.h" |
| 27 #include "chrome/browser/ui/browser_list.h" |
| 28 #include "chrome/browser/ui/browser_window.h" |
| 29 #include "chrome/browser/ui/webui/favicon_source.h" |
| 30 #include "chrome/common/chrome_paths.h" |
| 31 #include "chrome/common/chrome_switches.h" |
| 32 #include "chrome/common/jstemplate_builder.h" |
| 33 #include "chrome/common/net/url_fetcher.h" |
| 34 #include "chrome/common/time_format.h" |
| 35 #include "chrome/common/url_constants.h" |
| 36 #include "content/browser/browser_thread.h" |
| 37 #include "content/browser/tab_contents/tab_contents.h" |
| 38 #include "content/browser/user_metrics.h" |
| 39 #include "grit/browser_resources.h" |
| 40 #include "grit/chromium_strings.h" |
| 41 #include "grit/generated_resources.h" |
| 42 #include "grit/locale_settings.h" |
| 43 #include "net/base/escape.h" |
| 44 #include "net/base/load_flags.h" |
| 45 #include "net/url_request/url_request_job.h" |
| 46 #include "ui/base/resource/resource_bundle.h" |
| 47 |
| 48 #if defined(OS_CHROMEOS) |
| 49 #include "chrome/browser/chromeos/frame/panel_browser_view.h" |
| 50 #endif |
| 51 |
| 52 static const char* kMediaPlayerAppName = "mediaplayer"; |
| 53 static const int kPopupLeft = 0; |
| 54 static const int kPopupTop = 0; |
| 55 static const int kPopupWidth = 350; |
| 56 static const int kPopupHeight = 300; |
| 57 |
| 58 const MediaPlayer::UrlVector& MediaPlayer::GetPlaylist() const { |
| 59 return current_playlist_; |
| 60 } |
| 61 |
| 62 int MediaPlayer::GetPlaylistPosition() const { |
| 63 return current_position_; |
| 64 } |
| 65 |
| 66 //////////////////////////////////////////////////////////////////////////////// |
| 67 // |
| 68 // Mediaplayer |
| 69 // |
| 70 //////////////////////////////////////////////////////////////////////////////// |
| 71 |
| 72 // Allows InvokeLater without adding refcounting. This class is a Singleton and |
| 73 // won't be deleted until it's last InvokeLater is run. |
| 74 DISABLE_RUNNABLE_METHOD_REFCOUNT(MediaPlayer); |
| 75 |
| 76 MediaPlayer::~MediaPlayer() { |
| 77 } |
| 78 |
| 79 // static |
| 80 MediaPlayer* MediaPlayer::GetInstance() { |
| 81 return Singleton<MediaPlayer>::get(); |
| 82 } |
| 83 |
| 84 void MediaPlayer::EnqueueMediaFile(Profile* profile, const FilePath& file_path, |
| 85 Browser* creator) { |
| 86 GURL url; |
| 87 if (!FileManagerUtil::ConvertFileToFileSystemUrl(profile, file_path, |
| 88 GetOriginUrl(), &url)) { |
| 89 } |
| 90 EnqueueMediaFileUrl(url, creator); |
| 91 } |
| 92 |
| 93 void MediaPlayer::EnqueueMediaFileUrl(const GURL& url, Browser* creator) { |
| 94 if (mediaplayer_browser_ == NULL) { |
| 95 PopupMediaPlayer(creator); |
| 96 } |
| 97 EnqueueMediaFileUrl(url); |
| 98 } |
| 99 |
| 100 void MediaPlayer::EnqueueMediaFileUrl(const GURL& url) { |
| 101 current_playlist_.push_back(MediaUrl(url)); |
| 102 NotifyPlaylistChanged(); |
| 103 } |
| 104 |
| 105 void MediaPlayer::ForcePlayMediaFile(Profile* profile, |
| 106 const FilePath& file_path, |
| 107 Browser* creator) { |
| 108 GURL url; |
| 109 if (!FileManagerUtil::ConvertFileToFileSystemUrl(profile, file_path, |
| 110 GetOriginUrl(), &url)) { |
| 111 return; |
| 112 } |
| 113 ForcePlayMediaURL(url, creator); |
| 114 } |
| 115 |
| 116 void MediaPlayer::ForcePlayMediaURL(const GURL& url, Browser* creator) { |
| 117 if (mediaplayer_browser_ == NULL) { |
| 118 PopupMediaPlayer(creator); |
| 119 } |
| 120 current_playlist_.push_back(MediaUrl(url)); |
| 121 current_position_ = current_playlist_.size() - 1; |
| 122 pending_playback_request_ = true; |
| 123 NotifyPlaylistChanged(); |
| 124 } |
| 125 |
| 126 void MediaPlayer::TogglePlaylistWindowVisible() { |
| 127 if (playlist_browser_) { |
| 128 ClosePlaylistWindow(); |
| 129 } else { |
| 130 ShowPlaylistWindow(); |
| 131 } |
| 132 } |
| 133 |
| 134 void MediaPlayer::ShowPlaylistWindow() { |
| 135 if (playlist_browser_ == NULL) { |
| 136 PopupPlaylist(NULL); |
| 137 } |
| 138 } |
| 139 |
| 140 void MediaPlayer::ClosePlaylistWindow() { |
| 141 if (playlist_browser_ != NULL) { |
| 142 playlist_browser_->window()->Close(); |
| 143 } |
| 144 } |
| 145 |
| 146 void MediaPlayer::SetPlaylistPosition(int position) { |
| 147 const int playlist_size = current_playlist_.size(); |
| 148 if (current_position_ < 0 || current_position_ > playlist_size) |
| 149 position = current_playlist_.size(); |
| 150 if (current_position_ != position) { |
| 151 current_position_ = position; |
| 152 NotifyPlaylistChanged(); |
| 153 } |
| 154 } |
| 155 |
| 156 void MediaPlayer::SetPlaybackError(GURL const& url) { |
| 157 for (size_t x = 0; x < current_playlist_.size(); x++) { |
| 158 if (current_playlist_[x].url == url) { |
| 159 current_playlist_[x].haderror = true; |
| 160 } |
| 161 } |
| 162 NotifyPlaylistChanged(); |
| 163 } |
| 164 |
| 165 void MediaPlayer::Observe(NotificationType type, |
| 166 const NotificationSource& source, |
| 167 const NotificationDetails& details) { |
| 168 DCHECK(type == NotificationType::BROWSER_CLOSING); |
| 169 registrar_.Remove(this, |
| 170 NotificationType::BROWSER_CLOSING, |
| 171 source); |
| 172 if (Source<Browser>(source).ptr() == mediaplayer_browser_) { |
| 173 mediaplayer_browser_ = NULL; |
| 174 } else if (Source<Browser>(source).ptr() == playlist_browser_) { |
| 175 playlist_browser_ = NULL; |
| 176 } |
| 177 } |
| 178 |
| 179 void MediaPlayer::NotifyPlaylistChanged() { |
| 180 ExtensionMediaPlayerEventRouter::GetInstance()->NotifyPlaylistChanged(); |
| 181 } |
| 182 |
| 183 bool MediaPlayer::GetPendingPlayRequestAndReset() { |
| 184 bool result = pending_playback_request_; |
| 185 pending_playback_request_ = false; |
| 186 return result; |
| 187 } |
| 188 |
| 189 void MediaPlayer::SetPlaybackRequest() { |
| 190 pending_playback_request_ = true; |
| 191 } |
| 192 |
| 193 void MediaPlayer::ToggleFullscreen() { |
| 194 if (mediaplayer_browser_) { |
| 195 mediaplayer_browser_->ToggleFullscreenMode(); |
| 196 } |
| 197 } |
| 198 |
| 199 void MediaPlayer::PopupPlaylist(Browser* creator) { |
| 200 Profile* profile = BrowserList::GetLastActive()->profile(); |
| 201 playlist_browser_ = Browser::CreateForApp(Browser::TYPE_PANEL, |
| 202 kMediaPlayerAppName, |
| 203 gfx::Size(), |
| 204 profile); |
| 205 registrar_.Add(this, |
| 206 NotificationType::BROWSER_CLOSING, |
| 207 Source<Browser>(playlist_browser_)); |
| 208 playlist_browser_->AddSelectedTabWithURL(GetMediaplayerPlaylistUrl(), |
| 209 PageTransition::LINK); |
| 210 playlist_browser_->window()->SetBounds(gfx::Rect(kPopupLeft, |
| 211 kPopupTop, |
| 212 kPopupWidth, |
| 213 kPopupHeight)); |
| 214 playlist_browser_->window()->Show(); |
| 215 } |
| 216 |
| 217 void MediaPlayer::PopupMediaPlayer(Browser* creator) { |
| 218 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 219 BrowserThread::PostTask( |
| 220 BrowserThread::UI, FROM_HERE, |
| 221 NewRunnableMethod(this, &MediaPlayer::PopupMediaPlayer, |
| 222 static_cast<Browser*>(NULL))); |
| 223 return; |
| 224 } |
| 225 Profile* profile = BrowserList::GetLastActive()->profile(); |
| 226 mediaplayer_browser_ = Browser::CreateForApp(Browser::TYPE_PANEL, |
| 227 kMediaPlayerAppName, |
| 228 gfx::Size(), |
| 229 profile); |
| 230 registrar_.Add(this, |
| 231 NotificationType::BROWSER_CLOSING, |
| 232 Source<Browser>(mediaplayer_browser_)); |
| 233 |
| 234 #if defined(OS_CHROMEOS) |
| 235 // Since we are on chromeos, popups should be a PanelBrowserView, |
| 236 // so we can just cast it. |
| 237 if (creator) { |
| 238 chromeos::PanelBrowserView* creatorview = |
| 239 static_cast<chromeos::PanelBrowserView*>(creator->window()); |
| 240 chromeos::PanelBrowserView* view = |
| 241 static_cast<chromeos::PanelBrowserView*>( |
| 242 mediaplayer_browser_->window()); |
| 243 view->SetCreatorView(creatorview); |
| 244 } |
| 245 #endif |
| 246 mediaplayer_browser_->AddSelectedTabWithURL(GetMediaPlayerUrl(), |
| 247 PageTransition::LINK); |
| 248 mediaplayer_browser_->window()->SetBounds(gfx::Rect(kPopupLeft, |
| 249 kPopupTop, |
| 250 kPopupWidth, |
| 251 kPopupHeight)); |
| 252 mediaplayer_browser_->window()->Show(); |
| 253 } |
| 254 |
| 255 net::URLRequestJob* MediaPlayer::MaybeIntercept(net::URLRequest* request) { |
| 256 // Don't attempt to intercept here as we want to wait until the mime |
| 257 // type is fully determined. |
| 258 return NULL; |
| 259 } |
| 260 |
| 261 // This is the list of mime types currently supported by the Google |
| 262 // Document Viewer. |
| 263 static const char* const supported_mime_type_list[] = { |
| 264 "audio/mpeg", |
| 265 "video/mp4", |
| 266 "audio/mp3" |
| 267 }; |
| 268 |
| 269 net::URLRequestJob* MediaPlayer::MaybeInterceptResponse( |
| 270 net::URLRequest* request) { |
| 271 // Do not intercept this request if it is a download. |
| 272 if (request->load_flags() & net::LOAD_IS_DOWNLOAD) { |
| 273 return NULL; |
| 274 } |
| 275 |
| 276 std::string mime_type; |
| 277 request->GetMimeType(&mime_type); |
| 278 // If it is in our list of known URLs, enqueue the url then |
| 279 // Cancel the request so the mediaplayer can handle it when |
| 280 // it hits it in the playlist. |
| 281 if (supported_mime_types_.find(mime_type) != supported_mime_types_.end()) { |
| 282 if (request->referrer() != chrome::kChromeUIMediaplayerURL && |
| 283 !request->referrer().empty()) { |
| 284 EnqueueMediaFileUrl(request->url(), NULL); |
| 285 request->Cancel(); |
| 286 } |
| 287 } |
| 288 return NULL; |
| 289 } |
| 290 |
| 291 GURL MediaPlayer::GetOriginUrl() const { |
| 292 return FileManagerUtil::GetMediaPlayerUrl().GetOrigin(); |
| 293 } |
| 294 |
| 295 GURL MediaPlayer::GetMediaplayerPlaylistUrl() const { |
| 296 return FileManagerUtil::GetMediaPlayerPlaylistUrl(); |
| 297 } |
| 298 |
| 299 GURL MediaPlayer::GetMediaPlayerUrl() const { |
| 300 return FileManagerUtil::GetMediaPlayerUrl(); |
| 301 } |
| 302 |
| 303 MediaPlayer::MediaPlayer() |
| 304 : current_position_(0), |
| 305 pending_playback_request_(false), |
| 306 playlist_browser_(NULL), |
| 307 mediaplayer_browser_(NULL) { |
| 308 for (size_t i = 0; i < arraysize(supported_mime_type_list); ++i) { |
| 309 supported_mime_types_.insert(supported_mime_type_list[i]); |
| 310 } |
| 311 }; |
OLD | NEW |