Chromium Code Reviews| 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 struct LastMuteMetadata | 20 struct LastMuteMetadata |
| 21 : public content::WebContentsUserData<LastMuteMetadata> { | 21 : public content::WebContentsUserData<LastMuteMetadata> { |
| 22 std::string cause; // Extension ID or constant from header file | 22 std::string cause; // Extension ID or constant from header file |
| 23 // or empty string | 23 // or empty string |
| 24 base::TimeDelta token_bucket; | |
| 25 base::TimeTicks last_attempt; | |
| 26 | |
| 24 private: | 27 private: |
| 25 explicit LastMuteMetadata(content::WebContents* contents) {} | 28 explicit LastMuteMetadata(content::WebContents* contents) { |
| 29 token_bucket = | |
| 30 base::TimeDelta::FromSeconds(chrome::kMuteTokenBucketCapacitySeconds); | |
| 31 last_attempt = base::TimeTicks::Now(); | |
| 32 } | |
| 26 friend class content::WebContentsUserData<LastMuteMetadata>; | 33 friend class content::WebContentsUserData<LastMuteMetadata>; |
| 27 }; | 34 }; |
| 28 | 35 |
| 29 DEFINE_WEB_CONTENTS_USER_DATA_KEY(LastMuteMetadata); | 36 DEFINE_WEB_CONTENTS_USER_DATA_KEY(LastMuteMetadata); |
| 30 | 37 |
| 31 namespace chrome { | 38 namespace chrome { |
| 32 | 39 |
| 33 const char kMutedToggleCauseUser[] = "user"; | 40 const char kMutedToggleCauseUser[] = "user"; |
| 34 const char kMutedToggleCauseCapture[] = "capture"; | 41 const char kMutedToggleCauseCapture[] = "capture"; |
| 42 const int kMuteTokenBucketCostSeconds = 15; | |
| 43 const int kMuteTokenBucketCapacitySeconds = 3 * kMuteTokenBucketCostSeconds; | |
| 35 | 44 |
| 36 namespace { | 45 namespace { |
| 37 | 46 |
| 38 // Interval between frame updates of the tab indicator animations. This is not | 47 // Interval between frame updates of the tab indicator animations. This is not |
| 39 // the usual 60 FPS because a trade-off must be made between tab UI animation | 48 // the usual 60 FPS because a trade-off must be made between tab UI animation |
| 40 // smoothness and media recording/playback performance on low-end hardware. | 49 // smoothness and media recording/playback performance on low-end hardware. |
| 41 const int kIndicatorFrameIntervalMs = 50; // 20 FPS | 50 const int kIndicatorFrameIntervalMs = 50; // 20 FPS |
| 42 | 51 |
| 43 // Fade-in/out duration for the tab indicator animations. Fade-in is quick to | 52 // Fade-in/out duration for the tab indicator animations. Fade-in is quick to |
| 44 // immediately notify the user. Fade-out is more gradual, so that the user has | 53 // immediately notify the user. Fade-out is more gradual, so that the user has |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 84 gfx::Tween::EASE_IN)); | 93 gfx::Tween::EASE_IN)); |
| 85 } | 94 } |
| 86 const base::TimeDelta interval = | 95 const base::TimeDelta interval = |
| 87 base::TimeDelta::FromMilliseconds(kIndicatorFrameIntervalMs); | 96 base::TimeDelta::FromMilliseconds(kIndicatorFrameIntervalMs); |
| 88 scoped_ptr<TabRecordingIndicatorAnimation> animation( | 97 scoped_ptr<TabRecordingIndicatorAnimation> animation( |
| 89 new TabRecordingIndicatorAnimation(parts, interval)); | 98 new TabRecordingIndicatorAnimation(parts, interval)); |
| 90 animation->set_continuous(false); | 99 animation->set_continuous(false); |
| 91 return animation.Pass(); | 100 return animation.Pass(); |
| 92 } | 101 } |
| 93 | 102 |
| 103 // Only run when cause should be rate limited (i.e. extensions) | |
| 104 void UpdateTokenBuckets(content::WebContents* contents) { | |
| 105 DCHECK(contents); | |
|
miu
2015/07/18 00:42:42
To answer your question: The DCHECK here and at th
Jared Sohn
2015/07/18 23:57:24
I am removing them and replacing with a general co
| |
| 106 | |
| 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 = std::min( | |
| 115 base::TimeDelta::FromSeconds(chrome::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 bool IsTabAudioMutedRateLimited(content::WebContents* contents) { | |
| 122 DCHECK(contents); | |
| 123 | |
| 124 return (LastMuteMetadata::FromWebContents(contents)->token_bucket < | |
| 125 base::TimeDelta::FromSeconds(kMuteTokenBucketCostSeconds)); | |
| 126 } | |
| 127 | |
| 94 } // namespace | 128 } // namespace |
| 95 | 129 |
| 96 bool ShouldTabShowFavicon(int capacity, | 130 bool ShouldTabShowFavicon(int capacity, |
| 97 bool is_pinned_tab, | 131 bool is_pinned_tab, |
| 98 bool is_active_tab, | 132 bool is_active_tab, |
| 99 bool has_favicon, | 133 bool has_favicon, |
| 100 TabMediaState media_state) { | 134 TabMediaState media_state) { |
| 101 if (!has_favicon) | 135 if (!has_favicon) |
| 102 return false; | 136 return false; |
| 103 int required_capacity = 1; | 137 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) { | 303 const std::string& GetTabAudioMutedCause(content::WebContents* contents) { |
| 270 LastMuteMetadata::CreateForWebContents(contents); // Create if not exists. | 304 LastMuteMetadata::CreateForWebContents(contents); // Create if not exists. |
| 271 if (GetTabMediaStateForContents(contents) == TAB_MEDIA_STATE_CAPTURING) { | 305 if (GetTabMediaStateForContents(contents) == TAB_MEDIA_STATE_CAPTURING) { |
| 272 // For tab capture, libcontent forces muting off. | 306 // For tab capture, libcontent forces muting off. |
| 273 LastMuteMetadata::FromWebContents(contents)->cause = | 307 LastMuteMetadata::FromWebContents(contents)->cause = |
| 274 kMutedToggleCauseCapture; | 308 kMutedToggleCauseCapture; |
| 275 } | 309 } |
| 276 return LastMuteMetadata::FromWebContents(contents)->cause; | 310 return LastMuteMetadata::FromWebContents(contents)->cause; |
| 277 } | 311 } |
| 278 | 312 |
| 279 void SetTabAudioMuted(content::WebContents* contents, | 313 TabMutedResult SetTabAudioMuted(content::WebContents* contents, |
| 280 bool mute, | 314 bool mute, |
| 281 const std::string& cause) { | 315 const std::string& cause) { |
| 282 if (!contents || !chrome::CanToggleAudioMute(contents)) | 316 DCHECK(contents); |
| 283 return; | 317 |
| 318 if (!IsTabAudioMutingFeatureEnabled()) | |
| 319 return TAB_MUTED_RESULT_FAIL_NOT_ENABLED; | |
| 320 | |
| 321 if (!chrome::CanToggleAudioMute(contents)) | |
| 322 return TAB_MUTED_RESULT_FAIL_TABCAPTURE; | |
| 284 | 323 |
| 285 LastMuteMetadata::CreateForWebContents(contents); // Create if not exists. | 324 LastMuteMetadata::CreateForWebContents(contents); // Create if not exists. |
| 325 | |
| 326 if ((cause != kMutedToggleCauseUser) && (cause != kMutedToggleCauseCapture)) { | |
| 327 UpdateTokenBuckets(contents); | |
| 328 | |
| 329 if (IsTabAudioMutedRateLimited(contents)) | |
| 330 return TAB_MUTED_RESULT_FAIL_RATE_LIMITED; | |
| 331 | |
| 332 LastMuteMetadata::FromWebContents(contents)->token_bucket -= | |
| 333 base::TimeDelta::FromSeconds(kMuteTokenBucketCostSeconds); | |
| 334 } | |
| 335 | |
| 286 LastMuteMetadata::FromWebContents(contents)->cause = cause; | 336 LastMuteMetadata::FromWebContents(contents)->cause = cause; |
| 287 | 337 |
| 288 contents->SetAudioMuted(mute); | 338 contents->SetAudioMuted(mute); |
| 339 | |
| 340 return TAB_MUTED_RESULT_SUCCESS; | |
| 289 } | 341 } |
| 290 | 342 |
| 291 bool IsTabAudioMuted(content::WebContents* contents) { | 343 bool IsTabAudioMuted(content::WebContents* contents) { |
| 292 return contents && contents->IsAudioMuted(); | 344 return contents && contents->IsAudioMuted(); |
| 293 } | 345 } |
| 294 | 346 |
| 295 bool AreAllTabsMuted(const TabStripModel& tab_strip, | 347 bool AreAllTabsMuted(const TabStripModel& tab_strip, |
| 296 const std::vector<int>& indices) { | 348 const std::vector<int>& indices) { |
| 297 for (std::vector<int>::const_iterator i = indices.begin(); i != indices.end(); | 349 for (std::vector<int>::const_iterator i = indices.begin(); i != indices.end(); |
| 298 ++i) { | 350 ++i) { |
| 299 if (!IsTabAudioMuted(tab_strip.GetWebContentsAt(*i))) | 351 if (!IsTabAudioMuted(tab_strip.GetWebContentsAt(*i))) |
| 300 return false; | 352 return false; |
| 301 } | 353 } |
| 302 return true; | 354 return true; |
| 303 } | 355 } |
| 304 | 356 |
| 305 } // namespace chrome | 357 } // namespace chrome |
| OLD | NEW |