WebKit2 lacks API for obtaining a representation of the RenderLayer tree of a page...
[WebKit-https.git] / Source / WebKit2 / Shared / UserMessageCoders.h
1 /*
2  * Copyright (C) 2010 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 #ifndef UserMessageCoders_h
27 #define UserMessageCoders_h
28
29 #include "ArgumentDecoder.h"
30 #include "ArgumentEncoder.h"
31 #include "DataReference.h"
32 #include "ImmutableArray.h"
33 #include "ImmutableDictionary.h"
34 #include "ShareableBitmap.h"
35 #include "WebCertificateInfo.h"
36 #include "WebCoreArgumentCoders.h"
37 #include "WebData.h"
38 #include "WebGeometry.h"
39 #include "WebImage.h"
40 #include "WebNumber.h"
41 #include "WebRenderLayer.h"
42 #include "WebRenderObject.h"
43 #include "WebSerializedScriptValue.h"
44 #include "WebString.h"
45 #include "WebURL.h"
46 #include "WebURLRequest.h"
47 #include "WebUserContentURLPattern.h"
48
49 namespace WebKit {
50
51 //   - Null -> Null
52 //   - Array -> Array
53 //   - Dictionary -> Dictionary
54 //   - SerializedScriptValue -> SerializedScriptValue
55 //   - String -> String
56 //   - UserContentURLPattern -> UserContentURLPattern
57 //   - WebCertificateInfo -> WebCertificateInfo
58 //   - WebData -> WebData
59 //   - WebDouble -> WebDouble
60 //   - WebImage -> WebImage
61 //   - WebRenderLayer -> WebRenderLayer
62 //   - WebRenderObject -> WebRenderObject
63 //   - WebUInt64 -> WebUInt64
64 //   - WebURL -> WebURL
65 //   - WebURLRequest -> WebURLRequest
66
67 template<typename Owner>
68 class UserMessageEncoder {
69 public:
70     bool baseEncode(CoreIPC::ArgumentEncoder* encoder, APIObject::Type& type) const 
71     {
72         if (!m_root) {
73             encoder->encodeUInt32(APIObject::TypeNull);
74             return true;
75         }
76
77         type = m_root->type();
78         encoder->encodeUInt32(type);
79
80         switch (type) {
81         case APIObject::TypeArray: {
82             ImmutableArray* array = static_cast<ImmutableArray*>(m_root);
83             encoder->encode(static_cast<uint64_t>(array->size()));
84             for (size_t i = 0; i < array->size(); ++i)
85                 encoder->encode(Owner(array->at(i)));
86             return true;
87         }
88         case APIObject::TypeDictionary: {
89             ImmutableDictionary* dictionary = static_cast<ImmutableDictionary*>(m_root);
90             const ImmutableDictionary::MapType& map = dictionary->map();
91             encoder->encode(static_cast<uint64_t>(map.size()));
92
93             ImmutableDictionary::MapType::const_iterator it = map.begin();
94             ImmutableDictionary::MapType::const_iterator end = map.end();
95             for (; it != end; ++it) {
96                 encoder->encode(it->first);
97                 encoder->encode(Owner(it->second.get()));
98             }
99             return true;
100         }
101         case APIObject::TypeString: {
102             WebString* string = static_cast<WebString*>(m_root);
103             encoder->encode(string->string());
104             return true;
105         }
106         case APIObject::TypeSerializedScriptValue: {
107             WebSerializedScriptValue* scriptValue = static_cast<WebSerializedScriptValue*>(m_root);
108             encoder->encodeVariableLengthByteArray(scriptValue->dataReference());
109             return true;
110         }
111         case APIObject::TypeBoolean: {
112             WebBoolean* booleanObject = static_cast<WebBoolean*>(m_root);
113             encoder->encode(booleanObject->value());
114             return true;
115         }
116         case APIObject::TypeDouble: {
117             WebDouble* doubleObject = static_cast<WebDouble*>(m_root);
118             encoder->encode(doubleObject->value());
119             return true;
120         }
121         case APIObject::TypeUInt64: {
122             WebUInt64* uint64Object = static_cast<WebUInt64*>(m_root);
123             encoder->encode(uint64Object->value());
124             return true;
125         }
126         case APIObject::TypePoint: {
127             WebPoint* pointObject = static_cast<WebPoint*>(m_root);
128             encoder->encode(pointObject->point().x);
129             encoder->encode(pointObject->point().y);
130             return true;
131         }
132         case APIObject::TypeSize: {
133             WebSize* sizeObject = static_cast<WebSize*>(m_root);
134             encoder->encode(sizeObject->size().width);
135             encoder->encode(sizeObject->size().height);
136             return true;
137         }
138         case APIObject::TypeRect: {
139             WebRect* rectObject = static_cast<WebRect*>(m_root);
140             encoder->encode(rectObject->rect().origin.x);
141             encoder->encode(rectObject->rect().origin.y);
142             encoder->encode(rectObject->rect().size.width);
143             encoder->encode(rectObject->rect().size.height);
144             return true;
145         }
146         case APIObject::TypeRenderLayer: {
147             WebRenderLayer* renderLayer = static_cast<WebRenderLayer*>(m_root);
148             encoder->encode(renderLayer->renderObjectName());
149             encoder->encode(renderLayer->elementTagName());
150             encoder->encode(renderLayer->elementID());
151             encoder->encode(Owner(renderLayer->elementClassNames()));
152             encoder->encode(renderLayer->isReflection());
153             encoder->encode(renderLayer->isClipping());
154             encoder->encode(renderLayer->isClipped());
155             encoder->encode(static_cast<uint32_t>(renderLayer->compositingLayerType()));
156             encoder->encode(renderLayer->absoluteBoundingBox());
157             encoder->encode(Owner(renderLayer->negativeZOrderList()));
158             encoder->encode(Owner(renderLayer->normalFlowList()));
159             encoder->encode(Owner(renderLayer->positiveZOrderList()));
160             return true;
161         }
162         case APIObject::TypeRenderObject: {
163             WebRenderObject* renderObject = static_cast<WebRenderObject*>(m_root);
164             encoder->encode(renderObject->name());
165             encoder->encode(renderObject->absolutePosition());
166             encoder->encode(renderObject->frameRect());
167             encoder->encode(Owner(renderObject->children().get()));
168             return true;
169         }
170         case APIObject::TypeURL: {
171             WebURL* urlObject = static_cast<WebURL*>(m_root);
172             encoder->encode(urlObject->string());
173             return true;
174         }
175         case APIObject::TypeURLRequest: {
176             WebURLRequest* urlRequestObject = static_cast<WebURLRequest*>(m_root);
177             encoder->encode(urlRequestObject->resourceRequest());
178             return true;
179         }
180         case APIObject::TypeUserContentURLPattern: {
181             WebUserContentURLPattern* urlPattern = static_cast<WebUserContentURLPattern*>(m_root);
182             encoder->encode(urlPattern->patternString());
183             return true;
184         }
185         case APIObject::TypeImage: {
186             WebImage* image = static_cast<WebImage*>(m_root);
187
188             ShareableBitmap::Handle handle;
189             if (!image->bitmap() || !image->bitmap()->isBackedBySharedMemory() || !image->bitmap()->createHandle(handle)) {
190                 // Initial false indicates no allocated bitmap or is not shareable.
191                 encoder->encode(false);
192                 return true;
193             }
194
195             // Initial true indicates a bitmap was allocated and is shareable.
196             encoder->encode(true);
197
198             encoder->encode(handle);
199             return true;
200         }
201         case APIObject::TypeData: {
202             WebData* data = static_cast<WebData*>(m_root);
203             encoder->encodeVariableLengthByteArray(data->dataReference());
204             return true;
205         }
206         case APIObject::TypeCertificateInfo: {
207             WebCertificateInfo* certificateInfo = static_cast<WebCertificateInfo*>(m_root);
208             encoder->encode(certificateInfo->platformCertificateInfo());
209             return true;
210         }
211         default:
212             break;
213         }
214
215         return false;
216     }
217
218 protected:
219     UserMessageEncoder(APIObject* root) 
220         : m_root(root)
221     {
222     }
223
224     APIObject* m_root;
225 };
226
227
228 // Handles
229 //   - Null -> Null
230 //   - Array -> Array
231 //   - Dictionary -> Dictionary
232 //   - SerializedScriptValue -> SerializedScriptValue
233 //   - String -> String
234 //   - UserContentURLPattern -> UserContentURLPattern
235 //   - WebCertificateInfo -> WebCertificateInfo
236 //   - WebData -> WebData
237 //   - WebDouble -> WebDouble
238 //   - WebImage -> WebImage
239 //   - WebUInt64 -> WebUInt64
240 //   - WebURL -> WebURL
241 //   - WebURLRequest -> WebURLRequest
242
243 template<typename Owner>
244 class UserMessageDecoder {
245 public:
246     static bool baseDecode(CoreIPC::ArgumentDecoder* decoder, Owner& coder, APIObject::Type& type)
247     {
248         uint32_t typeAsUInt32;
249         if (!decoder->decode(typeAsUInt32))
250             return false;
251
252         type = static_cast<APIObject::Type>(typeAsUInt32);
253
254         switch (type) {
255         case APIObject::TypeArray: {
256             uint64_t size;
257             if (!decoder->decode(size))
258                 return false;
259
260             Vector<RefPtr<APIObject> > vector;
261             for (size_t i = 0; i < size; ++i) {
262                 RefPtr<APIObject> element;
263                 Owner messageCoder(coder, element);
264                 if (!decoder->decode(messageCoder))
265                     return false;
266                 vector.append(element.release());
267             }
268
269             coder.m_root = ImmutableArray::adopt(vector);
270             break;
271         }
272         case APIObject::TypeDictionary: {
273             uint64_t size;
274             if (!decoder->decode(size))
275                 return false;
276
277             ImmutableDictionary::MapType map;
278             for (size_t i = 0; i < size; ++i) {
279                 String key;
280                 if (!decoder->decode(key))
281                     return false;
282
283                 RefPtr<APIObject> element;
284                 Owner messageCoder(coder, element);
285                 if (!decoder->decode(messageCoder))
286                     return false;
287
288                 std::pair<ImmutableDictionary::MapType::iterator, bool> result = map.set(key, element.release());
289                 if (!result.second)
290                     return false;
291             }
292
293             coder.m_root = ImmutableDictionary::adopt(map);
294             break;
295         }
296         case APIObject::TypeString: {
297             String string;
298             if (!decoder->decode(string))
299                 return false;
300             coder.m_root = WebString::create(string);
301             break;
302         }
303         case APIObject::TypeSerializedScriptValue: {
304             CoreIPC::DataReference dataReference;
305             if (!decoder->decodeVariableLengthByteArray(dataReference))
306                 return false;
307             
308             Vector<uint8_t> vector = dataReference.vector();
309             coder.m_root = WebSerializedScriptValue::adopt(vector);
310             break;
311         }
312         case APIObject::TypeDouble: {
313             double value;
314             if (!decoder->decode(value))
315                 return false;
316             coder.m_root = WebDouble::create(value);
317             break;
318         }
319         case APIObject::TypeUInt64: {
320             uint64_t value;
321             if (!decoder->decode(value))
322                 return false;
323             coder.m_root = WebUInt64::create(value);
324             break;
325         }
326         case APIObject::TypeBoolean: {
327             bool value;
328             if (!decoder->decode(value))
329                 return false;
330             coder.m_root = WebBoolean::create(value);
331             break;
332         }
333         case APIObject::TypeSize: {
334             double width;
335             double height;
336             if (!decoder->decode(width))
337                 return false;
338             if (!decoder->decode(height))
339                 return false;
340             coder.m_root = WebSize::create(WKSizeMake(width, height));
341             break;
342         }
343         case APIObject::TypePoint: {
344             double x;
345             double y;
346             if (!decoder->decode(x))
347                 return false;
348             if (!decoder->decode(y))
349                 return false;
350             coder.m_root = WebPoint::create(WKPointMake(x, y));
351             break;
352         }
353         case APIObject::TypeRect: {
354             double x;
355             double y;
356             double width;
357             double height;
358             if (!decoder->decode(x))
359                 return false;
360             if (!decoder->decode(y))
361                 return false;
362             if (!decoder->decode(width))
363                 return false;
364             if (!decoder->decode(height))
365                 return false;
366             coder.m_root = WebRect::create(WKRectMake(x, y, width, height));
367             break;
368         }
369         case APIObject::TypeRenderLayer: {
370             String renderObjectName;
371             String elementTagName;
372             String elementID;
373             RefPtr<APIObject> elementClassNames;
374             bool isReflection;
375             bool isClipping;
376             bool isClipped;
377             uint32_t compositingLayerTypeAsUInt32;
378             WebCore::IntRect absoluteBoundingBox;
379             RefPtr<APIObject> negativeZOrderList;
380             RefPtr<APIObject> normalFlowList;
381             RefPtr<APIObject> positiveZOrderList;
382
383             if (!decoder->decode(renderObjectName))
384                 return false;
385             if (!decoder->decode(elementTagName))
386                 return false;
387             if (!decoder->decode(elementID))
388                 return false;
389             Owner classNamesCoder(coder, elementClassNames);
390             if (!decoder->decode(classNamesCoder))
391                 return false;
392             if (!decoder->decodeBool(isReflection))
393                 return false;
394             if (!decoder->decodeBool(isClipping))
395                 return false;
396             if (!decoder->decodeBool(isClipped))
397                 return false;
398             if (!decoder->decodeUInt32(compositingLayerTypeAsUInt32))
399                 return false;
400             if (!decoder->decode(absoluteBoundingBox))
401                 return false;
402             Owner negativeZOrderListCoder(coder, negativeZOrderList);
403             if (!decoder->decode(negativeZOrderListCoder))
404                 return false;
405             Owner normalFlowListCoder(coder, normalFlowList);
406             if (!decoder->decode(normalFlowListCoder))
407                 return false;
408             Owner positiveZOrderListCoder(coder, positiveZOrderList);
409             if (!decoder->decode(positiveZOrderListCoder))
410                 return false;
411             coder.m_root = WebRenderLayer::create(renderObjectName, elementTagName, elementID, static_pointer_cast<MutableArray>(elementClassNames),
412                 isReflection, isClipping, isClipped, static_cast<WebRenderLayer::CompositingLayerType>(compositingLayerTypeAsUInt32),
413                 absoluteBoundingBox, static_pointer_cast<MutableArray>(negativeZOrderList), static_pointer_cast<MutableArray>(normalFlowList),
414                 static_pointer_cast<MutableArray>(positiveZOrderList));
415             break;
416         }
417         case APIObject::TypeRenderObject: {
418             String name;
419             WebCore::IntPoint absolutePosition;
420             WebCore::IntRect frameRect;
421             RefPtr<APIObject> children;
422             
423             if (!decoder->decode(name))
424                 return false;
425             if (!decoder->decode(absolutePosition))
426                 return false;
427             if (!decoder->decode(frameRect))
428                 return false;
429             Owner messageCoder(coder, children);
430             if (!decoder->decode(messageCoder))
431                 return false;
432             if (children->type() != APIObject::TypeArray)
433                 return false;
434             coder.m_root = WebRenderObject::create(name, absolutePosition, frameRect, WTF::static_pointer_cast<MutableArray>(children));
435             break;
436         }
437         case APIObject::TypeURL: {
438             String string;
439             if (!decoder->decode(string))
440                 return false;
441             coder.m_root = WebURL::create(string);
442             break;
443         }
444         case APIObject::TypeURLRequest: {
445             WebCore::ResourceRequest request;
446             if (!decoder->decode(request))
447                 return false;
448             coder.m_root = WebURLRequest::create(request);
449             break;
450         }
451         case APIObject::TypeUserContentURLPattern: {
452             String string;
453             if (!decoder->decode(string))
454                 return false;
455             coder.m_root = WebUserContentURLPattern::create(string);
456             break;
457         }
458         case APIObject::TypeImage: {
459             bool didEncode = false;
460             if (!decoder->decode(didEncode))
461                 return false;
462
463             if (!didEncode)
464                 break;
465
466             ShareableBitmap::Handle handle;
467             if (!decoder->decode(handle))
468                 return false;
469
470             coder.m_root = WebImage::create(ShareableBitmap::create(handle));
471             return true;
472         }
473         case APIObject::TypeData: {
474             CoreIPC::DataReference dataReference;
475             if (!decoder->decodeVariableLengthByteArray(dataReference))
476                 return false;
477             coder.m_root = WebData::create(dataReference.data(), dataReference.size());
478             break;
479         }
480         case APIObject::TypeCertificateInfo: {
481             PlatformCertificateInfo platformCertificateInfo;
482             if (!decoder->decode(platformCertificateInfo))
483                 return false;
484             coder.m_root = WebCertificateInfo::create(platformCertificateInfo);
485             break;
486         }
487         default:
488             break;
489         }
490
491         return true;
492     }
493
494 protected:
495     UserMessageDecoder(RefPtr<APIObject>& root)
496         : m_root(root)
497     {
498     }
499
500     RefPtr<APIObject>& m_root;
501 };
502
503 } // namespace WebKit
504
505 #endif // UserMessageCoders_h