File: desktop_media_list_ash.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (177 lines) | stat: -rw-r--r-- 6,318 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/media/webrtc/desktop_media_list_ash.h"

#include <utility>

#include "ash/public/cpp/shell_window_ids.h"
#include "ash/public/cpp/window_properties.h"
#include "ash/shell.h"
#include "ash/wm/desks/desks_util.h"
#include "ash/wm/window_properties.h"
#include "base/containers/adapters.h"
#include "base/functional/bind.h"
#include "chrome/grit/generated_resources.h"
#include "media/base/video_util.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/image/image.h"
#include "ui/snapshot/snapshot.h"

using content::DesktopMediaID;

namespace {

// Update the list twice per second.
const int kDefaultDesktopMediaListUpdatePeriod = 500;

}  // namespace

DesktopMediaListAsh::DesktopMediaListAsh(DesktopMediaList::Type type)
    : DesktopMediaListBase(
          base::Milliseconds(kDefaultDesktopMediaListUpdatePeriod)) {
  DCHECK(type == DesktopMediaList::Type::kScreen ||
         type == DesktopMediaList::Type::kWindow);
  type_ = type;
}

DesktopMediaListAsh::~DesktopMediaListAsh() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}

void DesktopMediaListAsh::Refresh(bool update_thumbnails) {
  DCHECK(can_refresh());
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK_EQ(pending_window_capture_requests_, 0);

  std::vector<SourceDescription> new_sources;
  EnumerateSources(&new_sources, update_thumbnails);
  UpdateSourcesList(new_sources);
  OnRefreshMaybeComplete();
}

void DesktopMediaListAsh::EnumerateWindowsForRoot(
    std::vector<DesktopMediaListAsh::SourceDescription>* sources,
    bool update_thumbnails,
    aura::Window* root_window,
    int container_id) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  aura::Window* container = ash::Shell::GetContainer(root_window, container_id);
  if (!container) {
    return;
  }

  // The |container| has all the top-level windows in reverse order, e.g. the
  // most top-level window is at the end. So iterate children reversely to make
  // sure |sources| is in the expected order.
  for (aura::Window* window : base::Reversed(container->children())) {
    if (!window->IsVisible() || !window->CanFocus() ||
        window->GetProperty(ash::kOverviewUiKey) ||
        window->GetProperty(ash::kExcludeInMruKey)) {
      continue;
    }

    content::DesktopMediaID id = content::DesktopMediaID::RegisterNativeWindow(
        content::DesktopMediaID::TYPE_WINDOW, window);
    if (id.window_id == view_dialog_id_.window_id) {
      continue;
    }

    SourceDescription window_source(id, window->GetTitle());
    sources->push_back(window_source);

    if (update_thumbnails) {
      CaptureThumbnail(window_source.id, window);
    }
  }
}

void DesktopMediaListAsh::EnumerateSources(
    std::vector<DesktopMediaListAsh::SourceDescription>* sources,
    bool update_thumbnails) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  aura::Window::Windows root_windows = ash::Shell::GetAllRootWindows();

  for (size_t i = 0; i < root_windows.size(); ++i) {
    if (type_ == DesktopMediaList::Type::kScreen) {
      SourceDescription screen_source(
          content::DesktopMediaID::RegisterNativeWindow(
              content::DesktopMediaID::TYPE_SCREEN, root_windows[i]),
          root_windows[i]->GetTitle());

      if (root_windows[i] == ash::Shell::GetPrimaryRootWindow())
        sources->insert(sources->begin(), screen_source);
      else
        sources->push_back(screen_source);

      if (screen_source.name.empty()) {
        if (root_windows.size() > 1) {
          // 'Screen' in 'Screen 1, Screen 2, etc ' might be inflected in some
          // languages depending on the number although rather unlikely. To be
          // safe, use the plural format.
          // TODO(jshin): Revert to GetStringFUTF16Int (with native digits)
          // if none of UI languages inflects 'Screen' in this context.
          screen_source.name = l10n_util::GetPluralStringFUTF16(
              IDS_DESKTOP_MEDIA_PICKER_MULTIPLE_SCREEN_NAME,
              static_cast<int>(i + 1));
        } else {
          screen_source.name = l10n_util::GetStringUTF16(
              IDS_DESKTOP_MEDIA_PICKER_SINGLE_SCREEN_NAME);
        }
      }

      if (update_thumbnails)
        CaptureThumbnail(screen_source.id, root_windows[i]);
    } else {
      for (int desk_id : ash::desks_util::GetDesksContainersIds()) {
        EnumerateWindowsForRoot(sources, update_thumbnails, root_windows[i],
                                desk_id);
      }

      EnumerateWindowsForRoot(sources, update_thumbnails, root_windows[i],
                              ash::kShellWindowId_AlwaysOnTopContainer);
      EnumerateWindowsForRoot(sources, update_thumbnails, root_windows[i],
                              ash::kShellWindowId_PipContainer);
      EnumerateWindowsForRoot(sources, update_thumbnails, root_windows[i],
                              ash::kShellWindowId_FloatContainer);
    }
  }
}

void DesktopMediaListAsh::CaptureThumbnail(content::DesktopMediaID id,
                                           aura::Window* window) {
  gfx::Rect window_rect(window->bounds().width(), window->bounds().height());
  gfx::Rect scaled_rect = media::ComputeLetterboxRegion(
      gfx::Rect(thumbnail_size_), window_rect.size());

  ++pending_window_capture_requests_;
  ui::GrabWindowSnapshotAndScale(
      window, window_rect, scaled_rect.size(),
      base::BindOnce(&DesktopMediaListAsh::OnThumbnailCaptured,
                     weak_factory_.GetWeakPtr(), id));
}

void DesktopMediaListAsh::OnThumbnailCaptured(content::DesktopMediaID id,
                                              gfx::Image image) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  UpdateSourceThumbnail(id, image.AsImageSkia());

  --pending_window_capture_requests_;
  DCHECK_GE(pending_window_capture_requests_, 0);

  OnRefreshMaybeComplete();
}

void DesktopMediaListAsh::OnRefreshMaybeComplete() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (pending_window_capture_requests_ == 0) {
    // Once we've finished capturing all windows, notify the caller, which will
    // post a task for the next list update if necessary.
    OnRefreshComplete();
  }
}