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 WKSharedAPICast_h
27#define WKSharedAPICast_h
28
29#include "ImageOptions.h"
30#include "SameDocumentNavigationType.h"
31#include "WKBase.h"
32#include "WKContextMenuItemTypes.h"
33#include "WKEvent.h"
34#include "WKFindOptions.h"
35#include "WKGeometry.h"
36#include "WKImage.h"
37#include "WKPageLoadTypes.h"
38#include "WKPageLoadTypesPrivate.h"
39#include "WKPageVisibilityTypes.h"
40#include "WebError.h"
41#include "WebEvent.h"
42#include "WebFindOptions.h"
43#include "WebNumber.h"
44#include "WebSecurityOrigin.h"
45#include "WebString.h"
46#include "WebURL.h"
47#include "WebURLRequest.h"
48#include "WebURLResponse.h"
49#include <WebCore/ContextMenuItem.h>
50#include <WebCore/FloatRect.h>
51#include <WebCore/FrameLoaderTypes.h>
52#include <WebCore/IntRect.h>
53#include <WebCore/LayoutMilestones.h>
54#include <WebCore/PageVisibilityState.h>
55#include <WebCore/SecurityOrigin.h>
56#include <WebCore/UserContentTypes.h>
57#include <WebCore/UserScriptTypes.h>
58#include <wtf/TypeTraits.h>
59
60namespace WebKit {
61
62class ImmutableArray;
63class ImmutableDictionary;
64class MutableArray;
65class MutableDictionary;
66class ObjCObjectGraph;
67class WebArchive;
68class WebArchiveResource;
69class WebCertificateInfo;
70class WebConnection;
71class WebContextMenuItem;
72class WebData;
73class WebGraphicsContext;
74class WebImage;
75class WebPoint;
76class WebRect;
77class WebSecurityOrigin;
78class WebSerializedScriptValue;
79class WebSize;
80class WebURLRequest;
81class WebURLResponse;
82class WebUserContentURLPattern;
83
84template<typename APIType> struct APITypeInfo { };
85template<typename ImplType> struct ImplTypeInfo { };
86
87#define WK_ADD_API_MAPPING(TheAPIType, TheImplType) \
88    template<> struct APITypeInfo<TheAPIType> { typedef TheImplType* ImplType; }; \
89    template<> struct ImplTypeInfo<TheImplType*> { typedef TheAPIType APIType; };
90
91WK_ADD_API_MAPPING(WKArrayRef, ImmutableArray)
92WK_ADD_API_MAPPING(WKBooleanRef, WebBoolean)
93WK_ADD_API_MAPPING(WKCertificateInfoRef, WebCertificateInfo)
94WK_ADD_API_MAPPING(WKConnectionRef, WebConnection)
95WK_ADD_API_MAPPING(WKContextMenuItemRef, WebContextMenuItem)
96WK_ADD_API_MAPPING(WKDataRef, WebData)
97WK_ADD_API_MAPPING(WKDictionaryRef, ImmutableDictionary)
98WK_ADD_API_MAPPING(WKDoubleRef, WebDouble)
99WK_ADD_API_MAPPING(WKErrorRef, WebError)
100WK_ADD_API_MAPPING(WKGraphicsContextRef, WebGraphicsContext)
101WK_ADD_API_MAPPING(WKImageRef, WebImage)
102WK_ADD_API_MAPPING(WKMutableArrayRef, MutableArray)
103WK_ADD_API_MAPPING(WKMutableDictionaryRef, MutableDictionary)
104WK_ADD_API_MAPPING(WKPointRef, WebPoint)
105WK_ADD_API_MAPPING(WKRectRef, WebRect)
106WK_ADD_API_MAPPING(WKSecurityOriginRef, WebSecurityOrigin)
107WK_ADD_API_MAPPING(WKSerializedScriptValueRef, WebSerializedScriptValue)
108WK_ADD_API_MAPPING(WKSizeRef, WebSize)
109WK_ADD_API_MAPPING(WKStringRef, WebString)
110WK_ADD_API_MAPPING(WKTypeRef, APIObject)
111WK_ADD_API_MAPPING(WKUInt64Ref, WebUInt64)
112WK_ADD_API_MAPPING(WKURLRef, WebURL)
113WK_ADD_API_MAPPING(WKURLRequestRef, WebURLRequest)
114WK_ADD_API_MAPPING(WKURLResponseRef, WebURLResponse)
115WK_ADD_API_MAPPING(WKUserContentURLPatternRef, WebUserContentURLPattern)
116
117#if PLATFORM(MAC)
118WK_ADD_API_MAPPING(WKWebArchiveRef, WebArchive)
119WK_ADD_API_MAPPING(WKWebArchiveResourceRef, WebArchiveResource)
120WK_ADD_API_MAPPING(WKObjCTypeWrapperRef, ObjCObjectGraph)
121#endif
122
123template<typename ImplType, typename APIType = typename ImplTypeInfo<ImplType*>::APIType>
124class ProxyingRefPtr {
125public:
126    ProxyingRefPtr(PassRefPtr<ImplType> impl)
127        : m_impl(impl)
128    {
129    }
130
131    operator APIType() { return toAPI(m_impl.get()); }
132
133private:
134    RefPtr<ImplType> m_impl;
135};
136
137/* Opaque typing convenience methods */
138
139template<typename T>
140inline typename APITypeInfo<T>::ImplType toImpl(T t)
141{
142    // An example of the conversions that take place:
143    // const struct OpaqueWKArray* -> const struct OpaqueWKArray -> struct OpaqueWKArray -> struct OpaqueWKArray* -> ImmutableArray*
144
145    typedef typename WTF::RemovePointer<T>::Type PotentiallyConstValueType;
146    typedef typename WTF::RemoveConst<PotentiallyConstValueType>::Type NonConstValueType;
147
148    return reinterpret_cast<typename APITypeInfo<T>::ImplType>(const_cast<NonConstValueType*>(t));
149}
150
151template<typename T>
152inline typename ImplTypeInfo<T>::APIType toAPI(T t)
153{
154    return reinterpret_cast<typename ImplTypeInfo<T>::APIType>(t);
155}
156
157/* Special cases. */
158
159inline ProxyingRefPtr<WebString> toAPI(StringImpl* string)
160{
161    return ProxyingRefPtr<WebString>(WebString::create(string));
162}
163
164inline WKStringRef toCopiedAPI(const String& string)
165{
166    RefPtr<WebString> webString = WebString::create(string);
167    return toAPI(webString.release().leakRef());
168}
169
170inline ProxyingRefPtr<WebURL> toURLRef(StringImpl* string)
171{
172    if (!string)
173        return ProxyingRefPtr<WebURL>(0);
174    return ProxyingRefPtr<WebURL>(WebURL::create(String(string)));
175}
176
177inline WKURLRef toCopiedURLAPI(const String& string)
178{
179    if (!string)
180        return 0;
181    RefPtr<WebURL> webURL = WebURL::create(string);
182    return toAPI(webURL.release().leakRef());
183}
184
185inline String toWTFString(WKStringRef stringRef)
186{
187    if (!stringRef)
188        return String();
189    return toImpl(stringRef)->string();
190}
191
192inline String toWTFString(WKURLRef urlRef)
193{
194    if (!urlRef)
195        return String();
196    return toImpl(urlRef)->string();
197}
198
199inline ProxyingRefPtr<WebError> toAPI(const WebCore::ResourceError& error)
200{
201    return ProxyingRefPtr<WebError>(WebError::create(error));
202}
203
204inline ProxyingRefPtr<WebURLRequest> toAPI(const WebCore::ResourceRequest& request)
205{
206    return ProxyingRefPtr<WebURLRequest>(WebURLRequest::create(request));
207}
208
209inline ProxyingRefPtr<WebURLResponse> toAPI(const WebCore::ResourceResponse& response)
210{
211    return ProxyingRefPtr<WebURLResponse>(WebURLResponse::create(response));
212}
213
214inline WKSecurityOriginRef toCopiedAPI(WebCore::SecurityOrigin* origin)
215{
216    if (!origin)
217        return 0;
218    return toAPI(WebSecurityOrigin::create(origin).leakRef());
219}
220
221/* Geometry conversions */
222
223inline WebCore::FloatRect toFloatRect(const WKRect& wkRect)
224{
225    return WebCore::FloatRect(static_cast<float>(wkRect.origin.x), static_cast<float>(wkRect.origin.y),
226                              static_cast<float>(wkRect.size.width), static_cast<float>(wkRect.size.height));
227}
228
229inline WebCore::IntSize toIntSize(const WKSize& wkSize)
230{
231    return WebCore::IntSize(static_cast<int>(wkSize.width), static_cast<int>(wkSize.height));
232}
233
234inline WebCore::IntPoint toIntPoint(const WKPoint& wkPoint)
235{
236    return WebCore::IntPoint(static_cast<int>(wkPoint.x), static_cast<int>(wkPoint.y));
237}
238
239inline WebCore::IntRect toIntRect(const WKRect& wkRect)
240{
241    return WebCore::IntRect(static_cast<int>(wkRect.origin.x), static_cast<int>(wkRect.origin.y),
242                            static_cast<int>(wkRect.size.width), static_cast<int>(wkRect.size.height));
243}
244
245inline WKRect toAPI(const WebCore::FloatRect& rect)
246{
247    WKRect wkRect;
248    wkRect.origin.x = rect.x();
249    wkRect.origin.y = rect.y();
250    wkRect.size.width = rect.width();
251    wkRect.size.height = rect.height();
252    return wkRect;
253}
254
255inline WKRect toAPI(const WebCore::IntRect& rect)
256{
257    WKRect wkRect;
258    wkRect.origin.x = rect.x();
259    wkRect.origin.y = rect.y();
260    wkRect.size.width = rect.width();
261    wkRect.size.height = rect.height();
262    return wkRect;
263}
264
265inline WKSize toAPI(const WebCore::IntSize& size)
266{
267    WKSize wkSize;
268    wkSize.width = size.width();
269    wkSize.height = size.height();
270    return wkSize;
271}
272
273inline WKPoint toAPI(const WebCore::IntPoint& point)
274{
275    WKPoint wkPoint;
276    wkPoint.x = point.x();
277    wkPoint.y = point.y();
278    return wkPoint;
279}
280
281/* Enum conversions */
282
283inline WKTypeID toAPI(APIObject::Type type)
284{
285    return static_cast<WKTypeID>(type);
286}
287
288inline WKEventModifiers toAPI(WebEvent::Modifiers modifiers)
289{
290    WKEventModifiers wkModifiers = 0;
291    if (modifiers & WebEvent::ShiftKey)
292        wkModifiers |= kWKEventModifiersShiftKey;
293    if (modifiers & WebEvent::ControlKey)
294        wkModifiers |= kWKEventModifiersControlKey;
295    if (modifiers & WebEvent::AltKey)
296        wkModifiers |= kWKEventModifiersAltKey;
297    if (modifiers & WebEvent::MetaKey)
298        wkModifiers |= kWKEventModifiersMetaKey;
299    return wkModifiers;
300}
301
302inline WKEventMouseButton toAPI(WebMouseEvent::Button mouseButton)
303{
304    WKEventMouseButton wkMouseButton = kWKEventMouseButtonNoButton;
305
306    switch (mouseButton) {
307    case WebMouseEvent::NoButton:
308        wkMouseButton = kWKEventMouseButtonNoButton;
309        break;
310    case WebMouseEvent::LeftButton:
311        wkMouseButton = kWKEventMouseButtonLeftButton;
312        break;
313    case WebMouseEvent::MiddleButton:
314        wkMouseButton = kWKEventMouseButtonMiddleButton;
315        break;
316    case WebMouseEvent::RightButton:
317        wkMouseButton = kWKEventMouseButtonRightButton;
318        break;
319    }
320
321    return wkMouseButton;
322}
323
324inline WKContextMenuItemTag toAPI(WebCore::ContextMenuAction action)
325{
326    switch (action) {
327    case WebCore::ContextMenuItemTagNoAction:
328        return kWKContextMenuItemTagNoAction;
329    case WebCore::ContextMenuItemTagOpenLinkInNewWindow:
330        return kWKContextMenuItemTagOpenLinkInNewWindow;
331    case WebCore::ContextMenuItemTagDownloadLinkToDisk:
332        return kWKContextMenuItemTagDownloadLinkToDisk;
333    case WebCore::ContextMenuItemTagCopyLinkToClipboard:
334        return kWKContextMenuItemTagCopyLinkToClipboard;
335    case WebCore::ContextMenuItemTagOpenImageInNewWindow:
336        return kWKContextMenuItemTagOpenImageInNewWindow;
337    case WebCore::ContextMenuItemTagDownloadImageToDisk:
338        return kWKContextMenuItemTagDownloadImageToDisk;
339    case WebCore::ContextMenuItemTagCopyImageToClipboard:
340        return kWKContextMenuItemTagCopyImageToClipboard;
341#if PLATFORM(EFL) || PLATFORM(GTK) || PLATFORM(QT)
342    case WebCore::ContextMenuItemTagCopyImageUrlToClipboard:
343        return kWKContextMenuItemTagCopyImageUrlToClipboard;
344#endif
345    case WebCore::ContextMenuItemTagOpenFrameInNewWindow:
346        return kWKContextMenuItemTagOpenFrameInNewWindow;
347    case WebCore::ContextMenuItemTagCopy:
348        return kWKContextMenuItemTagCopy;
349    case WebCore::ContextMenuItemTagGoBack:
350        return kWKContextMenuItemTagGoBack;
351    case WebCore::ContextMenuItemTagGoForward:
352        return kWKContextMenuItemTagGoForward;
353    case WebCore::ContextMenuItemTagStop:
354        return kWKContextMenuItemTagStop;
355    case WebCore::ContextMenuItemTagReload:
356        return kWKContextMenuItemTagReload;
357    case WebCore::ContextMenuItemTagCut:
358        return kWKContextMenuItemTagCut;
359    case WebCore::ContextMenuItemTagPaste:
360        return kWKContextMenuItemTagPaste;
361#if PLATFORM(EFL) || PLATFORM(GTK) || PLATFORM(QT)
362    case WebCore::ContextMenuItemTagSelectAll:
363        return kWKContextMenuItemTagSelectAll;
364#endif
365    case WebCore::ContextMenuItemTagSpellingGuess:
366        return kWKContextMenuItemTagSpellingGuess;
367    case WebCore::ContextMenuItemTagNoGuessesFound:
368        return kWKContextMenuItemTagNoGuessesFound;
369    case WebCore::ContextMenuItemTagIgnoreSpelling:
370        return kWKContextMenuItemTagIgnoreSpelling;
371    case WebCore::ContextMenuItemTagLearnSpelling:
372        return kWKContextMenuItemTagLearnSpelling;
373    case WebCore::ContextMenuItemTagOther:
374        return kWKContextMenuItemTagOther;
375    case WebCore::ContextMenuItemTagSearchInSpotlight:
376        return kWKContextMenuItemTagSearchInSpotlight;
377    case WebCore::ContextMenuItemTagSearchWeb:
378        return kWKContextMenuItemTagSearchWeb;
379    case WebCore::ContextMenuItemTagLookUpInDictionary:
380        return kWKContextMenuItemTagLookUpInDictionary;
381    case WebCore::ContextMenuItemTagOpenWithDefaultApplication:
382        return kWKContextMenuItemTagOpenWithDefaultApplication;
383    case WebCore::ContextMenuItemPDFActualSize:
384        return kWKContextMenuItemTagPDFActualSize;
385    case WebCore::ContextMenuItemPDFZoomIn:
386        return kWKContextMenuItemTagPDFZoomIn;
387    case WebCore::ContextMenuItemPDFZoomOut:
388        return kWKContextMenuItemTagPDFZoomOut;
389    case WebCore::ContextMenuItemPDFAutoSize:
390        return kWKContextMenuItemTagPDFAutoSize;
391    case WebCore::ContextMenuItemPDFSinglePage:
392        return kWKContextMenuItemTagPDFSinglePage;
393    case WebCore::ContextMenuItemPDFFacingPages:
394        return kWKContextMenuItemTagPDFFacingPages;
395    case WebCore::ContextMenuItemPDFContinuous:
396        return kWKContextMenuItemTagPDFContinuous;
397    case WebCore::ContextMenuItemPDFNextPage:
398        return kWKContextMenuItemTagPDFNextPage;
399    case WebCore::ContextMenuItemPDFPreviousPage:
400        return kWKContextMenuItemTagPDFPreviousPage;
401    case WebCore::ContextMenuItemTagOpenLink:
402        return kWKContextMenuItemTagOpenLink;
403    case WebCore::ContextMenuItemTagIgnoreGrammar:
404        return kWKContextMenuItemTagIgnoreGrammar;
405    case WebCore::ContextMenuItemTagSpellingMenu:
406        return kWKContextMenuItemTagSpellingMenu;
407    case WebCore::ContextMenuItemTagShowSpellingPanel:
408        return kWKContextMenuItemTagShowSpellingPanel;
409    case WebCore::ContextMenuItemTagCheckSpelling:
410        return kWKContextMenuItemTagCheckSpelling;
411    case WebCore::ContextMenuItemTagCheckSpellingWhileTyping:
412        return kWKContextMenuItemTagCheckSpellingWhileTyping;
413    case WebCore::ContextMenuItemTagCheckGrammarWithSpelling:
414        return kWKContextMenuItemTagCheckGrammarWithSpelling;
415    case WebCore::ContextMenuItemTagFontMenu:
416        return kWKContextMenuItemTagFontMenu;
417    case WebCore::ContextMenuItemTagShowFonts:
418        return kWKContextMenuItemTagShowFonts;
419    case WebCore::ContextMenuItemTagBold:
420        return kWKContextMenuItemTagBold;
421    case WebCore::ContextMenuItemTagItalic:
422        return kWKContextMenuItemTagItalic;
423    case WebCore::ContextMenuItemTagUnderline:
424        return kWKContextMenuItemTagUnderline;
425    case WebCore::ContextMenuItemTagOutline:
426        return kWKContextMenuItemTagOutline;
427    case WebCore::ContextMenuItemTagStyles:
428        return kWKContextMenuItemTagStyles;
429    case WebCore::ContextMenuItemTagShowColors:
430        return kWKContextMenuItemTagShowColors;
431    case WebCore::ContextMenuItemTagSpeechMenu:
432        return kWKContextMenuItemTagSpeechMenu;
433    case WebCore::ContextMenuItemTagStartSpeaking:
434        return kWKContextMenuItemTagStartSpeaking;
435    case WebCore::ContextMenuItemTagStopSpeaking:
436        return kWKContextMenuItemTagStopSpeaking;
437    case WebCore::ContextMenuItemTagWritingDirectionMenu:
438        return kWKContextMenuItemTagWritingDirectionMenu;
439    case WebCore::ContextMenuItemTagDefaultDirection:
440        return kWKContextMenuItemTagDefaultDirection;
441    case WebCore::ContextMenuItemTagLeftToRight:
442        return kWKContextMenuItemTagLeftToRight;
443    case WebCore::ContextMenuItemTagRightToLeft:
444        return kWKContextMenuItemTagRightToLeft;
445    case WebCore::ContextMenuItemTagPDFSinglePageScrolling:
446        return kWKContextMenuItemTagPDFSinglePageScrolling;
447    case WebCore::ContextMenuItemTagPDFFacingPagesScrolling:
448        return kWKContextMenuItemTagPDFFacingPagesScrolling;
449    case WebCore::ContextMenuItemTagDictationAlternative:
450        return kWKContextMenuItemTagDictationAlternative;
451#if ENABLE(INSPECTOR)
452    case WebCore::ContextMenuItemTagInspectElement:
453        return kWKContextMenuItemTagInspectElement;
454#endif
455    case WebCore::ContextMenuItemTagTextDirectionMenu:
456        return kWKContextMenuItemTagTextDirectionMenu;
457    case WebCore::ContextMenuItemTagTextDirectionDefault:
458        return kWKContextMenuItemTagTextDirectionDefault;
459    case WebCore::ContextMenuItemTagTextDirectionLeftToRight:
460        return kWKContextMenuItemTagTextDirectionLeftToRight;
461    case WebCore::ContextMenuItemTagTextDirectionRightToLeft:
462        return kWKContextMenuItemTagTextDirectionRightToLeft;
463    case WebCore::ContextMenuItemTagOpenMediaInNewWindow:
464        return kWKContextMenuItemTagOpenMediaInNewWindow;
465    case WebCore::ContextMenuItemTagDownloadMediaToDisk:
466        return kWKContextMenuItemTagDownloadMediaToDisk;
467    case WebCore::ContextMenuItemTagCopyMediaLinkToClipboard:
468        return kWKContextMenuItemTagCopyMediaLinkToClipboard;
469    case WebCore::ContextMenuItemTagToggleMediaControls:
470        return kWKContextMenuItemTagToggleMediaControls;
471    case WebCore::ContextMenuItemTagToggleMediaLoop:
472        return kWKContextMenuItemTagToggleMediaLoop;
473    case WebCore::ContextMenuItemTagToggleVideoFullscreen:
474        return kWKContextMenuItemTagToggleVideoFullscreen;
475    case WebCore::ContextMenuItemTagEnterVideoFullscreen:
476        return kWKContextMenuItemTagEnterVideoFullscreen;
477    case WebCore::ContextMenuItemTagMediaPlayPause:
478        return kWKContextMenuItemTagMediaPlayPause;
479    case WebCore::ContextMenuItemTagMediaMute:
480        return kWKContextMenuItemTagMediaMute;
481#if PLATFORM(MAC)
482    case WebCore::ContextMenuItemTagCorrectSpellingAutomatically:
483        return kWKContextMenuItemTagCorrectSpellingAutomatically;
484    case WebCore::ContextMenuItemTagSubstitutionsMenu:
485        return kWKContextMenuItemTagSubstitutionsMenu;
486    case WebCore::ContextMenuItemTagShowSubstitutions:
487        return kWKContextMenuItemTagShowSubstitutions;
488    case WebCore::ContextMenuItemTagSmartCopyPaste:
489        return kWKContextMenuItemTagSmartCopyPaste;
490    case WebCore::ContextMenuItemTagSmartQuotes:
491        return kWKContextMenuItemTagSmartQuotes;
492    case WebCore::ContextMenuItemTagSmartDashes:
493        return kWKContextMenuItemTagSmartDashes;
494    case WebCore::ContextMenuItemTagSmartLinks:
495        return kWKContextMenuItemTagSmartLinks;
496    case WebCore::ContextMenuItemTagTextReplacement:
497        return kWKContextMenuItemTagTextReplacement;
498    case WebCore::ContextMenuItemTagTransformationsMenu:
499        return kWKContextMenuItemTagTransformationsMenu;
500    case WebCore::ContextMenuItemTagMakeUpperCase:
501        return kWKContextMenuItemTagMakeUpperCase;
502    case WebCore::ContextMenuItemTagMakeLowerCase:
503        return kWKContextMenuItemTagMakeLowerCase;
504    case WebCore::ContextMenuItemTagCapitalize:
505        return kWKContextMenuItemTagCapitalize;
506    case WebCore::ContextMenuItemTagChangeBack:
507        return kWKContextMenuItemTagChangeBack;
508#endif
509    case WebCore::ContextMenuItemTagOpenLinkInThisWindow:
510        return kWKContextMenuItemTagOpenLinkInThisWindow;
511    default:
512        if (action < WebCore::ContextMenuItemBaseApplicationTag)
513            LOG_ERROR("ContextMenuAction %i is an unknown tag but is below the allowable custom tag value of %i", action, WebCore::  ContextMenuItemBaseApplicationTag);
514        return static_cast<WKContextMenuItemTag>(action);
515    }
516}
517
518inline WebCore::ContextMenuAction toImpl(WKContextMenuItemTag tag)
519{
520    switch (tag) {
521    case kWKContextMenuItemTagNoAction:
522        return WebCore::ContextMenuItemTagNoAction;
523    case kWKContextMenuItemTagOpenLinkInNewWindow:
524        return WebCore::ContextMenuItemTagOpenLinkInNewWindow;
525    case kWKContextMenuItemTagDownloadLinkToDisk:
526        return WebCore::ContextMenuItemTagDownloadLinkToDisk;
527    case kWKContextMenuItemTagCopyLinkToClipboard:
528        return WebCore::ContextMenuItemTagCopyLinkToClipboard;
529    case kWKContextMenuItemTagOpenImageInNewWindow:
530        return WebCore::ContextMenuItemTagOpenImageInNewWindow;
531    case kWKContextMenuItemTagDownloadImageToDisk:
532        return WebCore::ContextMenuItemTagDownloadImageToDisk;
533    case kWKContextMenuItemTagCopyImageToClipboard:
534        return WebCore::ContextMenuItemTagCopyImageToClipboard;
535    case kWKContextMenuItemTagOpenFrameInNewWindow:
536#if PLATFORM(EFL) || PLATFORM(GTK) || PLATFORM(QT)
537    case kWKContextMenuItemTagCopyImageUrlToClipboard:
538        return WebCore::ContextMenuItemTagCopyImageUrlToClipboard;
539#endif
540        return WebCore::ContextMenuItemTagOpenFrameInNewWindow;
541    case kWKContextMenuItemTagCopy:
542        return WebCore::ContextMenuItemTagCopy;
543    case kWKContextMenuItemTagGoBack:
544        return WebCore::ContextMenuItemTagGoBack;
545    case kWKContextMenuItemTagGoForward:
546        return WebCore::ContextMenuItemTagGoForward;
547    case kWKContextMenuItemTagStop:
548        return WebCore::ContextMenuItemTagStop;
549    case kWKContextMenuItemTagReload:
550        return WebCore::ContextMenuItemTagReload;
551    case kWKContextMenuItemTagCut:
552        return WebCore::ContextMenuItemTagCut;
553    case kWKContextMenuItemTagPaste:
554        return WebCore::ContextMenuItemTagPaste;
555#if PLATFORM(EFL) || PLATFORM(GTK) || PLATFORM(QT)
556    case kWKContextMenuItemTagSelectAll:
557        return WebCore::ContextMenuItemTagSelectAll;
558#endif
559    case kWKContextMenuItemTagSpellingGuess:
560        return WebCore::ContextMenuItemTagSpellingGuess;
561    case kWKContextMenuItemTagNoGuessesFound:
562        return WebCore::ContextMenuItemTagNoGuessesFound;
563    case kWKContextMenuItemTagIgnoreSpelling:
564        return WebCore::ContextMenuItemTagIgnoreSpelling;
565    case kWKContextMenuItemTagLearnSpelling:
566        return WebCore::ContextMenuItemTagLearnSpelling;
567    case kWKContextMenuItemTagOther:
568        return WebCore::ContextMenuItemTagOther;
569    case kWKContextMenuItemTagSearchInSpotlight:
570        return WebCore::ContextMenuItemTagSearchInSpotlight;
571    case kWKContextMenuItemTagSearchWeb:
572        return WebCore::ContextMenuItemTagSearchWeb;
573    case kWKContextMenuItemTagLookUpInDictionary:
574        return WebCore::ContextMenuItemTagLookUpInDictionary;
575    case kWKContextMenuItemTagOpenWithDefaultApplication:
576        return WebCore::ContextMenuItemTagOpenWithDefaultApplication;
577    case kWKContextMenuItemTagPDFActualSize:
578        return WebCore::ContextMenuItemPDFActualSize;
579    case kWKContextMenuItemTagPDFZoomIn:
580        return WebCore::ContextMenuItemPDFZoomIn;
581    case kWKContextMenuItemTagPDFZoomOut:
582        return WebCore::ContextMenuItemPDFZoomOut;
583    case kWKContextMenuItemTagPDFAutoSize:
584        return WebCore::ContextMenuItemPDFAutoSize;
585    case kWKContextMenuItemTagPDFSinglePage:
586        return WebCore::ContextMenuItemPDFSinglePage;
587    case kWKContextMenuItemTagPDFFacingPages:
588        return WebCore::ContextMenuItemPDFFacingPages;
589    case kWKContextMenuItemTagPDFContinuous:
590        return WebCore::ContextMenuItemPDFContinuous;
591    case kWKContextMenuItemTagPDFNextPage:
592        return WebCore::ContextMenuItemPDFNextPage;
593    case kWKContextMenuItemTagPDFPreviousPage:
594        return WebCore::ContextMenuItemPDFPreviousPage;
595    case kWKContextMenuItemTagOpenLink:
596        return WebCore::ContextMenuItemTagOpenLink;
597    case kWKContextMenuItemTagIgnoreGrammar:
598        return WebCore::ContextMenuItemTagIgnoreGrammar;
599    case kWKContextMenuItemTagSpellingMenu:
600        return WebCore::ContextMenuItemTagSpellingMenu;
601    case kWKContextMenuItemTagShowSpellingPanel:
602        return WebCore::ContextMenuItemTagShowSpellingPanel;
603    case kWKContextMenuItemTagCheckSpelling:
604        return WebCore::ContextMenuItemTagCheckSpelling;
605    case kWKContextMenuItemTagCheckSpellingWhileTyping:
606        return WebCore::ContextMenuItemTagCheckSpellingWhileTyping;
607    case kWKContextMenuItemTagCheckGrammarWithSpelling:
608        return WebCore::ContextMenuItemTagCheckGrammarWithSpelling;
609    case kWKContextMenuItemTagFontMenu:
610        return WebCore::ContextMenuItemTagFontMenu;
611    case kWKContextMenuItemTagShowFonts:
612        return WebCore::ContextMenuItemTagShowFonts;
613    case kWKContextMenuItemTagBold:
614        return WebCore::ContextMenuItemTagBold;
615    case kWKContextMenuItemTagItalic:
616        return WebCore::ContextMenuItemTagItalic;
617    case kWKContextMenuItemTagUnderline:
618        return WebCore::ContextMenuItemTagUnderline;
619    case kWKContextMenuItemTagOutline:
620        return WebCore::ContextMenuItemTagOutline;
621    case kWKContextMenuItemTagStyles:
622        return WebCore::ContextMenuItemTagStyles;
623    case kWKContextMenuItemTagShowColors:
624        return WebCore::ContextMenuItemTagShowColors;
625    case kWKContextMenuItemTagSpeechMenu:
626        return WebCore::ContextMenuItemTagSpeechMenu;
627    case kWKContextMenuItemTagStartSpeaking:
628        return WebCore::ContextMenuItemTagStartSpeaking;
629    case kWKContextMenuItemTagStopSpeaking:
630        return WebCore::ContextMenuItemTagStopSpeaking;
631    case kWKContextMenuItemTagWritingDirectionMenu:
632        return WebCore::ContextMenuItemTagWritingDirectionMenu;
633    case kWKContextMenuItemTagDefaultDirection:
634        return WebCore::ContextMenuItemTagDefaultDirection;
635    case kWKContextMenuItemTagLeftToRight:
636        return WebCore::ContextMenuItemTagLeftToRight;
637    case kWKContextMenuItemTagRightToLeft:
638        return WebCore::ContextMenuItemTagRightToLeft;
639    case kWKContextMenuItemTagPDFSinglePageScrolling:
640        return WebCore::ContextMenuItemTagPDFSinglePageScrolling;
641    case kWKContextMenuItemTagPDFFacingPagesScrolling:
642        return WebCore::ContextMenuItemTagPDFFacingPagesScrolling;
643    case kWKContextMenuItemTagDictationAlternative:
644        return WebCore::ContextMenuItemTagDictationAlternative;
645#if ENABLE(INSPECTOR)
646    case kWKContextMenuItemTagInspectElement:
647        return WebCore::ContextMenuItemTagInspectElement;
648#endif
649    case kWKContextMenuItemTagTextDirectionMenu:
650        return WebCore::ContextMenuItemTagTextDirectionMenu;
651    case kWKContextMenuItemTagTextDirectionDefault:
652        return WebCore::ContextMenuItemTagTextDirectionDefault;
653    case kWKContextMenuItemTagTextDirectionLeftToRight:
654        return WebCore::ContextMenuItemTagTextDirectionLeftToRight;
655    case kWKContextMenuItemTagTextDirectionRightToLeft:
656        return WebCore::ContextMenuItemTagTextDirectionRightToLeft;
657    case kWKContextMenuItemTagOpenMediaInNewWindow:
658        return WebCore::ContextMenuItemTagOpenMediaInNewWindow;
659    case kWKContextMenuItemTagDownloadMediaToDisk:
660        return WebCore::ContextMenuItemTagDownloadMediaToDisk;
661    case kWKContextMenuItemTagCopyMediaLinkToClipboard:
662        return WebCore::ContextMenuItemTagCopyMediaLinkToClipboard;
663    case kWKContextMenuItemTagToggleMediaControls:
664        return WebCore::ContextMenuItemTagToggleMediaControls;
665    case kWKContextMenuItemTagToggleMediaLoop:
666        return WebCore::ContextMenuItemTagToggleMediaLoop;
667    case kWKContextMenuItemTagToggleVideoFullscreen:
668        return WebCore::ContextMenuItemTagToggleVideoFullscreen;
669    case kWKContextMenuItemTagEnterVideoFullscreen:
670        return WebCore::ContextMenuItemTagEnterVideoFullscreen;
671    case kWKContextMenuItemTagMediaPlayPause:
672        return WebCore::ContextMenuItemTagMediaPlayPause;
673    case kWKContextMenuItemTagMediaMute:
674        return WebCore::ContextMenuItemTagMediaMute;
675#if PLATFORM(MAC)
676    case kWKContextMenuItemTagCorrectSpellingAutomatically:
677        return WebCore::ContextMenuItemTagCorrectSpellingAutomatically;
678    case kWKContextMenuItemTagSubstitutionsMenu:
679        return WebCore::ContextMenuItemTagSubstitutionsMenu;
680    case kWKContextMenuItemTagShowSubstitutions:
681        return WebCore::ContextMenuItemTagShowSubstitutions;
682    case kWKContextMenuItemTagSmartCopyPaste:
683        return WebCore::ContextMenuItemTagSmartCopyPaste;
684    case kWKContextMenuItemTagSmartQuotes:
685        return WebCore::ContextMenuItemTagSmartQuotes;
686    case kWKContextMenuItemTagSmartDashes:
687        return WebCore::ContextMenuItemTagSmartDashes;
688    case kWKContextMenuItemTagSmartLinks:
689        return WebCore::ContextMenuItemTagSmartLinks;
690    case kWKContextMenuItemTagTextReplacement:
691        return WebCore::ContextMenuItemTagTextReplacement;
692    case kWKContextMenuItemTagTransformationsMenu:
693        return WebCore::ContextMenuItemTagTransformationsMenu;
694    case kWKContextMenuItemTagMakeUpperCase:
695        return WebCore::ContextMenuItemTagMakeUpperCase;
696    case kWKContextMenuItemTagMakeLowerCase:
697        return WebCore::ContextMenuItemTagMakeLowerCase;
698    case kWKContextMenuItemTagCapitalize:
699        return WebCore::ContextMenuItemTagCapitalize;
700    case kWKContextMenuItemTagChangeBack:
701        return WebCore::ContextMenuItemTagChangeBack;
702#endif
703    case kWKContextMenuItemTagOpenLinkInThisWindow:
704        return WebCore::ContextMenuItemTagOpenLinkInThisWindow;
705    default:
706        if (tag < kWKContextMenuItemBaseApplicationTag)
707            LOG_ERROR("WKContextMenuItemTag %i is an unknown tag but is below the allowable custom tag value of %i", tag, kWKContextMenuItemBaseApplicationTag);
708        return static_cast<WebCore::ContextMenuAction>(tag);
709    }
710}
711
712inline WKContextMenuItemType toAPI(WebCore::ContextMenuItemType type)
713{
714    switch(type) {
715    case WebCore::ActionType:
716        return kWKContextMenuItemTypeAction;
717    case WebCore::CheckableActionType:
718        return kWKContextMenuItemTypeCheckableAction;
719    case WebCore::SeparatorType:
720        return kWKContextMenuItemTypeSeparator;
721    case WebCore::SubmenuType:
722        return kWKContextMenuItemTypeSubmenu;
723    default:
724        ASSERT_NOT_REACHED();
725        return kWKContextMenuItemTypeAction;
726    }
727}
728
729inline FindOptions toFindOptions(WKFindOptions wkFindOptions)
730{
731    unsigned findOptions = 0;
732
733    if (wkFindOptions & kWKFindOptionsCaseInsensitive)
734        findOptions |= FindOptionsCaseInsensitive;
735    if (wkFindOptions & kWKFindOptionsAtWordStarts)
736        findOptions |= FindOptionsAtWordStarts;
737    if (wkFindOptions & kWKFindOptionsTreatMedialCapitalAsWordStart)
738        findOptions |= FindOptionsTreatMedialCapitalAsWordStart;
739    if (wkFindOptions & kWKFindOptionsBackwards)
740        findOptions |= FindOptionsBackwards;
741    if (wkFindOptions & kWKFindOptionsWrapAround)
742        findOptions |= FindOptionsWrapAround;
743    if (wkFindOptions & kWKFindOptionsShowOverlay)
744        findOptions |= FindOptionsShowOverlay;
745    if (wkFindOptions & kWKFindOptionsShowFindIndicator)
746        findOptions |= FindOptionsShowFindIndicator;
747    if (wkFindOptions & kWKFindOptionsShowHighlight)
748        findOptions |= FindOptionsShowHighlight;
749
750    return static_cast<FindOptions>(findOptions);
751}
752
753inline WKFrameNavigationType toAPI(WebCore::NavigationType type)
754{
755    WKFrameNavigationType wkType = kWKFrameNavigationTypeOther;
756
757    switch (type) {
758    case WebCore::NavigationTypeLinkClicked:
759        wkType = kWKFrameNavigationTypeLinkClicked;
760        break;
761    case WebCore::NavigationTypeFormSubmitted:
762        wkType = kWKFrameNavigationTypeFormSubmitted;
763        break;
764    case WebCore::NavigationTypeBackForward:
765        wkType = kWKFrameNavigationTypeBackForward;
766        break;
767    case WebCore::NavigationTypeReload:
768        wkType = kWKFrameNavigationTypeReload;
769        break;
770    case WebCore::NavigationTypeFormResubmitted:
771        wkType = kWKFrameNavigationTypeFormResubmitted;
772        break;
773    case WebCore::NavigationTypeOther:
774        wkType = kWKFrameNavigationTypeOther;
775        break;
776    }
777
778    return wkType;
779}
780
781inline WKSameDocumentNavigationType toAPI(SameDocumentNavigationType type)
782{
783    WKFrameNavigationType wkType = kWKSameDocumentNavigationAnchorNavigation;
784
785    switch (type) {
786    case SameDocumentNavigationAnchorNavigation:
787        wkType = kWKSameDocumentNavigationAnchorNavigation;
788        break;
789    case SameDocumentNavigationSessionStatePush:
790        wkType = kWKSameDocumentNavigationSessionStatePush;
791        break;
792    case SameDocumentNavigationSessionStateReplace:
793        wkType = kWKSameDocumentNavigationSessionStateReplace;
794        break;
795    case SameDocumentNavigationSessionStatePop:
796        wkType = kWKSameDocumentNavigationSessionStatePop;
797        break;
798    }
799
800    return wkType;
801}
802
803inline WKLayoutMilestones toWKLayoutMilestones(WebCore::LayoutMilestones milestones)
804{
805    unsigned wkMilestones = 0;
806
807    if (milestones & WebCore::DidFirstLayout)
808        wkMilestones |= kWKDidFirstLayout;
809    if (milestones & WebCore::DidFirstVisuallyNonEmptyLayout)
810        wkMilestones |= kWKDidFirstVisuallyNonEmptyLayout;
811    if (milestones & WebCore::DidHitRelevantRepaintedObjectsAreaThreshold)
812        wkMilestones |= kWKDidHitRelevantRepaintedObjectsAreaThreshold;
813    if (milestones & WebCore::DidFirstFlushForHeaderLayer)
814        wkMilestones |= kWKDidFirstFlushForHeaderLayer;
815    if (milestones & WebCore::DidFirstLayoutAfterSuppressedIncrementalRendering)
816        wkMilestones |= kWKDidFirstLayoutAfterSuppressedIncrementalRendering;
817    if (milestones & WebCore::DidFirstPaintAfterSuppressedIncrementalRendering)
818        wkMilestones |= kWKDidFirstPaintAfterSuppressedIncrementalRendering;
819
820    return wkMilestones;
821}
822
823inline WebCore::LayoutMilestones toLayoutMilestones(WKLayoutMilestones wkMilestones)
824{
825    WebCore::LayoutMilestones milestones = 0;
826
827    if (wkMilestones & kWKDidFirstLayout)
828        milestones |= WebCore::DidFirstLayout;
829    if (wkMilestones & kWKDidFirstVisuallyNonEmptyLayout)
830        milestones |= WebCore::DidFirstVisuallyNonEmptyLayout;
831    if (wkMilestones & kWKDidHitRelevantRepaintedObjectsAreaThreshold)
832        milestones |= WebCore::DidHitRelevantRepaintedObjectsAreaThreshold;
833    if (wkMilestones & kWKDidFirstFlushForHeaderLayer)
834        milestones |= WebCore::DidFirstFlushForHeaderLayer;
835    if (wkMilestones & kWKDidFirstLayoutAfterSuppressedIncrementalRendering)
836        milestones |= WebCore::DidFirstLayoutAfterSuppressedIncrementalRendering;
837    if (wkMilestones & kWKDidFirstPaintAfterSuppressedIncrementalRendering)
838        milestones |= WebCore::DidFirstPaintAfterSuppressedIncrementalRendering;
839
840    return milestones;
841}
842
843inline WebCore::PageVisibilityState toPageVisibilityState(WKPageVisibilityState wkPageVisibilityState)
844{
845    switch (wkPageVisibilityState) {
846    case kWKPageVisibilityStateVisible:
847        return WebCore::PageVisibilityStateVisible;
848    case kWKPageVisibilityStateHidden:
849        return WebCore::PageVisibilityStateHidden;
850    case kWKPageVisibilityStatePrerender:
851        return WebCore::PageVisibilityStatePrerender;
852    case kWKPageVisibilityStateUnloaded:
853        return WebCore::PageVisibilityStateUnloaded;
854    }
855
856    ASSERT_NOT_REACHED();
857    return WebCore::PageVisibilityStateVisible;
858}
859
860inline ImageOptions toImageOptions(WKImageOptions wkImageOptions)
861{
862    unsigned imageOptions = 0;
863
864    if (wkImageOptions & kWKImageOptionsShareable)
865        imageOptions |= ImageOptionsShareable;
866
867    return static_cast<ImageOptions>(imageOptions);
868}
869
870inline SnapshotOptions snapshotOptionsFromImageOptions(WKImageOptions wkImageOptions)
871{
872    unsigned snapshotOptions = 0;
873
874    if (wkImageOptions & kWKImageOptionsShareable)
875        snapshotOptions |= SnapshotOptionsShareable;
876
877    return snapshotOptions;
878}
879
880inline SnapshotOptions toSnapshotOptions(WKSnapshotOptions wkSnapshotOptions)
881{
882    unsigned snapshotOptions = 0;
883
884    if (wkSnapshotOptions & kWKSnapshotOptionsShareable)
885        snapshotOptions |= SnapshotOptionsShareable;
886    if (wkSnapshotOptions & kWKSnapshotOptionsExcludeSelectionHighlighting)
887        snapshotOptions |= SnapshotOptionsExcludeSelectionHighlighting;
888    if (wkSnapshotOptions & kWKSnapshotOptionsInViewCoordinates)
889        snapshotOptions |= SnapshotOptionsInViewCoordinates;
890    if (wkSnapshotOptions & kWKSnapshotOptionsPaintSelectionRectangle)
891        snapshotOptions |= SnapshotOptionsPaintSelectionRectangle;
892
893    return snapshotOptions;
894}
895
896inline WebCore::UserScriptInjectionTime toUserScriptInjectionTime(WKUserScriptInjectionTime wkInjectedTime)
897{
898    switch (wkInjectedTime) {
899    case kWKInjectAtDocumentStart:
900        return WebCore::InjectAtDocumentStart;
901    case kWKInjectAtDocumentEnd:
902        return WebCore::InjectAtDocumentEnd;
903    }
904
905    ASSERT_NOT_REACHED();
906    return WebCore::InjectAtDocumentStart;
907}
908
909inline WebCore::UserContentInjectedFrames toUserContentInjectedFrames(WKUserContentInjectedFrames wkInjectedFrames)
910{
911    switch (wkInjectedFrames) {
912    case kWKInjectInAllFrames:
913        return WebCore::InjectInAllFrames;
914    case kWKInjectInTopFrameOnly:
915        return WebCore::InjectInTopFrameOnly;
916    }
917
918    ASSERT_NOT_REACHED();
919    return WebCore::InjectInAllFrames;
920}
921
922} // namespace WebKit
923
924#endif // WKSharedAPICast_h
925