Fix the build with the public SDK.
[WebKit-https.git] / Source / WebKit / mac / WebCoreSupport / WebSelectionServiceController.mm
1 /*
2  * Copyright (C) 2014 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 #import "WebSelectionServiceController.h"
27
28 #if ENABLE(SERVICE_CONTROLS)
29
30 #import "WebViewInternal.h"
31 #import <WebCore/FrameSelection.h>
32 #import <WebCore/HTMLConverter.h>
33 #import <WebCore/Range.h>
34 #import <wtf/NeverDestroyed.h>
35
36 using namespace WebCore;
37
38 #if __has_include(<AppKit/NSSharingService_Private.h>)
39 #import <AppKit/NSSharingService_Private.h>
40 #else
41 typedef NS_OPTIONS(NSUInteger, NSSharingServiceMask) {
42     NSSharingServiceMaskShare = (1 << NSSharingServiceTypeShare),
43     NSSharingServiceMaskViewer = (1 << NSSharingServiceTypeViewer),
44     NSSharingServiceMaskEditor = (1 << NSSharingServiceTypeEditor),
45     
46     NSSharingServiceMaskAllTypes = 0xFFFF
47 } NS_ENUM_AVAILABLE_MAC(10_10);
48 #endif
49
50 @interface NSSharingService (WKDetails)
51 + (NSArray *)sharingServicesForItems:(NSArray *)items mask:(NSSharingServiceMask)maskForFiltering;
52 @end
53
54 WebSelectionServiceController::WebSelectionServiceController(WebView *webView) 
55     : m_webView(webView)
56 {
57 }
58
59 void WebSelectionServiceController::handleSelectionServiceClick(WebCore::FrameSelection& selection, const Vector<String>& telephoneNumbers, const WebCore::IntPoint& point)
60 {
61     Page* page = [m_webView page];
62     if (!page)
63         return;
64
65     RefPtr<Range> range = selection.selection().firstRange();
66     if (!range)
67         return;
68
69     RetainPtr<NSAttributedString> attributedSelection = attributedStringFromRange(*range);
70     if (!attributedSelection)
71         return;
72
73     NSArray *items = @[ attributedSelection.get() ];
74
75     bool isEditable = selection.selection().isContentEditable();
76     m_sharingServicePickerController = adoptNS([[WebSharingServicePickerController alloc] initWithItems:items includeEditorServices:isEditable client:this style:NSSharingServicePickerStyleTextSelection]);
77
78     RetainPtr<NSMenu> menu = adoptNS([[m_sharingServicePickerController menu] copy]);
79
80     [menu setShowsStateColumn:YES];
81
82     [menu popUpMenuPositioningItem:nil atLocation:[m_webView convertPoint:point toView:nil] inView:m_webView];
83 }
84
85 static bool hasCompatibleServicesForItems(NSArray *items)
86 {
87     return [NSSharingService sharingServicesForItems:items mask:NSSharingServiceMaskViewer | NSSharingServiceMaskEditor].count;
88 }
89
90 bool WebSelectionServiceController::hasRelevantSelectionServices(bool isTextOnly) const
91 {
92     RetainPtr<NSAttributedString> attributedString = adoptNS([[NSAttributedString alloc] initWithString:@"a"]);
93
94     bool hasSelectionServices = hasCompatibleServicesForItems(@[ attributedString.get() ]);
95     if (isTextOnly && hasSelectionServices)
96         return true;
97
98     NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
99     RetainPtr<NSImage> image = adoptNS([[NSImage alloc] init]);
100     NSTextAttachmentCell *cell = [[NSTextAttachmentCell alloc] initImageCell:image.get()];
101     [attachment setAttachmentCell:cell];
102     RetainPtr<NSMutableAttributedString> attributedStringWithRichContent = adoptNS((NSMutableAttributedString *)[NSMutableAttributedString attributedStringWithAttachment:attachment]);
103     [attributedStringWithRichContent appendAttributedString:attributedString.get()];
104
105     bool hasRichContentServices = hasCompatibleServicesForItems(@[ attributedStringWithRichContent.get() ]);
106     return hasRichContentServices;
107 }
108
109 void WebSelectionServiceController::sharingServicePickerWillBeDestroyed(WebSharingServicePickerController &)
110 {
111     m_sharingServicePickerController = nil;
112 }
113
114 WebCore::Page* WebSelectionServiceController::pageForSharingServicePicker(WebSharingServicePickerController &)
115 {
116     return [m_webView page];
117 }
118
119 RetainPtr<NSWindow> WebSelectionServiceController::windowForSharingServicePicker(WebSharingServicePickerController &)
120 {
121     return [m_webView window];
122 }
123
124 FloatRect WebSelectionServiceController::screenRectForCurrentSharingServicePickerItem(WebSharingServicePickerController &)
125 {
126     return FloatRect();
127 }
128
129 RetainPtr<NSImage> WebSelectionServiceController::imageForCurrentSharingServicePickerItem(WebSharingServicePickerController &)
130 {
131     return nil;
132 }
133
134 #endif