summaryrefslogtreecommitdiffstats
path: root/chrome/browser/spellchecker/feedback.h
blob: ce4a3a7a59922c184dfe68598dd7cdd388c34834 (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
// Copyright (c) 2013 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.
//
// An object to store user feedback to spellcheck suggestions from spelling
// service.
//
// Stores feedback for the spelling service in |Misspelling| objects. Each
// |Misspelling| object is identified by a |hash| and corresponds to a document
// marker with the same |hash| identifier in the renderer.

#ifndef CHROME_BROWSER_SPELLCHECKER_FEEDBACK_H_
#define CHROME_BROWSER_SPELLCHECKER_FEEDBACK_H_

#include <map>
#include <set>
#include <vector>

#include "chrome/browser/spellchecker/misspelling.h"

namespace spellcheck {

// Stores user feedback to spellcheck suggestions. Sample usage:
//    Feedback feedback;
//    feedback.AddMisspelling(renderer_process_id, Misspelling(
//        ASCIIToUTF16("Helllo world"), 0, 6, std::vector<string16>(),
//        GenerateRandomHash()));
//    feedback.FinalizeRemovedMisspellings(renderer_process_id,
//                                         std::vector<uint32>());
//    ProcessFeedback(feedback.GetMisspellingsInRenderer(renderer_process_id));
//    feedback.EraseFinalizedMisspellings(renderer_process_id);
class Feedback {
 public:
  Feedback();
  ~Feedback();

  // Returns the misspelling identified by |hash|. Returns NULL if there's no
  // misspelling identified by |hash|. Retains the ownership of the result. The
  // caller should not modify the hash in the returned misspelling.
  Misspelling* GetMisspelling(uint32 hash);

  // Finalizes the user actions on misspellings that are removed from the
  // renderer process with ID |renderer_process_id|.
  void FinalizeRemovedMisspellings(
      int renderer_process_id,
      const std::vector<uint32>& remaining_markers);

  // Returns true if the renderer with process ID |renderer_process_id| has
  // misspellings.
  bool RendererHasMisspellings(int renderer_process_id) const;

  // Returns a copy of the misspellings in renderer with process ID
  // |renderer_process_id|.
  std::vector<Misspelling> GetMisspellingsInRenderer(
      int renderer_process_id) const;

  // Erases the misspellings with final user actions in the renderer with
  // process ID |renderer_process_id|.
  void EraseFinalizedMisspellings(int renderer_process_id);

  // Returns true if there's a misspelling with |hash| identifier.
  bool HasMisspelling(uint32 hash) const;

  // Adds the |misspelling| to feedback data. If the |misspelling| has a
  // duplicate hash, then replaces the existing misspelling with the same hash.
  void AddMisspelling(int renderer_process_id, const Misspelling& misspelling);

  // Returns true if there're no misspellings.
  bool Empty() const;

  // Returns a list of process identifiers for renderers that have misspellings.
  std::vector<int> GetRendersWithMisspellings() const;

  // Finalizes all misspellings.
  void FinalizeAllMisspellings();

  // Returns a copy of all misspellings.
  std::vector<Misspelling> GetAllMisspellings() const;

  // Removes all misspellings.
  void Clear();

  // Returns a list of all misspelling identifiers for |misspelled_text|.
  const std::set<uint32>& FindMisspellings(
      const string16& misspelled_text) const;

 private:
  typedef std::map<uint32, Misspelling> HashMisspellingMap;
  typedef std::set<uint32> HashCollection;
  typedef std::map<int, HashCollection> RendererHashesMap;
  typedef std::map<string16, HashCollection> TextHashesMap;

  // An empty hash collection to return when FindMisspellings() does not find
  // misspellings.
  const HashCollection empty_hash_collection_;

  // A map of hashes that identify document markers to feedback data to be sent
  // to spelling service.
  HashMisspellingMap misspellings_;

  // A map of renderer process ID to hashes that identify misspellings.
  RendererHashesMap renderers_;

  // A map of misspelled text to hashes that identify misspellings.
  TextHashesMap text_;

  DISALLOW_COPY_AND_ASSIGN(Feedback);
};

}  // namespace spellcheck

#endif  // CHROME_BROWSER_SPELLCHECKER_FEEDBACK_H_