Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / media / desktop_media_list_ash.cc
blob98b05c81a53cd18395d1c4b3bb7c8949e2c5a98e
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"
7 #include <map>
9 #include "ash/shell.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;
27 namespace {
29 // Update the list twice per second.
30 const int kDefaultUpdatePeriod = 500;
32 } // namespace
34 DesktopMediaListAsh::SourceDescription::SourceDescription(
35 DesktopMediaID id,
36 const base::string16& name)
37 : id(id),
38 name(name) {
41 DesktopMediaListAsh::DesktopMediaListAsh(int source_types)
42 : source_types_(source_types),
43 update_period_(base::TimeDelta::FromMilliseconds(kDefaultUpdatePeriod)),
44 thumbnail_size_(100, 100),
45 view_dialog_id_(-1),
46 observer_(NULL),
47 pending_window_capture_requests_(0),
48 weak_factory_(this) {
51 DesktopMediaListAsh::~DesktopMediaListAsh() {}
53 void DesktopMediaListAsh::SetUpdatePeriod(base::TimeDelta period) {
54 DCHECK(!observer_);
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) {
69 DCHECK(!observer_);
71 observer_ = observer;
72 Refresh();
75 int DesktopMediaListAsh::GetSourceCount() const {
76 return sources_.size();
79 const DesktopMediaList::Source& DesktopMediaListAsh::GetSource(
80 int index) const {
81 return sources_[index];
84 // static
85 bool DesktopMediaListAsh::CompareSources(const SourceDescription& a,
86 const SourceDescription& b) {
87 return a.id < b.id;
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.
100 size_t pos = 0;
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);
107 continue;
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);
120 ++pos;
123 DCHECK_EQ(new_sources.size(), sources_.size());
126 void DesktopMediaListAsh::EnumerateWindowsForRoot(
127 std::vector<DesktopMediaListAsh::SourceDescription>* sources,
128 aura::Window* root_window,
129 int container_id) {
130 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
132 aura::Window* container = ash::Shell::GetContainer(root_window, container_id);
133 if (!container)
134 return;
135 for (aura::Window::Windows::const_iterator it = container->children().begin();
136 it != container->children().end(); ++it) {
137 if (!(*it)->IsVisible() || !(*it)->CanFocus())
138 continue;
139 content::DesktopMediaID id =
140 content::DesktopMediaID::RegisterAuraWindow(*it);
141 if (id.id == view_dialog_id_)
142 continue;
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(
185 window,
186 window_rect,
187 scaled_rect.size(),
188 BrowserThread::GetBlockingPool(),
189 base::Bind(&DesktopMediaListAsh::OnThumbnailCaptured,
190 weak_factory_.GetWeakPtr(),
191 id));
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);
200 break;
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
209 // update.
210 BrowserThread::PostDelayedTask(
211 BrowserThread::UI, FROM_HERE,
212 base::Bind(&DesktopMediaListAsh::Refresh,
213 weak_factory_.GetWeakPtr()),
214 update_period_);