2 * Copyright (C) 2014 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 #import "_WKElementActionInternal.h"
33 #import "GestureTypes.h"
34 #import "WKActionSheetAssistant.h"
35 #import "WKContentViewInteraction.h"
36 #import "_WKActivatedElementInfoInternal.h"
37 #import <WebCore/LocalizedStrings.h>
38 #import <WebCore/SoftLinking.h>
39 #import <wtf/RetainPtr.h>
40 #import <wtf/text/WTFString.h>
42 #if HAVE(SAFARI_SERVICES_FRAMEWORK)
43 #import <SafariServices/SSReadingList.h>
44 SOFT_LINK_FRAMEWORK(SafariServices);
45 SOFT_LINK_CLASS(SafariServices, SSReadingList);
48 typedef void (^WKElementActionHandlerInternal)(WKActionSheetAssistant *, _WKActivatedElementInfo *);
50 @implementation _WKElementAction {
51 RetainPtr<NSString> _title;
52 WKElementActionHandlerInternal _actionHandler;
53 WKElementActionDismissalHandler _dismissalHandler;
54 __weak WKActionSheetAssistant *_defaultActionSheetAssistant;
57 - (id)_initWithTitle:(NSString *)title actionHandler:(WKElementActionHandlerInternal)handler type:(_WKElementActionType)type assistant:(WKActionSheetAssistant *)assistant
59 if (!(self = [super init]))
62 _title = adoptNS([title copy]);
64 _actionHandler = [handler copy];
65 _defaultActionSheetAssistant = assistant;
71 [_actionHandler release];
72 [_dismissalHandler release];
77 + (instancetype)elementActionWithTitle:(NSString *)title actionHandler:(WKElementActionHandler)handler
79 return [[[self alloc] _initWithTitle:title actionHandler:^(WKActionSheetAssistant *, _WKActivatedElementInfo *actionInfo) { handler(actionInfo); }
80 type:_WKElementActionTypeCustom assistant:nil] autorelease];
83 #if HAVE(SAFARI_SERVICES_FRAMEWORK)
84 static void addToReadingList(NSURL *targetURL, NSString *title)
86 if (!title || [title length] == 0)
87 title = [targetURL absoluteString];
89 [[getSSReadingListClass() defaultReadingList] addReadingListItemWithURL:targetURL title:title previewText:nil error:nil];
93 + (instancetype)_elementActionWithType:(_WKElementActionType)type title:(NSString *)title actionHandler:(WKElementActionHandler)actionHandler
95 WKElementActionHandlerInternal handler = ^(WKActionSheetAssistant *, _WKActivatedElementInfo *actionInfo) { actionHandler(actionInfo); };
96 return [[[self alloc] _initWithTitle:title actionHandler:handler type:type assistant:nil] autorelease];
99 + (instancetype)_elementActionWithType:(_WKElementActionType)type customTitle:(NSString *)customTitle assistant:(WKActionSheetAssistant *)assistant
102 WKElementActionHandlerInternal handler;
104 case _WKElementActionTypeCopy:
105 title = WEB_UI_STRING_KEY("Copy", "Copy (ActionSheet)", "Title for Copy Link or Image action button");
106 handler = ^(WKActionSheetAssistant *assistant, _WKActivatedElementInfo *actionInfo) {
107 [assistant.delegate actionSheetAssistant:assistant performAction:WebKit::SheetAction::Copy];
110 case _WKElementActionTypeOpen:
111 title = WEB_UI_STRING("Open", "Title for Open Link action button");
112 handler = ^(WKActionSheetAssistant *assistant, _WKActivatedElementInfo *actionInfo) {
113 [assistant.delegate actionSheetAssistant:assistant openElementAtLocation:actionInfo._interactionLocation];
116 case _WKElementActionTypeSaveImage:
117 title = WEB_UI_STRING("Save Image", "Title for Save Image action button");
118 handler = ^(WKActionSheetAssistant *assistant, _WKActivatedElementInfo *actionInfo) {
119 [assistant.delegate actionSheetAssistant:assistant performAction:WebKit::SheetAction::SaveImage];
122 #if HAVE(SAFARI_SERVICES_FRAMEWORK)
123 case _WKElementActionTypeAddToReadingList:
124 title = WEB_UI_STRING("Add to Reading List", "Title for Add to Reading List action button");
125 handler = ^(WKActionSheetAssistant *, _WKActivatedElementInfo *actionInfo) {
126 addToReadingList(actionInfo.URL, actionInfo.title);
130 case _WKElementActionTypeShare:
131 title = WEB_UI_STRING("Share…", "Title for Share action button");
132 handler = ^(WKActionSheetAssistant *assistant, _WKActivatedElementInfo *actionInfo) {
133 [assistant.delegate actionSheetAssistant:assistant shareElementWithURL:actionInfo.URL rect:actionInfo.boundingRect];
137 [NSException raise:NSInvalidArgumentException format:@"There is no standard web element action of type %ld.", (long)type];
141 return [[[self alloc] _initWithTitle:(customTitle ? customTitle : title) actionHandler:handler type:type assistant:assistant] autorelease];
144 + (instancetype)_elementActionWithType:(_WKElementActionType)type assistant:(WKActionSheetAssistant *)assistant
146 return [self _elementActionWithType:type customTitle:nil assistant:assistant];
149 + (instancetype)elementActionWithType:(_WKElementActionType)type customTitle:(NSString *)customTitle
151 return [self _elementActionWithType:type customTitle:customTitle assistant:nil];
154 + (instancetype)elementActionWithType:(_WKElementActionType)type
156 return [self elementActionWithType:type customTitle:nil];
164 - (void)_runActionWithElementInfo:(_WKActivatedElementInfo *)info forActionSheetAssistant:(WKActionSheetAssistant *)assistant
166 _actionHandler(assistant, info);
169 - (void)runActionWithElementInfo:(_WKActivatedElementInfo *)info
171 [self _runActionWithElementInfo:info forActionSheetAssistant:_defaultActionSheetAssistant];
176 #endif // PLATFORM(IOS)
178 #endif // WK_API_ENABLED