| 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/ui/browser.h" |
| 8 #include "chrome/browser/ui/browser_list.h" |
| 9 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 10 |
| 11 namespace metrics { |
| 12 |
| 13 AudibleContentsTracker::AudibleContentsTracker(Observer* observer) |
| 14 : observer_(observer) { |
| 15 BrowserList* browser_list = BrowserList::GetInstance(); |
| 16 for (Browser* browser : *browser_list) |
| 17 browser->tab_strip_model()->AddObserver(this); |
| 18 browser_list->AddObserver(this); |
| 19 } |
| 20 |
| 21 AudibleContentsTracker::~AudibleContentsTracker() {} |
| 22 |
| 23 void AudibleContentsTracker::OnBrowserAdded(Browser* browser) { |
| 24 browser->tab_strip_model()->AddObserver(this); |
| 25 } |
| 26 |
| 27 void AudibleContentsTracker::OnBrowserRemoved(Browser* browser) { |
| 28 browser->tab_strip_model()->RemoveObserver(this); |
| 29 } |
| 30 |
| 31 void AudibleContentsTracker::TabClosingAt(TabStripModel* model, |
| 32 content::WebContents* web_contents, |
| 33 int index) { |
| 34 RemoveAudibleWebContents(web_contents); |
| 35 } |
| 36 |
| 37 void AudibleContentsTracker::TabChangedAt(content::WebContents* web_contents, |
| 38 int index, |
| 39 TabChangeType change_type) { |
| 40 // Ignore 'loading' and 'title' changes. |
| 41 if (change_type != TabStripModelObserver::ALL) |
| 42 return; |
| 43 |
| 44 if (web_contents->WasRecentlyAudible()) |
| 45 AddAudibleWebContents(web_contents); |
| 46 else |
| 47 RemoveAudibleWebContents(web_contents); |
| 48 } |
| 49 |
| 50 void AudibleContentsTracker::TabReplacedAt( |
| 51 TabStripModel* model, |
| 52 content::WebContents* old_web_contents, |
| 53 content::WebContents* new_web_contents, |
| 54 int index) { |
| 55 RemoveAudibleWebContents(old_web_contents); |
| 56 if (new_web_contents->WasRecentlyAudible()) |
| 57 AddAudibleWebContents(new_web_contents); |
| 58 } |
| 59 |
| 60 void AudibleContentsTracker::AddAudibleWebContents( |
| 61 content::WebContents* web_contents) { |
| 62 // The first web contents to become audible indicates that audio has started. |
| 63 bool added = audible_contents_.insert(web_contents).second; |
| 64 if (added && audible_contents_.size() == 1) |
| 65 observer_->OnAudioStart(); |
| 66 } |
| 67 |
| 68 void AudibleContentsTracker::RemoveAudibleWebContents( |
| 69 content::WebContents* web_contents) { |
| 70 // If the web content was previously audible and there are no other audible |
| 71 // web contents then notify that audio ended. |
| 72 bool removed = (audible_contents_.erase(web_contents) == 1); |
| 73 if (removed && audible_contents_.empty()) |
| 74 observer_->OnAudioEnd(); |
| 75 } |
| 76 |
| 77 } // namespace metrics |
| OLD | NEW |