Implement selection services menu for Legacy WebKit
authortimothy_horton@apple.com <timothy_horton@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 16 Oct 2014 20:13:35 +0000 (20:13 +0000)
committertimothy_horton@apple.com <timothy_horton@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 16 Oct 2014 20:13:35 +0000 (20:13 +0000)
https://bugs.webkit.org/show_bug.cgi?id=137582
<rdar://problem/18604241>

Reviewed by Brady Eidson.

* WebCore.exp.in:
* editing/Editor.h:
* editing/ios/EditorIOS.mm:
(WebCore::Editor::replaceSelectionWithAttributedString):
* editing/mac/EditorMac.mm:
(WebCore::Editor::replaceSelectionWithAttributedString):
Add replaceSelectionWithAttributedString, which replaces the selection
with the given attributed string (converting to plain text if the destination
is not richly editable).

* WebKit.xcodeproj/project.pbxproj:

* Misc/WebSharingServicePickerController.h:
Move SPI into the header (in the near future it will be moved to a modern-style SPI header).
Add WebSharingServicePickerClient, which other classes can implement
instead of tying WebSharingServicePickerController strongly to WebContextMenuClient.
Make initWithData:... -> initWithItems:... instead, taking an array of NSItemProviders.

* Misc/WebSharingServicePickerController.mm:
Pass in NSSharingServicePickerStyle, so that clients can specify the style.

(-[WebSharingServicePickerController clear]):
(-[WebSharingServicePickerController didShareImageData:confirmDataIsValidTIFFData:]):
(-[WebSharingServicePickerController sharingServicePicker:didChooseSharingService:]):
(-[WebSharingServicePickerController sharingService:sourceFrameOnScreenForShareItem:]):
(-[WebSharingServicePickerController sharingService:transitionImageForShareItem:contentRect:]):
(-[WebSharingServicePickerController sharingService:sourceWindowForShareItems:sharingContentScope:]):
Make use of WebSharingServicePickerClient.

(-[WebSharingServicePickerController sharingService:didShareItems:]):
If we're returned a NSAttributedString, paste it on top of the current selection,
similar to what the WebKit2 implementation currently does.

* WebCoreSupport/WebChromeClient.h:
* WebCoreSupport/WebChromeClient.mm:
(WebChromeClient::handleTelephoneNumberClick):
(WebChromeClient::handleSelectionServiceClick):
(WebChromeClient::hasRelevantSelectionServices):
Override the services-related ChromeClient functions, forward them to WebSelectionServiceController.

* WebCoreSupport/WebContextMenuClient.h:
* WebCoreSupport/WebContextMenuClient.mm:
(WebContextMenuClient::sharingServicePickerWillBeDestroyed):
(WebContextMenuClient::pageForSharingServicePicker):
(WebContextMenuClient::windowForSharingServicePicker):
(WebContextMenuClient::screenRectForCurrentSharingServicePickerItem):
(WebContextMenuClient::imageForCurrentSharingServicePickerItem):
(WebContextMenuClient::contextMenuForEvent):
(WebContextMenuClient::screenRectForHitTestNode): Deleted.
(WebContextMenuClient::renderedImageForControlledImage): Deleted.
(WebContextMenuClient::clearSharingServicePickerController): Deleted.
Have WebContextMenuClient implement WebSharingServicePickerClient to avoid
having WebSharingServicePickerController tightly bound to it.
Make an NSItemProvider array when instantiating WebSharingServicePickerController
instead of handing it data directly.

* WebCoreSupport/WebSelectionServiceController.h: Copied from Source/WebKit/mac/WebCoreSupport/WebContextMenuClient.h.
* WebCoreSupport/WebSelectionServiceController.mm: Added.
(WebSelectionServiceController::WebSelectionServiceController):
Add WebSelectionServiceController, which will act as the WebSharingServicePickerClient
for selection services.

(WebSelectionServiceController::handleSelectionServiceClick):
Create a WebSharingSericePickerController with the selection's attributed string, and show its menu.

(hasCompatibleServicesForItems):
(WebSelectionServiceController::hasRelevantSelectionServices):
Determine whether or not services are available.

(WebSelectionServiceController::sharingServicePickerWillBeDestroyed):
(WebSelectionServiceController::pageForSharingServicePicker):
(WebSelectionServiceController::windowForSharingServicePicker):
Implement the rest of the WebSharingServicePickerClient overrides.

(WebSelectionServiceController::screenRectForCurrentSharingServicePickerItem):
(WebSelectionServiceController::imageForCurrentSharingServicePickerItem):
These are only useful for image sharing services, so we don't need to
implement them (WebContextMenuClient does, on the other hand).

* WebView/WebView.mm:
(-[WebView _selectionServiceController]):
* WebView/WebViewData.h:
* WebView/WebViewData.mm:
* WebView/WebViewInternal.h:
Keep a WebSelectionServiceController.

* UIProcess/mac/WebContextMenuProxyMac.mm:
(-[WKSharingServicePickerDelegate sharingService:didShareItems:]):
Add a note that we should transition to using replaceSelectionWithAttributedString.

* WebProcess/WebPage/mac/WebPageMac.mm:
(WebKit::WebPage::handleSelectionServiceClick):
Allow editor services in editable-but-not-rich-text areas, just like in WebKit1.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@174791 268f45cc-cd09-0410-ab3c-d52691b4dbfc

23 files changed:
Source/WebCore/ChangeLog
Source/WebCore/WebCore.exp.in
Source/WebCore/editing/Editor.h
Source/WebCore/editing/ios/EditorIOS.mm
Source/WebCore/editing/mac/EditorMac.mm
Source/WebKit/ChangeLog
Source/WebKit/WebKit.xcodeproj/project.pbxproj
Source/WebKit/mac/ChangeLog
Source/WebKit/mac/Misc/WebSharingServicePickerController.h
Source/WebKit/mac/Misc/WebSharingServicePickerController.mm
Source/WebKit/mac/WebCoreSupport/WebChromeClient.h
Source/WebKit/mac/WebCoreSupport/WebChromeClient.mm
Source/WebKit/mac/WebCoreSupport/WebContextMenuClient.h
Source/WebKit/mac/WebCoreSupport/WebContextMenuClient.mm
Source/WebKit/mac/WebCoreSupport/WebSelectionServiceController.h [new file with mode: 0644]
Source/WebKit/mac/WebCoreSupport/WebSelectionServiceController.mm [new file with mode: 0644]
Source/WebKit/mac/WebView/WebView.mm
Source/WebKit/mac/WebView/WebViewData.h
Source/WebKit/mac/WebView/WebViewData.mm
Source/WebKit/mac/WebView/WebViewInternal.h
Source/WebKit2/ChangeLog
Source/WebKit2/UIProcess/mac/WebContextMenuProxyMac.mm
Source/WebKit2/WebProcess/WebPage/mac/WebPageMac.mm

index 05eec7af425e10ef328b9e068be8b953fa813708..5eb39ad1fe696fe84b1d342ac38bf1e8bb91c6b9 100644 (file)
@@ -1,3 +1,21 @@
+2014-10-16  Tim Horton  <timothy_horton@apple.com>
+
+        Implement selection services menu for Legacy WebKit
+        https://bugs.webkit.org/show_bug.cgi?id=137582
+        <rdar://problem/18604241>
+
+        Reviewed by Brady Eidson.
+
+        * WebCore.exp.in:
+        * editing/Editor.h:
+        * editing/ios/EditorIOS.mm:
+        (WebCore::Editor::replaceSelectionWithAttributedString):
+        * editing/mac/EditorMac.mm:
+        (WebCore::Editor::replaceSelectionWithAttributedString):
+        Add replaceSelectionWithAttributedString, which replaces the selection
+        with the given attributed string (converting to plain text if the destination
+        is not richly editable).
+
 2014-10-16  Alexey Proskuryakov  <ap@apple.com>
 
         Crashes in ResourceHandleCFURLConnectionDelegateWithOperationQueue due to unimplemented retain/release
index d154131d2cf1b220070b411316305381c6714f9f..3f2cdd4169e2f374796e39b44032e79b41298c75 100644 (file)
@@ -1250,6 +1250,7 @@ __ZN7WebCore6Editor33increaseSelectionListLevelOrderedEv
 __ZN7WebCore6Editor34setMarkedTextMatchesAreHighlightedEb
 __ZN7WebCore6Editor35increaseSelectionListLevelUnorderedEv
 __ZN7WebCore6Editor35setIgnoreCompositionSelectionChangeEb
+__ZN7WebCore6Editor36replaceSelectionWithAttributedStringEP18NSAttributedStringNS_22MailBlockquoteHandlingE
 __ZN7WebCore6Editor38commandIsSupportedFromMenuOrKeyBindingERKN3WTF6StringE
 __ZN7WebCore6Editor39insertParagraphSeparatorInQuotedContentEv
 __ZN7WebCore6Editor3cutEv
index e8c91d38c9249a3025ee0af18f7f7509b076d093..9b144b19587adef9933c9f124bcfa976083be475 100644 (file)
@@ -432,7 +432,7 @@ public:
     WEBCORE_EXPORT static RenderStyle* styleForSelectionStart(Frame* , Node *&nodeToRemove);
     WEBCORE_EXPORT bool insertParagraphSeparatorInQuotedContent();
     WEBCORE_EXPORT const SimpleFontData* fontForSelection(bool&) const;
-    WEBCORE_EXPORT NSDictionaryfontAttributesForSelectionStart() const;
+    WEBCORE_EXPORT NSDictionary *fontAttributesForSelectionStart() const;
     WEBCORE_EXPORT String stringSelectionForPasteboard();
     String stringSelectionForPasteboardWithImageAltText();
 #if !PLATFORM(IOS)
@@ -442,6 +442,7 @@ public:
     WEBCORE_EXPORT void replaceNodeFromPasteboard(Node*, const String& pasteboardName);
     WEBCORE_EXPORT PassRefPtr<SharedBuffer> dataSelectionForPasteboard(const String& pasteboardName);
 #endif // !PLATFORM(IOS)
+    WEBCORE_EXPORT void replaceSelectionWithAttributedString(NSAttributedString *, MailBlockquoteHandling = MailBlockquoteHandling::RespectBlockquote);
 #endif
 
 #if PLATFORM(COCOA) || PLATFORM(EFL) || PLATFORM(GTK)
index b8b0fc56668fb898e9f73e24c01927c7ab7ab4b0..0192d5e9011a2025e57c6c0c4ed31a086a1edb23 100644 (file)
@@ -625,4 +625,20 @@ PassRefPtr<DocumentFragment> Editor::createFragmentForImageResourceAndAddResourc
     return fragment.release();
 }
 
+void Editor::replaceSelectionWithAttributedString(NSAttributedString *attributedString, MailBlockquoteHandling mailBlockquoteHandling)
+{
+    if (m_frame.selection().isNone())
+        return;
+
+    if (m_frame.selection().selection().isContentRichlyEditable()) {
+        RefPtr<DocumentFragment> fragment = createFragmentAndAddResources(attributedString);
+        if (fragment && shouldInsertFragment(fragment, selectedRange(), EditorInsertActionPasted))
+            pasteAsFragment(fragment, false, false, mailBlockquoteHandling);
+    } else {
+        String text = [attributedString string];
+        if (shouldInsertText(text, selectedRange().get(), EditorInsertActionPasted))
+            pasteAsPlainText(text, false);
+    }
+}
+
 } // namespace WebCore
index 180a4f0c7f17d833bedbea096f87484dc01e9ba7..6e4190f9b653c42bfe1922e1322809e8c41ababc 100644 (file)
@@ -666,4 +666,20 @@ PassRefPtr<DocumentFragment> Editor::createFragmentAndAddResources(NSAttributedS
     return fragment.release();
 }
 
+void Editor::replaceSelectionWithAttributedString(NSAttributedString *attributedString, MailBlockquoteHandling mailBlockquoteHandling)
+{
+    if (m_frame.selection().isNone())
+        return;
+
+    if (m_frame.selection().selection().isContentRichlyEditable()) {
+        RefPtr<DocumentFragment> fragment = createFragmentAndAddResources(attributedString);
+        if (fragment && shouldInsertFragment(fragment, selectedRange(), EditorInsertActionPasted))
+            pasteAsFragment(fragment, false, false, mailBlockquoteHandling);
+    } else {
+        String text = [attributedString string];
+        if (shouldInsertText(text, selectedRange().get(), EditorInsertActionPasted))
+            pasteAsPlainText(text, false);
+    }
+}
+
 } // namespace WebCore
index 81574948ae18559ff377a5c30d1b202626958b25..e6b99dfd71bb759e9186b9ab603bb5fa3da4e74a 100644 (file)
@@ -1,3 +1,13 @@
+2014-10-16  Tim Horton  <timothy_horton@apple.com>
+
+        Implement selection services menu for Legacy WebKit
+        https://bugs.webkit.org/show_bug.cgi?id=137582
+        <rdar://problem/18604241>
+
+        Reviewed by Brady Eidson.
+
+        * WebKit.xcodeproj/project.pbxproj:
+
 2014-10-14  Brent Fulgham  <bfulgham@apple.com>
 
         [Win] Unreviewed gardening. Ignore Visual Studio *.sdf files.
index 5044688f91bd667e8f0ed0f13023b6237a6b358d..e6ab80af5f6984cdc5c3dff346d4e303f68daec3 100644 (file)
@@ -81,6 +81,8 @@
                29AEF960134C76FB00FE5096 /* OutlookQuirksUserScript.js in Resources */ = {isa = PBXBuildFile; fileRef = 29AEF95D134C755400FE5096 /* OutlookQuirksUserScript.js */; };
                2D25396618CE85C200270222 /* WebSharingServicePickerController.h in Headers */ = {isa = PBXBuildFile; fileRef = 2D25396418CE85C200270222 /* WebSharingServicePickerController.h */; };
                2D25396718CE85C200270222 /* WebSharingServicePickerController.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2D25396518CE85C200270222 /* WebSharingServicePickerController.mm */; };
+               2DD632C219E5D1F0002E9C7B /* WebSelectionServiceController.h in Headers */ = {isa = PBXBuildFile; fileRef = 2DD632C019E5D1F0002E9C7B /* WebSelectionServiceController.h */; };
+               2DD632C319E5D1F0002E9C7B /* WebSelectionServiceController.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2DD632C119E5D1F0002E9C7B /* WebSelectionServiceController.mm */; };
                312E2FE514E48182007CCA18 /* WebNotification.h in Headers */ = {isa = PBXBuildFile; fileRef = 312E2FE314E48182007CCA18 /* WebNotification.h */; settings = {ATTRIBUTES = (Private, ); }; };
                312E2FE614E48182007CCA18 /* WebNotification.mm in Sources */ = {isa = PBXBuildFile; fileRef = 312E2FE414E48182007CCA18 /* WebNotification.mm */; };
                312E2FE914E48215007CCA18 /* WebNotificationInternal.h in Headers */ = {isa = PBXBuildFile; fileRef = 312E2FE814E48215007CCA18 /* WebNotificationInternal.h */; };
                0AEBFF610F9FA8BE000D486B /* WebNetscapeContainerCheckPrivate.h */ = {isa = PBXFileReference; explicitFileType = sourcecode.c.h; fileEncoding = 4; path = WebNetscapeContainerCheckPrivate.h; sourceTree = "<group>"; };
                0AEBFF620F9FA8BE000D486B /* WebNetscapeContainerCheckPrivate.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WebNetscapeContainerCheckPrivate.mm; sourceTree = "<group>"; };
                14D8252D0AF955090004F057 /* WebChromeClient.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = WebChromeClient.h; sourceTree = "<group>"; };
-               14D8252E0AF955090004F057 /* WebChromeClient.mm */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.objcpp; path = WebChromeClient.mm; sourceTree = "<group>"; };
+               14D8252E0AF955090004F057 /* WebChromeClient.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WebChromeClient.mm; sourceTree = "<group>"; };
                1A2DBE9D0F251E3A0036F8A6 /* ProxyInstance.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ProxyInstance.h; sourceTree = "<group>"; };
                1A2DBE9E0F251E3A0036F8A6 /* ProxyInstance.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ProxyInstance.mm; sourceTree = "<group>"; };
                1A4DF5200EC8C74D006BD4B4 /* WebNetscapePluginView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebNetscapePluginView.h; sourceTree = "<group>"; };
                2D36FD5E03F78F9E00A80166 /* WebFormDelegatePrivate.h */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 4; lastKnownFileType = sourcecode.c.h; path = WebFormDelegatePrivate.h; sourceTree = "<group>"; tabWidth = 8; usesTabs = 0; };
                2D81DAB203EB0B2D00A80166 /* WebFormDelegate.h */ = {isa = PBXFileReference; fileEncoding = 30; indentWidth = 4; lastKnownFileType = sourcecode.c.h; path = WebFormDelegate.h; sourceTree = "<group>"; tabWidth = 8; usesTabs = 0; };
                2D81DAB303EB0B2D00A80166 /* WebFormDelegate.m */ = {isa = PBXFileReference; fileEncoding = 30; indentWidth = 4; lastKnownFileType = sourcecode.c.objc; path = WebFormDelegate.m; sourceTree = "<group>"; tabWidth = 8; usesTabs = 0; };
+               2DD632C019E5D1F0002E9C7B /* WebSelectionServiceController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebSelectionServiceController.h; sourceTree = "<group>"; };
+               2DD632C119E5D1F0002E9C7B /* WebSelectionServiceController.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WebSelectionServiceController.mm; sourceTree = "<group>"; };
                312E2FE314E48182007CCA18 /* WebNotification.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebNotification.h; sourceTree = "<group>"; };
                312E2FE414E48182007CCA18 /* WebNotification.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WebNotification.mm; sourceTree = "<group>"; };
                312E2FE814E48215007CCA18 /* WebNotificationInternal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebNotificationInternal.h; sourceTree = "<group>"; };
                                A5DEFC0711D5331C00885273 /* WebSecurityOrigin.mm */,
                                A5DEFC0811D5331C00885273 /* WebSecurityOriginInternal.h */,
                                A5DEFC0911D5331C00885273 /* WebSecurityOriginPrivate.h */,
+                               2DD632C019E5D1F0002E9C7B /* WebSelectionServiceController.h */,
+                               2DD632C119E5D1F0002E9C7B /* WebSelectionServiceController.mm */,
                                93EB178E09F88D510091F8FF /* WebSystemInterface.h */,
                                93EB178C09F88D460091F8FF /* WebSystemInterface.mm */,
                                1AB1DAC018BC0232004B6A9F /* WebViewGroup.h */,
                                7C01CB87173435C900C5D807 /* SearchPopupMenuMac.h in Headers */,
                                A10C1D6F1820300E0036883A /* WebGeolocationInternal.h in Headers */,
                                CEDA12DC152CBE6800D9E08D /* WebAlternativeTextClient.h in Headers */,
+                               2DD632C219E5D1F0002E9C7B /* WebSelectionServiceController.h in Headers */,
                                B6CE5C25100BC5F500219936 /* WebApplicationCache.h in Headers */,
                                A10C1D721820300E0036883A /* WebMIMETypeRegistry.h in Headers */,
                                A5DEFC1311D5344B00885273 /* WebApplicationCacheQuotaManager.h in Headers */,
                                1CCFFD130B1F81F2002EE926 /* OldWebAssertions.c in Sources */,
                                A10C1D8E1820305E0036883A /* WebPDFViewPlaceholder.mm in Sources */,
                                7C01CB86173435C900C5D807 /* PopupMenuMac.mm in Sources */,
+                               2DD632C319E5D1F0002E9C7B /* WebSelectionServiceController.mm in Sources */,
                                A10C1D771820300E0036883A /* WebVisiblePosition.mm in Sources */,
                                1A2DBEA00F251E3A0036F8A6 /* ProxyInstance.mm in Sources */,
                                070F549C17F1E42B00169E04 /* WebUserMediaClient.mm in Sources */,
index ef73304e0e79b37f935385be70c96eac15d523fe..20c80bf24357ab21167b7917c678a5f17425a603 100644 (file)
@@ -1,3 +1,85 @@
+2014-10-16  Tim Horton  <timothy_horton@apple.com>
+
+        Implement selection services menu for Legacy WebKit
+        https://bugs.webkit.org/show_bug.cgi?id=137582
+        <rdar://problem/18604241>
+
+        Reviewed by Brady Eidson.
+
+        * Misc/WebSharingServicePickerController.h:
+        Move SPI into the header (in the near future it will be moved to a modern-style SPI header).
+        Add WebSharingServicePickerClient, which other classes can implement
+        instead of tying WebSharingServicePickerController strongly to WebContextMenuClient.
+        Make initWithData:... -> initWithItems:... instead, taking an array of NSItemProviders.
+
+        * Misc/WebSharingServicePickerController.mm:
+        Pass in NSSharingServicePickerStyle, so that clients can specify the style.
+
+        (-[WebSharingServicePickerController clear]):
+        (-[WebSharingServicePickerController didShareImageData:confirmDataIsValidTIFFData:]):
+        (-[WebSharingServicePickerController sharingServicePicker:didChooseSharingService:]):
+        (-[WebSharingServicePickerController sharingService:sourceFrameOnScreenForShareItem:]):
+        (-[WebSharingServicePickerController sharingService:transitionImageForShareItem:contentRect:]):
+        (-[WebSharingServicePickerController sharingService:sourceWindowForShareItems:sharingContentScope:]):
+        Make use of WebSharingServicePickerClient.
+
+        (-[WebSharingServicePickerController sharingService:didShareItems:]):
+        If we're returned a NSAttributedString, paste it on top of the current selection,
+        similar to what the WebKit2 implementation currently does.
+        
+        * WebCoreSupport/WebChromeClient.h:
+        * WebCoreSupport/WebChromeClient.mm:
+        (WebChromeClient::handleTelephoneNumberClick):
+        (WebChromeClient::handleSelectionServiceClick):
+        (WebChromeClient::hasRelevantSelectionServices):
+        Override the services-related ChromeClient functions, forward them to WebSelectionServiceController.
+
+        * WebCoreSupport/WebContextMenuClient.h:
+        * WebCoreSupport/WebContextMenuClient.mm:
+        (WebContextMenuClient::sharingServicePickerWillBeDestroyed):
+        (WebContextMenuClient::pageForSharingServicePicker):
+        (WebContextMenuClient::windowForSharingServicePicker):
+        (WebContextMenuClient::screenRectForCurrentSharingServicePickerItem):
+        (WebContextMenuClient::imageForCurrentSharingServicePickerItem):
+        (WebContextMenuClient::contextMenuForEvent):
+        (WebContextMenuClient::screenRectForHitTestNode): Deleted.
+        (WebContextMenuClient::renderedImageForControlledImage): Deleted.
+        (WebContextMenuClient::clearSharingServicePickerController): Deleted.
+        Have WebContextMenuClient implement WebSharingServicePickerClient to avoid
+        having WebSharingServicePickerController tightly bound to it.
+        Make an NSItemProvider array when instantiating WebSharingServicePickerController
+        instead of handing it data directly.
+
+        * WebCoreSupport/WebSelectionServiceController.h: Copied from Source/WebKit/mac/WebCoreSupport/WebContextMenuClient.h.
+        * WebCoreSupport/WebSelectionServiceController.mm: Added.
+        (WebSelectionServiceController::WebSelectionServiceController):
+        Add WebSelectionServiceController, which will act as the WebSharingServicePickerClient
+        for selection services.
+
+        (WebSelectionServiceController::handleSelectionServiceClick):
+        Create a WebSharingSericePickerController with the selection's attributed string, and show its menu.
+
+        (hasCompatibleServicesForItems):
+        (WebSelectionServiceController::hasRelevantSelectionServices):
+        Determine whether or not services are available.
+
+        (WebSelectionServiceController::sharingServicePickerWillBeDestroyed):
+        (WebSelectionServiceController::pageForSharingServicePicker):
+        (WebSelectionServiceController::windowForSharingServicePicker):
+        Implement the rest of the WebSharingServicePickerClient overrides.
+
+        (WebSelectionServiceController::screenRectForCurrentSharingServicePickerItem):
+        (WebSelectionServiceController::imageForCurrentSharingServicePickerItem):
+        These are only useful for image sharing services, so we don't need to
+        implement them (WebContextMenuClient does, on the other hand).
+
+        * WebView/WebView.mm:
+        (-[WebView _selectionServiceController]):
+        * WebView/WebViewData.h:
+        * WebView/WebViewData.mm:
+        * WebView/WebViewInternal.h:
+        Keep a WebSelectionServiceController.
+
 2014-10-14  Myles C. Maxfield  <mmaxfield@apple.com>
 
         Improve debug description of WAKViews
index 8aa8a9df34f34c6cc8ab3a1e88c810402e706802..ab33afeb225c913026091c7113eb0adc8f7368be 100644 (file)
 #import <wtf/RetainPtr.h>
 
 @class NSSharingServicePicker;
+@class WebSharingServicePickerController;
 @protocol NSSharingServiceDelegate;
 @protocol NSSharingServicePickerDelegate;
 
+#if __has_include(<AppKit/NSSharingService_Private.h>)
+#import <AppKit/NSSharingService_Private.h>
+#else
+typedef NS_ENUM(NSInteger, NSSharingServicePickerStyle) {
+    NSSharingServicePickerStyleMenu = 0,
+    NSSharingServicePickerStyleRollover = 1,
+    NSSharingServicePickerStyleTextSelection = 2,
+    NSSharingServicePickerStyleDataDetector = 3
+} NS_ENUM_AVAILABLE_MAC(10_10);
+
+typedef NS_ENUM(NSInteger, NSSharingServiceType) {
+    NSSharingServiceTypeShare = 0,
+    NSSharingServiceTypeViewer = 1,
+    NSSharingServiceTypeEditor = 2
+} NS_ENUM_AVAILABLE_MAC(10_10);
+
+typedef NSUInteger NSSharingServiceMask;
+#endif
+
+namespace WebCore {
+class FloatRect;
+class Page;
+}
+
 class WebContextMenuClient;
 
+class WebSharingServicePickerClient {
+public:
+    virtual ~WebSharingServicePickerClient() { }
+
+    virtual void sharingServicePickerWillBeDestroyed(WebSharingServicePickerController &) = 0;
+    virtual WebCore::Page* pageForSharingServicePicker(WebSharingServicePickerController &) = 0;
+    virtual RetainPtr<NSWindow> windowForSharingServicePicker(WebSharingServicePickerController &) = 0;
+
+    virtual WebCore::FloatRect screenRectForCurrentSharingServicePickerItem(WebSharingServicePickerController &) = 0;
+    virtual RetainPtr<NSImage> imageForCurrentSharingServicePickerItem(WebSharingServicePickerController &) = 0;
+};
+
 @interface WebSharingServicePickerController : NSObject <NSSharingServiceDelegate, NSSharingServicePickerDelegate> {
-    WebContextMenuClient* _menuClient;
+    WebSharingServicePickerClient* _pickerClient;
     RetainPtr<NSSharingServicePicker> _picker;
     BOOL _includeEditorServices;
 }
 
-- (instancetype)initWithData:(NSData *)data includeEditorServices:(BOOL)includeEditorServices menuClient:(WebContextMenuClient*)menuClient;
+- (instancetype)initWithItems:(NSArray *)items includeEditorServices:(BOOL)includeEditorServices client:(WebSharingServicePickerClient*)pickerClient style:(NSSharingServicePickerStyle)style;
 - (NSMenu *)menu;
 - (void)didShareImageData:(NSData *)data confirmDataIsValidTIFFData:(BOOL)confirmData;
 - (void)clear;
index beab0ccd5620590f46ce8884cb7edf4ea0f380b4..097608a8d4eb834cf27b391cb4c231c431cbd0a3 100644 (file)
 #import <WebCore/ContextMenuController.h>
 #import <WebCore/Page.h>
 
-#if __has_include(<AppKit/NSSharingService_Private.h>)
-#import <AppKit/NSSharingService_Private.h>
-#else
-typedef enum {
-    NSSharingServicePickerStyleRollover = 1
-} NSSharingServicePickerStyle;
-
-typedef enum {
-    NSSharingServiceTypeEditor = 2
-} NSSharingServiceType;
-
-typedef NSUInteger NSSharingServiceMask;
-#endif
-
 @interface NSSharingServicePicker (Private)
 @property NSSharingServicePickerStyle style;
 - (NSMenu *)menu;
@@ -79,7 +65,7 @@ using namespace WebCore;
 
 @implementation WebSharingServicePickerController
 
-- (instancetype)initWithData:(NSData *)data includeEditorServices:(BOOL)includeEditorServices menuClient:(WebContextMenuClient*)menuClient
+- (instancetype)initWithItems:(NSArray *)items includeEditorServices:(BOOL)includeEditorServices client:(WebSharingServicePickerClient*)pickerClient style:(NSSharingServicePickerStyle)style
 {
 #ifndef __LP64__
     return nil;
@@ -87,14 +73,12 @@ using namespace WebCore;
     if (!(self = [super init]))
         return nil;
 
-    RetainPtr<NSItemProvider> itemProvider = adoptNS([[NSItemProvider alloc] initWithItem:data typeIdentifier:@"public.data"]);
-
-    _picker = adoptNS([[NSSharingServicePicker alloc] initWithItems:@[ itemProvider.get() ]]);
-    [_picker setStyle:NSSharingServicePickerStyleRollover];
+    _picker = adoptNS([[NSSharingServicePicker alloc] initWithItems:items]);
+    [_picker setStyle:style];
     [_picker setDelegate:self];
 
     _includeEditorServices = includeEditorServices;
-    _menuClient = menuClient;
+    _pickerClient = pickerClient;
 
     return self;
 #endif
@@ -105,11 +89,11 @@ using namespace WebCore;
     // Protect self from being dealloc'ed partway through this method.
     RetainPtr<WebSharingServicePickerController> protector(self);
 
-    if (_menuClient)
-        _menuClient->clearSharingServicePickerController();
+    if (_pickerClient)
+        _pickerClient->sharingServicePickerWillBeDestroyed(*self);
 
     _picker = nullptr;
-    _menuClient = nullptr;
+    _pickerClient = nullptr;
 }
 
 - (NSMenu *)menu
@@ -119,7 +103,7 @@ using namespace WebCore;
 
 - (void)didShareImageData:(NSData *)data confirmDataIsValidTIFFData:(BOOL)confirmData
 {
-    Page* page = [_menuClient->webView() page];
+    Page* page = _pickerClient->pageForSharingServicePicker(*self);
     if (!page)
         return;
 
@@ -170,13 +154,18 @@ using namespace WebCore;
 - (void)sharingServicePicker:(NSSharingServicePicker *)sharingServicePicker didChooseSharingService:(NSSharingService *)service
 {
     if (!service)
-        _menuClient->clearSharingServicePickerController();
+        _pickerClient->sharingServicePickerWillBeDestroyed(*self);
 }
 
 #pragma mark NSSharingServiceDelegate methods
 
 - (void)sharingService:(NSSharingService *)sharingService didShareItems:(NSArray *)items
 {
+    // We only care about what item was shared if we were interested in editor services
+    // (i.e., if we plan on replacing the selection with the returned item)
+    if (!_includeEditorServices)
+        return;
+
     // We only send one item, so we should only get one item back.
     if ([items count] != 1)
         return;
@@ -208,8 +197,11 @@ using namespace WebCore;
         }];
     }
 #endif
-    else
-        LOG_ERROR("sharingService:didShareItems: - Unknown item type returned");
+    else if ([item isKindOfClass:[NSAttributedString class]]) {
+        Frame& frame = _pickerClient->pageForSharingServicePicker(*self)->focusController().focusedOrMainFrame();
+        frame.editor().replaceSelectionWithAttributedString(item);
+    } else
+        LOG_ERROR("sharingService:didShareItems: - Unknown item type returned\n");
 }
 
 - (void)sharingService:(NSSharingService *)sharingService didFailToShareItems:(NSArray *)items error:(NSError *)error
@@ -219,23 +211,23 @@ using namespace WebCore;
 
 - (NSRect)sharingService:(NSSharingService *)sharingService sourceFrameOnScreenForShareItem:(id <NSPasteboardWriting>)item
 {
-    if (!_menuClient)
+    if (!_pickerClient)
         return NSZeroRect;
 
-    return _menuClient->screenRectForHitTestNode();
+    return _pickerClient->screenRectForCurrentSharingServicePickerItem(*self);
 }
 
 - (NSImage *)sharingService:(NSSharingService *)sharingService transitionImageForShareItem:(id <NSPasteboardWriting>)item contentRect:(NSRect *)contentRect
 {
-    if (!_menuClient)
+    if (!_pickerClient)
         return nil;
 
-    return _menuClient->renderedImageForControlledImage();
+    return _pickerClient->imageForCurrentSharingServicePickerItem(*self).get();
 }
 
 - (NSWindow *)sharingService:(NSSharingService *)sharingService sourceWindowForShareItems:(NSArray *)items sharingContentScope:(NSSharingContentScope *)sharingContentScope
 {
-    return [_menuClient->webView() window];
+    return _pickerClient->windowForSharingServicePicker(*self).get();
 }
 
 @end
index 7270ce131770cbafa57a12147ec46f0e7d4c4a28..f7f72f75d4515890c2a33828b0b4f9cefe350aa8 100644 (file)
@@ -205,6 +205,11 @@ public:
     virtual bool unwrapCryptoKey(const Vector<uint8_t>&, Vector<uint8_t>&) const override;
 #endif
 
+#if ENABLE(SERVICE_CONTROLS)
+    virtual void handleSelectionServiceClick(WebCore::FrameSelection&, const Vector<String>& telephoneNumbers, const WebCore::IntPoint&) override;
+    virtual bool hasRelevantSelectionServices(bool isTextOnly) const override;
+#endif
+
 #if PLATFORM(IOS)
     WebView* webView() const { return m_webView; }
 #else
@@ -212,6 +217,5 @@ public:
 #endif
 
 private:
-
     WebView *m_webView;
 };
index 3216807afb765ac0aaf87fbea9c17aeadf9ceed4..97de280d0d2c6a07f9a9e411b8edae1051fe23dc 100644 (file)
@@ -49,6 +49,7 @@
 #import "WebPlugin.h"
 #import "WebQuotaManager.h"
 #import "WebSecurityOriginInternal.h"
+#import "WebSelectionServiceController.h"
 #import "WebUIDelegatePrivate.h"
 #import "WebView.h"
 #import "WebViewInternal.h"
@@ -1039,3 +1040,16 @@ bool WebChromeClient::unwrapCryptoKey(const Vector<uint8_t>& wrappedKey, Vector<
     return unwrapSerializedCryptoKey(masterKey, wrappedKey, key);
 }
 #endif
+
+#if ENABLE(SERVICE_CONTROLS)
+void WebChromeClient::handleSelectionServiceClick(WebCore::FrameSelection& selection, const Vector<String>& telephoneNumbers, const WebCore::IntPoint& point)
+{
+    [m_webView _selectionServiceController].handleSelectionServiceClick(selection, telephoneNumbers, point);
+}
+
+bool WebChromeClient::hasRelevantSelectionServices(bool isTextOnly) const
+{
+    return [m_webView _selectionServiceController].hasRelevantSelectionServices(isTextOnly);
+}
+
+#endif
index 5a7b22d432cf32aab118e148a1443e835cbd1130..265a83af77e9b74b5e35271bbfaf79c46b1d3f6a 100644 (file)
@@ -26,6 +26,7 @@
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#import "WebSharingServicePickerController.h"
 #import <WebCore/ContextMenuClient.h>
 #import <WebCore/IntRect.h>
 
@@ -36,10 +37,14 @@ namespace WebCore {
 class Node;
 }
 
-class WebContextMenuClient : public WebCore::ContextMenuClient {
+class WebContextMenuClient : public WebCore::ContextMenuClient
+#if ENABLE(SERVICE_CONTROLS)
+, public WebSharingServicePickerClient
+#endif
+{
 public:
     WebContextMenuClient(WebView *webView);
-    ~WebContextMenuClient();
+    virtual ~WebContextMenuClient();
 
     virtual void contextMenuDestroyed() override;
     
@@ -55,11 +60,13 @@ public:
     virtual void searchWithSpotlight() override;
     virtual void showContextMenu() override;
 
-    NSRect screenRectForHitTestNode() const;
-
 #if ENABLE(SERVICE_CONTROLS)
-    void clearSharingServicePickerController();
-    NSImage *renderedImageForControlledImage() const;
+    // WebSharingServicePickerClient
+    virtual void sharingServicePickerWillBeDestroyed(WebSharingServicePickerController &) override;
+    virtual WebCore::Page* pageForSharingServicePicker(WebSharingServicePickerController &) override;
+    virtual RetainPtr<NSWindow> windowForSharingServicePicker(WebSharingServicePickerController &) override;
+    virtual WebCore::FloatRect screenRectForCurrentSharingServicePickerItem(WebSharingServicePickerController &) override;
+    virtual RetainPtr<NSImage> imageForCurrentSharingServicePickerItem(WebSharingServicePickerController &) override;
 #endif
 
     WebView *webView() { return m_webView; }
index 94b968d2ab7ea3a29d17ee5d1553a2c762bbfa5e..18f93e02bdcf090743365443f7e895a2b44b06e4 100644 (file)
@@ -387,7 +387,23 @@ bool WebContextMenuClient::clientFloatRectForNode(Node& node, FloatRect& rect) c
     return true;
 }
 
-NSRect WebContextMenuClient::screenRectForHitTestNode() const
+#if ENABLE(SERVICE_CONTROLS)
+void WebContextMenuClient::sharingServicePickerWillBeDestroyed(WebSharingServicePickerController &)
+{
+    m_sharingServicePickerController = nil;
+}
+
+WebCore::Page* WebContextMenuClient::pageForSharingServicePicker(WebSharingServicePickerController &)
+{
+    return [m_webView page];
+}
+
+RetainPtr<NSWindow> WebContextMenuClient::windowForSharingServicePicker(WebSharingServicePickerController &)
+{
+    return [m_webView window];
+}
+
+WebCore::FloatRect WebContextMenuClient::screenRectForCurrentSharingServicePickerItem(WebSharingServicePickerController &)
 {
     Page* page = [m_webView page];
     if (!page)
@@ -415,8 +431,7 @@ NSRect WebContextMenuClient::screenRectForHitTestNode() const
     return frameView->contentsToScreen(intRect);
 }
 
-#if ENABLE(SERVICE_CONTROLS)
-NSImage *WebContextMenuClient::renderedImageForControlledImage() const
+RetainPtr<NSImage> WebContextMenuClient::imageForCurrentSharingServicePickerItem(WebSharingServicePickerController &)
 {
     Page* page = [m_webView page];
     if (!page)
@@ -455,11 +470,10 @@ NSImage *WebContextMenuClient::renderedImageForControlledImage() const
     frameView->setPaintBehavior(oldPaintBehavior);
 
     RefPtr<Image> image = buffer->copyImage(DontCopyBackingStore);
-    return [[image->getNSImage() retain] autorelease];
+    return image->getNSImage();
 }
 #endif
 
-
 NSMenu *WebContextMenuClient::contextMenuForEvent(NSEvent *event, NSView *view, bool& isServicesMenu)
 {
     isServicesMenu = false;
@@ -472,12 +486,10 @@ NSMenu *WebContextMenuClient::contextMenuForEvent(NSEvent *event, NSView *view,
     if (Image* image = page->contextMenuController().context().controlledImage()) {
         ASSERT(page->contextMenuController().context().hitTestResult().innerNode());
 
-        RefPtr<SharedBuffer> data = image->data();
-        ASSERT(data);
-        RetainPtr<CFDataRef> cfData = data->createCFData();
+        RetainPtr<NSItemProvider> itemProvider = adoptNS([[NSItemProvider alloc] initWithItem:image->getNSImage() typeIdentifier:@"public.image"]);
 
         bool isContentEditable = page->contextMenuController().context().hitTestResult().innerNode()->isContentEditable();
-        m_sharingServicePickerController = adoptNS([[WebSharingServicePickerController alloc] initWithData:(NSData *)cfData.get() includeEditorServices:isContentEditable menuClient:this]);
+        m_sharingServicePickerController = adoptNS([[WebSharingServicePickerController alloc] initWithItems:@[ itemProvider.get() ] includeEditorServices:isContentEditable client:this style:NSSharingServicePickerStyleRollover]);
 
         isServicesMenu = true;
         return [m_sharingServicePickerController menu];
@@ -513,11 +525,4 @@ void WebContextMenuClient::showContextMenu()
     }
 }
 
-#if ENABLE(SERVICE_CONTROLS)
-void WebContextMenuClient::clearSharingServicePickerController()
-{
-    m_sharingServicePickerController = nil;
-}
-#endif
-
 #endif // !PLATFORM(IOS)
diff --git a/Source/WebKit/mac/WebCoreSupport/WebSelectionServiceController.h b/Source/WebKit/mac/WebCoreSupport/WebSelectionServiceController.h
new file mode 100644 (file)
index 0000000..d81bbe0
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2014 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef WebSelectionServiceController_h
+#define WebSelectionServiceController_h
+
+#if ENABLE(SERVICE_CONTROLS)
+
+#import "WebSharingServicePickerController.h"
+#import <wtf/RetainPtr.h>
+#import <wtf/Vector.h>
+#import <wtf/text/WTFString.h>
+
+OBJC_CLASS NSImage;
+OBJC_CLASS NSWindow;
+OBJC_CLASS WebView;
+
+namespace WebCore {
+class FrameSelection;
+class IntPoint;
+}
+
+class WebSelectionServiceController : public WebSharingServicePickerClient {
+public:
+    WebSelectionServiceController(WebView*);
+
+    void handleSelectionServiceClick(WebCore::FrameSelection&, const Vector<String>& telephoneNumbers, const WebCore::IntPoint&);
+    bool hasRelevantSelectionServices(bool isTextOnly) const;
+
+    // WebSharingServicePickerClient
+    virtual void sharingServicePickerWillBeDestroyed(WebSharingServicePickerController &) override;
+    virtual WebCore::Page* pageForSharingServicePicker(WebSharingServicePickerController &) override;
+    virtual RetainPtr<NSWindow> windowForSharingServicePicker(WebSharingServicePickerController &) override;
+    virtual WebCore::FloatRect screenRectForCurrentSharingServicePickerItem(WebSharingServicePickerController &) override;
+    virtual RetainPtr<NSImage> imageForCurrentSharingServicePickerItem(WebSharingServicePickerController &) override;
+
+private:
+    WebView *m_webView;
+
+    RetainPtr<WebSharingServicePickerController> m_sharingServicePickerController;
+};
+
+#endif // ENABLE(SERVICE_CONTROLS)
+
+#endif // WebSelectionServiceController_h
diff --git a/Source/WebKit/mac/WebCoreSupport/WebSelectionServiceController.mm b/Source/WebKit/mac/WebCoreSupport/WebSelectionServiceController.mm
new file mode 100644 (file)
index 0000000..1dd24cb
--- /dev/null
@@ -0,0 +1,129 @@
+/*
+ * Copyright (C) 2014 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import "WebSelectionServiceController.h"
+
+#if ENABLE(SERVICE_CONTROLS)
+
+#import "WebViewInternal.h"
+#import <WebCore/FrameSelection.h>
+#import <WebCore/HTMLConverter.h>
+#import <WebCore/Range.h>
+#import <wtf/NeverDestroyed.h>
+
+using namespace WebCore;
+
+#if __has_include(<AppKit/NSSharingService_Private.h>)
+#import <AppKit/NSSharingService_Private.h>
+#else
+typedef NS_ENUM(NSInteger, NSSharingServicePickerStyle) {
+    NSSharingServicePickerStyleMenu = 0,
+    NSSharingServicePickerStyleRollover = 1,
+    NSSharingServicePickerStyleTextSelection = 2,
+    NSSharingServicePickerStyleDataDetector = 3
+} NS_ENUM_AVAILABLE_MAC(10_10);
+#endif
+
+WebSelectionServiceController::WebSelectionServiceController(WebView *webView) 
+    : m_webView(webView)
+{
+}
+
+void WebSelectionServiceController::handleSelectionServiceClick(WebCore::FrameSelection& selection, const Vector<String>& telephoneNumbers, const WebCore::IntPoint& point)
+{
+    Page* page = [m_webView page];
+    if (!page)
+        return;
+
+    RefPtr<Range> range = selection.selection().firstRange();
+    if (!range)
+        return;
+
+    RetainPtr<NSAttributedString> attributedSelection = attributedStringFromRange(*range);
+    if (!attributedSelection)
+        return;
+
+    NSArray *items = @[ attributedSelection.get() ];
+
+    bool isEditable = selection.selection().isContentEditable();
+    m_sharingServicePickerController = adoptNS([[WebSharingServicePickerController alloc] initWithItems:items includeEditorServices:isEditable client:this style:NSSharingServicePickerStyleTextSelection]);
+
+    RetainPtr<NSMenu> menu = adoptNS([[m_sharingServicePickerController menu] copy]);
+
+    [menu setShowsStateColumn:YES];
+
+    [menu popUpMenuPositioningItem:nil atLocation:[m_webView convertPoint:point toView:nil] inView:m_webView];
+}
+
+static bool hasCompatibleServicesForItems(NSArray *items)
+{
+    return [NSSharingService sharingServicesForItems:items mask:NSSharingServiceMaskViewer | NSSharingServiceMaskEditor].count;
+}
+
+bool WebSelectionServiceController::hasRelevantSelectionServices(bool isTextOnly) const
+{
+    RetainPtr<NSAttributedString> attributedString = adoptNS([[NSAttributedString alloc] initWithString:@"a"]);
+
+    bool hasSelectionServices = hasCompatibleServicesForItems(@[ attributedString.get() ]);
+    if (isTextOnly && hasSelectionServices)
+        return true;
+
+    NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
+    RetainPtr<NSImage> image = adoptNS([[NSImage alloc] init]);
+    NSTextAttachmentCell *cell = [[NSTextAttachmentCell alloc] initImageCell:image.get()];
+    [attachment setAttachmentCell:cell];
+    RetainPtr<NSMutableAttributedString> attributedStringWithRichContent = adoptNS((NSMutableAttributedString *)[NSMutableAttributedString attributedStringWithAttachment:attachment]);
+    [attributedStringWithRichContent appendAttributedString:attributedString.get()];
+
+    bool hasRichContentServices = hasCompatibleServicesForItems(@[ attributedStringWithRichContent.get() ]);
+    return hasRichContentServices;
+}
+
+void WebSelectionServiceController::sharingServicePickerWillBeDestroyed(WebSharingServicePickerController &)
+{
+    m_sharingServicePickerController = nil;
+}
+
+WebCore::Page* WebSelectionServiceController::pageForSharingServicePicker(WebSharingServicePickerController &)
+{
+    return [m_webView page];
+}
+
+RetainPtr<NSWindow> WebSelectionServiceController::windowForSharingServicePicker(WebSharingServicePickerController &)
+{
+    return [m_webView window];
+}
+
+FloatRect WebSelectionServiceController::screenRectForCurrentSharingServicePickerItem(WebSharingServicePickerController &)
+{
+    return FloatRect();
+}
+
+RetainPtr<NSImage> WebSelectionServiceController::imageForCurrentSharingServicePickerItem(WebSharingServicePickerController &)
+{
+    return nil;
+}
+
+#endif
index 408536ae1beabf169f12dea16b978db351074fbc..86c49c6a7cb99b174e7b1fe488deab361e59aa7c 100644 (file)
@@ -97,6 +97,7 @@
 #import "WebProgressTrackerClient.h"
 #import "WebScriptDebugDelegate.h"
 #import "WebScriptWorldInternal.h"
+#import "WebSelectionServiceController.h"
 #import "WebStorageManagerInternal.h"
 #import "WebSystemInterface.h"
 #import "WebTextCompletionController.h"
@@ -8496,6 +8497,15 @@ static void glibContextIterationCallback(CFRunLoopObserverRef, CFRunLoopActivity
 }
 #endif
 
+#if ENABLE(SERVICE_CONTROLS)
+- (WebSelectionServiceController&)_selectionServiceController
+{
+    if (!_private->_selectionServiceController)
+        _private->_selectionServiceController = std::make_unique<WebSelectionServiceController>(self);
+    return *_private->_selectionServiceController;
+}
+#endif
+
 - (NSPoint)_convertPointFromRootView:(NSPoint)point
 {
     return NSMakePoint(point.x, [self bounds].size.height - point.y);
index a0be681fa2c1cc8c9bc9d1aa77482aa61ef97512..e32cd22ec46640a5eeda4e098a8052bf83bfb568 100644 (file)
@@ -82,6 +82,7 @@ extern int pluginDatabaseClientCount;
 
 class LayerFlushController;
 class WebViewGroup;
+class WebSelectionServiceController;
 
 class WebViewLayerFlushScheduler : public WebCore::LayerFlushScheduler {
 public:
@@ -270,6 +271,10 @@ private:
     id<WebUserMediaClient> m_userMediaClient;
 #endif
 
+#if ENABLE(SERVICE_CONTROLS)
+    std::unique_ptr<WebSelectionServiceController> _selectionServiceController;
+#endif
+
     RefPtr<WebCore::HistoryItem> _globalHistoryItem;
 
     BOOL interactiveFormValidationEnabled;
index e0636a5dc67cec9506c43cacc8fb1c953e1af493..b7cbae1bd3538873cfe3fc8b493d2175401b8a46 100644 (file)
@@ -31,6 +31,7 @@
 
 #import "WebKitLogging.h"
 #import "WebPreferenceKeysPrivate.h"
+#import "WebSelectionServiceController.h"
 #import "WebViewGroup.h"
 #import <WebCore/AlternativeTextUIController.h>
 #import <WebCore/WebCoreObjCExtras.h>
index 98397464de76dd9dadc85aa44322b03578e50c89..12a43be151eca6ef8da781a01560c0cf07fba012 100644 (file)
@@ -56,6 +56,8 @@ class RenderBox;
 class HTMLVideoElement;
 struct DictationAlternative;
 }
+
+class WebSelectionServiceController;
 #endif
 
 @class WebBasePluginPackage;
@@ -119,6 +121,10 @@ OBJC_CLASS NSTextAlternatives;
 - (Vector<String>)_dictationAlternatives:(uint64_t)dictationContext;
 #endif
 
+#if ENABLE(SERVICE_CONTROLS)
+- (WebSelectionServiceController&)_selectionServiceController;
+#endif
+
 @end
 
 #endif
index c1c077c2d7c9ee80332e71f2caccc029273e9b37..ed966273148949535fc8a60d6a6a304df4203eb5 100644 (file)
@@ -1,3 +1,19 @@
+2014-10-16  Tim Horton  <timothy_horton@apple.com>
+
+        Implement selection services menu for Legacy WebKit
+        https://bugs.webkit.org/show_bug.cgi?id=137582
+        <rdar://problem/18604241>
+
+        Reviewed by Brady Eidson.
+
+        * UIProcess/mac/WebContextMenuProxyMac.mm:
+        (-[WKSharingServicePickerDelegate sharingService:didShareItems:]):
+        Add a note that we should transition to using replaceSelectionWithAttributedString.
+
+        * WebProcess/WebPage/mac/WebPageMac.mm:
+        (WebKit::WebPage::handleSelectionServiceClick):
+        Allow editor services in editable-but-not-rich-text areas, just like in WebKit1.
+
 2014-10-16  Tim Horton  <timothy_horton@apple.com>
 
         Various crashes in ViewGestureControllerIOS when closing a tab while a swipe gesture is in progress
index 4e1c3ccb0eae833f55145b9183d537034851c3f6..58ad9917e33fb7cda21b06cacadb135b76105a2c 100644 (file)
@@ -275,6 +275,7 @@ using namespace WebCore;
         return;
     }
 
+    // FIXME: We should adopt replaceSelectionWithAttributedString instead of bouncing through the (fake) pasteboard.
     _menuProxy->page().replaceSelectionWithPasteboardData(types, dataReference);
 }
 
index e4a5f6d3df9d6c5df602e1e881397412b85e9c1c..e0b5581d22f4492313176c22043591d59b6d8b9d 100644 (file)
@@ -1039,7 +1039,7 @@ void WebPage::handleSelectionServiceClick(FrameSelection& selection, const Vecto
 
     NSData *selectionData = [attributedSelection RTFDFromRange:NSMakeRange(0, [attributedSelection length]) documentAttributes:nil];
     IPC::DataReference data = IPC::DataReference(reinterpret_cast<const uint8_t*>([selectionData bytes]), [selectionData length]);
-    bool isEditable = selection.selection().isContentRichlyEditable();
+    bool isEditable = selection.selection().isContentEditable();
 
     send(Messages::WebPageProxy::ShowSelectionServiceMenu(data, phoneNumbers, isEditable, point));
 }