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 "config.h"
27
28#if ENABLE(CONTEXT_MENUS)
29
30#include "WebContextMenuItemData.h"
31
32#include "APIObject.h"
33#include "ArgumentCoders.h"
34#include <wtf/text/CString.h>
35#include <WebCore/ContextMenu.h>
36
37namespace WebKit {
38using namespace WebCore;
39
40WebContextMenuItemData::WebContextMenuItemData()
41 : m_type(WebCore::ActionType)
42 , m_action(WebCore::ContextMenuItemTagNoAction)
43 , m_enabled(true)
44 , m_checked(false)
45{
46}
47
48WebContextMenuItemData::WebContextMenuItemData(WebCore::ContextMenuItemType type, WebCore::ContextMenuAction action, const String& title, bool enabled, bool checked)
49 : m_type(type)
50 , m_action(action)
51 , m_title(title)
52 , m_enabled(enabled)
53 , m_checked(checked)
54{
55 ASSERT(type == WebCore::ActionType || type == WebCore::CheckableActionType || type == WebCore::SeparatorType);
56}
57
58WebContextMenuItemData::WebContextMenuItemData(WebCore::ContextMenuAction action, const String& title, bool enabled, const Vector<WebContextMenuItemData>& submenu)
59 : m_type(WebCore::SubmenuType)
60 , m_action(action)
61 , m_title(title)
62 , m_enabled(enabled)
63 , m_checked(false)
64 , m_submenu(submenu)
65{
66}
67
68WebContextMenuItemData::WebContextMenuItemData(const WebCore::ContextMenuItem& item)
69 : m_type(item.type())
70 , m_action(item.action())
71 , m_title(item.title())
72{
73 if (m_type == WebCore::SubmenuType) {
74 const Vector<WebCore::ContextMenuItem>& coreSubmenu = item.subMenuItems();
75 m_submenu = kitItems(coreSubmenu);
76 }
77
78 m_enabled = item.enabled();
79 m_checked = item.checked();
80}
81
82ContextMenuItem WebContextMenuItemData::core() const
83{
84 if (m_type != SubmenuType)
85 return ContextMenuItem(m_type, m_action, m_title, m_enabled, m_checked);
86
87 Vector<ContextMenuItem> subMenuItems = coreItems(m_submenu);
88 return ContextMenuItem(m_action, m_title, m_enabled, m_checked, subMenuItems);
89}
90
91API::Object* WebContextMenuItemData::userData() const
92{
93 return m_userData.get();
94}
95
96void WebContextMenuItemData::setUserData(API::Object* userData)
97{
98 m_userData = userData;
99}
100
101void WebContextMenuItemData::encode(IPC::Encoder& encoder) const
102{
103 encoder.encodeEnum(m_type);
104 encoder.encodeEnum(m_action);
105 encoder << m_title;
106 encoder << m_checked;
107 encoder << m_enabled;
108 encoder << m_submenu;
109}
110
111Optional<WebContextMenuItemData> WebContextMenuItemData::decode(IPC::Decoder& decoder)
112{
113 WebCore::ContextMenuItemType type;
114 if (!decoder.decodeEnum(type))
115 return WTF::nullopt;
116
117 WebCore::ContextMenuAction action;
118 if (!decoder.decodeEnum(action))
119 return WTF::nullopt;
120
121 String title;
122 if (!decoder.decode(title))
123 return WTF::nullopt;
124
125 bool checked;
126 if (!decoder.decode(checked))
127 return WTF::nullopt;
128
129 bool enabled;
130 if (!decoder.decode(enabled))
131 return WTF::nullopt;
132
133 Optional<Vector<WebContextMenuItemData>> submenu;
134 decoder >> submenu;
135 if (!submenu)
136 return WTF::nullopt;
137
138 switch (type) {
139 case WebCore::ActionType:
140 case WebCore::SeparatorType:
141 case WebCore::CheckableActionType:
142 return {{ type, action, title, enabled, checked }};
143 break;
144 case WebCore::SubmenuType:
145 return {{ action, title, enabled, WTFMove(*submenu) }};
146 break;
147 }
148 ASSERT_NOT_REACHED();
149 return WTF::nullopt;
150}
151
152Vector<WebContextMenuItemData> kitItems(const Vector<WebCore::ContextMenuItem>& coreItemVector)
153{
154 Vector<WebContextMenuItemData> result;
155 result.reserveCapacity(coreItemVector.size());
156 for (unsigned i = 0; i < coreItemVector.size(); ++i)
157 result.append(WebContextMenuItemData(coreItemVector[i]));
158
159 return result;
160}
161
162Vector<ContextMenuItem> coreItems(const Vector<WebContextMenuItemData>& kitItemVector)
163{
164 Vector<ContextMenuItem> result;
165 result.reserveCapacity(kitItemVector.size());
166 for (unsigned i = 0; i < kitItemVector.size(); ++i)
167 result.append(kitItemVector[i].core());
168
169 return result;
170}
171
172} // namespace WebKit
173#endif // ENABLE(CONTEXT_MENUS)
174