Title: [199399] trunk/Source/WebKit2
Revision
199399
Author
[email protected]
Date
2016-04-12 18:07:26 -0700 (Tue, 12 Apr 2016)

Log Message

Should retrieve text surrounding the selection when performing lookup.
https://bugs.webkit.org/show_bug.cgi?id=156525
rdar://problem/25043678

Reviewed by Tim Horton.

The lookup functionality requires the surrounding text to improve
the quality of the results. This patch changes the implementation of
_lookup to retrieve the text before and the text after the selection.
It also renames DictationContextCallback to SelectionContextCallback so
that it can be used for both dictation and lookup, since they both need
the surrounding text.

* UIProcess/AutoCorrectionCallback.h:
* UIProcess/WebPageProxy.h:
* UIProcess/WebPageProxy.messages.in:
* UIProcess/ios/WKContentViewInteraction.mm:
(-[WKContentView _lookup:]):
* UIProcess/ios/WebPageProxyIOS.mm:
(WebKit::WebPageProxy::autocorrectionDataCallback):
(WebKit::WebPageProxy::selectionContextCallback):
(WebKit::WebPageProxy::getAutocorrectionContext):
(WebKit::WebPageProxy::getSelectionContext):
(WebKit::WebPageProxy::handleTwoFingerTapAtPoint):
(WebKit::WebPageProxy::dictationContextCallback): Deleted.
(WebKit::WebPageProxy::getLookupContextAtPoint): Deleted.
* WebProcess/WebPage/WebPage.h:
* WebProcess/WebPage/WebPage.messages.in:
* WebProcess/WebPage/ios/WebPageIOS.mm:
(WebKit::WebPage::performDefaultBehaviorForKeyEvent):
(WebKit::WebPage::getSelectionContext):
(WebKit::WebPage::accessibilityObjectForMainFramePlugin):
(WebKit::WebPage::requestDictationContext):
(WebKit::WebPage::replaceSelectedText):
(WebKit::WebPage::getLookupContextAtPoint): Deleted.

Modified Paths

Diff

Modified: trunk/Source/WebKit2/ChangeLog (199398 => 199399)


--- trunk/Source/WebKit2/ChangeLog	2016-04-13 00:48:50 UTC (rev 199398)
+++ trunk/Source/WebKit2/ChangeLog	2016-04-13 01:07:26 UTC (rev 199399)
@@ -1,3 +1,41 @@
+2016-04-12  Enrica Casucci  <[email protected]>
+
+        Should retrieve text surrounding the selection when performing lookup.
+        https://bugs.webkit.org/show_bug.cgi?id=156525
+        rdar://problem/25043678
+
+        Reviewed by Tim Horton.
+
+        The lookup functionality requires the surrounding text to improve
+        the quality of the results. This patch changes the implementation of
+        _lookup to retrieve the text before and the text after the selection.
+        It also renames DictationContextCallback to SelectionContextCallback so
+        that it can be used for both dictation and lookup, since they both need
+        the surrounding text.
+
+        * UIProcess/AutoCorrectionCallback.h:
+        * UIProcess/WebPageProxy.h:
+        * UIProcess/WebPageProxy.messages.in:
+        * UIProcess/ios/WKContentViewInteraction.mm:
+        (-[WKContentView _lookup:]):
+        * UIProcess/ios/WebPageProxyIOS.mm:
+        (WebKit::WebPageProxy::autocorrectionDataCallback):
+        (WebKit::WebPageProxy::selectionContextCallback):
+        (WebKit::WebPageProxy::getAutocorrectionContext):
+        (WebKit::WebPageProxy::getSelectionContext):
+        (WebKit::WebPageProxy::handleTwoFingerTapAtPoint):
+        (WebKit::WebPageProxy::dictationContextCallback): Deleted.
+        (WebKit::WebPageProxy::getLookupContextAtPoint): Deleted.
+        * WebProcess/WebPage/WebPage.h:
+        * WebProcess/WebPage/WebPage.messages.in:
+        * WebProcess/WebPage/ios/WebPageIOS.mm:
+        (WebKit::WebPage::performDefaultBehaviorForKeyEvent):
+        (WebKit::WebPage::getSelectionContext):
+        (WebKit::WebPage::accessibilityObjectForMainFramePlugin):
+        (WebKit::WebPage::requestDictationContext):
+        (WebKit::WebPage::replaceSelectedText):
+        (WebKit::WebPage::getLookupContextAtPoint): Deleted.
+
 2016-04-12  Joseph Pecoraro  <[email protected]>
 
         Web Inspector: Dock controls are not available in toolbar if Web Inspector window leaves fullscreen

Modified: trunk/Source/WebKit2/UIProcess/AutoCorrectionCallback.h (199398 => 199399)


--- trunk/Source/WebKit2/UIProcess/AutoCorrectionCallback.h	2016-04-13 00:48:50 UTC (rev 199398)
+++ trunk/Source/WebKit2/UIProcess/AutoCorrectionCallback.h	2016-04-13 01:07:26 UTC (rev 199399)
@@ -36,7 +36,7 @@
 
 typedef GenericCallback<const Vector<WebCore::FloatRect>&, const String&, double, uint64_t> AutocorrectionDataCallback;
 typedef GenericCallback<const String&, const String&, const String&, const String&, uint64_t, uint64_t> AutocorrectionContextCallback;
-typedef GenericCallback<const String&, const String&, const String&> DictationContextCallback;
+typedef GenericCallback<const String&, const String&, const String&> SelectionContextCallback;
 
 } // namespace WebKit
 

Modified: trunk/Source/WebKit2/UIProcess/WebPageProxy.h (199398 => 199399)


--- trunk/Source/WebKit2/UIProcess/WebPageProxy.h	2016-04-13 00:48:50 UTC (rev 199398)
+++ trunk/Source/WebKit2/UIProcess/WebPageProxy.h	2016-04-13 01:07:26 UTC (rev 199399)
@@ -515,7 +515,7 @@
     void disableDoubleTapGesturesDuringTapIfNecessary(uint64_t requestID);
     void didFinishDrawingPagesToPDF(const IPC::DataReference&);
     void contentSizeCategoryDidChange(const String& contentSizeCategory);
-    void getLookupContextAtPoint(const WebCore::IntPoint&, std::function<void(const String&, CallbackBase::Error)>);
+    void getSelectionContext(std::function<void(const String&, const String&, const String&, CallbackBase::Error)>);
     void handleTwoFingerTapAtPoint(const WebCore::IntPoint&, std::function<void(const String&, CallbackBase::Error)>);
     void updateForceAlwaysUserScalable();
 #endif
@@ -1365,7 +1365,7 @@
     void touchesCallback(const WebCore::IntPoint&, uint32_t, uint64_t);
     void autocorrectionDataCallback(const Vector<WebCore::FloatRect>&, const String&, float, uint64_t, uint64_t);
     void autocorrectionContextCallback(const String&, const String&, const String&, const String&, uint64_t, uint64_t, uint64_t);
-    void dictationContextCallback(const String&, const String&, const String&, uint64_t);
+    void selectionContextCallback(const String&, const String&, const String&, uint64_t);
     void interpretKeyEvent(const EditorState&, bool isCharEvent, bool& handled);
     void showPlaybackTargetPicker(bool hasVideo, const WebCore::IntRect& elementRect);
 #endif

Modified: trunk/Source/WebKit2/UIProcess/WebPageProxy.messages.in (199398 => 199399)


--- trunk/Source/WebKit2/UIProcess/WebPageProxy.messages.in	2016-04-13 00:48:50 UTC (rev 199398)
+++ trunk/Source/WebKit2/UIProcess/WebPageProxy.messages.in	2016-04-13 01:07:26 UTC (rev 199399)
@@ -174,7 +174,7 @@
     TouchesCallback(WebCore::IntPoint point, uint32_t touches, uint64_t callbackID)
     AutocorrectionDataCallback(Vector<WebCore::FloatRect> textRects, String fontName, double fontSize, uint64_t traits, uint64_t callbackID)
     AutocorrectionContextCallback(String beforeText, String markedText, String selectedText, String afterText, uint64_t location, uint64_t length, uint64_t callbackID)
-    DictationContextCallback(String selectedText, String beforeText, String afterText, uint64_t callbackID)
+    SelectionContextCallback(String selectedText, String beforeText, String afterText, uint64_t callbackID)
     InterpretKeyEvent(struct WebKit::EditorState state, bool isCharEvent) -> (bool handled)
     DidReceivePositionInformation(struct WebKit::InteractionInformationAtPosition information)
     SaveImageToLibrary(WebKit::SharedMemory::Handle handle, uint64_t size)

Modified: trunk/Source/WebKit2/UIProcess/ios/WKContentViewInteraction.mm (199398 => 199399)


--- trunk/Source/WebKit2/UIProcess/ios/WKContentViewInteraction.mm	2016-04-13 00:48:50 UTC (rev 199398)
+++ trunk/Source/WebKit2/UIProcess/ios/WKContentViewInteraction.mm	2016-04-13 01:07:26 UTC (rev 199399)
@@ -216,11 +216,13 @@
 - (void)scheduleChineseTransliterationForText:(NSString *)text;
 - (void)showShareSheetFor:(NSString *)selectedTerm fromRect:(CGRect)presentationRect;
 - (void)lookup:(NSString *)textWithContext fromRect:(CGRect)presentationRect;
+- (void)lookup:(NSString *)textWithContext withRange:(NSRange)range fromRect:(CGRect)presentationRect;
 @end
 
 @interface UIWKSelectionAssistant (StagingToRemove)
 - (void)showTextServiceFor:(NSString *)selectedTerm fromRect:(CGRect)presentationRect;
 - (void)lookup:(NSString *)textWithContext fromRect:(CGRect)presentationRect;
+- (void)lookup:(NSString *)textWithContext withRange:(NSRange)range fromRect:(CGRect)presentationRect;
 @end
 
 @interface UIKeyboardImpl (StagingToRemove)
@@ -1615,18 +1617,26 @@
 - (void)_lookup:(id)sender
 {
     RetainPtr<WKContentView> view = self;
-    _page->getSelectionOrContentsAsString([view](const String& string, CallbackBase::Error error) {
+    _page->getSelectionContext([view](const String& selectedText, const String& textBefore, const String& textAfter, CallbackBase::Error error) {
         if (error != CallbackBase::Error::None)
             return;
-        if (!string)
+        if (!selectedText)
             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];
+        String selectionContext = textBefore + selectedText + textAfter;
+        if (view->_textSelectionAssistant) {
+            if ([view->_textSelectionAssistant respondsToSelector:@selector(lookup:withRange:fromRect:)])
+                [view->_textSelectionAssistant lookup:selectionContext withRange:NSMakeRange(textBefore.length(), selectedText.length()) fromRect:presentationRect];
+            else
+                [view->_textSelectionAssistant lookup:selectedText fromRect:presentationRect];
+        } else {
+            if ([view->_webSelectionAssistant respondsToSelector:@selector(lookup:withRange:fromRect:)])
+                [view->_webSelectionAssistant lookup:selectionContext withRange:NSMakeRange(textBefore.length(), selectedText.length()) fromRect:presentationRect];
+            else
+                [view->_webSelectionAssistant lookup:selectedText fromRect:presentationRect];
+        }
     });
 }
 

Modified: trunk/Source/WebKit2/UIProcess/ios/WebPageProxyIOS.mm (199398 => 199399)


--- trunk/Source/WebKit2/UIProcess/ios/WebPageProxyIOS.mm	2016-04-13 00:48:50 UTC (rev 199398)
+++ trunk/Source/WebKit2/UIProcess/ios/WebPageProxyIOS.mm	2016-04-13 01:07:26 UTC (rev 199399)
@@ -160,9 +160,9 @@
     callback->performCallbackWithReturnValue(rects, fontName, fontSize, fontTraits);
 }
 
-void WebPageProxy::dictationContextCallback(const String& selectedText, const String& beforeText, const String& afterText, uint64_t callbackID)
+void WebPageProxy::selectionContextCallback(const String& selectedText, const String& beforeText, const String& afterText, uint64_t callbackID)
 {
-    auto callback = m_callbacks.take<DictationContextCallback>(callbackID);
+    auto callback = m_callbacks.take<SelectionContextCallback>(callbackID);
     if (!callback) {
         ASSERT_NOT_REACHED();
         return;
@@ -553,15 +553,15 @@
     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)
+void WebPageProxy::getSelectionContext(std::function<void(const String&, const String&, const String&, CallbackBase::Error)> callbackFunction)
 {
     if (!isValid()) {
-        callbackFunction(String(), CallbackBase::Error::Unknown);
+        callbackFunction(String(), String(), String(), CallbackBase::Error::Unknown);
         return;
     }
     
     uint64_t callbackID = m_callbacks.put(WTFMove(callbackFunction), m_process->throttler().backgroundActivityToken());
-    m_process->send(Messages::WebPage::GetLookupContextAtPoint(point, callbackID), m_pageID);
+    m_process->send(Messages::WebPage::GetSelectionContext(callbackID), m_pageID);
 }
 
 void WebPageProxy::handleTwoFingerTapAtPoint(const WebCore::IntPoint& point, std::function<void(const String&, CallbackBase::Error)> callbackFunction)

Modified: trunk/Source/WebKit2/WebProcess/WebPage/WebPage.h (199398 => 199399)


--- trunk/Source/WebKit2/WebProcess/WebPage/WebPage.h	2016-04-13 00:48:50 UTC (rev 199398)
+++ trunk/Source/WebKit2/WebProcess/WebPage/WebPage.h	2016-04-13 01:07:26 UTC (rev 199399)
@@ -556,7 +556,7 @@
     void resetAssistedNodeForFrame(WebFrame*);
     WebCore::IntRect rectForElementAtInteractionLocation();
     void updateSelectionAppearance();
-    void getLookupContextAtPoint(const WebCore::IntPoint, uint64_t callbackID);
+    void getSelectionContext(uint64_t callbackID);
     void handleTwoFingerTapAtPoint(const WebCore::IntPoint&, uint64_t callbackID);
 #if ENABLE(IOS_TOUCH_EVENTS)
     void dispatchAsynchronousTouchEvents(const Vector<WebTouchEvent, 1>& queue);

Modified: trunk/Source/WebKit2/WebProcess/WebPage/WebPage.messages.in (199398 => 199399)


--- trunk/Source/WebKit2/WebProcess/WebPage/WebPage.messages.in	2016-04-13 00:48:50 UTC (rev 199398)
+++ trunk/Source/WebKit2/WebProcess/WebPage/WebPage.messages.in	2016-04-13 01:07:26 UTC (rev 199399)
@@ -90,7 +90,7 @@
     ApplicationDidBecomeActive()
     ContentSizeCategoryDidChange(String contentSizeCategory)
     ExecuteEditCommandWithCallback(String name, uint64_t callbackID)
-    GetLookupContextAtPoint(WebCore::IntPoint point, uint64_t callbackID)
+    GetSelectionContext(uint64_t callbackID)
     SetAllowsMediaDocumentInlinePlayback(bool allows)
     HandleTwoFingerTapAtPoint(WebCore::IntPoint point, uint64_t callbackID)
     UpdateForceAlwaysUserScalable()

Modified: trunk/Source/WebKit2/WebProcess/WebPage/ios/WebPageIOS.mm (199398 => 199399)


--- trunk/Source/WebKit2/WebProcess/WebPage/ios/WebPageIOS.mm	2016-04-13 00:48:50 UTC (rev 199398)
+++ trunk/Source/WebKit2/WebProcess/WebPage/ios/WebPageIOS.mm	2016-04-13 01:07:26 UTC (rev 199399)
@@ -395,18 +395,20 @@
     return false;
 }
 
-void WebPage::getLookupContextAtPoint(const WebCore::IntPoint point, uint64_t callbackID)
+void WebPage::getSelectionContext(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());
+    if (!frame.selection().isRange()) {
+        send(Messages::WebPageProxy::SelectionContextCallback(String(), String(), String(), callbackID));
+        return;
     }
-    send(Messages::WebPageProxy::StringCallback(resultString, callbackID));
+    const int selectionExtendedContextLength = 350;
+    
+    String selectedText = plainTextReplacingNoBreakSpace(frame.selection().selection().toNormalizedRange().get());
+    String textBefore = plainTextReplacingNoBreakSpace(rangeExpandedByCharactersInDirectionAtWordBoundary(frame.selection().selection().start(), selectionExtendedContextLength, DirectionBackward).get(), TextIteratorDefaultBehavior, true);
+    String textAfter = plainTextReplacingNoBreakSpace(rangeExpandedByCharactersInDirectionAtWordBoundary(frame.selection().selection().end(), selectionExtendedContextLength, DirectionForward).get(), TextIteratorDefaultBehavior, true);
+
+    send(Messages::WebPageProxy::SelectionContextCallback(selectedText, textBefore, textAfter, callbackID));
 }
 
 NSObject *WebPage::accessibilityObjectForMainFramePlugin()
@@ -1911,7 +1913,7 @@
             contextAfter = plainTextReplacingNoBreakSpace(Range::create(*frame.document(), endPosition, lastPosition).ptr());
     }
 
-    send(Messages::WebPageProxy::DictationContextCallback(selectedText, contextBefore, contextAfter, callbackID));
+    send(Messages::WebPageProxy::SelectionContextCallback(selectedText, contextBefore, contextAfter, callbackID));
 }
 
 void WebPage::replaceSelectedText(const String& oldText, const String& newText)
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to