| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/media/desktop_media_list_ash.h" | |
| 6 | |
| 7 #include "ash/common/shell_window_ids.h" | |
| 8 #include "ash/shell.h" | |
| 9 #include "chrome/grit/generated_resources.h" | |
| 10 #include "content/public/browser/browser_thread.h" | |
| 11 #include "media/base/video_util.h" | |
| 12 #include "ui/base/l10n/l10n_util.h" | |
| 13 #include "ui/gfx/image/image.h" | |
| 14 #include "ui/snapshot/snapshot.h" | |
| 15 | |
| 16 using content::BrowserThread; | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 // Update the list twice per second. | |
| 21 const int kDefaultUpdatePeriod = 500; | |
| 22 | |
| 23 } // namespace | |
| 24 | |
| 25 DesktopMediaListAsh::DesktopMediaListAsh(int source_types) | |
| 26 : DesktopMediaListBase( | |
| 27 base::TimeDelta::FromMilliseconds(kDefaultUpdatePeriod)), | |
| 28 source_types_(source_types), | |
| 29 pending_window_capture_requests_(0), | |
| 30 weak_factory_(this) {} | |
| 31 | |
| 32 DesktopMediaListAsh::~DesktopMediaListAsh() {} | |
| 33 | |
| 34 void DesktopMediaListAsh::Refresh() { | |
| 35 std::vector<SourceDescription> new_sources; | |
| 36 EnumerateSources(&new_sources); | |
| 37 | |
| 38 UpdateSourcesList(new_sources); | |
| 39 } | |
| 40 | |
| 41 void DesktopMediaListAsh::EnumerateWindowsForRoot( | |
| 42 std::vector<DesktopMediaListAsh::SourceDescription>* sources, | |
| 43 aura::Window* root_window, | |
| 44 int container_id) { | |
| 45 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 46 | |
| 47 aura::Window* container = ash::Shell::GetContainer(root_window, container_id); | |
| 48 if (!container) | |
| 49 return; | |
| 50 for (aura::Window::Windows::const_iterator it = container->children().begin(); | |
| 51 it != container->children().end(); ++it) { | |
| 52 if (!(*it)->IsVisible() || !(*it)->CanFocus()) | |
| 53 continue; | |
| 54 content::DesktopMediaID id = content::DesktopMediaID::RegisterAuraWindow( | |
| 55 content::DesktopMediaID::TYPE_WINDOW, *it); | |
| 56 if (id.aura_id == view_dialog_id_.aura_id) | |
| 57 continue; | |
| 58 SourceDescription window_source(id, (*it)->title()); | |
| 59 sources->push_back(window_source); | |
| 60 | |
| 61 CaptureThumbnail(window_source.id, *it); | |
| 62 } | |
| 63 } | |
| 64 | |
| 65 void DesktopMediaListAsh::EnumerateSources( | |
| 66 std::vector<DesktopMediaListAsh::SourceDescription>* sources) { | |
| 67 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 68 | |
| 69 aura::Window::Windows root_windows = ash::Shell::GetAllRootWindows(); | |
| 70 | |
| 71 for (size_t i = 0; i < root_windows.size(); ++i) { | |
| 72 if (source_types_ & SCREENS) { | |
| 73 SourceDescription screen_source( | |
| 74 content::DesktopMediaID::RegisterAuraWindow( | |
| 75 content::DesktopMediaID::TYPE_SCREEN, root_windows[i]), | |
| 76 root_windows[i]->title()); | |
| 77 | |
| 78 if (root_windows[i] == ash::Shell::GetPrimaryRootWindow()) | |
| 79 sources->insert(sources->begin(), screen_source); | |
| 80 else | |
| 81 sources->push_back(screen_source); | |
| 82 | |
| 83 if (screen_source.name.empty()) { | |
| 84 if (root_windows.size() > 1) { | |
| 85 // 'Screen' in 'Screen 1, Screen 2, etc ' might be inflected in some | |
| 86 // languages depending on the number although rather unlikely. To be | |
| 87 // safe, use the plural format. | |
| 88 // TODO(jshin): Revert to GetStringFUTF16Int (with native digits) | |
| 89 // if none of UI languages inflects 'Screen' in this context. | |
| 90 screen_source.name = l10n_util::GetPluralStringFUTF16( | |
| 91 IDS_DESKTOP_MEDIA_PICKER_MULTIPLE_SCREEN_NAME, | |
| 92 static_cast<int>(i + 1)); | |
| 93 } else { | |
| 94 screen_source.name = l10n_util::GetStringUTF16( | |
| 95 IDS_DESKTOP_MEDIA_PICKER_SINGLE_SCREEN_NAME); | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 CaptureThumbnail(screen_source.id, root_windows[i]); | |
| 100 } | |
| 101 | |
| 102 if (source_types_ & WINDOWS) { | |
| 103 EnumerateWindowsForRoot( | |
| 104 sources, root_windows[i], ash::kShellWindowId_DefaultContainer); | |
| 105 EnumerateWindowsForRoot( | |
| 106 sources, root_windows[i], ash::kShellWindowId_AlwaysOnTopContainer); | |
| 107 EnumerateWindowsForRoot( | |
| 108 sources, root_windows[i], ash::kShellWindowId_DockedContainer); | |
| 109 } | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 void DesktopMediaListAsh::CaptureThumbnail(content::DesktopMediaID id, | |
| 114 aura::Window* window) { | |
| 115 gfx::Rect window_rect(window->bounds().width(), window->bounds().height()); | |
| 116 gfx::Rect scaled_rect = media::ComputeLetterboxRegion( | |
| 117 gfx::Rect(thumbnail_size_), window_rect.size()); | |
| 118 | |
| 119 ++pending_window_capture_requests_; | |
| 120 ui::GrabWindowSnapshotAndScaleAsync( | |
| 121 window, | |
| 122 window_rect, | |
| 123 scaled_rect.size(), | |
| 124 BrowserThread::GetBlockingPool(), | |
| 125 base::Bind(&DesktopMediaListAsh::OnThumbnailCaptured, | |
| 126 weak_factory_.GetWeakPtr(), | |
| 127 id)); | |
| 128 } | |
| 129 | |
| 130 void DesktopMediaListAsh::OnThumbnailCaptured(content::DesktopMediaID id, | |
| 131 const gfx::Image& image) { | |
| 132 UpdateSourceThumbnail(id, image.AsImageSkia()); | |
| 133 | |
| 134 --pending_window_capture_requests_; | |
| 135 DCHECK_GE(pending_window_capture_requests_, 0); | |
| 136 | |
| 137 if (!pending_window_capture_requests_) { | |
| 138 // Once we've finished capturing all windows post a task for the next list | |
| 139 // update. | |
| 140 ScheduleNextRefresh(); | |
| 141 } | |
| 142 } | |
| OLD | NEW |