02ca36735899770453a7f14f323eec16101d51ad
[WebKit-https.git] / Source / WebKit / Shared / EditorState.cpp
1 /*
2  * Copyright (C) 2010, 2011 Apple 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. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "EditorState.h"
28
29 #include "WebCoreArgumentCoders.h"
30 #include <wtf/text/TextStream.h>
31
32 using namespace WebCore;
33
34 namespace WebKit {
35
36 void EditorState::encode(IPC::Encoder& encoder) const
37 {
38     encoder << shouldIgnoreSelectionChanges;
39     encoder << selectionIsNone;
40     encoder << selectionIsRange;
41     encoder << isContentEditable;
42     encoder << isContentRichlyEditable;
43     encoder << isInPasswordField;
44     encoder << isInPlugin;
45     encoder << hasComposition;
46     encoder << isMissingPostLayoutData;
47
48     if (!isMissingPostLayoutData)
49         m_postLayoutData.encode(encoder);
50
51 #if PLATFORM(IOS)
52     encoder << firstMarkedRect;
53     encoder << lastMarkedRect;
54     encoder << markedText;
55 #endif
56 }
57
58 bool EditorState::decode(IPC::Decoder& decoder, EditorState& result)
59 {
60     if (!decoder.decode(result.shouldIgnoreSelectionChanges))
61         return false;
62
63     if (!decoder.decode(result.selectionIsNone))
64         return false;
65
66     if (!decoder.decode(result.selectionIsRange))
67         return false;
68
69     if (!decoder.decode(result.isContentEditable))
70         return false;
71
72     if (!decoder.decode(result.isContentRichlyEditable))
73         return false;
74
75     if (!decoder.decode(result.isInPasswordField))
76         return false;
77
78     if (!decoder.decode(result.isInPlugin))
79         return false;
80
81     if (!decoder.decode(result.hasComposition))
82         return false;
83
84     if (!decoder.decode(result.isMissingPostLayoutData))
85         return false;
86
87     if (!result.isMissingPostLayoutData) {
88         if (!PostLayoutData::decode(decoder, result.postLayoutData()))
89             return false;
90     }
91
92 #if PLATFORM(IOS)
93     if (!decoder.decode(result.firstMarkedRect))
94         return false;
95     if (!decoder.decode(result.lastMarkedRect))
96         return false;
97     if (!decoder.decode(result.markedText))
98         return false;
99 #endif
100
101     return true;
102 }
103
104 void EditorState::PostLayoutData::encode(IPC::Encoder& encoder) const
105 {
106     encoder << typingAttributes;
107 #if PLATFORM(IOS) || PLATFORM(GTK)
108     encoder << caretRectAtStart;
109 #endif
110 #if PLATFORM(IOS) || PLATFORM(MAC)
111     encoder << selectionClipRect;
112     encoder << selectedTextLength;
113     encoder << textAlignment;
114     encoder << textColor;
115     encoder << enclosingListType;
116 #endif
117 #if PLATFORM(IOS)
118     encoder << caretRectAtEnd;
119     encoder << selectionRects;
120     encoder << wordAtSelection;
121     encoder << characterAfterSelection;
122     encoder << characterBeforeSelection;
123     encoder << twoCharacterBeforeSelection;
124     encoder << isReplaceAllowed;
125     encoder << hasContent;
126     encoder << isStableStateUpdate;
127     encoder << insideFixedPosition;
128     encoder << hasPlainText;
129     encoder << caretColor;
130 #endif
131 #if PLATFORM(MAC)
132     encoder << candidateRequestStartPosition;
133     encoder << paragraphContextForCandidateRequest;
134     encoder << stringForCandidateRequest;
135 #endif
136     encoder << canCut;
137     encoder << canCopy;
138     encoder << canPaste;
139 }
140
141 bool EditorState::PostLayoutData::decode(IPC::Decoder& decoder, PostLayoutData& result)
142 {
143     if (!decoder.decode(result.typingAttributes))
144         return false;
145 #if PLATFORM(IOS) || PLATFORM(GTK)
146     if (!decoder.decode(result.caretRectAtStart))
147         return false;
148 #endif
149 #if PLATFORM(IOS) || PLATFORM(MAC)
150     if (!decoder.decode(result.selectionClipRect))
151         return false;
152     if (!decoder.decode(result.selectedTextLength))
153         return false;
154     if (!decoder.decode(result.textAlignment))
155         return false;
156     if (!decoder.decode(result.textColor))
157         return false;
158     if (!decoder.decode(result.enclosingListType))
159         return false;
160 #endif
161 #if PLATFORM(IOS)
162     if (!decoder.decode(result.caretRectAtEnd))
163         return false;
164     if (!decoder.decode(result.selectionRects))
165         return false;
166     if (!decoder.decode(result.wordAtSelection))
167         return false;
168     if (!decoder.decode(result.characterAfterSelection))
169         return false;
170     if (!decoder.decode(result.characterBeforeSelection))
171         return false;
172     if (!decoder.decode(result.twoCharacterBeforeSelection))
173         return false;
174     if (!decoder.decode(result.isReplaceAllowed))
175         return false;
176     if (!decoder.decode(result.hasContent))
177         return false;
178     if (!decoder.decode(result.isStableStateUpdate))
179         return false;
180     if (!decoder.decode(result.insideFixedPosition))
181         return false;
182     if (!decoder.decode(result.hasPlainText))
183         return false;
184     if (!decoder.decode(result.caretColor))
185         return false;
186 #endif
187 #if PLATFORM(MAC)
188     if (!decoder.decode(result.candidateRequestStartPosition))
189         return false;
190
191     if (!decoder.decode(result.paragraphContextForCandidateRequest))
192         return false;
193
194     if (!decoder.decode(result.stringForCandidateRequest))
195         return false;
196 #endif
197
198     if (!decoder.decode(result.canCut))
199         return false;
200     if (!decoder.decode(result.canCopy))
201         return false;
202     if (!decoder.decode(result.canPaste))
203         return false;
204
205     return true;
206 }
207
208 TextStream& operator<<(TextStream& ts, const EditorState& editorState)
209 {
210 #if PLATFORM(IOS)
211     if (editorState.firstMarkedRect != IntRect())
212         ts.dumpProperty("firstMarkedRect", editorState.firstMarkedRect);
213     if (editorState.lastMarkedRect != IntRect())
214         ts.dumpProperty("lastMarkedRect", editorState.lastMarkedRect);
215     if (editorState.markedText.length())
216         ts.dumpProperty("markedText", editorState.markedText);
217 #endif
218
219     if (editorState.shouldIgnoreSelectionChanges)
220         ts.dumpProperty("shouldIgnoreSelectionChanges", editorState.shouldIgnoreSelectionChanges);
221     if (!editorState.selectionIsNone)
222         ts.dumpProperty("selectionIsNone", editorState.selectionIsNone);
223     if (editorState.selectionIsRange)
224         ts.dumpProperty("selectionIsRange", editorState.selectionIsRange);
225     if (editorState.isContentEditable)
226         ts.dumpProperty("isContentEditable", editorState.isContentEditable);
227     if (editorState.isContentRichlyEditable)
228         ts.dumpProperty("isContentRichlyEditable", editorState.isContentRichlyEditable);
229     if (editorState.isInPasswordField)
230         ts.dumpProperty("isInPasswordField", editorState.isInPasswordField);
231     if (editorState.isInPlugin)
232         ts.dumpProperty("isInPlugin", editorState.isInPlugin);
233     if (editorState.hasComposition)
234         ts.dumpProperty("hasComposition", editorState.hasComposition);
235     if (editorState.isMissingPostLayoutData)
236         ts.dumpProperty("isMissingPostLayoutData", editorState.isMissingPostLayoutData);
237
238     if (editorState.isMissingPostLayoutData)
239         return ts;
240
241     TextStream::GroupScope scope(ts);
242     ts << "postLayoutData";
243     if (editorState.postLayoutData().typingAttributes != AttributeNone)
244         ts.dumpProperty("typingAttributes", editorState.postLayoutData().typingAttributes);
245 #if PLATFORM(IOS) || PLATFORM(GTK)
246     if (editorState.postLayoutData().caretRectAtStart != IntRect())
247         ts.dumpProperty("caretRectAtStart", editorState.postLayoutData().caretRectAtStart);
248 #endif
249 #if PLATFORM(IOS) || PLATFORM(MAC)
250     if (editorState.postLayoutData().selectionClipRect != IntRect())
251         ts.dumpProperty("selectionClipRect", editorState.postLayoutData().selectionClipRect);
252     if (editorState.postLayoutData().selectedTextLength)
253         ts.dumpProperty("selectedTextLength", editorState.postLayoutData().selectedTextLength);
254     if (editorState.postLayoutData().textAlignment != NoAlignment)
255         ts.dumpProperty("textAlignment", editorState.postLayoutData().textAlignment);
256     if (editorState.postLayoutData().textColor.isValid())
257         ts.dumpProperty("textColor", editorState.postLayoutData().textColor);
258     if (editorState.postLayoutData().enclosingListType != NoList)
259         ts.dumpProperty("enclosingListType", editorState.postLayoutData().enclosingListType);
260 #endif
261 #if PLATFORM(IOS)
262     if (editorState.postLayoutData().caretRectAtEnd != IntRect())
263         ts.dumpProperty("caretRectAtEnd", editorState.postLayoutData().caretRectAtEnd);
264     if (editorState.postLayoutData().selectionRects.size())
265         ts.dumpProperty("selectionRects", editorState.postLayoutData().selectionRects);
266     if (editorState.postLayoutData().wordAtSelection.length())
267         ts.dumpProperty("wordAtSelection", editorState.postLayoutData().wordAtSelection);
268     if (editorState.postLayoutData().characterAfterSelection)
269         ts.dumpProperty("characterAfterSelection", editorState.postLayoutData().characterAfterSelection);
270     if (editorState.postLayoutData().characterBeforeSelection)
271         ts.dumpProperty("characterBeforeSelection", editorState.postLayoutData().characterBeforeSelection);
272     if (editorState.postLayoutData().twoCharacterBeforeSelection)
273         ts.dumpProperty("twoCharacterBeforeSelection", editorState.postLayoutData().twoCharacterBeforeSelection);
274
275     if (editorState.postLayoutData().isReplaceAllowed)
276         ts.dumpProperty("isReplaceAllowed", editorState.postLayoutData().isReplaceAllowed);
277     if (editorState.postLayoutData().hasContent)
278         ts.dumpProperty("hasContent", editorState.postLayoutData().hasContent);
279     ts.dumpProperty("isStableStateUpdate", editorState.postLayoutData().isStableStateUpdate);
280     if (editorState.postLayoutData().insideFixedPosition)
281         ts.dumpProperty("insideFixedPosition", editorState.postLayoutData().insideFixedPosition);
282     if (editorState.postLayoutData().caretColor.isValid())
283         ts.dumpProperty("caretColor", editorState.postLayoutData().caretColor);
284 #endif
285 #if PLATFORM(MAC)
286     if (editorState.postLayoutData().candidateRequestStartPosition)
287         ts.dumpProperty("candidateRequestStartPosition", editorState.postLayoutData().candidateRequestStartPosition);
288     if (editorState.postLayoutData().paragraphContextForCandidateRequest.length())
289         ts.dumpProperty("paragraphContextForCandidateRequest", editorState.postLayoutData().paragraphContextForCandidateRequest);
290     if (editorState.postLayoutData().stringForCandidateRequest.length())
291         ts.dumpProperty("stringForCandidateRequest", editorState.postLayoutData().stringForCandidateRequest);
292 #endif
293
294     if (editorState.postLayoutData().canCut)
295         ts.dumpProperty("canCut", editorState.postLayoutData().canCut);
296     if (editorState.postLayoutData().canCopy)
297         ts.dumpProperty("canCopy", editorState.postLayoutData().canCopy);
298     if (editorState.postLayoutData().canPaste)
299         ts.dumpProperty("canPaste", editorState.postLayoutData().canPaste);
300
301     return ts;
302 }
303
304 } // namespace WebKit