summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/active_tab_tracker.cc
blob: 5a519841b7cbb34c5a37ccd3571455e26bbfbe9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/active_tab_tracker.h"

#include "chrome/browser/history/history_service.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/web_contents.h"

namespace {

// Amount of time a page has to be active before we commit it.
const int kTimeBeforeCommitMS = 1000;

// Number of seconds input should be received before considered idle.
const int kIdleTimeSeconds = 30;

}  // namespace

#if !defined(OS_WIN) && !defined(USE_AURA)
// static
NativeFocusTracker* NativeFocusTracker::Create(NativeFocusTrackerHost* host) {
  return NULL;
}
#endif

ActiveTabTracker::ActiveTabTracker()
    : browser_(NULL),
      web_contents_(NULL),
      idle_state_(IDLE_STATE_UNKNOWN),
      timer_(false, false),
      weak_ptr_factory_(this) {
  native_focus_tracker_.reset(NativeFocusTracker::Create(this));
  Browser* browser =
      BrowserList::GetInstance(chrome::GetActiveDesktop())->GetLastActive();
  SetBrowser(browser);
  BrowserList::AddObserver(this);
}

ActiveTabTracker::~ActiveTabTracker() {
  native_focus_tracker_.reset();
  SetBrowser(NULL);
  BrowserList::RemoveObserver(this);
}

void ActiveTabTracker::ActiveTabChanged(content::WebContents* old_contents,
                                        content::WebContents* new_contents,
                                        int index,
                                        int reason) {
  SetWebContents(new_contents);
}

void ActiveTabTracker::TabReplacedAt(TabStripModel* tab_strip_model,
                                     content::WebContents* old_contents,
                                     content::WebContents* new_contents,
                                     int index) {
  if (index == tab_strip_model->selection_model().active())
    SetWebContents(new_contents);
}

void ActiveTabTracker::TabStripEmpty() {
  SetBrowser(NULL);
}

void ActiveTabTracker::OnBrowserRemoved(Browser* browser) {
  if (browser == browser_)
    SetBrowser(NULL);
}

void ActiveTabTracker::Observe(int type,
                               const content::NotificationSource& source,
                               const content::NotificationDetails& details) {
  const GURL url = GetURLFromWebContents();
  if (url == url_)
    return;

  CommitActiveTime();
  url_ = url;
}

void ActiveTabTracker::SetBrowser(Browser* browser) {
  if (browser_ == browser)
    return;

  CommitActiveTime();
  if (browser_)
    browser_->tab_strip_model()->RemoveObserver(this);
  // Don't track anything for otr profiles.
  if (browser && browser->profile()->IsOffTheRecord())
    browser = NULL;
  browser_ = browser;
  content::WebContents* web_contents = NULL;
  if (browser_) {
    TabStripModel* tab_strip = browser_->tab_strip_model();
    tab_strip->AddObserver(this);
    web_contents = tab_strip->GetActiveWebContents();
  } else {
    idle_state_ = IDLE_STATE_UNKNOWN;
    timer_.Stop();
    weak_ptr_factory_.InvalidateWeakPtrs();
  }
  SetWebContents(web_contents);
}

void ActiveTabTracker::SetWebContents(content::WebContents* web_contents) {
  if (web_contents_ == web_contents)
    return;

  CommitActiveTime();

  active_time_ = base::TimeTicks::Now();
  web_contents_ = web_contents;
  url_ = GetURLFromWebContents();
  registrar_.RemoveAll();
  // TODO(sky): this isn't quite right. We should really not include transient
  // entries here. For that we need to make Browser::NavigationStateChanged()
  // call through to this class.
  if (web_contents_) {
    registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
                   content::Source<content::NavigationController>(
                       &web_contents_->GetController()));
    QueryIdleState();
  }
}

void ActiveTabTracker::SetIdleState(IdleState idle_state) {
  if (idle_state_ != idle_state) {
    if (idle_state_ == IDLE_STATE_ACTIVE)
      CommitActiveTime();
    else if (idle_state == IDLE_STATE_ACTIVE)
      active_time_ = base::TimeTicks::Now();
    idle_state_ = idle_state;
  }
  if (browser_) {
    timer_.Start(FROM_HERE,
                 base::TimeDelta::FromSeconds(kIdleTimeSeconds),
                 base::Bind(&ActiveTabTracker::QueryIdleState,
                            base::Unretained(this)));
  }
}

void ActiveTabTracker::QueryIdleState() {
  if (weak_ptr_factory_.HasWeakPtrs())
    return;

  CalculateIdleState(kIdleTimeSeconds,
                     base::Bind(&ActiveTabTracker::SetIdleState,
                                weak_ptr_factory_.GetWeakPtr()));
}

GURL ActiveTabTracker::GetURLFromWebContents() const {
  if (!web_contents_)
    return GURL();

  // TODO: handle subframe transitions better. Maybe go back to first entry
  // that isn't a main frame?
  content::NavigationEntry* entry =
      web_contents_->GetController().GetLastCommittedEntry();
  if (!entry || !PageTransitionIsMainFrame(entry->GetTransitionType()))
    return GURL();
  return !entry->GetUserTypedURL().is_empty() ?
      entry->GetUserTypedURL() : entry->GetURL();
}

void ActiveTabTracker::CommitActiveTime() {
  const base::TimeDelta active_delta = base::TimeTicks::Now() - active_time_;
  active_time_ = base::TimeTicks::Now();

  if (!web_contents_ || url_.is_empty() || idle_state_ != IDLE_STATE_ACTIVE ||
      active_delta.InMilliseconds() < kTimeBeforeCommitMS)
    return;

  Profile* profile = Profile::FromBrowserContext(
      web_contents_->GetBrowserContext());
  HistoryService* history = HistoryServiceFactory::GetForProfile(
      profile, Profile::EXPLICIT_ACCESS);
  if (history)
    history->IncreaseSegmentDuration(url_, base::Time::Now(), active_delta);
}