summaryrefslogtreecommitdiffstats
path: root/chrome/browser/media/media_stream_capture_indicator.h
blob: d904d6f3fab829ccdf668ea1931af35efff2651a (plain)
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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_MEDIA_MEDIA_STREAM_CAPTURE_INDICATOR_H_
#define CHROME_BROWSER_MEDIA_MEDIA_STREAM_CAPTURE_INDICATOR_H_
#pragma once

#include <list>
#include <string>

#include "base/memory/ref_counted.h"
#include "content/public/common/media_stream_request.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/models/simple_menu_model.h"

class StatusIcon;
class StatusTray;

// This indicator is owned by MediaInternals and deleted when MediaInternals
// is deleted.
class MediaStreamCaptureIndicator
    : public base::RefCountedThreadSafe<MediaStreamCaptureIndicator>,
      public ui::SimpleMenuModel::Delegate {
 public:
  MediaStreamCaptureIndicator();

  // Overrides from SimpleMenuModel::Delegate implementation.
  virtual bool IsCommandIdChecked(int command_id) const OVERRIDE;
  virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE;
  virtual bool GetAcceleratorForCommandId(
      int command_id,
      ui::Accelerator* accelerator) OVERRIDE;
  virtual void ExecuteCommand(int command_id) OVERRIDE;

  // Called on IO thread when MediaStream opens new capture devices.
  void CaptureDevicesOpened(int render_process_id,
                            int render_view_id,
                            const content::MediaStreamDevices& devices);

  // Called on IO thread when MediaStream closes the opened devices.
  void CaptureDevicesClosed(int render_process_id,
                            int render_view_id,
                            const content::MediaStreamDevices& devices);

 private:
  // Struct to store the usage information of the capture devices for each tab.
  struct CaptureDeviceTab {
    CaptureDeviceTab(int render_process_id,
                     int render_view_id,
                     content::MediaStreamDeviceType type)
        : render_process_id(render_process_id),
          render_view_id(render_view_id),
          type(type) {}

    int render_process_id;
    int render_view_id;
    content::MediaStreamDeviceType type;
  };

  // A private predicate used in std::find_if to find a |CaptureDeviceTab|
  // which matches the information specified at construction.
  class TabEquals {
   public:
    TabEquals(int render_process_id, int render_view_id,
              content::MediaStreamDeviceType type);
    TabEquals(int render_process_id, int render_view_id);

    bool operator() (
        const MediaStreamCaptureIndicator::CaptureDeviceTab& tab);

   private:
     int render_process_id_;
     int render_view_id_;
     content::MediaStreamDeviceType type_;
  };

  friend class base::RefCountedThreadSafe<MediaStreamCaptureIndicator>;
  virtual ~MediaStreamCaptureIndicator();

  // Called by the public functions, executed on UI thread.
  void DoDevicesOpenedOnUIThread(int render_process_id,
                                 int render_view_id,
                                 const content::MediaStreamDevices& devices);
  void DoDevicesClosedOnUIThread(int render_process_id,
                                 int render_view_id,
                                 const content::MediaStreamDevices& devices);

  // Following functions/variables are executed/accessed only on UI thread.
  // Creates the status tray if it has not been created.
  void CreateStatusTray();

  // Makes sure we have done one-time initialization of the |icon_image_|.
  void EnsureStatusTrayIcon();

  // Triggers a balloon in the corner telling capture devices are being used.
  void ShowBalloon(int render_process_id, int render_view_id,
                   const content::MediaStreamDevices& devices) const;

  // Hides the status tray from the desktop.
  void Hide();

  // Adds the new tab to the device usage list.
  void AddCaptureDeviceTab(int render_process_id,
                           int render_view_id,
                           const content::MediaStreamDevices& devices);

  // Removes the tab from the device usage list.
  void RemoveCaptureDeviceTab(int render_process_id,
                              int render_view_id,
                              const content::MediaStreamDevices& devices);

  // Gets the title of the tab.
  string16 GetTitle(int render_process_id, int render_view_id) const;

  // Updates the status tray menu with the new device list. This call will be
  // triggered by both AddCaptureDeviceTab() and RemoveCaptureDeviceTab().
  void UpdateStatusTrayIconContextMenu();

  // Reference to our status icon - owned by the StatusTray. If null,
  // the platform doesn't support status icons.
  StatusIcon* status_icon_;

  // Icon to be displayed on the status tray.
  SkBitmap icon_image_;

  // A list that contains the usage information of the opened capture devices.
  typedef std::list<CaptureDeviceTab> CaptureDeviceTabList;
  CaptureDeviceTabList tabs_;
};

#endif  // CHROME_BROWSER_MEDIA_MEDIA_STREAM_CAPTURE_INDICATOR_H_