summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/media/media_player.cc
blob: f62bd6e47fe6d73678eb1f71eb65f86cf0ce5978 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// 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/net/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) && defined(TOOLKIT_USES_GTK)
#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) {
  GURL url;
  if (!FileManagerUtil::ConvertFileToFileSystemUrl(profile, file_path,
                                                   GetOriginUrl(), &url)) {
  }
  EnqueueMediaFileUrl(url);
}

void MediaPlayer::EnqueueMediaFileUrl(const GURL& url) {
  current_playlist_.push_back(MediaUrl(url));
  NotifyPlaylistChanged();
}

void MediaPlayer::ForcePlayMediaFile(Profile* profile,
                                     const FilePath& file_path) {
  GURL url;
  if (!FileManagerUtil::ConvertFileToFileSystemUrl(profile, file_path,
                                                   GetOriginUrl(), &url)) {
    return;
  }
  ForcePlayMediaURL(url);
}

void MediaPlayer::ForcePlayMediaURL(const GURL& url) {
  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 {
    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 content::NotificationSource& source,
                          const content::NotificationDetails& details) {
  DCHECK(type == chrome::NOTIFICATION_BROWSER_CLOSED);
  registrar_.Remove(this,
                    chrome::NOTIFICATION_BROWSER_CLOSED,
                    source);
  if (content::Source<Browser>(source).ptr() == mediaplayer_browser_) {
    mediaplayer_browser_ = NULL;
  } else if (content::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(false);
  }
}

void MediaPlayer::PopupPlaylist(Browser* creator) {
  if (playlist_browser_)
    return;  // Already opened.

  Profile* profile = BrowserList::GetLastActive()->profile();
  playlist_browser_ = Browser::CreateForApp(Browser::TYPE_PANEL,
                                            kMediaPlayerAppName,
                                            gfx::Rect(),
                                            profile);
  registrar_.Add(this,
                 chrome::NOTIFICATION_BROWSER_CLOSED,
                 content::Source<Browser>(playlist_browser_));
  playlist_browser_->AddSelectedTabWithURL(GetMediaplayerPlaylistUrl(),
                                           content::PAGE_TRANSITION_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;
  }
  if (mediaplayer_browser_)
    return;  // Already opened.

  Profile* profile = BrowserList::GetLastActive()->profile();
  mediaplayer_browser_ = Browser::CreateForApp(Browser::TYPE_PANEL,
                                               kMediaPlayerAppName,
                                               gfx::Rect(),
                                               profile);
  registrar_.Add(this,
                 chrome::NOTIFICATION_BROWSER_CLOSED,
                 content::Source<Browser>(mediaplayer_browser_));

#if defined(OS_CHROMEOS) && defined(TOOLKIT_USES_GTK)
  // 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
  mediaplayer_browser_->AddSelectedTabWithURL(GetMediaPlayerUrl(),
                                              content::PAGE_TRANSITION_LINK);
  mediaplayer_browser_->window()->SetBounds(gfx::Rect(kPopupLeft,
                                                      kPopupTop,
                                                      kPopupWidth,
                                                      kPopupHeight));
  mediaplayer_browser_->window()->Show();
}

net::URLRequestJob* MediaPlayer::MaybeIntercept(net::URLRequest* request) {
  // Don't attempt to intercept here as we want to wait until the mime
  // type is fully determined.
  return NULL;
}

// This is the list of mime types currently supported by the Google
// Document Viewer.
static const char* const supported_mime_type_list[] = {
  "audio/mpeg",
  "video/mp4",
  "audio/mp3"
};

net::URLRequestJob* MediaPlayer::MaybeInterceptResponse(
    net::URLRequest* request) {
  // Do not intercept this request if it is a download.
  if (request->load_flags() & net::LOAD_IS_DOWNLOAD) {
    return NULL;
  }

  std::string mime_type;
  request->GetMimeType(&mime_type);
  // If it is in our list of known URLs, enqueue the url then
  // Cancel the request so the mediaplayer can handle it when
  // it hits it in the playlist.
  if (supported_mime_types_.find(mime_type) != supported_mime_types_.end()) {
    if (request->referrer() != chrome::kChromeUIMediaplayerURL &&
        !request->referrer().empty()) {
      PopupMediaPlayer(NULL);
      ForcePlayMediaURL(request->url());
      request->Cancel();
    }
  }
  return NULL;
}

GURL MediaPlayer::GetOriginUrl() const {
  return FileManagerUtil::GetMediaPlayerUrl().GetOrigin();
}

GURL MediaPlayer::GetMediaplayerPlaylistUrl() const {
  return FileManagerUtil::GetMediaPlayerPlaylistUrl();
}

GURL MediaPlayer::GetMediaPlayerUrl() const {
  return FileManagerUtil::GetMediaPlayerUrl();
}

MediaPlayer::MediaPlayer()
    : current_position_(0),
      pending_playback_request_(false),
      playlist_browser_(NULL),
      mediaplayer_browser_(NULL) {
  for (size_t i = 0; i < arraysize(supported_mime_type_list); ++i) {
    supported_mime_types_.insert(supported_mime_type_list[i]);
  }
};