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 #include "UserMessageCoders.h"
27 #include "WebContext.h"
28 #include "WebFrameProxy.h"
29 #include "WebPageGroup.h"
30 #include "WebPageGroupData.h"
31 #include "WebPageProxy.h"
32 
33 namespace WebKit {
34 
35 // Adds
36 // - Page -> BundlePage
37 // - Frame -> BundleFrame
38 // - PageGroup -> BundlePageGroup
39 
40 class WebContextUserMessageEncoder : public UserMessageEncoder<WebContextUserMessageEncoder> {
41 public:
42     typedef UserMessageEncoder<WebContextUserMessageEncoder> Base;
43 
WebContextUserMessageEncoder(APIObject * root)44     WebContextUserMessageEncoder(APIObject* root)
45         : Base(root)
46     {
47     }
48 
encode(CoreIPC::ArgumentEncoder * encoder)49     void encode(CoreIPC::ArgumentEncoder* encoder) const
50     {
51         APIObject::Type type = APIObject::TypeNull;
52         if (baseEncode(encoder, type))
53             return;
54 
55         switch (type) {
56         case APIObject::TypePage: {
57             WebPageProxy* page = static_cast<WebPageProxy*>(m_root);
58             encoder->encode(page->pageID());
59             break;
60         }
61         case APIObject::TypeFrame: {
62             WebFrameProxy* frame = static_cast<WebFrameProxy*>(m_root);
63             encoder->encode(frame->frameID());
64             break;
65         }
66         case APIObject::TypePageGroup: {
67             WebPageGroup* pageGroup = static_cast<WebPageGroup*>(m_root);
68             encoder->encode(pageGroup->data());
69             break;
70         }
71         default:
72             ASSERT_NOT_REACHED();
73             break;
74         }
75     }
76 };
77 
78 // Adds
79 //   - Page -> BundlePage
80 //   - Frame -> BundleFrame
81 //   - PageGroup -> BundlePageGroup
82 
83 class WebContextUserMessageDecoder : public UserMessageDecoder<WebContextUserMessageDecoder> {
84 public:
85     typedef UserMessageDecoder<WebContextUserMessageDecoder> Base;
86 
WebContextUserMessageDecoder(RefPtr<APIObject> & root,WebContext * context)87     WebContextUserMessageDecoder(RefPtr<APIObject>& root, WebContext* context)
88         : Base(root)
89         , m_context(context)
90     {
91     }
92 
WebContextUserMessageDecoder(WebContextUserMessageDecoder & userMessageDecoder,RefPtr<APIObject> & root)93     WebContextUserMessageDecoder(WebContextUserMessageDecoder& userMessageDecoder, RefPtr<APIObject>& root)
94         : Base(root)
95         , m_context(userMessageDecoder.m_context)
96     {
97     }
98 
decode(CoreIPC::ArgumentDecoder * decoder,WebContextUserMessageDecoder & coder)99     static bool decode(CoreIPC::ArgumentDecoder* decoder, WebContextUserMessageDecoder& coder)
100     {
101         APIObject::Type type = APIObject::TypeNull;
102         if (!Base::baseDecode(decoder, coder, type))
103             return false;
104 
105         if (coder.m_root || type == APIObject::TypeNull)
106             return true;
107 
108         switch (type) {
109         case APIObject::TypeBundlePage: {
110             uint64_t pageID;
111             if (!decoder->decode(pageID))
112                 return false;
113             coder.m_root = coder.m_context->process()->webPage(pageID);
114             break;
115         }
116         case APIObject::TypeBundleFrame: {
117             uint64_t frameID;
118             if (!decoder->decode(frameID))
119                 return false;
120             coder.m_root = coder.m_context->process()->webFrame(frameID);
121             break;
122         }
123         case APIObject::TypeBundlePageGroup: {
124             uint64_t pageGroupID;
125             if (!decoder->decode(pageGroupID))
126                 return false;
127             coder.m_root = WebPageGroup::get(pageGroupID);
128             break;
129         }
130         default:
131             return false;
132         }
133 
134         return true;
135     }
136 
137 private:
138     WebContext* m_context;
139 };
140 
141 } // namespace WebKit
142