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
36#if PLATFORM(COCOA)
37#include "ObjCObjectGraphCoders.h"
38#endif
39
40namespace WebKit {
41
42// Adds
43// - BundlePage -> Page
44// - BundleFrame -> Frame
45// - BundlePageGroup -> PageGroup
46
47class InjectedBundleUserMessageEncoder : public UserMessageEncoder<InjectedBundleUserMessageEncoder> {
48public:
49    typedef UserMessageEncoder<InjectedBundleUserMessageEncoder> Base;
50
51    InjectedBundleUserMessageEncoder(API::Object* root)
52        : Base(root)
53    {
54    }
55
56    InjectedBundleUserMessageEncoder(const InjectedBundleUserMessageEncoder&, API::Object* root)
57        : Base(root)
58    {
59    }
60
61    void encode(IPC::ArgumentEncoder& encoder) const
62    {
63        API::Object::Type type = API::Object::Type::Null;
64        if (baseEncode(encoder, *this, type))
65            return;
66
67        switch (type) {
68        case API::Object::Type::BundlePage: {
69            WebPage* page = static_cast<WebPage*>(m_root);
70            encoder << page->pageID();
71            break;
72        }
73        case API::Object::Type::BundleFrame: {
74            WebFrame* frame = static_cast<WebFrame*>(m_root);
75            encoder << frame->frameID();
76            break;
77        }
78        case API::Object::Type::BundlePageGroup: {
79            WebPageGroupProxy* pageGroup = static_cast<WebPageGroupProxy*>(m_root);
80            encoder << pageGroup->pageGroupID();
81            break;
82        }
83#if PLATFORM(COCOA)
84        case API::Object::Type::ObjCObjectGraph: {
85            ObjCObjectGraph* objectGraph = static_cast<ObjCObjectGraph*>(m_root);
86            encoder << InjectedBundleObjCObjectGraphEncoder(objectGraph, WebProcess::shared());
87            break;
88        }
89#endif
90        default:
91            ASSERT_NOT_REACHED();
92            break;
93        }
94    }
95};
96
97// Adds
98//   - Page -> BundlePage
99//   - Frame -> BundleFrame
100//   - PageGroup -> BundlePageGroup
101
102class InjectedBundleUserMessageDecoder : public UserMessageDecoder<InjectedBundleUserMessageDecoder> {
103public:
104    typedef UserMessageDecoder<InjectedBundleUserMessageDecoder> Base;
105
106    InjectedBundleUserMessageDecoder(RefPtr<API::Object>& root)
107        : Base(root)
108    {
109    }
110
111    InjectedBundleUserMessageDecoder(InjectedBundleUserMessageDecoder&, RefPtr<API::Object>& root)
112        : Base(root)
113    {
114    }
115
116    static bool decode(IPC::ArgumentDecoder& decoder, InjectedBundleUserMessageDecoder& coder)
117    {
118        API::Object::Type type = API::Object::Type::Null;
119        if (!Base::baseDecode(decoder, coder, type))
120            return false;
121
122        if (coder.m_root || type == API::Object::Type::Null)
123            return true;
124
125        switch (type) {
126        case API::Object::Type::Page: {
127            uint64_t pageID;
128            if (!decoder.decode(pageID))
129                return false;
130            coder.m_root = WebProcess::shared().webPage(pageID);
131            break;
132        }
133        case API::Object::Type::Frame: {
134            uint64_t frameID;
135            if (!decoder.decode(frameID))
136                return false;
137            coder.m_root = WebProcess::shared().webFrame(frameID);
138            break;
139        }
140        case API::Object::Type::PageGroup: {
141            WebPageGroupData pageGroupData;
142            if (!decoder.decode(pageGroupData))
143                return false;
144            coder.m_root = WebProcess::shared().webPageGroup(pageGroupData);
145            break;
146        }
147#if PLATFORM(COCOA)
148        case API::Object::Type::ObjCObjectGraph: {
149            RefPtr<ObjCObjectGraph> objectGraph;
150            InjectedBundleObjCObjectGraphDecoder objectGraphDecoder(objectGraph, WebProcess::shared());
151            if (!decoder.decode(objectGraphDecoder))
152                return false;
153            coder.m_root = objectGraph.get();
154            break;
155        }
156#endif
157        default:
158            return false;
159        }
160
161        return true;
162    }
163};
164
165} // namespace WebKit
166
167#endif // InjectedBundleUserMessageCoders_h
168