1 // Copyright 2017 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 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_EDITING_MARKERS_SUGGESTION_MARKER_H_
6 #define THIRD_PARTY_BLINK_RENDERER_CORE_EDITING_MARKERS_SUGGESTION_MARKER_H_
7 
8 #include "third_party/blink/renderer/core/editing/markers/styleable_marker.h"
9 #include "third_party/blink/renderer/platform/wtf/casting.h"
10 
11 namespace blink {
12 
13 class SuggestionMarkerProperties;
14 
15 // A subclass of StyleableMarker used to store information specific to
16 // suggestion markers (used to represent Android SuggestionSpans or ChromeOS's
17 // custom text spans). In addition
18 // to the formatting information StyleableMarker holds, we also store a list of
19 // suggested replacements for the marked region of text. In addition, each
20 // SuggestionMarker is tagged with an integer so browser code can identify which
21 // SuggestionMarker a suggestion replace operation pertains to.
22 class CORE_EXPORT SuggestionMarker final : public StyleableMarker {
23  public:
24   enum class SuggestionType { kMisspelling, kNotMisspelling, kAutocorrect };
25   enum class RemoveOnFinishComposing { kRemove, kDoNotRemove };
26 
27   SuggestionMarker(unsigned start_offset,
28                    unsigned end_offset,
29                    const SuggestionMarkerProperties&);
30 
31   // DocumentMarker implementations
32   MarkerType GetType() const final;
33 
34   // SuggestionMarker-specific
35   int32_t Tag() const;
36   SuggestionType GetSuggestionType() const;
37   const Vector<String>& Suggestions() const;
38   bool IsMisspelling() const;
39   bool NeedsRemovalOnFinishComposing() const;
40   Color SuggestionHighlightColor() const;
41 
42   // Replace the suggestion at suggestion_index with new_suggestion.
43   void SetSuggestion(unsigned suggestion_index, const String& new_suggestion);
44 
45  private:
46   static int32_t NextTag();
47 
48   static int32_t current_tag_;
49 
50   // We use a signed int for the tag since it's passed to Java (as an opaque
51   // identifier), and Java does not support unsigned ints.
52   const int32_t tag_;
53   Vector<String> suggestions_;
54   const SuggestionType suggestion_type_;
55   const RemoveOnFinishComposing remove_on_finish_composing_;
56   const Color suggestion_highlight_color_;
57 
58   DISALLOW_COPY_AND_ASSIGN(SuggestionMarker);
59 };
60 
61 template <>
62 struct DowncastTraits<SuggestionMarker> {
63   static bool AllowFrom(const DocumentMarker& marker) {
64     return marker.GetType() == DocumentMarker::kSuggestion;
65   }
66 };
67 
68 }  // namespace blink
69 
70 #endif
71