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