Commit 6bf84bd9 authored by Katie D's avatar Katie D Committed by Commit Bot

Refactor metrics_utils from select_to_speak.

This moves all metrics related functionality into a separate class,
and is a pure refactor.

Cq-Include-Trybots: luci.chromium.try:closure_compilation
Change-Id: Idba2a55655c3e67476500618e6c324a10cfa0b37
Reviewed-on: https://chromium-review.googlesource.com/1141115Reviewed-by: default avatarDavid Tseng <dtseng@chromium.org>
Commit-Queue: Katie Dektar <katie@chromium.org>
Cr-Commit-Position: refs/heads/master@{#576319}
parent b1f87486
...@@ -35,6 +35,7 @@ run_jsbundler("select_to_speak_copied_files") { ...@@ -35,6 +35,7 @@ run_jsbundler("select_to_speak_copied_files") {
"closure_shim.js", "closure_shim.js",
"earcons/null_selection.ogg", "earcons/null_selection.ogg",
"input_handler.js", "input_handler.js",
"metrics_utils.js",
"node_utils.js", "node_utils.js",
"options.css", "options.css",
"options.html", "options.html",
...@@ -171,6 +172,7 @@ js_type_check("closure_compile") { ...@@ -171,6 +172,7 @@ js_type_check("closure_compile") {
":../chromevox/cvox2/background/tree_walker", ":../chromevox/cvox2/background/tree_walker",
":closure_shim", ":closure_shim",
":input_handler", ":input_handler",
":metrics_utils",
":node_utils", ":node_utils",
":paragraph_utils", ":paragraph_utils",
":rect_utils", ":rect_utils",
...@@ -185,6 +187,7 @@ js_library("select_to_speak") { ...@@ -185,6 +187,7 @@ js_library("select_to_speak") {
":../chromevox/cvox2/background/automation_util", ":../chromevox/cvox2/background/automation_util",
":../chromevox/cvox2/background/constants", ":../chromevox/cvox2/background/constants",
":input_handler", ":input_handler",
":metrics_utils",
":node_utils", ":node_utils",
":paragraph_utils", ":paragraph_utils",
":rect_utils", ":rect_utils",
...@@ -196,7 +199,6 @@ js_library("select_to_speak") { ...@@ -196,7 +199,6 @@ js_library("select_to_speak") {
"$externs_path/chrome_extensions.js", "$externs_path/chrome_extensions.js",
"$externs_path/clipboard.js", "$externs_path/clipboard.js",
"$externs_path/command_line_private.js", "$externs_path/command_line_private.js",
"$externs_path/metrics_private.js",
"externs.js", "externs.js",
] ]
} }
...@@ -250,6 +252,10 @@ js_library("input_handler") { ...@@ -250,6 +252,10 @@ js_library("input_handler") {
js_library("rect_utils") { js_library("rect_utils") {
} }
js_library("metrics_utils") {
externs_list = [ "$externs_path/metrics_private.js" ]
}
js_library("../chromevox/cvox2/background/automation_util") { js_library("../chromevox/cvox2/background/automation_util") {
deps = [ deps = [
":../chromevox/cvox2/background/automation_predicate", ":../chromevox/cvox2/background/automation_predicate",
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
"tree_walker.js", "tree_walker.js",
"automation_util.js", "automation_util.js",
"input_handler.js", "input_handler.js",
"metrics_utils.js",
"node_utils.js", "node_utils.js",
"paragraph_utils.js", "paragraph_utils.js",
"word_utils.js", "word_utils.js",
......
// Copyright 2018 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.
// Utilities for UMA metrics.
/**
* @constructor
*/
let MetricsUtils = function() {};
/**
* Defines an enumeration metric. The |EVENT_COUNT| must be kept in sync
* with the number of enum values for each metric in
* tools/metrics/histograms/enums.xml.
* @typedef {{EVENT_COUNT: number, METRIC_NAME: string}}
*/
MetricsUtils.EnumerationMetric;
/**
* CrosSelectToSpeakStartSpeechMethod enums.
* These values are persisted to logs and should not be renumbered or re-used.
* See tools/metrics/histograms/enums.xml.
* @enum {number}
*/
MetricsUtils.StartSpeechMethod = {
MOUSE: 0,
KEYSTROKE: 1,
};
/**
* Constants for the start speech method metric,
* CrosSelectToSpeakStartSpeechMethod.
* @type {MetricsUtils.EnumerationMetric}
*/
MetricsUtils.START_SPEECH_METHOD_METRIC = {
EVENT_COUNT: Object.keys(MetricsUtils.StartSpeechMethod).length,
METRIC_NAME: 'Accessibility.CrosSelectToSpeak.StartSpeechMethod'
};
/**
* CrosSelectToSpeakStateChangeEvent enums.
* These values are persisted to logs and should not be renumbered or re-used.
* See tools/metrics/histograms/enums.xml.
* @enum {number}
*/
MetricsUtils.StateChangeEvent = {
START_SELECTION: 0,
CANCEL_SPEECH: 1,
CANCEL_SELECTION: 2,
};
/**
* Constants for the state change metric, CrosSelectToSpeakStateChangeEvent.
* @type {MetricsUtils.EnumerationMetric}
*/
MetricsUtils.STATE_CHANGE_METRIC = {
EVENT_COUNT: Object.keys(MetricsUtils.StateChangeEvent).length,
METRIC_NAME: 'Accessibility.CrosSelectToSpeak.StateChangeEvent'
};
/**
* The start speech metric name.
* @type {string}
*/
MetricsUtils.START_SPEECH_METRIC =
'Accessibility.CrosSelectToSpeak.StartSpeech';
/**
* The cancel speech metric name.
* @type {string}
*/
MetricsUtils.CANCEL_SPEECH_METRIC =
'Accessibility.CrosSelectToSpeak.CancelSpeech';
/**
* The speech pitch histogram metric name.
* @type {string}
*/
MetricsUtils.SPEECH_PITCH_METRIC =
'Accessibility.CrosSelectToSpeak.SpeechPitch';
/**
* The speech rate histogram metric name.
* @type {string}
*/
MetricsUtils.SPEECH_RATE_METRIC = 'Accessibility.CrosSelectToSpeak.SpeechRate';
/**
* The word highlighting metric name.
* @type {string}
*/
MetricsUtils.WORD_HIGHLIGHTING_METRIC =
'Accessibility.CrosSelectToSpeak.WordHighlighting';
/**
* Records a cancel event if speech was in progress.
* @param {boolean} speaking Whether speech was in progress
* @public
*/
MetricsUtils.recordCancelIfSpeaking = function(speaking) {
if (speaking) {
MetricsUtils.recordCancelEvent_();
}
};
/**
* Converts the speech rate into an enum based on
* tools/metrics/histograms/enums.xml.
* These values are persisted to logs. Entries should not be
* renumbered and numeric values should never be reused.
* @param {number} speechRate The current speech rate.
* @return {number} The current speech rate as an int for metrics.
* @private
*/
MetricsUtils.speechRateToSparceHistogramInt_ = function(speechRate) {
return speechRate * 100;
};
/**
* Converts the speech pitch into an enum based on
* tools/metrics/histograms/enums.xml.
* These values are persisted to logs. Entries should not be
* renumbered and numeric values should never be reused.
* @param {number} speechPitch The current speech pitch.
* @return {number} The current speech pitch as an int for metrics.
* @private
*/
MetricsUtils.speechPitchToSparceHistogramInt_ = function(speechPitch) {
return speechPitch * 100;
};
/**
* Records an event that Select-to-Speak has begun speaking.
* @param {number} method The CrosSelectToSpeakStartSpeechMethod enum
* that reflects how this event was triggered by the user.
* @param {number} speechRate The current speech rate.
* @param {number} speechPitch The current speech pitch.
* @param {boolean} wordHighlightingEnabled If word highlighting is enabled.
* @public
*/
MetricsUtils.recordStartEvent = function(
method, speechRate, speechPitch, wordHighlightingEnabled) {
chrome.metricsPrivate.recordUserAction(MetricsUtils.START_SPEECH_METRIC);
chrome.metricsPrivate.recordSparseValue(
MetricsUtils.SPEECH_RATE_METRIC,
MetricsUtils.speechRateToSparceHistogramInt_(speechRate));
chrome.metricsPrivate.recordSparseValue(
MetricsUtils.SPEECH_PITCH_METRIC,
MetricsUtils.speechPitchToSparceHistogramInt_(speechPitch));
chrome.metricsPrivate.recordBoolean(
MetricsUtils.WORD_HIGHLIGHTING_METRIC, wordHighlightingEnabled);
chrome.metricsPrivate.recordEnumerationValue(
MetricsUtils.START_SPEECH_METHOD_METRIC.METRIC_NAME, method,
MetricsUtils.START_SPEECH_METHOD_METRIC.EVENT_COUNT);
};
/**
* Records an event that Select-to-Speak speech has been canceled.
* @private
*/
MetricsUtils.recordCancelEvent_ = function() {
chrome.metricsPrivate.recordUserAction(MetricsUtils.CANCEL_SPEECH_METRIC);
};
/**
* Records a user-requested state change event from a given state.
* @param {number} changeType
* @public
*/
MetricsUtils.recordSelectToSpeakStateChangeEvent = function(changeType) {
chrome.metricsPrivate.recordEnumerationValue(
MetricsUtils.STATE_CHANGE_METRIC.METRIC_NAME, changeType,
MetricsUtils.STATE_CHANGE_METRIC.EVENT_COUNT);
};
...@@ -6,50 +6,6 @@ var AutomationEvent = chrome.automation.AutomationEvent; ...@@ -6,50 +6,6 @@ var AutomationEvent = chrome.automation.AutomationEvent;
var EventType = chrome.automation.EventType; var EventType = chrome.automation.EventType;
var RoleType = chrome.automation.RoleType; var RoleType = chrome.automation.RoleType;
/**
* CrosSelectToSpeakStartSpeechMethod enums.
* These values are persisted to logs and should not be renumbered or re-used.
* See tools/metrics/histograms/enums.xml.
* @enum {number}
*/
const StartSpeechMethod = {
MOUSE: 0,
KEYSTROKE: 1,
};
/**
* The number of enum values in CrosSelectToSpeakStartSpeechMethod. This should
* be kept in sync with the enum count in tools/metrics/histograms/enums.xml.
* @type {number}
*/
const START_SPEECH_METHOD_COUNT = Object.keys(StartSpeechMethod).length;
/**
* CrosSelectToSpeakStateChangeEvent enums.
* These values are persisted to logs and should not be renumbered or re-used.
* See tools/metrics/histograms/enums.xml.
* @enum {number}
*/
const StateChangeEvent = {
START_SELECTION: 0,
CANCEL_SPEECH: 1,
CANCEL_SELECTION: 2,
};
/**
* The number of enum values in CrosSelectToSpeakStateChangeEvent. This should
* be kept in sync with the enum count in tools/metrics/histograms/enums.xml.
* @type {number}
*/
const STATE_CHANGE_EVENT_COUNT = Object.keys(StateChangeEvent).length;
/**
* The name of the state change request metric.
* @type {string}
*/
const STATE_CHANGE_EVENT_METRIC_NAME =
'Accessibility.CrosSelectToSpeak.StateChangeEvent';
// This must be the same as in ash/system/accessibility/select_to_speak_tray.cc: // This must be the same as in ash/system/accessibility/select_to_speak_tray.cc:
// ash::kSelectToSpeakTrayClassName. // ash::kSelectToSpeakTrayClassName.
const SELECT_TO_SPEAK_TRAY_CLASS_NAME = const SELECT_TO_SPEAK_TRAY_CLASS_NAME =
...@@ -237,7 +193,9 @@ SelectToSpeak.prototype = { ...@@ -237,7 +193,9 @@ SelectToSpeak.prototype = {
return; return;
} }
this.startSpeechQueue_(nodes); this.startSpeechQueue_(nodes);
this.recordStartEvent_(StartSpeechMethod.MOUSE); MetricsUtils.recordStartEvent(
MetricsUtils.StartSpeechMethod.MOUSE, this.speechRate_,
this.speechPitch_, this.wordHighlight_);
}.bind(this)); }.bind(this));
}, },
...@@ -391,7 +349,9 @@ SelectToSpeak.prototype = { ...@@ -391,7 +349,9 @@ SelectToSpeak.prototype = {
return; return;
} }
this.initializeScrollingToOffscreenNodes_(focusedNode.root); this.initializeScrollingToOffscreenNodes_(focusedNode.root);
this.recordStartEvent_(StartSpeechMethod.KEYSTROKE); MetricsUtils.recordStartEvent(
MetricsUtils.StartSpeechMethod.KEYSTROKE, this.speechRate_,
this.speechPitch_, this.wordHighlight_);
}, },
/** /**
...@@ -527,21 +487,21 @@ SelectToSpeak.prototype = { ...@@ -527,21 +487,21 @@ SelectToSpeak.prototype = {
// Start selection. // Start selection.
this.inputHandler_.setTrackingMouse(true); this.inputHandler_.setTrackingMouse(true);
this.onStateChanged_(SelectToSpeakState.SELECTING); this.onStateChanged_(SelectToSpeakState.SELECTING);
this.recordSelectToSpeakStateChangeEvent_( MetricsUtils.recordSelectToSpeakStateChangeEvent(
StateChangeEvent.START_SELECTION); MetricsUtils.StateChangeEvent.START_SELECTION);
break; break;
case SelectToSpeakState.SPEAKING: case SelectToSpeakState.SPEAKING:
// Stop speaking. // Stop speaking.
this.cancelIfSpeaking_(true /* clear the focus ring */); this.cancelIfSpeaking_(true /* clear the focus ring */);
this.recordSelectToSpeakStateChangeEvent_( MetricsUtils.recordSelectToSpeakStateChangeEvent(
StateChangeEvent.CANCEL_SPEECH); MetricsUtils.StateChangeEvent.CANCEL_SPEECH);
break; break;
case SelectToSpeakState.SELECTING: case SelectToSpeakState.SELECTING:
// Cancelled selection. // Cancelled selection.
this.inputHandler_.setTrackingMouse(false); this.inputHandler_.setTrackingMouse(false);
this.onStateChanged_(SelectToSpeakState.INACTIVE); this.onStateChanged_(SelectToSpeakState.INACTIVE);
this.recordSelectToSpeakStateChangeEvent_( MetricsUtils.recordSelectToSpeakStateChangeEvent(
StateChangeEvent.CANCEL_SELECTION); MetricsUtils.StateChangeEvent.CANCEL_SELECTION);
} }
this.onStateChangeRequestedCallbackForTest_ && this.onStateChangeRequestedCallbackForTest_ &&
this.onStateChangeRequestedCallbackForTest_(); this.onStateChangeRequestedCallbackForTest_();
...@@ -769,7 +729,7 @@ SelectToSpeak.prototype = { ...@@ -769,7 +729,7 @@ SelectToSpeak.prototype = {
* as well. * as well.
*/ */
cancelIfSpeaking_: function(clearFocusRing) { cancelIfSpeaking_: function(clearFocusRing) {
chrome.tts.isSpeaking(this.recordCancelIfSpeaking_.bind(this)); chrome.tts.isSpeaking(MetricsUtils.recordCancelIfSpeaking);
if (clearFocusRing) { if (clearFocusRing) {
this.stopAll_(); this.stopAll_();
} else { } else {
...@@ -778,77 +738,6 @@ SelectToSpeak.prototype = { ...@@ -778,77 +738,6 @@ SelectToSpeak.prototype = {
} }
}, },
/**
* Records a cancel event if speech was in progress.
* @param {boolean} speaking Whether speech was in progress
*/
recordCancelIfSpeaking_: function(speaking) {
if (speaking) {
this.recordCancelEvent_();
}
},
/**
* Converts the speech rate into an enum based on
* tools/metrics/histograms/enums.xml.
* These values are persisted to logs. Entries should not be
* renumbered and numeric values should never be reused.
* @return {number} the current speech rate as an int for metrics.
*/
speechRateToSparceHistogramInt_: function() {
return this.speechRate_ * 100;
},
/**
* Converts the speech pitch into an enum based on
* tools/metrics/histograms/enums.xml.
* These values are persisted to logs. Entries should not be
* renumbered and numeric values should never be reused.
* @return {number} the current speech pitch as an int for metrics.
*/
speechPitchToSparceHistogramInt_: function() {
return this.speechPitch_ * 100;
},
/**
* Records an event that Select-to-Speak has begun speaking.
* @param {number} method The CrosSelectToSpeakStartSpeechMethod enum
* that reflects how this event was triggered by the user.
*/
recordStartEvent_: function(method) {
chrome.metricsPrivate.recordUserAction(
'Accessibility.CrosSelectToSpeak.StartSpeech');
chrome.metricsPrivate.recordSparseValue(
'Accessibility.CrosSelectToSpeak.SpeechRate',
this.speechRateToSparceHistogramInt_());
chrome.metricsPrivate.recordSparseValue(
'Accessibility.CrosSelectToSpeak.SpeechPitch',
this.speechPitchToSparceHistogramInt_());
chrome.metricsPrivate.recordBoolean(
'Accessibility.CrosSelectToSpeak.WordHighlighting',
this.wordHighlight_);
chrome.metricsPrivate.recordEnumerationValue(
'Accessibility.CrosSelectToSpeak.StartSpeechMethod', method,
START_SPEECH_METHOD_COUNT);
},
/**
* Records an event that Select-to-Speak speech has been canceled.
*/
recordCancelEvent_: function() {
chrome.metricsPrivate.recordUserAction(
'Accessibility.CrosSelectToSpeak.CancelSpeech');
},
/**
* Records a user-requested state change event from a given state.
* @param {number} changeType
*/
recordSelectToSpeakStateChangeEvent_: function(changeType) {
chrome.metricsPrivate.recordEnumerationValue(
STATE_CHANGE_EVENT_METRIC_NAME, changeType, STATE_CHANGE_EVENT_COUNT);
},
/** /**
* Loads preferences from chrome.storage, sets default values if * Loads preferences from chrome.storage, sets default values if
* necessary, and registers a listener to update prefs when they * necessary, and registers a listener to update prefs when they
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment