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.
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.
10 #include "chrome/browser/spellchecker/misspelling.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/values.h"
17 // Builds a value from a list of spellcheck suggestions. The caller owns the
19 base::Value
* BuildSuggestionsValue(const std::vector
<base::string16
>& list
) {
20 base::ListValue
* result
= new base::ListValue
;
21 result
->AppendStrings(list
);
25 // Builds a value from a spellcheck action. The caller owns the result.
26 base::Value
* BuildUserActionValue(const SpellcheckAction
& action
) {
27 base::ListValue
* result
= new base::ListValue
;
28 result
->Append(action
.Serialize());
34 Misspelling::Misspelling()
35 : location(0), length(0), hash(0), timestamp(base::Time::Now()) {}
37 Misspelling::Misspelling(const base::string16
& context
,
40 const std::vector
<base::string16
>& suggestions
,
45 suggestions(suggestions
),
47 timestamp(base::Time::Now()) {}
49 Misspelling::~Misspelling() {}
51 base::DictionaryValue
* SerializeMisspelling(const Misspelling
& misspelling
) {
52 base::DictionaryValue
* result
= new base::DictionaryValue
;
55 base::Int64ToString(static_cast<long>(misspelling
.timestamp
.ToJsTime())));
56 result
->SetInteger("misspelledLength", misspelling
.length
);
57 result
->SetInteger("misspelledStart", misspelling
.location
);
58 result
->SetString("originalText", misspelling
.context
);
59 result
->SetString("suggestionId", base::UintToString(misspelling
.hash
));
60 result
->Set("suggestions", BuildSuggestionsValue(misspelling
.suggestions
));
61 result
->Set("userActions", BuildUserActionValue(misspelling
.action
));
65 base::string16
GetMisspelledString(const Misspelling
& misspelling
) {
66 // Feedback sender does not create Misspelling objects for spellcheck results
67 // that are out-of-bounds of checked text length.
68 if (misspelling
.location
> misspelling
.context
.length())
69 return base::string16();
70 return misspelling
.context
.substr(misspelling
.location
, misspelling
.length
);