| 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 |
| 24 void AudibleContentsTracker::OnBrowserAdded(Browser* browser) { |
| 25 browser->tab_strip_model()->AddObserver(this); |
| 26 } |
| 27 |
| 28 void AudibleContentsTracker::OnBrowserRemoved(Browser* browser) { |
| 29 browser->tab_strip_model()->RemoveObserver(this); |
| 30 } |
| 31 |
| 32 void AudibleContentsTracker::TabClosingAt(TabStripModel* model, |
| 33 content::WebContents* web_contents, |
| 34 int index) { |
| 35 RemoveAudibleWebContents(web_contents); |
| 36 } |
| 37 |
| 38 void AudibleContentsTracker::TabChangedAt(content::WebContents* web_contents, |
| 39 int index, |
| 40 TabChangeType change_type) { |
| 41 // Ignore 'loading' and 'title' changes. |
| 42 if (change_type != TabStripModelObserver::ALL) |
| 43 return; |
| 44 |
| 45 if (web_contents->WasRecentlyAudible()) |
| 46 AddAudibleWebContents(web_contents); |
| 47 else |
| 48 RemoveAudibleWebContents(web_contents); |
| 49 } |
| 50 |
| 51 void AudibleContentsTracker::TabReplacedAt( |
| 52 TabStripModel* model, |
| 53 content::WebContents* old_web_contents, |
| 54 content::WebContents* new_web_contents, |
| 55 int index) { |
| 56 RemoveAudibleWebContents(old_web_contents); |
| 57 if (new_web_contents->WasRecentlyAudible()) |
| 58 AddAudibleWebContents(new_web_contents); |
| 59 } |
| 60 |
| 61 void AudibleContentsTracker::AddAudibleWebContents( |
| 62 content::WebContents* web_contents) { |
| 63 // The first web contents to become audible indicates that audio has started. |
| 64 bool added = audible_contents_.insert(web_contents).second; |
| 65 if (added && audible_contents_.size() == 1) |
| 66 observer_->OnAudioStart(); |
| 67 } |
| 68 |
| 69 void AudibleContentsTracker::RemoveAudibleWebContents( |
| 70 content::WebContents* web_contents) { |
| 71 // If the web content was previously audible and there are no other audible |
| 72 // web contents then notify that audio ended. |
| 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 |