initial import
[vuplus_webkit] / Source / WebKit2 / Shared / WebCoreArgumentCoders.cpp
1 /*
2  * Copyright (C) 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 "WebCoreArgumentCoders.h"
28
29 #include "ShareableBitmap.h"
30 #include <WebCore/AuthenticationChallenge.h>
31 #include <WebCore/Credential.h>
32 #include <WebCore/Cursor.h>
33 #include <WebCore/DatabaseDetails.h>
34 #include <WebCore/Editor.h>
35 #include <WebCore/GraphicsContext.h>
36 #include <WebCore/Image.h>
37 #include <WebCore/PluginData.h>
38 #include <WebCore/ProtectionSpace.h>
39 #include <WebCore/TextCheckerClient.h>
40 #include <WebCore/ViewportArguments.h>
41 #include <WebCore/WindowFeatures.h>
42 #include <wtf/text/StringHash.h>
43
44 #if PLATFORM(QT)
45 #include <WebCore/Animation.h>
46 #include <WebCore/FloatPoint3D.h>
47 #include <WebCore/IdentityTransformOperation.h>
48 #include <WebCore/Matrix3DTransformOperation.h>
49 #include <WebCore/MatrixTransformOperation.h>
50 #include <WebCore/PerspectiveTransformOperation.h>
51 #include <WebCore/RotateTransformOperation.h>
52 #include <WebCore/ScaleTransformOperation.h>
53 #include <WebCore/SkewTransformOperation.h>
54 #include <WebCore/TimingFunction.h>
55 #include <WebCore/TransformOperation.h>
56 #include <WebCore/TransformOperations.h>
57 #include <WebCore/TranslateTransformOperation.h>
58 #endif
59
60 using namespace WebCore;
61 using namespace WebKit;
62
63 namespace CoreIPC {
64
65 void ArgumentCoder<FloatPoint>::encode(ArgumentEncoder* encoder, const FloatPoint& floatPoint)
66 {
67     SimpleArgumentCoder<FloatPoint>::encode(encoder, floatPoint);
68 }
69
70 bool ArgumentCoder<FloatPoint>::decode(ArgumentDecoder* decoder, FloatPoint& floatPoint)
71 {
72     return SimpleArgumentCoder<FloatPoint>::decode(decoder, floatPoint);
73 }
74
75
76 void ArgumentCoder<FloatRect>::encode(ArgumentEncoder* encoder, const FloatRect& floatRect)
77 {
78     SimpleArgumentCoder<FloatRect>::encode(encoder, floatRect);
79 }
80
81 bool ArgumentCoder<FloatRect>::decode(ArgumentDecoder* decoder, FloatRect& floatRect)
82 {
83     return SimpleArgumentCoder<FloatRect>::decode(decoder, floatRect);
84 }
85
86
87 void ArgumentCoder<FloatSize>::encode(ArgumentEncoder* encoder, const FloatSize& floatSize)
88 {
89     SimpleArgumentCoder<FloatSize>::encode(encoder, floatSize);
90 }
91
92 bool ArgumentCoder<FloatSize>::decode(ArgumentDecoder* decoder, FloatSize& floatSize)
93 {
94     return SimpleArgumentCoder<FloatSize>::decode(decoder, floatSize);
95 }
96
97
98 void ArgumentCoder<IntPoint>::encode(ArgumentEncoder* encoder, const IntPoint& intPoint)
99 {
100     SimpleArgumentCoder<IntPoint>::encode(encoder, intPoint);
101 }
102
103 bool ArgumentCoder<IntPoint>::decode(ArgumentDecoder* decoder, IntPoint& intPoint)
104 {
105     return SimpleArgumentCoder<IntPoint>::decode(decoder, intPoint);
106 }
107
108
109 void ArgumentCoder<IntRect>::encode(ArgumentEncoder* encoder, const IntRect& intRect)
110 {
111     SimpleArgumentCoder<IntRect>::encode(encoder, intRect);
112 }
113
114 bool ArgumentCoder<IntRect>::decode(ArgumentDecoder* decoder, IntRect& intRect)
115 {
116     return SimpleArgumentCoder<IntRect>::decode(decoder, intRect);
117 }
118
119
120 void ArgumentCoder<IntSize>::encode(ArgumentEncoder* encoder, const IntSize& intSize)
121 {
122     SimpleArgumentCoder<IntSize>::encode(encoder, intSize);
123 }
124
125 bool ArgumentCoder<IntSize>::decode(ArgumentDecoder* decoder, IntSize& intSize)
126 {
127     return SimpleArgumentCoder<IntSize>::decode(decoder, intSize);
128 }
129
130
131 void ArgumentCoder<ViewportArguments>::encode(ArgumentEncoder* encoder, const ViewportArguments& viewportArguments)
132 {
133     SimpleArgumentCoder<ViewportArguments>::encode(encoder, viewportArguments);
134 }
135
136 bool ArgumentCoder<ViewportArguments>::decode(ArgumentDecoder* decoder, ViewportArguments& viewportArguments)
137 {
138     return SimpleArgumentCoder<ViewportArguments>::decode(decoder, viewportArguments);
139 }
140
141 void ArgumentCoder<MimeClassInfo>::encode(ArgumentEncoder* encoder, const MimeClassInfo& mimeClassInfo)
142 {
143     encoder->encode(mimeClassInfo.type);
144     encoder->encode(mimeClassInfo.desc);
145     encoder->encode(mimeClassInfo.extensions);
146 }
147
148 bool ArgumentCoder<MimeClassInfo>::decode(ArgumentDecoder* decoder, MimeClassInfo& mimeClassInfo)
149 {
150     if (!decoder->decode(mimeClassInfo.type))
151         return false;
152     if (!decoder->decode(mimeClassInfo.desc))
153         return false;
154     if (!decoder->decode(mimeClassInfo.extensions))
155         return false;
156
157     return true;
158 }
159
160
161 void ArgumentCoder<PluginInfo>::encode(ArgumentEncoder* encoder, const PluginInfo& pluginInfo)
162 {
163     encoder->encode(pluginInfo.name);
164     encoder->encode(pluginInfo.file);
165     encoder->encode(pluginInfo.desc);
166     encoder->encode(pluginInfo.mimes);
167 }
168     
169 bool ArgumentCoder<PluginInfo>::decode(ArgumentDecoder* decoder, PluginInfo& pluginInfo)
170 {
171     if (!decoder->decode(pluginInfo.name))
172         return false;
173     if (!decoder->decode(pluginInfo.file))
174         return false;
175     if (!decoder->decode(pluginInfo.desc))
176         return false;
177     if (!decoder->decode(pluginInfo.mimes))
178         return false;
179
180     return true;
181 }
182
183
184 void ArgumentCoder<HTTPHeaderMap>::encode(ArgumentEncoder* encoder, const HTTPHeaderMap& headerMap)
185 {
186     encoder->encode(static_cast<const HashMap<AtomicString, String, CaseFoldingHash>&>(headerMap));
187 }
188
189 bool ArgumentCoder<HTTPHeaderMap>::decode(ArgumentDecoder* decoder, HTTPHeaderMap& headerMap)
190 {
191     return decoder->decode(static_cast<HashMap<AtomicString, String, CaseFoldingHash>&>(headerMap));
192 }
193
194
195 void ArgumentCoder<AuthenticationChallenge>::encode(ArgumentEncoder* encoder, const AuthenticationChallenge& challenge)
196 {
197     encoder->encode(challenge.protectionSpace());
198     encoder->encode(challenge.proposedCredential());
199     encoder->encode(challenge.previousFailureCount());
200     encoder->encode(challenge.failureResponse());
201     encoder->encode(challenge.error());
202 }
203
204 bool ArgumentCoder<AuthenticationChallenge>::decode(ArgumentDecoder* decoder, AuthenticationChallenge& challenge)
205 {    
206     ProtectionSpace protectionSpace;
207     if (!decoder->decode(protectionSpace))
208         return false;
209
210     Credential proposedCredential;
211     if (!decoder->decode(proposedCredential))
212         return false;
213
214     unsigned previousFailureCount;    
215     if (!decoder->decode(previousFailureCount))
216         return false;
217
218     ResourceResponse failureResponse;
219     if (!decoder->decode(failureResponse))
220         return false;
221
222     ResourceError error;
223     if (!decoder->decode(error))
224         return false;
225     
226     challenge = AuthenticationChallenge(protectionSpace, proposedCredential, previousFailureCount, failureResponse, error);
227     return true;
228 }
229
230
231 void ArgumentCoder<ProtectionSpace>::encode(ArgumentEncoder* encoder, const ProtectionSpace& space)
232 {
233     encoder->encode(space.host());
234     encoder->encode(space.port());
235     encoder->encodeEnum(space.serverType());
236     encoder->encode(space.realm());
237     encoder->encodeEnum(space.authenticationScheme());
238 }
239
240 bool ArgumentCoder<ProtectionSpace>::decode(ArgumentDecoder* decoder, ProtectionSpace& space)
241 {
242     String host;
243     if (!decoder->decode(host))
244         return false;
245
246     int port;
247     if (!decoder->decode(port))
248         return false;
249
250     ProtectionSpaceServerType serverType;
251     if (!decoder->decodeEnum(serverType))
252         return false;
253
254     String realm;
255     if (!decoder->decode(realm))
256         return false;
257     
258     ProtectionSpaceAuthenticationScheme authenticationScheme;
259     if (!decoder->decodeEnum(authenticationScheme))
260         return false;
261
262     space = ProtectionSpace(host, port, serverType, realm, authenticationScheme);
263     return true;
264 }
265
266 void ArgumentCoder<Credential>::encode(ArgumentEncoder* encoder, const Credential& credential)
267 {
268     encoder->encode(credential.user());
269     encoder->encode(credential.password());
270     encoder->encodeEnum(credential.persistence());
271 }
272
273 bool ArgumentCoder<Credential>::decode(ArgumentDecoder* decoder, Credential& credential)
274 {
275     String user;
276     if (!decoder->decode(user))
277         return false;
278
279     String password;
280     if (!decoder->decode(password))
281         return false;
282
283     CredentialPersistence persistence;
284     if (!decoder->decodeEnum(persistence))
285         return false;
286     
287     credential = Credential(user, password, persistence);
288     return true;
289 }
290
291 static void encodeImage(ArgumentEncoder* encoder, Image* image)
292 {
293     RefPtr<ShareableBitmap> bitmap = ShareableBitmap::createShareable(image->size(), ShareableBitmap::SupportsAlpha);
294     bitmap->createGraphicsContext()->drawImage(image, ColorSpaceDeviceRGB, IntPoint());
295
296     ShareableBitmap::Handle handle;
297     bitmap->createHandle(handle);
298
299     encoder->encode(handle);
300 }
301
302 static bool decodeImage(ArgumentDecoder* decoder, RefPtr<Image>& image)
303 {
304     ShareableBitmap::Handle handle;
305     if (!decoder->decode(handle))
306         return false;
307     
308     RefPtr<ShareableBitmap> bitmap = ShareableBitmap::create(handle);
309     if (!bitmap)
310         return false;
311     image = bitmap->createImage();
312     if (!image)
313         return false;
314     return true;
315 }
316
317 void ArgumentCoder<Cursor>::encode(ArgumentEncoder* encoder, const Cursor& cursor)
318 {
319     encoder->encodeEnum(cursor.type());
320         
321     if (cursor.type() != Cursor::Custom)
322         return;
323
324     if (cursor.image()->isNull()) {
325         encoder->encodeBool(false); // There is no valid image being encoded.
326         return;
327     }
328
329     encoder->encodeBool(true);
330     encodeImage(encoder, cursor.image());
331     encoder->encode(cursor.hotSpot());
332 }
333
334 bool ArgumentCoder<Cursor>::decode(ArgumentDecoder* decoder, Cursor& cursor)
335 {
336     Cursor::Type type;
337     if (!decoder->decodeEnum(type))
338         return false;
339
340     if (type > Cursor::Custom)
341         return false;
342
343     if (type != Cursor::Custom) {
344         const Cursor& cursorReference = Cursor::fromType(type);
345         // Calling platformCursor here will eagerly create the platform cursor for the cursor singletons inside WebCore.
346         // This will avoid having to re-create the platform cursors over and over.
347         (void)cursorReference.platformCursor();
348
349         cursor = cursorReference;
350         return true;
351     }
352
353     bool isValidImagePresent;
354     if (!decoder->decode(isValidImagePresent))
355         return false;
356
357     if (!isValidImagePresent) {
358         cursor = Cursor(Image::nullImage(), IntPoint());
359         return true;
360     }
361
362     RefPtr<Image> image;
363     if (!decodeImage(decoder, image))
364         return false;
365
366     IntPoint hotSpot;
367     if (!decoder->decode(hotSpot))
368         return false;
369
370     if (!image->rect().contains(hotSpot))
371         return false;
372
373     cursor = Cursor(image.get(), hotSpot);
374     return true;
375 }
376
377
378 void ArgumentCoder<WindowFeatures>::encode(ArgumentEncoder* encoder, const WindowFeatures& windowFeatures)
379 {
380     encoder->encode(windowFeatures.x);
381     encoder->encode(windowFeatures.y);
382     encoder->encode(windowFeatures.width);
383     encoder->encode(windowFeatures.height);
384     encoder->encode(windowFeatures.xSet);
385     encoder->encode(windowFeatures.ySet);
386     encoder->encode(windowFeatures.widthSet);
387     encoder->encode(windowFeatures.heightSet);
388     encoder->encode(windowFeatures.menuBarVisible);
389     encoder->encode(windowFeatures.statusBarVisible);
390     encoder->encode(windowFeatures.toolBarVisible);
391     encoder->encode(windowFeatures.locationBarVisible);
392     encoder->encode(windowFeatures.scrollbarsVisible);
393     encoder->encode(windowFeatures.resizable);
394     encoder->encode(windowFeatures.fullscreen);
395     encoder->encode(windowFeatures.dialog);
396 }
397
398 bool ArgumentCoder<WindowFeatures>::decode(ArgumentDecoder* decoder, WindowFeatures& windowFeatures)
399 {
400     if (!decoder->decode(windowFeatures.x))
401         return false;
402     if (!decoder->decode(windowFeatures.y))
403         return false;
404     if (!decoder->decode(windowFeatures.width))
405         return false;
406     if (!decoder->decode(windowFeatures.height))
407         return false;
408     if (!decoder->decode(windowFeatures.xSet))
409         return false;
410     if (!decoder->decode(windowFeatures.ySet))
411         return false;
412     if (!decoder->decode(windowFeatures.widthSet))
413         return false;
414     if (!decoder->decode(windowFeatures.heightSet))
415         return false;
416     if (!decoder->decode(windowFeatures.menuBarVisible))
417         return false;
418     if (!decoder->decode(windowFeatures.statusBarVisible))
419         return false;
420     if (!decoder->decode(windowFeatures.toolBarVisible))
421         return false;
422     if (!decoder->decode(windowFeatures.locationBarVisible))
423         return false;
424     if (!decoder->decode(windowFeatures.scrollbarsVisible))
425         return false;
426     if (!decoder->decode(windowFeatures.resizable))
427         return false;
428     if (!decoder->decode(windowFeatures.fullscreen))
429         return false;
430     if (!decoder->decode(windowFeatures.dialog))
431         return false;
432     return true;
433 }
434
435
436 void ArgumentCoder<Color>::encode(ArgumentEncoder* encoder, const Color& color)
437 {
438     if (!color.isValid()) {
439         encoder->encodeBool(false);
440         return;
441     }
442
443     encoder->encodeBool(true);
444     encoder->encode(color.rgb());
445 }
446
447 bool ArgumentCoder<Color>::decode(ArgumentDecoder* decoder, Color& color)
448 {
449     bool isValid;
450     if (!decoder->decode(isValid))
451         return false;
452
453     if (!isValid) {
454         color = Color();
455         return true;
456     }
457
458     RGBA32 rgba;
459     if (!decoder->decode(rgba))
460         return false;
461
462     color = Color(rgba);
463     return true;
464 }
465
466
467 void ArgumentCoder<CompositionUnderline>::encode(ArgumentEncoder* encoder, const CompositionUnderline& underline)
468 {
469     encoder->encode(underline.startOffset);
470     encoder->encode(underline.endOffset);
471     encoder->encode(underline.thick);
472     encoder->encode(underline.color);
473 }
474
475 bool ArgumentCoder<CompositionUnderline>::decode(ArgumentDecoder* decoder, CompositionUnderline& underline)
476 {
477     if (!decoder->decode(underline.startOffset))
478         return false;
479     if (!decoder->decode(underline.endOffset))
480         return false;
481     if (!decoder->decode(underline.thick))
482         return false;
483     if (!decoder->decode(underline.color))
484         return false;
485
486     return true;
487 }
488
489
490 void ArgumentCoder<DatabaseDetails>::encode(ArgumentEncoder* encoder, const DatabaseDetails& details)
491 {
492     encoder->encode(details.name());
493     encoder->encode(details.displayName());
494     encoder->encode(details.expectedUsage());
495     encoder->encode(details.currentUsage());
496 }
497     
498 bool ArgumentCoder<DatabaseDetails>::decode(ArgumentDecoder* decoder, DatabaseDetails& details)
499 {
500     String name;
501     if (!decoder->decode(name))
502         return false;
503
504     String displayName;
505     if (!decoder->decode(displayName))
506         return false;
507
508     uint64_t expectedUsage;
509     if (!decoder->decode(expectedUsage))
510         return false;
511
512     uint64_t currentUsage;
513     if (!decoder->decode(currentUsage))
514         return false;
515     
516     details = DatabaseDetails(name, displayName, expectedUsage, currentUsage);
517     return true;
518 }
519
520
521 void ArgumentCoder<GrammarDetail>::encode(ArgumentEncoder* encoder, const GrammarDetail& detail)
522 {
523     encoder->encode(detail.location);
524     encoder->encode(detail.length);
525     encoder->encode(detail.guesses);
526     encoder->encode(detail.userDescription);
527 }
528
529 bool ArgumentCoder<GrammarDetail>::decode(ArgumentDecoder* decoder, GrammarDetail& detail)
530 {
531     if (!decoder->decode(detail.location))
532         return false;
533     if (!decoder->decode(detail.length))
534         return false;
535     if (!decoder->decode(detail.guesses))
536         return false;
537     if (!decoder->decode(detail.userDescription))
538         return false;
539
540     return true;
541 }
542
543
544 void ArgumentCoder<TextCheckingResult>::encode(ArgumentEncoder* encoder, const TextCheckingResult& result)
545 {
546     encoder->encodeEnum(result.type);
547     encoder->encode(result.location);
548     encoder->encode(result.length);
549     encoder->encode(result.details);
550     encoder->encode(result.replacement);
551 }
552
553 bool ArgumentCoder<TextCheckingResult>::decode(ArgumentDecoder* decoder, TextCheckingResult& result)
554 {
555     if (!decoder->decodeEnum(result.type))
556         return false;
557     if (!decoder->decode(result.location))
558         return false;
559     if (!decoder->decode(result.length))
560         return false;
561     if (!decoder->decode(result.details))
562         return false;
563     if (!decoder->decode(result.replacement))
564         return false;
565     return true;
566 }
567
568 #if PLATFORM(QT)
569
570 void ArgumentCoder<FloatPoint3D>::encode(ArgumentEncoder* encoder, const FloatPoint3D& floatPoint3D)
571 {
572     SimpleArgumentCoder<FloatPoint3D>::encode(encoder, floatPoint3D);
573 }
574
575 bool ArgumentCoder<FloatPoint3D>::decode(ArgumentDecoder* decoder, FloatPoint3D& floatPoint3D)
576 {
577     return SimpleArgumentCoder<FloatPoint3D>::decode(decoder, floatPoint3D);
578 }
579
580
581 void ArgumentCoder<Length>::encode(ArgumentEncoder* encoder, const Length& length)
582 {
583     SimpleArgumentCoder<Length>::encode(encoder, length);
584 }
585
586 bool ArgumentCoder<Length>::decode(ArgumentDecoder* decoder, Length& length)
587 {
588     return SimpleArgumentCoder<Length>::decode(decoder, length);
589 }
590
591
592 void ArgumentCoder<TransformationMatrix>::encode(ArgumentEncoder* encoder, const TransformationMatrix& transformationMatrix)
593 {
594     SimpleArgumentCoder<TransformationMatrix>::encode(encoder, transformationMatrix);
595 }
596
597 bool ArgumentCoder<TransformationMatrix>::decode(ArgumentDecoder* decoder, TransformationMatrix& transformationMatrix)
598 {
599     return SimpleArgumentCoder<TransformationMatrix>::decode(decoder, transformationMatrix);
600 }
601
602
603 void ArgumentCoder<MatrixTransformOperation>::encode(ArgumentEncoder* encoder, const MatrixTransformOperation& operation)
604 {
605     SimpleArgumentCoder<MatrixTransformOperation>::encode(encoder, operation);
606 }
607
608 bool ArgumentCoder<MatrixTransformOperation>::decode(ArgumentDecoder* decoder, MatrixTransformOperation& operation)
609 {
610     return SimpleArgumentCoder<MatrixTransformOperation>::decode(decoder, operation);
611 }
612
613
614 void ArgumentCoder<Matrix3DTransformOperation>::encode(ArgumentEncoder* encoder, const Matrix3DTransformOperation& operation)
615 {
616     SimpleArgumentCoder<Matrix3DTransformOperation>::encode(encoder, operation);
617 }
618
619 bool ArgumentCoder<Matrix3DTransformOperation>::decode(ArgumentDecoder* decoder, Matrix3DTransformOperation& operation)
620 {
621     return SimpleArgumentCoder<Matrix3DTransformOperation>::decode(decoder, operation);
622 }
623
624
625 void ArgumentCoder<PerspectiveTransformOperation>::encode(ArgumentEncoder* encoder, const PerspectiveTransformOperation& operation)
626 {
627     SimpleArgumentCoder<PerspectiveTransformOperation>::encode(encoder, operation);
628 }
629
630 bool ArgumentCoder<PerspectiveTransformOperation>::decode(ArgumentDecoder* decoder, PerspectiveTransformOperation& operation)
631 {
632     return SimpleArgumentCoder<PerspectiveTransformOperation>::decode(decoder, operation);
633 }
634
635
636 void ArgumentCoder<RotateTransformOperation>::encode(ArgumentEncoder* encoder, const RotateTransformOperation& operation)
637 {
638     SimpleArgumentCoder<RotateTransformOperation>::encode(encoder, operation);
639 }
640
641 bool ArgumentCoder<RotateTransformOperation>::decode(ArgumentDecoder* decoder, RotateTransformOperation& operation)
642 {
643     return SimpleArgumentCoder<RotateTransformOperation>::decode(decoder, operation);
644 }
645
646
647 void ArgumentCoder<ScaleTransformOperation>::encode(ArgumentEncoder* encoder, const ScaleTransformOperation& operation)
648 {
649     SimpleArgumentCoder<ScaleTransformOperation>::encode(encoder, operation);
650 }
651
652 bool ArgumentCoder<ScaleTransformOperation>::decode(ArgumentDecoder* decoder, ScaleTransformOperation& operation)
653 {
654     return SimpleArgumentCoder<ScaleTransformOperation>::decode(decoder, operation);
655 }
656
657
658 void ArgumentCoder<SkewTransformOperation>::encode(ArgumentEncoder* encoder, const SkewTransformOperation& operation)
659 {
660     SimpleArgumentCoder<SkewTransformOperation>::encode(encoder, operation);
661 }
662
663 bool ArgumentCoder<SkewTransformOperation>::decode(ArgumentDecoder* decoder, SkewTransformOperation& operation)
664 {
665     return SimpleArgumentCoder<SkewTransformOperation>::decode(decoder, operation);
666 }
667
668
669 void ArgumentCoder<TranslateTransformOperation>::encode(ArgumentEncoder* encoder, const TranslateTransformOperation& operation)
670 {
671     SimpleArgumentCoder<TranslateTransformOperation>::encode(encoder, operation);
672 }
673
674 bool ArgumentCoder<TranslateTransformOperation>::decode(ArgumentDecoder* decoder, TranslateTransformOperation& operation)
675 {
676     return SimpleArgumentCoder<TranslateTransformOperation>::decode(decoder, operation);
677 }
678
679 void ArgumentCoder<RefPtr<TimingFunction> >::encode(ArgumentEncoder* encoder, const RefPtr<TimingFunction>& function)
680 {
681     // We don't want to encode null-references.
682     ASSERT(function);
683
684     encoder->encodeEnum(function->type());
685     switch (function->type()) {
686     case TimingFunction::LinearFunction:
687         break;
688     case TimingFunction::CubicBezierFunction: {
689         CubicBezierTimingFunction* cubicFunction = static_cast<CubicBezierTimingFunction*>(function.get());
690         encoder->encodeDouble(cubicFunction->x1());
691         encoder->encodeDouble(cubicFunction->y1());
692         encoder->encodeDouble(cubicFunction->x2());
693         encoder->encodeDouble(cubicFunction->y2());
694         break;
695     }
696     case TimingFunction::StepsFunction: {
697         StepsTimingFunction* stepsFunction = static_cast<StepsTimingFunction*>(function.get());
698         encoder->encodeInt32(stepsFunction->numberOfSteps());
699         encoder->encodeBool(stepsFunction->stepAtStart());
700         break;
701     }
702     }
703 }
704
705 bool ArgumentCoder<RefPtr<TimingFunction> >::decode(ArgumentDecoder* decoder, RefPtr<TimingFunction>& function)
706 {
707     TimingFunction::TimingFunctionType type;
708     if (!decoder->decodeEnum(type))
709         return false;
710
711     switch (type) {
712     case TimingFunction::LinearFunction:
713         function = LinearTimingFunction::create();
714         return true;
715
716     case TimingFunction::CubicBezierFunction: {
717         double x1, y1, x2, y2;
718         if (!decoder->decodeDouble(x1))
719             return false;
720         if (!decoder->decodeDouble(y1))
721             return false;
722         if (!decoder->decodeDouble(x2))
723             return false;
724         if (!decoder->decodeDouble(y2))
725             return false;
726         function = CubicBezierTimingFunction::create(x1, y1, x2, y2);
727         return true;
728     }
729
730     case TimingFunction::StepsFunction: {
731         int numSteps;
732         bool stepAtStart;
733         if (!decoder->decodeInt32(numSteps))
734             return false;
735         if (!decoder->decodeBool(stepAtStart))
736             return false;
737
738         function = StepsTimingFunction::create(numSteps, stepAtStart);
739         return true;
740     }
741
742     }
743
744     return false;
745 }
746
747
748 template<typename T>
749 void encodeOperation(ArgumentEncoder* encoder, const RefPtr<TransformOperation>& operation)
750 {
751     encoder->encode(*static_cast<const T*>(operation.get()));
752 }
753
754 void ArgumentCoder<RefPtr<TransformOperation> >::encode(ArgumentEncoder* encoder, const RefPtr<TransformOperation>& operation)
755 {
756     // We don't want to encode null-references.
757     ASSERT(operation);
758
759     encoder->encodeEnum(operation->getOperationType());
760     switch (operation->getOperationType()) {
761     case TransformOperation::SCALE:
762     case TransformOperation::SCALE_X:
763     case TransformOperation::SCALE_Y:
764     case TransformOperation::SCALE_Z:
765     case TransformOperation::SCALE_3D:
766         encodeOperation<ScaleTransformOperation>(encoder, operation);
767         return;
768
769     case TransformOperation::TRANSLATE:
770     case TransformOperation::TRANSLATE_X:
771     case TransformOperation::TRANSLATE_Y:
772     case TransformOperation::TRANSLATE_Z:
773     case TransformOperation::TRANSLATE_3D:
774         encodeOperation<TranslateTransformOperation>(encoder, operation);
775         return;
776
777     case TransformOperation::ROTATE:
778     case TransformOperation::ROTATE_X:
779     case TransformOperation::ROTATE_Y:
780     case TransformOperation::ROTATE_3D:
781         encodeOperation<RotateTransformOperation>(encoder, operation);
782         return;
783
784     case TransformOperation::SKEW:
785     case TransformOperation::SKEW_X:
786     case TransformOperation::SKEW_Y:
787         encodeOperation<SkewTransformOperation>(encoder, operation);
788         return;
789
790     case TransformOperation::MATRIX:
791         encodeOperation<MatrixTransformOperation>(encoder, operation);
792         return;
793
794     case TransformOperation::MATRIX_3D:
795         encodeOperation<Matrix3DTransformOperation>(encoder, operation);
796         return;
797
798     case TransformOperation::PERSPECTIVE:
799         encodeOperation<PerspectiveTransformOperation>(encoder, operation);
800         return;
801
802     case TransformOperation::IDENTITY:
803     case TransformOperation::NONE:
804         return;
805     }
806 }
807
808 template<typename T>
809 bool decodeOperation(ArgumentDecoder* decoder, RefPtr<TransformOperation>& operation, PassRefPtr<T> newOperation)
810 {
811     if (!decoder->decode(*newOperation.get()))
812         return false;
813
814     operation = newOperation.get();
815     return true;
816 }
817
818 bool ArgumentCoder<RefPtr<TransformOperation> >::decode(ArgumentDecoder* decoder, RefPtr<TransformOperation>& operation)
819 {
820     TransformOperation::OperationType type;
821     if (!decoder->decodeEnum(type))
822         return false;
823
824     switch (type) {
825     case TransformOperation::SCALE:
826     case TransformOperation::SCALE_X:
827     case TransformOperation::SCALE_Y:
828     case TransformOperation::SCALE_Z:
829     case TransformOperation::SCALE_3D:
830         return decodeOperation(decoder, operation, ScaleTransformOperation::create(1.0, 1.0, type));
831
832     case TransformOperation::TRANSLATE:
833     case TransformOperation::TRANSLATE_X:
834     case TransformOperation::TRANSLATE_Y:
835     case TransformOperation::TRANSLATE_Z:
836     case TransformOperation::TRANSLATE_3D:
837         return decodeOperation(decoder, operation, TranslateTransformOperation::create(Length(0, WebCore::Fixed), Length(0, WebCore::Fixed), type));
838
839     case TransformOperation::ROTATE:
840     case TransformOperation::ROTATE_X:
841     case TransformOperation::ROTATE_Y:
842     case TransformOperation::ROTATE_3D:
843         return decodeOperation(decoder, operation, RotateTransformOperation::create(0.0, type));
844
845     case TransformOperation::SKEW:
846     case TransformOperation::SKEW_X:
847     case TransformOperation::SKEW_Y:
848         return decodeOperation(decoder, operation, SkewTransformOperation::create(0.0, 0.0, type));
849
850     case TransformOperation::MATRIX:
851         return decodeOperation(decoder, operation, MatrixTransformOperation::create(TransformationMatrix()));
852
853     case TransformOperation::MATRIX_3D:
854         return decodeOperation(decoder, operation, Matrix3DTransformOperation::create(TransformationMatrix()));
855
856     case TransformOperation::PERSPECTIVE:
857         return decodeOperation(decoder, operation, PerspectiveTransformOperation::create(Length(0, WebCore::Fixed)));
858
859     case TransformOperation::IDENTITY:
860     case TransformOperation::NONE:
861         operation = IdentityTransformOperation::create();
862         return true;
863     }
864
865     return false;
866 }
867
868 void ArgumentCoder<TransformOperations>::encode(ArgumentEncoder* encoder, const TransformOperations& operations)
869 {
870     Vector<RefPtr<TransformOperation> > operationsVector = operations.operations();
871     int size = operationsVector.size();
872     encoder->encodeInt32(size);
873     for (int i = 0; i < size; ++i)
874         ArgumentCoder<RefPtr<TransformOperation> >::encode(encoder, operationsVector[i]);
875 }
876
877 bool ArgumentCoder<TransformOperations>::decode(ArgumentDecoder* decoder, TransformOperations& operations)
878 {
879     int size;
880     if (!decoder->decodeInt32(size))
881         return false;
882
883     Vector<RefPtr<TransformOperation> >& operationVector = operations.operations();
884     operationVector.clear();
885     operationVector.resize(size);
886     for (int i = 0; i < size; ++i) {
887         RefPtr<TransformOperation> operation;
888         if (!ArgumentCoder<RefPtr<TransformOperation> >::decode(decoder, operation))
889             return false;
890         operationVector[i] = operation;
891     }
892
893     return true;
894 }
895
896 template<typename T>
897 static void encodeBoolAndValue(ArgumentEncoder* encoder, bool isSet, const T& value)
898 {
899     encoder->encodeBool(isSet);
900     if (isSet)
901         encoder->encode(value);
902 }
903
904 template<typename T>
905 static void encodeBoolAndEnumValue(ArgumentEncoder* encoder, bool isSet, T value)
906 {
907     encoder->encodeBool(isSet);
908     if (isSet)
909         encoder->encodeEnum(value);
910 }
911
912 void ArgumentCoder<Animation>::encode(ArgumentEncoder* encoder, const Animation& animation)
913 {
914     encodeBoolAndValue(encoder, animation.isDelaySet(), animation.delay());
915     encodeBoolAndEnumValue(encoder, animation.isDirectionSet(), animation.direction());
916     encodeBoolAndValue(encoder, animation.isDurationSet(), animation.duration());
917     encodeBoolAndValue(encoder, animation.isFillModeSet(), animation.fillMode());
918     encodeBoolAndValue(encoder, animation.isIterationCountSet(), animation.iterationCount());
919     encodeBoolAndValue(encoder, animation.isNameSet(), animation.name());
920     encodeBoolAndEnumValue(encoder, animation.isPlayStateSet(), animation.playState());
921     encodeBoolAndValue(encoder, animation.isPropertySet(), animation.property());
922     encodeBoolAndValue<RefPtr<TimingFunction> >(encoder, animation.isTimingFunctionSet(), animation.timingFunction());
923     encoder->encodeBool(animation.isNoneAnimation());
924 }
925
926
927 template<typename T>
928 static bool decodeBoolAndValue(ArgumentDecoder* decoder, bool& isSet, T& value)
929 {
930     if (!decoder->decodeBool(isSet))
931         return false;
932     if (!isSet)
933         return true;
934
935     return decoder->decode(value);
936 }
937
938 template<typename T>
939 static bool decodeBoolAndEnumValue(ArgumentDecoder* decoder, bool& isSet, T& value)
940 {
941     if (!decoder->decodeBool(isSet))
942         return false;
943     if (!isSet)
944         return true;
945
946     return decoder->decodeEnum(value);
947 }
948
949 bool ArgumentCoder<Animation>::decode(ArgumentDecoder* decoder, Animation& animation)
950 {
951     bool isDelaySet, isDirectionSet, isDurationSet, isFillModeSet, isIterationCountSet, isNameSet, isPlayStateSet, isPropertySet, isTimingFunctionSet;
952     int property, iterationCount, fillMode;
953     double duration;
954     RefPtr<TimingFunction> timingFunction;
955     String name;
956
957     animation.clearAll();
958
959     double delay;
960     if (!decodeBoolAndValue(decoder, isDelaySet, delay))
961         return false;
962
963     Animation::AnimationDirection direction = Animation::AnimationDirectionNormal;
964     if (!decodeBoolAndEnumValue(decoder, isDirectionSet, direction))
965         return false;
966     if (!decodeBoolAndValue(decoder, isDurationSet, duration))
967         return false;
968     if (!decodeBoolAndValue(decoder, isFillModeSet, fillMode))
969         return false;
970     if (!decodeBoolAndValue(decoder, isIterationCountSet, iterationCount))
971         return false;
972     if (!decodeBoolAndValue(decoder, isNameSet, name))
973         return false;
974
975     EAnimPlayState playState = AnimPlayStatePlaying;
976     if (!decodeBoolAndEnumValue(decoder, isPlayStateSet, playState))
977         return false;
978     if (!decodeBoolAndValue(decoder, isPropertySet, property))
979         return false;
980     if (!decodeBoolAndValue<RefPtr<TimingFunction> >(decoder, isTimingFunctionSet, timingFunction))
981         return false;
982
983     if (isDelaySet)
984         animation.setDelay(delay);
985     if (isDirectionSet)
986         animation.setDirection(direction);
987     if (isDurationSet)
988         animation.setDuration(duration);
989     if (isFillModeSet)
990         animation.setFillMode(fillMode);
991     if (isIterationCountSet)
992         animation.setIterationCount(iterationCount);
993     if (isNameSet)
994         animation.setName(name);
995     if (isPlayStateSet)
996         animation.setPlayState(playState);
997     if (isPropertySet)
998         animation.setProperty(property);
999     if (isTimingFunctionSet)
1000         animation.setTimingFunction(timingFunction);
1001
1002     return true;
1003 }
1004 #endif
1005
1006 } // namespace CoreIPC