<commit_before>// 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.

#include "chrome/browser/chromeos/media/media_player.h"

#include "base/command_line.h"
#include "base/logging.h"
#include "base/memory/singleton.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop.h"
#include "base/path_service.h"
#include "base/string_piece.h"
#include "base/string_util.h"
#include "base/threading/thread.h"
#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_util.h"
#include "chrome/browser/extensions/file_manager_util.h"
#include "chrome/browser/history/history_types.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/tabs/tab_strip_model.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/webui/favicon_source.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/jstemplate_builder.h"
#include "chrome/common/time_format.h"
#include "chrome/common/url_constants.h"
#include "content/browser/browser_thread.h"
#include "content/browser/download/download_manager.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "content/browser/user_metrics.h"
#include "content/common/url_fetcher.h"
#include "grit/browser_resources.h"
#include "grit/chromium_strings.h"
#include "grit/generated_resources.h"
#include "grit/locale_settings.h"
#include "net/base/escape.h"
#include "net/base/load_flags.h"
#include "net/url_request/url_request_job.h"
#include "ui/base/resource/resource_bundle.h"

#if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/frame/panel_browser_view.h"
#endif

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;

const MediaPlayer::UrlVector& MediaPlayer::GetPlaylist() const {
  return current_playlist_;
}

int MediaPlayer::GetPlaylistPosition() const {
  return current_position_;
}

////////////////////////////////////////////////////////////////////////////////
//
// Mediaplayer
//
////////////////////////////////////////////////////////////////////////////////

// Allows InvokeLater without adding refcounting. This class is a Singleton and
// won't be deleted until it's last InvokeLater is run.
DISABLE_RUNNABLE_METHOD_REFCOUNT(MediaPlayer);

MediaPlayer::~MediaPlayer() {
}

// static
MediaPlayer* MediaPlayer::GetInstance() {
  return Singleton<MediaPlayer>::get();
}

void MediaPlayer::EnqueueMediaFile(Profile* profile, const FilePath& file_path,
                                   Browser* creator) {
  GURL url;
  if (!FileManagerUtil::ConvertFileToFileSystemUrl(profile, file_path,
                                                   GetOriginUrl(), &url)) {
  }
  EnqueueMediaFileUrl(url, creator);
}

void MediaPlayer::EnqueueMediaFileUrl(const GURL& url, Browser* creator) {
  if (mediaplayer_browser_ == NULL) {
    PopupMediaPlayer(creator);
  }
  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) {
  GURL url;
  if (!FileManagerUtil::ConvertFileToFileSystemUrl(profile, file_path,
                                                   GetOriginUrl(), &url)) {
    return;
  }
  ForcePlayMediaURL(url, creator);
}

void MediaPlayer::ForcePlayMediaURL(const GURL& url, Browser* creator) {
  if (mediaplayer_browser_ == NULL) {
    PopupMediaPlayer(creator);
  }
  current_playlist_.clear();
  current_playlist_.push_back(MediaUrl(url));
  current_position_ = current_playlist_.size() - 1;
  pending_playback_request_ = true;
  NotifyPlaylistChanged();
}

void MediaPlayer::TogglePlaylistWindowVisible() {
  if (playlist_browser_) {
    ClosePlaylistWindow();
  } else {
    ShowPlaylistWindow();
  }
}

void MediaPlayer::ShowPlaylistWindow() {
  if (playlist_browser_ == NULL) {
    PopupPlaylist(NULL);
  }
}

void MediaPlayer::ClosePlaylistWindow() {
  if (playlist_browser_ != NULL) {
    playlist_browser_->window()->Close();
  }
}

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::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;
    }
  }
  NotifyPlaylistChanged();
}

void MediaPlayer::Observe(int type,
                          const NotificationSource& source,
                          const NotificationDetails& details) {
  DCHECK(type == chrome::NOTIFICATION_BROWSER_CLOSING);
  registrar_.Remove(this,
                    chrome::NOTIFICATION_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::NotifyPlaylistChanged() {
  ExtensionMediaPlayerEventRouter::GetInstance()->NotifyPlaylistChanged();
}

bool MediaPlayer::GetPendingPlayRequestAndReset() {
  bool result = pending_playback_request_;
  pending_playback_request_ = false;
  return result;
}

void MediaPlayer::SetPlaybackRequest() {
  pending_playback_request_ = true;
}

void MediaPlayer::ToggleFullscreen() {
  if (mediaplayer_browser_) {
    mediaplayer_browser_->ToggleFullscreenMode();
  }
}

void MediaPlayer::PopupPlaylist(Browser* creator) {
  Profile* profile = BrowserList::GetLastActive()->profile();
  playlist_browser_ = Browser::CreateForApp(Browser::TYPE_PANEL,
                                            kMediaPlayerAppName,
                                            gfx::Rect(),
                                            profile);
  registrar_.Add(this,
                 chrome::NOTIFICATION_BROWSER_CLOSING,
                 Source<Browser>(playlist_browser_));
  playlist_browser_->AddSelectedTabWithURL(GetMediaplayerPlaylistUrl(),
                                           PageTransition::LINK);
  playlist_browser_->window()->SetBounds(gfx::Rect(kPopupLeft,
                                                   kPopupTop,
                                                   kPopupWidth,
                                                   kPopupHeight));
  playlist_browser_->window()->Show();
}

void MediaPlayer::PopupMediaPlayer(Browser* creator) {
  if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
    BrowserThread::PostTask(
        BrowserThread::UI, FROM_HERE,
        NewRunnableMethod(this, &MediaPlayer::PopupMediaPlayer,
                          static_cast<Browser*>(NULL)));
    return;
  }
  Profile* profile = BrowserList::GetLastActive()->profile();
  mediaplayer_browser_ = Browser::CreateForApp(Browser::TYPE_PANEL,
                                               kMediaPlayerAppName,
                                               gfx::Rect(),
                                               profile);
  registrar_.Add(this,
                 chrome::NOTIFICATION_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.
  if (creator) {
    chromeos::PanelBrowserView* creatorview =
        static_cast<chromeos::PanelBrowserView*>(creator->window());
    chromeos::PanelBrowserView* view =
        static_cast<chromeos::PanelBrowserView*>(
            mediaplayer_browser_->window());
    view->SetCreatorView(creatorview);
  }
#endif<commit_msg>Fixing: Media file doesn't play after closing the media player using ctrl + w<commit_after>// 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.

#include "chrome/browser/chromeos/media/media_player.h"

#include "base/command_line.h"
#include "base/logging.h"
#include "base/memory/singleton.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop.h"
#include "base/path_service.h"
#include "base/string_piece.h"
#include "base/string_util.h"
#include "base/threading/thread.h"
#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_util.h"
#include "chrome/browser/extensions/file_manager_util.h"
#include "chrome/browser/history/history_types.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/tabs/tab_strip_model.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/webui/favicon_source.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/jstemplate_builder.h"
#include "chrome/common/time_format.h"
#include "chrome/common/url_constants.h"
#include "content/browser/browser_thread.h"
#include "content/browser/download/download_manager.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "content/browser/user_metrics.h"
#include "content/common/url_fetcher.h"
#include "grit/browser_resources.h"
#include "grit/chromium_strings.h"
#include "grit/generated_resources.h"
#include "grit/locale_settings.h"
#include "net/base/escape.h"
#include "net/base/load_flags.h"
#include "net/url_request/url_request_job.h"
#include "ui/base/resource/resource_bundle.h"

#if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/frame/panel_browser_view.h"
#endif

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;

const MediaPlayer::UrlVector& MediaPlayer::GetPlaylist() const {
  return current_playlist_;
}

int MediaPlayer::GetPlaylistPosition() const {
  return current_position_;
}

////////////////////////////////////////////////////////////////////////////////
//
// Mediaplayer
//
////////////////////////////////////////////////////////////////////////////////

// Allows InvokeLater without adding refcounting. This class is a Singleton and
// won't be deleted until it's last InvokeLater is run.
DISABLE_RUNNABLE_METHOD_REFCOUNT(MediaPlayer);

MediaPlayer::~MediaPlayer() {
}

// static
MediaPlayer* MediaPlayer::GetInstance() {
  return Singleton<MediaPlayer>::get();
}

void MediaPlayer::EnqueueMediaFile(Profile* profile, const FilePath& file_path,
                                   Browser* creator) {
  GURL url;
  if (!FileManagerUtil::ConvertFileToFileSystemUrl(profile, file_path,
                                                   GetOriginUrl(), &url)) {
  }
  EnqueueMediaFileUrl(url, creator);
}

void MediaPlayer::EnqueueMediaFileUrl(const GURL& url, Browser* creator) {
  if (mediaplayer_browser_ == NULL) {
    PopupMediaPlayer(creator);
  }
  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) {
  GURL url;
  if (!FileManagerUtil::ConvertFileToFileSystemUrl(profile, file_path,
                                                   GetOriginUrl(), &url)) {
    return;
  }
  ForcePlayMediaURL(url, creator);
}

void MediaPlayer::ForcePlayMediaURL(const GURL& url, Browser* creator) {
  if (mediaplayer_browser_ == NULL) {
    PopupMediaPlayer(creator);
  }
  current_playlist_.clear();
  current_playlist_.push_back(MediaUrl(url));
  current_position_ = current_playlist_.size() - 1;
  pending_playback_request_ = true;
  NotifyPlaylistChanged();
}

void MediaPlayer::TogglePlaylistWindowVisible() {
  if (playlist_browser_) {
    ClosePlaylistWindow();
  } else {
    ShowPlaylistWindow();
  }
}

void MediaPlayer::ShowPlaylistWindow() {
  if (playlist_browser_ == NULL) {
    PopupPlaylist(NULL);
  }
}

void MediaPlayer::ClosePlaylistWindow() {
  if (playlist_browser_ != NULL) {
    playlist_browser_->window()->Close();
  }
}

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::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;
    }
  }
  NotifyPlaylistChanged();
}

void MediaPlayer::Observe(int type,
                          const NotificationSource& source,
                          const NotificationDetails& details) {
  DCHECK(type == chrome::NOTIFICATION_BROWSER_CLOSED);
  registrar_.Remove(this,
                    chrome::NOTIFICATION_BROWSER_CLOSED,
                    source);
  if (Source<Browser>(source).ptr() == mediaplayer_browser_) {
    mediaplayer_browser_ = NULL;
  } else if (Source<Browser>(source).ptr() == playlist_browser_) {
    playlist_browser_ = NULL;
  }
}

void MediaPlayer::NotifyPlaylistChanged() {
  ExtensionMediaPlayerEventRouter::GetInstance()->NotifyPlaylistChanged();
}

bool MediaPlayer::GetPendingPlayRequestAndReset() {
  bool result = pending_playback_request_;
  pending_playback_request_ = false;
  return result;
}

void MediaPlayer::SetPlaybackRequest() {
  pending_playback_request_ = true;
}

void MediaPlayer::ToggleFullscreen() {
  if (mediaplayer_browser_) {
    mediaplayer_browser_->ToggleFullscreenMode();
  }
}

void MediaPlayer::PopupPlaylist(Browser* creator) {
  Profile* profile = BrowserList::GetLastActive()->profile();
  playlist_browser_ = Browser::CreateForApp(Browser::TYPE_PANEL,
                                            kMediaPlayerAppName,
                                            gfx::Rect(),
                                            profile);
  registrar_.Add(this,
                 chrome::NOTIFICATION_BROWSER_CLOSED,
                 Source<Browser>(playlist_browser_));
  playlist_browser_->AddSelectedTabWithURL(GetMediaplayerPlaylistUrl(),
                                           PageTransition::LINK);
  playlist_browser_->window()->SetBounds(gfx::Rect(kPopupLeft,
                                                   kPopupTop,
                                                   kPopupWidth,
                                                   kPopupHeight));
  playlist_browser_->window()->Show();
}

void MediaPlayer::PopupMediaPlayer(Browser* creator) {
  if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
    BrowserThread::PostTask(
        BrowserThread::UI, FROM_HERE,
        NewRunnableMethod(this, &MediaPlayer::PopupMediaPlayer,
                          static_cast<Browser*>(NULL)));
    return;
  }
  Profile* profile = BrowserList::GetLastActive()->profile();
  mediaplayer_browser_ = Browser::CreateForApp(Browser::TYPE_PANEL,
                                               kMediaPlayerAppName,
                                               gfx::Rect(),
                                               profile);
  registrar_.Add(this,
                 chrome::NOTIFICATION_BROWSER_CLOSED,
                 Source<Browser>(mediaplayer_browser_));

#if defined(OS_CHROMEOS)
  // Since we are on chromeos, popups should be a PanelBrowserView,
  // so we can just cast it.
  if (creator) {
    chromeos::PanelBrowserView* creatorview =
        static_cast<chromeos::PanelBrowserView*>(creator->window());
    chromeos::PanelBrowserView* view =
        static_cast<chromeos::PanelBrowserView*>(
            mediaplayer_browser_->window());
    view->SetCreatorView(creatorview);
  }
#endif
{'fraction_non_alphanumeric': 0.13023006317797114, 'fraction_numerical': 0.0017880557873405651, 'mean_word_length': 3.0686226964112513, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 62, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6806302', 'n_tokens_mistral': 4920, 'n_tokens_neox': 4830, 'n_words': 865}