Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(393)

Side by Side Diff: chrome/browser/ui/tabs/tab_utils.cc

Issue 1233263002: Clean up error handling logic for extension tab muting (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Restore capitalization of LastMuteMetadata Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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";
35 42
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 case TAB_MEDIA_STATE_AUDIO_MUTING: 266 case TAB_MEDIA_STATE_AUDIO_MUTING:
260 return IsTabAudioMutingFeatureEnabled(); 267 return IsTabAudioMutingFeatureEnabled();
261 case TAB_MEDIA_STATE_RECORDING: 268 case TAB_MEDIA_STATE_RECORDING:
262 case TAB_MEDIA_STATE_CAPTURING: 269 case TAB_MEDIA_STATE_CAPTURING:
263 return false; 270 return false;
264 } 271 }
265 NOTREACHED(); 272 NOTREACHED();
266 return false; 273 return false;
267 } 274 }
268 275
276 // Only run when cause should be rate limited (i.e. extensions)
277 void updateTokenBuckets(content::WebContents* contents) {
miu 2015/07/16 00:28:26 This function should be private, so please move it
278 DCHECK(contents);
279
280 LastMuteMetadata::CreateForWebContents(contents); // Create if not exists.
miu 2015/07/16 00:28:26 If this function is private, you could omit this c
281
282 base::TimeTicks now = base::TimeTicks::Now();
283 base::TimeDelta elapsed_since_last_attempt =
284 now - LastMuteMetadata::FromWebContents(contents)->last_attempt;
285 LastMuteMetadata::FromWebContents(contents)->last_attempt = now;
286
287 // Add tokens to the bucket, proportional to how much time has elapsed, capped
288 // at the maximum capacity.
289 LastMuteMetadata::FromWebContents(contents)->token_bucket = std::min(
290 base::TimeDelta::FromSeconds(chrome::kMuteTokenBucketCapacitySeconds),
291 LastMuteMetadata::FromWebContents(contents)->token_bucket +
292 elapsed_since_last_attempt);
293 }
294
295 // Only run when cause should be rate limited (i.e. extensions)
296 bool IsTabAudioMutedRateLimited(content::WebContents* contents) {
miu 2015/07/16 00:28:25 This function should probably be private as well.
297 DCHECK(contents);
298
299 LastMuteMetadata::CreateForWebContents(contents); // Create if not exists.
miu 2015/07/16 00:28:26 ditto: Also not needed if this function is private
300
301 return (LastMuteMetadata::FromWebContents(contents)->token_bucket <
302 base::TimeDelta::FromSeconds(kMuteTokenBucketCostSeconds));
303 }
304
269 const std::string& GetTabAudioMutedCause(content::WebContents* contents) { 305 const std::string& GetTabAudioMutedCause(content::WebContents* contents) {
270 LastMuteMetadata::CreateForWebContents(contents); // Create if not exists. 306 LastMuteMetadata::CreateForWebContents(contents); // Create if not exists.
271 if (GetTabMediaStateForContents(contents) == TAB_MEDIA_STATE_CAPTURING) { 307 if (GetTabMediaStateForContents(contents) == TAB_MEDIA_STATE_CAPTURING) {
272 // For tab capture, libcontent forces muting off. 308 // For tab capture, libcontent forces muting off.
273 LastMuteMetadata::FromWebContents(contents)->cause = 309 LastMuteMetadata::FromWebContents(contents)->cause =
274 kMutedToggleCauseCapture; 310 kMutedToggleCauseCapture;
275 } 311 }
276 return LastMuteMetadata::FromWebContents(contents)->cause; 312 return LastMuteMetadata::FromWebContents(contents)->cause;
277 } 313 }
278 314
279 void SetTabAudioMuted(content::WebContents* contents, 315 TabMutedResult SetTabAudioMuted(content::WebContents* contents,
280 bool mute, 316 bool mute,
281 const std::string& cause) { 317 const std::string& cause) {
282 if (!contents || !chrome::CanToggleAudioMute(contents)) 318 DCHECK(contents);
283 return; 319
320 if (!IsTabAudioMutingFeatureEnabled())
321 return TAB_MUTED_RESULT_FAIL_NOT_ENABLED;
322
323 if (!chrome::CanToggleAudioMute(contents))
324 return TAB_MUTED_RESULT_FAIL_TABCAPTURE;
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 }
284 335
285 LastMuteMetadata::CreateForWebContents(contents); // Create if not exists. 336 LastMuteMetadata::CreateForWebContents(contents); // Create if not exists.
286 LastMuteMetadata::FromWebContents(contents)->cause = cause; 337 LastMuteMetadata::FromWebContents(contents)->cause = cause;
287 338
288 contents->SetAudioMuted(mute); 339 contents->SetAudioMuted(mute);
340
341 return TAB_MUTED_RESULT_SUCCESS;
289 } 342 }
290 343
291 bool IsTabAudioMuted(content::WebContents* contents) { 344 bool IsTabAudioMuted(content::WebContents* contents) {
292 return contents && contents->IsAudioMuted(); 345 return contents && contents->IsAudioMuted();
293 } 346 }
294 347
295 bool AreAllTabsMuted(const TabStripModel& tab_strip, 348 bool AreAllTabsMuted(const TabStripModel& tab_strip,
296 const std::vector<int>& indices) { 349 const std::vector<int>& indices) {
297 for (std::vector<int>::const_iterator i = indices.begin(); i != indices.end(); 350 for (std::vector<int>::const_iterator i = indices.begin(); i != indices.end();
298 ++i) { 351 ++i) {
299 if (!IsTabAudioMuted(tab_strip.GetWebContentsAt(*i))) 352 if (!IsTabAudioMuted(tab_strip.GetWebContentsAt(*i)))
300 return false; 353 return false;
301 } 354 }
302 return true; 355 return true;
303 } 356 }
304 357
305 } // namespace chrome 358 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698