blob: ce4a3a7a59922c184dfe68598dd7cdd388c34834 [file] [log] [blame]
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +01001// Copyright (c) 2013 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.
Ben Murdochbb1529c2013-08-08 10:24:53 +01004//
5// An object to store user feedback to spellcheck suggestions from spelling
6// service.
7//
8// Stores feedback for the spelling service in |Misspelling| objects. Each
9// |Misspelling| object is identified by a |hash| and corresponds to a document
10// marker with the same |hash| identifier in the renderer.
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010011
12#ifndef CHROME_BROWSER_SPELLCHECKER_FEEDBACK_H_
13#define CHROME_BROWSER_SPELLCHECKER_FEEDBACK_H_
14
15#include <map>
16#include <set>
17#include <vector>
18
19#include "chrome/browser/spellchecker/misspelling.h"
20
21namespace spellcheck {
22
Ben Murdochbb1529c2013-08-08 10:24:53 +010023// Stores user feedback to spellcheck suggestions. Sample usage:
24// Feedback feedback;
25// feedback.AddMisspelling(renderer_process_id, Misspelling(
26// ASCIIToUTF16("Helllo world"), 0, 6, std::vector<string16>(),
27// GenerateRandomHash()));
28// feedback.FinalizeRemovedMisspellings(renderer_process_id,
29// std::vector<uint32>());
30// ProcessFeedback(feedback.GetMisspellingsInRenderer(renderer_process_id));
31// feedback.EraseFinalizedMisspellings(renderer_process_id);
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010032class Feedback {
33 public:
34 Feedback();
35 ~Feedback();
36
37 // Returns the misspelling identified by |hash|. Returns NULL if there's no
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010038 // misspelling identified by |hash|. Retains the ownership of the result. The
39 // caller should not modify the hash in the returned misspelling.
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010040 Misspelling* GetMisspelling(uint32 hash);
41
42 // Finalizes the user actions on misspellings that are removed from the
43 // renderer process with ID |renderer_process_id|.
44 void FinalizeRemovedMisspellings(
45 int renderer_process_id,
46 const std::vector<uint32>& remaining_markers);
47
48 // Returns true if the renderer with process ID |renderer_process_id| has
Ben Murdochbb1529c2013-08-08 10:24:53 +010049 // misspellings.
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010050 bool RendererHasMisspellings(int renderer_process_id) const;
51
52 // Returns a copy of the misspellings in renderer with process ID
53 // |renderer_process_id|.
54 std::vector<Misspelling> GetMisspellingsInRenderer(
55 int renderer_process_id) const;
56
57 // Erases the misspellings with final user actions in the renderer with
58 // process ID |renderer_process_id|.
59 void EraseFinalizedMisspellings(int renderer_process_id);
60
Ben Murdochbb1529c2013-08-08 10:24:53 +010061 // Returns true if there's a misspelling with |hash| identifier.
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010062 bool HasMisspelling(uint32 hash) const;
63
64 // Adds the |misspelling| to feedback data. If the |misspelling| has a
65 // duplicate hash, then replaces the existing misspelling with the same hash.
66 void AddMisspelling(int renderer_process_id, const Misspelling& misspelling);
67
Ben Murdochbb1529c2013-08-08 10:24:53 +010068 // Returns true if there're no misspellings.
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010069 bool Empty() const;
70
71 // Returns a list of process identifiers for renderers that have misspellings.
72 std::vector<int> GetRendersWithMisspellings() const;
73
74 // Finalizes all misspellings.
75 void FinalizeAllMisspellings();
76
77 // Returns a copy of all misspellings.
78 std::vector<Misspelling> GetAllMisspellings() const;
79
80 // Removes all misspellings.
81 void Clear();
82
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010083 // Returns a list of all misspelling identifiers for |misspelled_text|.
Ben Murdochbb1529c2013-08-08 10:24:53 +010084 const std::set<uint32>& FindMisspellings(
85 const string16& misspelled_text) const;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010086
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010087 private:
Ben Murdochbb1529c2013-08-08 10:24:53 +010088 typedef std::map<uint32, Misspelling> HashMisspellingMap;
89 typedef std::set<uint32> HashCollection;
90 typedef std::map<int, HashCollection> RendererHashesMap;
91 typedef std::map<string16, HashCollection> TextHashesMap;
92
93 // An empty hash collection to return when FindMisspellings() does not find
94 // misspellings.
95 const HashCollection empty_hash_collection_;
96
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010097 // A map of hashes that identify document markers to feedback data to be sent
98 // to spelling service.
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010099 HashMisspellingMap misspellings_;
100
101 // A map of renderer process ID to hashes that identify misspellings.
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100102 RendererHashesMap renderers_;
103
104 // A map of misspelled text to hashes that identify misspellings.
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100105 TextHashesMap text_;
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100106
107 DISALLOW_COPY_AND_ASSIGN(Feedback);
108};
109
110} // namespace spellcheck
111
112#endif // CHROME_BROWSER_SPELLCHECKER_FEEDBACK_H_