1 // Copyright (c) 2012 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.
5 #include "chrome/browser/chromeos/media/media_player.h"
10 #include "base/bind.h"
11 #include "chrome/browser/chromeos/extensions/file_manager/file_manager_util.h"
12 #include "chrome/browser/chromeos/extensions/media_player_api.h"
13 #include "chrome/browser/chromeos/extensions/media_player_event_router.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/profiles/profile_manager.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/browser/ui/browser_iterator.h"
18 #include "chrome/browser/ui/browser_tabstrip.h"
19 #include "chrome/browser/ui/browser_window.h"
20 #include "chrome/browser/ui/host_desktop.h"
21 #include "chrome/browser/ui/tabs/tab_strip_model.h"
22 #include "chrome/common/chrome_notification_types.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "content/public/browser/web_contents.h"
25 #include "ui/gfx/screen.h"
27 using content::BrowserThread
;
31 const char kMediaPlayerAppName
[] = "mediaplayer";
32 const int kPopupRight
= 20;
33 const int kPopupBottom
= 80;
34 const int kPopupWidth
= 280;
36 // Set the initial height to the minimum possible height. Keep the constants
37 // in sync with chrome/browser/resources/file_manager/css/audio_player.css.
38 // SetWindowHeight will be called soon after the popup creation with the correct
39 // height which will cause a nice slide-up animation.
40 // TODO(kaznacheev): Remove kTitleHeight when MediaPlayer becomes chromeless.
41 // kTitleHeight is an approximate value. May be different for touch-enabled UI.
42 const int kTitleHeight
= 35;
43 const int kTrackHeight
= 58;
44 const int kControlsHeight
= 35;
45 const int kPopupHeight
= kTitleHeight
+ kTrackHeight
+ kControlsHeight
;
49 const MediaPlayer::UrlVector
& MediaPlayer::GetPlaylist() const {
50 return current_playlist_
;
53 int MediaPlayer::GetPlaylistPosition() const {
54 return current_position_
;
57 ////////////////////////////////////////////////////////////////////////////////
61 ////////////////////////////////////////////////////////////////////////////////
63 MediaPlayer::~MediaPlayer() {
67 MediaPlayer
* MediaPlayer::GetInstance() {
68 return Singleton
<MediaPlayer
>::get();
71 // The client knows how high the client part of the window should be but
72 // cannot translate it to the window height (because the window title bar height
73 // is unknown). Instead it passes the height difference which this method
74 // applies to the window height.
75 void MediaPlayer::AdjustWindowHeight(int height_diff
) {
76 Browser
* browser
= GetBrowser();
77 if (browser
!= NULL
) {
78 gfx::Rect bounds
= browser
->window()->GetBounds();
79 int window_height
= bounds
.height() + height_diff
;
80 browser
->window()->SetBounds(gfx::Rect(
82 std::max(0, bounds
.bottom() - window_height
),
88 void MediaPlayer::CloseWindow() {
89 Browser
* browser
= GetBrowser();
90 if (browser
!= NULL
) {
91 browser
->window()->Close();
95 void MediaPlayer::ClearPlaylist() {
96 current_playlist_
.clear();
99 void MediaPlayer::EnqueueMediaFileUrl(const GURL
& url
) {
100 current_playlist_
.push_back(url
);
103 void MediaPlayer::ForcePlayMediaURL(const GURL
& url
) {
105 EnqueueMediaFileUrl(url
);
106 SetPlaylistPosition(0);
107 NotifyPlaylistChanged();
110 void MediaPlayer::SetPlaylistPosition(int position
) {
111 current_position_
= position
;
114 void MediaPlayer::NotifyPlaylistChanged() {
115 Browser
* browser
= GetBrowser();
117 extensions::MediaPlayerAPI::Get(browser
->profile())->
118 media_player_event_router()->NotifyPlaylistChanged();
122 void MediaPlayer::PopupMediaPlayer() {
123 if (!BrowserThread::CurrentlyOn(BrowserThread::UI
)) {
124 BrowserThread::PostTask(
125 BrowserThread::UI
, FROM_HERE
,
126 base::Bind(&MediaPlayer::PopupMediaPlayer
,
127 base::Unretained(this) /*this class is a singleton*/));
131 Browser
* browser
= GetBrowser();
133 const gfx::Size screen
=
134 ash::Shell::GetScreen()->GetPrimaryDisplay().size();
135 const gfx::Rect
bounds(screen
.width() - kPopupRight
- kPopupWidth
,
136 screen
.height() - kPopupBottom
- kPopupHeight
,
140 Profile
* profile
= ProfileManager::GetDefaultProfileOrOffTheRecord();
141 Browser::CreateParams
params(Browser::TYPE_POPUP
, profile
,
142 chrome::HOST_DESKTOP_TYPE_ASH
);
143 params
.app_name
= kMediaPlayerAppName
;
144 params
.initial_bounds
= bounds
;
145 browser
= new Browser(params
);
147 chrome::AddSelectedTabWithURL(browser
, GetMediaPlayerUrl(),
148 content::PAGE_TRANSITION_LINK
);
150 browser
->window()->Show();
153 GURL
MediaPlayer::GetMediaPlayerUrl() {
154 return file_manager_util::GetMediaPlayerUrl();
157 Browser
* MediaPlayer::GetBrowser() {
158 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
159 Browser
* browser
= *it
;
160 TabStripModel
* tab_strip
= browser
->tab_strip_model();
161 for (int idx
= 0; idx
< tab_strip
->count(); idx
++) {
162 const GURL
& url
= tab_strip
->GetWebContentsAt(idx
)->GetURL();
163 GURL
base_url(url
.GetOrigin().spec() + url
.path().substr(1));
164 if (base_url
== GetMediaPlayerUrl())
171 MediaPlayer::MediaPlayer()
172 : current_position_(0) {