1 // Copyright (c) 2013 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 #ifndef CHROME_BROWSER_UI_ACTIVE_TAB_TRACKER_H_ 6 #define CHROME_BROWSER_UI_ACTIVE_TAB_TRACKER_H_ 7 8 #include "base/logging.h" 9 #include "base/memory/weak_ptr.h" 10 #include "base/time/time.h" 11 #include "base/timer/timer.h" 12 #include "chrome/browser/idle.h" 13 #include "chrome/browser/ui/browser_list_observer.h" 14 #include "chrome/browser/ui/native_focus_tracker.h" 15 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h" 16 #include "content/public/browser/notification_observer.h" 17 #include "content/public/browser/notification_registrar.h" 18 #include "url/gurl.h" 19 20 class Browser; 21 22 namespace content { 23 class WebContents; 24 } 25 26 // ActiveTabTracker persists the amount of time the user views a page to 27 // history. Only pages the user views for more than |kTimeBeforeCommitMS| 28 // milliseconds while the page is active are persisted. 29 class ActiveTabTracker : public chrome::BrowserListObserver, 30 public TabStripModelObserver, 31 public content::NotificationObserver, 32 public NativeFocusTrackerHost { 33 public: 34 ActiveTabTracker(); 35 virtual ~ActiveTabTracker(); 36 37 // TODO(sky): remove this when we have NativeFocusTracker for other platforms. 38 bool is_valid() const { return native_focus_tracker_.get() != NULL; } 39 40 // TabStripModelObserver: 41 virtual void ActiveTabChanged(content::WebContents* old_contents, 42 content::WebContents* new_contents, 43 int index, 44 int reason) OVERRIDE; 45 virtual void TabReplacedAt(TabStripModel* tab_strip_model, 46 content::WebContents* old_contents, 47 content::WebContents* new_contents, 48 int index) OVERRIDE; 49 virtual void TabStripEmpty() OVERRIDE; 50 51 // BrowserListObserver: 52 virtual void OnBrowserRemoved(Browser* browser) OVERRIDE; 53 54 // content::NotificationObserver: 55 virtual void Observe(int type, 56 const content::NotificationSource& source, 57 const content::NotificationDetails& details) OVERRIDE; 58 59 // NativeFocusTrackerHost: 60 virtual void SetBrowser(Browser* browser) OVERRIDE; 61 62 private: 63 // Sets the active webcontents. 64 void SetWebContents(content::WebContents* web_contents); 65 66 // Sets the idle state. 67 void SetIdleState(IdleState idle_state); 68 69 // Starts the query for the idle state. Invokes SetIdleState() when idel state 70 // is found. 71 void QueryIdleState(); 72 73 // Returns the URL |web_contents_| is showing and needs to be tracked. Returns 74 // an empty GURL() is no |web_contents_| or the |web_contents_| is not showing 75 // a page that needs to be tracked. 76 GURL GetURLFromWebContents() const; 77 78 // If necessary commits the active time for the active tab. 79 void CommitActiveTime(); 80 81 scoped_ptr<NativeFocusTracker> native_focus_tracker_; 82 83 // The active Browser, or NULL if one is not active. 84 Browser* browser_; 85 86 // The active WebContents. 87 content::WebContents* web_contents_; 88 89 // Current idle state. Only valid if |browser_| is non-null and 90 // |weak_ptr_factory_| is empty. 91 IdleState idle_state_; 92 93 // Time the |idle_state_| became IDLE_STATE_ACTIVE, or a tab changed. 94 base::TimeTicks active_time_; 95 96 // Timer used to query for idle state. 97 base::Timer timer_; 98 99 // WeakPtrFactory used when querying for idle state. 100 base::WeakPtrFactory<ActiveTabTracker> weak_ptr_factory_; 101 102 // The url of the active tab. Empty indicates not valid. 103 GURL url_; 104 105 content::NotificationRegistrar registrar_; 106 107 DISALLOW_COPY_AND_ASSIGN(ActiveTabTracker); 108 }; 109 110 #endif // CHROME_BROWSER_UI_ACTIVE_TAB_TRACKER_H_ 111