Title: [161358] trunk/Source/WebKit2
Revision
161358
Author
[email protected]
Date
2014-01-06 11:44:32 -0800 (Mon, 06 Jan 2014)

Log Message

Add support to retrieve the autocorrection context.
https://bugs.webkit.org/show_bug.cgi?id=126479

Reviewed by Sam Weinig.
        
We are adding the support to retrieve the input context for autocorrection
and input methods on iOS. The implementation is provided both via synchronous
and asynchronous calls to the WebProcess.

* Platform/IPC/HandleMessage.h: Added template with zero input parameters and six reply parameters.
* UIProcess/API/ios/WKInteractionView.mm:
(autocorrectionData):
(-[WKInteractionView requestAutocorrectionRectsForString:withCompletionHandler:]):
(autocorrectionResult):
(-[WKInteractionView applyAutocorrection:toString:withCompletionHandler:]):
(autocorrectionContext):
(-[WKInteractionView requestAutocorrectionContextWithCompletionHandler:]):
(-[WKInteractionView hasMarkedText]):
(-[WKInteractionView _startAssistingNode]):
(+[WKAutocorrectionContext autocorrectionContextWithData:markedText:selectedText:afterText:selectedRangeInMarkedText:]):
(-[WKAutocorrectionContext dealloc]):
* UIProcess/AutoCorrectionCallback.h:
(WebKit::AutocorrectionContextCallback::create):
(WebKit::AutocorrectionContextCallback::~AutocorrectionContextCallback):
(WebKit::AutocorrectionContextCallback::performCallbackWithReturnValue):
(WebKit::AutocorrectionContextCallback::invalidate):
(WebKit::AutocorrectionContextCallback::AutocorrectionContextCallback):
* UIProcess/WebPageProxy.cpp:
(WebKit::WebPageProxy::resetState):
* UIProcess/WebPageProxy.h:
* UIProcess/WebPageProxy.messages.in:
* UIProcess/ios/WebPageProxyIOS.mm:
(WebKit::WebPageProxy::autocorrectionContextCallback):
(WebKit::WebPageProxy::requestAutocorrectionContext):
(WebKit::WebPageProxy::getAutocorrectionContext):
* WebProcess/WebPage/WebPage.h:
* WebProcess/WebPage/WebPage.messages.in:
* WebProcess/WebPage/ios/WebPageIOS.mm:
(WebKit::computeAutocorrectionContext):
(WebKit::WebPage::requestAutocorrectionContext):
(WebKit::WebPage::getAutocorrectionContext):

Modified Paths

Diff

Modified: trunk/Source/WebKit2/ChangeLog (161357 => 161358)


--- trunk/Source/WebKit2/ChangeLog	2014-01-06 19:29:11 UTC (rev 161357)
+++ trunk/Source/WebKit2/ChangeLog	2014-01-06 19:44:32 UTC (rev 161358)
@@ -1,3 +1,47 @@
+2014-01-06  Enrica Casucci  <[email protected]>
+
+        Add support to retrieve the autocorrection context.
+        https://bugs.webkit.org/show_bug.cgi?id=126479
+
+        Reviewed by Sam Weinig.
+        
+        We are adding the support to retrieve the input context for autocorrection
+        and input methods on iOS. The implementation is provided both via synchronous
+        and asynchronous calls to the WebProcess.
+
+        * Platform/IPC/HandleMessage.h: Added template with zero input parameters and six reply parameters.
+        * UIProcess/API/ios/WKInteractionView.mm:
+        (autocorrectionData):
+        (-[WKInteractionView requestAutocorrectionRectsForString:withCompletionHandler:]):
+        (autocorrectionResult):
+        (-[WKInteractionView applyAutocorrection:toString:withCompletionHandler:]):
+        (autocorrectionContext):
+        (-[WKInteractionView requestAutocorrectionContextWithCompletionHandler:]):
+        (-[WKInteractionView hasMarkedText]):
+        (-[WKInteractionView _startAssistingNode]):
+        (+[WKAutocorrectionContext autocorrectionContextWithData:markedText:selectedText:afterText:selectedRangeInMarkedText:]):
+        (-[WKAutocorrectionContext dealloc]):
+        * UIProcess/AutoCorrectionCallback.h:
+        (WebKit::AutocorrectionContextCallback::create):
+        (WebKit::AutocorrectionContextCallback::~AutocorrectionContextCallback):
+        (WebKit::AutocorrectionContextCallback::performCallbackWithReturnValue):
+        (WebKit::AutocorrectionContextCallback::invalidate):
+        (WebKit::AutocorrectionContextCallback::AutocorrectionContextCallback):
+        * UIProcess/WebPageProxy.cpp:
+        (WebKit::WebPageProxy::resetState):
+        * UIProcess/WebPageProxy.h:
+        * UIProcess/WebPageProxy.messages.in:
+        * UIProcess/ios/WebPageProxyIOS.mm:
+        (WebKit::WebPageProxy::autocorrectionContextCallback):
+        (WebKit::WebPageProxy::requestAutocorrectionContext):
+        (WebKit::WebPageProxy::getAutocorrectionContext):
+        * WebProcess/WebPage/WebPage.h:
+        * WebProcess/WebPage/WebPage.messages.in:
+        * WebProcess/WebPage/ios/WebPageIOS.mm:
+        (WebKit::computeAutocorrectionContext):
+        (WebKit::WebPage::requestAutocorrectionContext):
+        (WebKit::WebPage::getAutocorrectionContext):
+
 2014-01-06  Gavin Barraclough  <[email protected]>
 
         Move ViewState to WebCore

Modified: trunk/Source/WebKit2/Platform/IPC/HandleMessage.h (161357 => 161358)


--- trunk/Source/WebKit2/Platform/IPC/HandleMessage.h	2014-01-06 19:29:11 UTC (rev 161357)
+++ trunk/Source/WebKit2/Platform/IPC/HandleMessage.h	2014-01-06 19:44:32 UTC (rev 161358)
@@ -88,6 +88,12 @@
     (object->*function)(std::get<0>(replyArgs), std::get<1>(replyArgs));
 }
 
+template<typename C, typename MF, typename R1, typename R2, typename R3, typename R4, typename R5, typename R6>
+void callMemberFunction(std::tuple<>&&, std::tuple<R1, R2, R3, R4, R5, R6>& replyArgs, C* object, MF function)
+{
+    (object->*function)(std::get<0>(replyArgs), std::get<1>(replyArgs), std::get<2>(replyArgs), std::get<3>(replyArgs), std::get<4>(replyArgs), std::get<5>(replyArgs));
+}
+
 template<typename C, typename MF, typename P1>
 void callMemberFunction(std::tuple<P1>&& args, std::tuple<>&, C* object, MF function)
 {

Modified: trunk/Source/WebKit2/UIProcess/API/ios/WKInteractionView.mm (161357 => 161358)


--- trunk/Source/WebKit2/UIProcess/API/ios/WKInteractionView.mm	2014-01-06 19:29:11 UTC (rev 161357)
+++ trunk/Source/WebKit2/UIProcess/API/ios/WKInteractionView.mm	2014-01-06 19:44:32 UTC (rev 161358)
@@ -100,6 +100,17 @@
 
 @end
 
+@interface WKAutocorrectionContext : UIWKAutocorrectionContext {
+    NSString *_contextBeforeSelection;
+    NSString *_selectedText;
+    NSString *_markedText;
+    NSString *_contextAfterSelection;
+    NSRange _rangeInMarkedText;
+}
+
++ (WKAutocorrectionContext *)autocorrectionContextWithData:(NSString *)beforeText markedText:(NSString *)markedText selectedText:(NSString *)selectedText afterText:(NSString *)afterText selectedRangeInMarkedText:(NSRange)range;
+@end
+
 @interface UITextInteractionAssistant (UITextInteractionAssistant_Internal)
 // FIXME: this needs to be moved from the internal header to the private.
 - (id)initWithView:(UIResponder <UITextInput> *)view;
@@ -107,6 +118,7 @@
 @end
 
 typedef void (^UIWKAutocorrectionCompletionHandler)(UIWKAutocorrectionRects *rectsForInput);
+typedef void (^UIWKAutocorrectionContextHandler)(UIWKAutocorrectionContext *autocorrectionContext);
 
 struct WKAutoCorrectionData{
     String fontName;
@@ -114,7 +126,8 @@
     uint64_t fontTraits;
     CGRect textFirstRect;
     CGRect textLastRect;
-    UIWKAutocorrectionCompletionHandler completionHandler;
+    UIWKAutocorrectionCompletionHandler autocorrectionHandler;
+    UIWKAutocorrectionContextHandler autocorrectionContextHandler;
 };
 
 @interface WKInteractionView (Private)
@@ -808,9 +821,9 @@
     autocorrectionData->textFirstRect = firstRect;
     autocorrectionData->textLastRect = lastRect;
 
-    autocorrectionData->completionHandler(rects.size() ? [WKAutocorrectionRects autocorrectionRectsWithRects:firstRect lastRect:lastRect] : nil);
-    [autocorrectionData->completionHandler release];
-    autocorrectionData->completionHandler = nil;
+    autocorrectionData->autocorrectionHandler(rects.size() ? [WKAutocorrectionRects autocorrectionRectsWithRects:firstRect lastRect:lastRect] : nil);
+    [autocorrectionData->autocorrectionHandler release];
+    autocorrectionData->autocorrectionHandler = nil;
 }
 
 // The completion handler can pass nil if input does not match the actual text preceding the insertion point.
@@ -820,7 +833,7 @@
         completionHandler(nil);
         return;
     }
-    _autocorrectionData.completionHandler = [completionHandler copy];
+    _autocorrectionData.autocorrectionHandler = [completionHandler copy];
     _page->requestAutocorrectionData(input, AutocorrectionDataCallback::create(self, autocorrectionData));
 }
 
@@ -840,22 +853,44 @@
     ASSERT(view);
     WKAutoCorrectionData *autocorrectionData = view.autocorrectionData;
 
-    autocorrectionData->completionHandler(correction ? [WKAutocorrectionRects autocorrectionRectsWithRects:autocorrectionData->textFirstRect lastRect:autocorrectionData->textLastRect] : nil);
-    [autocorrectionData->completionHandler release];
-    autocorrectionData->completionHandler = nil;
+    autocorrectionData->autocorrectionHandler(correction ? [WKAutocorrectionRects autocorrectionRectsWithRects:autocorrectionData->textFirstRect lastRect:autocorrectionData->textLastRect] : nil);
+    [autocorrectionData->autocorrectionHandler release];
+    autocorrectionData->autocorrectionHandler = nil;
 }
 
 // The completion handler should pass the rect of the correction text after replacing the input text, or nil if the replacement could not be performed.
 - (void)applyAutocorrection:(NSString *)correction toString:(NSString *)input withCompletionHandler:(void (^)(UIWKAutocorrectionRects *rectsForCorrection))completionHandler
 {
-    _autocorrectionData.completionHandler = [completionHandler copy];
+    _autocorrectionData.autocorrectionHandler = [completionHandler copy];
     _page->applyAutocorrection(correction, input, StringCallback::create(self, autocorrectionResult));
 }
 
+static void autocorrectionContext(const String& beforeText, const String& markedText, const String& selectedText, const String& afterText, uint64_t location, uint64_t length, WKErrorRef error, void* context)
+{
+    WKInteractionView* view = static_cast<WKInteractionView*>(context);
+    ASSERT(view);
+    WKAutoCorrectionData *autocorrectionData = view.autocorrectionData;
+    autocorrectionData->autocorrectionContextHandler([WKAutocorrectionContext autocorrectionContextWithData:beforeText markedText:markedText selectedText:selectedText afterText:afterText selectedRangeInMarkedText:NSMakeRange(location, length)]);
+}
+
 - (void)requestAutocorrectionContextWithCompletionHandler:(void (^)(UIWKAutocorrectionContext *autocorrectionContext))completionHandler
 {
-    // FIXME: Need to retrieve the information from the WebProcess.
-    completionHandler(nil);
+    // FIXME: Remove the synchronous call as soon as Keyboard removes locking of the main thread.
+    const bool useSyncRequest = true;
+
+    if (useSyncRequest) {
+        String beforeText;
+        String markedText;
+        String selectedText;
+        String afterText;
+        uint64_t location;
+        uint64_t length;
+        _page->getAutocorrectionContext(beforeText, markedText, selectedText, afterText, location, length);
+        completionHandler([WKAutocorrectionContext autocorrectionContextWithData:beforeText markedText:markedText selectedText:selectedText afterText:afterText selectedRangeInMarkedText:NSMakeRange(location, length)]);
+    } else {
+        _autocorrectionData.autocorrectionContextHandler = [completionHandler copy];
+        _page->requestAutocorrectionContext(AutocorrectionContextCallback::create(self, autocorrectionContext));
+    }
 }
 
 // UIWebFormAccessoryDelegate
@@ -944,7 +979,7 @@
 
 - (BOOL)hasMarkedText
 {
-    return _page->editorState().hasComposition || [_markedText length];
+    return [_markedText length];
 }
 
 - (NSString *)markedText
@@ -1386,7 +1421,6 @@
 
     [self _startAssistingKeyboard];
     [self _updateAccessory];
-    [self reloadInputViews];
 }
 
 - (void)_stopAssistingNode
@@ -1601,3 +1635,33 @@
 }
 
 @end
+
+@implementation WKAutocorrectionContext
+
++ (WKAutocorrectionContext *)autocorrectionContextWithData:(NSString *)beforeText markedText:(NSString *)markedText selectedText:(NSString *)selectedText afterText:(NSString *)afterText selectedRangeInMarkedText:(NSRange)range
+{
+    WKAutocorrectionContext *context = [[WKAutocorrectionContext alloc] init];
+
+    if ([beforeText length])
+        context.contextBeforeSelection = [beforeText copy];
+    if ([selectedText length])
+        context.selectedText = [selectedText copy];
+    if ([markedText length])
+        context.markedText = [markedText copy];
+    if ([afterText length])
+        context.contextAfterSelection = [afterText copy];
+    context.rangeInMarkedText = range;
+    return [context autorelease];
+}
+
+- (void)dealloc
+{
+    [self.contextBeforeSelection release];
+    [self.markedText release];
+    [self.selectedText release];
+    [self.contextAfterSelection release];
+
+    [super dealloc];
+}
+
+@end

Modified: trunk/Source/WebKit2/UIProcess/AutoCorrectionCallback.h (161357 => 161358)


--- trunk/Source/WebKit2/UIProcess/AutoCorrectionCallback.h	2014-01-06 19:29:11 UTC (rev 161357)
+++ trunk/Source/WebKit2/UIProcess/AutoCorrectionCallback.h	2014-01-06 19:44:32 UTC (rev 161358)
@@ -69,7 +69,6 @@
     }
 
 private:
-
     AutocorrectionDataCallback(void* context, CallbackFunction callback)
         : CallbackBase(context)
         , m_callback(callback)
@@ -80,6 +79,50 @@
     CallbackFunction m_callback;
 };
 
+class AutocorrectionContextCallback : public CallbackBase {
+public:
+    typedef void (*CallbackFunction)(const String&, const String&, const String&, const String&, uint64_t, uint64_t, WKErrorRef, void*);
+
+    static PassRefPtr<AutocorrectionContextCallback> create(void* context, CallbackFunction callback)
+    {
+        return adoptRef(new AutocorrectionContextCallback(context, callback));
+    }
+
+    virtual ~AutocorrectionContextCallback()
+    {
+        ASSERT(!m_callback);
+    }
+
+    void performCallbackWithReturnValue(const String& returnValue1, const String& returnValue2, const String& returnValue3, const String& returnValue4, uint64_t returnValue5, uint64_t returnValue6)
+    {
+        ASSERT(m_callback);
+
+        m_callback(returnValue1, returnValue2, returnValue3, returnValue4, returnValue5, returnValue6, 0, context());
+
+        m_callback = 0;
+    }
+
+    void invalidate()
+    {
+        ASSERT(m_callback);
+
+        RefPtr<API::Error> error = API::Error::create();
+        m_callback(String(), String(), String(), String(), 0, 0, toAPI(error.get()), context());
+
+        m_callback = 0;
+    }
+
+private:
+    AutocorrectionContextCallback(void* context, CallbackFunction callback)
+        : CallbackBase(context)
+        , m_callback(callback)
+    {
+        ASSERT(m_callback);
+    }
+    
+    CallbackFunction m_callback;
+};
+
 } // namespace WebKit
 
 #endif // AutoCorrectionCallback_h

Modified: trunk/Source/WebKit2/UIProcess/WebPageProxy.cpp (161357 => 161358)


--- trunk/Source/WebKit2/UIProcess/WebPageProxy.cpp	2014-01-06 19:29:11 UTC (rev 161357)
+++ trunk/Source/WebKit2/UIProcess/WebPageProxy.cpp	2014-01-06 19:44:32 UTC (rev 161358)
@@ -3801,6 +3801,7 @@
     invalidateCallbackMap(m_gestureCallbacks);
     invalidateCallbackMap(m_touchesCallbacks);
     invalidateCallbackMap(m_autocorrectionCallbacks);
+    invalidateCallbackMap(m_autocorrectionContextCallbacks);
 #endif
 #if PLATFORM(GTK)
     invalidateCallbackMap(m_printFinishedCallbacks);

Modified: trunk/Source/WebKit2/UIProcess/WebPageProxy.h (161357 => 161358)


--- trunk/Source/WebKit2/UIProcess/WebPageProxy.h	2014-01-06 19:29:11 UTC (rev 161357)
+++ trunk/Source/WebKit2/UIProcess/WebPageProxy.h	2014-01-06 19:44:32 UTC (rev 161358)
@@ -437,6 +437,8 @@
     void extendSelection(WebCore::TextGranularity);
     void requestAutocorrectionData(const String& textForAutocorrection, PassRefPtr<AutocorrectionDataCallback>);
     void applyAutocorrection(const String& correction, const String& originalText, PassRefPtr<StringCallback>);
+    void requestAutocorrectionContext(PassRefPtr<AutocorrectionContextCallback>);
+    void getAutocorrectionContext(String& contextBefore, String& markedText, String& selectedText, String& contextAfter, uint64_t& location, uint64_t& length);
 #endif
 
     const EditorState& editorState() const { return m_editorState; }
@@ -1082,6 +1084,7 @@
     void gestureCallback(const WebCore::IntPoint&, uint32_t, uint32_t, uint32_t, uint64_t);
     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 interpretKeyEvent(const EditorState&, bool isCharEvent, bool& handled);
 #endif
 #if PLATFORM(GTK)
@@ -1201,6 +1204,7 @@
     HashMap<uint64_t, RefPtr<GestureCallback>> m_gestureCallbacks;
     HashMap<uint64_t, RefPtr<TouchesCallback>> m_touchesCallbacks;
     HashMap<uint64_t, RefPtr<AutocorrectionDataCallback>> m_autocorrectionCallbacks;
+    HashMap<uint64_t, RefPtr<AutocorrectionContextCallback>> m_autocorrectionContextCallbacks;
 #endif
 #if PLATFORM(GTK)
     HashMap<uint64_t, RefPtr<PrintFinishedCallback>> m_printFinishedCallbacks;

Modified: trunk/Source/WebKit2/UIProcess/WebPageProxy.messages.in (161357 => 161358)


--- trunk/Source/WebKit2/UIProcess/WebPageProxy.messages.in	2014-01-06 19:29:11 UTC (rev 161357)
+++ trunk/Source/WebKit2/UIProcess/WebPageProxy.messages.in	2014-01-06 19:44:32 UTC (rev 161358)
@@ -150,7 +150,8 @@
 #if PLATFORM(IOS)
     GestureCallback(WebCore::IntPoint point, uint32_t gestureType, uint32_t gestureState, uint32_t flags, uint64_t callbackID)
     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);
+    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)
     InterpretKeyEvent(WebKit::EditorState state, bool isCharEvent) -> (bool handled)
 #endif
 #if PLATFORM(GTK)

Modified: trunk/Source/WebKit2/UIProcess/ios/WebPageProxyIOS.mm (161357 => 161358)


--- trunk/Source/WebKit2/UIProcess/ios/WebPageProxyIOS.mm	2014-01-06 19:29:11 UTC (rev 161357)
+++ trunk/Source/WebKit2/UIProcess/ios/WebPageProxyIOS.mm	2014-01-06 19:44:32 UTC (rev 161358)
@@ -209,6 +209,17 @@
     callback->performCallbackWithReturnValue(rects, fontName, fontSize, fontTraits);
 }
 
+void WebPageProxy::autocorrectionContextCallback(const String& beforeText, const String& markedText, const String& selectedText, const String& afterText, uint64_t location, uint64_t length, uint64_t callbackID)
+{
+    RefPtr<AutocorrectionContextCallback> callback = m_autocorrectionContextCallbacks.take(callbackID);
+    if (!callback) {
+        ASSERT_NOT_REACHED();
+        return;
+    }
+
+    callback->performCallbackWithReturnValue(beforeText, markedText, selectedText, afterText, location, length);
+}
+
 void WebPageProxy::selectWithGesture(const WebCore::IntPoint point, WebCore::TextGranularity granularity, uint32_t gestureType, uint32_t gestureState, PassRefPtr<GestureCallback> callback)
 {
     if (!isValid()) {
@@ -257,6 +268,23 @@
     m_process->send(Messages::WebPage::ApplyAutocorrection(correction, originalText, callbackID), m_pageID);
 }
 
+void WebPageProxy::requestAutocorrectionContext(PassRefPtr<AutocorrectionContextCallback> callback)
+{
+    if (!isValid()) {
+        callback->invalidate();
+        return;
+    }
+
+    uint64_t callbackID = callback->callbackID();
+    m_autocorrectionContextCallbacks.set(callbackID, callback);
+    m_process->send(Messages::WebPage::RequestAutocorrectionContext(callbackID), m_pageID);
+}
+
+void WebPageProxy::getAutocorrectionContext(String& beforeContext, String& markedText, String& selectedText, String& afterContext, uint64_t& location, uint64_t& length)
+{
+    m_process->sendSync(Messages::WebPage::GetAutocorrectionContext(), Messages::WebPage::GetAutocorrectionContext::Reply(beforeContext, markedText, selectedText, afterContext, location, length), m_pageID);
+}
+
 void WebPageProxy::selectWithTwoTouches(const WebCore::IntPoint from, const WebCore::IntPoint to, uint32_t gestureType, uint32_t gestureState, PassRefPtr<GestureCallback> callback)
 {
     if (!isValid()) {

Modified: trunk/Source/WebKit2/WebProcess/WebPage/WebPage.h (161357 => 161358)


--- trunk/Source/WebKit2/WebProcess/WebPage/WebPage.h	2014-01-06 19:29:11 UTC (rev 161357)
+++ trunk/Source/WebKit2/WebProcess/WebPage/WebPage.h	2014-01-06 19:44:32 UTC (rev 161358)
@@ -411,6 +411,8 @@
     void elementDidBlur(WebCore::Node*);
     void requestAutocorrectionData(const String& textForAutocorrection, uint64_t callbackID);
     void applyAutocorrection(const String& correction, const String& originalText, uint64_t callbackID);
+    void requestAutocorrectionContext(uint64_t callbackID);
+    void getAutocorrectionContext(String& beforeText, String& markedText, String& selectedText, String& afterText, uint64_t& location, uint64_t& length);
     void insertText(const String& text, uint64_t replacementRangeStart, uint64_t replacementRangeEnd);
     void setComposition(const String& text, Vector<WebCore::CompositionUnderline> underlines, uint64_t selectionStart, uint64_t selectionEnd);
     void confirmComposition();

Modified: trunk/Source/WebKit2/WebProcess/WebPage/WebPage.messages.in (161357 => 161358)


--- trunk/Source/WebKit2/WebProcess/WebPage/WebPage.messages.in	2014-01-06 19:29:11 UTC (rev 161357)
+++ trunk/Source/WebKit2/WebProcess/WebPage/WebPage.messages.in	2014-01-06 19:44:32 UTC (rev 161358)
@@ -48,6 +48,8 @@
     ExtendSelection(uint32_t granularity)
     RequestAutocorrectionData(String textForAutocorrection, uint64_t callbackID)
     ApplyAutocorrection(String correction, String originalText, uint64_t callbackID)
+    RequestAutocorrectionContext(uint64_t callbackID)
+    GetAutocorrectionContext() -> (String beforeContext, String markedText, String selectedText, String afterContext, uint64_t location, uint64_t length) 
     InsertText(String text, uint64_t replacementRangeStart, uint64_t replacementRangeEnd)
     SetComposition(String text, Vector<WebCore::CompositionUnderline> underlines, uint64_t selectionStart, uint64_t selectionEnd)
     ConfirmComposition()

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


--- trunk/Source/WebKit2/WebProcess/WebPage/ios/WebPageIOS.mm	2014-01-06 19:29:11 UTC (rev 161357)
+++ trunk/Source/WebKit2/WebProcess/WebPage/ios/WebPageIOS.mm	2014-01-06 19:44:32 UTC (rev 161358)
@@ -722,6 +722,87 @@
     send(Messages::WebPageProxy::StringCallback(correction, callbackID));
 }
 
+static void computeAutocorrectionContext(Frame& frame, String& contextBefore, String& markedText, String& selectedText, String& contextAfter, uint64_t& location, uint64_t& length)
+{
+    RefPtr<Range> range;
+    VisiblePosition startPosition = frame.selection().selection().start();
+    VisiblePosition endPosition = frame.selection().selection().end();
+    location = NSNotFound;
+    length = 0;
+    const unsigned minContextWordCount = 3;
+    const unsigned minContextLenght = 12;
+    const unsigned maxContextLength = 30;
+
+    if (frame.selection().isRange())
+        selectedText = plainText(frame.selection().selection().toNormalizedRange().get());
+
+    if (frame.editor().hasComposition()) {
+        range = Range::create(*frame.document(), frame.editor().compositionRange()->startPosition(), startPosition);
+        String markedTextBefore;
+        if (range)
+            markedTextBefore = plainText(range.get());
+        range = Range::create(*frame.document(), endPosition, frame.editor().compositionRange()->endPosition());
+        String markedTextAfter;
+        if (range)
+            markedTextAfter = plainText(range.get());
+        markedText = markedTextBefore + selectedText + markedTextAfter;
+        if (!markedText.isEmpty()) {
+            location = markedTextBefore.length();
+            length = selectedText.length();
+        }
+    } else {
+        if (startPosition != startOfEditableContent(startPosition)) {
+            VisiblePosition currentPosition = startPosition;
+            VisiblePosition previousPosition;
+            unsigned totalContextLength = 0;
+            for (unsigned i = 0; i < minContextWordCount; ++i) {
+                if (contextBefore.length() >= minContextLenght)
+                    break;
+                previousPosition = startOfWord(positionOfNextBoundaryOfGranularity(currentPosition, WordGranularity, DirectionBackward));
+                if (previousPosition.isNull())
+                    break;
+                String currentWord = plainText(Range::create(*frame.document(), previousPosition, currentPosition).get());
+                totalContextLength += currentWord.length();
+                if (totalContextLength >= maxContextLength)
+                    break;
+                currentPosition = previousPosition;
+            }
+            if (currentPosition.isNotNull() && currentPosition != startPosition) {
+                contextBefore = plainText(Range::create(*frame.document(), currentPosition, startPosition).get());
+                if (atBoundaryOfGranularity(currentPosition, ParagraphGranularity, DirectionBackward))
+                    contextBefore = ASCIILiteral("\n ") + contextBefore;
+            }
+        }
+
+        if (endPosition != endOfEditableContent(endPosition)) {
+            VisiblePosition nextPosition;
+            if (!atBoundaryOfGranularity(endPosition, WordGranularity, DirectionForward) && withinTextUnitOfGranularity(endPosition, WordGranularity, DirectionForward))
+                nextPosition = positionOfNextBoundaryOfGranularity(endPosition, WordGranularity, DirectionForward);
+            if (nextPosition.isNotNull())
+                contextAfter = plainText(Range::create(*frame.document(), endPosition, nextPosition).get());
+        }
+    }
+}
+
+void WebPage::requestAutocorrectionContext(uint64_t callbackID)
+{
+    String contextBefore;
+    String contextAfter;
+    String selectedText;
+    String markedText;
+    uint64_t location;
+    uint64_t length;
+
+    computeAutocorrectionContext(m_page->focusController().focusedOrMainFrame(), contextBefore, markedText, selectedText, contextAfter, location, length);
+
+    send(Messages::WebPageProxy::AutocorrectionContextCallback(contextBefore, markedText, selectedText, contextAfter, location, length, callbackID));
+}
+
+void WebPage::getAutocorrectionContext(String& contextBefore, String& markedText, String& selectedText, String& contextAfter, uint64_t& location, uint64_t& length)
+{
+    computeAutocorrectionContext(m_page->focusController().focusedOrMainFrame(), contextBefore, markedText, selectedText, contextAfter, location, length);
+}
+
 void WebPage::elementDidFocus(WebCore::Node* node)
 {
     m_assistedNode = node;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to