Infobar material design refresh: layout
[chromium-blink-merge.git] / chrome / browser / ui / ash / media_delegate_chromeos.cc
blob3ea4aa4c33146b8d5fce7309dcc1a48fa54957e4
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"
7 #include "ash/shell.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"
24 namespace {
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();
46 ++iter) {
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)
51 continue;
52 GetMediaCaptureState(indicator, web_contents, media_state_out);
53 if (*media_state_out == ash::MEDIA_CAPTURE_AUDIO_VIDEO)
54 return;
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 =
66 apps.begin();
67 iter != apps.end();
68 ++iter) {
69 GetMediaCaptureState(indicator, (*iter)->web_contents(), media_state_out);
70 if (*media_state_out == ash::MEDIA_CAPTURE_AUDIO_VIDEO)
71 return;
75 void GetExtensionMediaCaptureState(
76 const MediaStreamCaptureIndicator* indicator,
77 content::BrowserContext* context,
78 int* media_state_out) {
79 for (content::RenderFrameHost* host :
80 extensions::ProcessManager::Get(context)->GetAllFrames()) {
81 content::WebContents* web_contents =
82 content::WebContents::FromRenderFrameHost(host);
83 // RFH may not have web contents.
84 if (!web_contents)
85 continue;
86 GetMediaCaptureState(indicator, web_contents, media_state_out);
87 if (*media_state_out == ash::MEDIA_CAPTURE_AUDIO_VIDEO)
88 return;
92 ash::MediaCaptureState GetMediaCaptureStateOfAllWebContents(
93 content::BrowserContext* context) {
94 if (!context)
95 return ash::MEDIA_CAPTURE_NONE;
97 scoped_refptr<MediaStreamCaptureIndicator> indicator =
98 MediaCaptureDevicesDispatcher::GetInstance()
99 ->GetMediaStreamCaptureIndicator();
101 int media_state = ash::MEDIA_CAPTURE_NONE;
102 // Browser windows
103 GetBrowserMediaCaptureState(indicator.get(), context, &media_state);
104 if (media_state == ash::MEDIA_CAPTURE_AUDIO_VIDEO)
105 return ash::MEDIA_CAPTURE_AUDIO_VIDEO;
107 // App windows
108 GetAppMediaCaptureState(indicator.get(), context, &media_state);
109 if (media_state == ash::MEDIA_CAPTURE_AUDIO_VIDEO)
110 return ash::MEDIA_CAPTURE_AUDIO_VIDEO;
112 // Extensions
113 GetExtensionMediaCaptureState(indicator.get(), context, &media_state);
115 return static_cast<ash::MediaCaptureState>(media_state);
118 } // namespace
120 MediaDelegateChromeOS::MediaDelegateChromeOS() : weak_ptr_factory_(this) {
121 MediaCaptureDevicesDispatcher::GetInstance()->AddObserver(this);
124 MediaDelegateChromeOS::~MediaDelegateChromeOS() {
125 MediaCaptureDevicesDispatcher::GetInstance()->RemoveObserver(this);
128 void MediaDelegateChromeOS::HandleMediaNextTrack() {
129 extensions::MediaPlayerAPI::Get(ProfileManager::GetActiveUserProfile())
130 ->media_player_event_router()
131 ->NotifyNextTrack();
134 void MediaDelegateChromeOS::HandleMediaPlayPause() {
135 extensions::MediaPlayerAPI::Get(ProfileManager::GetActiveUserProfile())
136 ->media_player_event_router()
137 ->NotifyTogglePlayState();
140 void MediaDelegateChromeOS::HandleMediaPrevTrack() {
141 extensions::MediaPlayerAPI::Get(ProfileManager::GetActiveUserProfile())
142 ->media_player_event_router()
143 ->NotifyPrevTrack();
146 ash::MediaCaptureState MediaDelegateChromeOS::GetMediaCaptureState(
147 content::BrowserContext* context) {
148 return GetMediaCaptureStateOfAllWebContents(context);
151 void MediaDelegateChromeOS::OnRequestUpdate(
152 int render_process_id,
153 int render_frame_id,
154 content::MediaStreamType stream_type,
155 const content::MediaRequestState state) {
156 base::MessageLoopForUI::current()->PostTask(
157 FROM_HERE,
158 base::Bind(&MediaDelegateChromeOS::NotifyMediaCaptureChange,
159 weak_ptr_factory_.GetWeakPtr()));
162 void MediaDelegateChromeOS::NotifyMediaCaptureChange() {
163 ash::Shell::GetInstance()
164 ->system_tray_notifier()
165 ->NotifyMediaCaptureChanged();