summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/pdf/metrics.js
blob: d0cb77c8d97dbfeb7499d1c95d71637c2a9017c8 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright 2017 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.

(function() {

'use strict';

// Keep in sync with enums.xml.
// Do not change the numeric values or reuse them since these numbers are
// persisted to logs.
const UserAction = {
  DOCUMENT_OPENED: 0,  // Baseline to use as denominator for all formulas.
  ROTATE_FIRST: 1,
  ROTATE: 2,
  FIT_TO_WIDTH_FIRST: 3,
  FIT_TO_WIDTH: 4,
  FIT_TO_PAGE_FIRST: 5,
  FIT_TO_PAGE: 6,
  OPEN_BOOKMARKS_PANEL_FIRST: 7,
  OPEN_BOOKMARKS_PANEL: 8,
  FOLLOW_BOOKMARK_FIRST: 9,
  FOLLOW_BOOKMARK: 10,
  PAGE_SELECTOR_NAVIGATE_FIRST: 11,
  PAGE_SELECTOR_NAVIGATE: 12,
  NUMBER_OF_ACTIONS: 13
};

/**
 * Handles events specific to the PDF viewer and logs the corresponding metrics.
 *
 * @interface
 */
window.PDFMetrics = class {
  constructor() {}

  /**
   * Call when the document is first loaded. This event serves as denominator to
   * determine percentages of documents in which an action was taken as well as
   * average number of each action per document.
   */
  onDocumentOpened() {}

  /**
   * Call when the document is rotated clockwise or counter-clockwise.
   */
  onRotation() {}

  /**
   * Call when the zoom mode is changed to fit a FittingType.
   *
   * @param {FittingType} fittingType the new FittingType.
   */
  onFitTo(fittingType) {}

  /**
   * Call when the bookmarks panel is opened.
   */
  onOpenBookmarksPanel() {}

  /**
   * Call when a bookmark is followed.
   */
  onFollowBookmark() {}

  /**
   * Call when the page selection is used to navigate to another page.
   */
  onPageSelectorNavigation() {}
};

/**
 * Dummy implementation of PDFMetrics.
 * This is used in print preview mode to avoid bundling the actions in the PDF
 * viewer and the print preview in the same histogram. Also, metricsPrivate is
 * not available in print preview.
 *
 * @implements {PDFMetrics}
 */
window.PDFMetricsDummy = class {
  constructor() {}

  /** @override */
  onDocumentOpened() {}

  /** @override */
  onRotation() {}

  /** @override */
  onFitTo(fittingType) {}

  /** @override */
  onOpenBookmarksPanel() {}

  /** @override */
  onFollowBookmark() {}

  /** @override */
  onPageSelectorNavigation() {}
};

/**
 * Implementation of PDFMetrics that logs the corresponding metrics to UMA
 * through chrome.metricsPrivate.
 *
 * @implements {PDFMetrics}
 */
window.PDFMetricsImpl = class {
  constructor() {
    /**
     * @private {Set}
     */
    this.firstEventLogged_ = new Set();

    /**
     * @private {Object}
     */
    this.actionsMetric_ = {
      'metricName': 'PDF.Actions',
      'type': chrome.metricsPrivate.MetricTypeType.HISTOGRAM_LOG,
      'min': 1,
      'max': UserAction.NUMBER_OF_ACTIONS,
      'buckets': UserAction.NUMBER_OF_ACTIONS + 1
    };
  }

  /** @override */
  onDocumentOpened() {
    this.logOnlyFirstTime_(UserAction.DOCUMENT_OPENED);
  }

  /** @override */
  onRotation() {
    this.logFirstAndTotal_(UserAction.ROTATE_FIRST, UserAction.ROTATE);
  }

  /** @override */
  onFitTo(fittingType) {
    if (fittingType == FittingType.FIT_TO_PAGE) {
      this.logFirstAndTotal_(
          UserAction.FIT_TO_PAGE_FIRST, UserAction.FIT_TO_PAGE);
    } else if (fittingType == FittingType.FIT_TO_WIDTH) {
      this.logFirstAndTotal_(
          UserAction.FIT_TO_WIDTH_FIRST, UserAction.FIT_TO_WIDTH);
    }
    // There is no user action to do a fit-to-height, this only happens with
    // the open param "view=FitV".
  }

  /** @override */
  onOpenBookmarksPanel() {
    this.logFirstAndTotal_(
        UserAction.OPEN_BOOKMARKS_PANEL_FIRST, UserAction.OPEN_BOOKMARKS_PANEL);
  }

  /** @override */
  onFollowBookmark() {
    this.logFirstAndTotal_(
        UserAction.FOLLOW_BOOKMARK_FIRST, UserAction.FOLLOW_BOOKMARK);
  }

  /** @override */
  onPageSelectorNavigation() {
    this.logFirstAndTotal_(
        UserAction.PAGE_SELECTOR_NAVIGATE_FIRST,
        UserAction.PAGE_SELECTOR_NAVIGATE);
  }

  /**
   * Logs the "first" event code if it hasn't been logged by this instance yet
   * and also log the "total" event code. This distinction allows analyzing
   * both:
   * - in what percentage of documents each action was taken;
   * - how many times, on average, each action is taken on a document;
   *
   * @param {number} firstEventCode event code for the "first" metric.
   * @return {number} totalEventCode event code for the "total"  metric.
   * @private
   */
  logFirstAndTotal_(firstEventCode, totalEventCode) {
    this.log_(totalEventCode);
    this.logOnlyFirstTime_(firstEventCode);
  }

  /**
   * Logs the given event code to chrome.metricsPrivate.
   *
   * @param {number} eventCode event code to log.
   * @private
   */
  log_(eventCode) {
    chrome.metricsPrivate.recordValue(this.actionsMetric_, eventCode);
  }

  /**
   * Logs the given event code. Subsequent calls of this method with the same
   * event code have no effect on the this PDFMetrics instance.
   *
   * @param {number} eventCode event code to log.
   * @private
   */
  logOnlyFirstTime_(eventCode) {
    if (!this.firstEventLogged_.has(eventCode)) {
      this.log_(eventCode);
      this.firstEventLogged_.add(eventCode);
    }
  }
};

window.PDFMetrics.UserAction = UserAction;

}());