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
32namespace WebKit {
33using namespace WebCore;
34
35void 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
59bool 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
108void 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(COCOA)
115 encoder << focusedElementRect;
116 encoder << selectedTextLength;
117 encoder << textAlignment;
118 encoder << textColor;
119 encoder << enclosingListType;
120 encoder << baseWritingDirection;
121#endif
122#if PLATFORM(IOS_FAMILY)
123 encoder << caretRectAtEnd;
124 encoder << selectionRects;
125 encoder << wordAtSelection;
126 encoder << characterAfterSelection;
127 encoder << characterBeforeSelection;
128 encoder << twoCharacterBeforeSelection;
129 encoder << isReplaceAllowed;
130 encoder << hasContent;
131 encoder << isStableStateUpdate;
132 encoder << insideFixedPosition;
133 encoder << hasPlainText;
134 encoder << editableRootIsTransparentOrFullyClipped;
135 encoder << caretColor;
136 encoder << atStartOfSentence;
137#endif
138#if PLATFORM(MAC)
139 encoder << candidateRequestStartPosition;
140 encoder << paragraphContextForCandidateRequest;
141 encoder << stringForCandidateRequest;
142#endif
143 encoder << fontAttributes;
144 encoder << canCut;
145 encoder << canCopy;
146 encoder << canPaste;
147}
148
149bool EditorState::PostLayoutData::decode(IPC::Decoder& decoder, PostLayoutData& result)
150{
151 if (!decoder.decode(result.typingAttributes))
152 return false;
153#if PLATFORM(IOS_FAMILY) || PLATFORM(GTK)
154 if (!decoder.decode(result.caretRectAtStart))
155 return false;
156#endif
157#if PLATFORM(COCOA)
158 if (!decoder.decode(result.focusedElementRect))
159 return false;
160 if (!decoder.decode(result.selectedTextLength))
161 return false;
162 if (!decoder.decode(result.textAlignment))
163 return false;
164 if (!decoder.decode(result.textColor))
165 return false;
166 if (!decoder.decode(result.enclosingListType))
167 return false;
168 if (!decoder.decode(result.baseWritingDirection))
169 return false;
170#endif
171#if PLATFORM(IOS_FAMILY)
172 if (!decoder.decode(result.caretRectAtEnd))
173 return false;
174 if (!decoder.decode(result.selectionRects))
175 return false;
176 if (!decoder.decode(result.wordAtSelection))
177 return false;
178 if (!decoder.decode(result.characterAfterSelection))
179 return false;
180 if (!decoder.decode(result.characterBeforeSelection))
181 return false;
182 if (!decoder.decode(result.twoCharacterBeforeSelection))
183 return false;
184 if (!decoder.decode(result.isReplaceAllowed))
185 return false;
186 if (!decoder.decode(result.hasContent))
187 return false;
188 if (!decoder.decode(result.isStableStateUpdate))
189 return false;
190 if (!decoder.decode(result.insideFixedPosition))
191 return false;
192 if (!decoder.decode(result.hasPlainText))
193 return false;
194 if (!decoder.decode(result.editableRootIsTransparentOrFullyClipped))
195 return false;
196 if (!decoder.decode(result.caretColor))
197 return false;
198 if (!decoder.decode(result.atStartOfSentence))
199 return false;
200#endif
201#if PLATFORM(MAC)
202 if (!decoder.decode(result.candidateRequestStartPosition))
203 return false;
204
205 if (!decoder.decode(result.paragraphContextForCandidateRequest))
206 return false;
207
208 if (!decoder.decode(result.stringForCandidateRequest))
209 return false;
210#endif
211
212 Optional<Optional<FontAttributes>> optionalFontAttributes;
213 decoder >> optionalFontAttributes;
214 if (!optionalFontAttributes)
215 return false;
216
217 result.fontAttributes = optionalFontAttributes.value();
218
219 if (!decoder.decode(result.canCut))
220 return false;
221 if (!decoder.decode(result.canCopy))
222 return false;
223 if (!decoder.decode(result.canPaste))
224 return false;
225
226 return true;
227}
228
229TextStream& operator<<(TextStream& ts, const EditorState& editorState)
230{
231#if PLATFORM(IOS_FAMILY)
232 if (editorState.firstMarkedRect != IntRect())
233 ts.dumpProperty("firstMarkedRect", editorState.firstMarkedRect);
234 if (editorState.lastMarkedRect != IntRect())
235 ts.dumpProperty("lastMarkedRect", editorState.lastMarkedRect);
236 if (editorState.markedText.length())
237 ts.dumpProperty("markedText", editorState.markedText);
238#endif
239
240 if (editorState.shouldIgnoreSelectionChanges)
241 ts.dumpProperty("shouldIgnoreSelectionChanges", editorState.shouldIgnoreSelectionChanges);
242 if (!editorState.selectionIsNone)
243 ts.dumpProperty("selectionIsNone", editorState.selectionIsNone);
244 if (editorState.selectionIsRange)
245 ts.dumpProperty("selectionIsRange", editorState.selectionIsRange);
246 if (editorState.isContentEditable)
247 ts.dumpProperty("isContentEditable", editorState.isContentEditable);
248 if (editorState.isContentRichlyEditable)
249 ts.dumpProperty("isContentRichlyEditable", editorState.isContentRichlyEditable);
250 if (editorState.isInPasswordField)
251 ts.dumpProperty("isInPasswordField", editorState.isInPasswordField);
252 if (editorState.isInPlugin)
253 ts.dumpProperty("isInPlugin", editorState.isInPlugin);
254 if (editorState.hasComposition)
255 ts.dumpProperty("hasComposition", editorState.hasComposition);
256 if (editorState.isMissingPostLayoutData)
257 ts.dumpProperty("isMissingPostLayoutData", editorState.isMissingPostLayoutData);
258
259 if (editorState.isMissingPostLayoutData)
260 return ts;
261
262 TextStream::GroupScope scope(ts);
263 ts << "postLayoutData";
264 if (editorState.postLayoutData().typingAttributes != AttributeNone)
265 ts.dumpProperty("typingAttributes", editorState.postLayoutData().typingAttributes);
266#if PLATFORM(IOS_FAMILY) || PLATFORM(GTK)
267 if (editorState.postLayoutData().caretRectAtStart != IntRect())
268 ts.dumpProperty("caretRectAtStart", editorState.postLayoutData().caretRectAtStart);
269#endif
270#if PLATFORM(COCOA)
271 if (editorState.postLayoutData().focusedElementRect != IntRect())
272 ts.dumpProperty("focusedElementRect", editorState.postLayoutData().focusedElementRect);
273 if (editorState.postLayoutData().selectedTextLength)
274 ts.dumpProperty("selectedTextLength", editorState.postLayoutData().selectedTextLength);
275 if (editorState.postLayoutData().textAlignment != NoAlignment)
276 ts.dumpProperty("textAlignment", editorState.postLayoutData().textAlignment);
277 if (editorState.postLayoutData().textColor.isValid())
278 ts.dumpProperty("textColor", editorState.postLayoutData().textColor);
279 if (editorState.postLayoutData().enclosingListType != NoList)
280 ts.dumpProperty("enclosingListType", editorState.postLayoutData().enclosingListType);
281 if (editorState.postLayoutData().baseWritingDirection != WebCore::WritingDirection::Natural)
282 ts.dumpProperty("baseWritingDirection", static_cast<uint8_t>(editorState.postLayoutData().baseWritingDirection));
283#endif // PLATFORM(COCOA)
284#if PLATFORM(IOS_FAMILY)
285 if (editorState.postLayoutData().caretRectAtEnd != IntRect())
286 ts.dumpProperty("caretRectAtEnd", editorState.postLayoutData().caretRectAtEnd);
287 if (editorState.postLayoutData().selectionRects.size())
288 ts.dumpProperty("selectionRects", editorState.postLayoutData().selectionRects);
289 if (editorState.postLayoutData().wordAtSelection.length())
290 ts.dumpProperty("wordAtSelection", editorState.postLayoutData().wordAtSelection);
291 if (editorState.postLayoutData().characterAfterSelection)
292 ts.dumpProperty("characterAfterSelection", editorState.postLayoutData().characterAfterSelection);
293 if (editorState.postLayoutData().characterBeforeSelection)
294 ts.dumpProperty("characterBeforeSelection", editorState.postLayoutData().characterBeforeSelection);
295 if (editorState.postLayoutData().twoCharacterBeforeSelection)
296 ts.dumpProperty("twoCharacterBeforeSelection", editorState.postLayoutData().twoCharacterBeforeSelection);
297
298 if (editorState.postLayoutData().isReplaceAllowed)
299 ts.dumpProperty("isReplaceAllowed", editorState.postLayoutData().isReplaceAllowed);
300 if (editorState.postLayoutData().hasContent)
301 ts.dumpProperty("hasContent", editorState.postLayoutData().hasContent);
302 ts.dumpProperty("isStableStateUpdate", editorState.postLayoutData().isStableStateUpdate);
303 if (editorState.postLayoutData().insideFixedPosition)
304 ts.dumpProperty("insideFixedPosition", editorState.postLayoutData().insideFixedPosition);
305 if (editorState.postLayoutData().caretColor.isValid())
306 ts.dumpProperty("caretColor", editorState.postLayoutData().caretColor);
307#endif
308#if PLATFORM(MAC)
309 if (editorState.postLayoutData().candidateRequestStartPosition)
310 ts.dumpProperty("candidateRequestStartPosition", editorState.postLayoutData().candidateRequestStartPosition);
311 if (editorState.postLayoutData().paragraphContextForCandidateRequest.length())
312 ts.dumpProperty("paragraphContextForCandidateRequest", editorState.postLayoutData().paragraphContextForCandidateRequest);
313 if (editorState.postLayoutData().stringForCandidateRequest.length())
314 ts.dumpProperty("stringForCandidateRequest", editorState.postLayoutData().stringForCandidateRequest);
315#endif
316
317 if (editorState.postLayoutData().canCut)
318 ts.dumpProperty("canCut", editorState.postLayoutData().canCut);
319 if (editorState.postLayoutData().canCopy)
320 ts.dumpProperty("canCopy", editorState.postLayoutData().canCopy);
321 if (editorState.postLayoutData().canPaste)
322 ts.dumpProperty("canPaste", editorState.postLayoutData().canPaste);
323
324 return ts;
325}
326
327} // namespace WebKit
328