| 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/ui/webui/mediaplayer_ui.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/download/download_manager.h" | |
| 20 #include "chrome/browser/download/download_util.h" | |
| 21 #include "chrome/browser/extensions/file_manager_util.h" | |
| 22 #include "chrome/browser/history/history_types.h" | |
| 23 #include "chrome/browser/profiles/profile.h" | |
| 24 #include "chrome/browser/tabs/tab_strip_model.h" | |
| 25 #include "chrome/browser/ui/browser.h" | |
| 26 #include "chrome/browser/ui/browser_list.h" | |
| 27 #include "chrome/browser/ui/browser_window.h" | |
| 28 #include "chrome/browser/ui/webui/favicon_source.h" | |
| 29 #include "chrome/common/chrome_paths.h" | |
| 30 #include "chrome/common/chrome_switches.h" | |
| 31 #include "chrome/common/jstemplate_builder.h" | |
| 32 #include "chrome/common/net/url_fetcher.h" | |
| 33 #include "chrome/common/time_format.h" | |
| 34 #include "chrome/common/url_constants.h" | |
| 35 #include "content/browser/browser_thread.h" | |
| 36 #include "content/browser/tab_contents/tab_contents.h" | |
| 37 #include "content/browser/user_metrics.h" | |
| 38 #include "grit/browser_resources.h" | |
| 39 #include "grit/chromium_strings.h" | |
| 40 #include "grit/generated_resources.h" | |
| 41 #include "grit/locale_settings.h" | |
| 42 #include "net/base/escape.h" | |
| 43 #include "net/base/load_flags.h" | |
| 44 #include "net/url_request/url_request_job.h" | |
| 45 #include "ui/base/resource/resource_bundle.h" | |
| 46 | |
| 47 #if defined(OS_CHROMEOS) | |
| 48 #include "chrome/browser/chromeos/frame/panel_browser_view.h" | |
| 49 #endif | |
| 50 | |
| 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"; | |
| 59 static const int kPopupLeft = 0; | |
| 60 static const int kPopupTop = 0; | |
| 61 static const int kPopupWidth = 350; | |
| 62 static const int kPopupHeight = 300; | |
| 63 | |
| 64 class MediaplayerUIHTMLSource : public ChromeURLDataManager::DataSource { | |
| 65 public: | |
| 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 } | |
| 163 | |
| 164 void MediaplayerUIHTMLSource::StartDataRequest(const std::string& path, | |
| 165 bool is_incognito, | |
| 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 } | |
| 198 | |
| 199 //////////////////////////////////////////////////////////////////////////////// | |
| 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 // | |
| 345 // Mediaplayer | |
| 346 // | |
| 347 //////////////////////////////////////////////////////////////////////////////// | |
| 348 | |
| 349 // Allows InvokeLater without adding refcounting. This class is a Singleton and | |
| 350 // won't be deleted until it's last InvokeLater is run. | |
| 351 DISABLE_RUNNABLE_METHOD_REFCOUNT(MediaPlayer); | |
| 352 | |
| 353 MediaPlayer::~MediaPlayer() { | |
| 354 } | |
| 355 | |
| 356 // static | |
| 357 MediaPlayer* MediaPlayer::GetInstance() { | |
| 358 return Singleton<MediaPlayer>::get(); | |
| 359 } | |
| 360 | |
| 361 void MediaPlayer::EnqueueMediaFile(Profile* profile, const FilePath& file_path, | |
| 362 Browser* creator) { | |
| 363 static GURL origin_url(kMediaplayerURL); | |
| 364 GURL url; | |
| 365 if (!FileManagerUtil::ConvertFileToFileSystemUrl(profile, file_path, | |
| 366 origin_url, &url)) { | |
| 367 } | |
| 368 EnqueueMediaFileUrl(url, creator); | |
| 369 } | |
| 370 | |
| 371 void MediaPlayer::EnqueueMediaFileUrl(const GURL& url, Browser* creator) { | |
| 372 if (handler_ == NULL) { | |
| 373 unhandled_urls_.push_back(url); | |
| 374 PopupMediaPlayer(creator); | |
| 375 } else { | |
| 376 handler_->EnqueueMediaFileUrl(url); | |
| 377 } | |
| 378 } | |
| 379 | |
| 380 void MediaPlayer::ForcePlayMediaFile(Profile* profile, | |
| 381 const FilePath& file_path, | |
| 382 Browser* creator) { | |
| 383 static GURL origin_url(kMediaplayerURL); | |
| 384 GURL url; | |
| 385 if (!FileManagerUtil::ConvertFileToFileSystemUrl(profile, file_path, | |
| 386 origin_url, &url)) { | |
| 387 } | |
| 388 ForcePlayMediaURL(url, creator); | |
| 389 } | |
| 390 | |
| 391 void MediaPlayer::ForcePlayMediaURL(const GURL& url, Browser* creator) { | |
| 392 if (handler_ == NULL) { | |
| 393 unhandled_urls_.push_back(url); | |
| 394 PopupMediaPlayer(creator); | |
| 395 } else { | |
| 396 handler_->PlaybackMediaFile(url); | |
| 397 } | |
| 398 } | |
| 399 | |
| 400 void MediaPlayer::TogglePlaylistWindowVisible() { | |
| 401 if (playlist_browser_) { | |
| 402 ClosePlaylistWindow(); | |
| 403 } else { | |
| 404 ShowPlaylistWindow(); | |
| 405 } | |
| 406 } | |
| 407 | |
| 408 void MediaPlayer::ShowPlaylistWindow() { | |
| 409 if (playlist_browser_ == NULL) { | |
| 410 PopupPlaylist(NULL); | |
| 411 } | |
| 412 } | |
| 413 | |
| 414 void MediaPlayer::ClosePlaylistWindow() { | |
| 415 if (playlist_browser_ != NULL) { | |
| 416 playlist_browser_->window()->Close(); | |
| 417 } | |
| 418 } | |
| 419 | |
| 420 void MediaPlayer::SetPlaylistOffset(int offset) { | |
| 421 if (handler_) { | |
| 422 handler_->SetCurrentPlaylistOffset(offset); | |
| 423 } | |
| 424 if (playlist_) { | |
| 425 playlist_->SetCurrentPlaylistOffset(offset); | |
| 426 } | |
| 427 } | |
| 428 | |
| 429 void MediaPlayer::SetNewHandler(MediaplayerHandler* handler, | |
| 430 TabContents* contents) { | |
| 431 handler_ = handler; | |
| 432 mediaplayer_tab_ = contents; | |
| 433 RegisterListeners(); | |
| 434 for (size_t x = 0; x < unhandled_urls_.size(); x++) { | |
| 435 handler_->EnqueueMediaFileUrl(unhandled_urls_[x]); | |
| 436 } | |
| 437 unhandled_urls_.clear(); | |
| 438 } | |
| 439 | |
| 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, | |
| 455 const NotificationSource& source, | |
| 456 const NotificationDetails& details) { | |
| 457 DCHECK(type == NotificationType::TAB_CONTENTS_DESTROYED); | |
| 458 if (Source<TabContents>(source).ptr() == mediaplayer_tab_) { | |
| 459 RemoveHandler(handler_); | |
| 460 RegisterListeners(); | |
| 461 ClosePlaylistWindow(); | |
| 462 } else if (Source<TabContents>(source).ptr() == playlist_tab_) { | |
| 463 RemovePlaylistHandler(playlist_); | |
| 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; | |
| 480 playlist_tab_ = NULL; | |
| 481 } | |
| 482 } | |
| 483 | |
| 484 void MediaPlayer::NotifyPlaylistChanged() { | |
| 485 if (handler_ && playlist_) { | |
| 486 playlist_->SetCurrentPlaylist(handler_->GetCurrentPlaylist(), | |
| 487 handler_->GetCurrentPlaylistOffset()); | |
| 488 } | |
| 489 } | |
| 490 | |
| 491 void MediaPlayer::ToggleFullscreen() { | |
| 492 if (handler_ && mediaplayer_browser_) { | |
| 493 mediaplayer_browser_->ToggleFullscreenMode(); | |
| 494 } | |
| 495 } | |
| 496 | |
| 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) { | |
| 506 Profile* profile = BrowserList::GetLastActive()->profile(); | |
| 507 playlist_browser_ = Browser::CreateForApp(Browser::TYPE_PANEL, | |
| 508 kMediaPlayerAppName, | |
| 509 gfx::Size(), | |
| 510 profile); | |
| 511 playlist_browser_->AddSelectedTabWithURL(GURL(kMediaplayerPlaylistURL), | |
| 512 PageTransition::LINK); | |
| 513 playlist_browser_->window()->SetBounds(gfx::Rect(kPopupLeft, | |
| 514 kPopupTop, | |
| 515 kPopupWidth, | |
| 516 kPopupHeight)); | |
| 517 playlist_browser_->window()->Show(); | |
| 518 } | |
| 519 | |
| 520 void MediaPlayer::PopupMediaPlayer(Browser* creator) { | |
| 521 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
| 522 BrowserThread::PostTask( | |
| 523 BrowserThread::UI, FROM_HERE, | |
| 524 NewRunnableMethod(this, &MediaPlayer::PopupMediaPlayer, | |
| 525 static_cast<Browser*>(NULL))); | |
| 526 return; | |
| 527 } | |
| 528 Profile* profile = BrowserList::GetLastActive()->profile(); | |
| 529 mediaplayer_browser_ = Browser::CreateForApp(Browser::TYPE_PANEL, | |
| 530 kMediaPlayerAppName, | |
| 531 gfx::Size(), | |
| 532 profile); | |
| 533 #if defined(OS_CHROMEOS) | |
| 534 // Since we are on chromeos, popups should be a PanelBrowserView, | |
| 535 // so we can just cast it. | |
| 536 if (creator) { | |
| 537 chromeos::PanelBrowserView* creatorview = | |
| 538 static_cast<chromeos::PanelBrowserView*>(creator->window()); | |
| 539 chromeos::PanelBrowserView* view = | |
| 540 static_cast<chromeos::PanelBrowserView*>( | |
| 541 mediaplayer_browser_->window()); | |
| 542 view->SetCreatorView(creatorview); | |
| 543 } | |
| 544 #endif | |
| 545 mediaplayer_browser_->AddSelectedTabWithURL(GURL(kMediaplayerURL), | |
| 546 PageTransition::LINK); | |
| 547 mediaplayer_browser_->window()->SetBounds(gfx::Rect(kPopupLeft, | |
| 548 kPopupTop, | |
| 549 kPopupWidth, | |
| 550 kPopupHeight)); | |
| 551 mediaplayer_browser_->window()->Show(); | |
| 552 } | |
| 553 | |
| 554 net::URLRequestJob* MediaPlayer::MaybeIntercept(net::URLRequest* request) { | |
| 555 // Don't attempt to intercept here as we want to wait until the mime | |
| 556 // type is fully determined. | |
| 557 return NULL; | |
| 558 } | |
| 559 | |
| 560 // This is the list of mime types currently supported by the Google | |
| 561 // Document Viewer. | |
| 562 static const char* const supported_mime_type_list[] = { | |
| 563 "audio/mpeg", | |
| 564 "video/mp4", | |
| 565 "audio/mp3" | |
| 566 }; | |
| 567 | |
| 568 net::URLRequestJob* MediaPlayer::MaybeInterceptResponse( | |
| 569 net::URLRequest* request) { | |
| 570 // Do not intercept this request if it is a download. | |
| 571 if (request->load_flags() & net::LOAD_IS_DOWNLOAD) { | |
| 572 return NULL; | |
| 573 } | |
| 574 | |
| 575 std::string mime_type; | |
| 576 request->GetMimeType(&mime_type); | |
| 577 // If it is in our list of known URLs, enqueue the url then | |
| 578 // Cancel the request so the mediaplayer can handle it when | |
| 579 // it hits it in the playlist. | |
| 580 if (supported_mime_types_.find(mime_type) != supported_mime_types_.end()) { | |
| 581 if (request->referrer() != chrome::kChromeUIMediaplayerURL && | |
| 582 !request->referrer().empty()) { | |
| 583 EnqueueMediaFileUrl(request->url(), NULL); | |
| 584 request->Cancel(); | |
| 585 } | |
| 586 } | |
| 587 return NULL; | |
| 588 } | |
| 589 | |
| 590 MediaPlayer::MediaPlayer() | |
| 591 : handler_(NULL), | |
| 592 playlist_(NULL), | |
| 593 playlist_browser_(NULL), | |
| 594 mediaplayer_browser_(NULL), | |
| 595 mediaplayer_tab_(NULL), | |
| 596 playlist_tab_(NULL) { | |
| 597 for (size_t i = 0; i < arraysize(supported_mime_type_list); ++i) { | |
| 598 supported_mime_types_.insert(supported_mime_type_list[i]); | |
| 599 } | |
| 600 }; | |
| 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 |