Modernize WebKit::AttributedString a little bit
authortimothy_horton@apple.com <timothy_horton@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 1 Mar 2019 21:37:33 +0000 (21:37 +0000)
committertimothy_horton@apple.com <timothy_horton@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 1 Mar 2019 21:37:33 +0000 (21:37 +0000)
https://bugs.webkit.org/show_bug.cgi?id=195200

Reviewed by Wenson Hsieh.

* Shared/mac/AttributedString.h:
(WebKit::AttributedString::AttributedString):
(WebKit::AttributedString::operator NSAttributedString * const):
* Shared/mac/AttributedString.mm:
(IPC::ArgumentCoder<WebKit::AttributedString>::encode):
(IPC::ArgumentCoder<WebKit::AttributedString>::decode):
(WebKit::AttributedString::encode const): Deleted.
(WebKit::AttributedString::decode): Deleted.
* UIProcess/Cocoa/WebViewImpl.mm:
(WebKit::WebViewImpl::attributedSubstringForProposedRange):
* WebProcess/WebPage/mac/WebPageMac.mm:
(WebKit::WebPage::attributedSubstringForCharacterRangeAsync):
Modern coders, and a constructor and cast operator to/from NSAttributedString.
Also adopt those in a few places just for fun.

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

Source/WebKit/ChangeLog
Source/WebKit/Shared/mac/AttributedString.h
Source/WebKit/Shared/mac/AttributedString.mm
Source/WebKit/UIProcess/Cocoa/WebViewImpl.mm
Source/WebKit/WebProcess/WebPage/mac/WebPageMac.mm

index c5b3217..eecd31a 100644 (file)
@@ -1,3 +1,25 @@
+2019-03-01  Tim Horton  <timothy_horton@apple.com>
+
+        Modernize WebKit::AttributedString a little bit
+        https://bugs.webkit.org/show_bug.cgi?id=195200
+
+        Reviewed by Wenson Hsieh.
+
+        * Shared/mac/AttributedString.h:
+        (WebKit::AttributedString::AttributedString):
+        (WebKit::AttributedString::operator NSAttributedString * const):
+        * Shared/mac/AttributedString.mm:
+        (IPC::ArgumentCoder<WebKit::AttributedString>::encode):
+        (IPC::ArgumentCoder<WebKit::AttributedString>::decode):
+        (WebKit::AttributedString::encode const): Deleted.
+        (WebKit::AttributedString::decode): Deleted.
+        * UIProcess/Cocoa/WebViewImpl.mm:
+        (WebKit::WebViewImpl::attributedSubstringForProposedRange):
+        * WebProcess/WebPage/mac/WebPageMac.mm:
+        (WebKit::WebPage::attributedSubstringForCharacterRangeAsync):
+        Modern coders, and a constructor and cast operator to/from NSAttributedString.
+        Also adopt those in a few places just for fun.
+
 2019-03-01  Youenn Fablet  <youenn@apple.com>
 
         Serialize IndexedDB::ObjectStoreOverwriteMode as an enum
index 37e23f0..ea1cd3e 100644 (file)
  * THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#ifndef AttributedString_h
-#define AttributedString_h
+#pragma once
 
+#include "ArgumentCoders.h"
 #include <wtf/RetainPtr.h>
 
 OBJC_CLASS NSAttributedString;
 
-namespace IPC {
-class Decoder;
-class Encoder;
-}
-
 namespace WebKit {
 
 struct AttributedString {
-    void encode(IPC::Encoder&) const;
-    static bool decode(IPC::Decoder&, AttributedString&);
+    AttributedString()
+    {
+    }
+
+#if defined(__OBJC__)
+    AttributedString(NSAttributedString *attributedString)
+        : string(attributedString)
+    {
+    }
+
+    operator NSAttributedString *() const
+    {
+        return string.get();
+    }
+#endif
     
     RetainPtr<NSAttributedString> string;
 };
 
 }
 
-#endif // AttributedString_h
+namespace IPC {
+template<> struct ArgumentCoder<WebKit::AttributedString> {
+    static void encode(Encoder&, const WebKit::AttributedString&);
+    static Optional<WebKit::AttributedString> decode(Decoder&);
+};
+}
+
index d6fb840..275a4f1 100644 (file)
 #import "config.h"
 #import "AttributedString.h"
 
+#import "ArgumentCodersCocoa.h"
 #import "ArgumentCodersMac.h"
-#import "Decoder.h"
-#import "Encoder.h"
+#import <Foundation/Foundation.h>
 
-namespace WebKit {
+namespace IPC {
 
-void AttributedString::encode(IPC::Encoder& encoder) const
+void ArgumentCoder<WebKit::AttributedString>::encode(Encoder& encoder, const WebKit::AttributedString& attributedString)
 {
-    encoder << static_cast<bool>(!string);
-    if (!string)
+    encoder << static_cast<bool>(!attributedString.string);
+    if (!attributedString.string)
         return;
-    IPC::encode(encoder, string.get());
+    IPC::encode(encoder, attributedString.string.get());
 }
 
-bool AttributedString::decode(IPC::Decoder& decoder, AttributedString& attributedString)
+Optional<WebKit::AttributedString> ArgumentCoder<WebKit::AttributedString>::decode(Decoder& decoder)
 {
     bool isNull;
     if (!decoder.decode(isNull))
-        return false;
+        return WTF::nullopt;
     if (isNull)
-        return true;
-    return IPC::decode(decoder, attributedString.string);
+        return WebKit::AttributedString { };
+
+    RetainPtr<NSAttributedString> attributedString;
+    IPC::decode(decoder, attributedString);
+    if (!attributedString)
+        return WTF::nullopt;
+
+    return WebKit::AttributedString { attributedString.get() };
 }
 
 }
index 7da7358..d48d6cc 100644 (file)
@@ -4860,8 +4860,9 @@ void WebViewImpl::attributedSubstringForProposedRange(NSRange proposedRange, voi
             completionHandlerBlock(0, NSMakeRange(NSNotFound, 0));
             return;
         }
-        LOG(TextInput, "    -> attributedSubstringFromRange returned %@", [string.string.get() string]);
-        completionHandlerBlock([[string.string.get() retain] autorelease], actualRange);
+        NSAttributedString *attributedString = string;
+        LOG(TextInput, "    -> attributedSubstringFromRange returned %@", [attributedString string]);
+        completionHandlerBlock([[attributedString retain] autorelease], actualRange);
     });
 }
 
index b32ca15..82f1206 100644 (file)
@@ -340,24 +340,21 @@ void WebPage::insertDictatedTextAsync(const String& text, const EditingRange& re
 
 void WebPage::attributedSubstringForCharacterRangeAsync(const EditingRange& editingRange, CallbackID callbackID)
 {
-    AttributedString result;
-
     Frame& frame = m_page->focusController().focusedOrMainFrame();
 
     const VisibleSelection& selection = frame.selection().selection();
     if (selection.isNone() || !selection.isContentEditable() || selection.isInPasswordField()) {
-        send(Messages::WebPageProxy::AttributedStringForCharacterRangeCallback(result, EditingRange(), callbackID));
+        send(Messages::WebPageProxy::AttributedStringForCharacterRangeCallback({ }, EditingRange(), callbackID));
         return;
     }
 
     RefPtr<Range> range = rangeFromEditingRange(frame, editingRange);
     if (!range) {
-        send(Messages::WebPageProxy::AttributedStringForCharacterRangeCallback(result, EditingRange(), callbackID));
+        send(Messages::WebPageProxy::AttributedStringForCharacterRangeCallback({ }, EditingRange(), callbackID));
         return;
     }
 
-    result.string = editingAttributedStringFromRange(*range, IncludeImagesInAttributedString::No);
-    NSAttributedString* attributedString = result.string.get();
+    NSAttributedString *attributedString = editingAttributedStringFromRange(*range, IncludeImagesInAttributedString::No);
     
     // WebCore::editingAttributedStringFromRange() insists on inserting a trailing
     // whitespace at the end of the string which breaks the ATOK input method.  <rdar://problem/5400551>
@@ -365,18 +362,18 @@ void WebPage::attributedSubstringForCharacterRangeAsync(const EditingRange& edit
     if ([attributedString length] > editingRange.length) {
         ASSERT([attributedString length] == editingRange.length + 1);
         ASSERT([[attributedString string] characterAtIndex:editingRange.length] == '\n' || [[attributedString string] characterAtIndex:editingRange.length] == ' ');
-        result.string = [attributedString attributedSubstringFromRange:NSMakeRange(0, editingRange.length)];
+        attributedString = [attributedString attributedSubstringFromRange:NSMakeRange(0, editingRange.length)];
     }
 
-    EditingRange rangeToSend(editingRange.location, [result.string length]);
+    EditingRange rangeToSend(editingRange.location, attributedString.length);
     ASSERT(rangeToSend.isValid());
     if (!rangeToSend.isValid()) {
         // Send an empty EditingRange as a last resort for <rdar://problem/27078089>.
-        send(Messages::WebPageProxy::AttributedStringForCharacterRangeCallback(result, EditingRange(), callbackID));
+        send(Messages::WebPageProxy::AttributedStringForCharacterRangeCallback(attributedString, EditingRange(), callbackID));
         return;
     }
 
-    send(Messages::WebPageProxy::AttributedStringForCharacterRangeCallback(result, rangeToSend, callbackID));
+    send(Messages::WebPageProxy::AttributedStringForCharacterRangeCallback(attributedString, rangeToSend, callbackID));
 }
 
 void WebPage::fontAtSelection(CallbackID callbackID)