blob: 89bcca47ed2b22984bf89e3bddcb22f198b03e3c [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// The |Misspelling| object stores the misspelling, a spellcheck suggestion for
6// it, and user's action on it. The misspelling is stored as |context|,
7// |location|, and |length| instead of only misspelled text, because the
8// spellcheck algorithm uses the context.
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +01009
10#include "chrome/browser/spellchecker/misspelling.h"
11
12#include "base/strings/string_number_conversions.h"
13#include "base/values.h"
14
15namespace {
16
17// Builds a value from a list of spellcheck suggestions. The caller owns the
18// result.
19base::Value* BuildSuggestionsValue(const std::vector<string16>& list) {
20 base::ListValue* result = new base::ListValue;
21 result->AppendStrings(list);
22 return result;
23}
24
25// Builds a value from a spellcheck action. The caller owns the result.
26base::Value* BuildUserActionValue(const SpellcheckAction& action) {
27 base::ListValue* result = new base::ListValue;
28 result->Append(action.Serialize());
29 return result;
30}
31
32} // namespace
33
34Misspelling::Misspelling()
35 : location(0), length(0), hash(0), timestamp(base::Time::Now()) {
36}
37
38Misspelling::Misspelling(const string16& context,
39 size_t location,
40 size_t length,
41 const std::vector<string16>& suggestions,
42 uint32 hash)
43 : context(context),
44 location(location),
45 length(length),
46 suggestions(suggestions),
47 hash(hash),
48 timestamp(base::Time::Now()) {
49}
50
51Misspelling::~Misspelling() {
52}
53
54base::DictionaryValue* Misspelling::Serialize() const {
55 base::DictionaryValue* result = new base::DictionaryValue;
56 result->SetString(
57 "timestamp",
58 base::Int64ToString(static_cast<long>(timestamp.ToJsTime())));
59 result->SetInteger("misspelledLength", length);
60 result->SetInteger("misspelledStart", location);
61 result->SetString("originalText", context);
62 result->SetString("suggestionId", base::UintToString(hash));
63 result->Set("suggestions", BuildSuggestionsValue(suggestions));
64 result->Set("userActions", BuildUserActionValue(action));
65 return result;
66}
Ben Murdochbb1529c2013-08-08 10:24:53 +010067
68string16 Misspelling::GetMisspelledString() const {
69 // Feedback sender does not create Misspelling objects for spellcheck results
70 // that are out-of-bounds of checked text length.
71 if (location > context.length())
72 return string16();
73 return context.substr(location, length);
74}