| 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/desktop_engagement/audible_contents_tracker.h" | |
| 6 | |
| 7 #include "chrome/browser/metrics/desktop_engagement/desktop_engagement_service.h
" | |
| 8 #include "chrome/browser/ui/browser.h" | |
| 9 #include "chrome/browser/ui/browser_list.h" | |
| 10 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
| 11 | |
| 12 namespace metrics { | |
| 13 | |
| 14 AudibleContentsTracker::AudibleContentsTracker(Observer* observer) | |
| 15 : observer_(observer) { | |
| 16 BrowserList* browser_list = BrowserList::GetInstance(); | |
| 17 for (Browser* browser : *browser_list) | |
| 18 browser->tab_strip_model()->AddObserver(this); | |
| 19 browser_list->AddObserver(this); | |
| 20 } | |
| 21 | |
| 22 AudibleContentsTracker::~AudibleContentsTracker() { | |
| 23 BrowserList::GetInstance()->RemoveObserver(this); | |
| 24 } | |
| 25 | |
| 26 void AudibleContentsTracker::OnBrowserAdded(Browser* browser) { | |
| 27 browser->tab_strip_model()->AddObserver(this); | |
| 28 } | |
| 29 | |
| 30 void AudibleContentsTracker::OnBrowserRemoved(Browser* browser) { | |
| 31 browser->tab_strip_model()->RemoveObserver(this); | |
| 32 } | |
| 33 | |
| 34 void AudibleContentsTracker::TabClosingAt(TabStripModel* model, | |
| 35 content::WebContents* web_contents, | |
| 36 int index) { | |
| 37 RemoveAudibleWebContents(web_contents); | |
| 38 } | |
| 39 | |
| 40 void AudibleContentsTracker::TabChangedAt(content::WebContents* web_contents, | |
| 41 int index, | |
| 42 TabChangeType change_type) { | |
| 43 // Ignore 'loading' and 'title' changes. | |
| 44 if (change_type != TabStripModelObserver::ALL) | |
| 45 return; | |
| 46 | |
| 47 if (web_contents->WasRecentlyAudible()) | |
| 48 AddAudibleWebContents(web_contents); | |
| 49 else | |
| 50 RemoveAudibleWebContents(web_contents); | |
| 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 // If the web content was previously audible and there are no other audible | |
| 74 // web contents then notify that audio ended. | |
| 75 bool removed = (audible_contents_.erase(web_contents) == 1); | |
| 76 if (removed && audible_contents_.empty()) | |
| 77 observer_->OnAudioEnd(); | |
| 78 } | |
| 79 | |
| 80 } // namespace metrics | |
| OLD | NEW |