1/*
2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "Element.h"
29#include "Range.h"
30#include "TextChecking.h"
31#include "Timer.h"
32#include <wtf/Deque.h>
33#include <wtf/Forward.h>
34#include <wtf/RefPtr.h>
35#include <wtf/Noncopyable.h>
36#include <wtf/text/WTFString.h>
37
38namespace WebCore {
39
40class Frame;
41class Node;
42class TextCheckerClient;
43class SpellChecker;
44
45class SpellCheckRequest : public TextCheckingRequest {
46public:
47 static RefPtr<SpellCheckRequest> create(OptionSet<TextCheckingType>, TextCheckingProcessType, Ref<Range>&& checkingRange, Ref<Range>&& automaticReplacementRange, Ref<Range>&& paragraphRange);
48 virtual ~SpellCheckRequest();
49
50 Range& checkingRange() const { return m_checkingRange.get(); }
51 Range& paragraphRange() const { return m_paragraphRange.get(); }
52 Range& automaticReplacementRange() const { return m_automaticReplacementRange.get(); }
53 Element* rootEditableElement() const { return m_rootEditableElement.get(); }
54
55 void setCheckerAndSequence(SpellChecker*, int sequence);
56 void requesterDestroyed();
57 bool isStarted() const { return m_checker; }
58
59 const TextCheckingRequestData& data() const override;
60 void didSucceed(const Vector<TextCheckingResult>&) override;
61 void didCancel() override;
62
63private:
64 SpellCheckRequest(Ref<Range>&& checkingRange, Ref<Range>&& automaticReplacementRange, Ref<Range>&& paragraphRange, const String&, OptionSet<TextCheckingType>, TextCheckingProcessType);
65
66 SpellChecker* m_checker { nullptr };
67 Ref<Range> m_checkingRange;
68 Ref<Range> m_automaticReplacementRange;
69 Ref<Range> m_paragraphRange;
70 RefPtr<Element> m_rootEditableElement;
71 TextCheckingRequestData m_requestData;
72};
73
74class SpellChecker {
75 WTF_MAKE_NONCOPYABLE(SpellChecker); WTF_MAKE_FAST_ALLOCATED;
76public:
77 friend class SpellCheckRequest;
78
79 explicit SpellChecker(Frame&);
80 ~SpellChecker();
81
82 bool isAsynchronousEnabled() const;
83 bool isCheckable(Range&) const;
84
85 void requestCheckingFor(Ref<SpellCheckRequest>&&);
86
87 int lastRequestSequence() const
88 {
89 return m_lastRequestSequence;
90 }
91
92 int lastProcessedSequence() const
93 {
94 return m_lastProcessedSequence;
95 }
96
97private:
98 typedef Deque<Ref<SpellCheckRequest>> RequestQueue;
99
100 bool canCheckAsynchronously(Range&) const;
101 TextCheckerClient* client() const;
102 void timerFiredToProcessQueuedRequest();
103 void invokeRequest(Ref<SpellCheckRequest>&&);
104 void enqueueRequest(Ref<SpellCheckRequest>&&);
105 void didCheckSucceed(int sequence, const Vector<TextCheckingResult>&);
106 void didCheckCancel(int sequence);
107 void didCheck(int sequence, const Vector<TextCheckingResult>&);
108
109 Frame& m_frame;
110 int m_lastRequestSequence;
111 int m_lastProcessedSequence;
112
113 Timer m_timerToProcessQueuedRequest;
114
115 RefPtr<SpellCheckRequest> m_processingRequest;
116 RequestQueue m_requestQueue;
117};
118
119} // namespace WebCore
120