896c03bede1a2c39d9c532814779eadb06cf7add
[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 namespace WebKit {
33 using namespace WebCore;
34
35 void EditorState::encode(IPC::Encoder& encoder) const
36 {
37     encoder << shouldIgnoreSelectionChanges;
38     encoder << selectionIsNone;
39     encoder << selectionIsRange;
40     encoder << isContentEditable;
41     encoder << isContentRichlyEditable;
42     encoder << isInPasswordField;
43     encoder << isInPlugin;
44     encoder << hasComposition;
45     encoder << isMissingPostLayoutData;
46
47     if (!isMissingPostLayoutData)
48         m_postLayoutData.encode(encoder);
49
50 #if PLATFORM(IOS_FAMILY)
51     encoder << firstMarkedRect;
52     encoder << lastMarkedRect;
53     encoder << markedText;
54 #endif
55
56     encoder << originIdentifierForPasteboard;
57 }
58
59 bool EditorState::decode(IPC::Decoder& decoder, EditorState& result)
60 {
61     if (!decoder.decode(result.shouldIgnoreSelectionChanges))
62         return false;
63
64     if (!decoder.decode(result.selectionIsNone))
65         return false;
66
67     if (!decoder.decode(result.selectionIsRange))
68         return false;
69
70     if (!decoder.decode(result.isContentEditable))
71         return false;
72
73     if (!decoder.decode(result.isContentRichlyEditable))
74         return false;
75
76     if (!decoder.decode(result.isInPasswordField))
77         return false;
78
79     if (!decoder.decode(result.isInPlugin))
80         return false;
81
82     if (!decoder.decode(result.hasComposition))
83         return false;
84
85     if (!decoder.decode(result.isMissingPostLayoutData))
86         return false;
87
88     if (!result.isMissingPostLayoutData) {
89         if (!PostLayoutData::decode(decoder, result.postLayoutData()))
90             return false;
91     }
92
93 #if PLATFORM(IOS_FAMILY)
94     if (!decoder.decode(result.firstMarkedRect))
95         return false;
96     if (!decoder.decode(result.lastMarkedRect))
97         return false;
98     if (!decoder.decode(result.markedText))
99         return false;
100 #endif
101
102     if (!decoder.decode(result.originIdentifierForPasteboard))
103         return false;
104
105     return true;
106 }
107
108 void EditorState::PostLayoutData::encode(IPC::Encoder& encoder) const
109 {
110     encoder << typingAttributes;
111 #if PLATFORM(IOS_FAMILY) || PLATFORM(GTK)
112     encoder << caretRectAtStart;
113 #endif
114 #if PLATFORM(IOS_FAMILY) || PLATFORM(MAC)
115     encoder << focusedElementRect;
116     encoder << selectedTextLength;
117     encoder << textAlignment;
118     encoder << textColor;
119     encoder << enclosingListType;
120 #endif
121 #if PLATFORM(IOS_FAMILY)
122     encoder << caretRectAtEnd;
123     encoder << selectionRects;
124     encoder << wordAtSelection;
125     encoder << characterAfterSelection;
126     encoder << characterBeforeSelection;
127     encoder << twoCharacterBeforeSelection;
128     encoder << isReplaceAllowed;
129     encoder << hasContent;
130     encoder << isStableStateUpdate;
131     encoder << insideFixedPosition;
132     encoder << hasPlainText;
133     encoder << elementIsTransparentOrFullyClipped;
134     encoder << caretColor;
135 #endif
136 #if PLATFORM(MAC)
137     encoder << candidateRequestStartPosition;
138     encoder << paragraphContextForCandidateRequest;
139     encoder << stringForCandidateRequest;
140 #endif
141     encoder << fontAttributes;
142     encoder << canCut;
143     encoder << canCopy;
144     encoder << canPaste;
145 }
146
147 bool EditorState::PostLayoutData::decode(IPC::Decoder& decoder, PostLayoutData& result)
148 {
149     if (!decoder.decode(result.typingAttributes))
150         return false;
151 #if PLATFORM(IOS_FAMILY) || PLATFORM(GTK)
152     if (!decoder.decode(result.caretRectAtStart))
153         return false;
154 #endif
155 #if PLATFORM(IOS_FAMILY) || PLATFORM(MAC)
156     if (!decoder.decode(result.focusedElementRect))
157         return false;
158     if (!decoder.decode(result.selectedTextLength))
159         return false;
160     if (!decoder.decode(result.textAlignment))
161         return false;
162     if (!decoder.decode(result.textColor))
163         return false;
164     if (!decoder.decode(result.enclosingListType))
165         return false;
166 #endif
167 #if PLATFORM(IOS_FAMILY)
168     if (!decoder.decode(result.caretRectAtEnd))
169         return false;
170     if (!decoder.decode(result.selectionRects))
171         return false;
172     if (!decoder.decode(result.wordAtSelection))
173         return false;
174     if (!decoder.decode(result.characterAfterSelection))
175         return false;
176     if (!decoder.decode(result.characterBeforeSelection))
177         return false;
178     if (!decoder.decode(result.twoCharacterBeforeSelection))
179         return false;
180     if (!decoder.decode(result.isReplaceAllowed))
181         return false;
182     if (!decoder.decode(result.hasContent))
183         return false;
184     if (!decoder.decode(result.isStableStateUpdate))
185         return false;
186     if (!decoder.decode(result.insideFixedPosition))
187         return false;
188     if (!decoder.decode(result.hasPlainText))
189         return false;
190     if (!decoder.decode(result.elementIsTransparentOrFullyClipped))
191         return false;
192     if (!decoder.decode(result.caretColor))
193         return false;
194 #endif
195 #if PLATFORM(MAC)
196     if (!decoder.decode(result.candidateRequestStartPosition))
197         return false;
198
199     if (!decoder.decode(result.paragraphContextForCandidateRequest))
200         return false;
201
202     if (!decoder.decode(result.stringForCandidateRequest))
203         return false;
204 #endif
205
206     Optional<Optional<FontAttributes>> optionalFontAttributes;
207     decoder >> optionalFontAttributes;
208     if (!optionalFontAttributes)
209         return false;
210
211     result.fontAttributes = optionalFontAttributes.value();
212
213     if (!decoder.decode(result.canCut))
214         return false;
215     if (!decoder.decode(result.canCopy))
216         return false;
217     if (!decoder.decode(result.canPaste))
218         return false;
219
220     return true;
221 }
222
223 TextStream& operator<<(TextStream& ts, const EditorState& editorState)
224 {
225 #if PLATFORM(IOS_FAMILY)
226     if (editorState.firstMarkedRect != IntRect())
227         ts.dumpProperty("firstMarkedRect", editorState.firstMarkedRect);
228     if (editorState.lastMarkedRect != IntRect())
229         ts.dumpProperty("lastMarkedRect", editorState.lastMarkedRect);
230     if (editorState.markedText.length())
231         ts.dumpProperty("markedText", editorState.markedText);
232 #endif
233
234     if (editorState.shouldIgnoreSelectionChanges)
235         ts.dumpProperty("shouldIgnoreSelectionChanges", editorState.shouldIgnoreSelectionChanges);
236     if (!editorState.selectionIsNone)
237         ts.dumpProperty("selectionIsNone", editorState.selectionIsNone);
238     if (editorState.selectionIsRange)
239         ts.dumpProperty("selectionIsRange", editorState.selectionIsRange);
240     if (editorState.isContentEditable)
241         ts.dumpProperty("isContentEditable", editorState.isContentEditable);
242     if (editorState.isContentRichlyEditable)
243         ts.dumpProperty("isContentRichlyEditable", editorState.isContentRichlyEditable);
244     if (editorState.isInPasswordField)
245         ts.dumpProperty("isInPasswordField", editorState.isInPasswordField);
246     if (editorState.isInPlugin)
247         ts.dumpProperty("isInPlugin", editorState.isInPlugin);
248     if (editorState.hasComposition)
249         ts.dumpProperty("hasComposition", editorState.hasComposition);
250     if (editorState.isMissingPostLayoutData)
251         ts.dumpProperty("isMissingPostLayoutData", editorState.isMissingPostLayoutData);
252
253     if (editorState.isMissingPostLayoutData)
254         return ts;
255
256     TextStream::GroupScope scope(ts);
257     ts << "postLayoutData";
258     if (editorState.postLayoutData().typingAttributes != AttributeNone)
259         ts.dumpProperty("typingAttributes", editorState.postLayoutData().typingAttributes);
260 #if PLATFORM(IOS_FAMILY) || PLATFORM(GTK)
261     if (editorState.postLayoutData().caretRectAtStart != IntRect())
262         ts.dumpProperty("caretRectAtStart", editorState.postLayoutData().caretRectAtStart);
263 #endif
264 #if PLATFORM(IOS_FAMILY) || PLATFORM(MAC)
265     if (editorState.postLayoutData().focusedElementRect != IntRect())
266         ts.dumpProperty("focusedElementRect", editorState.postLayoutData().focusedElementRect);
267     if (editorState.postLayoutData().selectedTextLength)
268         ts.dumpProperty("selectedTextLength", editorState.postLayoutData().selectedTextLength);
269     if (editorState.postLayoutData().textAlignment != NoAlignment)
270         ts.dumpProperty("textAlignment", editorState.postLayoutData().textAlignment);
271     if (editorState.postLayoutData().textColor.isValid())
272         ts.dumpProperty("textColor", editorState.postLayoutData().textColor);
273     if (editorState.postLayoutData().enclosingListType != NoList)
274         ts.dumpProperty("enclosingListType", editorState.postLayoutData().enclosingListType);
275 #endif
276 #if PLATFORM(IOS_FAMILY)
277     if (editorState.postLayoutData().caretRectAtEnd != IntRect())
278         ts.dumpProperty("caretRectAtEnd", editorState.postLayoutData().caretRectAtEnd);
279     if (editorState.postLayoutData().selectionRects.size())
280         ts.dumpProperty("selectionRects", editorState.postLayoutData().selectionRects);
281     if (editorState.postLayoutData().wordAtSelection.length())
282         ts.dumpProperty("wordAtSelection", editorState.postLayoutData().wordAtSelection);
283     if (editorState.postLayoutData().characterAfterSelection)
284         ts.dumpProperty("characterAfterSelection", editorState.postLayoutData().characterAfterSelection);
285     if (editorState.postLayoutData().characterBeforeSelection)
286         ts.dumpProperty("characterBeforeSelection", editorState.postLayoutData().characterBeforeSelection);
287     if (editorState.postLayoutData().twoCharacterBeforeSelection)
288         ts.dumpProperty("twoCharacterBeforeSelection", editorState.postLayoutData().twoCharacterBeforeSelection);
289
290     if (editorState.postLayoutData().isReplaceAllowed)
291         ts.dumpProperty("isReplaceAllowed", editorState.postLayoutData().isReplaceAllowed);
292     if (editorState.postLayoutData().hasContent)
293         ts.dumpProperty("hasContent", editorState.postLayoutData().hasContent);
294     ts.dumpProperty("isStableStateUpdate", editorState.postLayoutData().isStableStateUpdate);
295     if (editorState.postLayoutData().insideFixedPosition)
296         ts.dumpProperty("insideFixedPosition", editorState.postLayoutData().insideFixedPosition);
297     if (editorState.postLayoutData().caretColor.isValid())
298         ts.dumpProperty("caretColor", editorState.postLayoutData().caretColor);
299 #endif
300 #if PLATFORM(MAC)
301     if (editorState.postLayoutData().candidateRequestStartPosition)
302         ts.dumpProperty("candidateRequestStartPosition", editorState.postLayoutData().candidateRequestStartPosition);
303     if (editorState.postLayoutData().paragraphContextForCandidateRequest.length())
304         ts.dumpProperty("paragraphContextForCandidateRequest", editorState.postLayoutData().paragraphContextForCandidateRequest);
305     if (editorState.postLayoutData().stringForCandidateRequest.length())
306         ts.dumpProperty("stringForCandidateRequest", editorState.postLayoutData().stringForCandidateRequest);
307 #endif
308
309     if (editorState.postLayoutData().canCut)
310         ts.dumpProperty("canCut", editorState.postLayoutData().canCut);
311     if (editorState.postLayoutData().canCopy)
312         ts.dumpProperty("canCopy", editorState.postLayoutData().canCopy);
313     if (editorState.postLayoutData().canPaste)
314         ts.dumpProperty("canPaste", editorState.postLayoutData().canPaste);
315
316     return ts;
317 }
318
319 } // namespace WebKit