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 NPVariantData_h
27#define NPVariantData_h
28
29#if ENABLE(PLUGIN_PROCESS)
30
31#include <wtf/text/CString.h>
32
33namespace CoreIPC {
34    class ArgumentDecoder;
35    class ArgumentEncoder;
36}
37
38namespace WebKit {
39
40// The CoreIPC representation of an NPVariant.
41
42class NPVariantData {
43public:
44    enum Type {
45        Void,
46        Null,
47        Bool,
48        Int32,
49        Double,
50        String,
51        LocalNPObjectID,
52        RemoteNPObjectID,
53    };
54    NPVariantData();
55
56    static NPVariantData makeVoid();
57    static NPVariantData makeNull();
58    static NPVariantData makeBool(bool value);
59    static NPVariantData makeInt32(int32_t value);
60    static NPVariantData makeDouble(double value);
61    static NPVariantData makeString(const char* string, unsigned length);
62    static NPVariantData makeLocalNPObjectID(uint64_t value);
63    static NPVariantData makeRemoteNPObjectID(uint64_t value);
64
65    Type type() const { return static_cast<Type>(m_type); }
66
67    bool boolValue() const
68    {
69        ASSERT(type() == NPVariantData::Bool);
70        return m_boolValue;
71    }
72
73    int32_t int32Value() const
74    {
75        ASSERT(type() == NPVariantData::Int32);
76        return m_int32Value;
77    }
78
79    double doubleValue() const
80    {
81        ASSERT(type() == NPVariantData::Double);
82        return m_doubleValue;
83    }
84
85    const CString& stringValue() const
86    {
87        ASSERT(type() == NPVariantData::String);
88        return m_stringValue;
89    }
90
91    uint64_t localNPObjectIDValue() const
92    {
93        ASSERT(type() == NPVariantData::LocalNPObjectID);
94        return m_localNPObjectIDValue;
95    }
96
97    uint64_t remoteNPObjectIDValue() const
98    {
99        ASSERT(type() == NPVariantData::RemoteNPObjectID);
100        return m_remoteNPObjectIDValue;
101    }
102
103    void encode(CoreIPC::ArgumentEncoder*) const;
104    static bool decode(CoreIPC::ArgumentDecoder*, NPVariantData&);
105
106private:
107    uint32_t m_type;
108    bool m_boolValue;
109    int32_t m_int32Value;
110    double m_doubleValue;
111    CString m_stringValue;
112    uint64_t m_localNPObjectIDValue;
113    uint64_t m_remoteNPObjectIDValue;
114};
115
116} // namespace WebKit
117
118#endif // ENABLE(PLUGIN_PROCESS)
119
120#endif // NPVariantData_h
121