| 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 namespace { |
| 15 |
| 16 AudibleContentsTracker* g_tracker = nullptr; |
| 17 AudibleContentsTracker::Observer* g_observer = nullptr; |
| 18 |
| 19 } // namespace |
| 20 |
| 21 void AudibleContentsTracker::Observer::OnAudioStart() { |
| 22 DesktopEngagementService::Get()->OnAudioStart(); |
| 23 } |
| 24 |
| 25 void AudibleContentsTracker::Observer::OnAudioEnd() { |
| 26 DesktopEngagementService::Get()->OnAudioEnd(); |
| 27 } |
| 28 |
| 29 AudibleContentsTracker::AudibleContentsTracker(Observer* observer) |
| 30 : observer_(observer) { |
| 31 BrowserList* browser_list = BrowserList::GetInstance(); |
| 32 for (Browser* browser : *browser_list) |
| 33 browser->tab_strip_model()->AddObserver(this); |
| 34 browser_list->AddObserver(this); |
| 35 } |
| 36 |
| 37 AudibleContentsTracker::~AudibleContentsTracker() {} |
| 38 |
| 39 // static |
| 40 void AudibleContentsTracker::Initialize() { |
| 41 DCHECK(!g_observer); |
| 42 DCHECK(!g_tracker); |
| 43 g_observer = new AudibleContentsTracker::Observer(); |
| 44 g_tracker = new AudibleContentsTracker(g_observer); |
| 45 } |
| 46 |
| 47 void AudibleContentsTracker::OnBrowserAdded(Browser* browser) { |
| 48 browser->tab_strip_model()->AddObserver(this); |
| 49 } |
| 50 |
| 51 void AudibleContentsTracker::OnBrowserRemoved(Browser* browser) { |
| 52 browser->tab_strip_model()->RemoveObserver(this); |
| 53 } |
| 54 |
| 55 void AudibleContentsTracker::TabClosingAt(TabStripModel* model, |
| 56 content::WebContents* web_contents, |
| 57 int index) { |
| 58 RemoveAudibleWebContents(web_contents); |
| 59 } |
| 60 |
| 61 void AudibleContentsTracker::TabChangedAt(content::WebContents* web_contents, |
| 62 int index, |
| 63 TabChangeType change_type) { |
| 64 // Ignore 'loading' and 'title' changes. |
| 65 if (change_type != TabStripModelObserver::ALL) |
| 66 return; |
| 67 |
| 68 if (web_contents->WasRecentlyAudible()) |
| 69 AddAudibleWebContents(web_contents); |
| 70 else |
| 71 RemoveAudibleWebContents(web_contents); |
| 72 } |
| 73 |
| 74 void AudibleContentsTracker::TabReplacedAt( |
| 75 TabStripModel* model, |
| 76 content::WebContents* old_web_contents, |
| 77 content::WebContents* new_web_contents, |
| 78 int index) { |
| 79 RemoveAudibleWebContents(old_web_contents); |
| 80 if (new_web_contents->WasRecentlyAudible()) |
| 81 AddAudibleWebContents(new_web_contents); |
| 82 } |
| 83 |
| 84 void AudibleContentsTracker::AddAudibleWebContents( |
| 85 content::WebContents* web_contents) { |
| 86 // The first web contents to become audible indicates that audio has started. |
| 87 bool added = audible_contents_.insert(web_contents).second; |
| 88 if (added && audible_contents_.size() == 1) |
| 89 observer_->OnAudioStart(); |
| 90 } |
| 91 |
| 92 void AudibleContentsTracker::RemoveAudibleWebContents( |
| 93 content::WebContents* web_contents) { |
| 94 // If the web content was previously audible and there are no other audible |
| 95 // web contents then notify that audio ended. |
| 96 bool removed = (audible_contents_.erase(web_contents) == 1); |
| 97 if (removed && audible_contents_.empty()) |
| 98 observer_->OnAudioEnd(); |
| 99 } |
| 100 |
| 101 } // namespace metrics |
| OLD | NEW |