2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "WebContextMenuItemData.h"
29 #include "APIObject.h"
30 #include "ArgumentCoders.h"
31 #include "Arguments.h"
32 #include <wtf/text/CString.h>
33 #include <WebCore/ContextMenu.h>
35 using namespace WebCore;
39 WebContextMenuItemData::WebContextMenuItemData()
40 : m_type(WebCore::ActionType)
41 , m_action(WebCore::ContextMenuItemTagNoAction)
47 WebContextMenuItemData::WebContextMenuItemData(WebCore::ContextMenuItemType type, WebCore::ContextMenuAction action, const String& title, bool enabled, bool checked)
54 ASSERT(type == WebCore::ActionType || type == WebCore::CheckableActionType || type == WebCore::SeparatorType);
57 WebContextMenuItemData::WebContextMenuItemData(WebCore::ContextMenuAction action, const String& title, bool enabled, const Vector<WebContextMenuItemData>& submenu)
58 : m_type(WebCore::SubmenuType)
67 WebContextMenuItemData::WebContextMenuItemData(const WebCore::ContextMenuItem& item, WebCore::ContextMenu* menu)
69 , m_action(item.action())
70 , m_title(item.title())
72 if (m_type == WebCore::SubmenuType) {
73 #if USE(CROSS_PLATFORM_CONTEXT_MENUS)
74 const Vector<WebCore::ContextMenuItem>& coreSubmenu = item.subMenuItems();
76 Vector<WebCore::ContextMenuItem> coreSubmenu = WebCore::contextMenuItemVector(item.platformSubMenu());
78 m_submenu = kitItems(coreSubmenu, menu);
81 m_enabled = item.enabled();
82 m_checked = item.checked();
85 ContextMenuItem WebContextMenuItemData::core() const
87 if (m_type != SubmenuType)
88 return ContextMenuItem(m_type, m_action, m_title, m_enabled, m_checked);
90 Vector<ContextMenuItem> subMenuItems = coreItems(m_submenu);
91 return ContextMenuItem(m_action, m_title, m_enabled, m_checked, subMenuItems);
94 APIObject* WebContextMenuItemData::userData() const
96 return m_userData.get();
99 void WebContextMenuItemData::setUserData(APIObject* userData)
101 m_userData = userData;
104 void WebContextMenuItemData::encode(CoreIPC::ArgumentEncoder* encoder) const
106 encoder->encode(CoreIPC::In(static_cast<uint32_t>(m_type), static_cast<uint32_t>(m_action), m_title, m_checked, m_enabled, m_submenu));
109 bool WebContextMenuItemData::decode(CoreIPC::ArgumentDecoder* decoder, WebContextMenuItemData& item)
116 Vector<WebContextMenuItemData> submenu;
118 if (!decoder->decode(CoreIPC::Out(type, action, title, checked, enabled, submenu)))
122 case WebCore::ActionType:
123 case WebCore::SeparatorType:
124 case WebCore::CheckableActionType:
125 item = WebContextMenuItemData(static_cast<WebCore::ContextMenuItemType>(type), static_cast<WebCore::ContextMenuAction>(action), title, enabled, checked);
127 case WebCore::SubmenuType:
128 item = WebContextMenuItemData(static_cast<WebCore::ContextMenuAction>(action), title, enabled, submenu);
131 ASSERT_NOT_REACHED();
138 Vector<WebContextMenuItemData> kitItems(const Vector<WebCore::ContextMenuItem>& coreItemVector, WebCore::ContextMenu* menu)
140 Vector<WebContextMenuItemData> result;
141 result.reserveCapacity(coreItemVector.size());
142 for (unsigned i = 0; i < coreItemVector.size(); ++i)
143 result.append(WebContextMenuItemData(coreItemVector[i], menu));
148 Vector<ContextMenuItem> coreItems(const Vector<WebContextMenuItemData>& kitItemVector)
150 Vector<ContextMenuItem> result;
151 result.reserveCapacity(kitItemVector.size());
152 for (unsigned i = 0; i < kitItemVector.size(); ++i)
153 result.append(kitItemVector[i].core());
158 } // namespace WebKit