Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/metrics/audible_contents_tracker.h" | |
| 6 | |
| 7 #include <set> | |
|
Patrick Monette
2016/07/13 16:06:24
Nit: Already included in header
gayane -on leave until 09-2017
2016/07/20 20:22:02
Done.
| |
| 8 | |
| 9 #include "chrome/browser/ui/browser.h" | |
| 10 #include "chrome/browser/ui/browser_list.h" | |
| 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
| 12 | |
| 13 namespace metrics { | |
| 14 | |
| 15 AudibleContentsTracker::AudibleContentsTracker(Observer* observer) | |
| 16 : observer_(observer) { | |
| 17 auto browser_list = BrowserList::GetInstance(); | |
| 18 for (const auto browser : *browser_list) | |
| 19 browser->tab_strip_model()->AddObserver(this); | |
| 20 browser_list->AddObserver(this); | |
| 21 } | |
| 22 | |
| 23 AudibleContentsTracker::~AudibleContentsTracker() {} | |
| 24 | |
| 25 void AudibleContentsTracker::OnBrowserAdded(Browser* browser) { | |
| 26 browser->tab_strip_model()->AddObserver(this); | |
| 27 } | |
| 28 | |
| 29 void AudibleContentsTracker::OnBrowserRemoved(Browser* browser) { | |
| 30 browser->tab_strip_model()->RemoveObserver(this); | |
| 31 } | |
| 32 | |
| 33 void AudibleContentsTracker::TabClosingAt(TabStripModel* model, | |
| 34 content::WebContents* web_contents, | |
| 35 int index) { | |
| 36 RemoveAudibleWebContents(web_contents); | |
| 37 } | |
| 38 | |
| 39 void AudibleContentsTracker::TabChangedAt(content::WebContents* web_contents, | |
| 40 int index, | |
| 41 TabChangeType change_type) { | |
| 42 // Ignore 'loading' and 'title' changes. | |
| 43 if (change_type != TabStripModelObserver::ALL) | |
| 44 return; | |
| 45 | |
| 46 if (web_contents->WasRecentlyAudible()) { | |
| 47 AddAudibleWebContents(web_contents); | |
| 48 } else { | |
| 49 RemoveAudibleWebContents(web_contents); | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 void AudibleContentsTracker::TabReplacedAt( | |
| 54 TabStripModel* model, | |
| 55 content::WebContents* old_web_contents, | |
| 56 content::WebContents* new_web_contents, | |
| 57 int index) { | |
| 58 RemoveAudibleWebContents(old_web_contents); | |
| 59 if (new_web_contents->WasRecentlyAudible()) | |
| 60 AddAudibleWebContents(new_web_contents); | |
| 61 } | |
| 62 | |
| 63 void AudibleContentsTracker::AddAudibleWebContents( | |
| 64 content::WebContents* web_contents) { | |
| 65 // The first web contents to become audible indicates that audio has started. | |
| 66 bool added = audible_contents_.insert(web_contents).second; | |
| 67 if (added && audible_contents_.size() == 1) | |
| 68 observer_->OnAudioStart(); | |
| 69 } | |
| 70 | |
| 71 void AudibleContentsTracker::RemoveAudibleWebContents( | |
| 72 content::WebContents* web_contents) { | |
| 73 bool removed = (audible_contents_.erase(web_contents) == 1); | |
| 74 if (removed && audible_contents_.empty()) | |
| 75 observer_->OnAudioEnd(); | |
| 76 } | |
| 77 | |
| 78 } // namespace metrics | |
| OLD | NEW |