Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/ui/webui/mediaplayer_ui.h" | 5 #include "chrome/browser/ui/webui/mediaplayer_ui.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/singleton.h" | 9 #include "base/memory/singleton.h" |
| 10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 13 #include "base/string_piece.h" | 13 #include "base/string_piece.h" |
| 14 #include "base/string_util.h" | 14 #include "base/string_util.h" |
| 15 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
| 16 #include "base/time.h" | 16 #include "base/time.h" |
| 17 #include "base/values.h" | 17 #include "base/values.h" |
| 18 #include "chrome/browser/bookmarks/bookmark_model.h" | 18 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 19 #include "chrome/browser/chromeos/extensions/media_player_event_router.h" | |
| 19 #include "chrome/browser/download/download_manager.h" | 20 #include "chrome/browser/download/download_manager.h" |
| 20 #include "chrome/browser/download/download_util.h" | 21 #include "chrome/browser/download/download_util.h" |
| 21 #include "chrome/browser/extensions/file_manager_util.h" | 22 #include "chrome/browser/extensions/file_manager_util.h" |
| 22 #include "chrome/browser/history/history_types.h" | 23 #include "chrome/browser/history/history_types.h" |
| 23 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
| 24 #include "chrome/browser/tabs/tab_strip_model.h" | 25 #include "chrome/browser/tabs/tab_strip_model.h" |
| 25 #include "chrome/browser/ui/browser.h" | 26 #include "chrome/browser/ui/browser.h" |
| 26 #include "chrome/browser/ui/browser_list.h" | 27 #include "chrome/browser/ui/browser_list.h" |
| 27 #include "chrome/browser/ui/browser_window.h" | 28 #include "chrome/browser/ui/browser_window.h" |
| 28 #include "chrome/browser/ui/webui/favicon_source.h" | 29 #include "chrome/browser/ui/webui/favicon_source.h" |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 41 #include "grit/locale_settings.h" | 42 #include "grit/locale_settings.h" |
| 42 #include "net/base/escape.h" | 43 #include "net/base/escape.h" |
| 43 #include "net/base/load_flags.h" | 44 #include "net/base/load_flags.h" |
| 44 #include "net/url_request/url_request_job.h" | 45 #include "net/url_request/url_request_job.h" |
| 45 #include "ui/base/resource/resource_bundle.h" | 46 #include "ui/base/resource/resource_bundle.h" |
| 46 | 47 |
| 47 #if defined(OS_CHROMEOS) | 48 #if defined(OS_CHROMEOS) |
| 48 #include "chrome/browser/chromeos/frame/panel_browser_view.h" | 49 #include "chrome/browser/chromeos/frame/panel_browser_view.h" |
| 49 #endif | 50 #endif |
| 50 | 51 |
| 51 static const char kPropertyPath[] = "path"; | |
| 52 static const char kPropertyForce[] = "force"; | |
| 53 static const char kPropertyOffset[] = "currentOffset"; | |
| 54 static const char kPropertyError[] = "error"; | |
| 55 | |
| 56 static const char* kMediaplayerURL = "chrome://mediaplayer"; | |
| 57 static const char* kMediaplayerPlaylistURL = "chrome://mediaplayer#playlist"; | |
| 58 static const char* kMediaPlayerAppName = "mediaplayer"; | 52 static const char* kMediaPlayerAppName = "mediaplayer"; |
| 59 static const int kPopupLeft = 0; | 53 static const int kPopupLeft = 0; |
| 60 static const int kPopupTop = 0; | 54 static const int kPopupTop = 0; |
| 61 static const int kPopupWidth = 350; | 55 static const int kPopupWidth = 350; |
| 62 static const int kPopupHeight = 300; | 56 static const int kPopupHeight = 300; |
| 63 | 57 |
| 64 class MediaplayerUIHTMLSource : public ChromeURLDataManager::DataSource { | 58 const MediaPlayer::UrlVector& MediaPlayer::GetPlaylist() const { |
| 65 public: | 59 return current_playlist_; |
| 66 explicit MediaplayerUIHTMLSource(bool is_playlist); | |
| 67 | |
| 68 // Called when the network layer has requested a resource underneath | |
| 69 // the path we registered. | |
| 70 virtual void StartDataRequest(const std::string& path, | |
| 71 bool is_incognito, | |
| 72 int request_id); | |
| 73 virtual std::string GetMimeType(const std::string&) const { | |
| 74 return "text/html"; | |
| 75 } | |
| 76 | |
| 77 private: | |
| 78 ~MediaplayerUIHTMLSource() {} | |
| 79 bool is_playlist_; | |
| 80 | |
| 81 DISALLOW_COPY_AND_ASSIGN(MediaplayerUIHTMLSource); | |
| 82 }; | |
| 83 | |
| 84 // The handler for Javascript messages related to the "mediaplayer" view. | |
| 85 class MediaplayerHandler : public WebUIMessageHandler, | |
| 86 public base::SupportsWeakPtr<MediaplayerHandler> { | |
| 87 public: | |
| 88 | |
| 89 struct MediaUrl { | |
| 90 MediaUrl() {} | |
| 91 explicit MediaUrl(const GURL& newurl) | |
| 92 : url(newurl), | |
| 93 haderror(false) {} | |
| 94 GURL url; | |
| 95 bool haderror; | |
| 96 }; | |
| 97 typedef std::vector<MediaUrl> UrlVector; | |
| 98 | |
| 99 explicit MediaplayerHandler(bool is_playlist); | |
| 100 | |
| 101 virtual ~MediaplayerHandler(); | |
| 102 | |
| 103 // Init work after Attach. | |
| 104 void Init(bool is_playlist, TabContents* contents); | |
| 105 | |
| 106 // WebUIMessageHandler implementation. | |
| 107 virtual WebUIMessageHandler* Attach(WebUI* web_ui); | |
| 108 virtual void RegisterMessages(); | |
| 109 | |
| 110 // Callback for the "currentOffsetChanged" message. | |
| 111 void HandleCurrentOffsetChanged(const ListValue* args); | |
| 112 | |
| 113 void FirePlaylistChanged(const std::string& path, | |
| 114 bool force, | |
| 115 int offset); | |
| 116 | |
| 117 void PlaybackMediaFile(const GURL& url); | |
| 118 | |
| 119 void EnqueueMediaFileUrl(const GURL& url); | |
| 120 | |
| 121 void GetPlaylistValue(ListValue& args); | |
| 122 | |
| 123 // Callback for the "playbackError" message. | |
| 124 void HandlePlaybackError(const ListValue* args); | |
| 125 | |
| 126 // Callback for the "getCurrentPlaylist" message. | |
| 127 void HandleGetCurrentPlaylist(const ListValue* args); | |
| 128 | |
| 129 void HandleTogglePlaylist(const ListValue* args); | |
| 130 void HandleShowPlaylist(const ListValue* args); | |
| 131 void HandleSetCurrentPlaylistOffset(const ListValue* args); | |
| 132 void HandleToggleFullscreen(const ListValue* args); | |
| 133 | |
| 134 const UrlVector& GetCurrentPlaylist(); | |
| 135 | |
| 136 int GetCurrentPlaylistOffset(); | |
| 137 void SetCurrentPlaylistOffset(int offset); | |
| 138 // Sets the playlist for playlist views, since the playlist is | |
| 139 // maintained by the mediaplayer itself. Offset is the item in the | |
| 140 // playlist which is either now playing, or should be played. | |
| 141 void SetCurrentPlaylist(const UrlVector& playlist, int offset); | |
| 142 | |
| 143 private: | |
| 144 // The current playlist of urls. | |
| 145 UrlVector current_playlist_; | |
| 146 // The offset into the current_playlist_ of the currently playing item. | |
| 147 int current_offset_; | |
| 148 // Indicator of if this handler is a playlist or a mediaplayer. | |
| 149 bool is_playlist_; | |
| 150 DISALLOW_COPY_AND_ASSIGN(MediaplayerHandler); | |
| 151 }; | |
| 152 | |
| 153 //////////////////////////////////////////////////////////////////////////////// | |
| 154 // | |
| 155 // MediaplayerHTMLSource | |
| 156 // | |
| 157 //////////////////////////////////////////////////////////////////////////////// | |
| 158 | |
| 159 MediaplayerUIHTMLSource::MediaplayerUIHTMLSource(bool is_playlist) | |
| 160 : DataSource(chrome::kChromeUIMediaplayerHost, MessageLoop::current()) { | |
| 161 is_playlist_ = is_playlist; | |
| 162 } | 60 } |
| 163 | 61 |
| 164 void MediaplayerUIHTMLSource::StartDataRequest(const std::string& path, | 62 int MediaPlayer::GetPlaylistPosition() const { |
| 165 bool is_incognito, | 63 return current_position_; |
| 166 int request_id) { | |
| 167 DictionaryValue localized_strings; | |
| 168 // TODO(dhg): Fix the strings that are currently hardcoded so they | |
| 169 // use the localized versions. | |
| 170 localized_strings.SetString("errorstring", "Error Playing Back"); | |
| 171 | |
| 172 SetFontAndTextDirection(&localized_strings); | |
| 173 | |
| 174 std::string full_html; | |
| 175 | |
| 176 static const base::StringPiece mediaplayer_html( | |
| 177 ResourceBundle::GetSharedInstance().GetRawDataResource( | |
| 178 IDR_MEDIAPLAYER_HTML)); | |
| 179 | |
| 180 static const base::StringPiece playlist_html( | |
| 181 ResourceBundle::GetSharedInstance().GetRawDataResource( | |
| 182 IDR_MEDIAPLAYERPLAYLIST_HTML)); | |
| 183 | |
| 184 if (is_playlist_) { | |
| 185 full_html = jstemplate_builder::GetI18nTemplateHtml( | |
| 186 playlist_html, &localized_strings); | |
| 187 } else { | |
| 188 full_html = jstemplate_builder::GetI18nTemplateHtml( | |
| 189 mediaplayer_html, &localized_strings); | |
| 190 } | |
| 191 | |
| 192 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); | |
| 193 html_bytes->data.resize(full_html.size()); | |
| 194 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin()); | |
| 195 | |
| 196 SendResponse(request_id, html_bytes); | |
| 197 } | 64 } |
| 198 | 65 |
| 199 //////////////////////////////////////////////////////////////////////////////// | 66 //////////////////////////////////////////////////////////////////////////////// |
| 200 // | |
| 201 // MediaplayerHandler | |
| 202 // | |
| 203 //////////////////////////////////////////////////////////////////////////////// | |
| 204 MediaplayerHandler::MediaplayerHandler(bool is_playlist) | |
| 205 : current_offset_(0), | |
| 206 is_playlist_(is_playlist) { | |
| 207 } | |
| 208 | |
| 209 MediaplayerHandler::~MediaplayerHandler() { | |
| 210 } | |
| 211 | |
| 212 WebUIMessageHandler* MediaplayerHandler::Attach(WebUI* web_ui) { | |
| 213 // Create our favicon data source. | |
| 214 Profile* profile = web_ui->GetProfile(); | |
| 215 profile->GetChromeURLDataManager()->AddDataSource( | |
| 216 new FaviconSource(profile, FaviconSource::FAVICON)); | |
| 217 | |
| 218 return WebUIMessageHandler::Attach(web_ui); | |
| 219 } | |
| 220 | |
| 221 void MediaplayerHandler::Init(bool is_playlist, TabContents* contents) { | |
| 222 MediaPlayer* player = MediaPlayer::GetInstance(); | |
| 223 if (!is_playlist) { | |
| 224 player->SetNewHandler(this, contents); | |
| 225 } else { | |
| 226 player->RegisterNewPlaylistHandler(this, contents); | |
| 227 } | |
| 228 } | |
| 229 | |
| 230 void MediaplayerHandler::RegisterMessages() { | |
| 231 web_ui_->RegisterMessageCallback("currentOffsetChanged", | |
| 232 NewCallback(this, &MediaplayerHandler::HandleCurrentOffsetChanged)); | |
| 233 web_ui_->RegisterMessageCallback("playbackError", | |
| 234 NewCallback(this, &MediaplayerHandler::HandlePlaybackError)); | |
| 235 web_ui_->RegisterMessageCallback("getCurrentPlaylist", | |
| 236 NewCallback(this, &MediaplayerHandler::HandleGetCurrentPlaylist)); | |
| 237 web_ui_->RegisterMessageCallback("togglePlaylist", | |
| 238 NewCallback(this, &MediaplayerHandler::HandleTogglePlaylist)); | |
| 239 web_ui_->RegisterMessageCallback("setCurrentPlaylistOffset", | |
| 240 NewCallback(this, &MediaplayerHandler::HandleSetCurrentPlaylistOffset)); | |
| 241 web_ui_->RegisterMessageCallback("toggleFullscreen", | |
| 242 NewCallback(this, &MediaplayerHandler::HandleToggleFullscreen)); | |
| 243 web_ui_->RegisterMessageCallback("showPlaylist", | |
| 244 NewCallback(this, &MediaplayerHandler::HandleShowPlaylist)); | |
| 245 } | |
| 246 | |
| 247 void MediaplayerHandler::GetPlaylistValue(ListValue& urls) { | |
| 248 for (size_t x = 0; x < current_playlist_.size(); x++) { | |
| 249 DictionaryValue* url_value = new DictionaryValue(); | |
| 250 url_value->SetString(kPropertyPath, current_playlist_[x].url.spec()); | |
| 251 url_value->SetBoolean(kPropertyError, current_playlist_[x].haderror); | |
| 252 urls.Append(url_value); | |
| 253 } | |
| 254 } | |
| 255 | |
| 256 void MediaplayerHandler::PlaybackMediaFile(const GURL& url) { | |
| 257 current_playlist_.push_back(MediaplayerHandler::MediaUrl(url)); | |
| 258 FirePlaylistChanged(url.spec(), true, current_playlist_.size() - 1); | |
| 259 MediaPlayer::GetInstance()->NotifyPlaylistChanged(); | |
| 260 } | |
| 261 | |
| 262 const MediaplayerHandler::UrlVector& MediaplayerHandler::GetCurrentPlaylist() { | |
| 263 return current_playlist_; | |
| 264 } | |
| 265 | |
| 266 int MediaplayerHandler::GetCurrentPlaylistOffset() { | |
| 267 return current_offset_; | |
| 268 } | |
| 269 | |
| 270 void MediaplayerHandler::HandleToggleFullscreen(const ListValue* args) { | |
| 271 MediaPlayer::GetInstance()->ToggleFullscreen(); | |
| 272 } | |
| 273 | |
| 274 void MediaplayerHandler::HandleSetCurrentPlaylistOffset(const ListValue* args) { | |
| 275 int id; | |
| 276 CHECK(ExtractIntegerValue(args, &id)); | |
| 277 MediaPlayer::GetInstance()->SetPlaylistOffset(id); | |
| 278 } | |
| 279 | |
| 280 void MediaplayerHandler::FirePlaylistChanged(const std::string& path, | |
| 281 bool force, | |
| 282 int offset) { | |
| 283 DictionaryValue info_value; | |
| 284 ListValue urls; | |
| 285 GetPlaylistValue(urls); | |
| 286 info_value.SetString(kPropertyPath, path); | |
| 287 info_value.SetBoolean(kPropertyForce, force); | |
| 288 info_value.SetInteger(kPropertyOffset, offset); | |
| 289 web_ui_->CallJavascriptFunction("playlistChanged", info_value, urls); | |
| 290 } | |
| 291 | |
| 292 void MediaplayerHandler::SetCurrentPlaylistOffset(int offset) { | |
| 293 current_offset_ = offset; | |
| 294 FirePlaylistChanged(std::string(), true, current_offset_); | |
| 295 } | |
| 296 | |
| 297 void MediaplayerHandler::SetCurrentPlaylist( | |
| 298 const MediaplayerHandler::UrlVector& playlist, int offset) { | |
| 299 current_playlist_ = playlist; | |
| 300 current_offset_ = offset; | |
| 301 FirePlaylistChanged(std::string(), false, current_offset_); | |
| 302 } | |
| 303 | |
| 304 void MediaplayerHandler::EnqueueMediaFileUrl(const GURL& url) { | |
| 305 current_playlist_.push_back(MediaplayerHandler::MediaUrl(url)); | |
| 306 FirePlaylistChanged(url.spec(), false, current_offset_); | |
| 307 MediaPlayer::GetInstance()->NotifyPlaylistChanged(); | |
| 308 } | |
| 309 | |
| 310 void MediaplayerHandler::HandleCurrentOffsetChanged(const ListValue* args) { | |
| 311 CHECK(ExtractIntegerValue(args, ¤t_offset_)); | |
| 312 MediaPlayer::GetInstance()->NotifyPlaylistChanged(); | |
| 313 } | |
| 314 | |
| 315 void MediaplayerHandler::HandlePlaybackError(const ListValue* args) { | |
| 316 std::string error; | |
| 317 std::string url; | |
| 318 // Get path string. | |
| 319 if (args->GetString(0, &error)) | |
| 320 LOG(ERROR) << "Playback error" << error; | |
| 321 if (args->GetString(1, &url)) { | |
| 322 for (size_t x = 0; x < current_playlist_.size(); x++) { | |
| 323 if (current_playlist_[x].url == GURL(url)) { | |
| 324 current_playlist_[x].haderror = true; | |
| 325 } | |
| 326 } | |
| 327 FirePlaylistChanged(std::string(), false, current_offset_); | |
| 328 } | |
| 329 } | |
| 330 | |
| 331 void MediaplayerHandler::HandleGetCurrentPlaylist(const ListValue* args) { | |
| 332 FirePlaylistChanged(std::string(), false, current_offset_); | |
| 333 } | |
| 334 | |
| 335 void MediaplayerHandler::HandleTogglePlaylist(const ListValue* args) { | |
| 336 MediaPlayer::GetInstance()->TogglePlaylistWindowVisible(); | |
| 337 } | |
| 338 | |
| 339 void MediaplayerHandler::HandleShowPlaylist(const ListValue* args) { | |
| 340 MediaPlayer::GetInstance()->ShowPlaylistWindow(); | |
| 341 } | |
| 342 | |
| 343 //////////////////////////////////////////////////////////////////////////////// | |
| 344 // | 67 // |
| 345 // Mediaplayer | 68 // Mediaplayer |
| 346 // | 69 // |
| 347 //////////////////////////////////////////////////////////////////////////////// | 70 //////////////////////////////////////////////////////////////////////////////// |
| 348 | 71 |
| 349 // Allows InvokeLater without adding refcounting. This class is a Singleton and | 72 // Allows InvokeLater without adding refcounting. This class is a Singleton and |
| 350 // won't be deleted until it's last InvokeLater is run. | 73 // won't be deleted until it's last InvokeLater is run. |
| 351 DISABLE_RUNNABLE_METHOD_REFCOUNT(MediaPlayer); | 74 DISABLE_RUNNABLE_METHOD_REFCOUNT(MediaPlayer); |
| 352 | 75 |
| 353 MediaPlayer::~MediaPlayer() { | 76 MediaPlayer::~MediaPlayer() { |
|
zel
2011/05/24 17:54:25
can we move this to its own file media_player.cc/.
SeRya
2011/05/25 14:23:49
Done.
| |
| 354 } | 77 } |
| 355 | 78 |
| 356 // static | 79 // static |
| 357 MediaPlayer* MediaPlayer::GetInstance() { | 80 MediaPlayer* MediaPlayer::GetInstance() { |
| 358 return Singleton<MediaPlayer>::get(); | 81 return Singleton<MediaPlayer>::get(); |
| 359 } | 82 } |
| 360 | 83 |
| 361 void MediaPlayer::EnqueueMediaFile(Profile* profile, const FilePath& file_path, | 84 void MediaPlayer::EnqueueMediaFile(Profile* profile, const FilePath& file_path, |
| 362 Browser* creator) { | 85 Browser* creator) { |
| 363 static GURL origin_url(kMediaplayerURL); | |
| 364 GURL url; | 86 GURL url; |
| 365 if (!FileManagerUtil::ConvertFileToFileSystemUrl(profile, file_path, | 87 if (!FileManagerUtil::ConvertFileToFileSystemUrl(profile, file_path, |
| 366 origin_url, &url)) { | 88 GetOriginUrl(), &url)) { |
| 367 } | 89 } |
| 368 EnqueueMediaFileUrl(url, creator); | 90 EnqueueMediaFileUrl(url, creator); |
| 369 } | 91 } |
| 370 | 92 |
| 371 void MediaPlayer::EnqueueMediaFileUrl(const GURL& url, Browser* creator) { | 93 void MediaPlayer::EnqueueMediaFileUrl(const GURL& url, Browser* creator) { |
| 372 if (handler_ == NULL) { | 94 if (mediaplayer_browser_ == NULL) { |
| 373 unhandled_urls_.push_back(url); | |
| 374 PopupMediaPlayer(creator); | 95 PopupMediaPlayer(creator); |
| 375 } else { | |
| 376 handler_->EnqueueMediaFileUrl(url); | |
| 377 } | 96 } |
| 97 EnqueueMediaFileUrl(url); | |
| 98 } | |
| 99 | |
| 100 void MediaPlayer::EnqueueMediaFileUrl(const GURL& url) { | |
| 101 current_playlist_.push_back(MediaUrl(url)); | |
| 102 NotifyPlaylistChanged(); | |
| 378 } | 103 } |
| 379 | 104 |
| 380 void MediaPlayer::ForcePlayMediaFile(Profile* profile, | 105 void MediaPlayer::ForcePlayMediaFile(Profile* profile, |
| 381 const FilePath& file_path, | 106 const FilePath& file_path, |
| 382 Browser* creator) { | 107 Browser* creator) { |
| 383 static GURL origin_url(kMediaplayerURL); | |
| 384 GURL url; | 108 GURL url; |
| 385 if (!FileManagerUtil::ConvertFileToFileSystemUrl(profile, file_path, | 109 if (!FileManagerUtil::ConvertFileToFileSystemUrl(profile, file_path, |
| 386 origin_url, &url)) { | 110 GetOriginUrl(), &url)) { |
| 111 return; | |
| 387 } | 112 } |
| 388 ForcePlayMediaURL(url, creator); | 113 ForcePlayMediaURL(url, creator); |
| 389 } | 114 } |
| 390 | 115 |
| 391 void MediaPlayer::ForcePlayMediaURL(const GURL& url, Browser* creator) { | 116 void MediaPlayer::ForcePlayMediaURL(const GURL& url, Browser* creator) { |
| 392 if (handler_ == NULL) { | 117 if (mediaplayer_browser_ == NULL) { |
| 393 unhandled_urls_.push_back(url); | |
| 394 PopupMediaPlayer(creator); | 118 PopupMediaPlayer(creator); |
| 395 } else { | |
| 396 handler_->PlaybackMediaFile(url); | |
| 397 } | 119 } |
| 120 current_playlist_.push_back(MediaUrl(url)); | |
| 121 current_position_ = current_playlist_.size() - 1; | |
| 122 pending_playback_request_ = true; | |
| 123 NotifyPlaylistChanged(); | |
| 398 } | 124 } |
| 399 | 125 |
| 400 void MediaPlayer::TogglePlaylistWindowVisible() { | 126 void MediaPlayer::TogglePlaylistWindowVisible() { |
| 401 if (playlist_browser_) { | 127 if (playlist_browser_) { |
| 402 ClosePlaylistWindow(); | 128 ClosePlaylistWindow(); |
| 403 } else { | 129 } else { |
| 404 ShowPlaylistWindow(); | 130 ShowPlaylistWindow(); |
| 405 } | 131 } |
| 406 } | 132 } |
| 407 | 133 |
| 408 void MediaPlayer::ShowPlaylistWindow() { | 134 void MediaPlayer::ShowPlaylistWindow() { |
| 409 if (playlist_browser_ == NULL) { | 135 if (playlist_browser_ == NULL) { |
| 410 PopupPlaylist(NULL); | 136 PopupPlaylist(NULL); |
| 411 } | 137 } |
| 412 } | 138 } |
| 413 | 139 |
| 414 void MediaPlayer::ClosePlaylistWindow() { | 140 void MediaPlayer::ClosePlaylistWindow() { |
| 415 if (playlist_browser_ != NULL) { | 141 if (playlist_browser_ != NULL) { |
| 416 playlist_browser_->window()->Close(); | 142 playlist_browser_->window()->Close(); |
| 417 } | 143 } |
| 418 } | 144 } |
| 419 | 145 |
| 420 void MediaPlayer::SetPlaylistOffset(int offset) { | 146 void MediaPlayer::SetPlaylistPosition(int position) { |
| 421 if (handler_) { | 147 const int playlist_size = current_playlist_.size(); |
| 422 handler_->SetCurrentPlaylistOffset(offset); | 148 if (current_position_ < 0 || current_position_ > playlist_size) |
| 423 } | 149 position = current_playlist_.size(); |
| 424 if (playlist_) { | 150 if (current_position_ != position) { |
| 425 playlist_->SetCurrentPlaylistOffset(offset); | 151 current_position_ = position; |
| 152 NotifyPlaylistChanged(); | |
| 426 } | 153 } |
| 427 } | 154 } |
| 428 | 155 |
| 429 void MediaPlayer::SetNewHandler(MediaplayerHandler* handler, | 156 void MediaPlayer::SetPlaybackError(GURL const& url) { |
| 430 TabContents* contents) { | 157 for (size_t x = 0; x < current_playlist_.size(); x++) { |
| 431 handler_ = handler; | 158 if (current_playlist_[x].url == url) { |
| 432 mediaplayer_tab_ = contents; | 159 current_playlist_[x].haderror = true; |
| 433 RegisterListeners(); | 160 } |
| 434 for (size_t x = 0; x < unhandled_urls_.size(); x++) { | |
| 435 handler_->EnqueueMediaFileUrl(unhandled_urls_[x]); | |
| 436 } | 161 } |
| 437 unhandled_urls_.clear(); | 162 NotifyPlaylistChanged(); |
| 438 } | 163 } |
| 439 | 164 |
| 440 void MediaPlayer::RegisterListeners() { | |
| 441 registrar_.RemoveAll(); | |
| 442 if (playlist_tab_) { | |
| 443 registrar_.Add(this, | |
| 444 NotificationType::TAB_CONTENTS_DESTROYED, | |
| 445 Source<TabContents>(playlist_tab_)); | |
| 446 } | |
| 447 if (mediaplayer_tab_) { | |
| 448 registrar_.Add(this, | |
| 449 NotificationType::TAB_CONTENTS_DESTROYED, | |
| 450 Source<TabContents>(mediaplayer_tab_)); | |
| 451 } | |
| 452 }; | |
| 453 | |
| 454 void MediaPlayer::Observe(NotificationType type, | 165 void MediaPlayer::Observe(NotificationType type, |
| 455 const NotificationSource& source, | 166 const NotificationSource& source, |
| 456 const NotificationDetails& details) { | 167 const NotificationDetails& details) { |
| 457 DCHECK(type == NotificationType::TAB_CONTENTS_DESTROYED); | 168 DCHECK(type == NotificationType::BROWSER_CLOSING); |
| 458 if (Source<TabContents>(source).ptr() == mediaplayer_tab_) { | 169 registrar_.Remove(this, |
| 459 RemoveHandler(handler_); | 170 NotificationType::BROWSER_CLOSING, |
| 460 RegisterListeners(); | 171 source); |
| 461 ClosePlaylistWindow(); | 172 if (Source<Browser>(source).ptr() == mediaplayer_browser_) { |
| 462 } else if (Source<TabContents>(source).ptr() == playlist_tab_) { | 173 mediaplayer_browser_ = NULL; |
| 463 RemovePlaylistHandler(playlist_); | 174 } else if (Source<Browser>(source).ptr() == playlist_browser_) { |
| 464 RegisterListeners(); | |
| 465 } | |
| 466 } | |
| 467 | |
| 468 void MediaPlayer::RegisterNewPlaylistHandler(MediaplayerHandler* handler, | |
| 469 TabContents* contents) { | |
| 470 playlist_ = handler; | |
| 471 playlist_tab_ = contents; | |
| 472 RegisterListeners(); | |
| 473 NotifyPlaylistChanged(); | |
| 474 } | |
| 475 | |
| 476 void MediaPlayer::RemovePlaylistHandler(MediaplayerHandler* handler) { | |
| 477 if (handler == playlist_) { | |
| 478 playlist_ = NULL; | |
| 479 playlist_browser_ = NULL; | 175 playlist_browser_ = NULL; |
| 480 playlist_tab_ = NULL; | |
| 481 } | 176 } |
| 482 } | 177 } |
| 483 | 178 |
| 484 void MediaPlayer::NotifyPlaylistChanged() { | 179 void MediaPlayer::NotifyPlaylistChanged() { |
| 485 if (handler_ && playlist_) { | 180 ExtensionMediaPlayerEventRouter::GetInstance()->NotifyPlaylistChanged(); |
| 486 playlist_->SetCurrentPlaylist(handler_->GetCurrentPlaylist(), | 181 } |
| 487 handler_->GetCurrentPlaylistOffset()); | 182 |
| 488 } | 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; | |
| 489 } | 191 } |
| 490 | 192 |
| 491 void MediaPlayer::ToggleFullscreen() { | 193 void MediaPlayer::ToggleFullscreen() { |
| 492 if (handler_ && mediaplayer_browser_) { | 194 if (mediaplayer_browser_) { |
| 493 mediaplayer_browser_->ToggleFullscreenMode(); | 195 mediaplayer_browser_->ToggleFullscreenMode(); |
| 494 } | 196 } |
| 495 } | 197 } |
| 496 | 198 |
| 497 void MediaPlayer::RemoveHandler(MediaplayerHandler* handler) { | |
| 498 if (handler == handler_) { | |
| 499 handler_ = NULL; | |
| 500 mediaplayer_browser_ = NULL; | |
| 501 mediaplayer_tab_ = NULL; | |
| 502 } | |
| 503 } | |
| 504 | |
| 505 void MediaPlayer::PopupPlaylist(Browser* creator) { | 199 void MediaPlayer::PopupPlaylist(Browser* creator) { |
| 506 Profile* profile = BrowserList::GetLastActive()->profile(); | 200 Profile* profile = BrowserList::GetLastActive()->profile(); |
| 507 playlist_browser_ = Browser::CreateForApp(Browser::TYPE_PANEL, | 201 playlist_browser_ = Browser::CreateForApp(Browser::TYPE_PANEL, |
| 508 kMediaPlayerAppName, | 202 kMediaPlayerAppName, |
| 509 gfx::Size(), | 203 gfx::Size(), |
| 510 profile); | 204 profile); |
| 511 playlist_browser_->AddSelectedTabWithURL(GURL(kMediaplayerPlaylistURL), | 205 registrar_.Add(this, |
| 206 NotificationType::BROWSER_CLOSING, | |
| 207 Source<Browser>(playlist_browser_)); | |
| 208 playlist_browser_->AddSelectedTabWithURL(GetMediaplayerPlaylistUrl(), | |
| 512 PageTransition::LINK); | 209 PageTransition::LINK); |
| 513 playlist_browser_->window()->SetBounds(gfx::Rect(kPopupLeft, | 210 playlist_browser_->window()->SetBounds(gfx::Rect(kPopupLeft, |
| 514 kPopupTop, | 211 kPopupTop, |
| 515 kPopupWidth, | 212 kPopupWidth, |
| 516 kPopupHeight)); | 213 kPopupHeight)); |
| 517 playlist_browser_->window()->Show(); | 214 playlist_browser_->window()->Show(); |
| 518 } | 215 } |
| 519 | 216 |
| 520 void MediaPlayer::PopupMediaPlayer(Browser* creator) { | 217 void MediaPlayer::PopupMediaPlayer(Browser* creator) { |
| 521 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 218 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 522 BrowserThread::PostTask( | 219 BrowserThread::PostTask( |
| 523 BrowserThread::UI, FROM_HERE, | 220 BrowserThread::UI, FROM_HERE, |
| 524 NewRunnableMethod(this, &MediaPlayer::PopupMediaPlayer, | 221 NewRunnableMethod(this, &MediaPlayer::PopupMediaPlayer, |
| 525 static_cast<Browser*>(NULL))); | 222 static_cast<Browser*>(NULL))); |
| 526 return; | 223 return; |
| 527 } | 224 } |
| 528 Profile* profile = BrowserList::GetLastActive()->profile(); | 225 Profile* profile = BrowserList::GetLastActive()->profile(); |
| 529 mediaplayer_browser_ = Browser::CreateForApp(Browser::TYPE_PANEL, | 226 mediaplayer_browser_ = Browser::CreateForApp(Browser::TYPE_PANEL, |
| 530 kMediaPlayerAppName, | 227 kMediaPlayerAppName, |
| 531 gfx::Size(), | 228 gfx::Size(), |
| 532 profile); | 229 profile); |
| 230 registrar_.Add(this, | |
| 231 NotificationType::BROWSER_CLOSING, | |
| 232 Source<Browser>(mediaplayer_browser_)); | |
| 233 | |
| 533 #if defined(OS_CHROMEOS) | 234 #if defined(OS_CHROMEOS) |
| 534 // Since we are on chromeos, popups should be a PanelBrowserView, | 235 // Since we are on chromeos, popups should be a PanelBrowserView, |
| 535 // so we can just cast it. | 236 // so we can just cast it. |
| 536 if (creator) { | 237 if (creator) { |
| 537 chromeos::PanelBrowserView* creatorview = | 238 chromeos::PanelBrowserView* creatorview = |
| 538 static_cast<chromeos::PanelBrowserView*>(creator->window()); | 239 static_cast<chromeos::PanelBrowserView*>(creator->window()); |
| 539 chromeos::PanelBrowserView* view = | 240 chromeos::PanelBrowserView* view = |
| 540 static_cast<chromeos::PanelBrowserView*>( | 241 static_cast<chromeos::PanelBrowserView*>( |
| 541 mediaplayer_browser_->window()); | 242 mediaplayer_browser_->window()); |
| 542 view->SetCreatorView(creatorview); | 243 view->SetCreatorView(creatorview); |
| 543 } | 244 } |
| 544 #endif | 245 #endif |
| 545 mediaplayer_browser_->AddSelectedTabWithURL(GURL(kMediaplayerURL), | 246 mediaplayer_browser_->AddSelectedTabWithURL(GetMediaPlayerUrl(), |
| 546 PageTransition::LINK); | 247 PageTransition::LINK); |
| 547 mediaplayer_browser_->window()->SetBounds(gfx::Rect(kPopupLeft, | 248 mediaplayer_browser_->window()->SetBounds(gfx::Rect(kPopupLeft, |
| 548 kPopupTop, | 249 kPopupTop, |
| 549 kPopupWidth, | 250 kPopupWidth, |
| 550 kPopupHeight)); | 251 kPopupHeight)); |
| 551 mediaplayer_browser_->window()->Show(); | 252 mediaplayer_browser_->window()->Show(); |
| 552 } | 253 } |
| 553 | 254 |
| 554 net::URLRequestJob* MediaPlayer::MaybeIntercept(net::URLRequest* request) { | 255 net::URLRequestJob* MediaPlayer::MaybeIntercept(net::URLRequest* request) { |
| 555 // Don't attempt to intercept here as we want to wait until the mime | 256 // Don't attempt to intercept here as we want to wait until the mime |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 580 if (supported_mime_types_.find(mime_type) != supported_mime_types_.end()) { | 281 if (supported_mime_types_.find(mime_type) != supported_mime_types_.end()) { |
| 581 if (request->referrer() != chrome::kChromeUIMediaplayerURL && | 282 if (request->referrer() != chrome::kChromeUIMediaplayerURL && |
| 582 !request->referrer().empty()) { | 283 !request->referrer().empty()) { |
| 583 EnqueueMediaFileUrl(request->url(), NULL); | 284 EnqueueMediaFileUrl(request->url(), NULL); |
| 584 request->Cancel(); | 285 request->Cancel(); |
| 585 } | 286 } |
| 586 } | 287 } |
| 587 return NULL; | 288 return NULL; |
| 588 } | 289 } |
| 589 | 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 | |
| 590 MediaPlayer::MediaPlayer() | 303 MediaPlayer::MediaPlayer() |
| 591 : handler_(NULL), | 304 : current_position_(0), |
| 592 playlist_(NULL), | 305 pending_playback_request_(false), |
| 593 playlist_browser_(NULL), | 306 playlist_browser_(NULL), |
| 594 mediaplayer_browser_(NULL), | 307 mediaplayer_browser_(NULL) { |
| 595 mediaplayer_tab_(NULL), | |
| 596 playlist_tab_(NULL) { | |
| 597 for (size_t i = 0; i < arraysize(supported_mime_type_list); ++i) { | 308 for (size_t i = 0; i < arraysize(supported_mime_type_list); ++i) { |
| 598 supported_mime_types_.insert(supported_mime_type_list[i]); | 309 supported_mime_types_.insert(supported_mime_type_list[i]); |
| 599 } | 310 } |
| 600 }; | 311 }; |
| 601 | |
| 602 //////////////////////////////////////////////////////////////////////////////// | |
| 603 // | |
| 604 // MediaplayerUIContents | |
| 605 // | |
| 606 //////////////////////////////////////////////////////////////////////////////// | |
| 607 | |
| 608 MediaplayerUI::MediaplayerUI(TabContents* contents) : WebUI(contents) { | |
| 609 const GURL& url = contents->GetURL(); | |
| 610 bool is_playlist = (url.ref() == "playlist"); | |
| 611 MediaplayerHandler* handler = new MediaplayerHandler(is_playlist); | |
| 612 AddMessageHandler(handler->Attach(this)); | |
| 613 if (is_playlist) { | |
| 614 handler->Init(true, contents); | |
| 615 } else { | |
| 616 handler->Init(false, contents); | |
| 617 } | |
| 618 | |
| 619 MediaplayerUIHTMLSource* html_source = | |
| 620 new MediaplayerUIHTMLSource(is_playlist); | |
| 621 | |
| 622 // Set up the chrome://mediaplayer/ source. | |
| 623 contents->profile()->GetChromeURLDataManager()->AddDataSource(html_source); | |
| 624 } | |
| OLD | NEW |