Chromium Code Reviews| Index: chrome/browser/ui/tabs/tab_utils.cc |
| diff --git a/chrome/browser/ui/tabs/tab_utils.cc b/chrome/browser/ui/tabs/tab_utils.cc |
| index cfe6282b03aeb2d6ad9a80104fae4874f394a759..96c8f7533dac54ea288dd71f73416af730ff830f 100644 |
| --- a/chrome/browser/ui/tabs/tab_utils.cc |
| +++ b/chrome/browser/ui/tabs/tab_utils.cc |
| @@ -21,8 +21,15 @@ struct LastMuteMetadata |
| : public content::WebContentsUserData<LastMuteMetadata> { |
| std::string cause; // Extension ID or constant from header file |
| // or empty string |
| + base::TimeDelta token_bucket; |
| + base::TimeTicks last_attempt; |
| + |
| private: |
| - explicit LastMuteMetadata(content::WebContents* contents) {} |
| + explicit LastMuteMetadata(content::WebContents* contents) { |
| + token_bucket = |
| + base::TimeDelta::FromSeconds(chrome::kMuteTokenBucketCapacitySeconds); |
| + last_attempt = base::TimeTicks::Now(); |
| + } |
| friend class content::WebContentsUserData<LastMuteMetadata>; |
| }; |
| @@ -266,6 +273,35 @@ bool CanToggleAudioMute(content::WebContents* contents) { |
| return false; |
| } |
| +// Only run when cause should be rate limited (i.e. extensions) |
| +void updateTokenBuckets(content::WebContents* contents) { |
|
miu
2015/07/16 00:28:26
This function should be private, so please move it
|
| + DCHECK(contents); |
| + |
| + LastMuteMetadata::CreateForWebContents(contents); // Create if not exists. |
|
miu
2015/07/16 00:28:26
If this function is private, you could omit this c
|
| + |
| + base::TimeTicks now = base::TimeTicks::Now(); |
| + base::TimeDelta elapsed_since_last_attempt = |
| + now - LastMuteMetadata::FromWebContents(contents)->last_attempt; |
| + LastMuteMetadata::FromWebContents(contents)->last_attempt = now; |
| + |
| + // Add tokens to the bucket, proportional to how much time has elapsed, capped |
| + // at the maximum capacity. |
| + LastMuteMetadata::FromWebContents(contents)->token_bucket = std::min( |
| + base::TimeDelta::FromSeconds(chrome::kMuteTokenBucketCapacitySeconds), |
| + LastMuteMetadata::FromWebContents(contents)->token_bucket + |
| + elapsed_since_last_attempt); |
| +} |
| + |
| +// Only run when cause should be rate limited (i.e. extensions) |
| +bool IsTabAudioMutedRateLimited(content::WebContents* contents) { |
|
miu
2015/07/16 00:28:25
This function should probably be private as well.
|
| + DCHECK(contents); |
| + |
| + LastMuteMetadata::CreateForWebContents(contents); // Create if not exists. |
|
miu
2015/07/16 00:28:26
ditto: Also not needed if this function is private
|
| + |
| + return (LastMuteMetadata::FromWebContents(contents)->token_bucket < |
| + base::TimeDelta::FromSeconds(kMuteTokenBucketCostSeconds)); |
| +} |
| + |
| const std::string& GetTabAudioMutedCause(content::WebContents* contents) { |
| LastMuteMetadata::CreateForWebContents(contents); // Create if not exists. |
| if (GetTabMediaStateForContents(contents) == TAB_MEDIA_STATE_CAPTURING) { |
| @@ -276,16 +312,33 @@ const std::string& GetTabAudioMutedCause(content::WebContents* contents) { |
| return LastMuteMetadata::FromWebContents(contents)->cause; |
| } |
| -void SetTabAudioMuted(content::WebContents* contents, |
| - bool mute, |
| - const std::string& cause) { |
| - if (!contents || !chrome::CanToggleAudioMute(contents)) |
| - return; |
| +TabMutedResult SetTabAudioMuted(content::WebContents* contents, |
| + bool mute, |
| + const std::string& cause) { |
| + DCHECK(contents); |
| + |
| + if (!IsTabAudioMutingFeatureEnabled()) |
| + return TAB_MUTED_RESULT_FAIL_NOT_ENABLED; |
| + |
| + if (!chrome::CanToggleAudioMute(contents)) |
| + return TAB_MUTED_RESULT_FAIL_TABCAPTURE; |
| + |
| + if ((cause != kMutedToggleCauseUser) && (cause != kMutedToggleCauseCapture)) { |
| + updateTokenBuckets(contents); |
| + |
| + if (IsTabAudioMutedRateLimited(contents)) |
| + return TAB_MUTED_RESULT_FAIL_RATE_LIMITED; |
| + |
| + LastMuteMetadata::FromWebContents(contents)->token_bucket -= |
| + base::TimeDelta::FromSeconds(kMuteTokenBucketCostSeconds); |
| + } |
| LastMuteMetadata::CreateForWebContents(contents); // Create if not exists. |
| LastMuteMetadata::FromWebContents(contents)->cause = cause; |
| contents->SetAudioMuted(mute); |
| + |
| + return TAB_MUTED_RESULT_SUCCESS; |
| } |
| bool IsTabAudioMuted(content::WebContents* contents) { |