blob: 4232ddcee8d2e7689773dcf84b4dc0794c235c9f [file] [log] [blame]
// 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_
#include <string>
#include <vector>
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "chrome/browser/extensions/image_loading_tracker.h"
#include "content/public/common/media_stream_request.h"
#include "ui/base/models/simple_menu_model.h"
#include "ui/gfx/image/image_skia.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 ImageLoadingTracker::Observer {
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);
// ImageLoadingTracker::Observer implementation.
virtual void OnImageLoaded(const gfx::Image& image,
const std::string& extension_id,
int index) OVERRIDE;
private:
// Struct to store the usage information of the capture devices for each tab.
// TODO(estade): this should be called CaptureDeviceContents; not all the
// render views it represents are tabs.
struct CaptureDeviceTab {
CaptureDeviceTab(int render_process_id,
int render_view_id)
: render_process_id(render_process_id),
render_view_id(render_view_id),
audio_ref_count(0),
video_ref_count(0) {}
int render_process_id;
int render_view_id;
int audio_ref_count;
int video_ref_count;
};
// 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);
bool operator() (
const MediaStreamCaptureIndicator::CaptureDeviceTab& tab);
private:
int render_process_id_;
int render_view_id_;
};
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 images.
void EnsureStatusTrayIconResources();
// 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);
// Triggers a balloon in the corner telling capture devices are being used.
// This function is called by AddCaptureDeviceTab().
void ShowBalloon(int render_process_id, int render_view_id,
bool audio, bool video);
// Hides the status tray from the desktop. This function is called by
// RemoveCaptureDeviceTab() when the device usage list becomes empty.
void Hide();
// Updates the status tray menu with the new device list. This call will be
// triggered by both AddCaptureDeviceTab() and RemoveCaptureDeviceTab().
void UpdateStatusTrayIconContextMenu();
// Updates the status tray tooltip and image according to which kind of
// devices are being used. This function is called by
// UpdateStatusTrayIconContextMenu().
void UpdateStatusTrayIconDisplay(bool audio, bool video);
// Initializes image loading state.
void EnsureImageLoadingTracker();
// Reference to our status icon - owned by the StatusTray. If null,
// the platform doesn't support status icons.
StatusIcon* status_icon_;
// These images are owned by ResourceBundle and need not be destroyed.
gfx::ImageSkia* mic_image_;
gfx::ImageSkia* camera_image_;
gfx::ImageSkia* balloon_image_;
// A list that contains the usage information of the opened capture devices.
typedef std::vector<CaptureDeviceTab> CaptureDeviceTabs;
CaptureDeviceTabs tabs_;
// Tracks the load of extension icons.
scoped_ptr<ImageLoadingTracker> tracker_;
// The messages to display when extension images are loaded. The index
// corresponds to the index of the associated LoadImage request.
std::map<int, string16> pending_messages_;
// Tracks the number of requests to |tracker_|.
int request_index_;
};
#endif // CHROME_BROWSER_MEDIA_MEDIA_STREAM_CAPTURE_INDICATOR_H_