summaryrefslogtreecommitdiffstats
path: root/chrome/browser/spellchecker/spellcheck_custom_dictionary.h
blob: 1c244e7e123c19ec03e8132d200b1502f1a1697a (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
// Copyright (c) 2012 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.

#ifndef CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_CUSTOM_DICTIONARY_H_
#define CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_CUSTOM_DICTIONARY_H_

#include <string>
#include <vector>

#include "base/file_path.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/spellchecker/spellcheck_dictionary.h"
#include "chrome/common/spellcheck_common.h"

// Defines a custom dictionary which users can add their own words to.
class SpellcheckCustomDictionary : public SpellcheckDictionary {
 public:
  class Observer {
   public:
    virtual void OnCustomDictionaryLoaded() = 0;
    virtual void OnCustomDictionaryWordAdded(const std::string& word) = 0;
    virtual void OnCustomDictionaryWordRemoved(const std::string& word) = 0;
  };

  explicit SpellcheckCustomDictionary(Profile* profile);
  virtual ~SpellcheckCustomDictionary();

  // Overridden from SpellcheckDictionary:
  virtual void Load() OVERRIDE;

  const chrome::spellcheck_common::WordList& GetWords() const;

  void LoadDictionaryIntoCustomWordList(
      chrome::spellcheck_common::WordList* custom_words);

  // Moves the words from the |custom_words| argument into its own private
  // member variable. Does not delete the memory at |custom_words|.
  void SetCustomWordList(chrome::spellcheck_common::WordList* custom_words);

  // Adds the given word to the custom words list and inform renderer of the
  // update. Returns false for duplicate words.
  bool AddWord(const std::string& word);

  // Returns false for duplicate words.
  bool CustomWordAddedLocally(const std::string& word);

  void WriteWordToCustomDictionary(const std::string& word);

  // Removes the given word from the custom words list and inform renderer of
  // the update. Returns false for words that are not in the dictionary.
  bool RemoveWord(const std::string& word);

  // Returns false for words that are not in the dictionary.
  bool CustomWordRemovedLocally(const std::string& word);

  void EraseWordFromCustomDictionary(const std::string& word);

  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

 private:
  // Returns a newly allocated list of words read from custom dictionary file.
  // The caller owns this new list.
  chrome::spellcheck_common::WordList* LoadDictionary();

  // The reply point for PostTaskAndReplyWithResult. Called when LoadDictionary
  // is finished reading words from custom dictionary file. Moves the strings
  // from the |custom_words| argument into the private member variable |words_|
  // and deletes the memory at |custom_words|.
  void SetCustomWordListAndDelete(
      chrome::spellcheck_common::WordList* custom_words);

  // In-memory cache of the custom words file.
  chrome::spellcheck_common::WordList words_;

  // A path for custom dictionary per profile.
  FilePath custom_dictionary_path_;

  base::WeakPtrFactory<SpellcheckCustomDictionary> weak_ptr_factory_;

  std::vector<Observer*> observers_;

  DISALLOW_COPY_AND_ASSIGN(SpellcheckCustomDictionary);
};

#endif  // CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_CUSTOM_DICTIONARY_H_