File: media_stream_capture_indicator.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 (806 lines) | stat: -rw-r--r-- 28,477 bytes parent folder | download | duplicates (5)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
// Copyright 2012 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/media_stream_capture_indicator.h"

#include <stddef.h>

#include <memory>
#include <string>
#include <utility>

#include "base/check_op.h"
#include "base/containers/contains.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/notreached.h"
#include "base/observer_list.h"
#include "build/build_config.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/content_settings/chrome_content_settings_utils.h"
#include "chrome/browser/status_icons/status_icon.h"
#include "chrome/browser/status_icons/status_tray.h"
#include "chrome/browser/tab_contents/tab_util.h"
#include "components/url_formatter/elide_url.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/browser/web_contents_observer.h"
#include "extensions/buildflags/buildflags.h"
#include "third_party/blink/public/mojom/mediastream/media_stream.mojom.h"
#include "ui/gfx/image/image_skia.h"

#if !BUILDFLAG(IS_ANDROID)
#include "chrome/browser/media/webrtc/media_stream_focus_delegate.h"
#include "chrome/grit/branded_strings.h"
#include "components/vector_icons/vector_icons.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/paint_vector_icon.h"
#endif

#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "base/strings/utf_string_conversions.h"
#include "chrome/common/extensions/extension_constants.h"
#include "extensions/browser/extension_registry.h"  // nogncheck
#include "extensions/common/extension.h"
#endif

#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/chromeos/policy/dlp/dlp_content_manager.h"
#endif

using content::BrowserThread;
using content::WebContents;

namespace {

#if BUILDFLAG(ENABLE_EXTENSIONS)
const extensions::Extension* GetExtension(WebContents* web_contents) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  if (!web_contents)
    return nullptr;

  extensions::ExtensionRegistry* registry =
      extensions::ExtensionRegistry::Get(web_contents->GetBrowserContext());
  return registry->enabled_extensions().GetExtensionOrAppByURL(
      web_contents->GetLastCommittedURL());
}

#endif  // BUILDFLAG(ENABLE_EXTENSIONS)

std::u16string GetTitle(WebContents* web_contents) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  if (!web_contents)
    return std::u16string();

#if BUILDFLAG(ENABLE_EXTENSIONS)
  const extensions::Extension* const extension = GetExtension(web_contents);
  if (extension)
    return base::UTF8ToUTF16(extension->name());
#endif

  return url_formatter::FormatUrlForSecurityDisplay(
      web_contents->GetLastCommittedURL());
}

bool IsDeviceCapturingTab(const blink::MediaStreamDevice& device) {
  return device.display_media_info &&
         device.display_media_info->display_surface ==
             media::mojom::DisplayCaptureSurfaceType::BROWSER;
}

bool IsDeviceCapturingWindow(const blink::MediaStreamDevice& device) {
  return device.display_media_info &&
         device.display_media_info->display_surface ==
             media::mojom::DisplayCaptureSurfaceType::WINDOW;
}

// Returns if the passed |device| is capturing the whole display. This is
// different from capturing a tab or a single window on a desktop.
bool IsDeviceCapturingDisplay(const blink::MediaStreamDevice& device) {
  return device.display_media_info &&
         device.display_media_info->display_surface ==
             media::mojom::DisplayCaptureSurfaceType::MONITOR;
}

typedef void (MediaStreamCaptureIndicator::Observer::*ObserverMethod)(
    content::WebContents* web_contents,
    bool value);

ObserverMethod GetObserverMethodToCall(const blink::MediaStreamDevice& device) {
  switch (device.type) {
    case blink::mojom::MediaStreamType::DEVICE_AUDIO_CAPTURE:
      return &MediaStreamCaptureIndicator::Observer::OnIsCapturingAudioChanged;

    case blink::mojom::MediaStreamType::DEVICE_VIDEO_CAPTURE:
      return &MediaStreamCaptureIndicator::Observer::OnIsCapturingVideoChanged;

    case blink::mojom::MediaStreamType::GUM_TAB_AUDIO_CAPTURE:
    case blink::mojom::MediaStreamType::GUM_TAB_VIDEO_CAPTURE:
      return &MediaStreamCaptureIndicator::Observer::OnIsBeingMirroredChanged;

    case blink::mojom::MediaStreamType::GUM_DESKTOP_VIDEO_CAPTURE:
    case blink::mojom::MediaStreamType::GUM_DESKTOP_AUDIO_CAPTURE:
    case blink::mojom::MediaStreamType::DISPLAY_VIDEO_CAPTURE:
    case blink::mojom::MediaStreamType::DISPLAY_AUDIO_CAPTURE:
    case blink::mojom::MediaStreamType::DISPLAY_VIDEO_CAPTURE_THIS_TAB:
    case blink::mojom::MediaStreamType::DISPLAY_VIDEO_CAPTURE_SET:
      if (IsDeviceCapturingTab(device)) {
        return &MediaStreamCaptureIndicator::Observer::OnIsCapturingTabChanged;
      }
      if (IsDeviceCapturingWindow(device)) {
        return &MediaStreamCaptureIndicator::Observer::
            OnIsCapturingWindowChanged;
      }
      if (IsDeviceCapturingDisplay(device)) {
        return &MediaStreamCaptureIndicator::Observer::
            OnIsCapturingDisplayChanged;
      }
      NOTREACHED();

    case blink::mojom::MediaStreamType::NO_SERVICE:
    case blink::mojom::MediaStreamType::NUM_MEDIA_TYPES:
      NOTREACHED();
  }
}

}  // namespace

// Stores usage counts for all the capture devices associated with a single
// WebContents instance. Instances of this class are owned by
// MediaStreamCaptureIndicator. They also observe for the destruction of their
// corresponding WebContents and trigger their own deletion from their
// MediaStreamCaptureIndicator.
class MediaStreamCaptureIndicator::WebContentsDeviceUsage
    : public content::WebContentsObserver {
 public:
  WebContentsDeviceUsage(scoped_refptr<MediaStreamCaptureIndicator> indicator,
                         WebContents* web_contents)
      : WebContentsObserver(web_contents), indicator_(std::move(indicator)) {}

  WebContentsDeviceUsage(const WebContentsDeviceUsage&) = delete;
  WebContentsDeviceUsage& operator=(const WebContentsDeviceUsage&) = delete;

  ~WebContentsDeviceUsage() override = default;

  bool IsCapturingAudio() const { return audio_stream_count_ > 0; }
  bool IsCapturingVideo() const { return video_stream_count_ > 0; }
  bool IsMirroring() const { return mirroring_stream_count_ > 0; }
  bool IsCapturingTab() const { return tab_stream_count_ > 0; }
  bool IsCapturingWindow() const { return window_stream_count_ > 0; }
  bool IsCapturingDisplay() const { return display_stream_count_ > 0; }

  std::unique_ptr<content::MediaStreamUI> RegisterMediaStream(
      const blink::mojom::StreamDevices& devices,
      std::unique_ptr<MediaStreamUI> ui,
      const std::u16string application_title);

  // Increment ref-counts up based on the type of each device provided.
  void AddDevices(const blink::mojom::StreamDevices& devices,
                  base::OnceClosure stop_callback,
                  int stop_callback_id);

  // Decrement ref-counts up based on the type of each device provided.
  void RemoveDevices(const blink::mojom::StreamDevices& devices,
                     int stop_callback_id);

  void StopMediaCapturing(int media_type);

 private:
  int& GetStreamCount(const blink::MediaStreamDevice& device);

  void AddDevice(const blink::MediaStreamDevice& device);

  void RemoveDevice(const blink::MediaStreamDevice& device);

  // content::WebContentsObserver overrides.
  void WebContentsDestroyed() override {
    indicator_->UnregisterWebContents(web_contents());
  }

  void StopCallbacks(std::map<int, base::OnceClosure>& stop_callbacks);

  scoped_refptr<MediaStreamCaptureIndicator> indicator_;
  int audio_stream_count_ = 0;
  int video_stream_count_ = 0;
  int mirroring_stream_count_ = 0;
  int tab_stream_count_ = 0;
  int window_stream_count_ = 0;
  int display_stream_count_ = 0;

  std::map<int, base::OnceClosure> display_media_stop_callbacks_;
  std::map<int, base::OnceClosure> user_media_stop_callbacks_;
  base::WeakPtrFactory<WebContentsDeviceUsage> weak_factory_{this};
};

// Implements MediaStreamUI interface. Instances of this class are created for
// each MediaStream and their ownership is passed to MediaStream implementation
// in the content layer. Each UIDelegate keeps a weak pointer to the
// corresponding WebContentsDeviceUsage object to deliver updates about state of
// the stream.
class MediaStreamCaptureIndicator::UIDelegate : public content::MediaStreamUI {
 public:
  UIDelegate(WebContents* web_contents,
             base::WeakPtr<WebContentsDeviceUsage> device_usage,
             const blink::mojom::StreamDevices& devices,
             std::unique_ptr<::MediaStreamUI> ui,
             const std::u16string application_title)
      : device_usage_(device_usage),
        devices_(devices),
        ui_(std::move(ui)),
#if !BUILDFLAG(IS_ANDROID)
        focus_delegate_(web_contents),
#endif
        application_title_(std::move(application_title)),
        stop_callback_id_(MediaStreamCaptureIndicator::g_stop_callback_id_++) {
    DCHECK_CURRENTLY_ON(BrowserThread::UI);
    DCHECK(devices_.audio_device.has_value() ||
           devices_.video_device.has_value());
  }

  UIDelegate(const UIDelegate&) = delete;
  UIDelegate& operator=(const UIDelegate&) = delete;

  ~UIDelegate() override {
    if (started_ && device_usage_)
      device_usage_->RemoveDevices(devices_, stop_callback_id_);
  }

 private:
  // content::MediaStreamUI interface.
  gfx::NativeViewId OnStarted(
      base::RepeatingClosure stop_callback,
      content::MediaStreamUI::SourceCallback source_callback,
      const std::string& label,
      std::vector<content::DesktopMediaID> screen_capture_ids,
      StateChangeCallback state_change_callback) override {
    if (started_) {
      // Ignore possibly-compromised renderers that might call
      // MediaStreamDispatcherHost::OnStreamStarted() more than once.
      // See: https://crbug.com/1155426
      return 0;
    }
    started_ = true;

    if (device_usage_) {
      // |device_usage_| handles |stop_callback| when |ui_| is unspecified.
      device_usage_->AddDevices(devices_, stop_callback, stop_callback_id_);
    }

#if BUILDFLAG(IS_CHROMEOS)
    policy::DlpContentManager::Get()->OnScreenShareStarted(
        label, screen_capture_ids, application_title_, stop_callback,
        state_change_callback, source_callback);
#endif

    // If a custom |ui_| is specified, notify it that the stream started and let
    // it handle the |stop_callback| and |source_callback|.
    if (ui_)
      return ui_->OnStarted(stop_callback, std::move(source_callback),
                            screen_capture_ids);

    return 0;
  }

  void OnDeviceStoppedForSourceChange(
      const std::string& label,
      const content::DesktopMediaID& old_media_id,
      const content::DesktopMediaID& new_media_id,
      bool captured_surface_control_active) override {
#if BUILDFLAG(IS_CHROMEOS)
    policy::DlpContentManager::Get()->OnScreenShareSourceChanging(
        label, old_media_id, new_media_id, captured_surface_control_active);
#endif
  }

  void OnDeviceStopped(const std::string& label,
                       const content::DesktopMediaID& media_id) override {
#if BUILDFLAG(IS_CHROMEOS)
    policy::DlpContentManager::Get()->OnScreenShareStopped(label, media_id);
#endif
  }

  void OnRegionCaptureRectChanged(
      const std::optional<gfx::Rect>& region_capture_rect) override {
    DCHECK_CURRENTLY_ON(BrowserThread::UI);
    if (ui_) {
      ui_->OnRegionCaptureRectChanged(region_capture_rect);
    }
  }

#if !BUILDFLAG(IS_ANDROID)
  void SetFocus(const content::DesktopMediaID& media_id,
                bool focus,
                bool is_from_microtask,
                bool is_from_timer) override {
    focus_delegate_.SetFocus(media_id, focus, is_from_microtask, is_from_timer);
  }
#endif

  base::WeakPtr<WebContentsDeviceUsage> device_usage_;
  const blink::mojom::StreamDevices devices_;
  const std::unique_ptr<::MediaStreamUI> ui_;
#if !BUILDFLAG(IS_ANDROID)
  MediaStreamFocusDelegate focus_delegate_;
#endif
  const std::u16string application_title_;
  bool started_ = false;
  const int stop_callback_id_;
};

std::unique_ptr<content::MediaStreamUI>
MediaStreamCaptureIndicator::WebContentsDeviceUsage::RegisterMediaStream(
    const blink::mojom::StreamDevices& devices,
    std::unique_ptr<MediaStreamUI> ui,
    const std::u16string application_title) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  return std::make_unique<UIDelegate>(
      web_contents(), weak_factory_.GetWeakPtr(), devices, std::move(ui),
      std::move(application_title));
}

void MediaStreamCaptureIndicator::WebContentsDeviceUsage::AddDevices(
    const blink::mojom::StreamDevices& devices,
    base::OnceClosure stop_callback,
    int stop_callback_id) {
  MediaType type = MediaType::kUnknown;

  if (devices.audio_device.has_value()) {
    AddDevice(devices.audio_device.value());
    type = MediaStreamCaptureIndicator::GetMediaType(
        devices.audio_device.value().type);
  }

  if (devices.video_device.has_value()) {
    AddDevice(devices.video_device.value());
    type = MediaStreamCaptureIndicator::GetMediaType(
        devices.video_device.value().type);
  }

  if (type == MediaType::kUserMedia) {
    user_media_stop_callbacks_[stop_callback_id] = std::move(stop_callback);
  }

  if (type == MediaType::kDisplayMedia) {
    display_media_stop_callbacks_[stop_callback_id] = std::move(stop_callback);
  }

  if (web_contents()) {
    web_contents()->NotifyNavigationStateChanged(content::INVALIDATE_TYPE_TAB);
  }

  indicator_->UpdateNotificationUserInterface();
}

void MediaStreamCaptureIndicator::WebContentsDeviceUsage::RemoveDevices(
    const blink::mojom::StreamDevices& devices,
    int stop_callback_id) {
  MediaType type = MediaType::kUnknown;

  if (devices.audio_device.has_value()) {
    RemoveDevice(devices.audio_device.value());
    type = MediaStreamCaptureIndicator::GetMediaType(
        devices.audio_device.value().type);
  }

  if (devices.video_device.has_value()) {
    RemoveDevice(devices.video_device.value());
    type = MediaStreamCaptureIndicator::GetMediaType(
        devices.video_device.value().type);
  }
  if (type == MediaType::kUserMedia) {
    user_media_stop_callbacks_.erase(stop_callback_id);
  }

  if (type == MediaType::kDisplayMedia) {
    display_media_stop_callbacks_.erase(stop_callback_id);
  }

  if (web_contents() && !web_contents()->IsBeingDestroyed()) {
    web_contents()->NotifyNavigationStateChanged(content::INVALIDATE_TYPE_TAB);
    content_settings::UpdateLocationBarUiForWebContents(web_contents());
  }

  indicator_->UpdateNotificationUserInterface();
}

void MediaStreamCaptureIndicator::WebContentsDeviceUsage::StopMediaCapturing(
    int media_type) {
  if (media_type & MediaType::kUserMedia) {
    StopCallbacks(user_media_stop_callbacks_);
  }
  if (media_type & MediaType::kDisplayMedia) {
    StopCallbacks(display_media_stop_callbacks_);
  }
}

void MediaStreamCaptureIndicator::WebContentsDeviceUsage::StopCallbacks(
    std::map<int, base::OnceClosure>& stop_callbacks) {
  // This for loop is implemented in a weird way because std::move on the value
  // will invalid the iterator; we need to record the next iter before
  // std::move.
  for (auto it = stop_callbacks.begin(), it_next = it;
       it != stop_callbacks.end(); it = it_next) {
    ++it_next;
    std::move(it->second).Run();
  }
  stop_callbacks.clear();
}

int& MediaStreamCaptureIndicator::WebContentsDeviceUsage::GetStreamCount(
    const blink::MediaStreamDevice& device) {
  switch (device.type) {
    case blink::mojom::MediaStreamType::DEVICE_AUDIO_CAPTURE:
      return audio_stream_count_;

    case blink::mojom::MediaStreamType::DEVICE_VIDEO_CAPTURE:
      return video_stream_count_;

    case blink::mojom::MediaStreamType::GUM_TAB_AUDIO_CAPTURE:
    case blink::mojom::MediaStreamType::GUM_TAB_VIDEO_CAPTURE:
      return mirroring_stream_count_;

    case blink::mojom::MediaStreamType::GUM_DESKTOP_VIDEO_CAPTURE:
    case blink::mojom::MediaStreamType::GUM_DESKTOP_AUDIO_CAPTURE:
    case blink::mojom::MediaStreamType::DISPLAY_VIDEO_CAPTURE:
    case blink::mojom::MediaStreamType::DISPLAY_AUDIO_CAPTURE:
    case blink::mojom::MediaStreamType::DISPLAY_VIDEO_CAPTURE_THIS_TAB:
    case blink::mojom::MediaStreamType::DISPLAY_VIDEO_CAPTURE_SET:
      if (IsDeviceCapturingTab(device)) {
        return tab_stream_count_;
      }
      if (IsDeviceCapturingWindow(device)) {
        return window_stream_count_;
      }
      if (IsDeviceCapturingDisplay(device)) {
        return display_stream_count_;
      }
      NOTREACHED();

    case blink::mojom::MediaStreamType::NO_SERVICE:
    case blink::mojom::MediaStreamType::NUM_MEDIA_TYPES:
      NOTREACHED();
  }
}

void MediaStreamCaptureIndicator::WebContentsDeviceUsage::AddDevice(
    const blink::MediaStreamDevice& device) {
  int& stream_count = GetStreamCount(device);
  ++stream_count;

  if (web_contents() && stream_count == 1) {
    ObserverMethod obs_func = GetObserverMethodToCall(device);
    DCHECK(obs_func);
    for (Observer& obs : indicator_->observers_)
      (obs.*obs_func)(web_contents(), true);
  }
}

void MediaStreamCaptureIndicator::WebContentsDeviceUsage::RemoveDevice(
    const blink::MediaStreamDevice& device) {
  int& stream_count = GetStreamCount(device);
  --stream_count;
  DCHECK_GE(stream_count, 0);

  if (web_contents() && stream_count == 0) {
    ObserverMethod obs_func = GetObserverMethodToCall(device);
    DCHECK(obs_func);
    for (Observer& obs : indicator_->observers_)
      (obs.*obs_func)(web_contents(), false);
  }
}

// Return whether current device os a screen sharing.
MediaStreamCaptureIndicator::MediaType
MediaStreamCaptureIndicator::GetMediaType(blink::mojom::MediaStreamType type) {
  switch (type) {
    case blink::mojom::MediaStreamType::DEVICE_AUDIO_CAPTURE:
    case blink::mojom::MediaStreamType::DEVICE_VIDEO_CAPTURE:
      return MediaStreamCaptureIndicator::MediaType::kUserMedia;

    case blink::mojom::MediaStreamType::GUM_TAB_AUDIO_CAPTURE:
    case blink::mojom::MediaStreamType::GUM_TAB_VIDEO_CAPTURE:
    case blink::mojom::MediaStreamType::GUM_DESKTOP_VIDEO_CAPTURE:
    case blink::mojom::MediaStreamType::GUM_DESKTOP_AUDIO_CAPTURE:
    case blink::mojom::MediaStreamType::DISPLAY_VIDEO_CAPTURE:
    case blink::mojom::MediaStreamType::DISPLAY_AUDIO_CAPTURE:
    case blink::mojom::MediaStreamType::DISPLAY_VIDEO_CAPTURE_THIS_TAB:
      return MediaStreamCaptureIndicator::MediaType::kDisplayMedia;

    case blink::mojom::MediaStreamType::DISPLAY_VIDEO_CAPTURE_SET:
      return MediaStreamCaptureIndicator::MediaType::kAllScreensMedia;

    default:
      return MediaStreamCaptureIndicator::MediaType::kUnknown;
  }
}

MediaStreamCaptureIndicator::Observer::~Observer() {
  DCHECK(!IsInObserverList());
}

int MediaStreamCaptureIndicator::g_stop_callback_id_ = 0;

MediaStreamCaptureIndicator::MediaStreamCaptureIndicator() = default;

MediaStreamCaptureIndicator::~MediaStreamCaptureIndicator() {
  // The user is responsible for cleaning up by reporting the closure of any
  // opened devices.  However, there exists a race condition at shutdown: The UI
  // thread may be stopped before CaptureDevicesClosed() posts the task to
  // invoke DoDevicesClosedOnUIThread().  In this case, usage_map_ won't be
  // empty like it should.
  DCHECK(usage_map_.empty() ||
         !BrowserThread::IsThreadInitialized(BrowserThread::UI));
}

std::unique_ptr<content::MediaStreamUI>
MediaStreamCaptureIndicator::RegisterMediaStream(
    content::WebContents* web_contents,
    const blink::mojom::StreamDevices& devices,
    std::unique_ptr<MediaStreamUI> ui,
    const std::u16string application_title) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  DCHECK(web_contents);

  auto& usage = usage_map_[web_contents];
  if (!usage)
    usage = std::make_unique<WebContentsDeviceUsage>(this, web_contents);

  return usage->RegisterMediaStream(devices, std::move(ui),
                                    std::move(application_title));
}

void MediaStreamCaptureIndicator::ExecuteCommand(int command_id,
                                                 int event_flags) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  const int index =
      command_id - IDC_MEDIA_CONTEXT_MEDIA_STREAM_CAPTURE_LIST_FIRST;
  DCHECK_LE(0, index);
  DCHECK_GT(static_cast<int>(command_targets_.size()), index);
  WebContents* web_contents = command_targets_[index];
  if (base::Contains(usage_map_, web_contents))
    web_contents->GetDelegate()->ActivateContents(web_contents);
}

bool MediaStreamCaptureIndicator::CheckUsage(
    content::WebContents* web_contents,
    const WebContentsDeviceUsagePredicate& pred) const {
  auto it = usage_map_.find(web_contents);
  if (it != usage_map_.end() && pred(it->second.get())) {
    return true;
  }

  for (auto* inner_contents : web_contents->GetInnerWebContents()) {
    if (CheckUsage(inner_contents, pred))
      return true;
  }

  return false;
}

bool MediaStreamCaptureIndicator::IsCapturingUserMedia(
    content::WebContents* web_contents) const {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  return CheckUsage(web_contents, [](const WebContentsDeviceUsage* usage) {
    return usage->IsCapturingAudio() || usage->IsCapturingVideo();
  });
}

bool MediaStreamCaptureIndicator::IsCapturingVideo(
    content::WebContents* web_contents) const {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  return CheckUsage(web_contents, &WebContentsDeviceUsage::IsCapturingVideo);
}

bool MediaStreamCaptureIndicator::IsCapturingAudio(
    content::WebContents* web_contents) const {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  return CheckUsage(web_contents, &WebContentsDeviceUsage::IsCapturingAudio);
}

bool MediaStreamCaptureIndicator::IsBeingMirrored(
    content::WebContents* web_contents) const {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  return CheckUsage(web_contents, &WebContentsDeviceUsage::IsMirroring);
}

bool MediaStreamCaptureIndicator::IsCapturingTab(
    content::WebContents* web_contents) const {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  return CheckUsage(web_contents, &WebContentsDeviceUsage::IsCapturingTab);
}

bool MediaStreamCaptureIndicator::IsCapturingWindow(
    content::WebContents* web_contents) const {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  return CheckUsage(web_contents, &WebContentsDeviceUsage::IsCapturingWindow);
}

bool MediaStreamCaptureIndicator::IsCapturingDisplay(
    content::WebContents* web_contents) const {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  return CheckUsage(web_contents, &WebContentsDeviceUsage::IsCapturingDisplay);
}

void MediaStreamCaptureIndicator::StopMediaCapturing(
    content::WebContents* web_contents,
    int media_type) const {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  // AllScreensMedia is an managed device feature, and should not be stopped
  // unless fully discussed.
  CHECK(!(media_type & MediaType::kAllScreensMedia))
      << "AllScreensMedia should not be stopped by MediaStreamCaptureIndicator";

  auto it = usage_map_.find(web_contents);
  if (it != usage_map_.end()) {
    it->second->StopMediaCapturing(media_type);
  }
  for (auto* inner_contents : web_contents->GetInnerWebContents())
    StopMediaCapturing(inner_contents, media_type);
}

void MediaStreamCaptureIndicator::UnregisterWebContents(
    WebContents* web_contents) {
  if (IsCapturingVideo(web_contents)) {
    for (Observer& observer : observers_)
      observer.OnIsCapturingVideoChanged(web_contents, false);
  }
  if (IsCapturingAudio(web_contents)) {
    for (Observer& observer : observers_)
      observer.OnIsCapturingAudioChanged(web_contents, false);
  }
  if (IsBeingMirrored(web_contents)) {
    for (Observer& observer : observers_)
      observer.OnIsBeingMirroredChanged(web_contents, false);
  }
  if (IsCapturingTab(web_contents)) {
    for (Observer& observer : observers_) {
      observer.OnIsCapturingTabChanged(web_contents, false);
    }
  }
  if (IsCapturingWindow(web_contents)) {
    for (Observer& observer : observers_)
      observer.OnIsCapturingWindowChanged(web_contents, false);
  }
  if (IsCapturingDisplay(web_contents)) {
    for (Observer& observer : observers_)
      observer.OnIsCapturingDisplayChanged(web_contents, false);
  }
  usage_map_.erase(web_contents);
  UpdateNotificationUserInterface();
}

void MediaStreamCaptureIndicator::MaybeCreateStatusTrayIcon(bool audio,
                                                            bool video) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  if (status_icon_)
    return;

  // If there is no browser process, we should not create the status tray.
  if (!g_browser_process)
    return;

  StatusTray* status_tray = g_browser_process->status_tray();
  if (!status_tray)
    return;

  gfx::ImageSkia image;
  std::u16string tool_tip;
  GetStatusTrayIconInfo(audio, video, &image, &tool_tip);
  DCHECK(!image.isNull());
  DCHECK(!tool_tip.empty());

  status_icon_ = status_tray->CreateStatusIcon(
      StatusTray::MEDIA_STREAM_CAPTURE_ICON, image, tool_tip);
}

void MediaStreamCaptureIndicator::MaybeDestroyStatusTrayIcon() {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  if (!status_icon_)
    return;

  // If there is no browser process, we should not do anything.
  if (!g_browser_process)
    return;

  StatusTray* status_tray = g_browser_process->status_tray();
  if (status_tray != nullptr) {
    std::unique_ptr<StatusIcon> removed_icon =
        status_tray->RemoveStatusIcon(status_icon_);
    status_icon_ = nullptr;
    removed_icon.reset();
  }
}

void MediaStreamCaptureIndicator::UpdateNotificationUserInterface() {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  std::unique_ptr<StatusIconMenuModel> menu(new StatusIconMenuModel(this));
  bool audio = false;
  bool video = false;
  int command_id = IDC_MEDIA_CONTEXT_MEDIA_STREAM_CAPTURE_LIST_FIRST;
  command_targets_.clear();

  for (const auto& it : usage_map_) {
    // Check if any audio and video devices have been used.
    const WebContentsDeviceUsage& usage = *it.second;
    if (!usage.IsCapturingAudio() && !usage.IsCapturingVideo())
      continue;

    WebContents* const web_contents = it.first;

    // The audio/video icon is shown for extensions or on Android. For
    // regular tabs on desktop, we show an indicator in the tab icon.
#if BUILDFLAG(ENABLE_EXTENSIONS)
    const extensions::Extension* extension = GetExtension(web_contents);
    if (!extension)
      continue;
#endif

    audio = audio || usage.IsCapturingAudio();
    video = video || usage.IsCapturingVideo();

    command_targets_.push_back(web_contents);
    menu->AddItem(command_id, GetTitle(web_contents));

    // If the menu item is not a label, enable it.
    menu->SetCommandIdEnabled(command_id, command_id != IDC_MinimumLabelValue);

    // If reaching the maximum number, no more item will be added to the menu.
    if (command_id == IDC_MEDIA_CONTEXT_MEDIA_STREAM_CAPTURE_LIST_LAST)
      break;
    ++command_id;
  }

  if (command_targets_.empty()) {
    MaybeDestroyStatusTrayIcon();
    return;
  }

  // The icon will take the ownership of the passed context menu.
  MaybeCreateStatusTrayIcon(audio, video);
  if (status_icon_) {
    status_icon_->SetContextMenu(std::move(menu));
  }
}

void MediaStreamCaptureIndicator::GetStatusTrayIconInfo(
    bool audio,
    bool video,
    gfx::ImageSkia* image,
    std::u16string* tool_tip) {
#if BUILDFLAG(IS_ANDROID)
  NOTREACHED();
#else   // !BUILDFLAG(IS_ANDROID)
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  DCHECK(audio || video);
  DCHECK(image);
  DCHECK(tool_tip);

  int message_id = 0;
  const gfx::VectorIcon* icon = nullptr;
  if (audio && video) {
    message_id = IDS_MEDIA_STREAM_STATUS_TRAY_TEXT_AUDIO_AND_VIDEO;
    icon = &vector_icons::kVideocamIcon;
  } else if (audio && !video) {
    message_id = IDS_MEDIA_STREAM_STATUS_TRAY_TEXT_AUDIO_ONLY;
    icon = &vector_icons::kMicIcon;
  } else if (!audio && video) {
    message_id = IDS_MEDIA_STREAM_STATUS_TRAY_TEXT_VIDEO_ONLY;
    icon = &vector_icons::kVideocamIcon;
  }

  *tool_tip = l10n_util::GetStringUTF16(message_id);
  *image = gfx::CreateVectorIcon(*icon, 16, gfx::kGoogleGrey700);
#endif  // !BUILDFLAG(IS_ANDROID)
}