OLD | NEW |
| (Empty) |
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 // 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. | |
9 | |
10 #include "chrome/browser/spellchecker/misspelling.h" | |
11 | |
12 #include "base/strings/string_number_conversions.h" | |
13 #include "base/values.h" | |
14 | |
15 namespace { | |
16 | |
17 // Builds a value from a list of spellcheck suggestions. | |
18 std::unique_ptr<base::Value> BuildSuggestionsValue( | |
19 const std::vector<base::string16>& list) { | |
20 std::unique_ptr<base::ListValue> result(new base::ListValue); | |
21 result->AppendStrings(list); | |
22 return std::move(result); | |
23 } | |
24 | |
25 // Builds a value from a spellcheck action. | |
26 std::unique_ptr<base::Value> BuildUserActionValue( | |
27 const SpellcheckAction& action) { | |
28 std::unique_ptr<base::ListValue> result(new base::ListValue); | |
29 result->Append(action.Serialize()); | |
30 return std::move(result); | |
31 } | |
32 | |
33 } // namespace | |
34 | |
35 Misspelling::Misspelling() | |
36 : location(0), length(0), hash(0), timestamp(base::Time::Now()) {} | |
37 | |
38 Misspelling::Misspelling(const base::string16& context, | |
39 size_t location, | |
40 size_t length, | |
41 const std::vector<base::string16>& suggestions, | |
42 uint32_t hash) | |
43 : context(context), | |
44 location(location), | |
45 length(length), | |
46 suggestions(suggestions), | |
47 hash(hash), | |
48 timestamp(base::Time::Now()) {} | |
49 | |
50 Misspelling::Misspelling(const Misspelling& other) = default; | |
51 | |
52 Misspelling::~Misspelling() {} | |
53 | |
54 std::unique_ptr<base::DictionaryValue> SerializeMisspelling( | |
55 const Misspelling& misspelling) { | |
56 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue); | |
57 result->SetString( | |
58 "timestamp", | |
59 base::Int64ToString(static_cast<long>(misspelling.timestamp.ToJsTime()))); | |
60 result->SetInteger("misspelledLength", misspelling.length); | |
61 result->SetInteger("misspelledStart", misspelling.location); | |
62 result->SetString("originalText", misspelling.context); | |
63 result->SetString("suggestionId", base::UintToString(misspelling.hash)); | |
64 result->Set("suggestions", | |
65 BuildSuggestionsValue(misspelling.suggestions).release()); | |
66 result->Set("userActions", | |
67 BuildUserActionValue(misspelling.action).release()); | |
68 return result; | |
69 } | |
70 | |
71 base::string16 GetMisspelledString(const Misspelling& misspelling) { | |
72 // Feedback sender does not create Misspelling objects for spellcheck results | |
73 // that are out-of-bounds of checked text length. | |
74 if (misspelling.location > misspelling.context.length()) | |
75 return base::string16(); | |
76 return misspelling.context.substr(misspelling.location, misspelling.length); | |
77 } | |
78 | |
79 size_t ApproximateSerializedSize(const Misspelling& misspelling) { | |
80 // Estimated by eyeballing JSON overhead. | |
81 const size_t kNumberOfOverheadBytes = 180; | |
82 size_t result = misspelling.context.length() + kNumberOfOverheadBytes; | |
83 for (const base::string16& suggestion : misspelling.suggestions) | |
84 result += suggestion.size(); | |
85 return result; | |
86 } | |
OLD | NEW |