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#include "WKContextMenuItem.h"
28
29#include "MutableArray.h"
30#include "WebContextMenuItem.h"
31#include "WebContextMenuItemData.h"
32#include "WKAPICast.h"
33#include "WKContextMenuItemTypes.h"
34
35#if PLATFORM(MAC)
36#import <mach-o/dyld.h>
37#endif
38
39using namespace WebCore;
40using namespace WebKit;
41
42WKTypeID WKContextMenuItemGetTypeID()
43{
44#if ENABLE(CONTEXT_MENUS)
45    return toAPI(WebContextMenuItem::APIType);
46#else
47    return toAPI(APIObject::TypeNull);
48#endif
49}
50
51WKContextMenuItemRef WKContextMenuItemCreateAsAction(WKContextMenuItemTag tag, WKStringRef title, bool enabled)
52{
53#if ENABLE(CONTEXT_MENUS)
54    return toAPI(WebContextMenuItem::create(WebContextMenuItemData(ActionType, toImpl(tag), toImpl(title)->string(), enabled, false)).leakRef());
55#else
56    return 0;
57#endif
58}
59
60WKContextMenuItemRef WKContextMenuItemCreateAsCheckableAction(WKContextMenuItemTag tag, WKStringRef title, bool enabled, bool checked)
61{
62#if ENABLE(CONTEXT_MENUS)
63    return toAPI(WebContextMenuItem::create(WebContextMenuItemData(CheckableActionType, toImpl(tag), toImpl(title)->string(), enabled, checked)).leakRef());
64#else
65    return 0;
66#endif
67}
68
69WKContextMenuItemRef WKContextMenuItemCreateAsSubmenu(WKStringRef title, bool enabled, WKArrayRef submenuItems)
70{
71#if ENABLE(CONTEXT_MENUS)
72    return toAPI(WebContextMenuItem::create(toImpl(title)->string(), enabled, toImpl(submenuItems)).leakRef());
73#else
74    return 0;
75#endif
76}
77
78WKContextMenuItemRef WKContextMenuItemSeparatorItem()
79{
80#if ENABLE(CONTEXT_MENUS)
81    return toAPI(WebContextMenuItem::separatorItem());
82#else
83    return 0;
84#endif
85}
86
87#if PLATFORM(MAC)
88static WKContextMenuItemTag compatibleContextMenuItemTag(WKContextMenuItemTag tag)
89{
90    static bool needsWorkaround = ^bool {
91        const int32_t safariFrameworkVersionWithIncompatibleContextMenuItemTags = 0x02181900; // 536.25.0 (Safari 6.0)
92        return NSVersionOfRunTimeLibrary("Safari") == safariFrameworkVersionWithIncompatibleContextMenuItemTags;
93    }();
94
95    if (!needsWorkaround)
96        return tag;
97
98    // kWKContextMenuItemTagDictationAlternative was inserted before kWKContextMenuItemTagInspectElement.
99    // DictationAlternative is now at the end like it should have been. To be compatible we need to return
100    // InspectElement for DictationAlternative and shift InspectElement and after by one.
101    if (tag == kWKContextMenuItemTagDictationAlternative)
102        return kWKContextMenuItemTagInspectElement;
103    if (tag >= kWKContextMenuItemTagInspectElement && tag < kWKContextMenuItemBaseApplicationTag)
104        return tag + 1;
105    return tag;
106}
107#endif
108
109WKContextMenuItemTag WKContextMenuItemGetTag(WKContextMenuItemRef itemRef)
110{
111#if ENABLE(CONTEXT_MENUS)
112#if PLATFORM(MAC)
113    return compatibleContextMenuItemTag(toAPI(toImpl(itemRef)->data()->action()));
114#else
115    return toAPI(toImpl(itemRef)->data()->action());
116#endif
117#else
118    return toAPI(ContextMenuItemTagNoAction);
119#endif
120}
121
122WKContextMenuItemType WKContextMenuItemGetType(WKContextMenuItemRef itemRef)
123{
124#if ENABLE(CONTEXT_MENUS)
125    return toAPI(toImpl(itemRef)->data()->type());
126#else
127    return toAPI(ActionType);
128#endif
129}
130
131WKStringRef WKContextMenuItemCopyTitle(WKContextMenuItemRef itemRef)
132{
133#if ENABLE(CONTEXT_MENUS)
134    return toCopiedAPI(toImpl(itemRef)->data()->title().impl());
135#else
136    return 0;
137#endif
138}
139
140bool WKContextMenuItemGetEnabled(WKContextMenuItemRef itemRef)
141{
142#if ENABLE(CONTEXT_MENUS)
143    return toImpl(itemRef)->data()->enabled();
144#else
145    return false;
146#endif
147}
148
149bool WKContextMenuItemGetChecked(WKContextMenuItemRef itemRef)
150{
151#if ENABLE(CONTEXT_MENUS)
152    return toImpl(itemRef)->data()->checked();
153#else
154    return false;
155#endif
156}
157
158WKArrayRef WKContextMenuCopySubmenuItems(WKContextMenuItemRef itemRef)
159{
160#if ENABLE(CONTEXT_MENUS)
161    return toAPI(toImpl(itemRef)->submenuItemsAsImmutableArray().leakRef());
162#else
163    return 0;
164#endif
165}
166
167WKTypeRef WKContextMenuItemGetUserData(WKContextMenuItemRef itemRef)
168{
169#if ENABLE(CONTEXT_MENUS)
170    return toAPI(toImpl(itemRef)->userData());
171#else
172    return 0;
173#endif
174}
175
176void WKContextMenuItemSetUserData(WKContextMenuItemRef itemRef, WKTypeRef userDataRef)
177{
178#if ENABLE(CONTEXT_MENUS)
179    toImpl(itemRef)->setUserData(toImpl(userDataRef));
180#endif
181}
182