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 MessageID_h
27#define MessageID_h
28
29namespace CoreIPC {
30
31enum MessageClass {
32    MessageClassReserved = 0,
33
34    // Messages sent by Core IPC.
35    MessageClassCoreIPC,
36
37    // Messages sent by the UI process to the web process.
38    MessageClassAuthenticationManager,
39    MessageClassDrawingArea,
40    MessageClassDrawingAreaLegacy,
41    MessageClassInjectedBundle,
42    MessageClassWebApplicationCacheManager,
43    MessageClassWebCookieManager,
44    MessageClassWebDatabaseManager,
45    MessageClassWebFullScreenManager,
46    MessageClassWebGeolocationManagerProxy,
47    MessageClassWebIconDatabaseProxy,
48    MessageClassWebInspector,
49    MessageClassWebKeyValueStorageManager,
50    MessageClassWebMediaCacheManager,
51    MessageClassWebPage,
52    MessageClassWebProcess,
53    MessageClassWebResourceCacheManager,
54
55    // Messages sent by the web process to the UI process.
56    MessageClassDownloadProxy,
57    MessageClassDrawingAreaProxy,
58    MessageClassDrawingAreaProxyLegacy,
59    MessageClassWebApplicationCacheManagerProxy,
60    MessageClassWebContext,
61    MessageClassWebContextLegacy,
62    MessageClassWebCookieManagerProxy,
63    MessageClassWebDatabaseManagerProxy,
64    MessageClassWebFullScreenManagerProxy,
65    MessageClassWebGeolocationManager,
66    MessageClassWebIconDatabase,
67    MessageClassWebInspectorProxy,
68    MessageClassWebKeyValueStorageManagerProxy,
69    MessageClassWebMediaCacheManagerProxy,
70    MessageClassWebPageProxy,
71    MessageClassWebProcessProxy,
72    MessageClassWebResourceCacheManagerProxy,
73
74    // Messages sent by the UI process to the plug-in process.
75    MessageClassPluginProcess,
76
77    // Messages sent by the plug-in process to the UI process.
78    MessageClassPluginProcessProxy,
79
80    // Messages sent by the web process to the plug-in process.
81    MessageClassWebProcessConnection,
82    MessageClassPluginControllerProxy,
83
84    // Messages sent by the plug-in process to the web process.
85    MessageClassPluginProxy,
86
87    // NPObject messages sent by both the plug-in process and the web process.
88    MessageClassNPObjectMessageReceiver,
89};
90
91template<typename> struct MessageKindTraits { };
92
93
94/*
95    MessageID Layout (the most significant bit is reserved and therefore always zero)
96
97    ---------
98    | Flags | 7 bits
99    |-------|
100    | Class | 8 bits
101    |-------|
102    |  Msg  | 16 bits
103    |  Kind |
104    ---------
105*/
106
107class MessageID {
108public:
109    enum Flags {
110        SyncMessage = 1 << 0,
111        DispatchMessageWhenWaitingForSyncReply = 1 << 1,
112    };
113
114    MessageID()
115        : m_messageID(0)
116    {
117    }
118
119    template <typename EnumType>
120    explicit MessageID(EnumType messageKind, unsigned char flags = 0)
121        : m_messageID(stripMostSignificantBit(flags << 24 | (MessageKindTraits<EnumType>::messageClass) << 16 | messageKind))
122    {
123    }
124
125    MessageID messageIDWithAddedFlags(unsigned char flags)
126    {
127        MessageID messageID;
128
129        messageID.m_messageID = stripMostSignificantBit(m_messageID | (flags << 24));
130        return messageID;
131    }
132
133    template <typename EnumType>
134    EnumType get() const
135    {
136        ASSERT(getClass() == MessageKindTraits<EnumType>::messageClass);
137        return static_cast<EnumType>(m_messageID & 0xffff);
138    }
139
140    template <MessageClass K>
141    bool is() const
142    {
143        return getClass() == K;
144    }
145
146    template <typename EnumType>
147    bool operator==(EnumType messageKind) const
148    {
149        return m_messageID == MessageID(messageKind).m_messageID;
150    }
151
152    static MessageID fromInt(unsigned i)
153    {
154        MessageID messageID;
155        messageID.m_messageID = stripMostSignificantBit(i);
156
157        return messageID;
158    }
159
160    unsigned toInt() const { return m_messageID; }
161
162    bool shouldDispatchMessageWhenWaitingForSyncReply() const { return getFlags() & DispatchMessageWhenWaitingForSyncReply; }
163    bool isSync() const { return getFlags() & SyncMessage; }
164
165private:
166    static inline unsigned stripMostSignificantBit(unsigned value)
167    {
168        return value & 0x7fffffff;
169    }
170
171    unsigned char getFlags() const { return (m_messageID & 0xff000000) >> 24; }
172    unsigned char getClass() const { return (m_messageID & 0x00ff0000) >> 16; }
173
174    unsigned m_messageID;
175};
176
177} // namespace CoreIPC
178
179#endif // MessageID_h
180