190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// found in the LICENSE file.
4bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch//
5bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch// An object to store user feedback to spellcheck suggestions from spelling
6bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch// service.
7bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch//
8bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch// Stores feedback for the spelling service in |Misspelling| objects. Each
9bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch// |Misspelling| object is identified by a |hash| and corresponds to a document
10bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch// marker with the same |hash| identifier in the renderer.
1190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#ifndef CHROME_BROWSER_SPELLCHECKER_FEEDBACK_H_
1390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#define CHROME_BROWSER_SPELLCHECKER_FEEDBACK_H_
1490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include <map>
1690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include <set>
1790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include <vector>
1890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "chrome/browser/spellchecker/misspelling.h"
2090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace spellcheck {
2290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
23bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch// Stores user feedback to spellcheck suggestions. Sample usage:
24bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch//    Feedback feedback;
25bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch//    feedback.AddMisspelling(renderer_process_id, Misspelling(
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//        base::ASCIIToUTF16("Helllo world"), 0, 6,
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//        std::vector<base::string16>(), GenerateRandomHash()));
28bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch//    feedback.FinalizeRemovedMisspellings(renderer_process_id,
29bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch//                                         std::vector<uint32>());
30bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch//    ProcessFeedback(feedback.GetMisspellingsInRenderer(renderer_process_id));
31bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch//    feedback.EraseFinalizedMisspellings(renderer_process_id);
3290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class Feedback {
3390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) public:
3490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  Feedback();
3590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  ~Feedback();
3690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Returns the misspelling identified by |hash|. Returns NULL if there's no
38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // misspelling identified by |hash|. Retains the ownership of the result. The
39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // caller should not modify the hash in the returned misspelling.
4090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  Misspelling* GetMisspelling(uint32 hash);
4190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
4290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Finalizes the user actions on misspellings that are removed from the
4390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // renderer process with ID |renderer_process_id|.
4490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  void FinalizeRemovedMisspellings(
4590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      int renderer_process_id,
4690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      const std::vector<uint32>& remaining_markers);
4790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
4890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Returns true if the renderer with process ID |renderer_process_id| has
49bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  // misspellings.
5090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  bool RendererHasMisspellings(int renderer_process_id) const;
5190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
5290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Returns a copy of the misspellings in renderer with process ID
5390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // |renderer_process_id|.
5490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  std::vector<Misspelling> GetMisspellingsInRenderer(
5590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      int renderer_process_id) const;
5690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
5790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Erases the misspellings with final user actions in the renderer with
5890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // process ID |renderer_process_id|.
5990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  void EraseFinalizedMisspellings(int renderer_process_id);
6090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
61bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  // Returns true if there's a misspelling with |hash| identifier.
6290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  bool HasMisspelling(uint32 hash) const;
6390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
6490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Adds the |misspelling| to feedback data. If the |misspelling| has a
6590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // duplicate hash, then replaces the existing misspelling with the same hash.
6690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  void AddMisspelling(int renderer_process_id, const Misspelling& misspelling);
6790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
68bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  // Returns true if there're no misspellings.
6990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  bool Empty() const;
7090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
7190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Returns a list of process identifiers for renderers that have misspellings.
7290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  std::vector<int> GetRendersWithMisspellings() const;
7390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
7490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Finalizes all misspellings.
7590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  void FinalizeAllMisspellings();
7690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
7790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Returns a copy of all misspellings.
7890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  std::vector<Misspelling> GetAllMisspellings() const;
7990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
8090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Removes all misspellings.
8190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  void Clear();
8290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
83868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Returns a list of all misspelling identifiers for |misspelled_text|.
84bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  const std::set<uint32>& FindMisspellings(
85a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      const base::string16& misspelled_text) const;
86868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
8790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) private:
88bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  typedef std::map<uint32, Misspelling> HashMisspellingMap;
89bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  typedef std::set<uint32> HashCollection;
90bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  typedef std::map<int, HashCollection> RendererHashesMap;
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  typedef std::map<base::string16, HashCollection> TextHashesMap;
92bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch
93bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  // An empty hash collection to return when FindMisspellings() does not find
94bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  // misspellings.
95bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  const HashCollection empty_hash_collection_;
96bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch
9790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // A map of hashes that identify document markers to feedback data to be sent
9890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // to spelling service.
9990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  HashMisspellingMap misspellings_;
10090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
10190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // A map of renderer process ID to hashes that identify misspellings.
102868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  RendererHashesMap renderers_;
103868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
104868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // A map of misspelled text to hashes that identify misspellings.
105868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  TextHashesMap text_;
10690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
10790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(Feedback);
10890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)};
10990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
11090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}  // namespace spellcheck
11190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
11290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif  // CHROME_BROWSER_SPELLCHECKER_FEEDBACK_H_
113