Commit dccb8dd3 authored by jared.sohn's avatar jared.sohn Committed by Commit bot

Make tab audible and muted states available for an extension API

* Added 'cause' parameter to mute method to indicate if muted state was changed by a user or by an extension.
* Added support for audible and muted event listeners.

A future patch that relies on these changes will update an API to make the audible and muted states available to extensions.

BUG=438903

Review URL: https://codereview.chromium.org/757033005

Cr-Commit-Position: refs/heads/master@{#319574}
parent 6aed9aec
......@@ -211,6 +211,7 @@ James Wei <james.wei@intel.com>
James Willcox <jwillcox@litl.com>
Janwar Dinata <j.dinata@gmail.com>
Jared Shumway <jaredshumway94@gmail.com>
Jared Sohn <jared.sohn@gmail.com>
Jared Wein <weinjared@gmail.com>
Jay Soffian <jaysoffian@gmail.com>
Jeado Ko <haibane84@gmail.com>
......
......@@ -827,7 +827,8 @@ NSImage* Overlay(NSImage* ground, NSImage* overlay, CGFloat alpha) {
if (!tabStripModel_->ContainsIndex(index))
return;
WebContents* contents = tabStripModel_->GetWebContentsAt(index);
chrome::SetTabAudioMuted(contents, !chrome::IsTabAudioMuted(contents));
chrome::SetTabAudioMuted(contents, !chrome::IsTabAudioMuted(contents),
chrome::kMutedToggleCauseUser);
}
// Called when the user closes a tab. Asks the model to close the tab. |sender|
......
......@@ -1054,7 +1054,8 @@ void TabStripModel::ExecuteContextMenuCommand(
content::RecordAction(UserMetricsAction("TabContextMenu_UnmuteTabs"));
for (std::vector<int>::const_iterator i = indices.begin();
i != indices.end(); ++i) {
chrome::SetTabAudioMuted(GetWebContentsAt(*i), mute);
chrome::SetTabAudioMuted(GetWebContentsAt(*i), mute,
chrome::kMutedToggleCauseUser);
}
break;
}
......
......@@ -17,8 +17,22 @@
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/animation/multi_animation.h"
struct LastMuteMetadata
: public content::WebContentsUserData<LastMuteMetadata> {
std::string cause; // Extension ID or constant from header file
// or empty string
private:
explicit LastMuteMetadata(content::WebContents* contents) {}
friend class content::WebContentsUserData<LastMuteMetadata>;
};
DEFINE_WEB_CONTENTS_USER_DATA_KEY(LastMuteMetadata);
namespace chrome {
const char kMutedToggleCauseUser[] = "user";
const char kMutedToggleCauseCapture[] = "auto-forced for capture";
namespace {
// Interval between frame updates of the tab indicator animations. This is not
......@@ -252,9 +266,25 @@ bool CanToggleAudioMute(content::WebContents* contents) {
return false;
}
void SetTabAudioMuted(content::WebContents* contents, bool mute) {
const std::string& GetTabAudioMutedCause(content::WebContents* contents) {
LastMuteMetadata::CreateForWebContents(contents); // Create if not exists.
if (GetTabMediaStateForContents(contents) == TAB_MEDIA_STATE_CAPTURING) {
// For tab capture, libcontent forces muting off.
LastMuteMetadata::FromWebContents(contents)->cause =
kMutedToggleCauseCapture;
}
return LastMuteMetadata::FromWebContents(contents)->cause;
}
void SetTabAudioMuted(content::WebContents* contents,
bool mute,
const std::string& cause) {
if (!contents || !chrome::CanToggleAudioMute(contents))
return;
LastMuteMetadata::CreateForWebContents(contents); // Create if not exists.
LastMuteMetadata::FromWebContents(contents)->cause = cause;
contents->SetAudioMuted(mute);
}
......
......@@ -5,10 +5,12 @@
#ifndef CHROME_BROWSER_UI_TABS_TAB_UTILS_H_
#define CHROME_BROWSER_UI_TABS_TAB_UTILS_H_
#include <string>
#include <vector>
#include "base/memory/scoped_ptr.h"
#include "base/strings/string16.h"
#include "content/public/browser/web_contents_user_data.h"
class TabStripModel;
......@@ -33,6 +35,11 @@ enum TabMediaState {
namespace chrome {
// String to indicate reason for muted state change (user, capture, extension
// id, or empty string)
extern const char kMutedToggleCauseUser[];
extern const char kMutedToggleCauseCapture[];
// Logic to determine which components (i.e., close button, favicon, and media
// indicator) of a tab should be shown, given current state. |capacity|
// specifies how many components can be shown, given available tab width.
......@@ -97,9 +104,17 @@ bool IsTabAudioMutingFeatureEnabled();
// |contents|.
bool CanToggleAudioMute(content::WebContents* contents);
// Indicates/Sets whether all audio output from |contents| is muted.
// Indicates whether all audio output from |contents| is muted.
bool IsTabAudioMuted(content::WebContents* contents);
void SetTabAudioMuted(content::WebContents* contents, bool mute);
// Sets whether all audio output from |contents| is muted.
// Cause is extensionid, kMutedToggleCause constant, or empty string
void SetTabAudioMuted(content::WebContents* contents,
bool mute,
const std::string& cause);
// Get cause of mute (extensionid, kMutedToggleCause constant, or empty string)
const std::string& GetTabAudioMutedCause(content::WebContents* contents);
// Returns true if the tabs at the |indices| in |tab_strip| are all muted.
bool AreAllTabsMuted(const TabStripModel& tab_strip,
......
......@@ -299,7 +299,8 @@ void BrowserTabStripController::CloseTab(int model_index,
void BrowserTabStripController::ToggleTabAudioMute(int model_index) {
content::WebContents* const contents = model_->GetWebContentsAt(model_index);
chrome::SetTabAudioMuted(contents, !chrome::IsTabAudioMuted(contents));
chrome::SetTabAudioMuted(contents, !chrome::IsTabAudioMuted(contents),
chrome::kMutedToggleCauseUser);
}
void BrowserTabStripController::ShowContextMenuForTab(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment