Home | History | Annotate | Download | only in spellchecker
      1 // 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.
      4 //
      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.
     11 
     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 
     21 namespace spellcheck {
     22 
     23 // Stores user feedback to spellcheck suggestions. Sample usage:
     24 //    Feedback feedback;
     25 //    feedback.AddMisspelling(renderer_process_id, Misspelling(
     26 //        base::ASCIIToUTF16("Helllo world"), 0, 6,
     27 //        std::vector<base::string16>(), GenerateRandomHash()));
     28 //    feedback.FinalizeRemovedMisspellings(renderer_process_id,
     29 //                                         std::vector<uint32>());
     30 //    ProcessFeedback(feedback.GetMisspellingsInRenderer(renderer_process_id));
     31 //    feedback.EraseFinalizedMisspellings(renderer_process_id);
     32 class Feedback {
     33  public:
     34   Feedback();
     35   ~Feedback();
     36 
     37   // Returns the misspelling identified by |hash|. Returns NULL if there's no
     38   // misspelling identified by |hash|. Retains the ownership of the result. The
     39   // caller should not modify the hash in the returned misspelling.
     40   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
     49   // misspellings.
     50   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 
     61   // Returns true if there's a misspelling with |hash| identifier.
     62   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 
     68   // Returns true if there're no misspellings.
     69   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 
     83   // Returns a list of all misspelling identifiers for |misspelled_text|.
     84   const std::set<uint32>& FindMisspellings(
     85       const base::string16& misspelled_text) const;
     86 
     87  private:
     88   typedef std::map<uint32, Misspelling> HashMisspellingMap;
     89   typedef std::set<uint32> HashCollection;
     90   typedef std::map<int, HashCollection> RendererHashesMap;
     91   typedef std::map<base::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 
     97   // A map of hashes that identify document markers to feedback data to be sent
     98   // to spelling service.
     99   HashMisspellingMap misspellings_;
    100 
    101   // A map of renderer process ID to hashes that identify misspellings.
    102   RendererHashesMap renderers_;
    103 
    104   // A map of misspelled text to hashes that identify misspellings.
    105   TextHashesMap text_;
    106 
    107   DISALLOW_COPY_AND_ASSIGN(Feedback);
    108 };
    109 
    110 }  // namespace spellcheck
    111 
    112 #endif  // CHROME_BROWSER_SPELLCHECKER_FEEDBACK_H_
    113