Home | History | Annotate | Download | only in extensions
      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.
      4 
      5 #include "chrome/browser/chromeos/extensions/media_player_api.h"
      6 
      7 #include "base/lazy_instance.h"
      8 #include "base/values.h"
      9 #include "chrome/browser/chromeos/extensions/media_player_event_router.h"
     10 #include "chrome/browser/chromeos/media/media_player.h"
     11 
     12 namespace {
     13 
     14 static const char kPropertyItems[] = "items";
     15 static const char kPropertyPosition[] = "position";
     16 
     17 }  // namespace
     18 
     19 namespace extensions {
     20 
     21 bool MediaPlayerPrivatePlayFunction::RunImpl() {
     22   if (args_->GetSize() < 2) {
     23     return false;
     24   }
     25 
     26   ListValue* url_list = NULL;
     27   if (!args_->GetList(0, &url_list))
     28     return false;
     29 
     30   int position;
     31   if (!args_->GetInteger(1, &position))
     32     return false;
     33 
     34   MediaPlayer* player = MediaPlayer::GetInstance();
     35 
     36   player->PopupMediaPlayer();
     37   player->ClearPlaylist();
     38 
     39   size_t len = url_list->GetSize();
     40   for (size_t i = 0; i < len; ++i) {
     41     std::string path;
     42     url_list->GetString(i, &path);
     43     player->EnqueueMediaFileUrl(GURL(path));
     44   }
     45 
     46   player->SetPlaylistPosition(position);
     47   player->NotifyPlaylistChanged();
     48 
     49   return true;
     50 }
     51 
     52 static ListValue* GetPlaylistItems() {
     53   ListValue* result = new ListValue();
     54 
     55   MediaPlayer::UrlVector const& src = MediaPlayer::GetInstance()->GetPlaylist();
     56 
     57   for (size_t i = 0; i < src.size(); i++) {
     58     result->Append(new base::StringValue(src[i].spec()));
     59   }
     60   return result;
     61 }
     62 
     63 bool MediaPlayerPrivateGetPlaylistFunction::RunImpl() {
     64   DictionaryValue* result = new DictionaryValue();
     65   MediaPlayer* player = MediaPlayer::GetInstance();
     66 
     67   result->Set(kPropertyItems, GetPlaylistItems());
     68   result->SetInteger(kPropertyPosition, player->GetPlaylistPosition());
     69 
     70   SetResult(result);
     71   return true;
     72 }
     73 
     74 // TODO(kaznacheev): rename the API method to adjustWindowHeight here and in
     75 // media_player_private.json.
     76 bool MediaPlayerPrivateSetWindowHeightFunction::RunImpl() {
     77   int height_diff;
     78   if (!args_->GetInteger(0, &height_diff))
     79     return false;
     80   MediaPlayer::GetInstance()->AdjustWindowHeight(height_diff);
     81   return true;
     82 }
     83 
     84 bool MediaPlayerPrivateCloseWindowFunction::RunImpl() {
     85   MediaPlayer::GetInstance()->CloseWindow();
     86   return true;
     87 }
     88 
     89 MediaPlayerAPI::MediaPlayerAPI(Profile* profile)
     90     : profile_(profile) {
     91 }
     92 
     93 MediaPlayerAPI::~MediaPlayerAPI() {
     94 }
     95 
     96 // static
     97 MediaPlayerAPI* MediaPlayerAPI::Get(Profile* profile) {
     98   return ProfileKeyedAPIFactory<MediaPlayerAPI>::GetForProfile(profile);
     99 }
    100 
    101 MediaPlayerEventRouter* MediaPlayerAPI::media_player_event_router() {
    102   if (!media_player_event_router_)
    103     media_player_event_router_.reset(new MediaPlayerEventRouter(profile_));
    104   return media_player_event_router_.get();
    105 }
    106 
    107 static base::LazyInstance<ProfileKeyedAPIFactory<MediaPlayerAPI> >
    108 g_factory = LAZY_INSTANCE_INITIALIZER;
    109 
    110 // static
    111 ProfileKeyedAPIFactory<MediaPlayerAPI>* MediaPlayerAPI::GetFactoryInstance() {
    112   return &g_factory.Get();
    113 }
    114 
    115 }  // namespace extensions
    116