1 // Copyright 2013 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/media/desktop_media_list_ash.h"
10 #include "ash/shell_window_ids.h"
11 #include "base/hash.h"
12 #include "base/logging.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/threading/sequenced_worker_pool.h"
15 #include "chrome/browser/media/desktop_media_list_observer.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "grit/generated_resources.h"
18 #include "media/base/video_util.h"
19 #include "ui/base/l10n/l10n_util.h"
20 #include "ui/compositor/dip_util.h"
21 #include "ui/gfx/image/image.h"
22 #include "ui/snapshot/snapshot.h"
24 using content::BrowserThread
;
25 using content::DesktopMediaID
;
29 // Update the list twice per second.
30 const int kDefaultUpdatePeriod
= 500;
34 DesktopMediaListAsh::SourceDescription::SourceDescription(
36 const base::string16
& name
)
41 DesktopMediaListAsh::DesktopMediaListAsh(int source_types
)
42 : source_types_(source_types
),
43 update_period_(base::TimeDelta::FromMilliseconds(kDefaultUpdatePeriod
)),
44 thumbnail_size_(100, 100),
47 pending_window_capture_requests_(0),
51 DesktopMediaListAsh::~DesktopMediaListAsh() {}
53 void DesktopMediaListAsh::SetUpdatePeriod(base::TimeDelta period
) {
55 update_period_
= period
;
58 void DesktopMediaListAsh::SetThumbnailSize(
59 const gfx::Size
& thumbnail_size
) {
60 thumbnail_size_
= thumbnail_size
;
63 void DesktopMediaListAsh::SetViewDialogWindowId(
64 content::DesktopMediaID::Id dialog_id
) {
65 view_dialog_id_
= dialog_id
;
68 void DesktopMediaListAsh::StartUpdating(DesktopMediaListObserver
* observer
) {
75 int DesktopMediaListAsh::GetSourceCount() const {
76 return sources_
.size();
79 const DesktopMediaList::Source
& DesktopMediaListAsh::GetSource(
81 return sources_
[index
];
85 bool DesktopMediaListAsh::CompareSources(const SourceDescription
& a
,
86 const SourceDescription
& b
) {
90 void DesktopMediaListAsh::Refresh() {
91 std::vector
<SourceDescription
> new_sources
;
92 EnumerateSources(&new_sources
);
94 // Sort the list of sources so that they appear in a predictable order.
95 std::sort(new_sources
.begin(), new_sources
.end(), CompareSources
);
97 // Step through |new_sources| adding and removing entries from |sources_|, and
98 // notifying the |observer_|, until two match. Requires that |sources| and
99 // |sources_| have the same ordering.
101 while (pos
< sources_
.size() || pos
< new_sources
.size()) {
102 // If |sources_[pos]| is not in |new_sources| then remove it.
103 if (pos
< sources_
.size() &&
104 (pos
== new_sources
.size() || sources_
[pos
].id
< new_sources
[pos
].id
)) {
105 sources_
.erase(sources_
.begin() + pos
);
106 observer_
->OnSourceRemoved(pos
);
110 if (pos
== sources_
.size() || !(sources_
[pos
].id
== new_sources
[pos
].id
)) {
111 sources_
.insert(sources_
.begin() + pos
, Source());
112 sources_
[pos
].id
= new_sources
[pos
].id
;
113 sources_
[pos
].name
= new_sources
[pos
].name
;
114 observer_
->OnSourceAdded(pos
);
115 } else if (sources_
[pos
].name
!= new_sources
[pos
].name
) {
116 sources_
[pos
].name
= new_sources
[pos
].name
;
117 observer_
->OnSourceNameChanged(pos
);
123 DCHECK_EQ(new_sources
.size(), sources_
.size());
126 void DesktopMediaListAsh::EnumerateWindowsForRoot(
127 std::vector
<DesktopMediaListAsh::SourceDescription
>* sources
,
128 aura::Window
* root_window
,
130 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
132 aura::Window
* container
= ash::Shell::GetContainer(root_window
, container_id
);
135 for (aura::Window::Windows::const_iterator it
= container
->children().begin();
136 it
!= container
->children().end(); ++it
) {
137 if (!(*it
)->IsVisible() || !(*it
)->CanFocus())
139 content::DesktopMediaID id
=
140 content::DesktopMediaID::RegisterAuraWindow(*it
);
141 if (id
.id
== view_dialog_id_
)
143 SourceDescription
window_source(id
, (*it
)->title());
144 sources
->push_back(window_source
);
146 CaptureThumbnail(window_source
.id
, *it
);
150 void DesktopMediaListAsh::EnumerateSources(
151 std::vector
<DesktopMediaListAsh::SourceDescription
>* sources
) {
152 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
154 aura::Window::Windows root_windows
= ash::Shell::GetAllRootWindows();
156 for (aura::Window::Windows::const_iterator iter
= root_windows
.begin();
157 iter
!= root_windows
.end(); ++iter
) {
158 if (source_types_
& SCREENS
) {
159 SourceDescription
screen_source(
160 content::DesktopMediaID::RegisterAuraWindow(*iter
), (*iter
)->title());
161 sources
->push_back(screen_source
);
163 CaptureThumbnail(screen_source
.id
, *iter
);
166 if (source_types_
& WINDOWS
) {
167 EnumerateWindowsForRoot(
168 sources
, *iter
, ash::internal::kShellWindowId_DefaultContainer
);
169 EnumerateWindowsForRoot(
170 sources
, *iter
, ash::internal::kShellWindowId_AlwaysOnTopContainer
);
171 EnumerateWindowsForRoot(
172 sources
, *iter
, ash::internal::kShellWindowId_DockedContainer
);
177 void DesktopMediaListAsh::CaptureThumbnail(content::DesktopMediaID id
,
178 aura::Window
* window
) {
179 gfx::Rect
window_rect(window
->bounds().width(), window
->bounds().height());
180 gfx::Rect scaled_rect
= media::ComputeLetterboxRegion(
181 gfx::Rect(thumbnail_size_
), window_rect
.size());
183 ++pending_window_capture_requests_
;
184 ui::GrabWindowSnapshotAndScaleAsync(
188 BrowserThread::GetBlockingPool(),
189 base::Bind(&DesktopMediaListAsh::OnThumbnailCaptured
,
190 weak_factory_
.GetWeakPtr(),
194 void DesktopMediaListAsh::OnThumbnailCaptured(content::DesktopMediaID id
,
195 const gfx::Image
& image
) {
196 for (size_t i
= 0; i
< sources_
.size(); ++i
) {
197 if (sources_
[i
].id
== id
) {
198 sources_
[i
].thumbnail
= image
.AsImageSkia();
199 observer_
->OnSourceThumbnailChanged(i
);
204 --pending_window_capture_requests_
;
205 DCHECK_GE(pending_window_capture_requests_
, 0);
207 if (!pending_window_capture_requests_
) {
208 // Once we've finished capturing all windows post a task for the next list
210 BrowserThread::PostDelayedTask(
211 BrowserThread::UI
, FROM_HERE
,
212 base::Bind(&DesktopMediaListAsh::Refresh
,
213 weak_factory_
.GetWeakPtr()),