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
|
// Copyright 2014 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/ui/ash/media_delegate_chromeos.h"
#include "ash/shell.h"
#include "ash/system/tray/system_tray_notifier.h"
#include "base/message_loop/message_loop.h"
#include "chrome/browser/chromeos/extensions/media_player_api.h"
#include "chrome/browser/chromeos/extensions/media_player_event_router.h"
#include "chrome/browser/media/media_stream_capture_indicator.h"
#include "chrome/browser/profiles/profile_manager.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/tabs/tab_strip_model.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/app_window/app_window.h"
#include "extensions/browser/app_window/app_window_registry.h"
#include "extensions/browser/process_manager.h"
namespace {
void GetMediaCaptureState(
const MediaStreamCaptureIndicator* indicator,
content::WebContents* web_contents,
int* media_state_out) {
if (indicator->IsCapturingVideo(web_contents))
*media_state_out |= ash::MEDIA_CAPTURE_VIDEO;
if (indicator->IsCapturingAudio(web_contents))
*media_state_out |= ash::MEDIA_CAPTURE_AUDIO;
}
void GetBrowserMediaCaptureState(
const MediaStreamCaptureIndicator* indicator,
const content::BrowserContext* context,
int* media_state_out) {
const BrowserList* desktop_list =
BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH);
for (BrowserList::BrowserVector::const_iterator iter = desktop_list->begin();
iter != desktop_list->end();
++iter) {
TabStripModel* tab_strip_model = (*iter)->tab_strip_model();
for (int i = 0; i < tab_strip_model->count(); ++i) {
content::WebContents* web_contents = tab_strip_model->GetWebContentsAt(i);
if (web_contents->GetBrowserContext() != context)
continue;
GetMediaCaptureState(indicator, web_contents, media_state_out);
if (*media_state_out == ash::MEDIA_CAPTURE_AUDIO_VIDEO)
return;
}
}
}
void GetAppMediaCaptureState(
const MediaStreamCaptureIndicator* indicator,
content::BrowserContext* context,
int* media_state_out) {
const extensions::AppWindowRegistry::AppWindowList& apps =
extensions::AppWindowRegistry::Get(context)->app_windows();
for (extensions::AppWindowRegistry::AppWindowList::const_iterator iter =
apps.begin();
iter != apps.end();
++iter) {
GetMediaCaptureState(indicator, (*iter)->web_contents(), media_state_out);
if (*media_state_out == ash::MEDIA_CAPTURE_AUDIO_VIDEO)
return;
}
}
void GetExtensionMediaCaptureState(
const MediaStreamCaptureIndicator* indicator,
content::BrowserContext* context,
int* media_state_out) {
const extensions::ProcessManager::ViewSet view_set =
extensions::ProcessManager::Get(context)->GetAllViews();
for (extensions::ProcessManager::ViewSet::const_iterator iter =
view_set.begin();
iter != view_set.end();
++iter) {
content::WebContents* web_contents =
content::WebContents::FromRenderViewHost(*iter);
// RVH may not have web contents.
if (!web_contents)
continue;
GetMediaCaptureState(indicator, web_contents, media_state_out);
if (*media_state_out == ash::MEDIA_CAPTURE_AUDIO_VIDEO)
return;
}
}
ash::MediaCaptureState GetMediaCaptureStateOfAllWebContents(
content::BrowserContext* context) {
if (!context)
return ash::MEDIA_CAPTURE_NONE;
scoped_refptr<MediaStreamCaptureIndicator> indicator =
MediaCaptureDevicesDispatcher::GetInstance()
->GetMediaStreamCaptureIndicator();
int media_state = ash::MEDIA_CAPTURE_NONE;
// Browser windows
GetBrowserMediaCaptureState(indicator.get(), context, &media_state);
if (media_state == ash::MEDIA_CAPTURE_AUDIO_VIDEO)
return ash::MEDIA_CAPTURE_AUDIO_VIDEO;
// App windows
GetAppMediaCaptureState(indicator.get(), context, &media_state);
if (media_state == ash::MEDIA_CAPTURE_AUDIO_VIDEO)
return ash::MEDIA_CAPTURE_AUDIO_VIDEO;
// Extensions
GetExtensionMediaCaptureState(indicator.get(), context, &media_state);
return static_cast<ash::MediaCaptureState>(media_state);
}
} // namespace
MediaDelegateChromeOS::MediaDelegateChromeOS() : weak_ptr_factory_(this) {
MediaCaptureDevicesDispatcher::GetInstance()->AddObserver(this);
}
MediaDelegateChromeOS::~MediaDelegateChromeOS() {
MediaCaptureDevicesDispatcher::GetInstance()->RemoveObserver(this);
}
void MediaDelegateChromeOS::HandleMediaNextTrack() {
extensions::MediaPlayerAPI::Get(ProfileManager::GetActiveUserProfile())
->media_player_event_router()
->NotifyNextTrack();
}
void MediaDelegateChromeOS::HandleMediaPlayPause() {
extensions::MediaPlayerAPI::Get(ProfileManager::GetActiveUserProfile())
->media_player_event_router()
->NotifyTogglePlayState();
}
void MediaDelegateChromeOS::HandleMediaPrevTrack() {
extensions::MediaPlayerAPI::Get(ProfileManager::GetActiveUserProfile())
->media_player_event_router()
->NotifyPrevTrack();
}
ash::MediaCaptureState MediaDelegateChromeOS::GetMediaCaptureState(
content::BrowserContext* context) {
return GetMediaCaptureStateOfAllWebContents(context);
}
void MediaDelegateChromeOS::OnRequestUpdate(
int render_process_id,
int render_frame_id,
content::MediaStreamType stream_type,
const content::MediaRequestState state) {
base::MessageLoopForUI::current()->PostTask(
FROM_HERE,
base::Bind(&MediaDelegateChromeOS::NotifyMediaCaptureChange,
weak_ptr_factory_.GetWeakPtr()));
}
void MediaDelegateChromeOS::NotifyMediaCaptureChange() {
ash::Shell::GetInstance()
->system_tray_notifier()
->NotifyMediaCaptureChanged();
}
|