| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/tabs/tab_utils.h" | 5 #include "chrome/browser/ui/tabs/tab_utils.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/strings/string16.h" | 8 #include "base/strings/string16.h" |
| 9 #include "chrome/browser/media/media_capture_devices_dispatcher.h" | 9 #include "chrome/browser/media/media_capture_devices_dispatcher.h" |
| 10 #include "chrome/browser/media/media_stream_capture_indicator.h" | 10 #include "chrome/browser/media/media_stream_capture_indicator.h" |
| 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 12 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
| 13 #include "chrome/grit/generated_resources.h" | 13 #include "chrome/grit/generated_resources.h" |
| 14 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
| 15 #include "grit/theme_resources.h" | 15 #include "grit/theme_resources.h" |
| 16 #include "ui/base/l10n/l10n_util.h" | 16 #include "ui/base/l10n/l10n_util.h" |
| 17 #include "ui/base/resource/resource_bundle.h" | 17 #include "ui/base/resource/resource_bundle.h" |
| 18 #include "ui/gfx/animation/multi_animation.h" | 18 #include "ui/gfx/animation/multi_animation.h" |
| 19 | 19 |
| 20 const int kMuteTokenBucketCostSeconds = 15; |
| 21 const int kMuteTokenBucketCapacitySeconds = 3 * kMuteTokenBucketCostSeconds; |
| 22 |
| 20 struct LastMuteMetadata | 23 struct LastMuteMetadata |
| 21 : public content::WebContentsUserData<LastMuteMetadata> { | 24 : public content::WebContentsUserData<LastMuteMetadata> { |
| 22 std::string cause; // Extension ID or constant from header file | 25 std::string cause; // Extension ID or constant from header file |
| 23 // or empty string | 26 // or empty string |
| 27 base::TimeDelta token_bucket; |
| 28 base::TimeTicks last_attempt; |
| 29 |
| 24 private: | 30 private: |
| 25 explicit LastMuteMetadata(content::WebContents* contents) {} | 31 explicit LastMuteMetadata(content::WebContents* contents) { |
| 32 token_bucket = |
| 33 base::TimeDelta::FromSeconds(kMuteTokenBucketCapacitySeconds); |
| 34 last_attempt = base::TimeTicks::Now(); |
| 35 } |
| 26 friend class content::WebContentsUserData<LastMuteMetadata>; | 36 friend class content::WebContentsUserData<LastMuteMetadata>; |
| 27 }; | 37 }; |
| 28 | 38 |
| 29 DEFINE_WEB_CONTENTS_USER_DATA_KEY(LastMuteMetadata); | 39 DEFINE_WEB_CONTENTS_USER_DATA_KEY(LastMuteMetadata); |
| 30 | 40 |
| 31 namespace chrome { | 41 namespace chrome { |
| 32 | 42 |
| 33 const char kMutedToggleCauseUser[] = "user"; | 43 const char kMutedToggleCauseUser[] = "user"; |
| 34 const char kMutedToggleCauseCapture[] = "capture"; | 44 const char kMutedToggleCauseCapture[] = "capture"; |
| 35 | 45 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 gfx::Tween::EASE_IN)); | 94 gfx::Tween::EASE_IN)); |
| 85 } | 95 } |
| 86 const base::TimeDelta interval = | 96 const base::TimeDelta interval = |
| 87 base::TimeDelta::FromMilliseconds(kIndicatorFrameIntervalMs); | 97 base::TimeDelta::FromMilliseconds(kIndicatorFrameIntervalMs); |
| 88 scoped_ptr<TabRecordingIndicatorAnimation> animation( | 98 scoped_ptr<TabRecordingIndicatorAnimation> animation( |
| 89 new TabRecordingIndicatorAnimation(parts, interval)); | 99 new TabRecordingIndicatorAnimation(parts, interval)); |
| 90 animation->set_continuous(false); | 100 animation->set_continuous(false); |
| 91 return animation.Pass(); | 101 return animation.Pass(); |
| 92 } | 102 } |
| 93 | 103 |
| 104 // Only run when cause should be rate limited (i.e. extensions) |
| 105 // |contents| should exist and have LastMuteMetadata initialized. |
| 106 void UpdateTokenBuckets(content::WebContents* contents) { |
| 107 base::TimeTicks now = base::TimeTicks::Now(); |
| 108 base::TimeDelta elapsed_since_last_attempt = |
| 109 now - LastMuteMetadata::FromWebContents(contents)->last_attempt; |
| 110 LastMuteMetadata::FromWebContents(contents)->last_attempt = now; |
| 111 |
| 112 // Add tokens to the bucket, proportional to how much time has elapsed, capped |
| 113 // at the maximum capacity. |
| 114 LastMuteMetadata::FromWebContents(contents)->token_bucket = |
| 115 std::min(base::TimeDelta::FromSeconds(kMuteTokenBucketCapacitySeconds), |
| 116 LastMuteMetadata::FromWebContents(contents)->token_bucket + |
| 117 elapsed_since_last_attempt); |
| 118 } |
| 119 |
| 120 // Only run when cause should be rate limited (i.e. extensions) |
| 121 // |contents| should exist and have LastMuteMetadata initialized. |
| 122 bool IsTabAudioMutedRateLimited(content::WebContents* contents) { |
| 123 return (LastMuteMetadata::FromWebContents(contents)->token_bucket < |
| 124 base::TimeDelta::FromSeconds(kMuteTokenBucketCostSeconds)); |
| 125 } |
| 126 |
| 94 } // namespace | 127 } // namespace |
| 95 | 128 |
| 96 bool ShouldTabShowFavicon(int capacity, | 129 bool ShouldTabShowFavicon(int capacity, |
| 97 bool is_pinned_tab, | 130 bool is_pinned_tab, |
| 98 bool is_active_tab, | 131 bool is_active_tab, |
| 99 bool has_favicon, | 132 bool has_favicon, |
| 100 TabMediaState media_state) { | 133 TabMediaState media_state) { |
| 101 if (!has_favicon) | 134 if (!has_favicon) |
| 102 return false; | 135 return false; |
| 103 int required_capacity = 1; | 136 int required_capacity = 1; |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 const std::string& GetTabAudioMutedCause(content::WebContents* contents) { | 302 const std::string& GetTabAudioMutedCause(content::WebContents* contents) { |
| 270 LastMuteMetadata::CreateForWebContents(contents); // Create if not exists. | 303 LastMuteMetadata::CreateForWebContents(contents); // Create if not exists. |
| 271 if (GetTabMediaStateForContents(contents) == TAB_MEDIA_STATE_CAPTURING) { | 304 if (GetTabMediaStateForContents(contents) == TAB_MEDIA_STATE_CAPTURING) { |
| 272 // For tab capture, libcontent forces muting off. | 305 // For tab capture, libcontent forces muting off. |
| 273 LastMuteMetadata::FromWebContents(contents)->cause = | 306 LastMuteMetadata::FromWebContents(contents)->cause = |
| 274 kMutedToggleCauseCapture; | 307 kMutedToggleCauseCapture; |
| 275 } | 308 } |
| 276 return LastMuteMetadata::FromWebContents(contents)->cause; | 309 return LastMuteMetadata::FromWebContents(contents)->cause; |
| 277 } | 310 } |
| 278 | 311 |
| 279 void SetTabAudioMuted(content::WebContents* contents, | 312 TabMutedResult SetTabAudioMuted(content::WebContents* contents, |
| 280 bool mute, | 313 bool mute, |
| 281 const std::string& cause) { | 314 const std::string& cause) { |
| 282 if (!contents || !chrome::CanToggleAudioMute(contents)) | 315 DCHECK(contents); |
| 283 return; | 316 |
| 317 if (!IsTabAudioMutingFeatureEnabled()) |
| 318 return TAB_MUTED_RESULT_FAIL_NOT_ENABLED; |
| 319 |
| 320 if (!chrome::CanToggleAudioMute(contents)) |
| 321 return TAB_MUTED_RESULT_FAIL_TABCAPTURE; |
| 284 | 322 |
| 285 LastMuteMetadata::CreateForWebContents(contents); // Create if not exists. | 323 LastMuteMetadata::CreateForWebContents(contents); // Create if not exists. |
| 324 |
| 325 if ((IsTabAudioMuted(contents) == mute) && |
| 326 (LastMuteMetadata::FromWebContents(contents)->cause == cause)) { |
| 327 return TAB_MUTED_RESULT_SUCCESS; |
| 328 } |
| 329 |
| 330 if ((cause != kMutedToggleCauseUser) && (cause != kMutedToggleCauseCapture)) { |
| 331 UpdateTokenBuckets(contents); |
| 332 |
| 333 if (IsTabAudioMutedRateLimited(contents)) |
| 334 return TAB_MUTED_RESULT_FAIL_RATE_LIMITED; |
| 335 |
| 336 LastMuteMetadata::FromWebContents(contents)->token_bucket -= |
| 337 base::TimeDelta::FromSeconds(kMuteTokenBucketCostSeconds); |
| 338 } |
| 339 |
| 340 contents->SetAudioMuted(mute); |
| 286 LastMuteMetadata::FromWebContents(contents)->cause = cause; | 341 LastMuteMetadata::FromWebContents(contents)->cause = cause; |
| 287 | 342 |
| 288 contents->SetAudioMuted(mute); | 343 return TAB_MUTED_RESULT_SUCCESS; |
| 289 } | 344 } |
| 290 | 345 |
| 291 bool IsTabAudioMuted(content::WebContents* contents) { | 346 bool IsTabAudioMuted(content::WebContents* contents) { |
| 292 return contents && contents->IsAudioMuted(); | 347 return contents && contents->IsAudioMuted(); |
| 293 } | 348 } |
| 294 | 349 |
| 295 bool AreAllTabsMuted(const TabStripModel& tab_strip, | 350 bool AreAllTabsMuted(const TabStripModel& tab_strip, |
| 296 const std::vector<int>& indices) { | 351 const std::vector<int>& indices) { |
| 297 for (std::vector<int>::const_iterator i = indices.begin(); i != indices.end(); | 352 for (std::vector<int>::const_iterator i = indices.begin(); i != indices.end(); |
| 298 ++i) { | 353 ++i) { |
| 299 if (!IsTabAudioMuted(tab_strip.GetWebContentsAt(*i))) | 354 if (!IsTabAudioMuted(tab_strip.GetWebContentsAt(*i))) |
| 300 return false; | 355 return false; |
| 301 } | 356 } |
| 302 return true; | 357 return true; |
| 303 } | 358 } |
| 304 | 359 |
| 305 } // namespace chrome | 360 } // namespace chrome |
| OLD | NEW |