Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / spellchecker / misspelling.cc
blob0fd764afd230efd4856fe9caa975fb7b2426dc42
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.
10 #include "chrome/browser/spellchecker/misspelling.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/values.h"
15 namespace {
17 // Builds a value from a list of spellcheck suggestions. The caller owns the
18 // result.
19 base::Value* BuildSuggestionsValue(const std::vector<base::string16>& list) {
20 base::ListValue* result = new base::ListValue;
21 result->AppendStrings(list);
22 return result;
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());
29 return result;
32 } // namespace
34 Misspelling::Misspelling()
35 : location(0), length(0), hash(0), timestamp(base::Time::Now()) {}
37 Misspelling::Misspelling(const base::string16& context,
38 size_t location,
39 size_t length,
40 const std::vector<base::string16>& suggestions,
41 uint32 hash)
42 : context(context),
43 location(location),
44 length(length),
45 suggestions(suggestions),
46 hash(hash),
47 timestamp(base::Time::Now()) {}
49 Misspelling::~Misspelling() {}
51 base::DictionaryValue* Misspelling::Serialize() const {
52 base::DictionaryValue* result = new base::DictionaryValue;
53 result->SetString("timestamp", base::Int64ToString(
54 static_cast<long>(timestamp.ToJsTime())));
55 result->SetInteger("misspelledLength", length);
56 result->SetInteger("misspelledStart", location);
57 result->SetString("originalText", context);
58 result->SetString("suggestionId", base::UintToString(hash));
59 result->Set("suggestions", BuildSuggestionsValue(suggestions));
60 result->Set("userActions", BuildUserActionValue(action));
61 return result;
64 base::string16 Misspelling::GetMisspelledString() const {
65 // Feedback sender does not create Misspelling objects for spellcheck results
66 // that are out-of-bounds of checked text length.
67 if (location > context.length())
68 return base::string16();
69 return context.substr(location, length);