Add support for lookup on iOS.
authorenrica@apple.com <enrica@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 28 Apr 2015 20:18:36 +0000 (20:18 +0000)
committerenrica@apple.com <enrica@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 28 Apr 2015 20:18:36 +0000 (20:18 +0000)
https://bugs.webkit.org/show_bug.cgi?id=144342
rdar://problem/19994090

Reviewed by Tim Horton.

* UIProcess/WebPageProxy.h:
* UIProcess/ios/WKContentViewInteraction.mm:
(-[WKContentView _lookup:]):
(-[WKContentView canPerformAction:withSender:]):
* UIProcess/ios/WebPageProxyIOS.mm:
(WebKit::WebPageProxy::getLookupContextAtPoint):
* WebProcess/WebPage/WebPage.h:
* WebProcess/WebPage/WebPage.messages.in:
* WebProcess/WebPage/ios/WebPageIOS.mm:
(WebKit::WebPage::getLookupContextAtPoint):

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

Source/WebKit2/ChangeLog
Source/WebKit2/UIProcess/WebPageProxy.h
Source/WebKit2/UIProcess/ios/WKContentViewInteraction.mm
Source/WebKit2/UIProcess/ios/WebPageProxyIOS.mm
Source/WebKit2/WebProcess/WebPage/WebPage.h
Source/WebKit2/WebProcess/WebPage/WebPage.messages.in
Source/WebKit2/WebProcess/WebPage/ios/WebPageIOS.mm

index 1396884..940a8a0 100644 (file)
@@ -1,3 +1,22 @@
+2015-04-28  Enrica Casucci  <enrica@apple.com>
+
+        Add support for lookup on iOS.
+        https://bugs.webkit.org/show_bug.cgi?id=144342
+        rdar://problem/19994090
+
+        Reviewed by Tim Horton.
+
+        * UIProcess/WebPageProxy.h:
+        * UIProcess/ios/WKContentViewInteraction.mm:
+        (-[WKContentView _lookup:]):
+        (-[WKContentView canPerformAction:withSender:]):
+        * UIProcess/ios/WebPageProxyIOS.mm:
+        (WebKit::WebPageProxy::getLookupContextAtPoint):
+        * WebProcess/WebPage/WebPage.h:
+        * WebProcess/WebPage/WebPage.messages.in:
+        * WebProcess/WebPage/ios/WebPageIOS.mm:
+        (WebKit::WebPage::getLookupContextAtPoint):
+
 2015-04-28  Brady Eidson  <beidson@apple.com>
 
         Consolidate most "frame load" arguments into FrameLoadRequest.
index 539603e..0102658 100644 (file)
@@ -501,6 +501,7 @@ public:
     void viewportMetaTagWidthDidChange(float width);
     void didFinishDrawingPagesToPDF(const IPC::DataReference&);
     void contentSizeCategoryDidChange(const String& contentSizeCategory);
+    void getLookupContextAtPoint(const WebCore::IntPoint&, std::function<void(const String&, CallbackBase::Error)>);
 #endif
 
     void didCommitLayerTree(const WebKit::RemoteLayerTreeTransaction&);
index 0aaa6d0..f958008 100644 (file)
@@ -182,11 +182,13 @@ const CGFloat minimumTapHighlightRadius = 2.0;
 - (void)showTextServiceFor:(NSString *)selectedTerm fromRect:(CGRect)presentationRect;
 - (void)scheduleChineseTransliterationForText:(NSString *)text;
 - (void)showShareSheetFor:(NSString *)selectedTerm fromRect:(CGRect)presentationRect;
+- (void)lookup:(NSString *)textWithContext fromRect:(CGRect)presentationRect;
 @end
 
 @interface UIWKSelectionAssistant (StagingToRemove)
 - (void)showTextServiceFor:(NSString *)selectedTerm fromRect:(CGRect)presentationRect;
 - (void)showShareSheetFor:(NSString *)selectedTerm fromRect:(CGRect)presentationRect;
+- (void)lookup:(NSString *)textWithContext fromRect:(CGRect)presentationRect;
 @end
 
 @interface UIKeyboardImpl (StagingToRemove)
@@ -1295,6 +1297,24 @@ static void cancelPotentialTapIfNecessary(WKContentView* contentView)
     return (_page->editorState().isContentRichlyEditable) ? richTypes : plainTextTypes;
 }
 
+- (void)_lookup:(CGPoint)point
+{
+    RetainPtr<WKContentView> view = self;
+    _page->getLookupContextAtPoint(WebCore::IntPoint(point), [view](const String& string, CallbackBase::Error error) {
+        if (error != CallbackBase::Error::None)
+            return;
+        if (!string)
+            return;
+        
+        CGRect presentationRect = view->_page->editorState().selectionIsRange ? view->_page->editorState().postLayoutData().selectionRects[0].rect() : view->_page->editorState().postLayoutData().caretRectAtStart;
+        
+        if (view->_textSelectionAssistant && [view->_textSelectionAssistant respondsToSelector:@selector(lookup:fromRect:)])
+            [view->_textSelectionAssistant lookup:string fromRect:presentationRect];
+        else if (view->_webSelectionAssistant && [view->_webSelectionAssistant respondsToSelector:@selector(lookup:fromRect:)])
+            [view->_webSelectionAssistant lookup:string fromRect:presentationRect];
+    });
+}
+
 - (void)_share:(id)sender
 {
     RetainPtr<WKContentView> view = self;
@@ -1443,6 +1463,16 @@ static void cancelPotentialTapIfNecessary(WKContentView* contentView)
         return YES;
     }
 
+    if (action == @selector(_lookup:)) {
+        if (_page->editorState().isInPasswordField)
+            return NO;
+        
+        if ([[getMCProfileConnectionClass() sharedConnection] effectiveBoolValueForSetting:MCFeatureDefinitionLookupAllowed] == MCRestrictedBoolExplicitNo)
+            return NO;
+        
+        return YES;
+    }
+
     if (action == @selector(_share:)) {
         if (_page->editorState().isInPasswordField || !(hasWebSelection || _page->editorState().selectionIsRange))
             return NO;
index 5cfe595..082127f 100644 (file)
@@ -513,6 +513,17 @@ void WebPageProxy::getAutocorrectionContext(String& beforeContext, String& marke
     m_process->sendSync(Messages::WebPage::GetAutocorrectionContext(), Messages::WebPage::GetAutocorrectionContext::Reply(beforeContext, markedText, selectedText, afterContext, location, length), m_pageID);
 }
 
+void WebPageProxy::getLookupContextAtPoint(const WebCore::IntPoint& point, std::function<void(const String&, CallbackBase::Error)> callbackFunction)
+{
+    if (!isValid()) {
+        callbackFunction(String(), CallbackBase::Error::Unknown);
+        return;
+    }
+    
+    uint64_t callbackID = m_callbacks.put(WTF::move(callbackFunction), m_process->throttler().backgroundActivityToken());
+    m_process->send(Messages::WebPage::GetLookupContextAtPoint(point, callbackID), m_pageID);
+}
+
 void WebPageProxy::selectWithTwoTouches(const WebCore::IntPoint from, const WebCore::IntPoint to, uint32_t gestureType, uint32_t gestureState, std::function<void (const WebCore::IntPoint&, uint32_t, uint32_t, uint32_t, CallbackBase::Error)> callbackFunction)
 {
     if (!isValid()) {
index 8ffc130..05a4c67 100644 (file)
@@ -526,6 +526,7 @@ public:
     void resetAssistedNodeForFrame(WebFrame*);
     WebCore::IntRect rectForElementAtInteractionLocation();
     void updateSelectionAppearance();
+    void getLookupContextAtPoint(const WebCore::IntPoint, uint64_t callbackID);
 
 #if ENABLE(IOS_TOUCH_EVENTS)
     void dispatchAsynchronousTouchEvents(const Vector<WebTouchEvent, 1>& queue);
index 852ead1..70e6c57 100644 (file)
@@ -91,6 +91,7 @@ messages -> WebPage LegacyReceiver {
     ApplicationDidBecomeActive()
     ContentSizeCategoryDidChange(String contentSizeCategory)
     ExecuteEditCommandWithCallback(String name, uint64_t callbackID)
+    GetLookupContextAtPoint(WebCore::IntPoint point, uint64_t callbackID)
 #endif
 
 #if ENABLE(REMOTE_INSPECTOR)
index f3cca5e..a886da1 100644 (file)
@@ -406,6 +406,20 @@ bool WebPage::performDefaultBehaviorForKeyEvent(const WebKeyboardEvent&)
     return false;
 }
 
+void WebPage::getLookupContextAtPoint(const WebCore::IntPoint point, uint64_t callbackID)
+{
+    Frame& frame = m_page->focusController().focusedOrMainFrame();
+    VisiblePosition position = frame.visiblePositionForPoint(point);
+    String resultString;
+    if (!position.isNull()) {
+        // As context, we are going to use 250 characters of text before and after the point.
+        RefPtr<Range> fullCharacterRange = rangeExpandedAroundPositionByCharacters(position, 250);
+        if (fullCharacterRange)
+            resultString = plainText(fullCharacterRange.get());
+    }
+    send(Messages::WebPageProxy::StringCallback(resultString, callbackID));
+}
+
 NSObject *WebPage::accessibilityObjectForMainFramePlugin()
 {
     if (!m_page)