1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_HOST_METRICS_H_
6#define CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_HOST_METRICS_H_
7
8#include <string>
9#include <vector>
10
11#include "base/containers/hash_tables.h"
12#include "base/time/time.h"
13#include "base/timer/timer.h"
14
15// A helper object for recording spell-check related histograms.
16// This class encapsulates histogram names and metrics API.
17// This also carries a set of counters for collecting histograms
18// and a timer for making a periodical summary.
19//
20// We expect a user of SpellCheckHost class to instantiate this object,
21// and pass the metrics object to SpellCheckHost's factory method.
22//
23//   metrics.reset(new SpellCheckHostMetrics());
24//   spell_check_host = SpellChecHost::Create(...., metrics.get());
25//
26// The lifetime of the object should be managed by a caller,
27// and the lifetime should be longer than SpellCheckHost instance
28// because SpellCheckHost will use the object.
29class SpellCheckHostMetrics {
30 public:
31  SpellCheckHostMetrics();
32  ~SpellCheckHostMetrics();
33
34  // Collects the number of words in the custom dictionary, which is
35  // to be uploaded via UMA.
36  static void RecordCustomWordCountStats(size_t count);
37
38  // Collects status of spellchecking enabling state, which is
39  // to be uploaded via UMA
40  void RecordEnabledStats(bool enabled);
41
42  // Collects a histogram for dictionary corruption rate
43  // to be uploaded via UMA
44  void RecordDictionaryCorruptionStats(bool corrupted);
45
46  // Collects status of spellchecking enabling state, which is
47  // to be uploaded via UMA
48  void RecordCheckedWordStats(const base::string16& word, bool misspell);
49
50  // Collects a histogram for misspelled word replacement
51  // to be uploaded via UMA
52  void RecordReplacedWordStats(int delta);
53
54  // Collects a histogram for context menu showing as a spell correction
55  // attempt to be uploaded via UMA
56  void RecordSuggestionStats(int delta);
57
58  // Records if spelling service is enabled or disabled.
59  void RecordSpellingServiceStats(bool enabled);
60
61 private:
62  friend class SpellcheckHostMetricsTest;
63  void OnHistogramTimerExpired();
64
65  // Records various counters without changing their values.
66  void RecordWordCounts();
67
68  // Number of corrected words of checked words.
69  int misspelled_word_count_;
70  int last_misspelled_word_count_;
71
72  // Number of checked words.
73  int spellchecked_word_count_;
74  int last_spellchecked_word_count_;
75
76  // Number of suggestion list showings.
77  int suggestion_show_count_;
78  int last_suggestion_show_count_;
79
80  // Number of misspelled words replaced by a user.
81  int replaced_word_count_;
82  int last_replaced_word_count_;
83
84  // Last recorded number of unique words.
85  int last_unique_word_count_;
86
87  // Time when first spellcheck happened.
88  base::TimeTicks start_time_;
89  // Set of checked words in the hashed form.
90  base::hash_set<std::string> checked_word_hashes_;
91  base::RepeatingTimer<SpellCheckHostMetrics> recording_timer_;
92};
93
94#endif  // CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_HOST_METRICS_H_
95