1 // Copyright 2014 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/ui/ash/media_delegate_chromeos.h"
8 #include "ash/system/tray/system_tray_notifier.h"
9 #include "base/message_loop/message_loop.h"
10 #include "chrome/browser/chromeos/extensions/media_player_api.h"
11 #include "chrome/browser/chromeos/extensions/media_player_event_router.h"
12 #include "chrome/browser/media/media_stream_capture_indicator.h"
13 #include "chrome/browser/profiles/profile_manager.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_list.h"
16 #include "chrome/browser/ui/browser_window.h"
17 #include "chrome/browser/ui/tabs/tab_strip_model.h"
18 #include "content/public/browser/render_view_host.h"
19 #include "content/public/browser/web_contents.h"
20 #include "extensions/browser/app_window/app_window.h"
21 #include "extensions/browser/app_window/app_window_registry.h"
22 #include "extensions/browser/process_manager.h"
26 void GetMediaCaptureState(
27 const MediaStreamCaptureIndicator
* indicator
,
28 content::WebContents
* web_contents
,
29 int* media_state_out
) {
30 if (indicator
->IsCapturingVideo(web_contents
))
31 *media_state_out
|= ash::MEDIA_CAPTURE_VIDEO
;
32 if (indicator
->IsCapturingAudio(web_contents
))
33 *media_state_out
|= ash::MEDIA_CAPTURE_AUDIO
;
36 void GetBrowserMediaCaptureState(
37 const MediaStreamCaptureIndicator
* indicator
,
38 const content::BrowserContext
* context
,
39 int* media_state_out
) {
41 const BrowserList
* desktop_list
=
42 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH
);
44 for (BrowserList::BrowserVector::const_iterator iter
= desktop_list
->begin();
45 iter
!= desktop_list
->end();
47 TabStripModel
* tab_strip_model
= (*iter
)->tab_strip_model();
48 for (int i
= 0; i
< tab_strip_model
->count(); ++i
) {
49 content::WebContents
* web_contents
= tab_strip_model
->GetWebContentsAt(i
);
50 if (web_contents
->GetBrowserContext() != context
)
52 GetMediaCaptureState(indicator
, web_contents
, media_state_out
);
53 if (*media_state_out
== ash::MEDIA_CAPTURE_AUDIO_VIDEO
)
59 void GetAppMediaCaptureState(
60 const MediaStreamCaptureIndicator
* indicator
,
61 content::BrowserContext
* context
,
62 int* media_state_out
) {
63 const extensions::AppWindowRegistry::AppWindowList
& apps
=
64 extensions::AppWindowRegistry::Get(context
)->app_windows();
65 for (extensions::AppWindowRegistry::AppWindowList::const_iterator iter
=
69 GetMediaCaptureState(indicator
, (*iter
)->web_contents(), media_state_out
);
70 if (*media_state_out
== ash::MEDIA_CAPTURE_AUDIO_VIDEO
)
75 void GetExtensionMediaCaptureState(
76 const MediaStreamCaptureIndicator
* indicator
,
77 content::BrowserContext
* context
,
78 int* media_state_out
) {
79 const extensions::ProcessManager::ViewSet view_set
=
80 extensions::ProcessManager::Get(context
)->GetAllViews();
81 for (extensions::ProcessManager::ViewSet::const_iterator iter
=
83 iter
!= view_set
.end();
85 content::WebContents
* web_contents
=
86 content::WebContents::FromRenderViewHost(*iter
);
87 // RVH may not have web contents.
90 GetMediaCaptureState(indicator
, web_contents
, media_state_out
);
91 if (*media_state_out
== ash::MEDIA_CAPTURE_AUDIO_VIDEO
)
96 ash::MediaCaptureState
GetMediaCaptureStateOfAllWebContents(
97 content::BrowserContext
* context
) {
99 return ash::MEDIA_CAPTURE_NONE
;
101 scoped_refptr
<MediaStreamCaptureIndicator
> indicator
=
102 MediaCaptureDevicesDispatcher::GetInstance()
103 ->GetMediaStreamCaptureIndicator();
105 int media_state
= ash::MEDIA_CAPTURE_NONE
;
107 GetBrowserMediaCaptureState(indicator
.get(), context
, &media_state
);
108 if (media_state
== ash::MEDIA_CAPTURE_AUDIO_VIDEO
)
109 return ash::MEDIA_CAPTURE_AUDIO_VIDEO
;
112 GetAppMediaCaptureState(indicator
.get(), context
, &media_state
);
113 if (media_state
== ash::MEDIA_CAPTURE_AUDIO_VIDEO
)
114 return ash::MEDIA_CAPTURE_AUDIO_VIDEO
;
117 GetExtensionMediaCaptureState(indicator
.get(), context
, &media_state
);
119 return static_cast<ash::MediaCaptureState
>(media_state
);
124 MediaDelegateChromeOS::MediaDelegateChromeOS() : weak_ptr_factory_(this) {
125 MediaCaptureDevicesDispatcher::GetInstance()->AddObserver(this);
128 MediaDelegateChromeOS::~MediaDelegateChromeOS() {
129 MediaCaptureDevicesDispatcher::GetInstance()->RemoveObserver(this);
132 void MediaDelegateChromeOS::HandleMediaNextTrack() {
133 extensions::MediaPlayerAPI::Get(ProfileManager::GetActiveUserProfile())
134 ->media_player_event_router()
138 void MediaDelegateChromeOS::HandleMediaPlayPause() {
139 extensions::MediaPlayerAPI::Get(ProfileManager::GetActiveUserProfile())
140 ->media_player_event_router()
141 ->NotifyTogglePlayState();
144 void MediaDelegateChromeOS::HandleMediaPrevTrack() {
145 extensions::MediaPlayerAPI::Get(ProfileManager::GetActiveUserProfile())
146 ->media_player_event_router()
150 ash::MediaCaptureState
MediaDelegateChromeOS::GetMediaCaptureState(
151 content::BrowserContext
* context
) {
152 return GetMediaCaptureStateOfAllWebContents(context
);
155 void MediaDelegateChromeOS::OnRequestUpdate(
156 int render_process_id
,
158 content::MediaStreamType stream_type
,
159 const content::MediaRequestState state
) {
160 base::MessageLoopForUI::current()->PostTask(
162 base::Bind(&MediaDelegateChromeOS::NotifyMediaCaptureChange
,
163 weak_ptr_factory_
.GetWeakPtr()));
166 void MediaDelegateChromeOS::NotifyMediaCaptureChange() {
167 ash::Shell::GetInstance()
168 ->system_tray_notifier()
169 ->NotifyMediaCaptureChanged();