summaryrefslogtreecommitdiffstats
path: root/dom/base/ScrollingMetrics.cpp
blob: 78dc6070235cb172f130fcc9470db4974437e76a (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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "ScrollingMetrics.h"
#include "mozilla/dom/ContentParent.h"
#include "mozilla/StaticPrefs_browser.h"

namespace mozilla {

static TimeStamp gScrollingStartTime;
static TimeStamp gScrollingEndTime;
static uint32_t gScrollDistanceCSSPixels = 0;
static dom::InteractionData gScrollingInteraction = {};

void ScrollingMetrics::OnScrollingInteractionEnded() {
  // We are only interested in content process scrolling
  if (XRE_IsParentProcess()) {
    return;
  }

  if (!gScrollingStartTime.IsNull() && !gScrollingEndTime.IsNull()) {
    gScrollingInteraction.mInteractionCount++;
    gScrollingInteraction.mInteractionTimeInMilliseconds +=
        static_cast<uint32_t>(
            (gScrollingEndTime - gScrollingStartTime).ToMilliseconds());

    gScrollingInteraction.mScrollingDistanceInPixels +=
        gScrollDistanceCSSPixels;
  }

  gScrollDistanceCSSPixels = 0;
  gScrollingStartTime = TimeStamp();
  gScrollingEndTime = TimeStamp();
}

void ScrollingMetrics::OnScrollingInteraction(CSSCoord distanceScrolled) {
  // We are only interested in content process scrolling
  if (XRE_IsParentProcess()) {
    return;
  }

  TimeStamp now = TimeStamp::Now();
  if (gScrollingEndTime.IsNull()) {
    gScrollingEndTime = now;
  }

  TimeDuration delay = now - gScrollingEndTime;

  // Has it been too long since the last scroll input event to consider it part
  // of the same interaction?
  if (delay >
      TimeDuration::FromMilliseconds(
          StaticPrefs::browser_places_interactions_scrolling_timeout_ms())) {
    OnScrollingInteractionEnded();
  }

  if (gScrollingStartTime.IsNull()) {
    gScrollingStartTime = now;
  }
  gScrollingEndTime = now;
  gScrollDistanceCSSPixels += static_cast<uint32_t>(distanceScrolled);
}

StaticAutoPtr<ScrollingMetrics> ScrollingMetrics::sSingleton;

ScrollingMetrics* ScrollingMetrics::GetSingleton() {
  if (!sSingleton) {
    sSingleton = new ScrollingMetrics;
  }

  return sSingleton.get();
}

struct ScrollingMetricsCollector {
  void AppendScrollingMetrics(const std::tuple<uint32_t, uint32_t>& aMetrics,
                              dom::ContentParent* aParent) {
    mTimeScrolledMS += std::get<0>(aMetrics);
    mDistanceScrolledPixels += std::get<1>(aMetrics);
  }

  ~ScrollingMetricsCollector() {
    mPromiseHolder.Resolve(
        std::make_tuple(mTimeScrolledMS, mDistanceScrolledPixels), __func__);
  }

  uint32_t mTimeScrolledMS = 0;
  uint32_t mDistanceScrolledPixels = 0;
  MozPromiseHolder<ScrollingMetrics::ScrollingMetricsPromise> mPromiseHolder;
};

auto ScrollingMetrics::CollectScrollingMetricsInternal()
    -> RefPtr<ScrollingMetrics::ScrollingMetricsPromise> {
  std::shared_ptr<ScrollingMetricsCollector> collector =
      std::make_shared<ScrollingMetricsCollector>();

  nsTArray<dom::ContentParent*> contentParents;
  dom::ContentParent::GetAll(contentParents);
  for (dom::ContentParent* parent : contentParents) {
    RefPtr<dom::ContentParent> parentRef = parent;
    parent->SendCollectScrollingMetrics(
        [collector, parentRef](const std::tuple<uint32_t, uint32_t>& aMetrics) {
          collector->AppendScrollingMetrics(aMetrics, parentRef.get());
        },
        [](mozilla::ipc::ResponseRejectReason) {});
  }

  return collector->mPromiseHolder.Ensure(__func__);
}

std::tuple<uint32_t, uint32_t>
ScrollingMetrics::CollectLocalScrollingMetricsInternal() {
  OnScrollingInteractionEnded();

  std::tuple<uint32_t, uint32_t> metrics =
      std::make_tuple(gScrollingInteraction.mInteractionTimeInMilliseconds,
                      gScrollingInteraction.mScrollingDistanceInPixels);
  gScrollingInteraction = {};
  return metrics;
}

}  // namespace mozilla