summaryrefslogtreecommitdiffstats
path: root/content/browser/media/capture/window_activity_tracker_aura.cc
blob: 035c0ca68ea52a9c066a45bdf2cc7d32e3d1b5c3 (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
// Copyright (c) 2015 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 "content/browser/media/capture/window_activity_tracker_aura.h"

#include "base/logging.h"
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#include "ui/events/event_utils.h"

namespace content {

namespace {
// The time period within which a triggered UI event is considered
// currently active.
const int kTimePeriodUiEventMicros = 100000;  // 100 ms

// Minimum number of user interactions before we consider the user to be in
// interactive mode. The goal is to prevent user interactions to launch
// animated content from causing target playout time flip-flop.
const int kMinUserInteractions = 5;
}  // namespace

WindowActivityTrackerAura::WindowActivityTrackerAura(aura::Window* window)
    : window_(window),
      last_time_ui_event_detected_(base::TimeTicks()),
      ui_events_count_(0),
      weak_factory_(this) {
  if (window_) {
    window_->AddObserver(this);
    window_->AddPreTargetHandler(this);
  }
}

WindowActivityTrackerAura::~WindowActivityTrackerAura() {
  if (window_) {
    window_->RemoveObserver(this);
    window_->RemovePreTargetHandler(this);
  }
}

base::WeakPtr<WindowActivityTracker> WindowActivityTrackerAura::GetWeakPtr() {
  return weak_factory_.GetWeakPtr();
}

bool WindowActivityTrackerAura::IsUiInteractionActive() const {
  return ui_events_count_ > kMinUserInteractions;
}

void WindowActivityTrackerAura::RegisterMouseInteractionObserver(
    const base::Closure& observer) {
  mouse_interaction_observer_ = observer;
}

void WindowActivityTrackerAura::Reset() {
  ui_events_count_ = 0;
  last_time_ui_event_detected_ = base::TimeTicks();
}

void WindowActivityTrackerAura::OnEvent(ui::Event* event) {
  if (!mouse_interaction_observer_.is_null() && event->IsMouseEvent())
    mouse_interaction_observer_.Run();
  if (base::TimeTicks::Now() - last_time_ui_event_detected_ >
      base::TimeDelta::FromMicroseconds(kTimePeriodUiEventMicros)) {
    ui_events_count_++;
  }
  last_time_ui_event_detected_ = base::TimeTicks::Now();
}

void WindowActivityTrackerAura::OnWindowDestroying(aura::Window* window) {
  DCHECK_EQ(window_, window);
  window_->RemovePreTargetHandler(this);
  window_->RemoveObserver(this);
  window_ = nullptr;
}

}  // namespace content